Bitcoin ABC 0.26.3
P2P Digital Currency
Loading...
Searching...
No Matches
headerssync.h
Go to the documentation of this file.
1// Copyright (c) 2022 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> // For NodeId
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
28
30
32 nVersion = header.nVersion;
34 nTime = header.nTime;
35 nBits = header.nBits;
36 nNonce = header.nNonce;
37 }
38
42 ret.hashPrevBlock = hash_prev_block;
43 ret.hashMerkleRoot = hashMerkleRoot;
44 ret.nTime = nTime;
45 ret.nBits = nBits;
46 ret.nNonce = nNonce;
47 return ret;
48 };
49};
50
99public:
101
102 enum class State {
108 PRESYNC,
119 FINAL
120 };
121
123 State GetState() const { return m_download_state; }
124
127
133
139
152
155 std::vector<CBlockHeader> pow_validated_headers;
156 bool success{false};
157 bool request_more{false};
158 };
159
183 ProcessingResult
184 ProcessNextHeaders(const std::vector<CBlockHeader> &received_headers,
186
194
195private:
200 void Finalize();
201
210 const std::vector<CBlockHeader> &headers);
211
214
220
222 std::vector<CBlockHeader> PopHeadersReadyForAcceptance();
223
224private:
227
230
235 const CBlockIndex *m_chain_start{nullptr};
236
239
242
248
254
261 const unsigned m_commit_offset;
262
271
277
280
286 std::deque<CompressedHeader> m_redownloaded_headers;
287
290
297
304
307
314
317};
318
319#endif // BITCOIN_HEADERSSYNC_H
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition block.h:23
uint32_t nNonce
Definition block.h:31
uint32_t nBits
Definition block.h:30
uint32_t nTime
Definition block.h:29
int32_t nVersion
Definition block.h:26
uint256 hashMerkleRoot
Definition block.h:28
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition blockindex.h:25
HeadersSyncState:
Definition headerssync.h:98
arith_uint256 GetPresyncWork() const
Return the amount of work in the chain received during the PRESYNC phase.
uint64_t m_max_commitments
m_max_commitments is a bound we calculate on how long an honest peer's chain could be,...
arith_uint256 m_redownload_chain_work
The accumulated work on the redownloaded chain.
@ FINAL
We're done syncing with this peer and can discard any remaining state.
@ PRESYNC
PRESYNC means the peer has not yet demonstrated their chain has sufficient work and we're only buildi...
@ REDOWNLOAD
REDOWNLOAD means the peer has given us a high-enough-work chain, and now we're redownloading the head...
CBlockHeader m_last_header_received
Store the latest header received while in PRESYNC (initialized to m_chain_start)
BlockHash m_redownload_buffer_last_hash
Hash of last header in m_redownloaded_headers (initialized to m_chain_start).
arith_uint256 m_current_chain_work
Work that we've seen so far on the peer's chain.
int64_t m_current_height
Height of m_last_header_received.
const unsigned m_commit_offset
The (secret) offset on the heights for which to create commitments.
uint32_t GetPresyncTime() const
Return the block timestamp of the last header received during the PRESYNC phase.
const arith_uint256 m_minimum_required_work
Minimum work that we're looking for on this chain.
std::vector< CBlockHeader > PopHeadersReadyForAcceptance()
Return a set of headers that satisfy our proof-of-work threshold.
bool ValidateAndStoreHeadersCommitments(const std::vector< CBlockHeader > &headers)
Only called in PRESYNC.
const Consensus::Params & m_consensus_params
We use the consensus params in our anti-DoS calculations.
bool ValidateAndProcessSingleHeader(const CBlockHeader &current)
In PRESYNC, process and update state for a single header.
State m_download_state
Current state of our headers sync.
bool ValidateAndStoreRedownloadedHeader(const CBlockHeader &header)
In REDOWNLOAD, check a header's commitment (if applicable) and add to buffer for later processing.
bitdeque m_header_commitments
A queue of commitment bits, created during the 1st phase, and verified during the 2nd.
BlockHash m_redownload_buffer_first_prev_hash
The hashPrevBlock entry for the first header in m_redownloaded_headers We need this to reconstruct th...
const NodeId m_id
NodeId of the peer (used for log messages)
int64_t m_redownload_buffer_last_height
Height of last header in m_redownloaded_headers.
std::deque< CompressedHeader > m_redownloaded_headers
During phase 2 (REDOWNLOAD), we buffer redownloaded headers in memory until enough commitments have b...
const SaltedBlockHashHasher m_hasher
m_hasher is a salted hasher for making our 1-bit commitments to headers we've seen.
ProcessingResult ProcessNextHeaders(const std::vector< CBlockHeader > &received_headers, bool full_headers_message)
Process a batch of headers, once a sync via this mechanism has started.
State GetState() const
Return the current state of our download.
bool m_process_all_remaining_headers
Set this to true once we encounter the target blockheader during phase 2 (REDOWNLOAD).
void Finalize()
Clear out all download state that might be in progress (freeing any used memory), and mark this objec...
const CBlockIndex * m_chain_start
Store the last block in our block index that the peer's chain builds from.
int64_t GetPresyncHeight() const
Return the height reached during the PRESYNC phase.
CBlockLocator NextHeadersRequestLocator() const
Issue the next GETHEADERS message to our peer.
256-bit unsigned big integer.
void SetNull()
Definition uint256.h:41
Class that mimics std::deque<bool>, but with std::vector<bool>'s bit packing.
Definition bitdeque.h:22
256-bit opaque blob.
Definition uint256.h:129
int64_t NodeId
Definition nodeid.h:10
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
Definition random.h:85
A BlockHash is a unqiue identifier for a block.
Definition blockhash.h:13
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
Definition block.h:105
uint256 hashMerkleRoot
Definition headerssync.h:24
CBlockHeader GetFullHeader(const BlockHash &hash_prev_block)
Definition headerssync.h:39
CompressedHeader(const CBlockHeader &header)
Definition headerssync.h:31
Parameters that influence chain consensus.
Definition params.h:34
Result data structure for ProcessNextHeaders.
std::vector< CBlockHeader > pow_validated_headers