Bitcoin ABC  0.26.3
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 
46 extern RecursiveMutex cs_main;
47 
49 
56  const CBlockIndex &from,
57  const CBlockIndex &tip,
58  const Consensus::Params &);
63  const CBlockIndex *pb);
64 
68 bool AreOnTheSameFork(const CBlockIndex *pa, const CBlockIndex *pb);
69 
71 class CDiskBlockIndex : public CBlockIndex {
72 public:
73  static constexpr int TRACK_SIZE_VERSION = 220800;
74 
76 
78 
79  explicit CDiskBlockIndex(const CBlockIndex *pindex) : CBlockIndex(*pindex) {
81  }
82 
84  int _nVersion = s.GetVersion();
85  if (!(s.GetType() & SER_GETHASH)) {
87  }
88 
90  READWRITE(obj.nStatus);
91  READWRITE(VARINT(obj.nTx));
92 
93  // The size of the blocks are tracked starting at version 0.22.8
94  if (obj.nStatus.hasData() && _nVersion >= TRACK_SIZE_VERSION) {
95  READWRITE(VARINT(obj.nSize));
96  }
97 
98  if (obj.nStatus.hasData() || obj.nStatus.hasUndo()) {
100  }
101  if (obj.nStatus.hasData()) {
102  READWRITE(VARINT(obj.nDataPos));
103  }
104  if (obj.nStatus.hasUndo()) {
105  READWRITE(VARINT(obj.nUndoPos));
106  }
107 
108  // block header
109  READWRITE(obj.nVersion);
110  READWRITE(obj.hashPrev);
111  READWRITE(obj.hashMerkleRoot);
112  READWRITE(obj.nTime);
113  READWRITE(obj.nBits);
114  READWRITE(obj.nNonce);
115  }
116 
118  CBlockHeader block;
119  block.nVersion = nVersion;
120  block.hashPrevBlock = hashPrev;
122  block.nTime = nTime;
123  block.nBits = nBits;
124  block.nNonce = nNonce;
125  return block.GetHash();
126  }
127 
128  std::string ToString() const {
129  std::string str = "CDiskBlockIndex(";
130  str += CBlockIndex::ToString();
131  str += strprintf("\n hashBlock=%s, hashPrev=%s)",
133  return str;
134  }
135 };
136 
140 class CChain {
141 private:
142  std::vector<CBlockIndex *> vChain;
143 
144 public:
149  CBlockIndex *Genesis() const {
150  return vChain.size() > 0 ? vChain[0] : nullptr;
151  }
152 
156  CBlockIndex *Tip() const {
157  return vChain.size() > 0 ? vChain[vChain.size() - 1] : nullptr;
158  }
159 
165  if (nHeight < 0 || nHeight >= (int)vChain.size()) {
166  return nullptr;
167  }
168  return vChain[nHeight];
169  }
170 
172  bool Contains(const CBlockIndex *pindex) const {
173  return (*this)[pindex->nHeight] == pindex;
174  }
175 
180  CBlockIndex *Next(const CBlockIndex *pindex) const {
181  if (!Contains(pindex)) {
182  return nullptr;
183  }
184 
185  return (*this)[pindex->nHeight + 1];
186  }
187 
192  int Height() const { return vChain.size() - 1; }
193 
195  void SetTip(CBlockIndex *pindex);
196 
201  CBlockLocator GetLocator(const CBlockIndex *pindex = nullptr) const;
202 
206  const CBlockIndex *FindFork(const CBlockIndex *pindex) const;
207 
212  CBlockIndex *FindEarliestAtLeast(int64_t nTime, int height) const;
213 };
214 
215 #endif // BITCOIN_CHAIN_H
arith_uint256 GetBlockProof(const CBlockIndex &block)
Definition: chain.cpp:77
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:92
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
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
const CBlockIndex * LastCommonAncestor(const CBlockIndex *pa, const CBlockIndex *pb)
Find the forking point between two chain tips.
Definition: chain.cpp:115
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
RecursiveMutex cs_main
Global state.
Definition: validation.cpp:94
bool AreOnTheSameFork(const CBlockIndex *pa, const CBlockIndex *pb)
Check if two block index are on the same fork.
Definition: chain.cpp:139
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition: block.h:22
BlockHash GetHash() const
Definition: block.cpp:11
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:23
uint256 hashMerkleRoot
Definition: blockindex.h:89
std::string ToString() const
Definition: blockindex.h:196
CBlockIndex * pprev
pointer to the index of the predecessor of this block
Definition: blockindex.h:30
uint32_t nTime
Definition: blockindex.h:90
uint32_t nNonce
Definition: blockindex.h:92
uint32_t nBits
Definition: blockindex.h:91
int32_t nVersion
block header
Definition: blockindex.h:88
BlockHash GetBlockHash() const
Definition: blockindex.h:142
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: blockindex.h:36
An in-memory indexed chain of blocks.
Definition: chain.h:140
CBlockIndex * Genesis() const
Returns the index entry for the genesis block of this chain, or nullptr if none.
Definition: chain.h:149
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:164
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:180
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
Definition: chain.h:156
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:21
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:65
int Height() const
Return the maximal height in the chain.
Definition: chain.h:192
const CBlockIndex * FindFork(const CBlockIndex *pindex) const
Find the last common block between this chain and a block index entry.
Definition: chain.cpp:52
void SetTip(CBlockIndex *pindex)
Set/initialize a chain with a given tip.
Definition: chain.cpp:8
std::vector< CBlockIndex * > vChain
Definition: chain.h:142
bool Contains(const CBlockIndex *pindex) const
Efficiently check whether a block is present in this chain.
Definition: chain.h:172
Used to marshal pointers into hashes for db storage.
Definition: chain.h:71
BlockHash GetBlockHash() const
Definition: chain.h:117
std::string ToString() const
Definition: chain.h:128
BlockHash hashPrev
Definition: chain.h:75
static constexpr int TRACK_SIZE_VERSION
Definition: chain.h:73
SERIALIZE_METHODS(CDiskBlockIndex, obj)
Definition: chain.h:83
CDiskBlockIndex()
Definition: chain.h:77
CDiskBlockIndex(const CBlockIndex *pindex)
Definition: chain.h:79
256-bit unsigned big integer.
std::string ToString() const
Definition: uint256.h:78
unsigned int nHeight
#define VARINT(obj)
Definition: serialize.h:597
#define VARINT_MODE(obj, mode)
Definition: serialize.h:596
@ NONNEGATIVE_SIGNED
@ SER_GETHASH
Definition: serialize.h:168
#define READWRITE(...)
Definition: serialize.h:180
A BlockHash is a unqiue identifier for a block.
Definition: blockhash.h:13
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
Parameters that influence chain consensus.
Definition: params.h:76
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1201