Bitcoin ABC 0.32.5
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
28static constexpr int64_t MAX_FUTURE_BLOCK_TIME = 2 * 60 * 60;
29
36static constexpr int64_t TIMESTAMP_WINDOW = MAX_FUTURE_BLOCK_TIME;
37
44static 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
66bool AreOnTheSameFork(const CBlockIndex *pa, const CBlockIndex *pb);
67
77 static constexpr int DUMMY_VERSION = 320400;
78
79public:
81
83
84 explicit CDiskBlockIndex(const CBlockIndex *pindex) : CBlockIndex(*pindex) {
86 }
87
90 int _nVersion = DUMMY_VERSION;
92
94 READWRITE(obj.nStatus);
95 READWRITE(VARINT(obj.nTx));
96
97 if (obj.nStatus.hasData()) {
98 READWRITE(VARINT(obj.nSize));
99 }
100
101 if (obj.nStatus.hasData() || obj.nStatus.hasUndo()) {
103 }
104 if (obj.nStatus.hasData()) {
105 READWRITE(VARINT(obj.nDataPos));
106 }
107 if (obj.nStatus.hasUndo()) {
108 READWRITE(VARINT(obj.nUndoPos));
109 }
110
111 // block header
112 READWRITE(obj.nVersion);
113 READWRITE(obj.hashPrev);
114 READWRITE(obj.hashMerkleRoot);
115 READWRITE(obj.nTime);
116 READWRITE(obj.nBits);
117 READWRITE(obj.nNonce);
118 }
119
121 CBlockHeader block;
122 block.nVersion = nVersion;
123 block.hashPrevBlock = hashPrev;
125 block.nTime = nTime;
126 block.nBits = nBits;
127 block.nNonce = nNonce;
128 return block.GetHash();
129 }
130
132 std::string ToString() = delete;
133};
134
138class CChain {
139private:
140 std::vector<CBlockIndex *> vChain;
141
142public:
148 return vChain.size() > 0 ? vChain[0] : nullptr;
149 }
150
154 CBlockIndex *Tip() const {
155 return vChain.size() > 0 ? vChain[vChain.size() - 1] : nullptr;
156 }
157
163 if (nHeight < 0 || nHeight >= (int)vChain.size()) {
164 return nullptr;
165 }
166 return vChain[nHeight];
167 }
168
170 bool Contains(const CBlockIndex *pindex) const {
171 return (*this)[pindex->nHeight] == pindex;
172 }
173
178 CBlockIndex *Next(const CBlockIndex *pindex) const {
179 if (!Contains(pindex)) {
180 return nullptr;
181 }
182
183 return (*this)[pindex->nHeight + 1];
184 }
185
190 int Height() const { return int(vChain.size()) - 1; }
191
193 void SetTip(CBlockIndex &block);
194
197
201 const CBlockIndex *FindFork(const CBlockIndex *pindex) const;
202
207 CBlockIndex *FindEarliestAtLeast(int64_t nTime, int height) const;
208};
209
212
214std::vector<BlockHash> LocatorEntries(const CBlockIndex *index);
215
216#endif // BITCOIN_CHAIN_H
arith_uint256 GetBlockProof(const CBlockIndex &block)
Definition: chain.cpp:74
std::vector< BlockHash > LocatorEntries(const CBlockIndex *index)
Construct a list of hash entries to put in a locator.
Definition: chain.cpp:17
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
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
const CBlockIndex * LastCommonAncestor(const CBlockIndex *pa, const CBlockIndex *pb)
Find the forking point between two chain tips.
Definition: chain.cpp:112
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:75
CBlockIndex * pprev
pointer to the index of the predecessor of this block
Definition: blockindex.h:32
uint32_t nTime
Definition: blockindex.h:76
uint32_t nNonce
Definition: blockindex.h:78
uint32_t nBits
Definition: blockindex.h:77
int32_t nVersion
block header
Definition: blockindex.h:74
BlockHash GetBlockHash() const
Definition: blockindex.h:130
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:138
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
Definition: chain.h:154
void SetTip(CBlockIndex &block)
Set/initialize a chain with a given tip.
Definition: chain.cpp:8
CBlockIndex * Genesis() const
Returns the index entry for the genesis block of this chain, or nullptr if none.
Definition: chain.h:147
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:162
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:178
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:190
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:140
bool Contains(const CBlockIndex *pindex) const
Efficiently check whether a block is present in this chain.
Definition: chain.h:170
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
static constexpr int DUMMY_VERSION
Historically CBlockLocator's version field has been written to disk streams as the client version,...
Definition: chain.h:77
BlockHash hashPrev
Definition: chain.h:80
BlockHash ConstructBlockHash() const
Definition: chain.h:120
SERIALIZE_METHODS(CDiskBlockIndex, obj)
Definition: chain.h:88
CDiskBlockIndex()
Definition: chain.h:82
CDiskBlockIndex(const CBlockIndex *pindex)
Definition: chain.h:84
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:643
#define VARINT_MODE(obj, mode)
Definition: serialize.h:642
@ NONNEGATIVE_SIGNED
#define READWRITE(...)
Definition: serialize.h:189
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:108
Parameters that influence chain consensus.
Definition: params.h:34
#define LOCK(cs)
Definition: sync.h:306