Bitcoin ABC  0.24.7
P2P Digital Currency
chain.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #ifndef BITCOIN_CHAIN_H
7 #define BITCOIN_CHAIN_H
8 
9 #include <arith_uint256.h>
10 #include <blockindex.h>
11 #include <blockstatus.h>
12 #include <blockvalidity.h>
13 #include <consensus/params.h>
14 #include <crypto/common.h> // for ReadLE64
15 #include <flatfile.h>
16 #include <primitives/block.h>
17 #include <sync.h>
18 #include <tinyformat.h>
19 #include <uint256.h>
20 
21 #include <unordered_map>
22 #include <vector>
23 
28 static constexpr int64_t MAX_FUTURE_BLOCK_TIME = 2 * 60 * 60;
29 
36 static constexpr int64_t TIMESTAMP_WINDOW = MAX_FUTURE_BLOCK_TIME;
37 
44 static constexpr int64_t MAX_BLOCK_TIME_GAP = 90 * 60;
45 
49 struct BlockHasher {
50  // this used to call `GetCheapHash()` in uint256, which was later moved; the
51  // cheap hash function simply calls ReadLE64() however, so the end result is
52  // identical
53  size_t operator()(const BlockHash &hash) const {
54  return ReadLE64(hash.begin());
55  }
56 };
57 
58 extern RecursiveMutex cs_main;
59 
61 
68  const CBlockIndex &from,
69  const CBlockIndex &tip,
70  const Consensus::Params &);
75  const CBlockIndex *pb);
76 
80 bool AreOnTheSameFork(const CBlockIndex *pa, const CBlockIndex *pb);
81 
83 class CDiskBlockIndex : public CBlockIndex {
84 public:
85  static constexpr int TRACK_SIZE_VERSION = 220800;
86 
88 
90 
91  explicit CDiskBlockIndex(const CBlockIndex *pindex) : CBlockIndex(*pindex) {
93  }
94 
96  int _nVersion = s.GetVersion();
97  if (!(s.GetType() & SER_GETHASH)) {
99  }
100 
102  READWRITE(obj.nStatus);
103  READWRITE(VARINT(obj.nTx));
104 
105  // The size of the blocks are tracked starting at version 0.22.8
106  if (obj.nStatus.hasData() && _nVersion >= TRACK_SIZE_VERSION) {
107  READWRITE(VARINT(obj.nSize));
108  }
109 
110  if (obj.nStatus.hasData() || obj.nStatus.hasUndo()) {
112  }
113  if (obj.nStatus.hasData()) {
114  READWRITE(VARINT(obj.nDataPos));
115  }
116  if (obj.nStatus.hasUndo()) {
117  READWRITE(VARINT(obj.nUndoPos));
118  }
119 
120  // block header
121  READWRITE(obj.nVersion);
122  READWRITE(obj.hashPrev);
123  READWRITE(obj.hashMerkleRoot);
124  READWRITE(obj.nTime);
125  READWRITE(obj.nBits);
126  READWRITE(obj.nNonce);
127  }
128 
130  CBlockHeader block;
131  block.nVersion = nVersion;
132  block.hashPrevBlock = hashPrev;
134  block.nTime = nTime;
135  block.nBits = nBits;
136  block.nNonce = nNonce;
137  return block.GetHash();
138  }
139 
140  std::string ToString() const {
141  std::string str = "CDiskBlockIndex(";
142  str += CBlockIndex::ToString();
143  str += strprintf("\n hashBlock=%s, hashPrev=%s)",
145  return str;
146  }
147 };
148 
152 class CChain {
153 private:
154  std::vector<CBlockIndex *> vChain;
155 
156 public:
161  CBlockIndex *Genesis() const {
162  return vChain.size() > 0 ? vChain[0] : nullptr;
163  }
164 
168  CBlockIndex *Tip() const {
169  return vChain.size() > 0 ? vChain[vChain.size() - 1] : nullptr;
170  }
171 
177  if (nHeight < 0 || nHeight >= (int)vChain.size()) {
178  return nullptr;
179  }
180  return vChain[nHeight];
181  }
182 
184  bool Contains(const CBlockIndex *pindex) const {
185  return (*this)[pindex->nHeight] == pindex;
186  }
187 
192  CBlockIndex *Next(const CBlockIndex *pindex) const {
193  if (!Contains(pindex)) {
194  return nullptr;
195  }
196 
197  return (*this)[pindex->nHeight + 1];
198  }
199 
204  int Height() const { return vChain.size() - 1; }
205 
207  void SetTip(CBlockIndex *pindex);
208 
213  CBlockLocator GetLocator(const CBlockIndex *pindex = nullptr) const;
214 
218  const CBlockIndex *FindFork(const CBlockIndex *pindex) const;
219 
224  CBlockIndex *FindEarliestAtLeast(int64_t nTime, int height) const;
225 };
226 
227 #endif // BITCOIN_CHAIN_H
MAX_FUTURE_BLOCK_TIME
static constexpr int64_t MAX_FUTURE_BLOCK_TIME
Maximum amount of time that a block timestamp is allowed to exceed the current network-adjusted time ...
Definition: chain.h:28
block.h
AreOnTheSameFork
bool AreOnTheSameFork(const CBlockIndex *pa, const CBlockIndex *pb)
Check if two block index are on the same fork.
Definition: chain.cpp:142
arith_uint256.h
CBlockIndex::nTime
uint32_t nTime
Definition: blockindex.h:81
CBlockHeader::hashMerkleRoot
uint256 hashMerkleRoot
Definition: block.h:27
CDiskBlockIndex::GetBlockHash
BlockHash GetBlockHash() const
Definition: chain.h:129
CBlockHeader::nBits
uint32_t nBits
Definition: block.h:29
nHeight
unsigned int nHeight
Definition: mempool_eviction.cpp:13
MAX_BLOCK_TIME_GAP
static constexpr int64_t MAX_BLOCK_TIME_GAP
Maximum gap between node time and block time used for the "Catching up..." mode in GUI.
Definition: chain.h:44
CBlockHeader
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition: block.h:22
sync.h
CBlockHeader::nVersion
int32_t nVersion
Definition: block.h:25
VARINT_MODE
#define VARINT_MODE(obj, mode)
Definition: serialize.h:595
CBlockIndex::nBits
uint32_t nBits
Definition: blockindex.h:82
uint256.h
arith_uint256
256-bit unsigned big integer.
Definition: arith_uint256.h:285
flatfile.h
CBlockIndex::pprev
CBlockIndex * pprev
pointer to the index of the predecessor of this block
Definition: blockindex.h:30
CBlockIndex::nHeight
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: blockindex.h:36
ReadLE64
static uint64_t ReadLE64(const uint8_t *ptr)
Definition: common.h:29
CBlockIndex::ToString
std::string ToString() const
Definition: blockindex.h:187
CDiskBlockIndex::CDiskBlockIndex
CDiskBlockIndex(const CBlockIndex *pindex)
Definition: chain.h:91
VarIntMode::NONNEGATIVE_SIGNED
@ NONNEGATIVE_SIGNED
AnnotatedMixin< std::recursive_mutex >
CDiskBlockIndex
Used to marshal pointers into hashes for db storage.
Definition: chain.h:83
CChain::Tip
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
Definition: chain.h:168
BlockHasher::operator()
size_t operator()(const BlockHash &hash) const
Definition: chain.h:53
blockvalidity.h
BlockHasher
Maintain a map of CBlockIndex for all known headers.
Definition: chain.h:49
CBlockIndex::GetBlockHash
BlockHash GetBlockHash() const
Definition: blockindex.h:133
CChain::FindFork
const CBlockIndex * FindFork(const CBlockIndex *pindex) const
Find the last common block between this chain and a block index entry.
Definition: chain.cpp:55
tinyformat.h
CBlockHeader::nNonce
uint32_t nNonce
Definition: block.h:30
CChain::FindEarliestAtLeast
CBlockIndex * FindEarliestAtLeast(int64_t nTime, int height) const
Find the earliest block with timestamp equal or greater than the given time and height equal or great...
Definition: chain.cpp:68
Consensus::Params
Parameters that influence chain consensus.
Definition: params.h:59
VARINT
#define VARINT(obj)
Definition: serialize.h:596
CBlockIndex::nVersion
int32_t nVersion
block header
Definition: blockindex.h:79
CBlockIndex::hashMerkleRoot
uint256 hashMerkleRoot
Definition: blockindex.h:80
CDiskBlockIndex::ToString
std::string ToString() const
Definition: chain.h:140
CChain::GetLocator
CBlockLocator GetLocator(const CBlockIndex *pindex=nullptr) const
Return a CBlockLocator that refers to a block in this chain (by default the tip).
Definition: chain.cpp:24
CBlockHeader::nTime
uint32_t nTime
Definition: block.h:28
CBlockHeader::hashPrevBlock
BlockHash hashPrevBlock
Definition: block.h:26
base_blob::ToString
std::string ToString() const
Definition: uint256.h:78
blockstatus.h
READWRITE
#define READWRITE(...)
Definition: serialize.h:179
CDiskBlockIndex::hashPrev
BlockHash hashPrev
Definition: chain.h:87
CChain::SetTip
void SetTip(CBlockIndex *pindex)
Set/initialize a chain with a given tip.
Definition: chain.cpp:11
base_blob::begin
uint8_t * begin()
Definition: uint256.h:83
CChain::Height
int Height() const
Return the maximal height in the chain.
Definition: chain.h:204
CChain::vChain
std::vector< CBlockIndex * > vChain
Definition: chain.h:154
BlockHash
A BlockHash is a unqiue identifier for a block.
Definition: blockhash.h:13
strprintf
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1201
SER_GETHASH
@ SER_GETHASH
Definition: serialize.h:167
GetBlockProof
arith_uint256 GetBlockProof(const CBlockIndex &block)
Definition: chain.cpp:80
CChain
An in-memory indexed chain of blocks.
Definition: chain.h:152
common.h
blockindex.h
params.h
CBlockIndex::nNonce
uint32_t nNonce
Definition: blockindex.h:83
CChain::Contains
bool Contains(const CBlockIndex *pindex) const
Efficiently check whether a block is present in this chain.
Definition: chain.h:184
GetBlockProofEquivalentTime
int64_t GetBlockProofEquivalentTime(const CBlockIndex &to, const CBlockIndex &from, const CBlockIndex &tip, const Consensus::Params &)
Return the time it would take to redo the work difference between from and to, assuming the current h...
Definition: chain.cpp:95
LastCommonAncestor
const CBlockIndex * LastCommonAncestor(const CBlockIndex *pa, const CBlockIndex *pb)
Find the forking point between two chain tips.
Definition: chain.cpp:118
CDiskBlockIndex::SERIALIZE_METHODS
SERIALIZE_METHODS(CDiskBlockIndex, obj)
Definition: chain.h:95
CDiskBlockIndex::CDiskBlockIndex
CDiskBlockIndex()
Definition: chain.h:89
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:100
cs_main
RecursiveMutex cs_main
Global state.
Definition: validation.cpp:103
CBlockIndex
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: blockindex.h:23
CDiskBlockIndex::TRACK_SIZE_VERSION
static constexpr int TRACK_SIZE_VERSION
Definition: chain.h:85
CBlockHeader::GetHash
BlockHash GetHash() const
Definition: block.cpp:11
CChain::operator[]
CBlockIndex * operator[](int nHeight) const
Returns the index entry at a particular height in this chain, or nullptr if no such height exists.
Definition: chain.h:176
CChain::Next
CBlockIndex * Next(const CBlockIndex *pindex) const
Find the successor of a block in this chain, or nullptr if the given index is not found or is the tip...
Definition: chain.h:192
CChain::Genesis
CBlockIndex * Genesis() const
Returns the index entry for the genesis block of this chain, or nullptr if none.
Definition: chain.h:161
TIMESTAMP_WINDOW
static constexpr int64_t TIMESTAMP_WINDOW
Timestamp window used as a grace period by code that compares external timestamps (such as timestamps...
Definition: chain.h:36