Bitcoin Core  31.0.0
P2P Digital Currency
headerssync.h
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 http://www.opensource.org/licenses/mit-license.php.
4 
5 #ifndef BITCOIN_HEADERSSYNC_H
6 #define BITCOIN_HEADERSSYNC_H
7 
8 #include <arith_uint256.h>
9 #include <chain.h>
10 #include <consensus/params.h>
11 #include <net.h>
12 #include <primitives/block.h>
13 #include <uint256.h>
14 #include <util/bitdeque.h>
15 #include <util/hasher.h>
16 
17 #include <deque>
18 #include <vector>
19 
20 // A compressed CBlockHeader, which leaves out the prevhash
22  // header
23  int32_t nVersion{0};
25  uint32_t nTime{0};
26  uint32_t nBits{0};
27  uint32_t nNonce{0};
28 
30  {
32  }
33 
34  explicit CompressedHeader(const CBlockHeader& header)
35  : nVersion{header.nVersion},
36  hashMerkleRoot{header.hashMerkleRoot},
37  nTime{header.nTime},
38  nBits{header.nBits},
39  nNonce{header.nNonce}
40  {
41  }
42 
43  CBlockHeader GetFullHeader(const uint256& hash_prev_block) const
44  {
46  ret.nVersion = nVersion;
47  ret.hashPrevBlock = hash_prev_block;
48  ret.hashMerkleRoot = hashMerkleRoot;
49  ret.nTime = nTime;
50  ret.nBits = nBits;
51  ret.nNonce = nNonce;
52  return ret;
53  };
54 };
55 
103 public:
104  ~HeadersSyncState() = default;
105 
106  enum class State {
110  PRESYNC,
114  REDOWNLOAD,
116  FINAL
117  };
118 
120  State GetState() const { return m_download_state; }
121 
123  int64_t GetPresyncHeight() const { return m_current_height; }
124 
126  uint32_t GetPresyncTime() const { return m_last_header_received.nTime; }
127 
130 
139  HeadersSyncState(NodeId id, const Consensus::Params& consensus_params,
140  const HeadersSyncParams& params, const CBlockIndex& chain_start,
141  const arith_uint256& minimum_required_work);
142 
145  std::vector<CBlockHeader> pow_validated_headers;
146  bool success{false};
147  bool request_more{false};
148  };
149 
170  ProcessingResult ProcessNextHeaders(std::span<const CBlockHeader>
171  received_headers, bool full_headers_message);
172 
179 
180 protected:
185  const size_t m_commit_offset;
186 
187 private:
191  void Finalize();
192 
200  bool ValidateAndStoreHeadersCommitments(std::span<const CBlockHeader> headers);
201 
203  bool ValidateAndProcessSingleHeader(const CBlockHeader& current);
204 
208 
210  std::vector<CBlockHeader> PopHeadersReadyForAcceptance();
211 
212 private:
214  const NodeId m_id;
215 
218 
221 
224 
227 
230 
233 
236 
242  uint64_t m_max_commitments{0};
243 
246 
248  int64_t m_current_height{0};
249 
253  std::deque<CompressedHeader> m_redownloaded_headers;
254 
257 
263 
269 
272 
278 
281 };
282 
283 #endif // BITCOIN_HEADERSSYNC_H
uint32_t GetPresyncTime() const
Return the block timestamp of the last header received during the PRESYNC phase.
Definition: headerssync.h:126
int ret
Result data structure for ProcessNextHeaders.
Definition: headerssync.h:144
const HeadersSyncParams m_params
Parameters that impact memory usage for a given chain, especially when attacked.
Definition: headerssync.h:220
uint32_t nTime
Definition: headerssync.h:25
bool m_process_all_remaining_headers
Set this to true once we encounter the target blockheader during phase 2 (REDOWNLOAD).
Definition: headerssync.h:277
Describes a place in the block chain to another node such that if the other node doesn&#39;t have the sam...
Definition: block.h:116
const SaltedUint256Hasher m_hasher
m_hasher is a salted hasher for making our 1-bit commitments to headers we&#39;ve seen.
Definition: headerssync.h:232
const arith_uint256 m_minimum_required_work
Minimum work that we&#39;re looking for on this chain.
Definition: headerssync.h:226
arith_uint256 GetPresyncWork() const
Return the amount of work in the chain received during the PRESYNC phase.
Definition: headerssync.h:129
CBlockLocator NextHeadersRequestLocator() const
Issue the next GETHEADERS message to our peer.
State m_download_state
Current state of our headers sync.
Definition: headerssync.h:280
uint32_t nNonce
Definition: headerssync.h:27
std::deque< CompressedHeader > m_redownloaded_headers
During phase 2 (REDOWNLOAD), we buffer redownloaded headers in memory until enough commitments have b...
Definition: headerssync.h:253
We&#39;re done syncing with this peer and can discard any remaining state.
CBlockHeader GetFullHeader(const uint256 &hash_prev_block) const
Definition: headerssync.h:43
arith_uint256 m_redownload_chain_work
The accumulated work on the redownloaded chain.
Definition: headerssync.h:271
const size_t m_commit_offset
The (secret) offset on the heights for which to create commitments.
Definition: headerssync.h:185
uint64_t m_max_commitments
m_max_commitments is a bound we calculate on how long an honest peer&#39;s chain could be...
Definition: headerssync.h:242
bool ValidateAndProcessSingleHeader(const CBlockHeader &current)
In PRESYNC, process and update state for a single header.
uint32_t nTime
Definition: block.h:33
PRESYNC means the peer has not yet demonstrated their chain has sufficient work and we&#39;re only buildi...
uint256 hashMerkleRoot
Definition: headerssync.h:24
bool ValidateAndStoreHeadersCommitments(std::span< const CBlockHeader > headers)
Only called in PRESYNC.
bitdeque m_header_commitments
A queue of commitment bits, created during the 1st phase, and verified during the 2nd...
Definition: headerssync.h:235
int32_t nVersion
Definition: headerssync.h:23
HeadersSyncState(NodeId id, const Consensus::Params &consensus_params, const HeadersSyncParams &params, const CBlockIndex &chain_start, const arith_uint256 &minimum_required_work)
Construct a HeadersSyncState object representing a headers sync via this download-twice mechanism)...
Definition: headerssync.cpp:17
CBlockHeader m_last_header_received
Store the latest header received while in PRESYNC (initialized to m_chain_start)
Definition: headerssync.h:245
std::vector< CBlockHeader > pow_validated_headers
Definition: headerssync.h:145
~HeadersSyncState()=default
int64_t NodeId
Definition: net.h:103
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
CompressedHeader(const CBlockHeader &header)
Definition: headerssync.h:34
void Finalize()
Clear out all download state that might be in progress (freeing any used memory), and mark this objec...
Definition: headerssync.cpp:51
Parameters that influence chain consensus.
Definition: params.h:84
256-bit unsigned big integer.
int64_t GetPresyncHeight() const
Return the height reached during the PRESYNC phase.
Definition: headerssync.h:123
bool ValidateAndStoreRedownloadedHeader(const CBlockHeader &header)
In REDOWNLOAD, check a header&#39;s commitment (if applicable) and add to buffer for later processing...
uint32_t nBits
Definition: headerssync.h:26
256-bit opaque blob.
Definition: uint256.h:195
uint256 m_redownload_buffer_first_prev_hash
The hashPrevBlock entry for the first header in m_redownloaded_headers We need this to reconstruct th...
Definition: headerssync.h:268
REDOWNLOAD means the peer has given us a high-enough-work chain, and now we&#39;re redownloading the head...
std::vector< CBlockHeader > PopHeadersReadyForAcceptance()
Return a set of headers that satisfy our proof-of-work threshold.
constexpr void SetNull()
Definition: uint256.h:55
const Consensus::Params & m_consensus_params
We use the consensus params in our anti-DoS calculations.
Definition: headerssync.h:217
int64_t m_current_height
Height of m_last_header_received.
Definition: headerssync.h:248
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: chain.h:93
Configuration for headers sync memory usage.
Definition: chainparams.h:64
int64_t m_redownload_buffer_last_height
Height of last header in m_redownloaded_headers.
Definition: headerssync.h:256
Class that mimics std::deque<bool>, but with std::vector<bool>&#39;s bit packing.
Definition: bitdeque.h:22
HeadersSyncState:
Definition: headerssync.h:102
uint256 m_redownload_buffer_last_hash
Hash of last header in m_redownloaded_headers (initialized to m_chain_start).
Definition: headerssync.h:262
arith_uint256 m_current_chain_work
Work that we&#39;ve seen so far on the peer&#39;s chain.
Definition: headerssync.h:229
State GetState() const
Return the current state of our download.
Definition: headerssync.h:120
int32_t nVersion
Definition: block.h:30
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition: block.h:26
const NodeId m_id
NodeId of the peer (used for log messages)
Definition: headerssync.h:214
const CBlockIndex & m_chain_start
Store the last block in our block index that the peer&#39;s chain builds from.
Definition: headerssync.h:223