Bitcoin Core
32.99.0
P2P Digital Currency
Loading...
Searching...
No Matches
src
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 <
kernel/chainparams.h
>
12
#include <
net.h
>
13
#include <
primitives/block.h
>
14
#include <
uint256.h
>
15
#include <
util/bitdeque.h
>
16
#include <
util/hasher.h
>
17
18
#include <deque>
19
#include <stdexcept>
20
#include <vector>
21
22
// A compressed CBlockHeader, which leaves out the prevhash
23
struct
CompressedHeader
{
24
// header
25
int32_t
nVersion
{0};
26
uint256
hashMerkleRoot
;
27
uint32_t
nTime
{0};
28
uint32_t
nBits
{0};
29
uint32_t
nNonce
{0};
30
31
CompressedHeader
()
32
{
33
hashMerkleRoot
.
SetNull
();
34
}
35
36
explicit
CompressedHeader
(
const
CBlockHeader
& header)
37
:
nVersion
{header.
nVersion
},
38
hashMerkleRoot
{header.
hashMerkleRoot
},
39
nTime
{header.
nTime
},
40
nBits
{header.
nBits
},
41
nNonce
{header.
nNonce
}
42
{
43
}
44
45
CBlockHeader
GetFullHeader
(
const
uint256
&
hash_prev_block
)
const
46
{
47
CBlockHeader
ret
;
48
ret
.
nVersion
=
nVersion
;
49
ret
.hashPrevBlock =
hash_prev_block
;
50
ret
.hashMerkleRoot =
hashMerkleRoot
;
51
ret
.nTime =
nTime
;
52
ret
.nBits =
nBits
;
53
ret
.nNonce =
nNonce
;
54
return
ret
;
55
};
56
};
57
104
class
HeadersSyncState
105
{
106
public
:
107
struct
SystemClockError
: std::runtime_error {
108
using
std::runtime_error::runtime_error;
109
};
110
111
~HeadersSyncState
() =
default
;
112
113
enum class
State
{
117
PRESYNC
,
121
REDOWNLOAD
,
123
FINAL
124
};
125
127
State
GetState
()
const
{
return
m_download_state
; }
128
130
int64_t
GetPresyncHeight
()
const
{
return
m_current_height
; }
131
133
uint32_t
GetPresyncTime
()
const
{
return
m_last_header_received
.
nTime
; }
134
136
arith_uint256
GetPresyncWork
()
const
{
return
m_current_chain_work
; }
137
148
HeadersSyncState
(
NodeId
id
,
const
Consensus::Params
&
consensus_params
,
149
const
HeadersSyncParams
& params,
const
CBlockIndex
& chain_start,
150
const
arith_uint256
&
minimum_required_work
);
151
153
struct
ProcessingResult
{
154
std::vector<CBlockHeader>
pow_validated_headers
;
155
bool
success
{
false
};
156
bool
request_more
{
false
};
157
};
158
179
ProcessingResult
ProcessNextHeaders
(std::span<const CBlockHeader>
180
received_headers
,
bool
full_headers_message
);
181
187
CBlockLocator
NextHeadersRequestLocator
()
const
;
188
189
protected
:
194
const
size_t
m_commit_offset
;
195
196
private
:
200
void
Finalize
();
201
209
bool
ValidateAndStoreHeadersCommitments
(std::span<const CBlockHeader>
headers
);
210
212
bool
ValidateAndProcessSingleHeader
(
const
CBlockHeader
&
current
);
213
216
bool
ValidateAndStoreRedownloadedHeader
(
const
CBlockHeader
& header);
217
219
std::vector<CBlockHeader>
PopHeadersReadyForAcceptance
();
220
221
private
:
223
const
NodeId
m_id
;
224
226
const
Consensus::Params
&
m_consensus_params
;
227
229
const
HeadersSyncParams
m_params
;
230
232
const
CBlockIndex
&
m_chain_start
;
233
235
const
arith_uint256
m_minimum_required_work
;
236
238
arith_uint256
m_current_chain_work
;
239
241
const
SaltedUint256Hasher
m_hasher
;
242
244
bitdeque<>
m_header_commitments
;
245
251
uint64_t
m_max_commitments
{0};
252
254
CBlockHeader
m_last_header_received
;
255
257
int64_t
m_current_height
{0};
258
262
std::deque<CompressedHeader>
m_redownloaded_headers
;
263
265
int64_t
m_redownload_buffer_last_height
{0};
266
271
uint256
m_redownload_buffer_last_hash
;
272
277
uint256
m_redownload_buffer_first_prev_hash
;
278
280
arith_uint256
m_redownload_chain_work
;
281
286
bool
m_process_all_remaining_headers
{
false
};
287
289
State
m_download_state
{State::PRESYNC};
290
};
291
292
#endif
// BITCOIN_HEADERSSYNC_H
arith_uint256.h
ret
int ret
Definition
bitcoin-cli.cpp:1689
bitdeque.h
block.h
CBlockHeader
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition
block.h:27
CBlockHeader::nTime
uint32_t nTime
Definition
block.h:33
CBlockHeader::nVersion
int32_t nVersion
Definition
block.h:30
CBlockIndex
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition
chain.h:94
HeadersSyncState
HeadersSyncState:
Definition
headerssync.h:105
HeadersSyncState::GetPresyncWork
arith_uint256 GetPresyncWork() const
Return the amount of work in the chain received during the PRESYNC phase.
Definition
headerssync.h:136
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:251
HeadersSyncState::m_redownload_chain_work
arith_uint256 m_redownload_chain_work
The accumulated work on the redownloaded chain.
Definition
headerssync.h:280
HeadersSyncState::State
State
Definition
headerssync.h:113
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::ValidateAndStoreHeadersCommitments
bool ValidateAndStoreHeadersCommitments(std::span< const CBlockHeader > headers)
Only called in PRESYNC.
Definition
headerssync.cpp:147
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:254
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:238
HeadersSyncState::m_current_height
int64_t m_current_height
Height of m_last_header_received.
Definition
headerssync.h:257
HeadersSyncState::GetPresyncTime
uint32_t GetPresyncTime() const
Return the block timestamp of the last header received during the PRESYNC phase.
Definition
headerssync.h:133
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:235
HeadersSyncState::PopHeadersReadyForAcceptance
std::vector< CBlockHeader > PopHeadersReadyForAcceptance()
Return a set of headers that satisfy our proof-of-work threshold.
Definition
headerssync.cpp:288
HeadersSyncState::m_consensus_params
const Consensus::Params & m_consensus_params
We use the consensus params in our anti-DoS calculations.
Definition
headerssync.h:226
HeadersSyncState::ValidateAndProcessSingleHeader
bool ValidateAndProcessSingleHeader(const CBlockHeader ¤t)
In PRESYNC, process and update state for a single header.
Definition
headerssync.cpp:185
HeadersSyncState::~HeadersSyncState
~HeadersSyncState()=default
HeadersSyncState::m_download_state
State m_download_state
Current state of our headers sync.
Definition
headerssync.h:289
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:223
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:244
HeadersSyncState::m_id
const NodeId m_id
NodeId of the peer (used for log messages)
Definition
headerssync.h:223
HeadersSyncState::ProcessNextHeaders
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:76
HeadersSyncState::m_redownload_buffer_last_height
int64_t m_redownload_buffer_last_height
Height of last header in m_redownloaded_headers.
Definition
headerssync.h:265
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:262
HeadersSyncState::GetState
State GetState() const
Return the current state of our download.
Definition
headerssync.h:127
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:286
HeadersSyncState::m_params
const HeadersSyncParams m_params
Parameters that impact memory usage for a given chain, especially when attacked.
Definition
headerssync.h:229
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:59
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:271
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:277
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:232
HeadersSyncState::m_commit_offset
const size_t m_commit_offset
The (secret) offset on the heights for which to create commitments.
Definition
headerssync.h:194
HeadersSyncState::m_hasher
const SaltedUint256Hasher m_hasher
m_hasher is a salted hasher for making our 1-bit commitments to headers we've seen.
Definition
headerssync.h:241
HeadersSyncState::GetPresyncHeight
int64_t GetPresyncHeight() const
Return the height reached during the PRESYNC phase.
Definition
headerssync.h:130
HeadersSyncState::NextHeadersRequestLocator
CBlockLocator NextHeadersRequestLocator() const
Issue the next GETHEADERS message to our peer.
Definition
headerssync.cpp:304
SaltedUint256Hasher
Definition
hasher.h:18
arith_uint256
256-bit unsigned big integer.
Definition
arith_uint256.h:231
base_blob::SetNull
constexpr void SetNull()
Definition
uint256.h:57
bitdeque
Class that mimics std::deque<bool>, but with std::vector<bool>'s bit packing.
Definition
bitdeque.h:24
uint256
256-bit opaque blob.
Definition
uint256.h:196
hasher.h
headers
HTTPHeaders headers
Definition
httpserver_tests.cpp:172
chain.h
chainparams.h
NodeId
int64_t NodeId
Identifier of a peer connection.
Definition
net_types.h:15
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:117
CompressedHeader
Definition
headerssync.h:23
CompressedHeader::nBits
uint32_t nBits
Definition
headerssync.h:28
CompressedHeader::nTime
uint32_t nTime
Definition
headerssync.h:27
CompressedHeader::nNonce
uint32_t nNonce
Definition
headerssync.h:29
CompressedHeader::hashMerkleRoot
uint256 hashMerkleRoot
Definition
headerssync.h:26
CompressedHeader::nVersion
int32_t nVersion
Definition
headerssync.h:25
CompressedHeader::GetFullHeader
CBlockHeader GetFullHeader(const uint256 &hash_prev_block) const
Definition
headerssync.h:45
CompressedHeader::CompressedHeader
CompressedHeader(const CBlockHeader &header)
Definition
headerssync.h:36
CompressedHeader::CompressedHeader
CompressedHeader()
Definition
headerssync.h:31
Consensus::Params
Parameters that influence chain consensus.
Definition
params.h:88
HeadersSyncParams
Configuration for headers sync memory usage.
Definition
chainparams.h:64
HeadersSyncState::ProcessingResult
Result data structure for ProcessNextHeaders.
Definition
headerssync.h:153
HeadersSyncState::ProcessingResult::request_more
bool request_more
Definition
headerssync.h:156
HeadersSyncState::ProcessingResult::pow_validated_headers
std::vector< CBlockHeader > pow_validated_headers
Definition
headerssync.h:154
HeadersSyncState::ProcessingResult::success
bool success
Definition
headerssync.h:155
HeadersSyncState::SystemClockError
Definition
headerssync.h:107
net.h
uint256.h
Ticks
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
Definition
time.h:82
Generated on Thu Nov 12 2026 20:00:27 for Bitcoin Core by
1.9.8