Bitcoin ABC  0.22.12
P2P Digital Currency
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 <primitives/block.h>
12 #include <tinyformat.h>
13 #include <uint256.h>
14 
15 struct BlockHash;
16 
23 class CBlockIndex {
24 public:
27  const BlockHash *phashBlock{nullptr};
28 
30  CBlockIndex *pprev{nullptr};
31 
33  CBlockIndex *pskip{nullptr};
34 
36  int nHeight{0};
37 
39  int nFile{0};
40 
42  unsigned int nDataPos{0};
43 
45  unsigned int nUndoPos{0};
46 
50 
54  unsigned int nTx{0};
55 
59  unsigned int nSize{0};
60 
61 private:
67  unsigned int nChainTx{0};
68 
72  uint64_t nChainSize{0};
73 
74 public:
77 
79  int32_t nVersion{0};
81  uint32_t nTime{0};
82  uint32_t nBits{0};
83  uint32_t nNonce{0};
84 
87  int32_t nSequenceId{0};
88 
90  uint64_t nTimeReceived{0};
91 
93  unsigned int nTimeMax{0};
94 
95  explicit CBlockIndex() = default;
96 
97  explicit CBlockIndex(const CBlockHeader &block)
98  : nVersion{block.nVersion}, hashMerkleRoot{block.hashMerkleRoot},
99  nTime{block.nTime}, nBits{block.nBits}, nNonce{block.nNonce},
100  nTimeReceived{0} {}
101 
103  FlatFilePos ret;
104  if (nStatus.hasData()) {
105  ret.nFile = nFile;
106  ret.nPos = nDataPos;
107  }
108  return ret;
109  }
110 
112  FlatFilePos ret;
113  if (nStatus.hasUndo()) {
114  ret.nFile = nFile;
115  ret.nPos = nUndoPos;
116  }
117  return ret;
118  }
119 
121  CBlockHeader block;
122  block.nVersion = nVersion;
123  if (pprev) {
124  block.hashPrevBlock = pprev->GetBlockHash();
125  }
127  block.nTime = nTime;
128  block.nBits = nBits;
129  block.nNonce = nNonce;
130  return block;
131  }
132 
133  BlockHash GetBlockHash() const { return *phashBlock; }
134 
138  int64_t GetChainTxCount() const { return nChainTx; }
139 
143  uint64_t GetChainSize() const { return nChainSize; }
144 
148  bool UpdateChainStats();
149 
158  bool HaveTxsDownloaded() const { return GetChainTxCount() != 0; }
159 
160  int64_t GetBlockTime() const { return int64_t(nTime); }
161 
162  int64_t GetBlockTimeMax() const { return int64_t(nTimeMax); }
163 
164  int64_t GetHeaderReceivedTime() const { return nTimeReceived; }
165 
166  int64_t GetReceivedTimeDiff() const {
167  return GetHeaderReceivedTime() - GetBlockTime();
168  }
169 
170  static constexpr int nMedianTimeSpan = 11;
171 
172  int64_t GetMedianTimePast() const {
173  int64_t pmedian[nMedianTimeSpan];
174  int64_t *pbegin = &pmedian[nMedianTimeSpan];
175  int64_t *pend = &pmedian[nMedianTimeSpan];
176 
177  const CBlockIndex *pindex = this;
178  for (int i = 0; i < nMedianTimeSpan && pindex;
179  i++, pindex = pindex->pprev) {
180  *(--pbegin) = pindex->GetBlockTime();
181  }
182 
183  std::sort(pbegin, pend);
184  return pbegin[(pend - pbegin) / 2];
185  }
186 
187  std::string ToString() const {
188  return strprintf(
189  "CBlockIndex(pprev=%p, nHeight=%d, merkle=%s, hashBlock=%s)", pprev,
191  }
192 
196  return nStatus.isValid(nUpTo);
197  }
198 
201  bool RaiseValidity(enum BlockValidity nUpTo) {
202  // Only validity flags allowed.
203  if (nStatus.isInvalid()) {
204  return false;
205  }
206 
207  if (nStatus.getValidity() >= nUpTo) {
208  return false;
209  }
210 
211  nStatus = nStatus.withValidity(nUpTo);
212  return true;
213  }
214 
216  void BuildSkip();
217 
219  CBlockIndex *GetAncestor(int height);
220  const CBlockIndex *GetAncestor(int height) const;
221 };
222 
223 #endif // BITCOIN_BLOCKINDEX_H
uint32_t nNonce
Definition: block.h:30
arith_uint256 nChainWork
(memory only) Total amount of work (expected number of hashes) in the chain up to and including this ...
Definition: blockindex.h:49
std::string ToString() const
Definition: blockindex.h:187
int32_t nSequenceId
(memory only) Sequential id assigned to distinguish order in which blocks are received.
Definition: blockindex.h:87
CBlockIndex * pskip
pointer to the index of some further predecessor of this block
Definition: blockindex.h:33
bool RaiseValidity(enum BlockValidity nUpTo)
Raise the validity level of this block index entry.
Definition: blockindex.h:201
int64_t GetBlockTime() const
Definition: blockindex.h:160
CBlockIndex * pprev
pointer to the index of the predecessor of this block
Definition: blockindex.h:30
const BlockHash * phashBlock
pointer to the hash of the block, if any.
Definition: blockindex.h:27
BlockStatus withValidity(BlockValidity validity) const
Definition: blockstatus.h:50
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1201
int64_t GetHeaderReceivedTime() const
Definition: blockindex.h:164
uint64_t nChainSize
(memory only) Size of all blocks in the chain up to and including this block.
Definition: blockindex.h:72
BlockStatus nStatus
Verification status of this block. See enum BlockStatus.
Definition: blockindex.h:76
bool hasData() const
Definition: blockstatus.h:54
CBlockHeader GetBlockHeader() const
Definition: blockindex.h:120
FlatFilePos GetBlockPos() const
Definition: blockindex.h:102
uint64_t GetChainSize() const
Get the size of all the blocks in the chain so far.
Definition: blockindex.h:143
uint32_t nTime
Definition: blockindex.h:81
int nFile
Which # file this block is stored in (blk?????.dat)
Definition: blockindex.h:39
int nFile
Definition: flatfile.h:15
BlockValidity
Definition: blockvalidity.h:10
uint32_t nTime
Definition: block.h:28
unsigned int nChainTx
(memory only) Number of transactions in the chain up to and including this block. ...
Definition: blockindex.h:67
CBlockIndex()=default
static constexpr int nMedianTimeSpan
Definition: blockindex.h:170
BlockHash GetBlockHash() const
Definition: blockindex.h:133
unsigned int nTimeMax
(memory only) Maximum nTime in the chain up to and including this block.
Definition: blockindex.h:93
uint256 hashMerkleRoot
Definition: block.h:27
uint32_t nNonce
Definition: blockindex.h:83
unsigned int nDataPos
Byte offset within blk?????.dat where this block&#39;s data is stored.
Definition: blockindex.h:42
bool hasUndo() const
Definition: blockstatus.h:60
CBlockIndex(const CBlockHeader &block)
Definition: blockindex.h:97
bool UpdateChainStats()
Update chain tx stats.
Definition: blockindex.cpp:27
int64_t GetBlockTimeMax() const
Definition: blockindex.h:162
bool isInvalid() const
Definition: blockstatus.h:102
uint256 hashMerkleRoot
Definition: blockindex.h:80
int64_t GetChainTxCount() const
Get the number of transaction in the chain so far.
Definition: blockindex.h:138
void BuildSkip()
Build the skiplist pointer for this entry.
Definition: blockindex.cpp:76
uint64_t nTimeReceived
(memory only) block header metadata
Definition: blockindex.h:90
std::string ToString() const
Definition: uint256.h:74
Only first tx is coinbase, 2 <= coinbase input script length <= 100, transactions valid...
256-bit unsigned big integer.
int64_t GetMedianTimePast() const
Definition: blockindex.h:172
unsigned int nSize
Size of this block.
Definition: blockindex.h:59
unsigned int nUndoPos
Byte offset within rev?????.dat where this block&#39;s undo data is stored.
Definition: blockindex.h:45
int32_t nVersion
block header
Definition: blockindex.h:79
256-bit opaque blob.
Definition: uint256.h:120
bool IsValid(enum BlockValidity nUpTo=BlockValidity::TRANSACTIONS) const
Check whether this block index entry is valid up to the passed validity level.
Definition: blockindex.h:195
int64_t GetReceivedTimeDiff() const
Definition: blockindex.h:166
A BlockHash is a unqiue identifier for a block.
Definition: blockhash.h:13
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: blockindex.h:23
BlockValidity getValidity() const
Definition: blockstatus.h:46
FlatFilePos GetUndoPos() const
Definition: blockindex.h:111
bool isValid(enum BlockValidity nUpTo=BlockValidity::TRANSACTIONS) const
Check whether this block index entry is valid up to the passed validity level.
Definition: blockstatus.h:94
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: blockindex.h:36
CBlockIndex * GetAncestor(int height)
Efficiently find an ancestor of this block.
Definition: blockindex.cpp:71
unsigned int nPos
Definition: flatfile.h:16
int32_t nVersion
Definition: block.h:25
bool HaveTxsDownloaded() const
Check whether this block&#39;s and all previous blocks&#39; transactions have been downloaded (and stored to ...
Definition: blockindex.h:158
uint32_t nBits
Definition: blockindex.h:82
unsigned int nTx
Number of transactions in this block.
Definition: blockindex.h:54
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition: block.h:22
BlockHash hashPrevBlock
Definition: block.h:26
uint32_t nBits
Definition: block.h:29