6#include <chainparams.h>
12#include <validation.h>
17#include <boost/test/unit_test.hpp>
22#define CHECK_RESULT(result_expression, hss, exp_state, exp_success, exp_request_more, \
23 exp_headers_size, exp_pow_validated_prev, exp_locator_hash) \
25 const auto result{result_expression}; \
26 BOOST_REQUIRE_EQUAL(hss.GetState(), exp_state); \
27 BOOST_CHECK_EQUAL(result.success, exp_success); \
28 BOOST_CHECK_EQUAL(result.request_more, exp_request_more); \
29 BOOST_CHECK_EQUAL(result.pow_validated_headers.size(), exp_headers_size); \
30 const std::optional<uint256> pow_validated_prev_opt{exp_pow_validated_prev}; \
31 if (pow_validated_prev_opt) { \
32 BOOST_CHECK_EQUAL(result.pow_validated_headers.at(0).hashPrevBlock, pow_validated_prev_opt); \
34 BOOST_CHECK_EQUAL(exp_headers_size, 0); \
36 const std::optional<uint256> locator_hash_opt{exp_locator_hash}; \
37 if (locator_hash_opt) { \
38 BOOST_CHECK_EQUAL(hss.NextHeadersRequestLocator().vHave.at(0), locator_hash_opt); \
40 BOOST_CHECK_EQUAL(exp_state, State::FINAL); \
102 uint256 prev_hash, int32_t nVersion, uint32_t prev_time,
103 const uint256& merkle_root, uint32_t nBits);
114 const size_t count,
uint256 prev_hash,
const int32_t nVersion,
115 uint32_t prev_time,
const uint256& merkle_root,
const uint32_t nBits)
117 std::vector<CBlockHeader> headers(
count);
118 for (
auto& next_header : headers) {
119 next_header.nVersion = nVersion;
120 next_header.hashPrevBlock = prev_hash;
121 next_header.hashMerkleRoot = merkle_root;
122 next_header.nTime = ++prev_time;
123 next_header.nBits = nBits;
126 prev_hash = next_header.GetHash();
145 const auto& first_chain{FirstChain()};
146 const auto& second_chain{SecondChain()};
154 CHECK_RESULT(hss.ProcessNextHeaders({{first_chain.front()}},
true),
158 first_chain.front().GetHash());
162 CHECK_RESULT(hss.ProcessNextHeaders(std::span{first_chain}.subspan(1),
true),
174 CHECK_RESULT(hss.ProcessNextHeaders(second_chain,
true),
184 const auto& first_chain{FirstChain()};
187 for (
const bool full_headers_message : {
false,
true}) {
192 const auto genesis_hash{genesis.GetHash()};
193 CHECK_RESULT(hss.ProcessNextHeaders(first_chain, full_headers_message),
201 CHECK_RESULT(hss.ProcessNextHeaders({first_chain.begin(), REDOWNLOAD_BUFFER_SIZE},
true),
208 CHECK_RESULT(hss.ProcessNextHeaders({{first_chain[REDOWNLOAD_BUFFER_SIZE]}},
true),
216 CHECK_RESULT(hss.ProcessNextHeaders({first_chain.begin() + REDOWNLOAD_BUFFER_SIZE + 1, first_chain.end()}, full_headers_message),
220 first_chain.size() - 1, first_chain.front().GetHash(),
227 const auto& second_chain{SecondChain()};
235 CHECK_RESULT(hss.ProcessNextHeaders({{second_chain.front()}},
true),
239 second_chain.front().GetHash());
244 CHECK_RESULT(hss.ProcessNextHeaders(std::span{second_chain}.subspan(1),
false),
const CChainParams & Params()
Return the currently selected parameters.
The block chain is a tree shaped structure starting with the genesis block at the root,...
const CBlock & GenesisBlock() const
const Consensus::Params & GetConsensus() const
256-bit unsigned big integer.
static const uint256 ZERO
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
BOOST_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_SUITE_END()
static const unsigned int MAX_HEADERS_RESULTS
Number of headers sent in one getheaders result.
bool CheckProofOfWork(uint256 hash, unsigned int nBits, const Consensus::Params ¶ms)
Check whether a block hash satisfies the proof-of-work requirement specified by nBits.
Identical to TestingSetup, but chain set to regtest.
std::unique_ptr< ChainstateManager > chainman
#define WITH_LOCK(cs, code)
Run code while locking a mutex.