47 Commits

Author SHA1 Message Date
tomFlowee bb7275466b Stop using deprecated asio io_service
This ports the io-service to the source compatible io-context
class, with the most work going to the WorkerThreads which owns
that one.
2025-02-08 19:05:26 +01:00
tomFlowee 2bb858b62d Tweak UTXO numbers
As the "normal" machine that runs this kind of software gets more
memory, it is Ok to increase the numbers in order to aim to keep more
data cached in memory between check-points, and have less check-points.

This mostly helps speeding up initial block download, it also helps keeping
the utxo datadir small and non-fragmented. All good for speed.
2024-04-07 20:19:46 +02:00
tomFlowee 4fe9d69e7b Use more specific define-guards 2024-01-24 21:03:58 +01:00
tomFlowee bc47a700a4 Refactor; wrap BufferPool in shared_ptr
As we moved most of the creation of a BufferPool to be via the
Streaming::pool() method, which uses a thread-local, it makes sense
to start cleaning up the design and make it more modern C++.
The above mentioned method would return a reference and you'd see
loads of places use `auto &pool =` which is less than ideal.

As the number of places where we actually instantiate a BufferPool
goes down, the usage of some sort of smart pointer makes more sense.

This now makes all APIs use BufferPool be wrapped in a shared_ptr.
2023-12-21 15:23:23 +01:00
tomFlowee 1905789c9b Use the global instead of a hardcodd value 2023-12-19 23:47:29 +01:00
tomFlowee ae78dd0bb8 Update email address 2021-06-20 22:44:44 +02:00
tomFlowee f65cae8b0f More code to make static checkers happy 2020-12-24 14:14:22 +01:00
TomZ 477a536e25 [UTXO] UnitTest rollback-to-checkpoint code 2020-11-17 20:57:32 +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 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 4a6845ff76 Fixlets from static code inspector 2019-09-12 15:24:03 +02:00
TomZ df7de2ecd2 Clean up the hairy ball of util.cpp
Move some globals and all stuff pulling in crypto to the
server/serverutil.cpp file

Remove dead code.

Move several items that were used in only one place to the respective
files using them.

Move the class WaitUntilFinishedHelper into its own file.

Made sure no header includes utils.h
remove a handfull of files including utils.h for nothing.
2019-08-24 22:25:09 +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 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 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 ef5dafe6e0 Add private header warnings 2019-04-06 12:28:34 +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 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 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 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 5747a3b888 Change m_flushScheduled to be an atomic 2019-02-03 19:02:53 +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 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 cdec1fa593 Implement CTOR validation 2018-11-15 23:35:43 +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
TomZ 3fbc2e89f1 Speed up UTXO, replace mutex with lock-free list 2018-10-08 22:30:21 +02:00
TomZ a28ffd18bb Write less txid's to the DB file and write shorter ones.
When pruning we sort leafs by txid / output and refrain from
writing the txid repeatedly for outputs of the same tx.
Additionally, use the fact that we only ever get to a leaf via
a bucket and since the first 64 bits of a txid is there, skip
repeating them when writing to disk.
Last, make pruning have different strategies.

This should shrink the utxo by about 40%
2018-09-24 21:24:28 +02:00
TomZ e7e4a43084 Make UTXO auto-prune 2018-09-20 22:03:44 +02:00
TomZ 13c2a64f56 Identify another usecase of utxo rollback and fix
Including unit test
2018-09-20 21:55:07 +02:00
TomZ 88e5e829a5 Re-do the UTXO rollback functionality
this is a much less memory-intensive version as it doesn't copy
entire buckets anymore.
2018-09-20 21:55:07 +02:00
TomZ bf370f121d Make pruning work 2018-09-20 21:55:07 +02:00
TomZ 6c0f39317f Copy some data in the prune command 2018-09-20 21:55:07 +02:00
TomZ 6c68f9df12 Updates to UnspentOutputDatabase classes
* remove unused code
* reorg code to make the access to disk be outside the mutex
* add detection of slow disk-writes and slow down data coming in
* Update and fix the rmHint
2018-09-05 11:16:29 +02:00
TomZ c17be105cf Rename variables for clarity of what they do 2018-08-30 18:44:12 +02:00
TomZ eee7140368 Fix the loading and saving of the info files in case of corruption
If there is corruption in one file, we need to rollback all of them
to a stable point where the entire DB agrees
2018-08-06 12:00:14 +02:00
TomZ d5d21d214d Make the saving more fool-proof 2018-07-30 22:12:50 +02:00
TomZ 0bfa3cbb85 Introduce rollback() on UnspentOutputDB
together with blockFinished() as 'commit' this introduces the ability
to modify the in-memory utxo which can be rolled-back with ease.

This allows us to "modify" the utxo while validating a block in an
optimistic manner and only spend extra resources doing a rollback
should the block end up not being Ok.
2018-07-29 18:03:49 +02:00
TomZ 1600086ede Updates to and start using the UnspentOutputDB
The new UnspentOutputDatabase classes are only very loosly a database, we
purely register and store unspent outputs there. But unline a DB we don't
allow modification (just insert and delete).

This replaces the coin-db (which was based on leveldb) and as first goal
this gives us a higher level of stability. The level-database was known
to give corruption issues.

Notice that users will need to do a manual reindex on first update.
2018-07-23 19:49:32 +02:00
TomZ f0f07153fa Remove unused tag 2018-05-10 01:25:50 +02:00
TomZ 1ac5d85f18 Implement TODO 2018-05-10 01:10:22 +02:00
TomZ c368c6ef77 Create new UTXO database library
The old was based on levelDB which has scalability issues and stability
issues. (most often cited problem is corrupted database..)

This unspent output database I wrote is based on the idea that we need
never actually update any rows, which makes most old fashioned databases
a bad fit.
All we do is create rows and we forget rows. So lets design a DB to fit
that pattern.
2018-05-09 10:48:16 +02:00