63 Commits

Author SHA1 Message Date
tomFlowee 465aa8630d Move away from deprecated deadline_timer. 2026-04-09 19:25:27 +02:00
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 85a6e20e1a Have better spread checkpoints and adjust the unit test
This updates the unit test to move the tiny chain it tests to match the
new checkpoint location.
2024-07-06 22:00:34 +02:00
tomFlowee 9008b4a312 Update checkpoints
The checkpoints were formerly copied from the full node, so based
on possible fork-events.
That seems less useful nowadays and since we want to be able to
allow starting a blockchain instance from any checkpoint it makes
sense to spread them out more evenly.
Based on the concept that the BCH chain generates around 50K blocks
a year, we simply use that interval.

This removes various very old ones meaning that the total count is
roughly equal.
2024-06-29 20:26:35 +02:00
tomFlowee 0838a4cc93 Add more support for checkpoint based chains.
This improves the docs, adds a getter and last it makes the
blockHeightAtTime() more secure by not returning a value below the
checkpoint. As this would throw if used directly in the block() method.
2024-06-29 11:52:29 +02:00
tomFlowee 4acc8ee883 Fix check for chain integrity. 2024-06-26 21:57:07 +02:00
tomFlowee c2ae326620 More sanity checks for creating partial chain. 2024-06-25 23:01:59 +02:00
tomFlowee 3b91e503ff Fix binary search on smaller chain
When a chain starts from a checkpoint instead of '1', we should limit
our search to that in order to avoid codepaths that throw.
2024-06-25 23:00:51 +02:00
tomFlowee dc3545857d [p2pnet] Add Blockchain::replaceStatichain()
This public method allows an application to replace the static-
header files in a life-situation. Without needing to restart the app.

The idea is that a static chain starts at a checkpoint, and we extend
it into the past to start at an earlier checkpoint. This suddenly
makes available a larger amount of history without changing anything
of the data that was already there.
2024-06-02 21:22:37 +02:00
tomFlowee 5ada583bd1 Fixlets in code and comments. 2024-06-02 11:58:05 +02:00
tomFlowee 7ecd46ba1c Make checkpoints available via method call.
This also adds docs.
And this moves away from the map which isn't worth the code complexity
with the low number of entries.
2024-05-31 13:05:38 +02:00
tomFlowee 422822530b Add Blockchain::dataSources() getter
The blockchain object holds all block-headers, the backing store for
this class is now being exposed more to allow understanding what we have
or not have available. And also what the source of that info is.
2024-05-28 10:51:52 +02:00
tomFlowee 5fec1b94ef Fix off-by-one in headers check.
A headers call that notifies us about a new block, which is identical to
the one we already have as TIP is now recognized as having the same POW.
2024-01-14 12:16:29 +01:00
tomFlowee 678eccfa70 Be smarter at asking for 'headers' from peers 2024-01-04 21:53:16 +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 8e833c0a63 Add versioning and fix compacting
The system writes a new file every single run (provided new headers were
received) and we sometimes compact them into a big file again.
The code forgot to remove the newly introduced info files of the old
files it compacted. Leaving confusing things happening after.

This solves that by making the first run remove all info files and re-
build them, adding a version byte to allow us freedom to do that in the
future again.
2023-11-04 15:52:41 +01:00
tomFlowee 4970557f74 Account for a headers message starting with genesis 2023-11-03 22:09:10 +01:00
tomFlowee 8cf80a8634 Cleanup GetHeaders p2p call
Move the builder into the place its used (leftover from earlier ownership change)
Make the message we build be more compatible.
2023-11-03 22:07:57 +01:00
tomFlowee 457e8cc78a Remove not written info file
We optimistically create a new info file but as we start we might
instantly realize the file is useless and give up before having written
a single byte.
We now remove that file to avoid stale state.

Also be more verbose on warnings.
2023-10-24 15:13:24 +02:00
tomFlowee 51e872152b Redo various pieces of logic to be more sane.
A review of the numbers because this easily becomes off-by-one fest
that can make you get lost for hours! :-)
2023-10-16 20:29:14 +02:00
tomFlowee cbeb6dbb0a Allow a static blocks file to start at a checkpoint
The blockchain class can effectively now be shallow,
we can have a list of blockheaders of, for instance, the last year
alone. It builds on top of a known checkpoint (hardcoded block data)
and as long as we do not access block info that is unavailable, things
just work like normal.

We throw at the request of a blockheader that is too old.
2023-10-09 18:08:10 +02:00
tomFlowee 10034d9f0e Add chain-work to checkpoints.
Also rename the member 'checkpoints' to include the m_ prefix to be
internally consistent.
2023-10-09 11:35:20 +02:00
tomFlowee 35df33f3ab Do not allocate items for static block
This changed the vector to no longer have empty entries for the static
chain which were never inserted or read.
2023-10-09 11:35:20 +02:00
tomFlowee 6377a62baf Blockchain database fixes for compression of data. 2023-10-08 12:15:09 +02:00
tomFlowee 0ae791af1e Make the implementation fit the methodname.
The methodname:
  blockHeightAtTime()
makes the reader think about the actual height of the chain at that
time. Returning that value until the timestamp of the next block is
reached.
Now the method actally returns that number.

