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
11 #include "arith_uint256.h"
12 #include "primitives/block.h"
14 #include "tinyformat.h"
20 #include <boost/foreach.hpp>
22 static const int SPROUT_VALUE_VERSION = 1001400;
23 static const int SAPLING_VALUE_VERSION = 1010100;
30 ADD_SERIALIZE_METHODS;
32 template <typename Stream, typename Operation>
33 inline void SerializationOp(Stream& s, Operation ser_action) {
34 READWRITE(VARINT(nFile));
35 READWRITE(VARINT(nPos));
42 CDiskBlockPos(int nFileIn, unsigned int nPosIn) {
47 friend bool operator==(const CDiskBlockPos &a, const CDiskBlockPos &b) {
48 return (a.nFile == b.nFile && a.nPos == b.nPos);
51 friend bool operator!=(const CDiskBlockPos &a, const CDiskBlockPos &b) {
55 void SetNull() { nFile = -1; nPos = 0; }
56 bool IsNull() const { return (nFile == -1); }
58 std::string ToString() const
60 return strprintf("CBlockDiskPos(nFile=%i, nPos=%i)", nFile, nPos);
65 enum BlockStatus: uint32_t {
67 BLOCK_VALID_UNKNOWN = 0,
69 //! Parsed, version ok, hash satisfies claimed PoW, 1 <= vtx count <= max, timestamp not in future
70 BLOCK_VALID_HEADER = 1,
72 //! All parent headers found, difficulty matches, timestamp >= median previous, checkpoint. Implies all parents
73 //! are also at least TREE.
77 * Only first tx is coinbase, 2 <= coinbase input script length <= 100, transactions valid, no duplicate txids,
78 * sigops, size, merkle root. Implies all parents are at least TREE but not necessarily TRANSACTIONS. When all
79 * parent blocks also have TRANSACTIONS, CBlockIndex::nChainTx will be set.
81 BLOCK_VALID_TRANSACTIONS = 3,
83 //! Outputs do not overspend inputs, no double spends, coinbase output ok, no immature coinbase spends, BIP30.
84 //! Implies all parents are also at least CHAIN.
85 BLOCK_VALID_CHAIN = 4,
87 //! Scripts & signatures ok. Implies all parents are also at least SCRIPTS.
88 BLOCK_VALID_SCRIPTS = 5,
90 //! All validity bits.
91 BLOCK_VALID_MASK = BLOCK_VALID_HEADER | BLOCK_VALID_TREE | BLOCK_VALID_TRANSACTIONS |
92 BLOCK_VALID_CHAIN | BLOCK_VALID_SCRIPTS,
94 BLOCK_HAVE_DATA = 8, //! full block available in blk*.dat
95 BLOCK_HAVE_UNDO = 16, //! undo data available in rev*.dat
96 BLOCK_HAVE_MASK = BLOCK_HAVE_DATA | BLOCK_HAVE_UNDO,
98 BLOCK_FAILED_VALID = 32, //! stage after last reached validness failed
99 BLOCK_FAILED_CHILD = 64, //! descends from failed block
100 BLOCK_FAILED_MASK = BLOCK_FAILED_VALID | BLOCK_FAILED_CHILD,
102 BLOCK_ACTIVATES_UPGRADE = 128, //! block activates a network upgrade
105 //! Short-hand for the highest consensus validity we implement.
106 //! Blocks with this validity are assumed to satisfy all consensus rules.
107 static const BlockStatus BLOCK_VALID_CONSENSUS = BLOCK_VALID_SCRIPTS;
111 // This class provides an accumulator for both the chainwork and the chainPOS value
112 // CChainPower's can be compared, and the comparison ensures that work and proof of stake power
113 // are both used equally to determine which chain has the most work. This makes an attack
114 // that involves mining in secret completely ineffective, even before dPOW, unless a large part
115 // of the staking supply is also controlled. It also enables a faster deterministic convergence,
116 // aided by both POS and POW.
120 arith_uint256 chainWork;
121 arith_uint256 chainStake;
124 CChainPower() : nHeight(0), chainStake(0), chainWork(0) {}
125 CChainPower(CBlockIndex *pblockIndex);
126 CChainPower(CBlockIndex *pblockIndex, const arith_uint256 &stake, const arith_uint256 &work);
127 CChainPower(int32_t height) : nHeight(height), chainStake(0), chainWork(0) {}
128 CChainPower(int32_t height, const arith_uint256 &stake, const arith_uint256 &work) :
129 nHeight(height), chainStake(stake), chainWork(work) {}
131 CChainPower &operator=(const CChainPower &chainPower)
133 chainWork = chainPower.chainWork;
134 chainStake = chainPower.chainStake;
135 nHeight = chainPower.nHeight;
139 CChainPower &operator+=(const CChainPower &chainPower)
141 this->chainWork += chainPower.chainWork;
142 this->chainStake += chainPower.chainStake;
146 friend CChainPower operator+(const CChainPower &chainPowerA, const CChainPower &chainPowerB)
148 CChainPower result = CChainPower(chainPowerA);
149 result.chainWork += chainPowerB.chainWork;
150 result.chainStake += chainPowerB.chainStake;
154 friend CChainPower operator-(const CChainPower &chainPowerA, const CChainPower &chainPowerB)
156 CChainPower result = CChainPower(chainPowerA);
157 result.chainWork -= chainPowerB.chainWork;
158 result.chainStake -= chainPowerB.chainStake;
162 friend CChainPower operator*(const CChainPower &chainPower, int32_t x)
164 CChainPower result = CChainPower(chainPower);
165 result.chainWork *= x;
166 result.chainStake *= x;
170 CChainPower &addStake(const arith_uint256 &nChainStake)
172 chainStake += nChainStake;
176 CChainPower &addWork(const arith_uint256 &nChainWork)
178 chainWork += nChainWork;
182 friend bool operator==(const CChainPower &p1, const CChainPower &p2);
184 friend bool operator!=(const CChainPower &p1, const CChainPower &p2)
189 friend bool operator<(const CChainPower &p1, const CChainPower &p2);
191 friend bool operator<=(const CChainPower &p1, const CChainPower &p2);
193 friend bool operator>(const CChainPower &p1, const CChainPower &p2)
198 friend bool operator>=(const CChainPower &p1, const CChainPower &p2)
204 /** The block chain is a tree shaped structure starting with the
205 * genesis block at the root, with each block potentially having multiple
206 * candidates to be the next block. A blockindex may have multiple pprev pointing
207 * to it, but at most one of them can be part of the currently active branch.
212 //! pointer to the hash of the block, if any. Memory is owned by this CBlockIndex
213 const uint256* phashBlock;
215 //! pointer to the index of the predecessor of this block
218 //! pointer to the index of some further predecessor of this block
221 //! height of the entry in the chain. The genesis block has height 0
222 int64_t newcoins,zfunds; int8_t segid; // jl777 fields
223 //! Which # file this block is stored in (blk?????.dat)
226 //! Byte offset within blk?????.dat where this block's data is stored
227 unsigned int nDataPos;
229 //! Byte offset within rev?????.dat where this block's undo data is stored
230 unsigned int nUndoPos;
232 //! (memory only) Total amount of work (expected number of hashes) in the chain up to and including this block
233 CChainPower chainPower;
235 //! Number of transactions in this block.
236 //! Note: in a potential headers-first mode, this number cannot be relied upon
239 //! (memory only) Number of transactions in the chain up to and including this block.
240 //! This value will be non-zero only if and only if transactions for this block and all its parents are available.
241 //! Change to 64-bit type when necessary; won't happen before 2030
242 unsigned int nChainTx;
244 //! Verification status of this block. See enum BlockStatus
245 unsigned int nStatus;
247 //! Branch ID corresponding to the consensus rules used to validate this block.
248 //! Only cached if block validity is BLOCK_VALID_CONSENSUS.
249 //! Persisted at each activation height, memory-only for intervening blocks.
250 boost::optional<uint32_t> nCachedBranchId;
252 //! The anchor for the tree state up to the start of this block
253 uint256 hashSproutAnchor;
255 //! (memory only) The anchor for the tree state up to the end of this block
256 uint256 hashFinalSproutRoot;
258 //! Change in value held by the Sprout circuit over this block.
259 //! Will be boost::none for older blocks on old nodes until a reindex has taken place.
260 boost::optional<CAmount> nSproutValue;
262 //! (memory only) Total value held by the Sprout circuit up to and including this block.
263 //! Will be boost::none for on old nodes until a reindex has taken place.
264 //! Will be boost::none if nChainTx is zero.
265 boost::optional<CAmount> nChainSproutValue;
267 //! Change in value held by the Sapling circuit over this block.
268 //! Not a boost::optional because this was added before Sapling activated, so we can
269 //! rely on the invariant that every block before this was added had nSaplingValue = 0.
270 CAmount nSaplingValue;
272 //! (memory only) Total value held by the Sapling circuit up to and including this block.
273 //! Will be boost::none if nChainTx is zero.
274 boost::optional<CAmount> nChainSaplingValue;
278 uint256 hashMerkleRoot;
279 uint256 hashFinalSaplingRoot;
283 std::vector<unsigned char> nSolution;
285 //! (memory only) Sequential id assigned to distinguish order in which blocks are received.
286 uint32_t nSequenceId;
291 newcoins = zfunds = 0;
298 chainPower = CChainPower();
302 nCachedBranchId = boost::none;
303 hashSproutAnchor = uint256();
304 hashFinalSproutRoot = uint256();
306 nSproutValue = boost::none;
307 nChainSproutValue = boost::none;
309 nChainSaplingValue = boost::none;
312 hashMerkleRoot = uint256();
313 hashFinalSaplingRoot = uint256();
325 CBlockIndex(const CBlockHeader& block)
329 nVersion = block.nVersion;
330 hashMerkleRoot = block.hashMerkleRoot;
331 hashFinalSaplingRoot = block.hashFinalSaplingRoot;
334 nNonce = block.nNonce;
335 nSolution = block.nSolution;
338 int32_t SetHeight(int32_t height)
340 this->chainPower.nHeight = height;
343 inline int32_t GetHeight() const
345 return this->chainPower.nHeight;
348 CDiskBlockPos GetBlockPos() const {
350 if (nStatus & BLOCK_HAVE_DATA) {
357 CDiskBlockPos GetUndoPos() const {
359 if (nStatus & BLOCK_HAVE_UNDO) {
366 CBlockHeader GetBlockHeader() const
369 block.nVersion = nVersion;
371 block.hashPrevBlock = pprev->GetBlockHash();
372 block.hashMerkleRoot = hashMerkleRoot;
373 block.hashFinalSaplingRoot = hashFinalSaplingRoot;
376 block.nNonce = nNonce;
377 block.nSolution = nSolution;
381 uint256 GetBlockHash() const
386 int64_t GetBlockTime() const
388 return (int64_t)nTime;
391 enum { nMedianTimeSpan=11 };
393 int64_t GetMedianTimePast() const
395 int64_t pmedian[nMedianTimeSpan];
396 int64_t* pbegin = &pmedian[nMedianTimeSpan];
397 int64_t* pend = &pmedian[nMedianTimeSpan];
399 const CBlockIndex* pindex = this;
400 for (int i = 0; i < nMedianTimeSpan && pindex; i++, pindex = pindex->pprev)
401 *(--pbegin) = pindex->GetBlockTime();
403 std::sort(pbegin, pend);
404 return pbegin[(pend - pbegin)/2];
407 std::string ToString() const
409 return strprintf("CBlockIndex(pprev=%p, nHeight=%d, merkle=%s, hashBlock=%s)",
410 pprev, this->chainPower.nHeight,
411 hashMerkleRoot.ToString(),
412 GetBlockHash().ToString());
415 //! Check whether this block index entry is valid up to the passed validity level.
416 bool IsValid(enum BlockStatus nUpTo = BLOCK_VALID_TRANSACTIONS) const
418 assert(!(nUpTo & ~BLOCK_VALID_MASK)); // Only validity flags allowed.
419 if (nStatus & BLOCK_FAILED_MASK)
421 return ((nStatus & BLOCK_VALID_MASK) >= nUpTo);
424 //! Raise the validity level of this block index entry.
425 //! Returns true if the validity was changed.
426 bool RaiseValidity(enum BlockStatus nUpTo)
428 assert(!(nUpTo & ~BLOCK_VALID_MASK)); // Only validity flags allowed.
429 if (nStatus & BLOCK_FAILED_MASK)
431 if ((nStatus & BLOCK_VALID_MASK) < nUpTo) {
432 nStatus = (nStatus & ~BLOCK_VALID_MASK) | nUpTo;
438 //! Build the skiplist pointer for this entry.
441 //! Efficiently find an ancestor of this block.
442 CBlockIndex* GetAncestor(int height);
443 const CBlockIndex* GetAncestor(int height) const;
445 int32_t GetVerusPOSTarget() const
447 return GetBlockHeader().GetVerusPOSTarget();
450 bool IsVerusPOSBlock() const
452 return GetBlockHeader().IsVerusPOSBlock();
455 bool GetRawVerusPOSHash(uint256 &ret) const;
456 uint256 GetVerusEntropyHash() const;
458 // return a node from this block index as is, including hash of merkle root and block hash as well as compact chain power, to put into an MMR
459 CMMRPowerNode GetMMRNode()
461 uint256 blockHash = GetBlockHash();
463 uint256 preHash = Hash(BEGIN(hashMerkleRoot), END(hashMerkleRoot), BEGIN(blockHash), END(blockHash));
464 uint256 power = ArithToUint256(GetCompactPower(nNonce, nBits, nVersion));
466 return CMMRPowerNode(Hash(BEGIN(preHash), END(preHash), BEGIN(power), END(power)), power);
469 void AddMerkleProofBridge(CMerkleBranch &branch)
471 // we need to add the block hash on the right, no change to index, as bit is zero
472 branch.branch.push_back(GetBlockHash());
475 void AddBlockProofBridge(CMerkleBranch &branch)
477 // we need to add the merkle root on the left
478 branch.nIndex |= (1 << branch.branch.size());
479 branch.branch.push_back(hashMerkleRoot);
483 /** Used to marshal pointers into hashes for db storage. */
484 class CDiskBlockIndex : public CBlockIndex
489 CDiskBlockIndex() : CBlockIndex() {
490 hashPrev = uint256();
493 explicit CDiskBlockIndex(const CBlockIndex* pindex) : CBlockIndex(*pindex) {
494 hashPrev = (pprev ? pprev->GetBlockHash() : uint256());
497 ADD_SERIALIZE_METHODS;
499 template <typename Stream, typename Operation>
500 inline void SerializationOp(Stream& s, Operation ser_action) {
501 int nVersion = s.GetVersion();
502 #ifdef VERUSHASHDEBUG
503 if (!ser_action.ForRead()) printf("Serializing block index %s, stream version: %x\n", ToString().c_str(), nVersion);
505 if (!(s.GetType() & SER_GETHASH))
506 READWRITE(VARINT(nVersion));
508 if (ser_action.ForRead()) {
509 chainPower = CChainPower();
511 READWRITE(VARINT(chainPower.nHeight));
512 READWRITE(VARINT(nStatus));
513 READWRITE(VARINT(nTx));
514 if (nStatus & (BLOCK_HAVE_DATA | BLOCK_HAVE_UNDO))
515 READWRITE(VARINT(nFile));
516 if (nStatus & BLOCK_HAVE_DATA)
517 READWRITE(VARINT(nDataPos));
518 if (nStatus & BLOCK_HAVE_UNDO)
519 READWRITE(VARINT(nUndoPos));
520 if (nStatus & BLOCK_ACTIVATES_UPGRADE) {
521 if (ser_action.ForRead()) {
524 nCachedBranchId = branchId;
526 // nCachedBranchId must always be set if BLOCK_ACTIVATES_UPGRADE is set.
527 assert(nCachedBranchId);
528 uint32_t branchId = *nCachedBranchId;
532 READWRITE(hashSproutAnchor);
535 READWRITE(this->nVersion);
537 READWRITE(hashMerkleRoot);
538 READWRITE(hashFinalSaplingRoot);
542 READWRITE(nSolution);
544 // Only read/write nSproutValue if the client version used to create
545 // this index was storing them.
546 if ((s.GetType() & SER_DISK) && (nVersion >= SPROUT_VALUE_VERSION)) {
547 READWRITE(nSproutValue);
550 // Only read/write nSaplingValue if the client version used to create
551 // this index was storing them.
552 if ((s.GetType() & SER_DISK) && (nVersion >= SAPLING_VALUE_VERSION)) {
553 READWRITE(nSaplingValue);
557 uint256 GetBlockHash() const
560 block.nVersion = nVersion;
561 block.hashPrevBlock = hashPrev;
562 block.hashMerkleRoot = hashMerkleRoot;
563 block.hashFinalSaplingRoot = hashFinalSaplingRoot;
566 block.nNonce = nNonce;
567 block.nSolution = nSolution;
568 return block.GetHash();
572 std::string ToString() const
574 std::string str = "CDiskBlockIndex(";
575 str += strprintf("nVersion=%x, pprev=%p, nHeight=%d, merkle=%s\nhashBlock=%s, hashPrev=%s)\n",
576 this->nVersion, pprev, this->chainPower.nHeight, hashMerkleRoot.ToString(), GetBlockHash().ToString(), hashPrev.ToString());
581 /** An in-memory indexed chain of blocks.
582 * With Verus and PBaaS chains, this also provides a complete Merkle Mountain Range (MMR) for the chain at all times,
583 * enabling proof of any transaction that can be exported and trusted on any chain that has a trusted oracle or other lite proof of this chain.
587 std::vector<CBlockIndex*> vChain;
588 CMerkleMountainRange<CMMRPowerNode, CChunkedLayer<CMMRPowerNode>, COverlayNodeLayer<CMMRPowerNode, CChain>> mmr;
589 CBlockIndex *lastTip;
592 CChain() : vChain(), mmr(COverlayNodeLayer<CMMRPowerNode, CChain>(*this)) {}
594 /** Returns the index entry for the genesis block of this chain, or NULL if none. */
595 CBlockIndex *Genesis() const {
596 return vChain.size() > 0 ? vChain[0] : NULL;
599 /** Returns the index entry for the tip of this chain, or NULL if none. */
600 CBlockIndex *Tip() const {
601 return vChain.size() > 0 ? vChain[vChain.size() - 1] : NULL;
604 /** Returns the last tip of the chain, or NULL if none. */
605 CBlockIndex *LastTip() const {
606 return vChain.size() > 0 ? lastTip : NULL;
609 /** Returns the index entry at a particular height in this chain, or NULL if no such height exists. */
610 CBlockIndex *operator[](int nHeight) const {
611 if (nHeight < 0 || nHeight >= (int)vChain.size())
613 return vChain[nHeight];
616 /** Get the Merkle Mountain Range for this chain. */
617 const CMerkleMountainRange<CMMRPowerNode, CChunkedLayer<CMMRPowerNode>, COverlayNodeLayer<CMMRPowerNode, CChain>> &GetMMR()
622 /** Get the Merkle Mountain Range for this chain. */
623 CMerkleMountainView<CMMRPowerNode, CChunkedLayer<CMMRPowerNode>, COverlayNodeLayer<CMMRPowerNode, CChain>> GetMMV()
625 return CMerkleMountainView<CMMRPowerNode, CChunkedLayer<CMMRPowerNode>, COverlayNodeLayer<CMMRPowerNode, CChain>>(mmr, mmr.size());
628 CMMRPowerNode GetMMRNode(int index)
630 return vChain[index]->GetMMRNode();
633 bool GetBlockProof(CMerkleMountainView<CMMRPowerNode, CChunkedLayer<CMMRPowerNode>, COverlayNodeLayer<CMMRPowerNode, CChain>> &view,
634 CMerkleBranch &retBranch,
637 CBlockIndex *pindex = (index < 0 || index >= (int)vChain.size()) ? NULL : vChain[index];
640 pindex->AddBlockProofBridge(retBranch);
641 return view.GetProof(retBranch, index);
649 bool GetMerkleProof(CMerkleMountainView<CMMRPowerNode, CChunkedLayer<CMMRPowerNode>, COverlayNodeLayer<CMMRPowerNode, CChain>> &view,
650 CMerkleBranch &retBranch,
653 CBlockIndex *pindex = (index < 0 || index >= (int)vChain.size()) ? NULL : vChain[index];
656 pindex->AddMerkleProofBridge(retBranch);
657 return view.GetProof(retBranch, index);
665 /** Compare two chains efficiently. */
666 friend bool operator==(const CChain &a, const CChain &b) {
667 return a.vChain.size() == b.vChain.size() &&
668 a.vChain[a.vChain.size() - 1] == b.vChain[b.vChain.size() - 1];
671 /** Efficiently check whether a block is present in this chain. */
672 bool Contains(const CBlockIndex *pindex) const {
673 return (*this)[pindex->GetHeight()] == pindex;
676 /** Find the successor of a block in this chain, or NULL if the given index is not found or is the tip. */
677 CBlockIndex *Next(const CBlockIndex *pindex) const {
678 if (Contains(pindex))
679 return (*this)[pindex->GetHeight() + 1];
684 /** Return the maximal height in the chain. Is equal to chain.Tip() ? chain.Tip()->GetHeight() : -1. */
686 return vChain.size() - 1;
691 return vChain.size();
694 /** Set/initialize a chain with a given tip. */
695 void SetTip(CBlockIndex *pindex);
697 /** Return a CBlockLocator that refers to a block in this chain (by default the tip). */
698 CBlockLocator GetLocator(const CBlockIndex *pindex = NULL) const;
700 /** Find the last common block between this chain and a block index entry. */
701 const CBlockIndex *FindFork(const CBlockIndex *pindex) const;
704 CChainPower ExpandCompactPower(uint256 compactPower, uint32_t height = 0);
706 typedef CMerkleMountainRange<CMMRPowerNode, CChunkedLayer<CMMRPowerNode>, COverlayNodeLayer<CMMRPowerNode, CChain>> ChainMerkleMountainRange;
707 typedef CMerkleMountainView<CMMRPowerNode, CChunkedLayer<CMMRPowerNode>, COverlayNodeLayer<CMMRPowerNode, CChain>> ChainMerkleMountainView;
709 #endif // BITCOIN_CHAIN_H