6 #include <chainparams.h> 13 #include <validation.h> 18 #include <boost/test/unit_test.hpp> 23 #define CHECK_RESULT(result_expression, hss, exp_state, exp_success, exp_request_more, \ 24 exp_headers_size, exp_pow_validated_prev, exp_locator_hash) \ 26 const auto result{result_expression}; \ 27 BOOST_REQUIRE_EQUAL(hss.GetState(), exp_state); \ 28 BOOST_CHECK_EQUAL(result.success, exp_success); \ 29 BOOST_CHECK_EQUAL(result.request_more, exp_request_more); \ 30 BOOST_CHECK_EQUAL(result.pow_validated_headers.size(), exp_headers_size); \ 31 const std::optional<uint256> pow_validated_prev_opt{exp_pow_validated_prev}; \ 32 if (pow_validated_prev_opt) { \ 33 BOOST_CHECK_EQUAL(result.pow_validated_headers.at(0).hashPrevBlock, pow_validated_prev_opt); \ 35 BOOST_CHECK_EQUAL(exp_headers_size, 0); \ 37 const std::optional<uint256> locator_hash_opt{exp_locator_hash}; \ 38 if (locator_hash_opt) { \ 39 BOOST_CHECK_EQUAL(hss.NextHeadersRequestLocator().vHave.at(0), locator_hash_opt); \ 41 BOOST_CHECK_EQUAL(exp_state, State::FINAL); \ 46 constexpr arith_uint256 CHAIN_WORK{TARGET_BLOCKS * 2}; 48 // Subtract MAX_HEADERS_RESULTS (2000 headers/message) + an arbitrary smaller 49 // value (123) so our redownload buffer is well below the number of blocks 50 // required to reach the CHAIN_WORK threshold, to behave similarly to mainnet. 51 constexpr size_t REDOWNLOAD_BUFFER_SIZE{TARGET_BLOCKS - (MAX_HEADERS_RESULTS + 123)}; 52 constexpr size_t COMMITMENT_PERIOD{600}; // Somewhat close to mainnet. 54 struct HeadersGeneratorSetup : public RegTestingSetup { 55 const CBlock& genesis{Params().GenesisBlock()}; 56 CBlockIndex& chain_start{WITH_LOCK(::cs_main, return *Assert(m_node.chainman->m_blockman.LookupBlockIndex(genesis.GetHash())))}; 58 // Generate headers for two different chains (using differing merkle roots 59 // to ensure the headers are different). 60 const std::vector<CBlockHeader>& FirstChain() 62 // Block header hash target is half of max uint256 (2**256 / 2), expressible 63 // roughly as the coefficient 0x7fffff with the exponent 0x20 (32 bytes). 64 // This implies around every 2nd hash attempt should succeed, which 65 // is why CHAIN_WORK == TARGET_BLOCKS * 2. 66 assert(genesis.nBits == 0x207fffff); 68 // Subtract 1 since the genesis block also contributes work so we reach 69 // the CHAIN_WORK target. 70 static const auto first_chain{GenerateHeaders(/*count=*/TARGET_BLOCKS - 1, genesis.GetHash(), 71 genesis.nVersion, genesis.nTime, /*merkle_root=*/uint256::ZERO, genesis.nBits)}; 74 const std::vector<CBlockHeader>& SecondChain() 76 // Subtract 2 to keep total work below the target. 77 static const auto second_chain{GenerateHeaders(/*count=*/TARGET_BLOCKS - 2, genesis.GetHash(), 78 genesis.nVersion, genesis.nTime, /*merkle_root=*/uint256::ONE, genesis.nBits)}; 82 HeadersSyncState CreateState() 85 Params().GetConsensus(), 87 .commitment_period = COMMITMENT_PERIOD, 88 .redownload_buffer_size = REDOWNLOAD_BUFFER_SIZE, 91 /*minimum_required_work=*/CHAIN_WORK}; 96 void FindProofOfWork(CBlockHeader& starting_header); 102 std::vector<CBlockHeader> GenerateHeaders(size_t count, 103 uint256 prev_hash, int32_t nVersion, uint32_t prev_time, 104 const uint256& merkle_root, uint32_t nBits); 107 void HeadersGeneratorSetup::FindProofOfWork(CBlockHeader& starting_header) 109 while (!CheckProofOfWork(starting_header.GetHash(), starting_header.nBits, Params().GetConsensus())) { 110 ++starting_header.nNonce; 114 std::vector<CBlockHeader> HeadersGeneratorSetup::GenerateHeaders( 115 const size_t count, uint256 prev_hash, const int32_t nVersion, 116 uint32_t prev_time, const uint256& merkle_root, const uint32_t nBits) 118 std::vector<CBlockHeader> headers(count); 119 for (auto& next_header : headers) { 120 next_header.nVersion = nVersion; 121 next_header.hashPrevBlock = prev_hash; 122 next_header.hashMerkleRoot = merkle_root; 123 next_header.nTime = ++prev_time; 124 next_header.nBits = nBits; 126 FindProofOfWork(next_header); 127 prev_hash = next_header.GetHash(); 132 // In this test, we construct two sets of headers from genesis, one with 133 // sufficient proof of work and one without. 134 // 1. We deliver the first set of headers and verify that the headers sync state 135 // updates to the REDOWNLOAD phase successfully. 136 // Then we deliver the second set of headers and verify that they fail 137 // processing (presumably due to commitments not matching). 138 // 2. Verify that repeating with the first set of headers in both phases is 140 // 3. Repeat the second set of headers in both phases to demonstrate behavior 141 // when the chain a peer provides has too little work. 142 BOOST_FIXTURE_TEST_SUITE(headers_sync_chainwork_tests, HeadersGeneratorSetup) 144 BOOST_AUTO_TEST_CASE(sneaky_redownload) 146 const auto& first_chain{FirstChain()}; 147 const auto& second_chain{SecondChain()}; 149 // Feed the first chain to HeadersSyncState, by delivering 1 header 150 // initially and then the rest. 151 HeadersSyncState hss{CreateState()}; 153 // Just feed one header and check state. 154 // Pretend the message is still "full", so we don't abort.
155 CHECK_RESULT(hss.ProcessNextHeaders({{first_chain.front()}},
true),
159 first_chain.front().GetHash());
163 CHECK_RESULT(hss.ProcessNextHeaders(std::span{first_chain}.subspan(1),
true),
164 hss, State::REDOWNLOAD,
175 CHECK_RESULT(hss.ProcessNextHeaders(second_chain,
true),
185 const auto& first_chain{FirstChain()};
188 for (
const bool full_headers_message : {
false,
true}) {
193 const auto genesis_hash{genesis.GetHash()};
194 CHECK_RESULT(hss.ProcessNextHeaders(first_chain, full_headers_message),
195 hss, State::REDOWNLOAD,
202 CHECK_RESULT(hss.ProcessNextHeaders({first_chain.begin(), REDOWNLOAD_BUFFER_SIZE},
true),
203 hss, State::REDOWNLOAD,
209 CHECK_RESULT(hss.ProcessNextHeaders({{first_chain[REDOWNLOAD_BUFFER_SIZE]}},
true),
210 hss, State::REDOWNLOAD,
217 CHECK_RESULT(hss.ProcessNextHeaders({first_chain.begin() + REDOWNLOAD_BUFFER_SIZE + 1, first_chain.end()}, full_headers_message),
221 first_chain.size() - 1, first_chain.front().GetHash(),
228 const auto& second_chain{SecondChain()};
233 BOOST_REQUIRE_EQUAL(hss.GetState(), State::PRESYNC);
236 CHECK_RESULT(hss.ProcessNextHeaders({{second_chain.front()}},
true),
240 second_chain.front().GetHash());
245 CHECK_RESULT(hss.ProcessNextHeaders(std::span{second_chain}.subspan(1),
false),
State
The various states a (txhash,peer) pair can be in.
BOOST_AUTO_TEST_SUITE_END()