Commit Graph

78 Commits

Author SHA1 Message Date
TomZ 477a536e25 [UTXO] UnitTest rollback-to-checkpoint code 2020-11-17 20:57:32 +01:00
TomZ 089145a3a6 Delete copy constructor 2020-11-17 16:04:45 +01:00
TomZ 36a0bb10f2 Improve log info. 2020-11-10 17:28:40 +01:00
TomZ e9a2437c2d Add unit tests for UTXO and fixes
This fixes that the .info file is sometimes not written when nothing
changed in the db file.

This fixes that the info file with .1.info extension is reused again
and again, effectively reducing the usefulness of the snapshots.
This only really affects the indexer as pruning avoids this most
of the time.

Closes issue flowee-issues#6
2020-05-18 22:51:41 +02:00
TomZ e9ef907c1f Sync checkpoint save and index-db saveCaches
When the UTXO saved checkpoints this change makes sure we also store the
index-db changes.

Since we stopped saving simple state changes fromt the index-db, the
only real data we save is the 'undo-block-index', as such this will be
relatively cheap to save.
Without an undo block position we will currently fail to verify those
blocks and as such it is useful to save all at the same time to actually
have a state we can start from.
2020-03-05 23:29:53 +01:00
TomZ 4f077da5a6 remove assert(true) 2020-03-05 23:29:53 +01:00
TomZ bd58ac420a Avoid reuse (shadow) of variable 2020-03-05 23:29:53 +01:00
TomZ d9604e3f33 Design a better detection for need of UTXO-GC
Doing a garbage collect of the Sha256 based databases means we remove
all the records that have been deleted from our file.
We also sort the file to have all the jump-tables at the end, making it
much cheaper on memory-locality to find (or not) items in the DB.

The downsides are that this prune step takes time, writes dozens of MBs
and that we lose checkpoints. The latter means we no longer can rollback
to a safe position, simply because we flushed those records.

So we want to do this often enough to avoid fragmentation but not too
often because it creates a greater risk on data consistency.

This algoritm checks the actual data and calculates the fragmentation of
the jump-tables to decide if we want to start a GC.

When we do GC, we try to do as many files as makes sense, to make sure we
can wait quite a long time before we need to do a new GC.
2020-03-05 23:29:52 +01:00
TomZ a5a2aef53e Increase the amount of checkpoints from 5 to 15.
Now we can revert much cheaper to an earlier checkpoint it becomes much
more important that we actually keep them around to be usable.
2020-03-05 23:18:42 +01:00
TomZ 1141de5c13 Cleanups 2020-02-28 00:34:12 +01:00
TomZ 9a3dde8400 Make sure the UTXO doesn't write a new info file when there are no changes 2020-02-28 00:34:12 +01:00
TomZ 60d371afa7 Make loading find a functional UTXO state
When, on loading, the blockindex and the UTXO don't agree then try to find an older UTXO
state where they do agree.
The most typical state issue is where a block stored in the blocksdb is not available in
the index due to corruption or similar.
2020-02-28 00:34:08 +01:00
TomZ 489a9a4859 Move nStatus of blockindex into the UTXO
To allow the UTXO to actually use the power of checkpoints we need to
make sure that the block-validation state is not stored separately from
it.
The goal is that when we have some curruption we can just go back to an
earlier state of the UTXO and re-validate the blocks to get to the
current tip. The often seen problem is that corruption will instead
leave the block-index (leveldb) with an incorrect state so the replay
fails.

This change solves that by no longer reading the block-validation-state
and no longer writing it on a state change.
2020-02-28 00:33:04 +01:00
TomZ 8057429dfa Re-do the UTXO garbage collect planning algo
The UTXO keeps outdated records around in an append-only file, which
means we need to do a garbage collect regularly.
This new algo uses the commulatative amount of changes since last GC
(aka prune) as an indicator to plan a new one.

