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  LOCK(::cs_main);
85  int _nVersion = s.GetVersion();
86  if (!(s.GetType() & SER_GETHASH)) {
88  }
89 
91  READWRITE(obj.nStatus);
92  READWRITE(VARINT(obj.nTx));
93 
94  // The size of the blocks are tracked starting at version 0.22.8
95  if (obj.nStatus.hasData() && _nVersion >= TRACK_SIZE_VERSION) {
96  READWRITE(VARINT(obj.nSize));
97  }
98 
99  if (obj.nStatus.hasData() || obj.nStatus.hasUndo()) {
101  }
102  if (obj.nStatus.hasData()) {
103  READWRITE(VARINT(obj.nDataPos));
104  }
105  if (obj.nStatus.hasUndo()) {
106  READWRITE(VARINT(obj.nUndoPos));
107  }
108 
109  // block header
110  READWRITE(obj.nVersion);
111  READWRITE(obj.hashPrev);
112  READWRITE(obj.hashMerkleRoot);
113  READWRITE(obj.nTime);
114  READWRITE(obj.nBits);
115  READWRITE(obj.nNonce);
116  }
117 
119  CBlockHeader block;
120  block.nVersion = nVersion;
121  block.hashPrevBlock = hashPrev;
123  block.nTime = nTime;
124  block.nBits = nBits;
125  block.nNonce = nNonce;
126  return block.GetHash();
127  }
128 
129  std::string ToString() const {
130  std::string str = "CDiskBlockIndex(";
131  str += CBlockIndex::ToString();
132  str += strprintf("\n hashBlock=%s, hashPrev=%s)",
134  return str;
135  }
136 };
137 
141 class CChain {
142 private:
143  std::vector<CBlockIndex *> vChain;
144 
145 public:
150  CBlockIndex *Genesis() const {
151  return vChain.size() > 0 ? vChain[0] : nullptr;
152  }
153 
157  CBlockIndex *Tip() const {
158  return vChain.size() > 0 ? vChain[vChain.size() - 1] : nullptr;
159  }
160 
166  if (nHeight < 0 || nHeight >= (int)vChain.size()) {
167  return nullptr;
168  }
169  return vChain[nHeight];
170  }
171 
173  bool Contains(const CBlockIndex *pindex) const {
174  return (*this)[pindex->nHeight] == pindex;
175  }
176 
181  CBlockIndex *Next(const CBlockIndex *pindex) const {
182  if (!Contains(pindex)) {
183  return nullptr;
184  }
185 
186  return (*this)[pindex->nHeight + 1];
187  }
188 
193  int Height() const { return int(vChain.size()) - 1; }
194 
196  void SetTip(CBlockIndex *pindex);
197 
202  CBlockLocator GetLocator(const CBlockIndex *pindex = nullptr) const;
203 
207  const CBlockIndex *FindFork(const CBlockIndex *pindex) const;
208 
213  CBlockIndex *FindEarliestAtLeast(int64_t nTime, int height) const;
214 };
215 
216 #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:113
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:26
uint256 hashMerkleRoot
Definition: blockindex.h:92
std::string ToString() const
Definition: blockindex.h:201
CBlockIndex * pprev
pointer to the index of the predecessor of this block
Definition: blockindex.h:33
uint32_t nTime
Definition: blockindex.h:93
uint32_t nNonce
Definition: blockindex.h:95
uint32_t nBits
Definition: blockindex.h:94
int32_t nVersion
block header
Definition: blockindex.h:91
BlockHash GetBlockHash() const
Definition: blockindex.h:147
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: blockindex.h:39
An in-memory indexed chain of blocks.
Definition: chain.h:141
CBlockIndex * Genesis() const
Returns the index entry for the genesis block of this chain, or nullptr if none.
Definition: chain.h:150
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:165
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:181
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
Definition: chain.h:157
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:193
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:143
bool Contains(const CBlockIndex *pindex) const
Efficiently check whether a block is present in this chain.
Definition: chain.h:173
Used to marshal pointers into hashes for db storage.
Definition: chain.h:71
BlockHash GetBlockHash() const
Definition: chain.h:118
std::string ToString() const
Definition: chain.h:129
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:33
#define LOCK(cs)
Definition: sync.h:306
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1202