Branch data Line data Source code
1 : : // Copyright (c) 2009-2010 Satoshi Nakamoto
2 : : // Copyright (c) 2009-2022 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 <consensus/params.h>
11 : : #include <flatfile.h>
12 : : #include <kernel/cs_main.h>
13 : : #include <primitives/block.h>
14 : : #include <serialize.h>
15 : : #include <sync.h>
16 : : #include <uint256.h>
17 : : #include <util/time.h>
18 : :
19 : : #include <algorithm>
20 : : #include <cassert>
21 : : #include <cstdint>
22 : : #include <string>
23 : : #include <vector>
24 : :
25 : : /**
26 : : * Maximum amount of time that a block timestamp is allowed to exceed the
27 : : * current time before the block will be accepted.
28 : : */
29 : : static constexpr int64_t MAX_FUTURE_BLOCK_TIME = 2 * 60 * 60;
30 : :
31 : : /**
32 : : * Timestamp window used as a grace period by code that compares external
33 : : * timestamps (such as timestamps passed to RPCs, or wallet key creation times)
34 : : * to block timestamps. This should be set at least as high as
35 : : * MAX_FUTURE_BLOCK_TIME.
36 : : */
37 : : static constexpr int64_t TIMESTAMP_WINDOW = MAX_FUTURE_BLOCK_TIME;
38 : : //! Init values for CBlockIndex nSequenceId when loaded from disk
39 : : static constexpr int32_t SEQ_ID_BEST_CHAIN_FROM_DISK = 0;
40 : : static constexpr int32_t SEQ_ID_INIT_FROM_DISK = 1;
41 : :
42 : : /**
43 : : * Maximum gap between node time and block time used
44 : : * for the "Catching up..." mode in GUI.
45 : : *
46 : : * Ref: https://github.com/bitcoin/bitcoin/pull/1026
47 : : */
48 : : static constexpr int64_t MAX_BLOCK_TIME_GAP = 90 * 60;
49 : :
50 : : class CBlockFileInfo
51 : : {
52 : : public:
53 : : unsigned int nBlocks{}; //!< number of blocks stored in file
54 : : unsigned int nSize{}; //!< number of used bytes of block file
55 : : unsigned int nUndoSize{}; //!< number of used bytes in the undo file
56 : : unsigned int nHeightFirst{}; //!< lowest height of block in file
57 : : unsigned int nHeightLast{}; //!< highest height of block in file
58 : : uint64_t nTimeFirst{}; //!< earliest time of block in file
59 : : uint64_t nTimeLast{}; //!< latest time of block in file
60 : :
61 : 230718 : SERIALIZE_METHODS(CBlockFileInfo, obj)
62 : : {
63 : 132671 : READWRITE(VARINT(obj.nBlocks));
64 : 132658 : READWRITE(VARINT(obj.nSize));
65 : 132648 : READWRITE(VARINT(obj.nUndoSize));
66 : 132641 : READWRITE(VARINT(obj.nHeightFirst));
67 : 132639 : READWRITE(VARINT(obj.nHeightLast));
68 : 132639 : READWRITE(VARINT(obj.nTimeFirst));
69 : 132632 : READWRITE(VARINT(obj.nTimeLast));
70 : 132626 : }
71 : :
72 : 1688 : CBlockFileInfo() = default;
73 : :
74 : : std::string ToString() const;
75 : :
76 : : /** update statistics (does not update nSize) */
77 : 112285 : void AddBlock(unsigned int nHeightIn, uint64_t nTimeIn)
78 : : {
79 [ + + - + ]: 112285 : if (nBlocks == 0 || nHeightFirst > nHeightIn)
80 : 1688 : nHeightFirst = nHeightIn;
81 [ + + - + ]: 112285 : if (nBlocks == 0 || nTimeFirst > nTimeIn)
82 : 1688 : nTimeFirst = nTimeIn;
83 : 112285 : nBlocks++;
84 [ + + ]: 112285 : if (nHeightIn > nHeightLast)
85 : 101972 : nHeightLast = nHeightIn;
86 [ + + ]: 112285 : if (nTimeIn > nTimeLast)
87 : 21648 : nTimeLast = nTimeIn;
88 : 112285 : }
89 : : };
90 : :
91 : : enum BlockStatus : uint32_t {
92 : : //! Unused.
93 : : BLOCK_VALID_UNKNOWN = 0,
94 : :
95 : : //! Reserved (was BLOCK_VALID_HEADER).
96 : : BLOCK_VALID_RESERVED = 1,
97 : :
98 : : //! All parent headers found, difficulty matches, timestamp >= median previous. Implies all parents
99 : : //! are also at least TREE.
100 : : BLOCK_VALID_TREE = 2,
101 : :
102 : : /**
103 : : * Only first tx is coinbase, 2 <= coinbase input script length <= 100, transactions valid, no duplicate txids,
104 : : * sigops, size, merkle root. Implies all parents are at least TREE but not necessarily TRANSACTIONS.
105 : : *
106 : : * If a block's validity is at least VALID_TRANSACTIONS, CBlockIndex::nTx will be set. If a block and all previous
107 : : * blocks back to the genesis block or an assumeutxo snapshot block are at least VALID_TRANSACTIONS,
108 : : * CBlockIndex::m_chain_tx_count will be set.
109 : : */
110 : : BLOCK_VALID_TRANSACTIONS = 3,
111 : :
112 : : //! Outputs do not overspend inputs, no double spends, coinbase output ok, no immature coinbase spends, BIP30.
113 : : //! Implies all previous blocks back to the genesis block or an assumeutxo snapshot block are at least VALID_CHAIN.
114 : : BLOCK_VALID_CHAIN = 4,
115 : :
116 : : //! Scripts & signatures ok. Implies all previous blocks back to the genesis block or an assumeutxo snapshot block
117 : : //! are at least VALID_SCRIPTS.
118 : : BLOCK_VALID_SCRIPTS = 5,
119 : :
120 : : //! All validity bits.
121 : : BLOCK_VALID_MASK = BLOCK_VALID_RESERVED | BLOCK_VALID_TREE | BLOCK_VALID_TRANSACTIONS |
122 : : BLOCK_VALID_CHAIN | BLOCK_VALID_SCRIPTS,
123 : :
124 : : BLOCK_HAVE_DATA = 8, //!< full block available in blk*.dat
125 : : BLOCK_HAVE_UNDO = 16, //!< undo data available in rev*.dat
126 : : BLOCK_HAVE_MASK = BLOCK_HAVE_DATA | BLOCK_HAVE_UNDO,
127 : :
128 : : BLOCK_FAILED_VALID = 32, //!< stage after last reached validness failed
129 : : BLOCK_FAILED_CHILD = 64, //!< descends from failed block
130 : : BLOCK_FAILED_MASK = BLOCK_FAILED_VALID | BLOCK_FAILED_CHILD,
131 : :
132 : : BLOCK_OPT_WITNESS = 128, //!< block data in blk*.dat was received with a witness-enforcing client
133 : :
134 : : BLOCK_STATUS_RESERVED = 256, //!< Unused flag that was previously set on assumeutxo snapshot blocks and their
135 : : //!< ancestors before they were validated, and unset when they were validated.
136 : : };
137 : :
138 : : /** The block chain is a tree shaped structure starting with the
139 : : * genesis block at the root, with each block potentially having multiple
140 : : * candidates to be the next block. A blockindex may have multiple pprev pointing
141 : : * to it, but at most one of them can be part of the currently active branch.
142 : : */
143 : : class CBlockIndex
144 : : {
145 : : public:
146 : : //! pointer to the hash of the block, if any. Memory is owned by this CBlockIndex
147 : : const uint256* phashBlock{nullptr};
148 : :
149 : : //! pointer to the index of the predecessor of this block
150 : : CBlockIndex* pprev{nullptr};
151 : :
152 : : //! pointer to the index of some further predecessor of this block
153 : : CBlockIndex* pskip{nullptr};
154 : :
155 : : //! height of the entry in the chain. The genesis block has height 0
156 : : int nHeight{0};
157 : :
158 : : //! Which # file this block is stored in (blk?????.dat)
159 : : int nFile GUARDED_BY(::cs_main){0};
160 : :
161 : : //! Byte offset within blk?????.dat where this block's data is stored
162 : : unsigned int nDataPos GUARDED_BY(::cs_main){0};
163 : :
164 : : //! Byte offset within rev?????.dat where this block's undo data is stored
165 : : unsigned int nUndoPos GUARDED_BY(::cs_main){0};
166 : :
167 : : //! (memory only) Total amount of work (expected number of hashes) in the chain up to and including this block
168 : : arith_uint256 nChainWork{};
169 : :
170 : : //! Number of transactions in this block. This will be nonzero if the block
171 : : //! reached the VALID_TRANSACTIONS level, and zero otherwise.
172 : : //! Note: in a potential headers-first mode, this number cannot be relied upon
173 : : unsigned int nTx{0};
174 : :
175 : : //! (memory only) Number of transactions in the chain up to and including this block.
176 : : //! This value will be non-zero if this block and all previous blocks back
177 : : //! to the genesis block or an assumeutxo snapshot block have reached the
178 : : //! VALID_TRANSACTIONS level.
179 : : uint64_t m_chain_tx_count{0};
180 : :
181 : : //! Verification status of this block. See enum BlockStatus
182 : : //!
183 : : //! Note: this value is modified to show BLOCK_OPT_WITNESS during UTXO snapshot
184 : : //! load to avoid a spurious startup failure requiring -reindex.
185 : : //! @sa NeedsRedownload
186 : : //! @sa ActivateSnapshot
187 : : uint32_t nStatus GUARDED_BY(::cs_main){0};
188 : :
189 : : //! block header
190 : : int32_t nVersion{0};
191 : : uint256 hashMerkleRoot{};
192 : : uint32_t nTime{0};
193 : : uint32_t nBits{0};
194 : : uint32_t nNonce{0};
195 : :
196 : : //! (memory only) Sequential id assigned to distinguish order in which blocks are received.
197 : : //! Initialized to SEQ_ID_INIT_FROM_DISK{1} when loading blocks from disk, except for blocks
198 : : //! belonging to the best chain which overwrite it to SEQ_ID_BEST_CHAIN_FROM_DISK{0}.
199 : : int32_t nSequenceId{SEQ_ID_INIT_FROM_DISK};
200 : :
201 : : //! (memory only) Maximum nTime in the chain up to and including this block.
202 : : unsigned int nTimeMax{0};
203 : :
204 : 2866750 : explicit CBlockIndex(const CBlockHeader& block)
205 : 2866750 : : nVersion{block.nVersion},
206 : 2866750 : hashMerkleRoot{block.hashMerkleRoot},
207 : 2866750 : nTime{block.nTime},
208 : 2866750 : nBits{block.nBits},
209 : 2866750 : nNonce{block.nNonce}
210 : : {
211 : 2866750 : }
212 : :
213 : 6491 : FlatFilePos GetBlockPos() const EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
214 : : {
215 : 6491 : AssertLockHeld(::cs_main);
216 : 6491 : FlatFilePos ret;
217 [ + + ]: 6491 : if (nStatus & BLOCK_HAVE_DATA) {
218 : 6304 : ret.nFile = nFile;
219 : 6304 : ret.nPos = nDataPos;
220 : : }
221 : 6491 : return ret;
222 : : }
223 : :
224 : 101783 : FlatFilePos GetUndoPos() const EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
225 : : {
226 : 101783 : AssertLockHeld(::cs_main);
227 : 101783 : FlatFilePos ret;
228 [ + + ]: 101783 : if (nStatus & BLOCK_HAVE_UNDO) {
229 : 13 : ret.nFile = nFile;
230 : 13 : ret.nPos = nUndoPos;
231 : : }
232 : 101783 : return ret;
233 : : }
234 : :
235 : 89312 : CBlockHeader GetBlockHeader() const
236 : : {
237 : 89312 : CBlockHeader block;
238 : 89312 : block.nVersion = nVersion;
239 [ + + ]: 89312 : if (pprev)
240 : 88289 : block.hashPrevBlock = pprev->GetBlockHash();
241 : 89312 : block.hashMerkleRoot = hashMerkleRoot;
242 : 89312 : block.nTime = nTime;
243 : 89312 : block.nBits = nBits;
244 : 89312 : block.nNonce = nNonce;
245 : 89312 : return block;
246 : : }
247 : :
248 : 6030100 : uint256 GetBlockHash() const
249 : : {
250 [ - + ]: 6030100 : assert(phashBlock != nullptr);
251 : 6030100 : return *phashBlock;
252 : : }
253 : :
254 : : /**
255 : : * Check whether this block and all previous blocks back to the genesis block or an assumeutxo snapshot block have
256 : : * reached VALID_TRANSACTIONS and had transactions downloaded (and stored to disk) at some point.
257 : : *
258 : : * Does not imply the transactions are consensus-valid (ConnectTip might fail)
259 : : * Does not imply the transactions are still stored on disk. (IsBlockPruned might return true)
260 : : *
261 : : * Note that this will be true for the snapshot base block, if one is loaded, since its m_chain_tx_count value will have
262 : : * been set manually based on the related AssumeutxoData entry.
263 : : */
264 [ + - ][ + + : 97588390 : bool HaveNumChainTxs() const { return m_chain_tx_count != 0; }
- + - - +
- - - - -
- - + - -
+ ][ + - -
- # # # #
# # # # #
# # # #
# ]
265 : :
266 : 714581 : NodeSeconds Time() const
267 : : {
268 [ + + ]: 714581 : return NodeSeconds{std::chrono::seconds{nTime}};
269 : : }
270 : :
271 : 34649952 : int64_t GetBlockTime() const
272 : : {
273 [ + + + + ]: 34511077 : return (int64_t)nTime;
[ + - # # ]
[ + + - -
+ - ]
274 : : }
275 : :
276 : 0 : int64_t GetBlockTimeMax() const
277 : : {
278 [ # # ]: 0 : return (int64_t)nTimeMax;
279 : : }
280 : :
281 : : static constexpr int nMedianTimeSpan = 11;
282 : :
283 : 3132029 : int64_t GetMedianTimePast() const
284 : : {
285 : 3132029 : int64_t pmedian[nMedianTimeSpan];
286 : 3132029 : int64_t* pbegin = &pmedian[nMedianTimeSpan];
287 : 3132029 : int64_t* pend = &pmedian[nMedianTimeSpan];
288 : :
289 : 3132029 : const CBlockIndex* pindex = this;
290 [ + + ]: 36384653 : for (int i = 0; i < nMedianTimeSpan && pindex; i++, pindex = pindex->pprev)
291 : 33252624 : *(--pbegin) = pindex->GetBlockTime();
292 : :
293 : 3132029 : std::sort(pbegin, pend);
294 : 3132029 : return pbegin[(pend - pbegin) / 2];
295 : : }
296 : :
297 : : std::string ToString() const;
298 : :
299 : : //! Check whether this block index entry is valid up to the passed validity level.
300 : 110410 : bool IsValid(enum BlockStatus nUpTo) const
301 : : EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
302 : : {
303 : 110410 : AssertLockHeld(::cs_main);
304 : 110410 : assert(!(nUpTo & ~BLOCK_VALID_MASK)); // Only validity flags allowed.
305 [ # # ][ - - : 110410 : if (nStatus & BLOCK_FAILED_MASK)
- - - - -
- - - - -
+ - + - ]
[ - - + -
- - + - +
- - - - -
# # ]
306 : : return false;
307 [ + - ]: 110410 : return ((nStatus & BLOCK_VALID_MASK) >= nUpTo);
[ # # # # ]
[ - - - -
- - - - -
- - - + -
+ - ][ - -
+ - - - +
+ - + - -
- - # # ]
308 : : }
309 : :
310 : : //! Raise the validity level of this block index entry.
311 : : //! Returns true if the validity was changed.
312 : 423728 : bool RaiseValidity(enum BlockStatus nUpTo) EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
313 : : {
314 : 423728 : AssertLockHeld(::cs_main);
315 [ - + ]: 423728 : assert(!(nUpTo & ~BLOCK_VALID_MASK)); // Only validity flags allowed.
316 [ + + ]: 423728 : if (nStatus & BLOCK_FAILED_MASK) return false;
317 : :
318 [ + + ]: 423476 : if ((nStatus & BLOCK_VALID_MASK) < nUpTo) {
319 : 423134 : nStatus = (nStatus & ~BLOCK_VALID_MASK) | nUpTo;
320 : 423134 : return true;
321 : : }
322 : : return false;
323 : : }
324 : :
325 : : //! Build the skiplist pointer for this entry.
326 : : void BuildSkip();
327 : :
328 : : //! Efficiently find an ancestor of this block.
329 : : CBlockIndex* GetAncestor(int height);
330 : : const CBlockIndex* GetAncestor(int height) const;
331 : :
332 : 741 : CBlockIndex() = default;
333 : : ~CBlockIndex() = default;
334 : :
335 : : protected:
336 : : //! CBlockIndex should not allow public copy construction because equality
337 : : //! comparison via pointer is very common throughout the codebase, making
338 : : //! use of copy a footgun. Also, use of copies do not have the benefit
339 : : //! of simplifying lifetime considerations due to attributes like pprev and
340 : : //! pskip, which are at risk of becoming dangling pointers in a copied
341 : : //! instance.
342 : : //!
343 : : //! We declare these protected instead of simply deleting them so that
344 : : //! CDiskBlockIndex can reuse copy construction.
345 : : CBlockIndex(const CBlockIndex&) = default;
346 : : CBlockIndex& operator=(const CBlockIndex&) = delete;
347 : : CBlockIndex(CBlockIndex&&) = delete;
348 : : CBlockIndex& operator=(CBlockIndex&&) = delete;
349 : : };
350 : :
351 : : arith_uint256 GetBlockProof(const CBlockIndex& block);
352 : : /** Return the time it would take to redo the work difference between from and to, assuming the current hashrate corresponds to the difficulty at tip, in seconds. */
353 : : int64_t GetBlockProofEquivalentTime(const CBlockIndex& to, const CBlockIndex& from, const CBlockIndex& tip, const Consensus::Params&);
354 : : /** Find the forking point between two chain tips. */
355 : : const CBlockIndex* LastCommonAncestor(const CBlockIndex* pa, const CBlockIndex* pb);
356 : :
357 : :
358 : : /** Used to marshal pointers into hashes for db storage. */
359 : : class CDiskBlockIndex : public CBlockIndex
360 : : {
361 : : /** Historically CBlockLocator's version field has been written to disk
362 : : * streams as the client version, but the value has never been used.
363 : : *
364 : : * Hard-code to the highest client version ever written.
365 : : * SerParams can be used if the field requires any meaning in the future.
366 : : **/
367 : : static constexpr int DUMMY_VERSION = 259900;
368 : :
369 : : public:
370 : : uint256 hashPrev;
371 : :
372 : 741 : CDiskBlockIndex()
373 : 741 : {
374 : 741 : hashPrev = uint256();
375 : 741 : }
376 : :
377 [ + + ]: 1547044 : explicit CDiskBlockIndex(const CBlockIndex* pindex) : CBlockIndex(*pindex)
378 : : {
379 [ + + ]: 1547044 : hashPrev = (pprev ? pprev->GetBlockHash() : uint256());
380 : 1547044 : }
381 : :
382 : 3094881 : SERIALIZE_METHODS(CDiskBlockIndex, obj)
383 : : {
384 : 1547811 : LOCK(::cs_main);
385 [ + + ]: 1547811 : int _nVersion = DUMMY_VERSION;
386 [ + + ]: 1547811 : READWRITE(VARINT_MODE(_nVersion, VarIntMode::NONNEGATIVE_SIGNED));
387 : :
388 [ + + ]: 1547803 : READWRITE(VARINT_MODE(obj.nHeight, VarIntMode::NONNEGATIVE_SIGNED));
389 [ + + ]: 1547795 : READWRITE(VARINT(obj.nStatus));
390 [ + + ]: 1547785 : READWRITE(VARINT(obj.nTx));
391 [ + + + + ]: 1547780 : if (obj.nStatus & (BLOCK_HAVE_DATA | BLOCK_HAVE_UNDO)) READWRITE(VARINT_MODE(obj.nFile, VarIntMode::NONNEGATIVE_SIGNED));
392 [ + + + + ]: 1547777 : if (obj.nStatus & BLOCK_HAVE_DATA) READWRITE(VARINT(obj.nDataPos));
393 [ + + + + ]: 1547769 : if (obj.nStatus & BLOCK_HAVE_UNDO) READWRITE(VARINT(obj.nUndoPos));
394 : :
395 : : // block header
396 [ + + ]: 1547764 : READWRITE(obj.nVersion);
397 [ + + ]: 1547758 : READWRITE(obj.hashPrev);
398 [ + - ]: 1547756 : READWRITE(obj.hashMerkleRoot);
399 [ + - ]: 1547756 : READWRITE(obj.nTime);
400 [ + - ]: 1547756 : READWRITE(obj.nBits);
401 [ + - + - ]: 3095512 : READWRITE(obj.nNonce);
402 : 1547756 : }
403 : :
404 : 660 : uint256 ConstructBlockHash() const
405 : : {
406 : 660 : CBlockHeader block;
407 : 660 : block.nVersion = nVersion;
408 : 660 : block.hashPrevBlock = hashPrev;
409 : 660 : block.hashMerkleRoot = hashMerkleRoot;
410 : 660 : block.nTime = nTime;
411 : 660 : block.nBits = nBits;
412 : 660 : block.nNonce = nNonce;
413 : 660 : return block.GetHash();
414 : : }
415 : :
416 : : uint256 GetBlockHash() = delete;
417 : : std::string ToString() = delete;
418 : : };
419 : :
420 : : /** An in-memory indexed chain of blocks. */
421 [ + - ]: 506636 : class CChain
422 : : {
423 : : private:
424 : : std::vector<CBlockIndex*> vChain;
425 : :
426 : : public:
427 : 3321 : CChain() = default;
428 : : CChain(const CChain&) = delete;
429 : : CChain& operator=(const CChain&) = delete;
430 : :
431 : : /** Returns the index entry for the genesis block of this chain, or nullptr if none. */
432 : 507300 : CBlockIndex* Genesis() const
433 : : {
434 [ # # # # ]: 507300 : return vChain.size() > 0 ? vChain[0] : nullptr;
[ - + + -
+ - - + -
+ + - ]
435 : : }
436 : :
437 : : /** Returns the index entry for the tip of this chain, or nullptr if none. */
438 : 56720813 : CBlockIndex* Tip() const
439 : : {
440 [ - + + - : 57496127 : return vChain.size() > 0 ? vChain[vChain.size() - 1] : nullptr;
# # # # ]
[ - + + -
- + + - ]
[ - + + -
+ - + - -
- - - - -
- - - - -
- - - - +
+ - - + +
- - + + -
- + + - -
+ + - - -
- + + - -
+ + - - +
+ - - + +
- - - -
- ][ - + +
- + - # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ][ # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ][ # # #
# # # # #
# # # # #
# # # # #
# # # # ]
[ - - - -
- - - - -
- - + + -
- + ][ - +
+ - - - -
- - - - -
- - - + +
- - + + +
+ - - + +
- + - + -
- - - - -
- - - - +
+ - - + -
+ - + + -
+ - - + -
- - - - -
- - - + +
- - - - -
- - - - -
+ + - - +
+ + + - -
- - - - -
- - - - -
- - - - -
- - - - -
+ + - - +
+ - - + +
- - + + +
- + + + -
+ + - - +
+ - + - +
- - + + +
- + + + +
- + - - +
+ - - + +
- - + + +
- - - - -
- - + + -
- + + - -
+ - + + -
- + + - -
+ + + + -
- + + - -
+ + - - +
+ - - + +
- - + + -
+ - - + +
- - - - -
- - - - -
- - - - -
- - - - -
- - + + -
- - - - -
- - - ][ -
- - - - -
- - - - -
- - + + -
- - - - -
+ + - - +
+ - - + +
- - + + -
- - - - -
+ + - - +
+ - + - -
+ + - - +
+ - - - -
- - + + -
- - - - -
- - - # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
441 : : }
442 : :
443 : : /** Returns the index entry at a particular height in this chain, or nullptr if no such height exists. */
444 : 132208777 : CBlockIndex* operator[](int nHeight) const
445 : : {
446 [ # # # # : 132208776 : if (nHeight < 0 || nHeight >= (int)vChain.size())
# # # # ]
[ - - - -
- - - - -
- - - - -
- - - - -
- - - + -
- - - - -
- - - ][ -
+ + + # #
# # ][ - -
- - - - +
- - + + -
- - - - -
- - - - -
- - - - -
- ][ + - +
- + - - +
+ + + - -
+ + - + -
- + + + -
- - - - -
- - ][ - -
- - - - -
- - - - -
- - - - -
- - + + +
- - - - #
# # # ]
447 : : return nullptr;
448 [ - - ]: 131544290 : return vChain[nHeight];
449 : : }
450 : :
451 : : /** Efficiently check whether a block is present in this chain. */
452 : 96984610 : bool Contains(const CBlockIndex* pindex) const
453 : : {
454 [ + - ]: 96984610 : return (*this)[pindex->nHeight] == pindex;
455 : : }
456 : :
457 : : /** Find the successor of a block in this chain, or nullptr if the given index is not found or is the tip. */
458 : 129052 : CBlockIndex* Next(const CBlockIndex* pindex) const
459 : : {
460 [ + - ]: 129052 : if (Contains(pindex))
461 [ + - ]: 129052 : return (*this)[pindex->nHeight + 1];
462 : : else
463 : : return nullptr;
464 : : }
465 : :
466 : : /** Return the maximal height in the chain. Is equal to chain.Tip() ? chain.Tip()->nHeight : -1. */
467 : 4012758 : int Height() const
468 : : {
469 [ + + # # : 3898716 : return int(vChain.size()) - 1;
# # # # ]
[ - + + -
- + + + ]
[ # # # #
# # ][ + -
- - - - -
+ + + - +
+ - - + +
+ ][ - + +
+ # # # #
# # # # #
# # # #
# ][ # # #
# # # # #
# # # # #
# ][ - - -
- - - - -
- - - - -
+ + + - +
- + - + -
+ - + - +
- - - - -
- - - - -
- - - - -
- - - - -
- - - + +
- - + + -
- - - + +
- - + - +
+ - - + +
- - + - +
- - - - ]
[ - + + +
- - - - -
- - - # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
470 : : }
471 : :
472 : : /** Set/initialize a chain with a given tip. */
473 : : void SetTip(CBlockIndex& block);
474 : :
475 : : /** Find the last common block between this chain and a block index entry. */
476 : : const CBlockIndex* FindFork(const CBlockIndex* pindex) const;
477 : :
478 : : /** Find the earliest block with timestamp equal or greater than the given time and height equal or greater than the given height. */
479 : : CBlockIndex* FindEarliestAtLeast(int64_t nTime, int height) const;
480 : : };
481 : :
482 : : /** Get a locator for a block index entry. */
483 : : CBlockLocator GetLocator(const CBlockIndex* index);
484 : :
485 : : /** Construct a list of hash entries to put in a locator. */
486 : : std::vector<uint256> LocatorEntries(const CBlockIndex* index);
487 : :
488 : : #endif // BITCOIN_CHAIN_H
|