Bitcoin ABC  0.22.12
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 
89  CDiskBlockIndex() : hashPrev() {}
90 
91  explicit CDiskBlockIndex(const CBlockIndex *pindex) : CBlockIndex(*pindex) {
92  hashPrev = (pprev ? pprev->GetBlockHash() : BlockHash());
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;
133  block.hashMerkleRoot = hashMerkleRoot;
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)",
144  GetBlockHash().ToString(), hashPrev.ToString());
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  friend bool operator==(const CChain &a, const CChain &b) {
185  return a.vChain.size() == b.vChain.size() &&
186  a.vChain[a.vChain.size() - 1] == b.vChain[b.vChain.size() - 1];
187  }
188 
190  bool Contains(const CBlockIndex *pindex) const {
191  return (*this)[pindex->nHeight] == pindex;
192  }
193 
198  CBlockIndex *Next(const CBlockIndex *pindex) const {
199  if (!Contains(pindex)) {
200  return nullptr;
201  }
202 
203  return (*this)[pindex->nHeight + 1];
204  }
205 
210  int Height() const { return vChain.size() - 1; }
211 
213  void SetTip(CBlockIndex *pindex);
214 
219  CBlockLocator GetLocator(const CBlockIndex *pindex = nullptr) const;
220 
224  const CBlockIndex *FindFork(const CBlockIndex *pindex) const;
225 
230  CBlockIndex *FindEarliestAtLeast(int64_t nTime, int height) const;
231 };
232 
233 #endif // BITCOIN_CHAIN_H
uint32_t nNonce
Definition: block.h:30
#define VARINT(obj)
Definition: serialize.h:608
BlockHash GetBlockHash() const
Definition: chain.h:129
std::string ToString() const
Definition: blockindex.h:187
Describes a place in the block chain to another node such that if the other node doesn&#39;t have the sam...
Definition: block.h:110
BlockHash hashPrev
Definition: chain.h:87
const CBlockIndex * LastCommonAncestor(const CBlockIndex *pa, const CBlockIndex *pb)
Find the forking point between two chain tips.
Definition: chain.cpp:118
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1201
An in-memory indexed chain of blocks.
Definition: chain.h:152
unsigned int nHeight
int Height() const
Return the maximal height in the chain.
Definition: chain.h:210
CBlockIndex * Genesis() const
Returns the index entry for the genesis block of this chain, or nullptr if none.
Definition: chain.h:161
SERIALIZE_METHODS(CDiskBlockIndex, obj)
Definition: chain.h:95
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
uint32_t nTime
Definition: block.h:28
std::vector< CBlockIndex * > vChain
Definition: chain.h:154
#define VARINT_MODE(obj, mode)
Definition: serialize.h:607
std::string ToString(const T &t)
Locale-independent version of std::to_string.
Definition: string.h:67
arith_uint256 GetBlockProof(const CBlockIndex &block)
Definition: chain.cpp:80
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
uint256 hashMerkleRoot
Definition: block.h:27
bool AreOnTheSameFork(const CBlockIndex *pa, const CBlockIndex *pb)
Check if two block index are on the same fork.
Definition: chain.cpp:142
bool Contains(const CBlockIndex *pindex) const
Efficiently check whether a block is present in this chain.
Definition: chain.h:190
size_t operator()(const BlockHash &hash) const
Definition: chain.h:53
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:198
CDiskBlockIndex()
Definition: chain.h:89
friend bool operator==(const CChain &a, const CChain &b)
Compare two chains efficiently.
Definition: chain.h:184
Used to marshal pointers into hashes for db storage.
Definition: chain.h:83
uint8_t * begin()
Definition: uint256.h:76
std::string ToString() const
Definition: uint256.h:74
Parameters that influence chain consensus.
Definition: params.h:59
256-bit unsigned big integer.
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
Maintain a map of CBlockIndex for all known headers.
Definition: chain.h:49
A BlockHash is a unqiue identifier for a block.
Definition: blockhash.h:13
CDiskBlockIndex(const CBlockIndex *pindex)
Definition: chain.h:91
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: blockindex.h:23
BlockHash GetHash() const
Definition: block.cpp:11
RecursiveMutex cs_main
Global state.
Definition: validation.cpp:90
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
Definition: chain.h:168
std::string ToString() const
Definition: chain.h:140
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
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: blockindex.h:36
#define READWRITE(...)
Definition: serialize.h:191
static uint64_t ReadLE64(const uint8_t *ptr)
Definition: common.h:29
int32_t nVersion
Definition: block.h:25
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
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