Bitcoin Core 31.99.0
P2P Digital Currency
headerssync.cpp
Go to the documentation of this file.
1// Copyright (c) 2022-present The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or https://opensource.org/license/mit.
4
5#include <arith_uint256.h>
6#include <chain.h>
7#include <chainparams.h>
8#include <headerssync.h>
9#include <test/fuzz/fuzz.h>
10#include <test/fuzz/util.h>
12#include <test/util/time.h>
13#include <uint256.h>
14#include <util/chaintype.h>
15#include <util/time.h>
16#include <validation.h>
17
18#include <iterator>
19#include <vector>
20
22{
23 static const auto testing_setup = MakeNoLogFileContext<>(
24 /*chain_type=*/ChainType::MAIN);
25}
26
28 const CBlockHeader& genesis_header,
29 const std::vector<CBlockHeader>& all_headers,
30 std::vector<CBlockHeader>& new_headers)
31{
32 Assume(!new_headers.empty());
33
34 const CBlockHeader* prev_header{
35 all_headers.empty() ? &genesis_header : &all_headers.back()};
36
37 for (auto& header : new_headers) {
38 header.hashPrevBlock = prev_header->GetHash();
39
40 prev_header = &header;
41 }
42}
43
45{
46public:
47 FuzzedHeadersSyncState(const HeadersSyncParams& sync_params, const size_t commit_offset,
48 const CBlockIndex& chain_start, const arith_uint256& minimum_required_work)
49 : HeadersSyncState(/*id=*/0, Params().GetConsensus(), sync_params, chain_start, minimum_required_work)
50 {
51 const_cast<size_t&>(m_commit_offset) = commit_offset;
52 }
53};
54
56{
58 FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
59
60 CBlockHeader genesis_header{Params().GenesisBlock()};
61 CBlockIndex start_index(genesis_header);
62
63 NodeClockContext clock_ctx{ConsumeTime(fuzzed_data_provider, /*min=*/start_index.GetMedianTimePast())};
64
65 const uint256 genesis_hash = genesis_header.GetHash();
66 start_index.phashBlock = &genesis_hash;
67
68 const HeadersSyncParams params{
70 .redownload_buffer_size = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, Params().HeadersSync().redownload_buffer_size * 2),
71 };
73 FuzzedHeadersSyncState headers_sync(
74 params,
75 /*commit_offset=*/fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, params.commitment_period - 1),
76 /*chain_start=*/start_index,
77 /*minimum_required_work=*/min_work);
78
79 // Store headers for potential redownload phase.
80 std::vector<CBlockHeader> all_headers;
81 std::vector<CBlockHeader>::const_iterator redownloaded_it;
82 bool presync{true};
83 bool requested_more{true};
84
85 while (requested_more) {
86 std::vector<CBlockHeader> headers;
87
88 // Consume headers from fuzzer or maybe replay headers if we got to the
89 // redownload phase.
90 if (presync || fuzzed_data_provider.ConsumeBool()) {
91 auto deser_headers = ConsumeDeserializable<std::vector<CBlockHeader>>(fuzzed_data_provider);
92 if (!deser_headers || deser_headers->empty()) return;
93
95 MakeHeadersContinuous(genesis_header, all_headers, *deser_headers);
96 }
97
98 headers.swap(*deser_headers);
99 } else if (auto num_headers_left{std::distance(redownloaded_it, all_headers.cend())}; num_headers_left > 0) {
100 // Consume some headers from the redownload buffer (At least one
101 // header is consumed).
102 auto begin_it{redownloaded_it};
103 std::advance(redownloaded_it, fuzzed_data_provider.ConsumeIntegralInRange<int>(1, num_headers_left));
104 headers.insert(headers.cend(), begin_it, redownloaded_it);
105 }
106
107 if (headers.empty()) return;
108 auto result = headers_sync.ProcessNextHeaders(headers, fuzzed_data_provider.ConsumeBool());
109 requested_more = result.request_more;
110
111 if (result.request_more) {
112 if (presync) {
113 all_headers.insert(all_headers.cend(), headers.cbegin(), headers.cend());
114
115 if (headers_sync.GetState() == HeadersSyncState::State::REDOWNLOAD) {
116 presync = false;
117 redownloaded_it = all_headers.cbegin();
118
119 // If we get to redownloading, the presynced headers need
120 // to have the min amount of work on them.
121 assert(CalculateClaimedHeadersWork(all_headers) >= min_work);
122 }
123 }
124
125 (void)headers_sync.NextHeadersRequestLocator();
126 }
127 }
128}
arith_uint256 UintToArith256(const uint256 &a)
const CChainParams & Params()
Return the currently selected parameters.
#define Assume(val)
Assume is the identity function.
Definition: check.h:128
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition: block.h:27
uint256 hashPrevBlock
Definition: block.h:31
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: chain.h:94
int64_t GetMedianTimePast() const
Definition: chain.h:233
const uint256 * phashBlock
pointer to the hash of the block, if any. Memory is owned by this CBlockIndex
Definition: chain.h:97
const CBlock & GenesisBlock() const
Definition: chainparams.h:94
const HeadersSyncParams & HeadersSync() const
Definition: chainparams.h:117
T ConsumeIntegralInRange(T min, T max)
FuzzedHeadersSyncState(const HeadersSyncParams &sync_params, const size_t commit_offset, const CBlockIndex &chain_start, const arith_uint256 &minimum_required_work)
Definition: headerssync.cpp:47
HeadersSyncState:
Definition: headerssync.h:102
@ REDOWNLOAD
REDOWNLOAD means the peer has given us a high-enough-work chain, and now we're redownloading the head...
ProcessingResult ProcessNextHeaders(std::span< const CBlockHeader > received_headers, bool full_headers_message)
Process a batch of headers, once a sync via this mechanism has started.
Definition: headerssync.cpp:68
State GetState() const
Return the current state of our download.
Definition: headerssync.h:120
const size_t m_commit_offset
The (secret) offset on the heights for which to create commitments.
Definition: headerssync.h:185
CBlockLocator NextHeadersRequestLocator() const
Issue the next GETHEADERS message to our peer.
Helper to initialize the global NodeClock, let a duration elapse, and reset it after use in a test.
Definition: time.h:40
256-bit unsigned big integer.
256-bit opaque blob.
Definition: uint256.h:196
Definition: basic.cpp:8
Configuration for headers sync memory usage.
Definition: chainparams.h:64
size_t redownload_buffer_size
Minimum number of validated headers to accumulate in the redownload buffer before feeding them into t...
Definition: chainparams.h:69
size_t commitment_period
Distance in blocks between header commitments.
Definition: chainparams.h:66
static void initialize_headers_sync_state_fuzz()
Definition: headerssync.cpp:21
FUZZ_TARGET(headers_sync_state,.init=initialize_headers_sync_state_fuzz)
Definition: headerssync.cpp:55
void MakeHeadersContinuous(const CBlockHeader &genesis_header, const std::vector< CBlockHeader > &all_headers, std::vector< CBlockHeader > &new_headers)
Definition: headerssync.cpp:27
NodeSeconds ConsumeTime(FuzzedDataProvider &fuzzed_data_provider, const std::optional< int64_t > &min, const std::optional< int64_t > &max) noexcept
Definition: util.cpp:34
uint256 ConsumeUInt256(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: util.h:176
void SeedRandomStateForTest(SeedRand seedtype)
Seed the global RNG state for testing and log the seed value.
Definition: random.cpp:19
@ ZEROS
Seed with a compile time constant of zeros.
arith_uint256 CalculateClaimedHeadersWork(std::span< const CBlockHeader > headers)
Return the sum of the claimed work on a given set of headers.
assert(!tx.IsCoinBase())
FuzzedDataProvider & fuzzed_data_provider
Definition: fees.cpp:39