Bitcoin Core 32.99.0
P2P Digital Currency
Loading...
Searching...
No Matches
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 <headerssync.h>
6
7#include <arith_uint256.h>
8#include <chain.h>
9#include <chainparams.h>
10#include <test/fuzz/fuzz.h>
11#include <test/fuzz/util.h>
13#include <test/util/time.h>
14#include <uint256.h>
15#include <util/chaintype.h>
16#include <util/check.h>
17#include <util/time.h>
18#include <validation.h>
19
20#include <iterator>
21#include <vector>
22
24{
25 static const auto testing_setup = MakeNoLogFileContext<>(
26 /*chain_type=*/ChainType::MAIN);
27}
28
31 const std::vector<CBlockHeader>& all_headers,
32 std::vector<CBlockHeader>& new_headers)
33{
34 Assume(!new_headers.empty());
35
37 all_headers.empty() ? &genesis_header : &all_headers.back()};
38
39 for (auto& header : new_headers) {
40 header.hashPrevBlock = prev_header->GetHash();
41
42 prev_header = &header;
43 }
44}
45
47{
48public:
50 const CBlockIndex& chain_start, const arith_uint256& minimum_required_work)
51 : HeadersSyncState(/*id=*/0, Params().GetConsensus(), sync_params, chain_start, minimum_required_work)
52 {
53 const_cast<size_t&>(m_commit_offset) = commit_offset;
54 }
55};
56
58{
60 FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
61
64
65 FakeNodeClock clock{ConsumeTime(fuzzed_data_provider, /*min=*/start_index.GetMedianTimePast())};
66
67 const uint256 genesis_hash = genesis_header.GetHash();
68 start_index.phashBlock = &genesis_hash;
69
70 const HeadersSyncParams params{
72 .redownload_buffer_size = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, Params().HeadersSync().redownload_buffer_size * 2),
73 };
76 params,
77 /*commit_offset=*/fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, params.commitment_period - 1),
78 /*chain_start=*/start_index,
79 /*minimum_required_work=*/min_work);
80
81 // Store headers for potential redownload phase.
82 std::vector<CBlockHeader> all_headers;
83 std::vector<CBlockHeader>::const_iterator redownloaded_it;
84 bool presync{true};
85 bool requested_more{true};
86
87 while (requested_more) {
88 std::vector<CBlockHeader> headers;
89
90 // Consume headers from fuzzer or maybe replay headers if we got to the
91 // redownload phase.
94 if (!deser_headers || deser_headers->empty()) return;
95
98 }
99
100 headers.swap(*deser_headers);
101 } else if (auto num_headers_left{std::distance(redownloaded_it, all_headers.cend())}; num_headers_left > 0) {
102 // Consume some headers from the redownload buffer (At least one
103 // header is consumed).
106 headers.insert(headers.cend(), begin_it, redownloaded_it);
107 }
108
109 if (headers.empty()) return;
110 auto result = headers_sync.ProcessNextHeaders(headers, fuzzed_data_provider.ConsumeBool());
111 requested_more = result.request_more;
112
113 if (result.request_more) {
114 if (presync) {
115 all_headers.insert(all_headers.cend(), headers.cbegin(), headers.cend());
116
117 if (headers_sync.GetState() == HeadersSyncState::State::REDOWNLOAD) {
118 presync = false;
119 redownloaded_it = all_headers.cbegin();
120
121 // If we get to redownloading, the presynced headers need
122 // to have the min amount of work on them.
124 }
125 }
126
127 (void)headers_sync.NextHeadersRequestLocator();
128 }
129 }
130}
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:131
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
const CBlock & GenesisBlock() const
Definition chainparams.h:94
const HeadersSyncParams & HeadersSync() const
Helper to initialize the global NodeClock, let a duration elapse, and reset it after use in a test.
Definition time.h:54
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)
HeadersSyncState:
const size_t m_commit_offset
The (secret) offset on the heights for which to create commitments.
256-bit unsigned big integer.
256-bit opaque blob.
Definition uint256.h:196
#define FUZZ_TARGET(...)
Definition fuzz.h:35
HTTPHeaders headers
Definition basic.cpp:11
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
SeedRandomStateForTest(SeedRand::ZEROS)
static void initialize_headers_sync_state_fuzz()
void MakeHeadersContinuous(const CBlockHeader &genesis_header, const std::vector< CBlockHeader > &all_headers, std::vector< CBlockHeader > &new_headers)
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:196
@ ZEROS
Seed with a compile time constant of zeros.
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
Definition time.h:82
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:46
FakeNodeClock clock