1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 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"
17 #include <boost/foreach.hpp>
24 ADD_SERIALIZE_METHODS;
26 template <typename Stream, typename Operation>
27 inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
28 READWRITE(VARINT(nFile));
29 READWRITE(VARINT(nPos));
36 CDiskBlockPos(int nFileIn, unsigned int nPosIn) {
41 friend bool operator==(const CDiskBlockPos &a, const CDiskBlockPos &b) {
42 return (a.nFile == b.nFile && a.nPos == b.nPos);
45 friend bool operator!=(const CDiskBlockPos &a, const CDiskBlockPos &b) {
49 void SetNull() { nFile = -1; nPos = 0; }
50 bool IsNull() const { return (nFile == -1); }
55 BLOCK_VALID_UNKNOWN = 0,
57 //! Parsed, version ok, hash satisfies claimed PoW, 1 <= vtx count <= max, timestamp not in future
58 BLOCK_VALID_HEADER = 1,
60 //! All parent headers found, difficulty matches, timestamp >= median previous, checkpoint. Implies all parents
61 //! are also at least TREE.
65 * Only first tx is coinbase, 2 <= coinbase input script length <= 100, transactions valid, no duplicate txids,
66 * sigops, size, merkle root. Implies all parents are at least TREE but not necessarily TRANSACTIONS. When all
67 * parent blocks also have TRANSACTIONS, CBlockIndex::nChainTx will be set.
69 BLOCK_VALID_TRANSACTIONS = 3,
71 //! Outputs do not overspend inputs, no double spends, coinbase output ok, immature coinbase spends, BIP30.
72 //! Implies all parents are also at least CHAIN.
73 BLOCK_VALID_CHAIN = 4,
75 //! Scripts & signatures ok. Implies all parents are also at least SCRIPTS.
76 BLOCK_VALID_SCRIPTS = 5,
78 //! All validity bits.
79 BLOCK_VALID_MASK = BLOCK_VALID_HEADER | BLOCK_VALID_TREE | BLOCK_VALID_TRANSACTIONS |
80 BLOCK_VALID_CHAIN | BLOCK_VALID_SCRIPTS,
82 BLOCK_HAVE_DATA = 8, //! full block available in blk*.dat
83 BLOCK_HAVE_UNDO = 16, //! undo data available in rev*.dat
84 BLOCK_HAVE_MASK = BLOCK_HAVE_DATA | BLOCK_HAVE_UNDO,
86 BLOCK_FAILED_VALID = 32, //! stage after last reached validness failed
87 BLOCK_FAILED_CHILD = 64, //! descends from failed block
88 BLOCK_FAILED_MASK = BLOCK_FAILED_VALID | BLOCK_FAILED_CHILD,
91 /** The block chain is a tree shaped structure starting with the
92 * genesis block at the root, with each block potentially having multiple
93 * candidates to be the next block. A blockindex may have multiple pprev pointing
94 * to it, but at most one of them can be part of the currently active branch.
99 //! pointer to the hash of the block, if any. Memory is owned by this CBlockIndex
100 const uint256* phashBlock;
102 //! pointer to the index of the predecessor of this block
105 //! pointer to the index of some further predecessor of this block
108 //! height of the entry in the chain. The genesis block has height 0
111 //! Which # file this block is stored in (blk?????.dat)
114 //! Byte offset within blk?????.dat where this block's data is stored
115 unsigned int nDataPos;
117 //! Byte offset within rev?????.dat where this block's undo data is stored
118 unsigned int nUndoPos;
120 //! (memory only) Total amount of work (expected number of hashes) in the chain up to and including this block
121 arith_uint256 nChainWork;
123 //! Number of transactions in this block.
124 //! Note: in a potential headers-first mode, this number cannot be relied upon
127 //! (memory only) Number of transactions in the chain up to and including this block.
128 //! This value will be non-zero only if and only if transactions for this block and all its parents are available.
129 //! Change to 64-bit type when necessary; won't happen before 2030
130 unsigned int nChainTx;
132 //! Verification status of this block. See enum BlockStatus
133 unsigned int nStatus;
137 uint256 hashMerkleRoot;
142 //! (memory only) Sequential id assigned to distinguish order in which blocks are received.
143 uint32_t nSequenceId;
154 nChainWork = arith_uint256();
161 hashMerkleRoot = uint256();
172 CBlockIndex(const CBlockHeader& block)
176 nVersion = block.nVersion;
177 hashMerkleRoot = block.hashMerkleRoot;
180 nNonce = block.nNonce;
183 CDiskBlockPos GetBlockPos() const {
185 if (nStatus & BLOCK_HAVE_DATA) {
192 CDiskBlockPos GetUndoPos() const {
194 if (nStatus & BLOCK_HAVE_UNDO) {
201 CBlockHeader GetBlockHeader() const
204 block.nVersion = nVersion;
206 block.hashPrevBlock = pprev->GetBlockHash();
207 block.hashMerkleRoot = hashMerkleRoot;
210 block.nNonce = nNonce;
214 uint256 GetBlockHash() const
219 int64_t GetBlockTime() const
221 return (int64_t)nTime;
224 enum { nMedianTimeSpan=11 };
226 int64_t GetMedianTimePast() const
228 int64_t pmedian[nMedianTimeSpan];
229 int64_t* pbegin = &pmedian[nMedianTimeSpan];
230 int64_t* pend = &pmedian[nMedianTimeSpan];
232 const CBlockIndex* pindex = this;
233 for (int i = 0; i < nMedianTimeSpan && pindex; i++, pindex = pindex->pprev)
234 *(--pbegin) = pindex->GetBlockTime();
236 std::sort(pbegin, pend);
237 return pbegin[(pend - pbegin)/2];
240 std::string ToString() const
242 return strprintf("CBlockIndex(pprev=%p, nHeight=%d, merkle=%s, hashBlock=%s)",
244 hashMerkleRoot.ToString(),
245 GetBlockHash().ToString());
248 //! Check whether this block index entry is valid up to the passed validity level.
249 bool IsValid(enum BlockStatus nUpTo = BLOCK_VALID_TRANSACTIONS) const
251 assert(!(nUpTo & ~BLOCK_VALID_MASK)); // Only validity flags allowed.
252 if (nStatus & BLOCK_FAILED_MASK)
254 return ((nStatus & BLOCK_VALID_MASK) >= nUpTo);
257 //! Raise the validity level of this block index entry.
258 //! Returns true if the validity was changed.
259 bool RaiseValidity(enum BlockStatus nUpTo)
261 assert(!(nUpTo & ~BLOCK_VALID_MASK)); // Only validity flags allowed.
262 if (nStatus & BLOCK_FAILED_MASK)
264 if ((nStatus & BLOCK_VALID_MASK) < nUpTo) {
265 nStatus = (nStatus & ~BLOCK_VALID_MASK) | nUpTo;
271 //! Build the skiplist pointer for this entry.
274 //! Efficiently find an ancestor of this block.
275 CBlockIndex* GetAncestor(int height);
276 const CBlockIndex* GetAncestor(int height) const;
279 /** Used to marshal pointers into hashes for db storage. */
280 class CDiskBlockIndex : public CBlockIndex
286 hashPrev = uint256();
289 explicit CDiskBlockIndex(const CBlockIndex* pindex) : CBlockIndex(*pindex) {
290 hashPrev = (pprev ? pprev->GetBlockHash() : uint256());
293 ADD_SERIALIZE_METHODS;
295 template <typename Stream, typename Operation>
296 inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
297 if (!(nType & SER_GETHASH))
298 READWRITE(VARINT(nVersion));
300 READWRITE(VARINT(nHeight));
301 READWRITE(VARINT(nStatus));
302 READWRITE(VARINT(nTx));
303 if (nStatus & (BLOCK_HAVE_DATA | BLOCK_HAVE_UNDO))
304 READWRITE(VARINT(nFile));
305 if (nStatus & BLOCK_HAVE_DATA)
306 READWRITE(VARINT(nDataPos));
307 if (nStatus & BLOCK_HAVE_UNDO)
308 READWRITE(VARINT(nUndoPos));
311 READWRITE(this->nVersion);
313 READWRITE(hashMerkleRoot);
319 uint256 GetBlockHash() const
322 block.nVersion = nVersion;
323 block.hashPrevBlock = hashPrev;
324 block.hashMerkleRoot = hashMerkleRoot;
327 block.nNonce = nNonce;
328 return block.GetHash();
332 std::string ToString() const
334 std::string str = "CDiskBlockIndex(";
335 str += CBlockIndex::ToString();
336 str += strprintf("\n hashBlock=%s, hashPrev=%s)",
337 GetBlockHash().ToString(),
338 hashPrev.ToString());
343 /** An in-memory indexed chain of blocks. */
346 std::vector<CBlockIndex*> vChain;
349 /** Returns the index entry for the genesis block of this chain, or NULL if none. */
350 CBlockIndex *Genesis() const {
351 return vChain.size() > 0 ? vChain[0] : NULL;
354 /** Returns the index entry for the tip of this chain, or NULL if none. */
355 CBlockIndex *Tip() const {
356 return vChain.size() > 0 ? vChain[vChain.size() - 1] : NULL;
359 /** Returns the index entry at a particular height in this chain, or NULL if no such height exists. */
360 CBlockIndex *operator[](int nHeight) const {
361 if (nHeight < 0 || nHeight >= (int)vChain.size())
363 return vChain[nHeight];
366 /** Compare two chains efficiently. */
367 friend bool operator==(const CChain &a, const CChain &b) {
368 return a.vChain.size() == b.vChain.size() &&
369 a.vChain[a.vChain.size() - 1] == b.vChain[b.vChain.size() - 1];
372 /** Efficiently check whether a block is present in this chain. */
373 bool Contains(const CBlockIndex *pindex) const {
374 return (*this)[pindex->nHeight] == pindex;
377 /** Find the successor of a block in this chain, or NULL if the given index is not found or is the tip. */
378 CBlockIndex *Next(const CBlockIndex *pindex) const {
379 if (Contains(pindex))
380 return (*this)[pindex->nHeight + 1];
385 /** Return the maximal height in the chain. Is equal to chain.Tip() ? chain.Tip()->nHeight : -1. */
387 return vChain.size() - 1;
390 /** Set/initialize a chain with a given tip. */
391 void SetTip(CBlockIndex *pindex);
393 /** Return a CBlockLocator that refers to a block in this chain (by default the tip). */
394 CBlockLocator GetLocator(const CBlockIndex *pindex = NULL) const;
396 /** Find the last common block between this chain and a block index entry. */
397 const CBlockIndex *FindFork(const CBlockIndex *pindex) const;
400 #endif // BITCOIN_CHAIN_H