From 1883cecb4d05788a02d673b1c7541d93fb9af2c7 Mon Sep 17 00:00:00 2001 From: Hodlinator <172445034+hodlinator@users.noreply.github.com> Date: Wed, 19 Aug 2026 09:41:04 +0200 Subject: [PATCH] test: Characterize lagging-clock headers presync MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The node currently continues low-work headers presync and requests more headers when its clock is more than `MAX_FUTURE_BLOCK_TIME` behind the chain-start MTP. Record this behavior before the follow-up rejects the invalid elapsed-time calculation. The unit test covers HeadersSyncState() behavior while the functional test covers net_processing.cpp behavior. Co-authored-by: Lőrinc --- src/test/headers_sync_chainwork_tests.cpp | 13 +++++++++++++ .../p2p_headers_sync_with_minchainwork.py | 8 ++++++++ 2 files changed, 21 insertions(+) diff --git a/src/test/headers_sync_chainwork_tests.cpp b/src/test/headers_sync_chainwork_tests.cpp index bba612f8b46..e18b6f4a027 100644 --- a/src/test/headers_sync_chainwork_tests.cpp +++ b/src/test/headers_sync_chainwork_tests.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include @@ -252,4 +253,16 @@ BOOST_AUTO_TEST_CASE(too_little_work) /*exp_locator_hash=*/std::nullopt); } +BOOST_AUTO_TEST_CASE(system_clock_lagging_behind_chain_start) +{ + FakeNodeClock clock{(chain_start.GetBlockTime() - MAX_FUTURE_BLOCK_TIME) * 1s}; + BOOST_CHECK_NO_THROW(CreateState()); + + clock -= 1s; + // TODO: Fix - Being more than MAX_FUTURE_BLOCK_TIME behind the starting + // block leads HeadersSyncState() to compute a negative max_seconds_since_start + // which leads to very high HeadersSyncState::m_max_commitments. + BOOST_CHECK_NO_THROW(CreateState()); +} + BOOST_AUTO_TEST_SUITE_END() diff --git a/test/functional/p2p_headers_sync_with_minchainwork.py b/test/functional/p2p_headers_sync_with_minchainwork.py index 1dc38faadb2..cd46a82c2a1 100755 --- a/test/functional/p2p_headers_sync_with_minchainwork.py +++ b/test/functional/p2p_headers_sync_with_minchainwork.py @@ -15,6 +15,7 @@ from test_framework.messages import ( ) from test_framework.blocktools import ( + MAX_FUTURE_BLOCK_TIME, NORMAL_GBT_REQUEST_PARAMS, create_block, ) @@ -144,6 +145,13 @@ class RejectLowDifficultyHeadersTest(BitcoinTestFramework): # getpeerinfo should show a sync in progress assert_equal(node.getpeerinfo()[0]['presynced_headers'], 2000) + self.log.info("Test whether a lagging clock aborts low-work headers sync") + node.disconnect_p2ps() + node.setmocktime(node.getblockheader(node.getblockhash(0))['mediantime'] - MAX_FUTURE_BLOCK_TIME - 1) + p2p = node.add_p2p_connection(P2PInterface()) + p2p.send_without_ping(headers_message) + p2p.wait_for_getheaders(timeout=30, block_hash=hashPrevBlock) # TODO: A negative elapsed interval should trigger fatal shutdown. + def test_large_reorgs_can_succeed(self): self.log.info("Test that a 2000+ block reorg, starting from a point that is more than 2000 blocks before a locator entry, can succeed")