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 <kernel/cs_main.h>
17 #include <primitives/block.h>
18 #include <sync.h>
19 #include <tinyformat.h>
20 #include <uint256.h>
21 
22 #include <unordered_map>
23 #include <vector>
24 
29 static constexpr int64_t MAX_FUTURE_BLOCK_TIME = 2 * 60 * 60;
30 
37 static constexpr int64_t TIMESTAMP_WINDOW = MAX_FUTURE_BLOCK_TIME;
38 
45 static constexpr int64_t MAX_BLOCK_TIME_GAP = 90 * 60;
46 
48 
55  const CBlockIndex &from,
56  const CBlockIndex &tip,
57  const Consensus::Params &);
62  const CBlockIndex *pb);
63 
67 bool AreOnTheSameFork(const CBlockIndex *pa, const CBlockIndex *pb);
68 
70 class CDiskBlockIndex : public CBlockIndex {
71 public:
72  static constexpr int TRACK_SIZE_VERSION = 220800;
73 
75 
77 
78  explicit CDiskBlockIndex(const CBlockIndex *pindex) : CBlockIndex(*pindex) {
80  }
81 
83  LOCK(::cs_main);
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 int(vChain.size()) - 1; }
193 
195  void SetTip(CBlockIndex *pindex);
196 
198  CBlockLocator GetLocator() const;
199 
203  const CBlockIndex *FindFork(const CBlockIndex *pindex) const;
204 
209  CBlockIndex *FindEarliestAtLeast(int64_t nTime, int height) const;
210 };
211 
213 CBlockLocator GetLocator(const CBlockIndex *index);
214 
216 std::vector<BlockHash> LocatorEntries(const CBlockIndex *index);
217 
218 #endif // BITCOIN_CHAIN_H
std::vector< BlockHash > LocatorEntries(const CBlockIndex *index)
Construct a list of hash entries to put in a locator.
Definition: chain.cpp:21
arith_uint256 GetBlockProof(const CBlockIndex &block)
Definition: chain.cpp:78
CBlockLocator GetLocator(const CBlockIndex *index)
Get a locator for a block index entry.
Definition: chain.cpp:45
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:93
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:45
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:29
const CBlockIndex * LastCommonAncestor(const CBlockIndex *pa, const CBlockIndex *pb)
Find the forking point between two chain tips.
Definition: chain.cpp:116
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:37
bool AreOnTheSameFork(const CBlockIndex *pa, const CBlockIndex *pb)
Check if two block index are on the same fork.
Definition: chain.cpp:140
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition: block.h:23
BlockHash GetHash() const
Definition: block.cpp:11
uint32_t nNonce
Definition: block.h:31
uint32_t nBits
Definition: block.h:30
uint32_t nTime
Definition: block.h:29
BlockHash hashPrevBlock
Definition: block.h:27
int32_t nVersion
Definition: block.h:26
uint256 hashMerkleRoot
Definition: block.h:28
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:205
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: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
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:66
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:53
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
CBlockLocator GetLocator() const
Return a CBlockLocator that refers to the tip of this chain.
Definition: chain.cpp:49
Used to marshal pointers into hashes for db storage.
Definition: chain.h:70
BlockHash GetBlockHash() const
Definition: chain.h:117
std::string ToString() const
Definition: chain.h:128
BlockHash hashPrev
Definition: chain.h:74
static constexpr int TRACK_SIZE_VERSION
Definition: chain.h:72
SERIALIZE_METHODS(CDiskBlockIndex, obj)
Definition: chain.h:82
CDiskBlockIndex()
Definition: chain.h:76
CDiskBlockIndex(const CBlockIndex *pindex)
Definition: chain.h:78
256-bit unsigned big integer.
std::string ToString() const
Definition: uint256.h:78
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: cs_main.cpp:7
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:105
Parameters that influence chain consensus.
Definition: params.h:34
#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