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); \
103 uint256 prev_hash, int32_t nVersion, uint32_t prev_time,
104 const uint256& merkle_root, uint32_t nBits);
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;
127 prev_hash = next_header.GetHash();
146 const auto& first_chain{FirstChain()};
147 const auto& second_chain{SecondChain()};
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),
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),
202 CHECK_RESULT(hss.ProcessNextHeaders({first_chain.begin(), REDOWNLOAD_BUFFER_SIZE},
true),
209 CHECK_RESULT(hss.ProcessNextHeaders({{first_chain[REDOWNLOAD_BUFFER_SIZE]}},
true),
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()};
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),
const CChainParams & Params()
Return the currently selected parameters.
#define Assert(val)
Identity function.
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.