Bitcoin ABC 0.31.0
P2P Digital Currency
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
blockindex.h
Go to the documentation of this file.
1// Copyright (c) 2009-2020 The Bitcoin 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_BLOCKINDEX_H
6#define BITCOIN_BLOCKINDEX_H
7
8#include <arith_uint256.h>
9#include <blockstatus.h>
10#include <flatfile.h>
11#include <kernel/cs_main.h>
12#include <primitives/block.h>
13#include <sync.h>
14#include <uint256.h>
15#include <util/time.h>
16
17struct BlockHash;
18
26public:
29 const BlockHash *phashBlock{nullptr};
30
32 CBlockIndex *pprev{nullptr};
33
35 CBlockIndex *pskip{nullptr};
36
38 int nHeight{0};
39
41 int nFile GUARDED_BY(::cs_main){0};
42
44 unsigned int nDataPos GUARDED_BY(::cs_main){0};
45
47 unsigned int nUndoPos GUARDED_BY(::cs_main){0};
48
52
55 unsigned int nTx{0};
56
60 unsigned int nSize{0};
61
68 unsigned int nChainTx{0};
69
72
74 int32_t nVersion{0};
76 uint32_t nTime{0};
77 uint32_t nBits{0};
78 uint32_t nNonce{0};
79
82 int32_t nSequenceId{0};
83
85 int64_t nTimeReceived{0};
86
88 unsigned int nTimeMax{0};
89
90 explicit CBlockIndex() = default;
91
92 explicit CBlockIndex(const CBlockHeader &block)
94 nTime{block.nTime}, nBits{block.nBits}, nNonce{block.nNonce},
95 nTimeReceived{0} {}
96
99 FlatFilePos ret;
100 if (nStatus.hasData()) {
101 ret.nFile = nFile;
102 ret.nPos = nDataPos;
103 }
104 return ret;
105 }
106
109 FlatFilePos ret;
110 if (nStatus.hasUndo()) {
111 ret.nFile = nFile;
112 ret.nPos = nUndoPos;
113 }
114 return ret;
115 }
116
118 CBlockHeader block;
119 block.nVersion = nVersion;
120 if (pprev) {
122 }
124 block.nTime = nTime;
125 block.nBits = nBits;
126 block.nNonce = nNonce;
127 return block;
128 }
129
131 assert(phashBlock != nullptr);
132 return *phashBlock;
133 }
134
138 int64_t GetChainTxCount() const { return nChainTx; }
139
143 void ResetChainStats();
144
150 void MaybeResetChainStats(bool is_snapshot_base_block);
151
157 bool UpdateChainStats();
158
173 bool HaveNumChainTxs() const { return GetChainTxCount() != 0; }
174
176 return NodeSeconds{std::chrono::seconds{nTime}};
177 }
178
179 int64_t GetBlockTime() const { return int64_t(nTime); }
180
181 int64_t GetBlockTimeMax() const { return int64_t(nTimeMax); }
182
183 int64_t GetHeaderReceivedTime() const { return nTimeReceived; }
184
185 int64_t GetReceivedTimeDiff() const {
187 }
188
189 static constexpr int nMedianTimeSpan = 11;
190
191 int64_t GetMedianTimePast() const {
192 int64_t pmedian[nMedianTimeSpan];
193 int64_t *pbegin = &pmedian[nMedianTimeSpan];
194 int64_t *pend = &pmedian[nMedianTimeSpan];
195
196 const CBlockIndex *pindex = this;
197 for (int i = 0; i < nMedianTimeSpan && pindex;
198 i++, pindex = pindex->pprev) {
199 *(--pbegin) = pindex->GetBlockTime();
200 }
201
202 std::sort(pbegin, pend);
203 return pbegin[(pend - pbegin) / 2];
204 }
205
206 std::string ToString() const;
207
213 return nStatus.isValid(nUpTo);
214 }
215
221 // Only validity flags allowed.
222 if (nStatus.isInvalid()) {
223 return false;
224 }
225
226 if (nStatus.getValidity() >= nUpTo) {
227 return false;
228 }
229
230 nStatus = nStatus.withValidity(nUpTo);
231 return true;
232 }
233
235 void BuildSkip();
236
238 CBlockIndex *GetAncestor(int height);
239 const CBlockIndex *GetAncestor(int height) const;
240};
241
242#endif // BITCOIN_BLOCKINDEX_H
BlockValidity
Definition: blockvalidity.h:10
@ TRANSACTIONS
Only first tx is coinbase, 2 <= coinbase input script length <= 100, transactions valid,...
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
BlockHash hashPrevBlock
Definition: block.h:27
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
bool IsValid(enum BlockValidity nUpTo=BlockValidity::TRANSACTIONS) const EXCLUSIVE_LOCKS_REQUIRED(
Check whether this block index entry is valid up to the passed validity level.
Definition: blockindex.h:210
uint256 hashMerkleRoot
Definition: blockindex.h:75
void ResetChainStats()
Reset chain tx stats.
Definition: blockindex.cpp:36
std::string ToString() const
Definition: blockindex.cpp:30
CBlockIndex * pprev
pointer to the index of the predecessor of this block
Definition: blockindex.h:32
void BuildSkip()
Build the skiplist pointer for this entry.
Definition: blockindex.cpp:107
int64_t GetHeaderReceivedTime() const
Definition: blockindex.h:183
void MaybeResetChainStats(bool is_snapshot_base_block)
Reset chain tx stats and log a warning if the block is not the snapshot block, and the nChainTx value...
Definition: blockindex.cpp:40
CBlockHeader GetBlockHeader() const
Definition: blockindex.h:117
arith_uint256 nChainWork
(memory only) Total amount of work (expected number of hashes) in the chain up to and including this ...
Definition: blockindex.h:51
const BlockHash * phashBlock
pointer to the hash of the block, if any.
Definition: blockindex.h:29
CBlockIndex()=default
unsigned int nDataPos GUARDED_BY(::cs_main)
Byte offset within blk?????.dat where this block's data is stored.
Definition: blockindex.h:44
unsigned int nUndoPos GUARDED_BY(::cs_main)
Byte offset within rev?????.dat where this block's undo data is stored.
Definition: blockindex.h:47
BlockStatus nStatus GUARDED_BY(::cs_main)
Verification status of this block. See enum BlockStatus.
Definition: blockindex.h:71
int64_t GetChainTxCount() const
Get the number of transaction in the chain so far.
Definition: blockindex.h:138
bool HaveNumChainTxs() const
Check whether this block and all previous blocks back to the genesis block or an assumeutxo snapshot ...
Definition: blockindex.h:173
uint32_t nTime
Definition: blockindex.h:76
unsigned int nTimeMax
(memory only) Maximum nTime in the chain up to and including this block.
Definition: blockindex.h:88
int32_t nSequenceId
(memory only) Sequential id assigned to distinguish order in which blocks are received.
Definition: blockindex.h:82
uint32_t nNonce
Definition: blockindex.h:78
int64_t GetReceivedTimeDiff() const
Definition: blockindex.h:185
int64_t GetBlockTime() const
Definition: blockindex.h:179
int64_t GetMedianTimePast() const
Definition: blockindex.h:191
FlatFilePos GetUndoPos() const EXCLUSIVE_LOCKS_REQUIRED(
Definition: blockindex.h:107
uint32_t nBits
Definition: blockindex.h:77
bool UpdateChainStats()
Update chain tx stats and return True if this block is the genesis block or all parents have their tx...
Definition: blockindex.cpp:56
CBlockIndex * pskip
pointer to the index of some further predecessor of this block
Definition: blockindex.h:35
int64_t GetBlockTimeMax() const
Definition: blockindex.h:181
unsigned int nTx
Number of transactions in this block.
Definition: blockindex.h:55
CBlockIndex(const CBlockHeader &block)
Definition: blockindex.h:92
bool RaiseValidity(enum BlockValidity nUpTo) EXCLUSIVE_LOCKS_REQUIRED(
Raise the validity level of this block index entry.
Definition: blockindex.h:218
NodeSeconds Time() const
Definition: blockindex.h:175
int32_t nVersion
block header
Definition: blockindex.h:74
int64_t nTimeReceived
(memory only) block header metadata
Definition: blockindex.h:85
CBlockIndex * GetAncestor(int height)
Efficiently find an ancestor of this block.
Definition: blockindex.cpp:102
BlockHash GetBlockHash() const
Definition: blockindex.h:130
unsigned int nSize
Size of this block.
Definition: blockindex.h:60
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: blockindex.h:38
FlatFilePos GetBlockPos() const EXCLUSIVE_LOCKS_REQUIRED(
Definition: blockindex.h:97
unsigned int nChainTx
(memory only) Number of transactions in the chain up to and including this block.
Definition: blockindex.h:68
int nFile GUARDED_BY(::cs_main)
Which # file this block is stored in (blk?????.dat)
Definition: blockindex.h:41
static constexpr int nMedianTimeSpan
Definition: blockindex.h:189
256-bit unsigned big integer.
256-bit opaque blob.
Definition: uint256.h:129
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: cs_main.cpp:7
A BlockHash is a unqiue identifier for a block.
Definition: blockhash.h:13
int nFile
Definition: flatfile.h:15
unsigned int nPos
Definition: flatfile.h:16
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:56
std::chrono::time_point< NodeClock, std::chrono::seconds > NodeSeconds
Definition: time.h:25
AssertLockHeld(pool.cs)
assert(!tx.IsCoinBase())