Bitcoin ABC  0.26.3
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 <sync.h>
13 #include <tinyformat.h>
14 #include <uint256.h>
15 
16 struct BlockHash;
17 
18 extern RecursiveMutex cs_main;
19 
26 class CBlockIndex {
27 public:
30  const BlockHash *phashBlock{nullptr};
31 
33  CBlockIndex *pprev{nullptr};
34 
36  CBlockIndex *pskip{nullptr};
37 
39  int nHeight{0};
40 
42  int nFile GUARDED_BY(::cs_main){0};
43 
45  unsigned int nDataPos GUARDED_BY(::cs_main){0};
46 
48  unsigned int nUndoPos GUARDED_BY(::cs_main){0};
49 
53 
61  unsigned int nTx{0};
62 
66  unsigned int nSize{0};
67 
78  unsigned int nChainTx{0};
79 
80 private:
84  uint64_t nChainSize{0};
85 
86 public:
89 
91  int32_t nVersion{0};
93  uint32_t nTime{0};
94  uint32_t nBits{0};
95  uint32_t nNonce{0};
96 
99  int32_t nSequenceId{0};
100 
102  uint64_t nTimeReceived{0};
103 
105  unsigned int nTimeMax{0};
106 
107  explicit CBlockIndex() = default;
108 
109  explicit CBlockIndex(const CBlockHeader &block)
111  nTime{block.nTime}, nBits{block.nBits}, nNonce{block.nNonce},
112  nTimeReceived{0} {}
113 
116  FlatFilePos ret;
117  if (nStatus.hasData()) {
118  ret.nFile = nFile;
119  ret.nPos = nDataPos;
120  }
121  return ret;
122  }
123 
126  FlatFilePos ret;
127  if (nStatus.hasUndo()) {
128  ret.nFile = nFile;
129  ret.nPos = nUndoPos;
130  }
131  return ret;
132  }
133 
135  CBlockHeader block;
136  block.nVersion = nVersion;
137  if (pprev) {
138  block.hashPrevBlock = pprev->GetBlockHash();
139  }
141  block.nTime = nTime;
142  block.nBits = nBits;
143  block.nNonce = nNonce;
144  return block;
145  }
146 
147  BlockHash GetBlockHash() const { return *phashBlock; }
148 
152  int64_t GetChainTxCount() const { return nChainTx; }
153 
157  uint64_t GetChainSize() const { return nChainSize; }
158 
162  bool UpdateChainStats();
163 
172  bool HaveTxsDownloaded() const { return GetChainTxCount() != 0; }
173 
174  int64_t GetBlockTime() const { return int64_t(nTime); }
175 
176  int64_t GetBlockTimeMax() const { return int64_t(nTimeMax); }
177 
178  int64_t GetHeaderReceivedTime() const { return nTimeReceived; }
179 
180  int64_t GetReceivedTimeDiff() const {
181  return GetHeaderReceivedTime() - GetBlockTime();
182  }
183 
184  static constexpr int nMedianTimeSpan = 11;
185 
186  int64_t GetMedianTimePast() const {
187  int64_t pmedian[nMedianTimeSpan];
188  int64_t *pbegin = &pmedian[nMedianTimeSpan];
189  int64_t *pend = &pmedian[nMedianTimeSpan];
190 
191  const CBlockIndex *pindex = this;
192  for (int i = 0; i < nMedianTimeSpan && pindex;
193  i++, pindex = pindex->pprev) {
194  *(--pbegin) = pindex->GetBlockTime();
195  }
196 
197  std::sort(pbegin, pend);
198  return pbegin[(pend - pbegin) / 2];
199  }
200 
201  std::string ToString() const {
202  return strprintf(
203  "CBlockIndex(pprev=%p, nHeight=%d, merkle=%s, hashBlock=%s)", pprev,
205  }
206 
212  return nStatus.isValid(nUpTo);
213  }
214 
219  return nStatus.isAssumedValid();
220  }
221 
224  bool RaiseValidity(enum BlockValidity nUpTo)
227  // Only validity flags allowed.
228  if (nStatus.isInvalid()) {
229  return false;
230  }
231 
232  if (nStatus.getValidity() >= nUpTo) {
233  return false;
234  }
235 
236  // If this block had been marked assumed-valid and we're raising
237  // its validity to a certain point, there is no longer an assumption.
238  if (IsAssumedValid() && nUpTo >= BlockValidity::SCRIPTS) {
239  nStatus = nStatus.withClearedAssumedValidFlags();
240  }
241 
242  nStatus = nStatus.withValidity(nUpTo);
243  return true;
244  }
245 
247  void BuildSkip();
248 
250  CBlockIndex *GetAncestor(int height);
251  const CBlockIndex *GetAncestor(int height) const;
252 };
253 
254 #endif // BITCOIN_BLOCKINDEX_H
RecursiveMutex cs_main
Global state.
Definition: validation.cpp:113
BlockValidity
Definition: blockvalidity.h:10
@ TRANSACTIONS
Only first tx is coinbase, 2 <= coinbase input script length <= 100, transactions valid,...
@ SCRIPTS
Scripts & signatures ok.
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition: block.h:22
uint32_t nNonce
Definition: block.h:30
uint32_t nBits
Definition: block.h:29
uint32_t nTime
Definition: block.h:28
BlockHash hashPrevBlock
Definition: block.h:26
int32_t nVersion
Definition: block.h:25
uint256 hashMerkleRoot
Definition: block.h:27
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: blockindex.h:26
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:209
uint256 hashMerkleRoot
Definition: blockindex.h:92
uint64_t nTimeReceived
(memory only) block header metadata
Definition: blockindex.h:102
std::string ToString() const
Definition: blockindex.h:201
CBlockIndex * pprev
pointer to the index of the predecessor of this block
Definition: blockindex.h:33
bool IsAssumedValid() const EXCLUSIVE_LOCKS_REQUIRED(
Definition: blockindex.h:217
void BuildSkip()
Build the skiplist pointer for this entry.
Definition: blockindex.cpp:76
int64_t GetHeaderReceivedTime() const
Definition: blockindex.h:178
CBlockHeader GetBlockHeader() const
Definition: blockindex.h:134
uint64_t nChainSize
(memory only) Size of all blocks in the chain up to and including this block.
Definition: blockindex.h:84
arith_uint256 nChainWork
(memory only) Total amount of work (expected number of hashes) in the chain up to and including this ...
Definition: blockindex.h:52
const BlockHash * phashBlock
pointer to the hash of the block, if any.
Definition: blockindex.h:30
CBlockIndex()=default
bool HaveTxsDownloaded() const
Check whether this block's and all previous blocks' transactions have been downloaded (and stored to ...
Definition: blockindex.h:172
unsigned int nDataPos GUARDED_BY(::cs_main)
Byte offset within blk?????.dat where this block's data is stored.
Definition: blockindex.h:45
unsigned int nUndoPos GUARDED_BY(::cs_main)
Byte offset within rev?????.dat where this block's undo data is stored.
Definition: blockindex.h:48
BlockStatus nStatus GUARDED_BY(::cs_main)
Verification status of this block. See enum BlockStatus.
Definition: blockindex.h:88
int64_t GetChainTxCount() const
Get the number of transaction in the chain so far.
Definition: blockindex.h:152
uint32_t nTime
Definition: blockindex.h:93
unsigned int nTimeMax
(memory only) Maximum nTime in the chain up to and including this block.
Definition: blockindex.h:105
int32_t nSequenceId
(memory only) Sequential id assigned to distinguish order in which blocks are received.
Definition: blockindex.h:99
uint64_t GetChainSize() const
Get the size of all the blocks in the chain so far.
Definition: blockindex.h:157
uint32_t nNonce
Definition: blockindex.h:95
int64_t GetReceivedTimeDiff() const
Definition: blockindex.h:180
int64_t GetBlockTime() const
Definition: blockindex.h:174
int64_t GetMedianTimePast() const
Definition: blockindex.h:186
FlatFilePos GetUndoPos() const EXCLUSIVE_LOCKS_REQUIRED(
Definition: blockindex.h:124
uint32_t nBits
Definition: blockindex.h:94
bool UpdateChainStats()
Update chain tx stats.
Definition: blockindex.cpp:27
CBlockIndex * pskip
pointer to the index of some further predecessor of this block
Definition: blockindex.h:36
int64_t GetBlockTimeMax() const
Definition: blockindex.h:176
unsigned int nTx
Number of transactions in this block.
Definition: blockindex.h:61
CBlockIndex(const CBlockHeader &block)
Definition: blockindex.h:109
bool RaiseValidity(enum BlockValidity nUpTo) EXCLUSIVE_LOCKS_REQUIRED(
Raise the validity level of this block index entry.
Definition: blockindex.h:224
int32_t nVersion
block header
Definition: blockindex.h:91
CBlockIndex * GetAncestor(int height)
Efficiently find an ancestor of this block.
Definition: blockindex.cpp:71
BlockHash GetBlockHash() const
Definition: blockindex.h:147
unsigned int nSize
Size of this block.
Definition: blockindex.h:66
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: blockindex.h:39
FlatFilePos GetBlockPos() const EXCLUSIVE_LOCKS_REQUIRED(
Definition: blockindex.h:114
unsigned int nChainTx
(memory only) Number of transactions in the chain up to and including this block.
Definition: blockindex.h:78
int nFile GUARDED_BY(::cs_main)
Which # file this block is stored in (blk?????.dat)
Definition: blockindex.h:42
static constexpr int nMedianTimeSpan
Definition: blockindex.h:184
256-bit unsigned big integer.
std::string ToString() const
Definition: uint256.h:78
256-bit opaque blob.
Definition: uint256.h:127
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
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1201
AssertLockHeld(pool.cs)