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.
6 #ifndef BITCOIN_CHAIN_H
7 #define BITCOIN_CHAIN_H
9 #include "arith_uint256.h"
10 #include "primitives/block.h"
12 #include "tinyformat.h"
18 * Maximum amount of time that a block timestamp is allowed to exceed the
19 * current network-adjusted time before the block will be accepted.
21 static const int64_t MAX_FUTURE_BLOCK_TIME
= 2 * 60 * 60;
24 * Timestamp window used as a grace period by code that compares external
25 * timestamps (such as timestamps passed to RPCs, or wallet key creation times)
26 * to block timestamps. This should be set at least as high as
27 * MAX_FUTURE_BLOCK_TIME.
29 static const int64_t TIMESTAMP_WINDOW
= MAX_FUTURE_BLOCK_TIME
;
34 unsigned int nBlocks
; //!< number of blocks stored in file
35 unsigned int nSize
; //!< number of used bytes of block file
36 unsigned int nUndoSize
; //!< number of used bytes in the undo file
37 unsigned int nHeightFirst
; //!< lowest height of block in file
38 unsigned int nHeightLast
; //!< highest height of block in file
39 uint64_t nTimeFirst
; //!< earliest time of block in file
40 uint64_t nTimeLast
; //!< latest time of block in file
42 ADD_SERIALIZE_METHODS
;
44 template <typename Stream
, typename Operation
>
45 inline void SerializationOp(Stream
& s
, Operation ser_action
) {
46 READWRITE(VARINT(nBlocks
));
47 READWRITE(VARINT(nSize
));
48 READWRITE(VARINT(nUndoSize
));
49 READWRITE(VARINT(nHeightFirst
));
50 READWRITE(VARINT(nHeightLast
));
51 READWRITE(VARINT(nTimeFirst
));
52 READWRITE(VARINT(nTimeLast
));
69 std::string
ToString() const;
71 /** update statistics (does not update nSize) */
72 void AddBlock(unsigned int nHeightIn
, uint64_t nTimeIn
) {
73 if (nBlocks
==0 || nHeightFirst
> nHeightIn
)
74 nHeightFirst
= nHeightIn
;
75 if (nBlocks
==0 || nTimeFirst
> nTimeIn
)
78 if (nHeightIn
> nHeightLast
)
79 nHeightLast
= nHeightIn
;
80 if (nTimeIn
> nTimeLast
)
90 ADD_SERIALIZE_METHODS
;
92 template <typename Stream
, typename Operation
>
93 inline void SerializationOp(Stream
& s
, Operation ser_action
) {
94 READWRITE(VARINT(nFile
));
95 READWRITE(VARINT(nPos
));
102 CDiskBlockPos(int nFileIn
, unsigned int nPosIn
) {
107 friend bool operator==(const CDiskBlockPos
&a
, const CDiskBlockPos
&b
) {
108 return (a
.nFile
== b
.nFile
&& a
.nPos
== b
.nPos
);
111 friend bool operator!=(const CDiskBlockPos
&a
, const CDiskBlockPos
&b
) {
115 void SetNull() { nFile
= -1; nPos
= 0; }
116 bool IsNull() const { return (nFile
== -1); }
118 std::string
ToString() const
120 return strprintf("CBlockDiskPos(nFile=%i, nPos=%i)", nFile
, nPos
);
125 enum BlockStatus
: uint32_t {
127 BLOCK_VALID_UNKNOWN
= 0,
129 //! Parsed, version ok, hash satisfies claimed PoW, 1 <= vtx count <= max, timestamp not in future
130 BLOCK_VALID_HEADER
= 1,
132 //! All parent headers found, difficulty matches, timestamp >= median previous, checkpoint. Implies all parents
133 //! are also at least TREE.
134 BLOCK_VALID_TREE
= 2,
137 * Only first tx is coinbase, 2 <= coinbase input script length <= 100, transactions valid, no duplicate txids,
138 * sigops, size, merkle root. Implies all parents are at least TREE but not necessarily TRANSACTIONS. When all
139 * parent blocks also have TRANSACTIONS, CBlockIndex::nChainTx will be set.
141 BLOCK_VALID_TRANSACTIONS
= 3,
143 //! Outputs do not overspend inputs, no double spends, coinbase output ok, no immature coinbase spends, BIP30.
144 //! Implies all parents are also at least CHAIN.
145 BLOCK_VALID_CHAIN
= 4,
147 //! Scripts & signatures ok. Implies all parents are also at least SCRIPTS.
148 BLOCK_VALID_SCRIPTS
= 5,
150 //! All validity bits.
151 BLOCK_VALID_MASK
= BLOCK_VALID_HEADER
| BLOCK_VALID_TREE
| BLOCK_VALID_TRANSACTIONS
|
152 BLOCK_VALID_CHAIN
| BLOCK_VALID_SCRIPTS
,
154 BLOCK_HAVE_DATA
= 8, //!< full block available in blk*.dat
155 BLOCK_HAVE_UNDO
= 16, //!< undo data available in rev*.dat
156 BLOCK_HAVE_MASK
= BLOCK_HAVE_DATA
| BLOCK_HAVE_UNDO
,
158 BLOCK_FAILED_VALID
= 32, //!< stage after last reached validness failed
159 BLOCK_FAILED_CHILD
= 64, //!< descends from failed block
160 BLOCK_FAILED_MASK
= BLOCK_FAILED_VALID
| BLOCK_FAILED_CHILD
,
162 BLOCK_OPT_WITNESS
= 128, //!< block data in blk*.data was received with a witness-enforcing client
165 /** The block chain is a tree shaped structure starting with the
166 * genesis block at the root, with each block potentially having multiple
167 * candidates to be the next block. A blockindex may have multiple pprev pointing
168 * to it, but at most one of them can be part of the currently active branch.
173 //! pointer to the hash of the block, if any. Memory is owned by this CBlockIndex
174 const uint256
* phashBlock
;
176 //! pointer to the index of the predecessor of this block
179 //! pointer to the index of some further predecessor of this block
182 //! height of the entry in the chain. The genesis block has height 0
185 //! Which # file this block is stored in (blk?????.dat)
188 //! Byte offset within blk?????.dat where this block's data is stored
189 unsigned int nDataPos
;
191 //! Byte offset within rev?????.dat where this block's undo data is stored
192 unsigned int nUndoPos
;
194 //! (memory only) Total amount of work (expected number of hashes) in the chain up to and including this block
195 arith_uint256 nChainWork
;
197 //! Number of transactions in this block.
198 //! Note: in a potential headers-first mode, this number cannot be relied upon
201 //! (memory only) Number of transactions in the chain up to and including this block.
202 //! This value will be non-zero only if and only if transactions for this block and all its parents are available.
203 //! Change to 64-bit type when necessary; won't happen before 2030
204 unsigned int nChainTx
;
206 //! Verification status of this block. See enum BlockStatus
207 unsigned int nStatus
;
211 uint256 hashMerkleRoot
;
216 //! (memory only) Sequential id assigned to distinguish order in which blocks are received.
219 //! (memory only) Maximum nTime in the chain up to and including this block.
220 unsigned int nTimeMax
;
224 phashBlock
= nullptr;
231 nChainWork
= arith_uint256();
239 hashMerkleRoot
= uint256();
250 explicit CBlockIndex(const CBlockHeader
& block
)
254 nVersion
= block
.nVersion
;
255 hashMerkleRoot
= block
.hashMerkleRoot
;
258 nNonce
= block
.nNonce
;
261 CDiskBlockPos
GetBlockPos() const {
263 if (nStatus
& BLOCK_HAVE_DATA
) {
270 CDiskBlockPos
GetUndoPos() const {
272 if (nStatus
& BLOCK_HAVE_UNDO
) {
279 CBlockHeader
GetBlockHeader() const
282 block
.nVersion
= nVersion
;
284 block
.hashPrevBlock
= pprev
->GetBlockHash();
285 block
.hashMerkleRoot
= hashMerkleRoot
;
288 block
.nNonce
= nNonce
;
292 uint256
GetBlockHash() const
297 int64_t GetBlockTime() const
299 return (int64_t)nTime
;
302 int64_t GetBlockTimeMax() const
304 return (int64_t)nTimeMax
;
307 enum { nMedianTimeSpan
=11 };
309 int64_t GetMedianTimePast() const
311 int64_t pmedian
[nMedianTimeSpan
];
312 int64_t* pbegin
= &pmedian
[nMedianTimeSpan
];
313 int64_t* pend
= &pmedian
[nMedianTimeSpan
];
315 const CBlockIndex
* pindex
= this;
316 for (int i
= 0; i
< nMedianTimeSpan
&& pindex
; i
++, pindex
= pindex
->pprev
)
317 *(--pbegin
) = pindex
->GetBlockTime();
319 std::sort(pbegin
, pend
);
320 return pbegin
[(pend
- pbegin
)/2];
323 std::string
ToString() const
325 return strprintf("CBlockIndex(pprev=%p, nHeight=%d, merkle=%s, hashBlock=%s)",
327 hashMerkleRoot
.ToString(),
328 GetBlockHash().ToString());
331 //! Check whether this block index entry is valid up to the passed validity level.
332 bool IsValid(enum BlockStatus nUpTo
= BLOCK_VALID_TRANSACTIONS
) const
334 assert(!(nUpTo
& ~BLOCK_VALID_MASK
)); // Only validity flags allowed.
335 if (nStatus
& BLOCK_FAILED_MASK
)
337 return ((nStatus
& BLOCK_VALID_MASK
) >= nUpTo
);
340 //! Raise the validity level of this block index entry.
341 //! Returns true if the validity was changed.
342 bool RaiseValidity(enum BlockStatus nUpTo
)
344 assert(!(nUpTo
& ~BLOCK_VALID_MASK
)); // Only validity flags allowed.
345 if (nStatus
& BLOCK_FAILED_MASK
)
347 if ((nStatus
& BLOCK_VALID_MASK
) < nUpTo
) {
348 nStatus
= (nStatus
& ~BLOCK_VALID_MASK
) | nUpTo
;
354 //! Build the skiplist pointer for this entry.
357 //! Efficiently find an ancestor of this block.
358 CBlockIndex
* GetAncestor(int height
);
359 const CBlockIndex
* GetAncestor(int height
) const;
362 arith_uint256
GetBlockProof(const CBlockIndex
& block
);
363 /** Return the time it would take to redo the work difference between from and to, assuming the current hashrate corresponds to the difficulty at tip, in seconds. */
364 int64_t GetBlockProofEquivalentTime(const CBlockIndex
& to
, const CBlockIndex
& from
, const CBlockIndex
& tip
, const Consensus::Params
&);
365 /** Find the forking point between two chain tips. */
366 const CBlockIndex
* LastCommonAncestor(const CBlockIndex
* pa
, const CBlockIndex
* pb
);
369 /** Used to marshal pointers into hashes for db storage. */
370 class CDiskBlockIndex
: public CBlockIndex
376 hashPrev
= uint256();
379 explicit CDiskBlockIndex(const CBlockIndex
* pindex
) : CBlockIndex(*pindex
) {
380 hashPrev
= (pprev
? pprev
->GetBlockHash() : uint256());
383 ADD_SERIALIZE_METHODS
;
385 template <typename Stream
, typename Operation
>
386 inline void SerializationOp(Stream
& s
, Operation ser_action
) {
387 int _nVersion
= s
.GetVersion();
388 if (!(s
.GetType() & SER_GETHASH
))
389 READWRITE(VARINT(_nVersion
));
391 READWRITE(VARINT(nHeight
));
392 READWRITE(VARINT(nStatus
));
393 READWRITE(VARINT(nTx
));
394 if (nStatus
& (BLOCK_HAVE_DATA
| BLOCK_HAVE_UNDO
))
395 READWRITE(VARINT(nFile
));
396 if (nStatus
& BLOCK_HAVE_DATA
)
397 READWRITE(VARINT(nDataPos
));
398 if (nStatus
& BLOCK_HAVE_UNDO
)
399 READWRITE(VARINT(nUndoPos
));
402 READWRITE(this->nVersion
);
404 READWRITE(hashMerkleRoot
);
410 uint256
GetBlockHash() const
413 block
.nVersion
= nVersion
;
414 block
.hashPrevBlock
= hashPrev
;
415 block
.hashMerkleRoot
= hashMerkleRoot
;
418 block
.nNonce
= nNonce
;
419 return block
.GetHash();
423 std::string
ToString() const
425 std::string str
= "CDiskBlockIndex(";
426 str
+= CBlockIndex::ToString();
427 str
+= strprintf("\n hashBlock=%s, hashPrev=%s)",
428 GetBlockHash().ToString(),
429 hashPrev
.ToString());
434 /** An in-memory indexed chain of blocks. */
437 std::vector
<CBlockIndex
*> vChain
;
440 /** Returns the index entry for the genesis block of this chain, or nullptr if none. */
441 CBlockIndex
*Genesis() const {
442 return vChain
.size() > 0 ? vChain
[0] : nullptr;
445 /** Returns the index entry for the tip of this chain, or nullptr if none. */
446 CBlockIndex
*Tip() const {
447 return vChain
.size() > 0 ? vChain
[vChain
.size() - 1] : nullptr;
450 /** Returns the index entry at a particular height in this chain, or nullptr if no such height exists. */
451 CBlockIndex
*operator[](int nHeight
) const {
452 if (nHeight
< 0 || nHeight
>= (int)vChain
.size())
454 return vChain
[nHeight
];
457 /** Compare two chains efficiently. */
458 friend bool operator==(const CChain
&a
, const CChain
&b
) {
459 return a
.vChain
.size() == b
.vChain
.size() &&
460 a
.vChain
[a
.vChain
.size() - 1] == b
.vChain
[b
.vChain
.size() - 1];
463 /** Efficiently check whether a block is present in this chain. */
464 bool Contains(const CBlockIndex
*pindex
) const {
465 return (*this)[pindex
->nHeight
] == pindex
;
468 /** Find the successor of a block in this chain, or nullptr if the given index is not found or is the tip. */
469 CBlockIndex
*Next(const CBlockIndex
*pindex
) const {
470 if (Contains(pindex
))
471 return (*this)[pindex
->nHeight
+ 1];
476 /** Return the maximal height in the chain. Is equal to chain.Tip() ? chain.Tip()->nHeight : -1. */
478 return vChain
.size() - 1;
481 /** Set/initialize a chain with a given tip. */
482 void SetTip(CBlockIndex
*pindex
);
484 /** Return a CBlockLocator that refers to a block in this chain (by default the tip). */
485 CBlockLocator
GetLocator(const CBlockIndex
*pindex
= nullptr) const;
487 /** Find the last common block between this chain and a block index entry. */
488 const CBlockIndex
*FindFork(const CBlockIndex
*pindex
) const;
490 /** Find the earliest block with timestamp equal or greater than the given. */
491 CBlockIndex
* FindEarliestAtLeast(int64_t nTime
) const;
494 #endif // BITCOIN_CHAIN_H