The effect should be much smaller files to keep in memory and the data and
jump tables being much more localized which should result in higher
throughput.
2020-02-23 11:15:55 +01:00
TomZ 86d580463a Make selection of UTXO info files better
This avoids some cornercases.
2020-02-22 13:43:16 +01:00
TomZ 175096b2bd Refactor: move files
Move some files back to the server "library".
Merge the 'console' lib with server, as it doesn't really make sense with
just one file and nobody exclusively linking to it.

The server "libary" is not really a library, its the place we put all
the files shared by hub-qt hub-cli and hub.
We no longer depend on these files from other places (mostly due to
moving to the new logging framework) and as such we can move the files
back.
2019-11-13 19:09:24 +01:00
TomZ cf4eca26ba Merge branch '2019.06' 2019-06-22 13:02:11 +02:00
TomZ 21ce0163f7 Fix possible threading issue, use an atomic
The changecount integer was updated in two different threads,
so it needs to be an atomic to be safe.
2019-06-22 12:37:01 +02:00
TomZ 49b6f802a5 Make loglines less confusing
Now multiple different targets use this database, the messages
were confusing. Next to that "pruning" is not really correct
and it lead to confusing. More correct is the term GC.
2019-06-22 10:59:37 +02:00
TomZ c0752a944c Fix sometimes not being able to load a UTXO
In the rare case when we already created a new DB file, but never
managed to write a .info file, the constructor would end up rejecting
the whole DB.
This just drops off the last useless file and goes from there.
2019-06-19 21:49:04 +02:00
TomZ 5ca03cddd5 Futher improve how often save is called
Realize that the changes made before committing are changes I'm
not allowed to save yet and as such move scheduling to the commit()
method.
2019-06-15 21:36:52 +02:00
TomZ 230611b98e Tweak the numbers to be better on IBD 2019-06-12 23:53:24 +02:00
TomZ 840cd5a913 Fix change-counting and saving in UTXO
Fix a bug that could make the wait between saves too long and now aim to
make the saving more often because this helps us determine in time that
the file is full so we don't go over the end.
2019-06-11 18:59:51 +02:00
TomZ ac1a3db905 Write new variable to UTXO-DB info files
Write a changesSincePruning integer which has the following effects;
after a restart;
* We will do a prune faster and/or more often for nodes that are not
  running very long stretches of time.
* We will skip pruning files that don't need it.

Context; pruning of the UTXO is throwing away already spent outputs
that are still in our dirty files.
2019-04-10 23:59:10 +02:00
TomZ 6db08ef9f8 Also prune the first file 2019-04-03 14:33:56 +02:00
TomZ 67d2019434 Have a special exception for UTXO internal errors
This avoids an internal error from failing one block due
to a perceived missing input while the block may be fine
but other issues need looking at (for instance disk errors).
2019-04-02 18:40:56 +02:00
TomZ 3e3c4859fe minor bugfix
Make sure we count changes correctly (off-by-one)
2019-03-31 19:53:53 +02:00
TomZ 0264e56551 Second part of 'file full' utxo detection
Do checks during insertAll for those really large blocks.
2019-03-25 12:50:27 +01:00
TomZ 7e8ce613bb Fix regression, failure on shutdown 2019-03-24 21:39:41 +01:00
TomZ b0e57ffd7a Make UTXO able to handle much larger blocks.
Checking if my database file is full can not be assumed to only happen
on end-of-block if blocks get really large.
I'm taking a small performance hit to check this on insert and
flushSomeNodesToDisk on a more regular basis.
2019-03-24 10:23:52 +01:00
TomZ 54c565fdd8 DB files start at 1 2019-03-23 19:24:20 +01:00
TomZ cfe690320d Move various standalone simple classes into utils
The 'server' library has always been a catch-all and
ideally only the hub links it in (far future goal).
In line with this I move a list of files out of server
into the utils lib.
I choose 'utils' because all these are plain old data
objects that many crypto apps will find useful.

now in utils/primitives/
* CScript
* CPubKey
* CTransaction
* CBlock
* FastTransaction
* FastBlock
* CScript

