]> Git Repo - VerusCoin.git/blob - src/chain.h
Catch mined PoS lookalike blocks
[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 #include "arith_uint256.h"
10 #include "primitives/block.h"
11 #include "pow.h"
12 #include "tinyformat.h"
13 #include "uint256.h"
14
15 #include <vector>
16
17 #include <boost/foreach.hpp>
18
19 static const int SPROUT_VALUE_VERSION = 1001400;
20
21 struct CDiskBlockPos
22 {
23     int nFile;
24     unsigned int nPos;
25
26     ADD_SERIALIZE_METHODS;
27
28     template <typename Stream, typename Operation>
29     inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
30         READWRITE(VARINT(nFile));
31         READWRITE(VARINT(nPos));
32     }
33
34     CDiskBlockPos() {
35         SetNull();
36     }
37
38     CDiskBlockPos(int nFileIn, unsigned int nPosIn) {
39         nFile = nFileIn;
40         nPos = nPosIn;
41     }
42
43     friend bool operator==(const CDiskBlockPos &a, const CDiskBlockPos &b) {
44         return (a.nFile == b.nFile && a.nPos == b.nPos);
45     }
46
47     friend bool operator!=(const CDiskBlockPos &a, const CDiskBlockPos &b) {
48         return !(a == b);
49     }
50
51     void SetNull() { nFile = -1; nPos = 0; }
52     bool IsNull() const { return (nFile == -1); }
53
54     std::string ToString() const
55     {
56         return strprintf("CBlockDiskPos(nFile=%i, nPos=%i)", nFile, nPos);
57     }
58
59 };
60
61 enum BlockStatus: uint32_t {
62     //! Unused.
63     BLOCK_VALID_UNKNOWN      =    0,
64
65     //! Parsed, version ok, hash satisfies claimed PoW, 1 <= vtx count <= max, timestamp not in future
66     BLOCK_VALID_HEADER       =    1,
67
68     //! All parent headers found, difficulty matches, timestamp >= median previous, checkpoint. Implies all parents
69     //! are also at least TREE.
70     BLOCK_VALID_TREE         =    2,
71
72     /**
73      * Only first tx is coinbase, 2 <= coinbase input script length <= 100, transactions valid, no duplicate txids,
74      * sigops, size, merkle root. Implies all parents are at least TREE but not necessarily TRANSACTIONS. When all
75      * parent blocks also have TRANSACTIONS, CBlockIndex::nChainTx will be set.
76      */
77     BLOCK_VALID_TRANSACTIONS =    3,
78
79     //! Outputs do not overspend inputs, no double spends, coinbase output ok, no immature coinbase spends, BIP30.
80     //! Implies all parents are also at least CHAIN.
81     BLOCK_VALID_CHAIN        =    4,
82
83     //! Scripts & signatures ok. Implies all parents are also at least SCRIPTS.
84     BLOCK_VALID_SCRIPTS      =    5,
85
86     //! All validity bits.
87     BLOCK_VALID_MASK         =   BLOCK_VALID_HEADER | BLOCK_VALID_TREE | BLOCK_VALID_TRANSACTIONS |
88                                  BLOCK_VALID_CHAIN | BLOCK_VALID_SCRIPTS,
89
90     BLOCK_HAVE_DATA          =    8, //! full block available in blk*.dat
91     BLOCK_HAVE_UNDO          =   16, //! undo data available in rev*.dat
92     BLOCK_HAVE_MASK          =   BLOCK_HAVE_DATA | BLOCK_HAVE_UNDO,
93
94     BLOCK_FAILED_VALID       =   32, //! stage after last reached validness failed
95     BLOCK_FAILED_CHILD       =   64, //! descends from failed block
96     BLOCK_FAILED_MASK        =   BLOCK_FAILED_VALID | BLOCK_FAILED_CHILD,
97
98     BLOCK_ACTIVATES_UPGRADE  =   128, //! block activates a network upgrade
99 };
100
101 //! Short-hand for the highest consensus validity we implement.
102 //! Blocks with this validity are assumed to satisfy all consensus rules.
103 static const BlockStatus BLOCK_VALID_CONSENSUS = BLOCK_VALID_SCRIPTS;
104
105 /** The block chain is a tree shaped structure starting with the
106  * genesis block at the root, with each block potentially having multiple
107  * candidates to be the next block. A blockindex may have multiple pprev pointing
108  * to it, but at most one of them can be part of the currently active branch.
109  */
110 class CBlockIndex
111 {
112 public:
113     //! pointer to the hash of the block, if any. Memory is owned by this CBlockIndex
114     const uint256* phashBlock;
115
116     //! pointer to the index of the predecessor of this block
117     CBlockIndex* pprev;
118
119     //! pointer to the index of some further predecessor of this block
120     CBlockIndex* pskip;
121
122     //! height of the entry in the chain. The genesis block has height 0
123     int nHeight;
124     int64_t newcoins,zfunds;
125     //! Which # file this block is stored in (blk?????.dat)
126     int nFile;
127
128     //! Byte offset within blk?????.dat where this block's data is stored
129     unsigned int nDataPos;
130
131     //! Byte offset within rev?????.dat where this block's undo data is stored
132     unsigned int nUndoPos;
133
134     //! (memory only) Total amount of work (expected number of hashes) in the chain up to and including this block
135     arith_uint256 nChainWork;
136
137     //! Number of transactions in this block.
138     //! Note: in a potential headers-first mode, this number cannot be relied upon
139     unsigned int nTx;
140
141     //! (memory only) Number of transactions in the chain up to and including this block.
142     //! This value will be non-zero only if and only if transactions for this block and all its parents are available.
143     //! Change to 64-bit type when necessary; won't happen before 2030
144     unsigned int nChainTx;
145
146     //! Verification status of this block. See enum BlockStatus
147     unsigned int nStatus;
148
149     //! Branch ID corresponding to the consensus rules used to validate this block.
150     //! Only cached if block validity is BLOCK_VALID_CONSENSUS.
151     //! Persisted at each activation height, memory-only for intervening blocks.
152     boost::optional<uint32_t> nCachedBranchId;
153
154     //! The anchor for the tree state up to the start of this block
155     uint256 hashAnchor;
156
157     //! (memory only) The anchor for the tree state up to the end of this block
158     uint256 hashAnchorEnd;
159
160     //! Change in value held by the Sprout circuit over this block.
161     //! Will be boost::none for older blocks on old nodes until a reindex has taken place.
162     boost::optional<CAmount> nSproutValue;
163
164     //! (memory only) Total value held by the Sprout circuit up to and including this block.
165     //! Will be boost::none for on old nodes until a reindex has taken place.
166     //! Will be boost::none if nChainTx is zero.
167     boost::optional<CAmount> nChainSproutValue;
168
169     //! block header
170     int nVersion;
171     uint256 hashMerkleRoot;
172     uint256 hashReserved;
173     unsigned int nTime;
174     unsigned int nBits;
175     uint256 nNonce;
176     std::vector<unsigned char> nSolution;
177
178     //! (memory only) Sequential id assigned to distinguish order in which blocks are received.
179     uint32_t nSequenceId;
180     
181     void SetNull()
182     {
183         phashBlock = NULL;
184         newcoins = zfunds = 0;
185         pprev = NULL;
186         pskip = NULL;
187         nHeight = 0;
188         nFile = 0;
189         nDataPos = 0;
190         nUndoPos = 0;
191         nChainWork = arith_uint256();
192         nTx = 0;
193         nChainTx = 0;
194         nStatus = 0;
195         nCachedBranchId = boost::none;
196         hashAnchor = uint256();
197         hashAnchorEnd = uint256();
198         nSequenceId = 0;
199         nSproutValue = boost::none;
200         nChainSproutValue = boost::none;
201
202         nVersion       = 0;
203         hashMerkleRoot = uint256();
204         hashReserved   = uint256();
205         nTime          = 0;
206         nBits          = 0;
207         nNonce         = uint256();
208         nSolution.clear();
209     }
210
211     CBlockIndex()
212     {
213         SetNull();
214     }
215
216     CBlockIndex(const CBlockHeader& block)
217     {
218         SetNull();
219
220         nVersion       = block.nVersion;
221         hashMerkleRoot = block.hashMerkleRoot;
222         hashReserved   = block.hashReserved;
223         nTime          = block.nTime;
224         nBits          = block.nBits;
225         nNonce         = block.nNonce;
226         nSolution      = block.nSolution;
227     }
228
229     CDiskBlockPos GetBlockPos() const {
230         CDiskBlockPos ret;
231         if (nStatus & BLOCK_HAVE_DATA) {
232             ret.nFile = nFile;
233             ret.nPos  = nDataPos;
234         }
235         return ret;
236     }
237
238     CDiskBlockPos GetUndoPos() const {
239         CDiskBlockPos ret;
240         if (nStatus & BLOCK_HAVE_UNDO) {
241             ret.nFile = nFile;
242             ret.nPos  = nUndoPos;
243         }
244         return ret;
245     }
246
247     CBlockHeader GetBlockHeader() const
248     {
249         CBlockHeader block;
250         block.nVersion       = nVersion;
251         if (pprev)
252             block.hashPrevBlock = pprev->GetBlockHash();
253         block.hashMerkleRoot = hashMerkleRoot;
254         block.hashReserved   = hashReserved;
255         block.nTime          = nTime;
256         block.nBits          = nBits;
257         block.nNonce         = nNonce;
258         block.nSolution      = nSolution;
259         return block;
260     }
261
262     uint256 GetBlockHash() const
263     {
264         return *phashBlock;
265     }
266
267     int64_t GetBlockTime() const
268     {
269         return (int64_t)nTime;
270     }
271
272     enum { nMedianTimeSpan=11 };
273
274     int64_t GetMedianTimePast() const
275     {
276         int64_t pmedian[nMedianTimeSpan];
277         int64_t* pbegin = &pmedian[nMedianTimeSpan];
278         int64_t* pend = &pmedian[nMedianTimeSpan];
279
280         const CBlockIndex* pindex = this;
281         for (int i = 0; i < nMedianTimeSpan && pindex; i++, pindex = pindex->pprev)
282             *(--pbegin) = pindex->GetBlockTime();
283
284         std::sort(pbegin, pend);
285         return pbegin[(pend - pbegin)/2];
286     }
287
288     std::string ToString() const
289     {
290         return strprintf("CBlockIndex(pprev=%p, nHeight=%d, merkle=%s, hashBlock=%s)",
291             pprev, nHeight,
292             hashMerkleRoot.ToString(),
293             GetBlockHash().ToString());
294     }
295
296     //! Check whether this block index entry is valid up to the passed validity level.
297     bool IsValid(enum BlockStatus nUpTo = BLOCK_VALID_TRANSACTIONS) const
298     {
299         assert(!(nUpTo & ~BLOCK_VALID_MASK)); // Only validity flags allowed.
300         if (nStatus & BLOCK_FAILED_MASK)
301             return false;
302         return ((nStatus & BLOCK_VALID_MASK) >= nUpTo);
303     }
304
305     //! Raise the validity level of this block index entry.
306     //! Returns true if the validity was changed.
307     bool RaiseValidity(enum BlockStatus nUpTo)
308     {
309         assert(!(nUpTo & ~BLOCK_VALID_MASK)); // Only validity flags allowed.
310         if (nStatus & BLOCK_FAILED_MASK)
311             return false;
312         if ((nStatus & BLOCK_VALID_MASK) < nUpTo) {
313             nStatus = (nStatus & ~BLOCK_VALID_MASK) | nUpTo;
314             return true;
315         }
316         return false;
317     }
318
319     //! Build the skiplist pointer for this entry.
320     void BuildSkip();
321
322     //! Efficiently find an ancestor of this block.
323     CBlockIndex* GetAncestor(int height);
324     const CBlockIndex* GetAncestor(int height) const;
325
326 };
327
328 /** Used to marshal pointers into hashes for db storage. */
329 class CDiskBlockIndex : public CBlockIndex
330 {
331 public:
332     uint256 hashPrev;
333
334     CDiskBlockIndex() {
335         hashPrev = uint256();
336     }
337
338     explicit CDiskBlockIndex(const CBlockIndex* pindex) : CBlockIndex(*pindex) {
339         hashPrev = (pprev ? pprev->GetBlockHash() : uint256());
340     }
341
342     ADD_SERIALIZE_METHODS;
343
344     template <typename Stream, typename Operation>
345     inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
346         if (!(nType & SER_GETHASH))
347             READWRITE(VARINT(nVersion));
348
349         READWRITE(VARINT(nHeight));
350         READWRITE(VARINT(nStatus));
351         READWRITE(VARINT(nTx));
352         if (nStatus & (BLOCK_HAVE_DATA | BLOCK_HAVE_UNDO))
353             READWRITE(VARINT(nFile));
354         if (nStatus & BLOCK_HAVE_DATA)
355             READWRITE(VARINT(nDataPos));
356         if (nStatus & BLOCK_HAVE_UNDO)
357             READWRITE(VARINT(nUndoPos));
358         if (nStatus & BLOCK_ACTIVATES_UPGRADE) {
359             if (ser_action.ForRead()) {
360                 uint32_t branchId;
361                 READWRITE(branchId);
362                 nCachedBranchId = branchId;
363             } else {
364                 // nCachedBranchId must always be set if BLOCK_ACTIVATES_UPGRADE is set.
365                 assert(nCachedBranchId);
366                 uint32_t branchId = *nCachedBranchId;
367                 READWRITE(branchId);
368             }
369         }
370         READWRITE(hashAnchor);
371
372         // block header
373         READWRITE(this->nVersion);
374         READWRITE(hashPrev);
375         READWRITE(hashMerkleRoot);
376         READWRITE(hashReserved);
377         READWRITE(nTime);
378         READWRITE(nBits);
379         READWRITE(nNonce);
380         READWRITE(nSolution);
381
382         // Only read/write nSproutValue if the client version used to create
383         // this index was storing them.
384         if ((nType & SER_DISK) && (nVersion >= SPROUT_VALUE_VERSION)) {
385             READWRITE(nSproutValue);
386         }
387     }
388
389     uint256 GetBlockHash() const
390     {
391         CBlockHeader block;
392         block.nVersion        = nVersion;
393         block.hashPrevBlock   = hashPrev;
394         block.hashMerkleRoot  = hashMerkleRoot;
395         block.hashReserved    = hashReserved;
396         block.nTime           = nTime;
397         block.nBits           = nBits;
398         block.nNonce          = nNonce;
399         block.nSolution       = nSolution;
400         return block.GetHash();
401     }
402
403
404     std::string ToString() const
405     {
406         std::string str = "CDiskBlockIndex(";
407         str += CBlockIndex::ToString();
408         str += strprintf("\n                hashBlock=%s, hashPrev=%s)",
409             GetBlockHash().ToString(),
410             hashPrev.ToString());
411         return str;
412     }
413 };
414
415 /** An in-memory indexed chain of blocks. */
416 class CChain {
417 private:
418     std::vector<CBlockIndex*> vChain;
419     CBlockIndex *lastTip;
420
421 public:
422     /** Returns the index entry for the genesis block of this chain, or NULL if none. */
423     CBlockIndex *Genesis() const {
424         return vChain.size() > 0 ? vChain[0] : NULL;
425     }
426
427     /** Returns the index entry for the tip of this chain, or NULL if none. */
428     CBlockIndex *Tip() const {
429         return vChain.size() > 0 ? vChain[vChain.size() - 1] : NULL;
430     }
431     
432     /** Returns the last tip of the chain, or NULL if none. */
433     CBlockIndex *LastTip() const {
434         return vChain.size() > 0 ? lastTip : NULL;
435     }
436
437     /** Returns the index entry at a particular height in this chain, or NULL if no such height exists. */
438     CBlockIndex *operator[](int nHeight) const {
439         if (nHeight < 0 || nHeight >= (int)vChain.size())
440             return NULL;
441         return vChain[nHeight];
442     }
443
444     /** Compare two chains efficiently. */
445     friend bool operator==(const CChain &a, const CChain &b) {
446         return a.vChain.size() == b.vChain.size() &&
447                a.vChain[a.vChain.size() - 1] == b.vChain[b.vChain.size() - 1];
448     }
449
450     /** Efficiently check whether a block is present in this chain. */
451     bool Contains(const CBlockIndex *pindex) const {
452         return (*this)[pindex->nHeight] == pindex;
453     }
454
455     /** Find the successor of a block in this chain, or NULL if the given index is not found or is the tip. */
456     CBlockIndex *Next(const CBlockIndex *pindex) const {
457         if (Contains(pindex))
458             return (*this)[pindex->nHeight + 1];
459         else
460             return NULL;
461     }
462
463     /** Return the maximal height in the chain. Is equal to chain.Tip() ? chain.Tip()->nHeight : -1. */
464     int Height() const {
465         return vChain.size() - 1;
466     }
467
468     /** Set/initialize a chain with a given tip. */
469     void SetTip(CBlockIndex *pindex);
470
471     /** Return a CBlockLocator that refers to a block in this chain (by default the tip). */
472     CBlockLocator GetLocator(const CBlockIndex *pindex = NULL) const;
473
474     /** Find the last common block between this chain and a block index entry. */
475     const CBlockIndex *FindFork(const CBlockIndex *pindex) const;
476 };
477
478 #endif // BITCOIN_CHAIN_H
This page took 0.048755 seconds and 4 git commands to generate.