]>
Commit | Line | Data |
---|---|---|
e8b5f0d5 | 1 | // Copyright (c) 2009-2010 Satoshi Nakamoto |
f914f1a7 | 2 | // Copyright (c) 2009-2014 The Bitcoin Core developers |
7bec6dd2 | 3 | // Distributed under the MIT software license, see the accompanying |
e8b5f0d5 | 4 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. |
5 | ||
84738627 PJ |
6 | #ifndef BITCOIN_CHAIN_H |
7 | #define BITCOIN_CHAIN_H | |
e8b5f0d5 | 8 | |
734f85c4 | 9 | #include "arith_uint256.h" |
d2270111 | 10 | #include "primitives/block.h" |
e8b5f0d5 | 11 | #include "pow.h" |
85c579e3 | 12 | #include "tinyformat.h" |
e8b5f0d5 | 13 | #include "uint256.h" |
14 | ||
15 | #include <vector> | |
16 | ||
17 | #include <boost/foreach.hpp> | |
18 | ||
ad6a36ad JG |
19 | static const int SPROUT_VALUE_VERSION = 1001400; |
20 | ||
e8b5f0d5 | 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); } | |
f5791c6a WL |
53 | |
54 | std::string ToString() const | |
55 | { | |
56 | return strprintf("CBlockDiskPos(nFile=%i, nPos=%i)", nFile, nPos); | |
57 | } | |
58 | ||
e8b5f0d5 | 59 | }; |
60 | ||
0e2b1ae2 | 61 | enum BlockStatus: uint32_t { |
2fdc3351 | 62 | //! Unused. |
e8b5f0d5 | 63 | BLOCK_VALID_UNKNOWN = 0, |
341735eb | 64 | |
2fdc3351 | 65 | //! Parsed, version ok, hash satisfies claimed PoW, 1 <= vtx count <= max, timestamp not in future |
341735eb PW |
66 | BLOCK_VALID_HEADER = 1, |
67 | ||
2fdc3351 MF |
68 | //! All parent headers found, difficulty matches, timestamp >= median previous, checkpoint. Implies all parents |
69 | //! are also at least TREE. | |
341735eb PW |
70 | BLOCK_VALID_TREE = 2, |
71 | ||
2fdc3351 MF |
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 | */ | |
341735eb PW |
77 | BLOCK_VALID_TRANSACTIONS = 3, |
78 | ||
b05a89b2 | 79 | //! Outputs do not overspend inputs, no double spends, coinbase output ok, no immature coinbase spends, BIP30. |
2fdc3351 | 80 | //! Implies all parents are also at least CHAIN. |
341735eb PW |
81 | BLOCK_VALID_CHAIN = 4, |
82 | ||
2fdc3351 | 83 | //! Scripts & signatures ok. Implies all parents are also at least SCRIPTS. |
341735eb PW |
84 | BLOCK_VALID_SCRIPTS = 5, |
85 | ||
2fdc3351 | 86 | //! All validity bits. |
e8b5f0d5 | 87 | BLOCK_VALID_MASK = BLOCK_VALID_HEADER | BLOCK_VALID_TREE | BLOCK_VALID_TRANSACTIONS | |
88 | BLOCK_VALID_CHAIN | BLOCK_VALID_SCRIPTS, | |
89 | ||
2fdc3351 MF |
90 | BLOCK_HAVE_DATA = 8, //! full block available in blk*.dat |
91 | BLOCK_HAVE_UNDO = 16, //! undo data available in rev*.dat | |
e8b5f0d5 | 92 | BLOCK_HAVE_MASK = BLOCK_HAVE_DATA | BLOCK_HAVE_UNDO, |
93 | ||
2fdc3351 MF |
94 | BLOCK_FAILED_VALID = 32, //! stage after last reached validness failed |
95 | BLOCK_FAILED_CHILD = 64, //! descends from failed block | |
e8b5f0d5 | 96 | BLOCK_FAILED_MASK = BLOCK_FAILED_VALID | BLOCK_FAILED_CHILD, |
89f20450 | 97 | |
9e851450 | 98 | BLOCK_ACTIVATES_UPGRADE = 128, //! block activates a network upgrade |
e8b5f0d5 | 99 | }; |
100 | ||
9e851450 JG |
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 | ||
e8b5f0d5 | 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: | |
bf7835c2 | 113 | //! pointer to the hash of the block, if any. Memory is owned by this CBlockIndex |
e8b5f0d5 | 114 | const uint256* phashBlock; |
115 | ||
2fdc3351 | 116 | //! pointer to the index of the predecessor of this block |
e8b5f0d5 | 117 | CBlockIndex* pprev; |
118 | ||
2fdc3351 | 119 | //! pointer to the index of some further predecessor of this block |
e8b5f0d5 | 120 | CBlockIndex* pskip; |
121 | ||
2fdc3351 | 122 | //! height of the entry in the chain. The genesis block has height 0 |
e8b5f0d5 | 123 | int nHeight; |
124 | ||
2fdc3351 | 125 | //! Which # file this block is stored in (blk?????.dat) |
e8b5f0d5 | 126 | int nFile; |
127 | ||
2fdc3351 | 128 | //! Byte offset within blk?????.dat where this block's data is stored |
e8b5f0d5 | 129 | unsigned int nDataPos; |
130 | ||
2fdc3351 | 131 | //! Byte offset within rev?????.dat where this block's undo data is stored |
e8b5f0d5 | 132 | unsigned int nUndoPos; |
133 | ||
2fdc3351 | 134 | //! (memory only) Total amount of work (expected number of hashes) in the chain up to and including this block |
734f85c4 | 135 | arith_uint256 nChainWork; |
e8b5f0d5 | 136 | |
2fdc3351 MF |
137 | //! Number of transactions in this block. |
138 | //! Note: in a potential headers-first mode, this number cannot be relied upon | |
e8b5f0d5 | 139 | unsigned int nTx; |
140 | ||
2fdc3351 MF |
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; | |
e8b5f0d5 | 145 | |
2fdc3351 | 146 | //! Verification status of this block. See enum BlockStatus |
e8b5f0d5 | 147 | unsigned int nStatus; |
148 | ||
9e851450 JG |
149 | //! Branch ID corresponding to the consensus rules used to validate this block. |
150 | //! Only accurate if block validity is BLOCK_VALID_CONSENSUS. | |
151 | //! Persisted at each activation height, memory-only for intervening blocks. | |
152 | uint32_t nConsensusBranchId; | |
153 | ||
b6961fc1 JG |
154 | //! The anchor for the tree state up to the start of this block |
155 | uint256 hashAnchor; | |
156 | ||
0bc1e2c4 JG |
157 | //! (memory only) The anchor for the tree state up to the end of this block |
158 | uint256 hashAnchorEnd; | |
159 | ||
ad6a36ad JG |
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 | ||
2fdc3351 | 169 | //! block header |
e8b5f0d5 | 170 | int nVersion; |
171 | uint256 hashMerkleRoot; | |
a8d384ae | 172 | uint256 hashReserved; |
e8b5f0d5 | 173 | unsigned int nTime; |
174 | unsigned int nBits; | |
fdda3c50 | 175 | uint256 nNonce; |
5be6abbf | 176 | std::vector<unsigned char> nSolution; |
e8b5f0d5 | 177 | |
2fdc3351 | 178 | //! (memory only) Sequential id assigned to distinguish order in which blocks are received. |
e8b5f0d5 | 179 | uint32_t nSequenceId; |
180 | ||
181 | void SetNull() | |
182 | { | |
183 | phashBlock = NULL; | |
184 | pprev = NULL; | |
185 | pskip = NULL; | |
186 | nHeight = 0; | |
187 | nFile = 0; | |
188 | nDataPos = 0; | |
189 | nUndoPos = 0; | |
734f85c4 | 190 | nChainWork = arith_uint256(); |
e8b5f0d5 | 191 | nTx = 0; |
192 | nChainTx = 0; | |
193 | nStatus = 0; | |
9e851450 | 194 | nConsensusBranchId = 0; |
b6961fc1 | 195 | hashAnchor = uint256(); |
0bc1e2c4 | 196 | hashAnchorEnd = uint256(); |
e8b5f0d5 | 197 | nSequenceId = 0; |
ad6a36ad JG |
198 | nSproutValue = boost::none; |
199 | nChainSproutValue = boost::none; | |
e8b5f0d5 | 200 | |
201 | nVersion = 0; | |
4f152496 | 202 | hashMerkleRoot = uint256(); |
a8d384ae | 203 | hashReserved = uint256(); |
e8b5f0d5 | 204 | nTime = 0; |
205 | nBits = 0; | |
fdda3c50 JG |
206 | nNonce = uint256(); |
207 | nSolution.clear(); | |
e8b5f0d5 | 208 | } |
209 | ||
210 | CBlockIndex() | |
211 | { | |
212 | SetNull(); | |
213 | } | |
214 | ||
341735eb | 215 | CBlockIndex(const CBlockHeader& block) |
e8b5f0d5 | 216 | { |
217 | SetNull(); | |
218 | ||
219 | nVersion = block.nVersion; | |
220 | hashMerkleRoot = block.hashMerkleRoot; | |
a8d384ae | 221 | hashReserved = block.hashReserved; |
e8b5f0d5 | 222 | nTime = block.nTime; |
223 | nBits = block.nBits; | |
224 | nNonce = block.nNonce; | |
fdda3c50 | 225 | nSolution = block.nSolution; |
e8b5f0d5 | 226 | } |
227 | ||
228 | CDiskBlockPos GetBlockPos() const { | |
229 | CDiskBlockPos ret; | |
230 | if (nStatus & BLOCK_HAVE_DATA) { | |
231 | ret.nFile = nFile; | |
232 | ret.nPos = nDataPos; | |
233 | } | |
234 | return ret; | |
235 | } | |
236 | ||
237 | CDiskBlockPos GetUndoPos() const { | |
238 | CDiskBlockPos ret; | |
239 | if (nStatus & BLOCK_HAVE_UNDO) { | |
240 | ret.nFile = nFile; | |
241 | ret.nPos = nUndoPos; | |
242 | } | |
243 | return ret; | |
244 | } | |
245 | ||
246 | CBlockHeader GetBlockHeader() const | |
247 | { | |
248 | CBlockHeader block; | |
249 | block.nVersion = nVersion; | |
250 | if (pprev) | |
251 | block.hashPrevBlock = pprev->GetBlockHash(); | |
252 | block.hashMerkleRoot = hashMerkleRoot; | |
a8d384ae | 253 | block.hashReserved = hashReserved; |
e8b5f0d5 | 254 | block.nTime = nTime; |
255 | block.nBits = nBits; | |
256 | block.nNonce = nNonce; | |
fdda3c50 | 257 | block.nSolution = nSolution; |
e8b5f0d5 | 258 | return block; |
259 | } | |
260 | ||
261 | uint256 GetBlockHash() const | |
262 | { | |
263 | return *phashBlock; | |
264 | } | |
265 | ||
266 | int64_t GetBlockTime() const | |
267 | { | |
268 | return (int64_t)nTime; | |
269 | } | |
270 | ||
e8b5f0d5 | 271 | enum { nMedianTimeSpan=11 }; |
272 | ||
273 | int64_t GetMedianTimePast() const | |
274 | { | |
275 | int64_t pmedian[nMedianTimeSpan]; | |
276 | int64_t* pbegin = &pmedian[nMedianTimeSpan]; | |
277 | int64_t* pend = &pmedian[nMedianTimeSpan]; | |
278 | ||
279 | const CBlockIndex* pindex = this; | |
280 | for (int i = 0; i < nMedianTimeSpan && pindex; i++, pindex = pindex->pprev) | |
281 | *(--pbegin) = pindex->GetBlockTime(); | |
282 | ||
283 | std::sort(pbegin, pend); | |
284 | return pbegin[(pend - pbegin)/2]; | |
285 | } | |
286 | ||
e8b5f0d5 | 287 | std::string ToString() const |
288 | { | |
289 | return strprintf("CBlockIndex(pprev=%p, nHeight=%d, merkle=%s, hashBlock=%s)", | |
290 | pprev, nHeight, | |
291 | hashMerkleRoot.ToString(), | |
292 | GetBlockHash().ToString()); | |
293 | } | |
294 | ||
2fdc3351 | 295 | //! Check whether this block index entry is valid up to the passed validity level. |
e8b5f0d5 | 296 | bool IsValid(enum BlockStatus nUpTo = BLOCK_VALID_TRANSACTIONS) const |
297 | { | |
298 | assert(!(nUpTo & ~BLOCK_VALID_MASK)); // Only validity flags allowed. | |
299 | if (nStatus & BLOCK_FAILED_MASK) | |
300 | return false; | |
301 | return ((nStatus & BLOCK_VALID_MASK) >= nUpTo); | |
302 | } | |
303 | ||
2fdc3351 MF |
304 | //! Raise the validity level of this block index entry. |
305 | //! Returns true if the validity was changed. | |
e8b5f0d5 | 306 | bool RaiseValidity(enum BlockStatus nUpTo) |
307 | { | |
308 | assert(!(nUpTo & ~BLOCK_VALID_MASK)); // Only validity flags allowed. | |
309 | if (nStatus & BLOCK_FAILED_MASK) | |
310 | return false; | |
311 | if ((nStatus & BLOCK_VALID_MASK) < nUpTo) { | |
312 | nStatus = (nStatus & ~BLOCK_VALID_MASK) | nUpTo; | |
313 | return true; | |
314 | } | |
315 | return false; | |
316 | } | |
317 | ||
2fdc3351 | 318 | //! Build the skiplist pointer for this entry. |
e8b5f0d5 | 319 | void BuildSkip(); |
320 | ||
2fdc3351 | 321 | //! Efficiently find an ancestor of this block. |
e8b5f0d5 | 322 | CBlockIndex* GetAncestor(int height); |
323 | const CBlockIndex* GetAncestor(int height) const; | |
324 | }; | |
325 | ||
326 | /** Used to marshal pointers into hashes for db storage. */ | |
327 | class CDiskBlockIndex : public CBlockIndex | |
328 | { | |
329 | public: | |
330 | uint256 hashPrev; | |
331 | ||
332 | CDiskBlockIndex() { | |
4f152496 | 333 | hashPrev = uint256(); |
e8b5f0d5 | 334 | } |
335 | ||
63d1ae55 | 336 | explicit CDiskBlockIndex(const CBlockIndex* pindex) : CBlockIndex(*pindex) { |
4f152496 | 337 | hashPrev = (pprev ? pprev->GetBlockHash() : uint256()); |
e8b5f0d5 | 338 | } |
339 | ||
340 | ADD_SERIALIZE_METHODS; | |
341 | ||
342 | template <typename Stream, typename Operation> | |
343 | inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { | |
344 | if (!(nType & SER_GETHASH)) | |
345 | READWRITE(VARINT(nVersion)); | |
346 | ||
347 | READWRITE(VARINT(nHeight)); | |
348 | READWRITE(VARINT(nStatus)); | |
349 | READWRITE(VARINT(nTx)); | |
350 | if (nStatus & (BLOCK_HAVE_DATA | BLOCK_HAVE_UNDO)) | |
351 | READWRITE(VARINT(nFile)); | |
352 | if (nStatus & BLOCK_HAVE_DATA) | |
353 | READWRITE(VARINT(nDataPos)); | |
354 | if (nStatus & BLOCK_HAVE_UNDO) | |
355 | READWRITE(VARINT(nUndoPos)); | |
9e851450 JG |
356 | if (nStatus & BLOCK_ACTIVATES_UPGRADE) |
357 | READWRITE(nConsensusBranchId); | |
b6961fc1 | 358 | READWRITE(hashAnchor); |
e8b5f0d5 | 359 | |
360 | // block header | |
361 | READWRITE(this->nVersion); | |
362 | READWRITE(hashPrev); | |
363 | READWRITE(hashMerkleRoot); | |
a8d384ae | 364 | READWRITE(hashReserved); |
e8b5f0d5 | 365 | READWRITE(nTime); |
366 | READWRITE(nBits); | |
367 | READWRITE(nNonce); | |
fdda3c50 | 368 | READWRITE(nSolution); |
ad6a36ad JG |
369 | |
370 | // Only read/write nSproutValue if the client version used to create | |
371 | // this index was storing them. | |
9d0c70e9 | 372 | if ((nType & SER_DISK) && (nVersion >= SPROUT_VALUE_VERSION)) { |
ad6a36ad JG |
373 | READWRITE(nSproutValue); |
374 | } | |
e8b5f0d5 | 375 | } |
376 | ||
377 | uint256 GetBlockHash() const | |
378 | { | |
379 | CBlockHeader block; | |
380 | block.nVersion = nVersion; | |
381 | block.hashPrevBlock = hashPrev; | |
382 | block.hashMerkleRoot = hashMerkleRoot; | |
a8d384ae | 383 | block.hashReserved = hashReserved; |
e8b5f0d5 | 384 | block.nTime = nTime; |
385 | block.nBits = nBits; | |
386 | block.nNonce = nNonce; | |
fdda3c50 | 387 | block.nSolution = nSolution; |
e8b5f0d5 | 388 | return block.GetHash(); |
389 | } | |
390 | ||
391 | ||
392 | std::string ToString() const | |
393 | { | |
394 | std::string str = "CDiskBlockIndex("; | |
395 | str += CBlockIndex::ToString(); | |
396 | str += strprintf("\n hashBlock=%s, hashPrev=%s)", | |
397 | GetBlockHash().ToString(), | |
398 | hashPrev.ToString()); | |
399 | return str; | |
400 | } | |
401 | }; | |
402 | ||
403 | /** An in-memory indexed chain of blocks. */ | |
404 | class CChain { | |
405 | private: | |
406 | std::vector<CBlockIndex*> vChain; | |
407 | ||
408 | public: | |
409 | /** Returns the index entry for the genesis block of this chain, or NULL if none. */ | |
410 | CBlockIndex *Genesis() const { | |
411 | return vChain.size() > 0 ? vChain[0] : NULL; | |
412 | } | |
413 | ||
414 | /** Returns the index entry for the tip of this chain, or NULL if none. */ | |
415 | CBlockIndex *Tip() const { | |
416 | return vChain.size() > 0 ? vChain[vChain.size() - 1] : NULL; | |
417 | } | |
418 | ||
419 | /** Returns the index entry at a particular height in this chain, or NULL if no such height exists. */ | |
420 | CBlockIndex *operator[](int nHeight) const { | |
421 | if (nHeight < 0 || nHeight >= (int)vChain.size()) | |
422 | return NULL; | |
423 | return vChain[nHeight]; | |
424 | } | |
425 | ||
426 | /** Compare two chains efficiently. */ | |
427 | friend bool operator==(const CChain &a, const CChain &b) { | |
428 | return a.vChain.size() == b.vChain.size() && | |
429 | a.vChain[a.vChain.size() - 1] == b.vChain[b.vChain.size() - 1]; | |
430 | } | |
431 | ||
432 | /** Efficiently check whether a block is present in this chain. */ | |
433 | bool Contains(const CBlockIndex *pindex) const { | |
434 | return (*this)[pindex->nHeight] == pindex; | |
435 | } | |
436 | ||
437 | /** Find the successor of a block in this chain, or NULL if the given index is not found or is the tip. */ | |
438 | CBlockIndex *Next(const CBlockIndex *pindex) const { | |
439 | if (Contains(pindex)) | |
440 | return (*this)[pindex->nHeight + 1]; | |
441 | else | |
442 | return NULL; | |
443 | } | |
444 | ||
445 | /** Return the maximal height in the chain. Is equal to chain.Tip() ? chain.Tip()->nHeight : -1. */ | |
446 | int Height() const { | |
447 | return vChain.size() - 1; | |
448 | } | |
449 | ||
b7ae2c17 | 450 | /** Set/initialize a chain with a given tip. */ |
451 | void SetTip(CBlockIndex *pindex); | |
e8b5f0d5 | 452 | |
453 | /** Return a CBlockLocator that refers to a block in this chain (by default the tip). */ | |
454 | CBlockLocator GetLocator(const CBlockIndex *pindex = NULL) const; | |
455 | ||
456 | /** Find the last common block between this chain and a block index entry. */ | |
457 | const CBlockIndex *FindFork(const CBlockIndex *pindex) const; | |
458 | }; | |
459 | ||
84738627 | 460 | #endif // BITCOIN_CHAIN_H |