Commit Graph

10 Commits

Author SHA1 Message Date
Hodlinator
cc5dda1de3 headerssync: Make HeadersSyncState more flexible and move constants
Move calculated constants from the top of src/headerssync.cpp into src/kernel/chainparams.cpp.

Instead of being hardcoded to mainnet parameters, HeadersSyncState can now vary depending on chain or test. (This means we can reset TARGET_BLOCKS back to the nice round number of 15'000).

Signet and testnets got new HeadersSyncParams constants through temporarily altering headerssync-params.py with corresponding GENESIS_TIME and MINCHAINWORK_HEADERS (based off defaultAssumeValid block height comments, corresponding to nMinimumChainWork). Regtest doesn't have a default assume valid block height, so the values are copied from Testnet 4. Since the constants only affect memory usage, and have very low impact unless dealing with a largely malicious chain, it's not that critical to keep updating them for non-mainnet chains.

GENESIS_TIMEs (UTC):
Testnet3: 1296688602 = datetime(2011, 2, 2)
Testnet4: 1714777860 = datetime(2024, 5, 3)
Signet: 1598918400 = datetime(2020, 9, 1)
2025-09-12 22:28:41 +02:00
Hodlinator
8fd1c2893e test(headerssync): Test returning of pow_validated_headers behavior
Adding these checks necessitates increasing the length of the generated test chains so that we can properly exceed the REDOWNLOAD_BUFFER_SIZE during the test.

One can check out this commit and locally revert the TARGET_BLOCKS value change to prove the need for tests being able to control the buffer size, as is done by the next commit. Beyond the current REDOWNLOAD_BUFFER_SIZE of 15'009 we need 3 extra - 15'012 TARGET_BLOCKS:
* 1 for the genesis block.
* 1 for the test wanting to check that we start receiving headers for permanent storage *before* the final header (first_chain.back()).
* 1 to exceed REDOWNLOAD_BUFFER_SIZE in HeadersSyncState::PopHeadersReadyForAcceptance().

(The release process includes an occasional increase of the REDOWNLOAD_BUFFER_SIZE value, see release-process.md and history of headerssync.cpp).
2025-09-12 22:28:41 +02:00
Hodlinator
7b00643ef5 test(headerssync): headers_sync_chainwork test improvements
Introduces CHECK_RESULT for consistently validating ProcessingResult.
* Verifies HeadersSyncState::State directly after ProcessNextHeaders().
* Uses BOOST_REQUIRE_EQUAL for HeadersSyncState::State - Nicer failure output and prevents continuing test in nonsensical state.
* Encourages checking Locator and result.pow_validated_headers.

Changes happy_path to test both full & non-full headers messages.
2025-09-12 22:28:41 +02:00
Hodlinator
04eeb9578c doc(test): Improve comments
+ new assert helping explain why CHAIN_WORK == TARGET_BLOCKS * 2.
2025-09-12 22:28:41 +02:00
Hodlinator
fe896f8faa refactor(test): Store HeadersSyncState on the stack 2025-09-12 22:28:41 +02:00
Hodlinator
f03686892a refactor(test): Break up headers_sync_state
Helps logically separate the scenarios being tested.

Also adds missing comment for part 4.

(unique_ptrs and ProcessingResults will be cleaned up in next commit).
2025-09-12 22:28:41 +02:00
Hodlinator
e984618d0b refactor(headerssync): Process spans of headers
More lightweight than vectors which needed to be copied in tests. Also good to get rid of headers_batch-vector before breaking up test.
2025-09-12 22:28:41 +02:00
Hodlinator
a4ac9915a9 refactor(headerssync): Extract test constants ahead of breakup into functions
Made arith_uint256 constexpr-constructible so it can be used for compile time constants.

Co-authored-by: Lőrinc <pap.lorinc@gmail.com>
2025-09-12 22:28:41 +02:00
MarcoFalke
fa05a726c2 tidy: modernize-use-emplace 2023-10-12 11:27:19 +02:00
Suhas Daftuar
0b6aa826b5 Add unit test for HeadersSyncState 2022-08-29 08:10:35 -04:00