Bitcoin ABC  0.29.9
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 <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 
47 
54  const CBlockIndex &from,
55  const CBlockIndex &tip,
56  const Consensus::Params &);
61  const CBlockIndex *pb);
62 
66 bool AreOnTheSameFork(const CBlockIndex *pa, const CBlockIndex *pb);
67 
69 class CDiskBlockIndex : public CBlockIndex {
70 public:
71  static constexpr int TRACK_SIZE_VERSION = 220800;
72 
74 
76 
77  explicit CDiskBlockIndex(const CBlockIndex *pindex) : CBlockIndex(*pindex) {
79  }
80 
82  LOCK(::cs_main);
83  int _nVersion = s.GetVersion();
84  if (!(s.GetType() & SER_GETHASH)) {
86  }
87 
89  READWRITE(obj.nStatus);
90  READWRITE(VARINT(obj.nTx));
91 
92  // The size of the blocks are tracked starting at version 0.22.8
93  if (obj.nStatus.hasData() && _nVersion >= TRACK_SIZE_VERSION) {
94  READWRITE(VARINT(obj.nSize));
95  }
96 
97  if (obj.nStatus.hasData() || obj.nStatus.hasUndo()) {
99  }
100  if (obj.nStatus.hasData()) {
101  READWRITE(VARINT(obj.nDataPos));
102  }
103  if (obj.nStatus.hasUndo()) {
104  READWRITE(VARINT(obj.nUndoPos));
105  }
106 
107  // block header
108  READWRITE(obj.nVersion);
109  READWRITE(obj.hashPrev);
110  READWRITE(obj.hashMerkleRoot);
111  READWRITE(obj.nTime);
112  READWRITE(obj.nBits);
113  READWRITE(obj.nNonce);
114  }
115 
117  CBlockHeader block;
118  block.nVersion = nVersion;
119  block.hashPrevBlock = hashPrev;
121  block.nTime = nTime;
122  block.nBits = nBits;
123  block.nNonce = nNonce;
124  return block.GetHash();
125  }
126 
128  std::string ToString() = delete;
129 };
130 
134 class CChain {
135 private:
136  std::vector<CBlockIndex *> vChain;
137 
138 public:
143  CBlockIndex *Genesis() const {
144  return vChain.size() > 0 ? vChain[0] : nullptr;
145  }
146 
150  CBlockIndex *Tip() const {
151  return vChain.size() > 0 ? vChain[vChain.size() - 1] : nullptr;
152  }
153 
159  if (nHeight < 0 || nHeight >= (int)vChain.size()) {
160  return nullptr;
161  }
162  return vChain[nHeight];
163  }
164 
166  bool Contains(const CBlockIndex *pindex) const {
167  return (*this)[pindex->nHeight] == pindex;
168  }
169 
174  CBlockIndex *Next(const CBlockIndex *pindex) const {
175  if (!Contains(pindex)) {
176  return nullptr;
177  }
178 
179  return (*this)[pindex->nHeight + 1];
180  }
181 
186  int Height() const { return int(vChain.size()) - 1; }
187 
189  void SetTip(CBlockIndex &block);
190 
192  CBlockLocator GetLocator() const;
193 
197  const CBlockIndex *FindFork(const CBlockIndex *pindex) const;
198 
203  CBlockIndex *FindEarliestAtLeast(int64_t nTime, int height) const;
204 };
205 
207 CBlockLocator GetLocator(const CBlockIndex *index);
208 
210 std::vector<BlockHash> LocatorEntries(const CBlockIndex *index);
211 
212 #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:17
arith_uint256 GetBlockProof(const CBlockIndex &block)
Definition: chain.cpp:74
CBlockLocator GetLocator(const CBlockIndex *index)
Get a locator for a block index entry.
Definition: chain.cpp:41
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:89
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:112
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
bool AreOnTheSameFork(const CBlockIndex *pa, const CBlockIndex *pb)
Check if two block index are on the same fork.
Definition: chain.cpp:136
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:25
uint256 hashMerkleRoot
Definition: blockindex.h:91
CBlockIndex * pprev
pointer to the index of the predecessor of this block
Definition: blockindex.h:32
uint32_t nTime
Definition: blockindex.h:92
uint32_t nNonce
Definition: blockindex.h:94
uint32_t nBits
Definition: blockindex.h:93
int32_t nVersion
block header
Definition: blockindex.h:90
BlockHash GetBlockHash() const
Definition: blockindex.h:146
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: blockindex.h:38
An in-memory indexed chain of blocks.
Definition: chain.h:134
CBlockIndex * Genesis() const
Returns the index entry for the genesis block of this chain, or nullptr if none.
Definition: chain.h:143
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:158
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:174
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
Definition: chain.h:150
void SetTip(CBlockIndex &block)
Set/initialize a chain with a given tip.
Definition: chain.cpp:8
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:62
int Height() const
Return the maximal height in the chain.
Definition: chain.h:186
const CBlockIndex * FindFork(const CBlockIndex *pindex) const
Find the last common block between this chain and a block index entry.
Definition: chain.cpp:49
std::vector< CBlockIndex * > vChain
Definition: chain.h:136
bool Contains(const CBlockIndex *pindex) const
Efficiently check whether a block is present in this chain.
Definition: chain.h:166
CBlockLocator GetLocator() const
Return a CBlockLocator that refers to the tip of this chain.
Definition: chain.cpp:45
Used to marshal pointers into hashes for db storage.
Definition: chain.h:69
BlockHash GetBlockHash()=delete
std::string ToString()=delete
BlockHash hashPrev
Definition: chain.h:73
static constexpr int TRACK_SIZE_VERSION
Definition: chain.h:71
BlockHash ConstructBlockHash() const
Definition: chain.h:116
SERIALIZE_METHODS(CDiskBlockIndex, obj)
Definition: chain.h:81
CDiskBlockIndex()
Definition: chain.h:75
CDiskBlockIndex(const CBlockIndex *pindex)
Definition: chain.h:77
256-bit unsigned big integer.
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:579
#define VARINT_MODE(obj, mode)
Definition: serialize.h:578
@ NONNEGATIVE_SIGNED
@ SER_GETHASH
Definition: serialize.h:154
#define READWRITE(...)
Definition: serialize.h:166
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