The code does not do any work to account for blocks going backwards in
time compared to the block before.
2023-06-14 16:51:23 +02:00
tomFlowee e4d6528428 Fix off by one. 2023-04-18 09:37:17 +02:00
tomFlowee a088a7867a Make GetHeaders replies more relevant
Lets avoid just getting empty answers when we are up-to-date.
2023-04-17 19:24:40 +02:00
tomFlowee 5374a50712 Minor cleanups. 2023-03-27 15:16:54 +02:00
Your Name 0ea51337d2 Minor fixes
Use a simple buffer on stack to read into, instead of a buffer pool.
Small fixes as well.
2022-11-14 13:15:08 +01:00
tomFlowee 46c6febfa1 Make compile on CLANG 2022-11-13 12:20:23 +01:00
tomFlowee 8780f16383 Implement the compressSaveFiles method
Makes the change from yesterday complete: we now
compress all the chunks into one periodically.

This, again, is done with a timeout instead of on-close.
We have 45 seconds as a timeout to balance between need to save and
waiting long enough to get additional headers should it
have been a while since last run.
2022-11-13 12:16:26 +01:00
tomFlowee 06b3d3d491 Save the downloaded block headers into multiple files
This change makes it so that we rely less on the downloaded
blockheader data being only in memory and us saving it when
the client shuts down.
Instead, we save more regularly and we save smaller files
instead of always overwriting the complete downloaded headers.

Having less to save at shutdown is then a happy side-effect.
2022-11-12 23:42:59 +01:00
tomFlowee 5f629a161e Let the actions do the assignments of segments.
This removes some code from the connectionmanager / blockchain classes
which assigned a random segment on a new peer.
2022-07-13 13:50:23 +02:00
tomFlowee 0f434e0425 Check file exists before using it. 2022-05-03 18:50:53 +02:00
tomFlowee 7e3e7c2fb7 Fix testing failure 2022-01-24 12:26:37 +01:00
tomFlowee 9c4984dcf7 [P2Plib] Make initial startup 70% faster
What we did previously is load a static chain which was to avoid lots of
work by simply memory-mapping the data instead of inserting it in a
vector.

This improves upon that by also having a metadata file which holds the
block-hashes directly readable. This avoids us having to iterate over
the blockheaders and hashing them.

Additionally we now have a single chain-work field that is the total
amount added work for the headers. This saves us from doing 256-bit
divisions and work for each blockheader as well.

Result is that we make the statup mostly about memory moving and remove
all CPU intensive stuff. Going from 1450ms to 400ms on desktop.
I expect an even greater gain on mobile CPUs.
2022-01-12 19:45:51 +01:00
tomFlowee d79ab0a301 Special case peers that are behind
A peer that is behind is marked as "on a different chain", which is
ultimately correct, but the response to punish or ban them is too much.
We could very well reconnect later when the situation is resolved.

So, detect when a peer is simply behind and respond by disconnecting
without punishment.
2021-11-01 17:19:00 +01:00
tomFlowee 3841257505 Fix possible deadlock.
The new iteration avoids having the blockchain mutex locked when it
calls up to the connection manager (which locks its own mutex).
2021-11-01 15:52:55 +01:00
tomFlowee a5c4a308f4 We reached 700K, pre-allocate more :) 2021-08-09 21:22:25 +02:00
tomFlowee c8a531e6f8 Comments and fix warning
Statics should be PODs.
2021-06-24 19:10:14 +02:00
tomFlowee 460d4be807 Add new method Blockchain::blockHeightAtTime() 2021-05-28 14:52:27 +02:00
tomFlowee c45e7d9c3e [P2P] Do not save blockchain data when not changed 2021-05-27 19:08:46 +02:00
tomFlowee d1fa3fa4a9 [P2P] Allow static loading of blockheaders
This adds a unit test for the Blockchain class in the p2p lib and this
introduces the functionality to have a blockchain pre-shipped with an
app.
2021-05-27 19:08:46 +02:00
tomFlowee ceb44cc3b5 Remove dead code. 2021-02-05 17:26:40 +01:00
tomFlowee 1f2f7c36d2 Port the boost container to the std one.
The unordered_map is now available in stl, and said to be better,
so lets slowly switch over
2021-02-03 16:31:46 +01:00
tomFlowee 5d6d09e4d6 Assign segments only to good peers
Instead of assigning a privacy segment at connect, wait until we have
determined if this peer is on the same chain as us before we allocate a
peer slot.

This also helps us limit the amount of peers we send our bloom filter to.
2021-02-03 14:41:51 +01:00
tomFlowee adfe4081cb Update copyright notice and email address 2021-02-02 13:08:07 +01:00
TomZ 93148976e9 add the checkpoint to p2pnet 2020-11-15 15:27:32 +01:00
TomZ 37e1f2fcd3 Fix bad usage of create_directories() 2020-11-14 23:59:01 +01:00
TomZ 301a4ca3ee Fix properly following the chain-tip
This is done in several steps:

1. Separate my height from the remote peer heights.
Instead of assuming we have one height, recognize that a peer may
not be at the tip at the same time we are. We monitor headers/invs
to update the 'peerHeight' variable.

2. Ask for merkle blocks from a peer to the maximum height of that
peer (but not later than what we validated to be true).
This avoids us asking past the remotes tip which they didn't like.

3. Redo the SyncSPVAction to use all this and make it much more
reliable in finding peers to download from and getting all the
changes as fast as possible.
2020-11-13 21:03:56 +01:00