LCOV - code coverage report
Current view: top level - src - chain.h (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 106 174 60.9 %
Date: 2023-11-12 01:39:15 Functions: 21 53 39.6 %
Branches: 29 142 20.4 %

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

Generated by: LCOV version 1.14