]> Git Repo - VerusCoin.git/blob - src/chain.h
PBaaS - bulk of non-activated PBaaS changes for testing
[VerusCoin.git] / src / chain.h
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.
5
6 #ifndef BITCOIN_CHAIN_H
7 #define BITCOIN_CHAIN_H
8
9 class CChainPower;
10
11 #include "arith_uint256.h"
12 #include "primitives/block.h"
13 #include "pow.h"
14 #include "tinyformat.h"
15 #include "uint256.h"
16 #include "mmr.h"
17
18 #include <vector>
19
20 #include <boost/foreach.hpp>
21
22 static const int SPROUT_VALUE_VERSION = 1001400;
23 static const int SAPLING_VALUE_VERSION = 1010100;
24
25 struct CDiskBlockPos
26 {
27     int nFile;
28     unsigned int nPos;
29
30     ADD_SERIALIZE_METHODS;
31
32     template <typename Stream, typename Operation>
33     inline void SerializationOp(Stream& s, Operation ser_action) {
34         READWRITE(VARINT(nFile));
35         READWRITE(VARINT(nPos));
36     }
37
38     CDiskBlockPos() {
39         SetNull();
40     }
41
42     CDiskBlockPos(int nFileIn, unsigned int nPosIn) {
43         nFile = nFileIn;
44         nPos = nPosIn;
45     }
46
47     friend bool operator==(const CDiskBlockPos &a, const CDiskBlockPos &b) {
48         return (a.nFile == b.nFile && a.nPos == b.nPos);
49     }
50
51     friend bool operator!=(const CDiskBlockPos &a, const CDiskBlockPos &b) {
52         return !(a == b);
53     }
54
55     void SetNull() { nFile = -1; nPos = 0; }
56     bool IsNull() const { return (nFile == -1); }
57
58     std::string ToString() const
59     {
60         return strprintf("CBlockDiskPos(nFile=%i, nPos=%i)", nFile, nPos);
61     }
62
63 };
64
65 enum BlockStatus: uint32_t {
66     //! Unused.
67     BLOCK_VALID_UNKNOWN      =    0,
68
69     //! Parsed, version ok, hash satisfies claimed PoW, 1 <= vtx count <= max, timestamp not in future
70     BLOCK_VALID_HEADER       =    1,
71
72     //! All parent headers found, difficulty matches, timestamp >= median previous, checkpoint. Implies all parents
73     //! are also at least TREE.
74     BLOCK_VALID_TREE         =    2,
75
76     /**
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.
80      */
81     BLOCK_VALID_TRANSACTIONS =    3,
82
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,
86
87     //! Scripts & signatures ok. Implies all parents are also at least SCRIPTS.
88     BLOCK_VALID_SCRIPTS      =    5,
89
90     //! All validity bits.
91     BLOCK_VALID_MASK         =   BLOCK_VALID_HEADER | BLOCK_VALID_TREE | BLOCK_VALID_TRANSACTIONS |
92                                  BLOCK_VALID_CHAIN | BLOCK_VALID_SCRIPTS,
93
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,
97
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,
101
102     BLOCK_ACTIVATES_UPGRADE  =   128, //! block activates a network upgrade
103 };
104
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;
108
109 class CBlockIndex;
110
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.
117 class CChainPower
118 {
119     public:
120         arith_uint256 chainWork;
121         arith_uint256 chainStake;
122         int32_t nHeight;
123
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) {}
130
131         CChainPower &operator=(const CChainPower &chainPower)
132         {
133             chainWork = chainPower.chainWork;
134             chainStake = chainPower.chainStake;
135             nHeight = chainPower.nHeight;
136             return *this;
137         }
138
139         CChainPower &operator+=(const CChainPower &chainPower)
140         {
141             this->chainWork += chainPower.chainWork;
142             this->chainStake += chainPower.chainStake;
143             return *this;
144         }
145
146         friend CChainPower operator+(const CChainPower &chainPowerA, const CChainPower &chainPowerB)
147         {
148             CChainPower result = CChainPower(chainPowerA);
149             result.chainWork += chainPowerB.chainWork;
150             result.chainStake += chainPowerB.chainStake;
151             return result;
152         }
153
154         friend CChainPower operator-(const CChainPower &chainPowerA, const CChainPower &chainPowerB)
155         {
156             CChainPower result = CChainPower(chainPowerA);
157             result.chainWork -= chainPowerB.chainWork;
158             result.chainStake -= chainPowerB.chainStake;
159             return result;
160         }
161
162         friend CChainPower operator*(const CChainPower &chainPower, int32_t x)
163         {
164             CChainPower result = CChainPower(chainPower);
165             result.chainWork *= x;
166             result.chainStake *= x;
167             return result;
168         }
169
170         CChainPower &addStake(const arith_uint256 &nChainStake)
171         {
172             chainStake += nChainStake;
173             return *this;
174         }
175
176         CChainPower &addWork(const arith_uint256 &nChainWork)
177         {
178             chainWork += nChainWork;
179             return *this;
180         }
181
182         friend bool operator==(const CChainPower &p1, const CChainPower &p2);
183
184         friend bool operator!=(const CChainPower &p1, const CChainPower &p2)
185         {
186             return !(p1 == p2);
187         }
188
189         friend bool operator<(const CChainPower &p1, const CChainPower &p2);
190
191         friend bool operator<=(const CChainPower &p1, const CChainPower &p2);
192
193         friend bool operator>(const CChainPower &p1, const CChainPower &p2)
194         {
195             return !(p1 <= p2);
196         }
197
198         friend bool operator>=(const CChainPower &p1, const CChainPower &p2)
199         {
200             return !(p1 < p2);
201         }
202 };
203
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.
208  */
209 class CBlockIndex
210 {
211 public:
212     //! pointer to the hash of the block, if any. Memory is owned by this CBlockIndex
213     const uint256* phashBlock;
214
215     //! pointer to the index of the predecessor of this block
216     CBlockIndex* pprev;
217
218     //! pointer to the index of some further predecessor of this block
219     CBlockIndex* pskip;
220
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)
224     int nFile;
225
226     //! Byte offset within blk?????.dat where this block's data is stored
227     unsigned int nDataPos;
228
229     //! Byte offset within rev?????.dat where this block's undo data is stored
230     unsigned int nUndoPos;
231
232     //! (memory only) Total amount of work (expected number of hashes) in the chain up to and including this block
233     CChainPower chainPower;
234
235     //! Number of transactions in this block.
236     //! Note: in a potential headers-first mode, this number cannot be relied upon
237     unsigned int nTx;
238
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;
243
244     //! Verification status of this block. See enum BlockStatus
245     unsigned int nStatus;
246
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;
251
252     //! The anchor for the tree state up to the start of this block
253     uint256 hashSproutAnchor;
254
255     //! (memory only) The anchor for the tree state up to the end of this block
256     uint256 hashFinalSproutRoot;
257
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;
261
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;
266
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;
271
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;
275
276     //! block header
277     int nVersion;
278     uint256 hashMerkleRoot;
279     uint256 hashFinalSaplingRoot;
280     unsigned int nTime;
281     unsigned int nBits;
282     uint256 nNonce;
283     std::vector<unsigned char> nSolution;
284
285     //! (memory only) Sequential id assigned to distinguish order in which blocks are received.
286     uint32_t nSequenceId;
287     
288     void SetNull()
289     {
290         phashBlock = NULL;
291         newcoins = zfunds = 0;
292         segid = -2;
293         pprev = NULL;
294         pskip = NULL;
295         nFile = 0;
296         nDataPos = 0;
297         nUndoPos = 0;
298         chainPower = CChainPower();
299         nTx = 0;
300         nChainTx = 0;
301         nStatus = 0;
302         nCachedBranchId = boost::none;
303         hashSproutAnchor = uint256();
304         hashFinalSproutRoot = uint256();
305         nSequenceId = 0;
306         nSproutValue = boost::none;
307         nChainSproutValue = boost::none;
308         nSaplingValue = 0;
309         nChainSaplingValue = boost::none;
310
311         nVersion       = 0;
312         hashMerkleRoot = uint256();
313         hashFinalSaplingRoot   = uint256();
314         nTime          = 0;
315         nBits          = 0;
316         nNonce         = uint256();
317         nSolution.clear();
318     }
319
320     CBlockIndex()
321     {
322         SetNull();
323     }
324
325     CBlockIndex(const CBlockHeader& block)
326     {
327         SetNull();
328
329         nVersion       = block.nVersion;
330         hashMerkleRoot = block.hashMerkleRoot;
331         hashFinalSaplingRoot   = block.hashFinalSaplingRoot;
332         nTime          = block.nTime;
333         nBits          = block.nBits;
334         nNonce         = block.nNonce;
335         nSolution      = block.nSolution;
336     }
337
338     int32_t SetHeight(int32_t height)
339     {
340         this->chainPower.nHeight = height;
341     }
342
343     inline int32_t GetHeight() const
344     {
345         return this->chainPower.nHeight;
346     }
347
348     CDiskBlockPos GetBlockPos() const {
349         CDiskBlockPos ret;
350         if (nStatus & BLOCK_HAVE_DATA) {
351             ret.nFile = nFile;
352             ret.nPos  = nDataPos;
353         }
354         return ret;
355     }
356
357     CDiskBlockPos GetUndoPos() const {
358         CDiskBlockPos ret;
359         if (nStatus & BLOCK_HAVE_UNDO) {
360             ret.nFile = nFile;
361             ret.nPos  = nUndoPos;
362         }
363         return ret;
364     }
365
366     CBlockHeader GetBlockHeader() const
367     {
368         CBlockHeader block;
369         block.nVersion       = nVersion;
370         if (pprev)
371             block.hashPrevBlock = pprev->GetBlockHash();
372         block.hashMerkleRoot = hashMerkleRoot;
373         block.hashFinalSaplingRoot   = hashFinalSaplingRoot;
374         block.nTime          = nTime;
375         block.nBits          = nBits;
376         block.nNonce         = nNonce;
377         block.nSolution      = nSolution;
378         return block;
379     }
380
381     uint256 GetBlockHash() const
382     {
383         return *phashBlock;
384     }
385
386     int64_t GetBlockTime() const
387     {
388         return (int64_t)nTime;
389     }
390
391     enum { nMedianTimeSpan=11 };
392
393     int64_t GetMedianTimePast() const
394     {
395         int64_t pmedian[nMedianTimeSpan];
396         int64_t* pbegin = &pmedian[nMedianTimeSpan];
397         int64_t* pend = &pmedian[nMedianTimeSpan];
398
399         const CBlockIndex* pindex = this;
400         for (int i = 0; i < nMedianTimeSpan && pindex; i++, pindex = pindex->pprev)
401             *(--pbegin) = pindex->GetBlockTime();
402
403         std::sort(pbegin, pend);
404         return pbegin[(pend - pbegin)/2];
405     }
406
407     std::string ToString() const
408     {
409         return strprintf("CBlockIndex(pprev=%p, nHeight=%d, merkle=%s, hashBlock=%s)",
410             pprev, this->chainPower.nHeight,
411             hashMerkleRoot.ToString(),
412             GetBlockHash().ToString());
413     }
414
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
417     {
418         assert(!(nUpTo & ~BLOCK_VALID_MASK)); // Only validity flags allowed.
419         if (nStatus & BLOCK_FAILED_MASK)
420             return false;
421         return ((nStatus & BLOCK_VALID_MASK) >= nUpTo);
422     }
423
424     //! Raise the validity level of this block index entry.
425     //! Returns true if the validity was changed.
426     bool RaiseValidity(enum BlockStatus nUpTo)
427     {
428         assert(!(nUpTo & ~BLOCK_VALID_MASK)); // Only validity flags allowed.
429         if (nStatus & BLOCK_FAILED_MASK)
430             return false;
431         if ((nStatus & BLOCK_VALID_MASK) < nUpTo) {
432             nStatus = (nStatus & ~BLOCK_VALID_MASK) | nUpTo;
433             return true;
434         }
435         return false;
436     }
437
438     //! Build the skiplist pointer for this entry.
439     void BuildSkip();
440
441     //! Efficiently find an ancestor of this block.
442     CBlockIndex* GetAncestor(int height);
443     const CBlockIndex* GetAncestor(int height) const;
444
445     int32_t GetVerusPOSTarget() const
446     {
447         return GetBlockHeader().GetVerusPOSTarget();
448     }
449
450     bool IsVerusPOSBlock() const
451     {
452         return GetBlockHeader().IsVerusPOSBlock();
453     }
454
455     bool GetRawVerusPOSHash(uint256 &ret) const;
456     uint256 GetVerusEntropyHash() const;
457
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()
460     {
461         uint256 blockHash = GetBlockHash();
462
463         uint256 preHash = Hash(BEGIN(hashMerkleRoot), END(hashMerkleRoot), BEGIN(blockHash), END(blockHash));
464         uint256 power = ArithToUint256(GetCompactPower(nNonce, nBits, nVersion));
465
466         return CMMRPowerNode(Hash(BEGIN(preHash), END(preHash), BEGIN(power), END(power)), power);
467     }
468
469     void AddMerkleProofBridge(CMerkleBranch &branch)
470     {
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());
473     }
474
475     void AddBlockProofBridge(CMerkleBranch &branch)
476     {
477         // we need to add the merkle root on the left
478         branch.nIndex |= (1 << branch.branch.size());
479         branch.branch.push_back(hashMerkleRoot);
480     }
481 };
482
483 /** Used to marshal pointers into hashes for db storage. */
484 class CDiskBlockIndex : public CBlockIndex
485 {
486 public:
487     uint256 hashPrev;
488
489     CDiskBlockIndex() : CBlockIndex() {
490         hashPrev = uint256();
491     }
492
493     explicit CDiskBlockIndex(const CBlockIndex* pindex) : CBlockIndex(*pindex) {
494         hashPrev = (pprev ? pprev->GetBlockHash() : uint256());
495     }
496
497     ADD_SERIALIZE_METHODS;
498
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);
504 #endif
505         if (!(s.GetType() & SER_GETHASH))
506             READWRITE(VARINT(nVersion));
507
508         if (ser_action.ForRead()) {
509             chainPower = CChainPower();
510         }
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()) {
522                 uint32_t branchId;
523                 READWRITE(branchId);
524                 nCachedBranchId = branchId;
525             } else {
526                 // nCachedBranchId must always be set if BLOCK_ACTIVATES_UPGRADE is set.
527                 assert(nCachedBranchId);
528                 uint32_t branchId = *nCachedBranchId;
529                 READWRITE(branchId);
530             }
531         }
532         READWRITE(hashSproutAnchor);
533
534         // block header
535         READWRITE(this->nVersion);
536         READWRITE(hashPrev);
537         READWRITE(hashMerkleRoot);
538         READWRITE(hashFinalSaplingRoot);
539         READWRITE(nTime);
540         READWRITE(nBits);
541         READWRITE(nNonce);
542         READWRITE(nSolution);
543
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);
548         }
549
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);
554         }
555     }
556
557     uint256 GetBlockHash() const
558     {
559         CBlockHeader block;
560         block.nVersion        = nVersion;
561         block.hashPrevBlock   = hashPrev;
562         block.hashMerkleRoot  = hashMerkleRoot;
563         block.hashFinalSaplingRoot    = hashFinalSaplingRoot;
564         block.nTime           = nTime;
565         block.nBits           = nBits;
566         block.nNonce          = nNonce;
567         block.nSolution       = nSolution;
568         return block.GetHash();
569     }
570
571
572     std::string ToString() const
573     {
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());
577         return str;
578     }
579 };
580
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.
584 */
585 class CChain {
586 private:
587     std::vector<CBlockIndex*> vChain;
588     CMerkleMountainRange<CMMRPowerNode, CChunkedLayer<CMMRPowerNode>, COverlayNodeLayer<CMMRPowerNode, CChain>> mmr;
589     CBlockIndex *lastTip;
590
591 public:
592     CChain() : vChain(), mmr(COverlayNodeLayer<CMMRPowerNode, CChain>(*this)) {}
593
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;
597     }
598
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;
602     }
603     
604     /** Returns the last tip of the chain, or NULL if none. */
605     CBlockIndex *LastTip() const {
606         return vChain.size() > 0 ? lastTip : NULL;
607     }
608
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())
612             return NULL;
613         return vChain[nHeight];
614     }
615
616     /** Get the Merkle Mountain Range for this chain. */
617     const CMerkleMountainRange<CMMRPowerNode, CChunkedLayer<CMMRPowerNode>, COverlayNodeLayer<CMMRPowerNode, CChain>> &GetMMR()
618     {
619         return mmr;
620     }
621
622     /** Get the Merkle Mountain Range for this chain. */
623     CMerkleMountainView<CMMRPowerNode, CChunkedLayer<CMMRPowerNode>, COverlayNodeLayer<CMMRPowerNode, CChain>> GetMMV()
624     {
625         return CMerkleMountainView<CMMRPowerNode, CChunkedLayer<CMMRPowerNode>, COverlayNodeLayer<CMMRPowerNode, CChain>>(mmr, mmr.size());
626     }    
627
628     CMMRPowerNode GetMMRNode(int index)
629     {
630         return vChain[index]->GetMMRNode();
631     }
632
633     bool GetBlockProof(CMerkleMountainView<CMMRPowerNode, CChunkedLayer<CMMRPowerNode>, COverlayNodeLayer<CMMRPowerNode, CChain>> &view, 
634                        CMerkleBranch &retBranch,
635                        int index)
636     {
637         CBlockIndex *pindex = (index < 0 || index >= (int)vChain.size()) ? NULL : vChain[index];
638         if (pindex)
639         {
640             pindex->AddBlockProofBridge(retBranch);
641             return view.GetProof(retBranch, index);
642         }
643         else
644         {
645             return false;
646         }
647     }
648
649     bool GetMerkleProof(CMerkleMountainView<CMMRPowerNode, CChunkedLayer<CMMRPowerNode>, COverlayNodeLayer<CMMRPowerNode, CChain>> &view, 
650                         CMerkleBranch &retBranch,
651                         int index)
652     {
653         CBlockIndex *pindex = (index < 0 || index >= (int)vChain.size()) ? NULL : vChain[index];
654         if (pindex)
655         {
656             pindex->AddMerkleProofBridge(retBranch);
657             return view.GetProof(retBranch, index);
658         }
659         else
660         {
661             return false;
662         }
663     }
664
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];
669     }
670
671     /** Efficiently check whether a block is present in this chain. */
672     bool Contains(const CBlockIndex *pindex) const {
673         return (*this)[pindex->GetHeight()] == pindex;
674     }
675
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];
680         else
681             return NULL;
682     }
683
684     /** Return the maximal height in the chain. Is equal to chain.Tip() ? chain.Tip()->GetHeight() : -1. */
685     int Height() const {
686         return vChain.size() - 1;
687     }
688
689     uint64_t size()
690     {
691         return vChain.size();
692     }
693
694     /** Set/initialize a chain with a given tip. */
695     void SetTip(CBlockIndex *pindex);
696
697     /** Return a CBlockLocator that refers to a block in this chain (by default the tip). */
698     CBlockLocator GetLocator(const CBlockIndex *pindex = NULL) const;
699
700     /** Find the last common block between this chain and a block index entry. */
701     const CBlockIndex *FindFork(const CBlockIndex *pindex) const;
702 };
703
704 CChainPower ExpandCompactPower(uint256 compactPower, uint32_t height = 0);
705
706 typedef CMerkleMountainRange<CMMRPowerNode, CChunkedLayer<CMMRPowerNode>, COverlayNodeLayer<CMMRPowerNode, CChain>> ChainMerkleMountainRange;
707 typedef CMerkleMountainView<CMMRPowerNode, CChunkedLayer<CMMRPowerNode>, COverlayNodeLayer<CMMRPowerNode, CChain>> ChainMerkleMountainView;
708
709 #endif // BITCOIN_CHAIN_H
This page took 0.061264 seconds and 4 git commands to generate.