streams.h is now in utils/streaming/
hash.h is now in utils/
2019-03-11 15:40:42 +01:00
TomZ 1f779a3d9b For the UTXO set the no-CoW flag
This is really only relevant for people using btrfs on Linux, we will
set the utxo dir to be no-copy-on-write which causes all new databases
to follow this flag and be faster due to lack of copying on write.
2019-03-06 15:11:42 +01:00
TomZ a09339dc05 Applying lessons learned for lock-free programming
The reason there are no standard library versions of lock-free
containers is because you want to always take full advantage of
the details in question.
In this case (read millions of times for each modification) it makes
no sense to use anything other than a standard container, but put in
a copy-on-write block. Simple and easy.
2019-03-05 18:52:49 +01:00
TomZ bc53fe2d4b Make this a little less confusing; use BCH not BTC 2019-03-04 12:46:26 +01:00
TomZ 31650b7340 Squashed commit of the utxo-lockfree-map branch
Replace the m_buckets unsorted map with a lock-free version
based on atomic pointers. (BucketMap)
remove the m_leafs and move those into the bucket struct.
Make the access to the jumptable transactional to avoid one big lock
over all datastructures.

On my threadripper 2990WX the entire 150GB BCH blockchain was
parsed and imported in under 3 hours.
2019-02-23 15:33:22 +01:00
TomZ 3c13ff18e4 Make exceptions have a bit more context 2019-02-18 18:31:17 +01:00
TomZ c09a5f94f6 Simplify and avoid race-conditions 2019-02-06 16:30:25 +01:00
TomZ 5747a3b888 Change m_flushScheduled to be an atomic 2019-02-03 19:02:53 +01:00
TomZ 775a06b4d1 Make optimization do what the comment says
The optimization was wrongly implemented making it worse
instead of better. This fixes that.
2019-02-03 19:00:55 +01:00
TomZ f790960a97 Another tag.
At least 50% of the UTXO entries reuse the posOnDisk
because they are outputs on the same tx. This changes
such entries from using 2 bytes to 1 byte only when saved
in a bucket.
2018-12-07 20:59:16 +01:00
TomZ 121895df1e Make UTXO DB logging less noisy 2018-11-24 11:50:21 +01:00
TomZ c6297727b8 Change the inserting to be in batches 2018-11-21 20:20:31 +01:00
TomZ 9d5f9d2142 Fix UTXO throttling feature
On low core-count machines the request to save could end up being
delayed since all threads are busy doing things like validating
transactions.
So when a really big block came in I ended up throttling while there
was no save method running in parallel at all.
This fixes this and also makes throtteling be a per-thread thing
instead of doing it inside the mutex.
2018-11-18 14:42:14 +01:00
TomZ 71b45cd38c Merge branch 'dev/HFNov2018' 2018-11-15 23:40:56 +01:00
TomZ cdec1fa593 Implement CTOR validation 2018-11-15 23:35:43 +01:00
TomZ d55c7bfed8 Make UTXO write timebased as well
The current system writes based on the amount of changes, which is a bit
risky as long as we have small blocks as the amount of changes may for
hours be below the "lets write" limit.

So also write every 5 minutes, just to make sure we won't keep data in
memory longer than needed. Also this allows pruning to happen for people
that don't run their node all the time.
2018-11-11 16:47:54 +01:00
TomZ 89eee0d333 Fix semi-mem-leak in the UTXO DB
we only save a subsection of stuff in memory on each round of saving,
but I set the counter to zero on how long to wait for the next save.
This makes us save more often till we actually saved most stuff, avoiding
buildups of caches until we do the periodic big flush.
2018-11-11 16:47:45 +01:00
TomZ fdd693541e Make the UTXO have some limits "configurable"
This change makes the limits be static variables, which means they are
only settable once for a process. The only usecase so far is to use much
smaller limits in testing situations.
2018-10-21 13:21:24 +02:00
TomZ d8adc57786 Make pruning atomic swap the old for the new DB
This allows queries like RPC and mempool-accept to continue
even during a prune (we just use the 'old' DB) and when its
done the last active query will delete the old DB.

This assumes a sane filesystem where you can rename a file that
is in use.
2018-10-21 10:52:40 +02:00