Bitcoin Core
28.0.0
P2P Digital Currency
Loading...
Searching...
No Matches
src
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
21
struct
CompressedHeader
{
22
// header
23
int32_t
nVersion
{0};
24
uint256
hashMerkleRoot
;
25
uint32_t
nTime
{0};
26
uint32_t
nBits
{0};
27
uint32_t
nNonce
{0};
28
29
CompressedHeader
()
30
{
31
hashMerkleRoot
.
SetNull
();
32
}
33
34
CompressedHeader
(
const
CBlockHeader
& header)
35
{
36
nVersion
= header.
nVersion
;
37
hashMerkleRoot
= header.
hashMerkleRoot
;
38
nTime
= header.
nTime
;
39
nBits
= header.
nBits
;
40
nNonce
= header.
nNonce
;
41
}
42
43
CBlockHeader
GetFullHeader
(
const
uint256
& hash_prev_block) {
44
CBlockHeader
ret
;
45
ret
.
nVersion
=
nVersion
;
46
ret
.hashPrevBlock = hash_prev_block;
47
ret
.hashMerkleRoot =
hashMerkleRoot
;
48
ret
.nTime =
nTime
;
49
ret
.nBits =
nBits
;
50
ret
.nNonce =
nNonce
;
51
return
ret
;
52
};
53
};
54
101
class
HeadersSyncState
{
102
public
:
103
~HeadersSyncState
() =
default
;
104
105
enum class
State
{
109
PRESYNC
,
113
REDOWNLOAD
,
115
FINAL
116
};
117
119
State
GetState
()
const
{
return
m_download_state
; }
120
122
int64_t
GetPresyncHeight
()
const
{
return
m_current_height
; }
123
125
uint32_t
GetPresyncTime
()
const
{
return
m_last_header_received
.
nTime
; }
126
128
arith_uint256
GetPresyncWork
()
const
{
return
m_current_chain_work
; }
129
138
HeadersSyncState
(
NodeId
id
,
const
Consensus::Params
& consensus_params,
139
const
CBlockIndex
* chain_start,
const
arith_uint256
& minimum_required_work);
140
142
struct
ProcessingResult
{
143
std::vector<CBlockHeader>
pow_validated_headers
;
144
bool
success
{
false
};
145
bool
request_more
{
false
};
146
};
147
168
ProcessingResult
ProcessNextHeaders
(
const
std::vector<CBlockHeader>&
169
received_headers,
bool
full_headers_message);
170
176
CBlockLocator
NextHeadersRequestLocator
()
const
;
177
178
protected
:
183
const
unsigned
m_commit_offset
;
184
185
private
:
189
void
Finalize
();
190
198
bool
ValidateAndStoreHeadersCommitments
(
const
std::vector<CBlockHeader>& headers);
199
201
bool
ValidateAndProcessSingleHeader
(
const
CBlockHeader
& current);
202
205
bool
ValidateAndStoreRedownloadedHeader
(
const
CBlockHeader
& header);
206
208
std::vector<CBlockHeader>
PopHeadersReadyForAcceptance
();
209
210
private
:
212
const
NodeId
m_id
;
213
215
const
Consensus::Params
&
m_consensus_params
;
216
218
const
CBlockIndex
*
m_chain_start
{
nullptr
};
219
221
const
arith_uint256
m_minimum_required_work
;
222
224
arith_uint256
m_current_chain_work
;
225
227
const
SaltedTxidHasher
m_hasher
;
228
230
bitdeque<>
m_header_commitments
;
231
237
uint64_t
m_max_commitments
{0};
238
240
CBlockHeader
m_last_header_received
;
241
243
int64_t
m_current_height
{0};
244
248
std::deque<CompressedHeader>
m_redownloaded_headers
;
249
251
int64_t
m_redownload_buffer_last_height
{0};
252
257
uint256
m_redownload_buffer_last_hash
;
258
263
uint256
m_redownload_buffer_first_prev_hash
;
264
266
arith_uint256
m_redownload_chain_work
;
267
272
bool
m_process_all_remaining_headers
{
false
};
273
275
State
m_download_state
{
State::PRESYNC
};
276
};
277
278
#endif
// BITCOIN_HEADERSSYNC_H
arith_uint256.h
ret
int ret
Definition
bitcoin-cli.cpp:1306
bitdeque.h
block.h
chain.h
CBlockHeader
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition
block.h:22
CBlockHeader::nNonce
uint32_t nNonce
Definition
block.h:30
CBlockHeader::nBits
uint32_t nBits
Definition
block.h:29
CBlockHeader::nTime
uint32_t nTime
Definition
block.h:28
CBlockHeader::nVersion
int32_t nVersion
Definition
block.h:25
CBlockHeader::hashMerkleRoot
uint256 hashMerkleRoot
Definition
block.h:27
CBlockIndex
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition
chain.h:141
HeadersSyncState
HeadersSyncState:
Definition
headerssync.h:101
HeadersSyncState::GetPresyncWork
arith_uint256 GetPresyncWork() const
Return the amount of work in the chain received during the PRESYNC phase.
Definition
headerssync.h:128
HeadersSyncState::m_hasher
const SaltedTxidHasher m_hasher
m_hasher is a salted hasher for making our 1-bit commitments to headers we've seen.
Definition
headerssync.h:227
HeadersSyncState::m_max_commitments
uint64_t m_max_commitments
m_max_commitments is a bound we calculate on how long an honest peer's chain could be,...
Definition
headerssync.h:237
HeadersSyncState::HeadersSyncState
HeadersSyncState(NodeId id, const Consensus::Params &consensus_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:26
HeadersSyncState::m_redownload_chain_work
arith_uint256 m_redownload_chain_work
The accumulated work on the redownloaded chain.
Definition
headerssync.h:266
HeadersSyncState::State
State
Definition
headerssync.h:105
HeadersSyncState::State::FINAL
@ FINAL
We're done syncing with this peer and can discard any remaining state.
HeadersSyncState::State::PRESYNC
@ PRESYNC
PRESYNC means the peer has not yet demonstrated their chain has sufficient work and we're only buildi...
HeadersSyncState::State::REDOWNLOAD
@ REDOWNLOAD
REDOWNLOAD means the peer has given us a high-enough-work chain, and now we're redownloading the head...
HeadersSyncState::m_last_header_received
CBlockHeader m_last_header_received
Store the latest header received while in PRESYNC (initialized to m_chain_start)
Definition
headerssync.h:240
HeadersSyncState::m_current_chain_work
arith_uint256 m_current_chain_work
Work that we've seen so far on the peer's chain.
Definition
headerssync.h:224
HeadersSyncState::m_current_height
int64_t m_current_height
Height of m_last_header_received.
Definition
headerssync.h:243
HeadersSyncState::m_commit_offset
const unsigned m_commit_offset
The (secret) offset on the heights for which to create commitments.
Definition
headerssync.h:183
HeadersSyncState::GetPresyncTime
uint32_t GetPresyncTime() const
Return the block timestamp of the last header received during the PRESYNC phase.
Definition
headerssync.h:125
HeadersSyncState::m_minimum_required_work
const arith_uint256 m_minimum_required_work
Minimum work that we're looking for on this chain.
Definition
headerssync.h:221
HeadersSyncState::PopHeadersReadyForAcceptance
std::vector< CBlockHeader > PopHeadersReadyForAcceptance()
Return a set of headers that satisfy our proof-of-work threshold.
Definition
headerssync.cpp:281
HeadersSyncState::ValidateAndStoreHeadersCommitments
bool ValidateAndStoreHeadersCommitments(const std::vector< CBlockHeader > &headers)
Only called in PRESYNC.
Definition
headerssync.cpp:140
HeadersSyncState::m_consensus_params
const Consensus::Params & m_consensus_params
We use the consensus params in our anti-DoS calculations.
Definition
headerssync.h:215
HeadersSyncState::ValidateAndProcessSingleHeader
bool ValidateAndProcessSingleHeader(const CBlockHeader ¤t)
In PRESYNC, process and update state for a single header.
Definition
headerssync.cpp:178
HeadersSyncState::~HeadersSyncState
~HeadersSyncState()=default
HeadersSyncState::m_download_state
State m_download_state
Current state of our headers sync.
Definition
headerssync.h:275
HeadersSyncState::ValidateAndStoreRedownloadedHeader
bool ValidateAndStoreRedownloadedHeader(const CBlockHeader &header)
In REDOWNLOAD, check a header's commitment (if applicable) and add to buffer for later processing.
Definition
headerssync.cpp:216
HeadersSyncState::m_header_commitments
bitdeque m_header_commitments
A queue of commitment bits, created during the 1st phase, and verified during the 2nd.
Definition
headerssync.h:230
HeadersSyncState::m_id
const NodeId m_id
NodeId of the peer (used for log messages)
Definition
headerssync.h:212
HeadersSyncState::m_redownload_buffer_last_height
int64_t m_redownload_buffer_last_height
Height of last header in m_redownloaded_headers.
Definition
headerssync.h:251
HeadersSyncState::m_redownloaded_headers
std::deque< CompressedHeader > m_redownloaded_headers
During phase 2 (REDOWNLOAD), we buffer redownloaded headers in memory until enough commitments have b...
Definition
headerssync.h:248
HeadersSyncState::ProcessNextHeaders
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.
Definition
headerssync.cpp:69
HeadersSyncState::GetState
State GetState() const
Return the current state of our download.
Definition
headerssync.h:119
HeadersSyncState::m_process_all_remaining_headers
bool m_process_all_remaining_headers
Set this to true once we encounter the target blockheader during phase 2 (REDOWNLOAD).
Definition
headerssync.h:272
HeadersSyncState::Finalize
void Finalize()
Clear out all download state that might be in progress (freeing any used memory), and mark this objec...
Definition
headerssync.cpp:52
HeadersSyncState::m_redownload_buffer_last_hash
uint256 m_redownload_buffer_last_hash
Hash of last header in m_redownloaded_headers (initialized to m_chain_start).
Definition
headerssync.h:257
HeadersSyncState::m_redownload_buffer_first_prev_hash
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:263
HeadersSyncState::m_chain_start
const CBlockIndex * m_chain_start
Store the last block in our block index that the peer's chain builds from.
Definition
headerssync.h:218
HeadersSyncState::GetPresyncHeight
int64_t GetPresyncHeight() const
Return the height reached during the PRESYNC phase.
Definition
headerssync.h:122
HeadersSyncState::NextHeadersRequestLocator
CBlockLocator NextHeadersRequestLocator() const
Issue the next GETHEADERS message to our peer.
Definition
headerssync.cpp:297
SaltedTxidHasher
Definition
hasher.h:19
arith_uint256
256-bit unsigned big integer.
Definition
arith_uint256.h:245
base_blob::SetNull
constexpr void SetNull()
Definition
uint256.h:53
bitdeque
Class that mimics std::deque<bool>, but with std::vector<bool>'s bit packing.
Definition
bitdeque.h:23
uint256
256-bit opaque blob.
Definition
uint256.h:178
hasher.h
net.h
NodeId
int64_t NodeId
Definition
net.h:97
params.h
CBlockLocator
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
Definition
block.h:124
CompressedHeader
Definition
headerssync.h:21
CompressedHeader::nBits
uint32_t nBits
Definition
headerssync.h:26
CompressedHeader::GetFullHeader
CBlockHeader GetFullHeader(const uint256 &hash_prev_block)
Definition
headerssync.h:43
CompressedHeader::nTime
uint32_t nTime
Definition
headerssync.h:25
CompressedHeader::nNonce
uint32_t nNonce
Definition
headerssync.h:27
CompressedHeader::hashMerkleRoot
uint256 hashMerkleRoot
Definition
headerssync.h:24
CompressedHeader::nVersion
int32_t nVersion
Definition
headerssync.h:23
CompressedHeader::CompressedHeader
CompressedHeader(const CBlockHeader &header)
Definition
headerssync.h:34
CompressedHeader::CompressedHeader
CompressedHeader()
Definition
headerssync.h:29
Consensus::Params
Parameters that influence chain consensus.
Definition
params.h:74
HeadersSyncState::ProcessingResult
Result data structure for ProcessNextHeaders.
Definition
headerssync.h:142
HeadersSyncState::ProcessingResult::request_more
bool request_more
Definition
headerssync.h:145
HeadersSyncState::ProcessingResult::pow_validated_headers
std::vector< CBlockHeader > pow_validated_headers
Definition
headerssync.h:143
HeadersSyncState::ProcessingResult::success
bool success
Definition
headerssync.h:144
uint256.h
Generated on Thu Oct 3 2024 09:20:15 for Bitcoin Core by
1.12.0