LCOV - code coverage report
Current view: top level - src - validation.h (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 30.3 % 89 27
Test Date: 2024-10-23 17:21:46 Functions: 21.7 % 23 5
Branches: 3.2 % 682 22

             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_VALIDATION_H
       7                 :             : #define BITCOIN_VALIDATION_H
       8                 :             : 
       9                 :             : #include <arith_uint256.h>
      10                 :             : #include <attributes.h>
      11                 :             : #include <chain.h>
      12                 :             : #include <checkqueue.h>
      13                 :             : #include <consensus/amount.h>
      14                 :             : #include <cuckoocache.h>
      15                 :             : #include <deploymentstatus.h>
      16                 :             : #include <kernel/chain.h>
      17                 :             : #include <kernel/chainparams.h>
      18                 :             : #include <kernel/chainstatemanager_opts.h>
      19                 :             : #include <kernel/cs_main.h> // IWYU pragma: export
      20                 :             : #include <node/blockstorage.h>
      21                 :             : #include <policy/feerate.h>
      22                 :             : #include <policy/packages.h>
      23                 :             : #include <policy/policy.h>
      24                 :             : #include <script/script_error.h>
      25                 :             : #include <script/sigcache.h>
      26                 :             : #include <sync.h>
      27                 :             : #include <txdb.h>
      28                 :             : #include <txmempool.h> // For CTxMemPool::cs
      29                 :             : #include <uint256.h>
      30                 :             : #include <util/check.h>
      31                 :             : #include <util/fs.h>
      32                 :             : #include <util/hasher.h>
      33                 :             : #include <util/result.h>
      34                 :             : #include <util/translation.h>
      35                 :             : #include <versionbits.h>
      36                 :             : 
      37                 :             : #include <atomic>
      38                 :             : #include <map>
      39                 :             : #include <memory>
      40                 :             : #include <optional>
      41                 :             : #include <set>
      42                 :             : #include <span>
      43                 :             : #include <stdint.h>
      44                 :             : #include <string>
      45                 :             : #include <thread>
      46                 :             : #include <type_traits>
      47                 :             : #include <utility>
      48                 :             : #include <vector>
      49                 :             : 
      50                 :             : class Chainstate;
      51                 :             : class CTxMemPool;
      52                 :             : class ChainstateManager;
      53                 :             : struct ChainTxData;
      54                 :             : class DisconnectedBlockTransactions;
      55                 :             : struct PrecomputedTransactionData;
      56                 :             : struct LockPoints;
      57                 :             : struct AssumeutxoData;
      58                 :             : namespace node {
      59                 :             : class SnapshotMetadata;
      60                 :             : } // namespace node
      61                 :             : namespace Consensus {
      62                 :             : struct Params;
      63                 :             : } // namespace Consensus
      64                 :             : namespace util {
      65                 :             : class SignalInterrupt;
      66                 :             : } // namespace util
      67                 :             : 
      68                 :             : /** Block files containing a block-height within MIN_BLOCKS_TO_KEEP of ActiveChain().Tip() will not be pruned. */
      69                 :             : static const unsigned int MIN_BLOCKS_TO_KEEP = 288;
      70                 :             : static const signed int DEFAULT_CHECKBLOCKS = 6;
      71                 :             : static constexpr int DEFAULT_CHECKLEVEL{3};
      72                 :             : // Require that user allocate at least 550 MiB for block & undo files (blk???.dat and rev???.dat)
      73                 :             : // At 1MB per block, 288 blocks = 288MB.
      74                 :             : // Add 15% for Undo data = 331MB
      75                 :             : // Add 20% for Orphan block rate = 397MB
      76                 :             : // We want the low water mark after pruning to be at least 397 MB and since we prune in
      77                 :             : // full block file chunks, we need the high water mark which triggers the prune to be
      78                 :             : // one 128MB block file + added 15% undo data = 147MB greater for a total of 545MB
      79                 :             : // Setting the target to >= 550 MiB will make it likely we can respect the target.
      80                 :             : static const uint64_t MIN_DISK_SPACE_FOR_BLOCK_FILES = 550 * 1024 * 1024;
      81                 :             : 
      82                 :             : /** Current sync state passed to tip changed callbacks. */
      83                 :             : enum class SynchronizationState {
      84                 :             :     INIT_REINDEX,
      85                 :             :     INIT_DOWNLOAD,
      86                 :             :     POST_INIT
      87                 :             : };
      88                 :             : 
      89                 :             : extern GlobalMutex g_best_block_mutex;
      90                 :             : extern std::condition_variable g_best_block_cv;
      91                 :             : /** Used to notify getblocktemplate RPC of new tips. */
      92                 :             : extern uint256 g_best_block;
      93                 :             : 
      94                 :             : /** Documentation for argument 'checklevel'. */
      95                 :             : extern const std::vector<std::string> CHECKLEVEL_DOC;
      96                 :             : 
      97                 :             : CAmount GetBlockSubsidy(int nHeight, const Consensus::Params& consensusParams);
      98                 :             : 
      99                 :             : bool FatalError(kernel::Notifications& notifications, BlockValidationState& state, const bilingual_str& message);
     100                 :             : 
     101                 :             : /** Guess verification progress (as a fraction between 0.0=genesis and 1.0=current tip). */
     102                 :             : double GuessVerificationProgress(const ChainTxData& data, const CBlockIndex* pindex);
     103                 :             : 
     104                 :             : /** Prune block files up to a given height */
     105                 :             : void PruneBlockFilesManual(Chainstate& active_chainstate, int nManualPruneHeight);
     106                 :             : 
     107                 :             : /**
     108                 :             : * Validation result for a transaction evaluated by MemPoolAccept (single or package).
     109                 :             : * Here are the expected fields and properties of a result depending on its ResultType, applicable to
     110                 :             : * results returned from package evaluation:
     111                 :             : *+---------------------------+----------------+-------------------+------------------+----------------+-------------------+
     112                 :             : *| Field or property         |    VALID       |                 INVALID              |  MEMPOOL_ENTRY | DIFFERENT_WITNESS |
     113                 :             : *|                           |                |--------------------------------------|                |                   |
     114                 :             : *|                           |                | TX_RECONSIDERABLE |     Other        |                |                   |
     115                 :             : *+---------------------------+----------------+-------------------+------------------+----------------+-------------------+
     116                 :             : *| txid in mempool?          | yes            | no                | no*              | yes            | yes               |
     117                 :             : *| wtxid in mempool?         | yes            | no                | no*              | yes            | no                |
     118                 :             : *| m_state                   | yes, IsValid() | yes, IsInvalid()  | yes, IsInvalid() | yes, IsValid() | yes, IsValid()    |
     119                 :             : *| m_vsize                   | yes            | no                | no               | yes            | no                |
     120                 :             : *| m_base_fees               | yes            | no                | no               | yes            | no                |
     121                 :             : *| m_effective_feerate       | yes            | yes               | no               | no             | no                |
     122                 :             : *| m_wtxids_fee_calculations | yes            | yes               | no               | no             | no                |
     123                 :             : *| m_other_wtxid             | no             | no                | no               | no             | yes               |
     124                 :             : *+---------------------------+----------------+-------------------+------------------+----------------+-------------------+
     125                 :             : * (*) Individual transaction acceptance doesn't return MEMPOOL_ENTRY and DIFFERENT_WITNESS. It returns
     126                 :             : * INVALID, with the errors txn-already-in-mempool and txn-same-nonwitness-data-in-mempool
     127                 :             : * respectively. In those cases, the txid or wtxid may be in the mempool for a TX_CONFLICT.
     128                 :             : */
     129                 :             : struct MempoolAcceptResult {
     130                 :             :     /** Used to indicate the results of mempool validation. */
     131                 :             :     enum class ResultType {
     132                 :             :         VALID, //!> Fully validated, valid.
     133                 :             :         INVALID, //!> Invalid.
     134                 :             :         MEMPOOL_ENTRY, //!> Valid, transaction was already in the mempool.
     135                 :             :         DIFFERENT_WITNESS, //!> Not validated. A same-txid-different-witness tx (see m_other_wtxid) already exists in the mempool and was not replaced.
     136                 :             :     };
     137                 :             :     /** Result type. Present in all MempoolAcceptResults. */
     138                 :             :     const ResultType m_result_type;
     139                 :             : 
     140                 :             :     /** Contains information about why the transaction failed. */
     141                 :             :     const TxValidationState m_state;
     142                 :             : 
     143                 :             :     /** Mempool transactions replaced by the tx. */
     144                 :             :     const std::list<CTransactionRef> m_replaced_transactions;
     145                 :             :     /** Virtual size as used by the mempool, calculated using serialized size and sigops. */
     146                 :             :     const std::optional<int64_t> m_vsize;
     147                 :             :     /** Raw base fees in satoshis. */
     148                 :             :     const std::optional<CAmount> m_base_fees;
     149                 :             :     /** The feerate at which this transaction was considered. This includes any fee delta added
     150                 :             :      * using prioritisetransaction (i.e. modified fees). If this transaction was submitted as a
     151                 :             :      * package, this is the package feerate, which may also include its descendants and/or
     152                 :             :      * ancestors (see m_wtxids_fee_calculations below).
     153                 :             :      */
     154                 :             :     const std::optional<CFeeRate> m_effective_feerate;
     155                 :             :     /** Contains the wtxids of the transactions used for fee-related checks. Includes this
     156                 :             :      * transaction's wtxid and may include others if this transaction was validated as part of a
     157                 :             :      * package. This is not necessarily equivalent to the list of transactions passed to
     158                 :             :      * ProcessNewPackage().
     159                 :             :      * Only present when m_result_type = ResultType::VALID. */
     160                 :             :     const std::optional<std::vector<Wtxid>> m_wtxids_fee_calculations;
     161                 :             : 
     162                 :             :     /** The wtxid of the transaction in the mempool which has the same txid but different witness. */
     163                 :             :     const std::optional<uint256> m_other_wtxid;
     164                 :             : 
     165                 :           0 :     static MempoolAcceptResult Failure(TxValidationState state) {
     166         [ #  # ]:           0 :         return MempoolAcceptResult(state);
     167                 :             :     }
     168                 :             : 
     169                 :           0 :     static MempoolAcceptResult FeeFailure(TxValidationState state,
     170                 :             :                                           CFeeRate effective_feerate,
     171                 :             :                                           const std::vector<Wtxid>& wtxids_fee_calculations) {
     172         [ #  # ]:           0 :         return MempoolAcceptResult(state, effective_feerate, wtxids_fee_calculations);
     173                 :             :     }
     174                 :             : 
     175                 :           0 :     static MempoolAcceptResult Success(std::list<CTransactionRef>&& replaced_txns,
     176                 :             :                                        int64_t vsize,
     177                 :             :                                        CAmount fees,
     178                 :             :                                        CFeeRate effective_feerate,
     179                 :             :                                        const std::vector<Wtxid>& wtxids_fee_calculations) {
     180                 :           0 :         return MempoolAcceptResult(std::move(replaced_txns), vsize, fees,
     181   [ #  #  #  #  :           0 :                                    effective_feerate, wtxids_fee_calculations);
             #  #  #  # ]
     182                 :             :     }
     183                 :             : 
     184                 :           0 :     static MempoolAcceptResult MempoolTx(int64_t vsize, CAmount fees) {
     185                 :           0 :         return MempoolAcceptResult(vsize, fees);
     186                 :             :     }
     187                 :             : 
     188                 :           0 :     static MempoolAcceptResult MempoolTxDifferentWitness(const uint256& other_wtxid) {
     189                 :           0 :         return MempoolAcceptResult(other_wtxid);
     190                 :             :     }
     191                 :             : 
     192                 :             : // Private constructors. Use static methods MempoolAcceptResult::Success, etc. to construct.
     193                 :             : private:
     194                 :             :     /** Constructor for failure case */
     195                 :           0 :     explicit MempoolAcceptResult(TxValidationState state)
     196         [ #  # ]:           0 :         : m_result_type(ResultType::INVALID), m_state(state) {
     197         [ #  # ]:           0 :             Assume(!state.IsValid()); // Can be invalid or error
     198                 :           0 :         }
     199                 :             : 
     200                 :             :     /** Constructor for success case */
     201                 :           0 :     explicit MempoolAcceptResult(std::list<CTransactionRef>&& replaced_txns,
     202                 :             :                                  int64_t vsize,
     203                 :             :                                  CAmount fees,
     204                 :             :                                  CFeeRate effective_feerate,
     205                 :             :                                  const std::vector<Wtxid>& wtxids_fee_calculations)
     206                 :           0 :         : m_result_type(ResultType::VALID),
     207                 :           0 :         m_replaced_transactions(std::move(replaced_txns)),
     208         [ #  # ]:           0 :         m_vsize{vsize},
     209                 :           0 :         m_base_fees(fees),
     210                 :           0 :         m_effective_feerate(effective_feerate),
     211         [ #  # ]:           0 :         m_wtxids_fee_calculations(wtxids_fee_calculations) {}
     212                 :             : 
     213                 :             :     /** Constructor for fee-related failure case */
     214                 :           0 :     explicit MempoolAcceptResult(TxValidationState state,
     215                 :             :                                  CFeeRate effective_feerate,
     216                 :             :                                  const std::vector<Wtxid>& wtxids_fee_calculations)
     217                 :           0 :         : m_result_type(ResultType::INVALID),
     218                 :           0 :         m_state(state),
     219         [ #  # ]:           0 :         m_effective_feerate(effective_feerate),
     220         [ #  # ]:           0 :         m_wtxids_fee_calculations(wtxids_fee_calculations) {}
     221                 :             : 
     222                 :             :     /** Constructor for already-in-mempool case. It wouldn't replace any transactions. */
     223                 :           0 :     explicit MempoolAcceptResult(int64_t vsize, CAmount fees)
     224         [ #  # ]:           0 :         : m_result_type(ResultType::MEMPOOL_ENTRY), m_vsize{vsize}, m_base_fees(fees) {}
     225                 :             : 
     226                 :             :     /** Constructor for witness-swapped case. */
     227                 :           0 :     explicit MempoolAcceptResult(const uint256& other_wtxid)
     228                 :           0 :         : m_result_type(ResultType::DIFFERENT_WITNESS), m_other_wtxid(other_wtxid) {}
     229                 :             : };
     230                 :             : 
     231                 :             : /**
     232                 :             : * Validation result for package mempool acceptance.
     233                 :             : */
     234                 :             : struct PackageMempoolAcceptResult
     235                 :             : {
     236                 :             :     PackageValidationState m_state;
     237                 :             :     /**
     238                 :             :     * Map from wtxid to finished MempoolAcceptResults. The client is responsible
     239                 :             :     * for keeping track of the transaction objects themselves. If a result is not
     240                 :             :     * present, it means validation was unfinished for that transaction. If there
     241                 :             :     * was a package-wide error (see result in m_state), m_tx_results will be empty.
     242                 :             :     */
     243                 :             :     std::map<uint256, MempoolAcceptResult> m_tx_results;
     244                 :             : 
     245                 :           0 :     explicit PackageMempoolAcceptResult(PackageValidationState state,
     246                 :             :                                         std::map<uint256, MempoolAcceptResult>&& results)
     247   [ #  #  #  #  :           0 :         : m_state{state}, m_tx_results(std::move(results)) {}
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
     248                 :             : 
     249                 :             :     explicit PackageMempoolAcceptResult(PackageValidationState state, CFeeRate feerate,
     250                 :             :                                         std::map<uint256, MempoolAcceptResult>&& results)
     251                 :             :         : m_state{state}, m_tx_results(std::move(results)) {}
     252                 :             : 
     253                 :             :     /** Constructor to create a PackageMempoolAcceptResult from a single MempoolAcceptResult */
     254                 :           0 :     explicit PackageMempoolAcceptResult(const uint256& wtxid, const MempoolAcceptResult& result)
     255   [ #  #  #  #  :           0 :         : m_tx_results{ {wtxid, result} } {}
             #  #  #  # ]
     256                 :             : };
     257                 :             : 
     258                 :             : /**
     259                 :             :  * Try to add a transaction to the mempool. This is an internal function and is exposed only for testing.
     260                 :             :  * Client code should use ChainstateManager::ProcessTransaction()
     261                 :             :  *
     262                 :             :  * @param[in]  active_chainstate  Reference to the active chainstate.
     263                 :             :  * @param[in]  tx                 The transaction to submit for mempool acceptance.
     264                 :             :  * @param[in]  accept_time        The timestamp for adding the transaction to the mempool.
     265                 :             :  *                                It is also used to determine when the entry expires.
     266                 :             :  * @param[in]  bypass_limits      When true, don't enforce mempool fee and capacity limits,
     267                 :             :  *                                and set entry_sequence to zero.
     268                 :             :  * @param[in]  test_accept        When true, run validation checks but don't submit to mempool.
     269                 :             :  *
     270                 :             :  * @returns a MempoolAcceptResult indicating whether the transaction was accepted/rejected with reason.
     271                 :             :  */
     272                 :             : MempoolAcceptResult AcceptToMemoryPool(Chainstate& active_chainstate, const CTransactionRef& tx,
     273                 :             :                                        int64_t accept_time, bool bypass_limits, bool test_accept)
     274                 :             :     EXCLUSIVE_LOCKS_REQUIRED(cs_main);
     275                 :             : 
     276                 :             : /**
     277                 :             : * Validate (and maybe submit) a package to the mempool. See doc/policy/packages.md for full details
     278                 :             : * on package validation rules.
     279                 :             : * @param[in]    test_accept         When true, run validation checks but don't submit to mempool.
     280                 :             : * @param[in]    client_maxfeerate    If exceeded by an individual transaction, rest of (sub)package evaluation is aborted.
     281                 :             : *                                   Only for sanity checks against local submission of transactions.
     282                 :             : * @returns a PackageMempoolAcceptResult which includes a MempoolAcceptResult for each transaction.
     283                 :             : * If a transaction fails, validation will exit early and some results may be missing. It is also
     284                 :             : * possible for the package to be partially submitted.
     285                 :             : */
     286                 :             : PackageMempoolAcceptResult ProcessNewPackage(Chainstate& active_chainstate, CTxMemPool& pool,
     287                 :             :                                                    const Package& txns, bool test_accept, const std::optional<CFeeRate>& client_maxfeerate)
     288                 :             :                                                    EXCLUSIVE_LOCKS_REQUIRED(cs_main);
     289                 :             : 
     290                 :             : /* Mempool validation helper functions */
     291                 :             : 
     292                 :             : /**
     293                 :             :  * Check if transaction will be final in the next block to be created.
     294                 :             :  */
     295                 :             : bool CheckFinalTxAtTip(const CBlockIndex& active_chain_tip, const CTransaction& tx) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
     296                 :             : 
     297                 :             : /**
     298                 :             :  * Calculate LockPoints required to check if transaction will be BIP68 final in the next block
     299                 :             :  * to be created on top of tip.
     300                 :             :  *
     301                 :             :  * @param[in]   tip             Chain tip for which tx sequence locks are calculated. For
     302                 :             :  *                              example, the tip of the current active chain.
     303                 :             :  * @param[in]   coins_view      Any CCoinsView that provides access to the relevant coins for
     304                 :             :  *                              checking sequence locks. For example, it can be a CCoinsViewCache
     305                 :             :  *                              that isn't connected to anything but contains all the relevant
     306                 :             :  *                              coins, or a CCoinsViewMemPool that is connected to the
     307                 :             :  *                              mempool and chainstate UTXO set. In the latter case, the caller
     308                 :             :  *                              is responsible for holding the appropriate locks to ensure that
     309                 :             :  *                              calls to GetCoin() return correct coins.
     310                 :             :  * @param[in]   tx              The transaction being evaluated.
     311                 :             :  *
     312                 :             :  * @returns The resulting height and time calculated and the hash of the block needed for
     313                 :             :  *          calculation, or std::nullopt if there is an error.
     314                 :             :  */
     315                 :             : std::optional<LockPoints> CalculateLockPointsAtTip(
     316                 :             :     CBlockIndex* tip,
     317                 :             :     const CCoinsView& coins_view,
     318                 :             :     const CTransaction& tx);
     319                 :             : 
     320                 :             : /**
     321                 :             :  * Check if transaction will be BIP68 final in the next block to be created on top of tip.
     322                 :             :  * @param[in]   tip             Chain tip to check tx sequence locks against. For example,
     323                 :             :  *                              the tip of the current active chain.
     324                 :             :  * @param[in]   lock_points     LockPoints containing the height and time at which this
     325                 :             :  *                              transaction is final.
     326                 :             :  * Simulates calling SequenceLocks() with data from the tip passed in.
     327                 :             :  * The LockPoints should not be considered valid if CheckSequenceLocksAtTip returns false.
     328                 :             :  */
     329                 :             : bool CheckSequenceLocksAtTip(CBlockIndex* tip,
     330                 :             :                              const LockPoints& lock_points);
     331                 :             : 
     332                 :             : /**
     333                 :             :  * Closure representing one script verification
     334                 :             :  * Note that this stores references to the spending transaction
     335                 :             :  */
     336                 :           0 : class CScriptCheck
     337                 :             : {
     338                 :             : private:
     339                 :             :     CTxOut m_tx_out;
     340                 :             :     const CTransaction *ptxTo;
     341                 :             :     unsigned int nIn;
     342                 :             :     unsigned int nFlags;
     343                 :             :     bool cacheStore;
     344                 :             :     ScriptError error{SCRIPT_ERR_UNKNOWN_ERROR};
     345                 :             :     PrecomputedTransactionData *txdata;
     346                 :             :     SignatureCache* m_signature_cache;
     347                 :             : 
     348                 :             : public:
     349                 :           0 :     CScriptCheck(const CTxOut& outIn, const CTransaction& txToIn, SignatureCache& signature_cache, unsigned int nInIn, unsigned int nFlagsIn, bool cacheIn, PrecomputedTransactionData* txdataIn) :
     350   [ #  #  #  # ]:           0 :         m_tx_out(outIn), ptxTo(&txToIn), nIn(nInIn), nFlags(nFlagsIn), cacheStore(cacheIn), txdata(txdataIn), m_signature_cache(&signature_cache) { }
     351                 :             : 
     352                 :             :     CScriptCheck(const CScriptCheck&) = delete;
     353                 :             :     CScriptCheck& operator=(const CScriptCheck&) = delete;
     354                 :           0 :     CScriptCheck(CScriptCheck&&) = default;
     355                 :           0 :     CScriptCheck& operator=(CScriptCheck&&) = default;
     356                 :             : 
     357                 :             :     bool operator()();
     358                 :             : 
     359   [ #  #  #  # ]:           0 :     ScriptError GetScriptError() const { return error; }
     360                 :             : };
     361                 :             : 
     362                 :             : // CScriptCheck is used a lot in std::vector, make sure that's efficient
     363                 :             : static_assert(std::is_nothrow_move_assignable_v<CScriptCheck>);
     364                 :             : static_assert(std::is_nothrow_move_constructible_v<CScriptCheck>);
     365                 :             : static_assert(std::is_nothrow_destructible_v<CScriptCheck>);
     366                 :             : 
     367                 :             : /**
     368                 :             :  * Convenience class for initializing and passing the script execution cache
     369                 :             :  * and signature cache.
     370                 :             :  */
     371                 :           1 : class ValidationCache
     372                 :             : {
     373                 :             : private:
     374                 :             :     //! Pre-initialized hasher to avoid having to recreate it for every hash calculation.
     375                 :             :     CSHA256 m_script_execution_cache_hasher;
     376                 :             : 
     377                 :             : public:
     378                 :             :     CuckooCache::cache<uint256, SignatureCacheHasher> m_script_execution_cache;
     379                 :             :     SignatureCache m_signature_cache;
     380                 :             : 
     381                 :             :     ValidationCache(size_t script_execution_cache_bytes, size_t signature_cache_bytes);
     382                 :             : 
     383                 :             :     ValidationCache(const ValidationCache&) = delete;
     384                 :             :     ValidationCache& operator=(const ValidationCache&) = delete;
     385                 :             : 
     386                 :             :     //! Return a copy of the pre-initialized hasher.
     387                 :           0 :     CSHA256 ScriptExecutionCacheHasher() const { return m_script_execution_cache_hasher; }
     388                 :             : };
     389                 :             : 
     390                 :             : /** Functions for validating blocks and updating the block tree */
     391                 :             : 
     392                 :             : /** Context-independent validity checks */
     393                 :             : bool CheckBlock(const CBlock& block, BlockValidationState& state, const Consensus::Params& consensusParams, bool fCheckPOW = true, bool fCheckMerkleRoot = true);
     394                 :             : 
     395                 :             : /** Check a block is completely valid from start to finish (only works on top of our current best block) */
     396                 :             : bool TestBlockValidity(BlockValidationState& state,
     397                 :             :                        const CChainParams& chainparams,
     398                 :             :                        Chainstate& chainstate,
     399                 :             :                        const CBlock& block,
     400                 :             :                        CBlockIndex* pindexPrev,
     401                 :             :                        bool fCheckPOW = true,
     402                 :             :                        bool fCheckMerkleRoot = true) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
     403                 :             : 
     404                 :             : /** Check with the proof of work on each blockheader matches the value in nBits */
     405                 :             : bool HasValidProofOfWork(const std::vector<CBlockHeader>& headers, const Consensus::Params& consensusParams);
     406                 :             : 
     407                 :             : /** Check if a block has been mutated (with respect to its merkle root and witness commitments). */
     408                 :             : bool IsBlockMutated(const CBlock& block, bool check_witness_root);
     409                 :             : 
     410                 :             : /** Return the sum of the claimed work on a given set of headers. No verification of PoW is done. */
     411                 :             : arith_uint256 CalculateClaimedHeadersWork(std::span<const CBlockHeader> headers);
     412                 :             : 
     413                 :             : enum class VerifyDBResult {
     414                 :             :     SUCCESS,
     415                 :             :     CORRUPTED_BLOCK_DB,
     416                 :             :     INTERRUPTED,
     417                 :             :     SKIPPED_L3_CHECKS,
     418                 :             :     SKIPPED_MISSING_BLOCKS,
     419                 :             : };
     420                 :             : 
     421                 :             : /** RAII wrapper for VerifyDB: Verify consistency of the block and coin databases */
     422                 :             : class CVerifyDB
     423                 :             : {
     424                 :             : private:
     425                 :             :     kernel::Notifications& m_notifications;
     426                 :             : 
     427                 :             : public:
     428                 :             :     explicit CVerifyDB(kernel::Notifications& notifications);
     429                 :             :     ~CVerifyDB();
     430                 :             :     [[nodiscard]] VerifyDBResult VerifyDB(
     431                 :             :         Chainstate& chainstate,
     432                 :             :         const Consensus::Params& consensus_params,
     433                 :             :         CCoinsView& coinsview,
     434                 :             :         int nCheckLevel,
     435                 :             :         int nCheckDepth) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
     436                 :             : };
     437                 :             : 
     438                 :             : enum DisconnectResult
     439                 :             : {
     440                 :             :     DISCONNECT_OK,      // All good.
     441                 :             :     DISCONNECT_UNCLEAN, // Rolled back, but UTXO set was inconsistent with block.
     442                 :             :     DISCONNECT_FAILED   // Something else went wrong.
     443                 :             : };
     444                 :             : 
     445                 :             : class ConnectTrace;
     446                 :             : 
     447                 :             : /** @see Chainstate::FlushStateToDisk */
     448                 :             : enum class FlushStateMode {
     449                 :             :     NONE,
     450                 :             :     IF_NEEDED,
     451                 :             :     PERIODIC,
     452                 :             :     ALWAYS
     453                 :             : };
     454                 :             : 
     455                 :             : /**
     456                 :             :  * A convenience class for constructing the CCoinsView* hierarchy used
     457                 :             :  * to facilitate access to the UTXO set.
     458                 :             :  *
     459                 :             :  * This class consists of an arrangement of layered CCoinsView objects,
     460                 :             :  * preferring to store and retrieve coins in memory via `m_cacheview` but
     461                 :             :  * ultimately falling back on cache misses to the canonical store of UTXOs on
     462                 :             :  * disk, `m_dbview`.
     463                 :             :  */
     464                 :             : class CoinsViews {
     465                 :             : 
     466                 :             : public:
     467                 :             :     //! The lowest level of the CoinsViews cache hierarchy sits in a leveldb database on disk.
     468                 :             :     //! All unspent coins reside in this store.
     469                 :             :     CCoinsViewDB m_dbview GUARDED_BY(cs_main);
     470                 :             : 
     471                 :             :     //! This view wraps access to the leveldb instance and handles read errors gracefully.
     472                 :             :     CCoinsViewErrorCatcher m_catcherview GUARDED_BY(cs_main);
     473                 :             : 
     474                 :             :     //! This is the top layer of the cache hierarchy - it keeps as many coins in memory as
     475                 :             :     //! can fit per the dbcache setting.
     476                 :             :     std::unique_ptr<CCoinsViewCache> m_cacheview GUARDED_BY(cs_main);
     477                 :             : 
     478                 :             :     //! This constructor initializes CCoinsViewDB and CCoinsViewErrorCatcher instances, but it
     479                 :             :     //! *does not* create a CCoinsViewCache instance by default. This is done separately because the
     480                 :             :     //! presence of the cache has implications on whether or not we're allowed to flush the cache's
     481                 :             :     //! state to disk, which should not be done until the health of the database is verified.
     482                 :             :     //!
     483                 :             :     //! All arguments forwarded onto CCoinsViewDB.
     484                 :             :     CoinsViews(DBParams db_params, CoinsViewOptions options);
     485                 :             : 
     486                 :             :     //! Initialize the CCoinsViewCache member.
     487                 :             :     void InitCache() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
     488                 :             : };
     489                 :             : 
     490                 :             : enum class CoinsCacheSizeState
     491                 :             : {
     492                 :             :     //! The coins cache is in immediate need of a flush.
     493                 :             :     CRITICAL = 2,
     494                 :             :     //! The cache is at >= 90% capacity.
     495                 :             :     LARGE = 1,
     496                 :             :     OK = 0
     497                 :             : };
     498                 :             : 
     499                 :             : /**
     500                 :             :  * Chainstate stores and provides an API to update our local knowledge of the
     501                 :             :  * current best chain.
     502                 :             :  *
     503                 :             :  * Eventually, the API here is targeted at being exposed externally as a
     504                 :             :  * consumable library, so any functions added must only call
     505                 :             :  * other class member functions, pure functions in other parts of the consensus
     506                 :             :  * library, callbacks via the validation interface, or read/write-to-disk
     507                 :             :  * functions (eventually this will also be via callbacks).
     508                 :             :  *
     509                 :             :  * Anything that is contingent on the current tip of the chain is stored here,
     510                 :             :  * whereas block information and metadata independent of the current tip is
     511                 :             :  * kept in `BlockManager`.
     512                 :             :  */
     513                 :             : class Chainstate
     514                 :             : {
     515                 :             : protected:
     516                 :             :     /**
     517                 :             :      * The ChainState Mutex
     518                 :             :      * A lock that must be held when modifying this ChainState - held in ActivateBestChain() and
     519                 :             :      * InvalidateBlock()
     520                 :             :      */
     521                 :             :     Mutex m_chainstate_mutex;
     522                 :             : 
     523                 :             :     //! Optional mempool that is kept in sync with the chain.
     524                 :             :     //! Only the active chainstate has a mempool.
     525                 :             :     CTxMemPool* m_mempool;
     526                 :             : 
     527                 :             :     //! Manages the UTXO set, which is a reflection of the contents of `m_chain`.
     528                 :             :     std::unique_ptr<CoinsViews> m_coins_views;
     529                 :             : 
     530                 :             :     //! This toggle exists for use when doing background validation for UTXO
     531                 :             :     //! snapshots.
     532                 :             :     //!
     533                 :             :     //! In the expected case, it is set once the background validation chain reaches the
     534                 :             :     //! same height as the base of the snapshot and its UTXO set is found to hash to
     535                 :             :     //! the expected assumeutxo value. It signals that we should no longer connect
     536                 :             :     //! blocks to the background chainstate. When set on the background validation
     537                 :             :     //! chainstate, it signifies that we have fully validated the snapshot chainstate.
     538                 :             :     //!
     539                 :             :     //! In the unlikely case that the snapshot chainstate is found to be invalid, this
     540                 :             :     //! is set to true on the snapshot chainstate.
     541                 :             :     bool m_disabled GUARDED_BY(::cs_main) {false};
     542                 :             : 
     543                 :             :     //! Cached result of LookupBlockIndex(*m_from_snapshot_blockhash)
     544                 :             :     const CBlockIndex* m_cached_snapshot_base GUARDED_BY(::cs_main) {nullptr};
     545                 :             : 
     546                 :             : public:
     547                 :             :     //! Reference to a BlockManager instance which itself is shared across all
     548                 :             :     //! Chainstate instances.
     549                 :             :     node::BlockManager& m_blockman;
     550                 :             : 
     551                 :             :     //! The chainstate manager that owns this chainstate. The reference is
     552                 :             :     //! necessary so that this instance can check whether it is the active
     553                 :             :     //! chainstate within deeply nested method calls.
     554                 :             :     ChainstateManager& m_chainman;
     555                 :             : 
     556                 :             :     explicit Chainstate(
     557                 :             :         CTxMemPool* mempool,
     558                 :             :         node::BlockManager& blockman,
     559                 :             :         ChainstateManager& chainman,
     560         [ +  - ]:           1 :         std::optional<uint256> from_snapshot_blockhash = std::nullopt);
     561                 :             : 
     562                 :             :     //! Return the current role of the chainstate. See `ChainstateManager`
     563                 :             :     //! documentation for a description of the different types of chainstates.
     564                 :             :     //!
     565                 :             :     //! @sa ChainstateRole
     566                 :             :     ChainstateRole GetRole() const EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
     567                 :             : 
     568                 :             :     /**
     569                 :             :      * Initialize the CoinsViews UTXO set database management data structures. The in-memory
     570                 :             :      * cache is initialized separately.
     571                 :             :      *
     572                 :             :      * All parameters forwarded to CoinsViews.
     573                 :             :      */
     574                 :             :     void InitCoinsDB(
     575                 :             :         size_t cache_size_bytes,
     576                 :             :         bool in_memory,
     577                 :             :         bool should_wipe,
     578   [ +  -  -  + ]:           2 :         fs::path leveldb_name = "chainstate");
     579                 :             : 
     580                 :             :     //! Initialize the in-memory coins cache (to be done after the health of the on-disk database
     581                 :             :     //! is verified).
     582                 :             :     void InitCoinsCache(size_t cache_size_bytes) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
     583                 :             : 
     584                 :             :     //! @returns whether or not the CoinsViews object has been fully initialized and we can
     585                 :             :     //!          safely flush this object to disk.
     586                 :           4 :     bool CanFlushToDisk() const EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
     587                 :             :     {
     588                 :           4 :         AssertLockHeld(::cs_main);
     589   [ +  -  +  - ]:           4 :         return m_coins_views && m_coins_views->m_cacheview;
           [ #  #  #  #  
             #  #  #  # ]
     590                 :             :     }
     591                 :             : 
     592                 :             :     //! The current chain of blockheaders we consult and build on.
     593                 :             :     //! @see CChain, CBlockIndex.
     594                 :             :     CChain m_chain;
     595                 :             : 
     596                 :             :     /**
     597                 :             :      * The blockhash which is the base of the snapshot this chainstate was created from.
     598                 :             :      *
     599                 :             :      * std::nullopt if this chainstate was not created from a snapshot.
     600                 :             :      */
     601                 :             :     const std::optional<uint256> m_from_snapshot_blockhash;
     602                 :             : 
     603                 :             :     /**
     604                 :             :      * The base of the snapshot this chainstate was created from.
     605                 :             :      *
     606                 :             :      * nullptr if this chainstate was not created from a snapshot.
     607                 :             :      */
     608                 :             :     const CBlockIndex* SnapshotBase() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
     609                 :             : 
     610                 :             :     /**
     611                 :             :      * The set of all CBlockIndex entries that have as much work as our current
     612                 :             :      * tip or more, and transaction data needed to be validated (with
     613                 :             :      * BLOCK_VALID_TRANSACTIONS for each block and its parents back to the
     614                 :             :      * genesis block or an assumeutxo snapshot block). Entries may be failed,
     615                 :             :      * though, and pruning nodes may be missing the data for the block.
     616                 :             :      */
     617                 :             :     std::set<CBlockIndex*, node::CBlockIndexWorkComparator> setBlockIndexCandidates;
     618                 :             : 
     619                 :             :     //! @returns A reference to the in-memory cache of the UTXO set.
     620                 :          14 :     CCoinsViewCache& CoinsTip() EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
     621                 :             :     {
     622                 :          14 :         AssertLockHeld(::cs_main);
     623                 :          14 :         Assert(m_coins_views);
     624                 :          14 :         return *Assert(m_coins_views->m_cacheview);
     625                 :             :     }
     626                 :             : 
     627                 :             :     //! @returns A reference to the on-disk UTXO set database.
     628                 :           3 :     CCoinsViewDB& CoinsDB() EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
     629                 :             :     {
     630                 :           3 :         AssertLockHeld(::cs_main);
     631                 :           3 :         return Assert(m_coins_views)->m_dbview;
     632                 :             :     }
     633                 :             : 
     634                 :             :     //! @returns A pointer to the mempool.
     635                 :           0 :     CTxMemPool* GetMempool()
     636                 :             :     {
     637   [ #  #  #  #  :           0 :         return m_mempool;
             #  #  #  # ]
           [ #  #  #  #  
             #  #  #  # ]
     638                 :             :     }
     639                 :             : 
     640                 :             :     //! @returns A reference to a wrapped view of the in-memory UTXO set that
     641                 :             :     //!     handles disk read errors gracefully.
     642                 :           0 :     CCoinsViewErrorCatcher& CoinsErrorCatcher() EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
     643                 :             :     {
     644                 :           0 :         AssertLockHeld(::cs_main);
     645                 :           0 :         return Assert(m_coins_views)->m_catcherview;
     646                 :             :     }
     647                 :             : 
     648                 :             :     //! Destructs all objects related to accessing the UTXO set.
     649         [ #  # ]:           0 :     void ResetCoinsViews() { m_coins_views.reset(); }
     650                 :             : 
     651                 :             :     //! Does this chainstate have a UTXO set attached?
     652         [ #  # ]:           0 :     bool HasCoinsViews() const { return (bool)m_coins_views; }
     653                 :             : 
     654                 :             :     //! The cache size of the on-disk coins view.
     655                 :             :     size_t m_coinsdb_cache_size_bytes{0};
     656                 :             : 
     657                 :             :     //! The cache size of the in-memory coins view.
     658                 :             :     size_t m_coinstip_cache_size_bytes{0};
     659                 :             : 
     660                 :             :     //! Resize the CoinsViews caches dynamically and flush state to disk.
     661                 :             :     //! @returns true unless an error occurred during the flush.
     662                 :             :     bool ResizeCoinsCaches(size_t coinstip_size, size_t coinsdb_size)
     663                 :             :         EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
     664                 :             : 
     665                 :             :     /**
     666                 :             :      * Update the on-disk chain state.
     667                 :             :      * The caches and indexes are flushed depending on the mode we're called with
     668                 :             :      * if they're too large, if it's been a while since the last write,
     669                 :             :      * or always and in all cases if we're in prune mode and are deleting files.
     670                 :             :      *
     671                 :             :      * If FlushStateMode::NONE is used, then FlushStateToDisk(...) won't do anything
     672                 :             :      * besides checking if we need to prune.
     673                 :             :      *
     674                 :             :      * @returns true unless a system error occurred
     675                 :             :      */
     676                 :             :     bool FlushStateToDisk(
     677                 :             :         BlockValidationState& state,
     678                 :             :         FlushStateMode mode,
     679                 :             :         int nManualPruneHeight = 0);
     680                 :             : 
     681                 :             :     //! Unconditionally flush all changes to disk.
     682                 :             :     void ForceFlushStateToDisk();
     683                 :             : 
     684                 :             :     //! Prune blockfiles from the disk if necessary and then flush chainstate changes
     685                 :             :     //! if we pruned.
     686                 :             :     void PruneAndFlush();
     687                 :             : 
     688                 :             :     /**
     689                 :             :      * Find the best known block, and make it the tip of the block chain. The
     690                 :             :      * result is either failure or an activated best chain. pblock is either
     691                 :             :      * nullptr or a pointer to a block that is already loaded (to avoid loading
     692                 :             :      * it again from disk).
     693                 :             :      *
     694                 :             :      * ActivateBestChain is split into steps (see ActivateBestChainStep) so that
     695                 :             :      * we avoid holding cs_main for an extended period of time; the length of this
     696                 :             :      * call may be quite long during reindexing or a substantial reorg.
     697                 :             :      *
     698                 :             :      * May not be called with cs_main held. May not be called in a
     699                 :             :      * validationinterface callback.
     700                 :             :      *
     701                 :             :      * Note that if this is called while a snapshot chainstate is active, and if
     702                 :             :      * it is called on a background chainstate whose tip has reached the base block
     703                 :             :      * of the snapshot, its execution will take *MINUTES* while it hashes the
     704                 :             :      * background UTXO set to verify the assumeutxo value the snapshot was activated
     705                 :             :      * with. `cs_main` will be held during this time.
     706                 :             :      *
     707                 :             :      * @returns true unless a system error occurred
     708                 :             :      */
     709                 :             :     bool ActivateBestChain(
     710                 :             :         BlockValidationState& state,
     711                 :             :         std::shared_ptr<const CBlock> pblock = nullptr)
     712                 :             :         EXCLUSIVE_LOCKS_REQUIRED(!m_chainstate_mutex)
     713                 :             :         LOCKS_EXCLUDED(::cs_main);
     714                 :             : 
     715                 :             :     // Block (dis)connection on a given view:
     716                 :             :     DisconnectResult DisconnectBlock(const CBlock& block, const CBlockIndex* pindex, CCoinsViewCache& view)
     717                 :             :         EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
     718                 :             :     bool ConnectBlock(const CBlock& block, BlockValidationState& state, CBlockIndex* pindex,
     719                 :             :                       CCoinsViewCache& view, bool fJustCheck = false) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
     720                 :             : 
     721                 :             :     // Apply the effects of a block disconnection on the UTXO set.
     722                 :             :     bool DisconnectTip(BlockValidationState& state, DisconnectedBlockTransactions* disconnectpool) EXCLUSIVE_LOCKS_REQUIRED(cs_main, m_mempool->cs);
     723                 :             : 
     724                 :             :     // Manual block validity manipulation:
     725                 :             :     /** Mark a block as precious and reorganize.
     726                 :             :      *
     727                 :             :      * May not be called in a validationinterface callback.
     728                 :             :      */
     729                 :             :     bool PreciousBlock(BlockValidationState& state, CBlockIndex* pindex)
     730                 :             :         EXCLUSIVE_LOCKS_REQUIRED(!m_chainstate_mutex)
     731                 :             :         LOCKS_EXCLUDED(::cs_main);
     732                 :             : 
     733                 :             :     /** Mark a block as invalid. */
     734                 :             :     bool InvalidateBlock(BlockValidationState& state, CBlockIndex* pindex)
     735                 :             :         EXCLUSIVE_LOCKS_REQUIRED(!m_chainstate_mutex)
     736                 :             :         LOCKS_EXCLUDED(::cs_main);
     737                 :             : 
     738                 :             :     /** Remove invalidity status from a block and its descendants. */
     739                 :             :     void ResetBlockFailureFlags(CBlockIndex* pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
     740                 :             : 
     741                 :             :     /** Replay blocks that aren't fully applied to the database. */
     742                 :             :     bool ReplayBlocks();
     743                 :             : 
     744                 :             :     /** Whether the chain state needs to be redownloaded due to lack of witness data */
     745                 :             :     [[nodiscard]] bool NeedsRedownload() const EXCLUSIVE_LOCKS_REQUIRED(cs_main);
     746                 :             :     /** Ensures we have a genesis block in the block tree, possibly writing one to disk. */
     747                 :             :     bool LoadGenesisBlock();
     748                 :             : 
     749                 :             :     void TryAddBlockIndexCandidate(CBlockIndex* pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
     750                 :             : 
     751                 :             :     void PruneBlockIndexCandidates();
     752                 :             : 
     753                 :             :     void ClearBlockIndexCandidates() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
     754                 :             : 
     755                 :             :     /** Find the last common block of this chain and a locator. */
     756                 :             :     const CBlockIndex* FindForkInGlobalIndex(const CBlockLocator& locator) const EXCLUSIVE_LOCKS_REQUIRED(cs_main);
     757                 :             : 
     758                 :             :     /** Update the chain tip based on database information, i.e. CoinsTip()'s best block. */
     759                 :             :     bool LoadChainTip() EXCLUSIVE_LOCKS_REQUIRED(cs_main);
     760                 :             : 
     761                 :             :     //! Dictates whether we need to flush the cache to disk or not.
     762                 :             :     //!
     763                 :             :     //! @return the state of the size of the coins cache.
     764                 :             :     CoinsCacheSizeState GetCoinsCacheSizeState() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
     765                 :             : 
     766                 :             :     CoinsCacheSizeState GetCoinsCacheSizeState(
     767                 :             :         size_t max_coins_cache_size_bytes,
     768                 :             :         size_t max_mempool_size_bytes) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
     769                 :             : 
     770                 :             :     std::string ToString() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
     771                 :             : 
     772                 :             :     //! Indirection necessary to make lock annotations work with an optional mempool.
     773                 :           1 :     RecursiveMutex* MempoolMutex() const LOCK_RETURNED(m_mempool->cs)
     774                 :             :     {
     775   [ -  -  +  - ]:           1 :         return m_mempool ? &m_mempool->cs : nullptr;
     776                 :             :     }
     777                 :             : 
     778                 :             : private:
     779                 :             :     bool ActivateBestChainStep(BlockValidationState& state, CBlockIndex* pindexMostWork, const std::shared_ptr<const CBlock>& pblock, bool& fInvalidFound, ConnectTrace& connectTrace) EXCLUSIVE_LOCKS_REQUIRED(cs_main, m_mempool->cs);
     780                 :             :     bool ConnectTip(BlockValidationState& state, CBlockIndex* pindexNew, const std::shared_ptr<const CBlock>& pblock, ConnectTrace& connectTrace, DisconnectedBlockTransactions& disconnectpool) EXCLUSIVE_LOCKS_REQUIRED(cs_main, m_mempool->cs);
     781                 :             : 
     782                 :             :     void InvalidBlockFound(CBlockIndex* pindex, const BlockValidationState& state) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
     783                 :             :     CBlockIndex* FindMostWorkChain() EXCLUSIVE_LOCKS_REQUIRED(cs_main);
     784                 :             : 
     785                 :             :     bool RollforwardBlock(const CBlockIndex* pindex, CCoinsViewCache& inputs) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
     786                 :             : 
     787                 :             :     void CheckForkWarningConditions() EXCLUSIVE_LOCKS_REQUIRED(cs_main);
     788                 :             :     void InvalidChainFound(CBlockIndex* pindexNew) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
     789                 :             : 
     790                 :             :     /**
     791                 :             :      * Make mempool consistent after a reorg, by re-adding or recursively erasing
     792                 :             :      * disconnected block transactions from the mempool, and also removing any
     793                 :             :      * other transactions from the mempool that are no longer valid given the new
     794                 :             :      * tip/height.
     795                 :             :      *
     796                 :             :      * Note: we assume that disconnectpool only contains transactions that are NOT
     797                 :             :      * confirmed in the current chain nor already in the mempool (otherwise,
     798                 :             :      * in-mempool descendants of such transactions would be removed).
     799                 :             :      *
     800                 :             :      * Passing fAddToMempool=false will skip trying to add the transactions back,
     801                 :             :      * and instead just erase from the mempool as needed.
     802                 :             :      */
     803                 :             :     void MaybeUpdateMempoolForReorg(
     804                 :             :         DisconnectedBlockTransactions& disconnectpool,
     805                 :             :         bool fAddToMempool) EXCLUSIVE_LOCKS_REQUIRED(cs_main, m_mempool->cs);
     806                 :             : 
     807                 :             :     /** Check warning conditions and do some notifications on new chain tip set. */
     808                 :             :     void UpdateTip(const CBlockIndex* pindexNew)
     809                 :             :         EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
     810                 :             : 
     811                 :             :     SteadyClock::time_point m_last_write{};
     812                 :             :     SteadyClock::time_point m_last_flush{};
     813                 :             : 
     814                 :             :     /**
     815                 :             :      * In case of an invalid snapshot, rename the coins leveldb directory so
     816                 :             :      * that it can be examined for issue diagnosis.
     817                 :             :      */
     818                 :             :     [[nodiscard]] util::Result<void> InvalidateCoinsDBOnDisk() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
     819                 :             : 
     820                 :             :     friend ChainstateManager;
     821                 :             : };
     822                 :             : 
     823                 :             : enum class SnapshotCompletionResult {
     824                 :             :     SUCCESS,
     825                 :             :     SKIPPED,
     826                 :             : 
     827                 :             :     // Expected assumeutxo configuration data is not found for the height of the
     828                 :             :     // base block.
     829                 :             :     MISSING_CHAINPARAMS,
     830                 :             : 
     831                 :             :     // Failed to generate UTXO statistics (to check UTXO set hash) for the background
     832                 :             :     // chainstate.
     833                 :             :     STATS_FAILED,
     834                 :             : 
     835                 :             :     // The UTXO set hash of the background validation chainstate does not match
     836                 :             :     // the one expected by assumeutxo chainparams.
     837                 :             :     HASH_MISMATCH,
     838                 :             : 
     839                 :             :     // The blockhash of the current tip of the background validation chainstate does
     840                 :             :     // not match the one expected by the snapshot chainstate.
     841                 :             :     BASE_BLOCKHASH_MISMATCH,
     842                 :             : };
     843                 :             : 
     844                 :             : /**
     845                 :             :  * Provides an interface for creating and interacting with one or two
     846                 :             :  * chainstates: an IBD chainstate generated by downloading blocks, and
     847                 :             :  * an optional snapshot chainstate loaded from a UTXO snapshot. Managed
     848                 :             :  * chainstates can be maintained at different heights simultaneously.
     849                 :             :  *
     850                 :             :  * This class provides abstractions that allow the retrieval of the current
     851                 :             :  * most-work chainstate ("Active") as well as chainstates which may be in
     852                 :             :  * background use to validate UTXO snapshots.
     853                 :             :  *
     854                 :             :  * Definitions:
     855                 :             :  *
     856                 :             :  * *IBD chainstate*: a chainstate whose current state has been "fully"
     857                 :             :  *   validated by the initial block download process.
     858                 :             :  *
     859                 :             :  * *Snapshot chainstate*: a chainstate populated by loading in an
     860                 :             :  *    assumeutxo UTXO snapshot.
     861                 :             :  *
     862                 :             :  * *Active chainstate*: the chainstate containing the current most-work
     863                 :             :  *    chain. Consulted by most parts of the system (net_processing,
     864                 :             :  *    wallet) as a reflection of the current chain and UTXO set.
     865                 :             :  *    This may either be an IBD chainstate or a snapshot chainstate.
     866                 :             :  *
     867                 :             :  * *Background IBD chainstate*: an IBD chainstate for which the
     868                 :             :  *    IBD process is happening in the background while use of the
     869                 :             :  *    active (snapshot) chainstate allows the rest of the system to function.
     870                 :             :  */
     871                 :             : class ChainstateManager
     872                 :             : {
     873                 :             : private:
     874                 :             :     //! The chainstate used under normal operation (i.e. "regular" IBD) or, if
     875                 :             :     //! a snapshot is in use, for background validation.
     876                 :             :     //!
     877                 :             :     //! Its contents (including on-disk data) will be deleted *upon shutdown*
     878                 :             :     //! after background validation of the snapshot has completed. We do not
     879                 :             :     //! free the chainstate contents immediately after it finishes validation
     880                 :             :     //! to cautiously avoid a case where some other part of the system is still
     881                 :             :     //! using this pointer (e.g. net_processing).
     882                 :             :     //!
     883                 :             :     //! Once this pointer is set to a corresponding chainstate, it will not
     884                 :             :     //! be reset until init.cpp:Shutdown().
     885                 :             :     //!
     886                 :             :     //! It is important for the pointer to not be deleted until shutdown,
     887                 :             :     //! because cs_main is not always held when the pointer is accessed, for
     888                 :             :     //! example when calling ActivateBestChain, so there's no way you could
     889                 :             :     //! prevent code from using the pointer while deleting it.
     890                 :             :     std::unique_ptr<Chainstate> m_ibd_chainstate GUARDED_BY(::cs_main);
     891                 :             : 
     892                 :             :     //! A chainstate initialized on the basis of a UTXO snapshot. If this is
     893                 :             :     //! non-null, it is always our active chainstate.
     894                 :             :     //!
     895                 :             :     //! Once this pointer is set to a corresponding chainstate, it will not
     896                 :             :     //! be reset until init.cpp:Shutdown().
     897                 :             :     //!
     898                 :             :     //! It is important for the pointer to not be deleted until shutdown,
     899                 :             :     //! because cs_main is not always held when the pointer is accessed, for
     900                 :             :     //! example when calling ActivateBestChain, so there's no way you could
     901                 :             :     //! prevent code from using the pointer while deleting it.
     902                 :             :     std::unique_ptr<Chainstate> m_snapshot_chainstate GUARDED_BY(::cs_main);
     903                 :             : 
     904                 :             :     //! Points to either the ibd or snapshot chainstate; indicates our
     905                 :             :     //! most-work chain.
     906                 :             :     Chainstate* m_active_chainstate GUARDED_BY(::cs_main) {nullptr};
     907                 :             : 
     908                 :             :     CBlockIndex* m_best_invalid GUARDED_BY(::cs_main){nullptr};
     909                 :             : 
     910                 :             :     /** The last header for which a headerTip notification was issued. */
     911                 :             :     CBlockIndex* m_last_notified_header GUARDED_BY(GetMutex()){nullptr};
     912                 :             : 
     913                 :             :     bool NotifyHeaderTip() LOCKS_EXCLUDED(GetMutex());
     914                 :             : 
     915                 :             :     //! Internal helper for ActivateSnapshot().
     916                 :             :     //!
     917                 :             :     //! De-serialization of a snapshot that is created with
     918                 :             :     //! the dumptxoutset RPC.
     919                 :             :     //! To reduce space the serialization format of the snapshot avoids
     920                 :             :     //! duplication of tx hashes. The code takes advantage of the guarantee by
     921                 :             :     //! leveldb that keys are lexicographically sorted.
     922                 :             :     [[nodiscard]] util::Result<void> PopulateAndValidateSnapshot(
     923                 :             :         Chainstate& snapshot_chainstate,
     924                 :             :         AutoFile& coins_file,
     925                 :             :         const node::SnapshotMetadata& metadata);
     926                 :             : 
     927                 :             :     /**
     928                 :             :      * If a block header hasn't already been seen, call CheckBlockHeader on it, ensure
     929                 :             :      * that it doesn't descend from an invalid block, and then add it to m_block_index.
     930                 :             :      * Caller must set min_pow_checked=true in order to add a new header to the
     931                 :             :      * block index (permanent memory storage), indicating that the header is
     932                 :             :      * known to be part of a sufficiently high-work chain (anti-dos check).
     933                 :             :      */
     934                 :             :     bool AcceptBlockHeader(
     935                 :             :         const CBlockHeader& block,
     936                 :             :         BlockValidationState& state,
     937                 :             :         CBlockIndex** ppindex,
     938                 :             :         bool min_pow_checked) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
     939                 :             :     friend Chainstate;
     940                 :             : 
     941                 :             :     /** Most recent headers presync progress update, for rate-limiting. */
     942                 :             :     std::chrono::time_point<std::chrono::steady_clock> m_last_presync_update GUARDED_BY(::cs_main) {};
     943                 :             : 
     944                 :             :     std::array<ThresholdConditionCache, VERSIONBITS_NUM_BITS> m_warningcache GUARDED_BY(::cs_main);
     945                 :             : 
     946                 :             :     //! Return true if a chainstate is considered usable.
     947                 :             :     //!
     948                 :             :     //! This is false when a background validation chainstate has completed its
     949                 :             :     //! validation of an assumed-valid chainstate, or when a snapshot
     950                 :             :     //! chainstate has been found to be invalid.
     951                 :          16 :     bool IsUsable(const Chainstate* const cs) const EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
     952   [ +  -  -  +  :          15 :         return cs && !cs->m_disabled;
          -  +  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  +  
          +  +  -  -  -  
          -  -  -  -  -  
           - ][ #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
     953                 :             :     }
     954                 :             : 
     955                 :             :     //! A queue for script verifications that have to be performed by worker threads.
     956                 :             :     CCheckQueue<CScriptCheck> m_script_check_queue;
     957                 :             : 
     958                 :             :     //! Timers and counters used for benchmarking validation in both background
     959                 :             :     //! and active chainstates.
     960                 :             :     SteadyClock::duration GUARDED_BY(::cs_main) time_check{};
     961                 :             :     SteadyClock::duration GUARDED_BY(::cs_main) time_forks{};
     962                 :             :     SteadyClock::duration GUARDED_BY(::cs_main) time_connect{};
     963                 :             :     SteadyClock::duration GUARDED_BY(::cs_main) time_verify{};
     964                 :             :     SteadyClock::duration GUARDED_BY(::cs_main) time_undo{};
     965                 :             :     SteadyClock::duration GUARDED_BY(::cs_main) time_index{};
     966                 :             :     SteadyClock::duration GUARDED_BY(::cs_main) time_total{};
     967                 :             :     int64_t GUARDED_BY(::cs_main) num_blocks_total{0};
     968                 :             :     SteadyClock::duration GUARDED_BY(::cs_main) time_connect_total{};
     969                 :             :     SteadyClock::duration GUARDED_BY(::cs_main) time_flush{};
     970                 :             :     SteadyClock::duration GUARDED_BY(::cs_main) time_chainstate{};
     971                 :             :     SteadyClock::duration GUARDED_BY(::cs_main) time_post_connect{};
     972                 :             : 
     973                 :             : public:
     974                 :             :     using Options = kernel::ChainstateManagerOpts;
     975                 :             : 
     976                 :             :     explicit ChainstateManager(const util::SignalInterrupt& interrupt, Options options, node::BlockManager::Options blockman_options);
     977                 :             : 
     978                 :             :     //! Function to restart active indexes; set dynamically to avoid a circular
     979                 :             :     //! dependency on `base/index.cpp`.
     980                 :             :     std::function<void()> restart_indexes = std::function<void()>();
     981                 :             : 
     982   [ #  #  #  # ]:           4 :     const CChainParams& GetParams() const { return m_options.chainparams; }
           [ -  -  +  -  
             -  -  -  - ]
           [ +  -  #  #  
             #  #  #  # ]
           [ #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
     983 [ #  # ][ -  -  :           3 :     const Consensus::Params& GetConsensus() const { return m_options.chainparams.GetConsensus(); }
             -  -  -  - ]
           [ -  -  -  -  
           #  # ][ #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
     984                 :             :     bool ShouldCheckBlockIndex() const;
     985                 :           6 :     const arith_uint256& MinimumChainWork() const { return *Assert(m_options.minimum_chain_work); }
     986                 :           1 :     const uint256& AssumedValidBlock() const { return *Assert(m_options.assumed_valid_block); }
     987   [ -  -  -  -  :           1 :     kernel::Notifications& GetNotifications() const { return m_options.notifications; };
          -  -  -  -  -  
          -  -  -  -  -  
          +  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
             -  -  -  -  
           - ][ #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                      # ]
     988                 :             : 
     989                 :             :     /**
     990                 :             :      * Make various assertions about the state of the block index.
     991                 :             :      *
     992                 :             :      * By default this only executes fully when using the Regtest chain; see: m_options.check_block_index.
     993                 :             :      */
     994                 :             :     void CheckBlockIndex();
     995                 :             : 
     996                 :             :     /**
     997                 :             :      * Alias for ::cs_main.
     998                 :             :      * Should be used in new code to make it easier to make ::cs_main a member
     999                 :             :      * of this class.
    1000                 :             :      * Generally, methods of this class should be annotated to require this
    1001                 :             :      * mutex. This will make calling code more verbose, but also help to:
    1002                 :             :      * - Clarify that the method will acquire a mutex that heavily affects
    1003                 :             :      *   overall performance.
    1004                 :             :      * - Force call sites to think how long they need to acquire the mutex to
    1005                 :             :      *   get consistent results.
    1006                 :             :      */
    1007 [ +  - ][ #  #  :           1 :     RecursiveMutex& GetMutex() const LOCK_RETURNED(::cs_main) { return ::cs_main; }
             #  #  #  # ]
    1008                 :             : 
    1009                 :             :     const util::SignalInterrupt& m_interrupt;
    1010                 :             :     const Options m_options;
    1011                 :             :     std::thread m_thread_load;
    1012                 :             :     //! A single BlockManager instance is shared across each constructed
    1013                 :             :     //! chainstate to avoid duplicating block metadata.
    1014                 :             :     node::BlockManager m_blockman;
    1015                 :             : 
    1016                 :             :     ValidationCache m_validation_cache;
    1017                 :             : 
    1018                 :             :     /**
    1019                 :             :      * Whether initial block download has ended and IsInitialBlockDownload
    1020                 :             :      * should return false from now on.
    1021                 :             :      *
    1022                 :             :      * Mutable because we need to be able to mark IsInitialBlockDownload()
    1023                 :             :      * const, which latches this for caching purposes.
    1024                 :             :      */
    1025                 :             :     mutable std::atomic<bool> m_cached_finished_ibd{false};
    1026                 :             : 
    1027                 :             :     /**
    1028                 :             :      * Every received block is assigned a unique and increasing identifier, so we
    1029                 :             :      * know which one to give priority in case of a fork.
    1030                 :             :      */
    1031                 :             :     /** Blocks loaded from disk are assigned id 0, so start the counter at 1. */
    1032                 :             :     int32_t nBlockSequenceId GUARDED_BY(::cs_main) = 1;
    1033                 :             :     /** Decreasing counter (used by subsequent preciousblock calls). */
    1034                 :             :     int32_t nBlockReverseSequenceId = -1;
    1035                 :             :     /** chainwork for the last block that preciousblock has been applied to. */
    1036                 :             :     arith_uint256 nLastPreciousChainwork = 0;
    1037                 :             : 
    1038                 :             :     // Reset the memory-only sequence counters we use to track block arrival
    1039                 :             :     // (used by tests to reset state)
    1040                 :             :     void ResetBlockSequenceCounters() EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
    1041                 :             :     {
    1042                 :             :         AssertLockHeld(::cs_main);
    1043                 :             :         nBlockSequenceId = 1;
    1044                 :             :         nBlockReverseSequenceId = -1;
    1045                 :             :     }
    1046                 :             : 
    1047                 :             : 
    1048                 :             :     /**
    1049                 :             :      * In order to efficiently track invalidity of headers, we keep the set of
    1050                 :             :      * blocks which we tried to connect and found to be invalid here (ie which
    1051                 :             :      * were set to BLOCK_FAILED_VALID since the last restart). We can then
    1052                 :             :      * walk this set and check if a new header is a descendant of something in
    1053                 :             :      * this set, preventing us from having to walk m_block_index when we try
    1054                 :             :      * to connect a bad block and fail.
    1055                 :             :      *
    1056                 :             :      * While this is more complicated than marking everything which descends
    1057                 :             :      * from an invalid block as invalid at the time we discover it to be
    1058                 :             :      * invalid, doing so would require walking all of m_block_index to find all
    1059                 :             :      * descendants. Since this case should be very rare, keeping track of all
    1060                 :             :      * BLOCK_FAILED_VALID blocks in a set should be just fine and work just as
    1061                 :             :      * well.
    1062                 :             :      *
    1063                 :             :      * Because we already walk m_block_index in height-order at startup, we go
    1064                 :             :      * ahead and mark descendants of invalid blocks as FAILED_CHILD at that time,
    1065                 :             :      * instead of putting things in this set.
    1066                 :             :      */
    1067                 :             :     std::set<CBlockIndex*> m_failed_blocks;
    1068                 :             : 
    1069                 :             :     /** Best header we've seen so far (used for getheaders queries' starting points). */
    1070                 :             :     CBlockIndex* m_best_header GUARDED_BY(::cs_main){nullptr};
    1071                 :             : 
    1072                 :             :     //! The total number of bytes available for us to use across all in-memory
    1073                 :             :     //! coins caches. This will be split somehow across chainstates.
    1074                 :             :     int64_t m_total_coinstip_cache{0};
    1075                 :             :     //
    1076                 :             :     //! The total number of bytes available for us to use across all leveldb
    1077                 :             :     //! coins databases. This will be split somehow across chainstates.
    1078                 :             :     int64_t m_total_coinsdb_cache{0};
    1079                 :             : 
    1080                 :             :     //! Instantiate a new chainstate.
    1081                 :             :     //!
    1082                 :             :     //! @param[in] mempool              The mempool to pass to the chainstate
    1083                 :             :     //                                  constructor
    1084                 :             :     Chainstate& InitializeChainstate(CTxMemPool* mempool) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
    1085                 :             : 
    1086                 :             :     //! Get all chainstates currently being used.
    1087                 :             :     std::vector<Chainstate*> GetAll();
    1088                 :             : 
    1089                 :             :     //! Construct and activate a Chainstate on the basis of UTXO snapshot data.
    1090                 :             :     //!
    1091                 :             :     //! Steps:
    1092                 :             :     //!
    1093                 :             :     //! - Initialize an unused Chainstate.
    1094                 :             :     //! - Load its `CoinsViews` contents from `coins_file`.
    1095                 :             :     //! - Verify that the hash of the resulting coinsdb matches the expected hash
    1096                 :             :     //!   per assumeutxo chain parameters.
    1097                 :             :     //! - Wait for our headers chain to include the base block of the snapshot.
    1098                 :             :     //! - "Fast forward" the tip of the new chainstate to the base of the snapshot.
    1099                 :             :     //! - Move the new chainstate to `m_snapshot_chainstate` and make it our
    1100                 :             :     //!   ChainstateActive().
    1101                 :             :     [[nodiscard]] util::Result<CBlockIndex*> ActivateSnapshot(
    1102                 :             :         AutoFile& coins_file, const node::SnapshotMetadata& metadata, bool in_memory);
    1103                 :             : 
    1104                 :             :     //! Once the background validation chainstate has reached the height which
    1105                 :             :     //! is the base of the UTXO snapshot in use, compare its coins to ensure
    1106                 :             :     //! they match those expected by the snapshot.
    1107                 :             :     //!
    1108                 :             :     //! If the coins match (expected), then mark the validation chainstate for
    1109                 :             :     //! deletion and continue using the snapshot chainstate as active.
    1110                 :             :     //! Otherwise, revert to using the ibd chainstate and shutdown.
    1111                 :             :     SnapshotCompletionResult MaybeCompleteSnapshotValidation() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
    1112                 :             : 
    1113                 :             :     //! Returns nullptr if no snapshot has been loaded.
    1114                 :             :     const CBlockIndex* GetSnapshotBaseBlock() const EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
    1115                 :             : 
    1116                 :             :     //! The most-work chain.
    1117                 :             :     Chainstate& ActiveChainstate() const;
    1118   [ #  #  #  # ]:           6 :     CChain& ActiveChain() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex()) { return ActiveChainstate().m_chain; }
           [ +  -  -  +  
          +  -  +  +  #  
          #  #  #  #  #  
           #  # ][ #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           #  # ][ #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           #  # ][ #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
           [ #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           #  # ][ #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           # ][ #  #  #  
                #  #  # ]
    1119                 :           0 :     int ActiveHeight() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex()) { return ActiveChain().Height(); }
    1120         [ #  # ]:           0 :     CBlockIndex* ActiveTip() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex()) { return ActiveChain().Tip(); }
    1121                 :             : 
    1122                 :             :     //! The state of a background sync (for net processing)
    1123                 :           0 :     bool BackgroundSyncInProgress() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex()) {
    1124   [ #  #  #  # ]:           0 :         return IsUsable(m_snapshot_chainstate.get()) && IsUsable(m_ibd_chainstate.get());
    1125                 :             :     }
    1126                 :             : 
    1127                 :             :     //! The tip of the background sync chain
    1128                 :           0 :     const CBlockIndex* GetBackgroundSyncTip() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex()) {
    1129   [ #  #  #  # ]:           0 :         return BackgroundSyncInProgress() ? m_ibd_chainstate->m_chain.Tip() : nullptr;
    1130                 :             :     }
    1131                 :             : 
    1132                 :             :     node::BlockMap& BlockIndex() EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
    1133                 :             :     {
    1134                 :             :         AssertLockHeld(::cs_main);
    1135         [ -  + ]:           1 :         return m_blockman.m_block_index;
    1136                 :             :     }
    1137                 :             : 
    1138                 :             :     /**
    1139                 :             :      * Track versionbit status
    1140                 :             :      */
    1141                 :             :     mutable VersionBitsCache m_versionbitscache;
    1142                 :             : 
    1143                 :             :     //! @returns true if a snapshot-based chainstate is in use. Also implies
    1144                 :             :     //!          that a background validation chainstate is also in use.
    1145                 :             :     bool IsSnapshotActive() const;
    1146                 :             : 
    1147                 :             :     std::optional<uint256> SnapshotBlockhash() const;
    1148                 :             : 
    1149                 :             :     //! Is there a snapshot in use and has it been fully validated?
    1150                 :           0 :     bool IsSnapshotValidated() const EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
    1151                 :             :     {
    1152   [ #  #  #  #  :           0 :         return m_snapshot_chainstate && m_ibd_chainstate && m_ibd_chainstate->m_disabled;
                   #  # ]
    1153                 :             :     }
    1154                 :             : 
    1155                 :             :     /** Check whether we are doing an initial block download (synchronizing from disk or network) */
    1156                 :             :     bool IsInitialBlockDownload() const;
    1157                 :             : 
    1158                 :             :     /**
    1159                 :             :      * Import blocks from an external file
    1160                 :             :      *
    1161                 :             :      * During reindexing, this function is called for each block file (datadir/blocks/blk?????.dat).
    1162                 :             :      * It reads all blocks contained in the given file and attempts to process them (add them to the
    1163                 :             :      * block index). The blocks may be out of order within each file and across files. Often this
    1164                 :             :      * function reads a block but finds that its parent hasn't been read yet, so the block can't be
    1165                 :             :      * processed yet. The function will add an entry to the blocks_with_unknown_parent map (which is
    1166                 :             :      * passed as an argument), so that when the block's parent is later read and processed, this
    1167                 :             :      * function can re-read the child block from disk and process it.
    1168                 :             :      *
    1169                 :             :      * Because a block's parent may be in a later file, not just later in the same file, the
    1170                 :             :      * blocks_with_unknown_parent map must be passed in and out with each call. It's a multimap,
    1171                 :             :      * rather than just a map, because multiple blocks may have the same parent (when chain splits
    1172                 :             :      * or stale blocks exist). It maps from parent-hash to child-disk-position.
    1173                 :             :      *
    1174                 :             :      * This function can also be used to read blocks from user-specified block files using the
    1175                 :             :      * -loadblock= option. There's no unknown-parent tracking, so the last two arguments are omitted.
    1176                 :             :      *
    1177                 :             :      *
    1178                 :             :      * @param[in]     file_in                       File containing blocks to read
    1179                 :             :      * @param[in]     dbp                           (optional) Disk block position (only for reindex)
    1180                 :             :      * @param[in,out] blocks_with_unknown_parent    (optional) Map of disk positions for blocks with
    1181                 :             :      *                                              unknown parent, key is parent block hash
    1182                 :             :      *                                              (only used for reindex)
    1183                 :             :      * */
    1184                 :             :     void LoadExternalBlockFile(
    1185                 :             :         AutoFile& file_in,
    1186                 :             :         FlatFilePos* dbp = nullptr,
    1187                 :             :         std::multimap<uint256, FlatFilePos>* blocks_with_unknown_parent = nullptr);
    1188                 :             : 
    1189                 :             :     /**
    1190                 :             :      * Process an incoming block. This only returns after the best known valid
    1191                 :             :      * block is made active. Note that it does not, however, guarantee that the
    1192                 :             :      * specific block passed to it has been checked for validity!
    1193                 :             :      *
    1194                 :             :      * If you want to *possibly* get feedback on whether block is valid, you must
    1195                 :             :      * install a CValidationInterface (see validationinterface.h) - this will have
    1196                 :             :      * its BlockChecked method called whenever *any* block completes validation.
    1197                 :             :      *
    1198                 :             :      * Note that we guarantee that either the proof-of-work is valid on block, or
    1199                 :             :      * (and possibly also) BlockChecked will have been called.
    1200                 :             :      *
    1201                 :             :      * May not be called in a validationinterface callback.
    1202                 :             :      *
    1203                 :             :      * @param[in]   block The block we want to process.
    1204                 :             :      * @param[in]   force_processing Process this block even if unrequested; used for non-network block sources.
    1205                 :             :      * @param[in]   min_pow_checked  True if proof-of-work anti-DoS checks have
    1206                 :             :      *                               been done by caller for headers chain
    1207                 :             :      *                               (note: only affects headers acceptance; if
    1208                 :             :      *                               block header is already present in block
    1209                 :             :      *                               index then this parameter has no effect)
    1210                 :             :      * @param[out]  new_block A boolean which is set to indicate if the block was first received via this call
    1211                 :             :      * @returns     If the block was processed, independently of block validity
    1212                 :             :      */
    1213                 :             :     bool ProcessNewBlock(const std::shared_ptr<const CBlock>& block, bool force_processing, bool min_pow_checked, bool* new_block) LOCKS_EXCLUDED(cs_main);
    1214                 :             : 
    1215                 :             :     /**
    1216                 :             :      * Process incoming block headers.
    1217                 :             :      *
    1218                 :             :      * May not be called in a
    1219                 :             :      * validationinterface callback.
    1220                 :             :      *
    1221                 :             :      * @param[in]  headers The block headers themselves
    1222                 :             :      * @param[in]  min_pow_checked  True if proof-of-work anti-DoS checks have been done by caller for headers chain
    1223                 :             :      * @param[out] state This may be set to an Error state if any error occurred processing them
    1224                 :             :      * @param[out] ppindex If set, the pointer will be set to point to the last new block index object for the given headers
    1225                 :             :      */
    1226                 :             :     bool ProcessNewBlockHeaders(std::span<const CBlockHeader> headers, bool min_pow_checked, BlockValidationState& state, const CBlockIndex** ppindex = nullptr) LOCKS_EXCLUDED(cs_main);
    1227                 :             : 
    1228                 :             :     /**
    1229                 :             :      * Sufficiently validate a block for disk storage (and store on disk).
    1230                 :             :      *
    1231                 :             :      * @param[in]   pblock          The block we want to process.
    1232                 :             :      * @param[in]   fRequested      Whether we requested this block from a
    1233                 :             :      *                              peer.
    1234                 :             :      * @param[in]   dbp             The location on disk, if we are importing
    1235                 :             :      *                              this block from prior storage.
    1236                 :             :      * @param[in]   min_pow_checked True if proof-of-work anti-DoS checks have
    1237                 :             :      *                              been done by caller for headers chain
    1238                 :             :      *
    1239                 :             :      * @param[out]  state       The state of the block validation.
    1240                 :             :      * @param[out]  ppindex     Optional return parameter to get the
    1241                 :             :      *                          CBlockIndex pointer for this block.
    1242                 :             :      * @param[out]  fNewBlock   Optional return parameter to indicate if the
    1243                 :             :      *                          block is new to our storage.
    1244                 :             :      *
    1245                 :             :      * @returns   False if the block or header is invalid, or if saving to disk fails (likely a fatal error); true otherwise.
    1246                 :             :      */
    1247                 :             :     bool AcceptBlock(const std::shared_ptr<const CBlock>& pblock, BlockValidationState& state, CBlockIndex** ppindex, bool fRequested, const FlatFilePos* dbp, bool* fNewBlock, bool min_pow_checked) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
    1248                 :             : 
    1249                 :             :     void ReceivedBlockTransactions(const CBlock& block, CBlockIndex* pindexNew, const FlatFilePos& pos) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
    1250                 :             : 
    1251                 :             :     /**
    1252                 :             :      * Try to add a transaction to the memory pool.
    1253                 :             :      *
    1254                 :             :      * @param[in]  tx              The transaction to submit for mempool acceptance.
    1255                 :             :      * @param[in]  test_accept     When true, run validation checks but don't submit to mempool.
    1256                 :             :      */
    1257                 :             :     [[nodiscard]] MempoolAcceptResult ProcessTransaction(const CTransactionRef& tx, bool test_accept=false)
    1258                 :             :         EXCLUSIVE_LOCKS_REQUIRED(cs_main);
    1259                 :             : 
    1260                 :             :     //! Load the block tree and coins database from disk, initializing state if we're running with -reindex
    1261                 :             :     bool LoadBlockIndex() EXCLUSIVE_LOCKS_REQUIRED(cs_main);
    1262                 :             : 
    1263                 :             :     //! Check to see if caches are out of balance and if so, call
    1264                 :             :     //! ResizeCoinsCaches() as needed.
    1265                 :             :     void MaybeRebalanceCaches() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
    1266                 :             : 
    1267                 :             :     /** Update uncommitted block structures (currently: only the witness reserved value). This is safe for submitted blocks. */
    1268                 :             :     void UpdateUncommittedBlockStructures(CBlock& block, const CBlockIndex* pindexPrev) const;
    1269                 :             : 
    1270                 :             :     /** Produce the necessary coinbase commitment for a block (modifies the hash, don't call for mined blocks). */
    1271                 :             :     std::vector<unsigned char> GenerateCoinbaseCommitment(CBlock& block, const CBlockIndex* pindexPrev) const;
    1272                 :             : 
    1273                 :             :     /** This is used by net_processing to report pre-synchronization progress of headers, as
    1274                 :             :      *  headers are not yet fed to validation during that time, but validation is (for now)
    1275                 :             :      *  responsible for logging and signalling through NotifyHeaderTip, so it needs this
    1276                 :             :      *  information. */
    1277                 :             :     void ReportHeadersPresync(const arith_uint256& work, int64_t height, int64_t timestamp);
    1278                 :             : 
    1279                 :             :     //! When starting up, search the datadir for a chainstate based on a UTXO
    1280                 :             :     //! snapshot that is in the process of being validated.
    1281                 :             :     bool DetectSnapshotChainstate() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
    1282                 :             : 
    1283                 :             :     void ResetChainstates() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
    1284                 :             : 
    1285                 :             :     //! Remove the snapshot-based chainstate and all on-disk artifacts.
    1286                 :             :     //! Used when reindex{-chainstate} is called during snapshot use.
    1287                 :             :     [[nodiscard]] bool DeleteSnapshotChainstate() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
    1288                 :             : 
    1289                 :             :     //! Switch the active chainstate to one based on a UTXO snapshot that was loaded
    1290                 :             :     //! previously.
    1291                 :             :     Chainstate& ActivateExistingSnapshot(uint256 base_blockhash) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
    1292                 :             : 
    1293                 :             :     //! If we have validated a snapshot chain during this runtime, copy its
    1294                 :             :     //! chainstate directory over to the main `chainstate` location, completing
    1295                 :             :     //! validation of the snapshot.
    1296                 :             :     //!
    1297                 :             :     //! If the cleanup succeeds, the caller will need to ensure chainstates are
    1298                 :             :     //! reinitialized, since ResetChainstates() will be called before leveldb
    1299                 :             :     //! directories are moved or deleted.
    1300                 :             :     //!
    1301                 :             :     //! @sa node/chainstate:LoadChainstate()
    1302                 :             :     bool ValidatedSnapshotCleanup() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
    1303                 :             : 
    1304                 :             :     //! @returns the chainstate that indexes should consult when ensuring that an
    1305                 :             :     //!   index is synced with a chain where we can expect block index entries to have
    1306                 :             :     //!   BLOCK_HAVE_DATA beneath the tip.
    1307                 :             :     //!
    1308                 :             :     //!   In other words, give us the chainstate for which we can reasonably expect
    1309                 :             :     //!   that all blocks beneath the tip have been indexed. In practice this means
    1310                 :             :     //!   when using an assumed-valid chainstate based upon a snapshot, return only the
    1311                 :             :     //!   fully validated chain.
    1312                 :             :     Chainstate& GetChainstateForIndexing() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
    1313                 :             : 
    1314                 :             :     //! Return the [start, end] (inclusive) of block heights we can prune.
    1315                 :             :     //!
    1316                 :             :     //! start > end is possible, meaning no blocks can be pruned.
    1317                 :             :     std::pair<int, int> GetPruneRange(
    1318                 :             :         const Chainstate& chainstate, int last_height_can_prune) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
    1319                 :             : 
    1320                 :             :     //! Return the height of the base block of the snapshot in use, if one exists, else
    1321                 :             :     //! nullopt.
    1322                 :             :     std::optional<int> GetSnapshotBaseHeight() const EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
    1323                 :             : 
    1324                 :           0 :     CCheckQueue<CScriptCheck>& GetCheckQueue() { return m_script_check_queue; }
    1325                 :             : 
    1326                 :             :     ~ChainstateManager();
    1327                 :             : };
    1328                 :             : 
    1329                 :             : /** Deployment* info via ChainstateManager */
    1330                 :             : template<typename DEP>
    1331                 :           0 : bool DeploymentActiveAfter(const CBlockIndex* pindexPrev, const ChainstateManager& chainman, DEP dep)
    1332                 :             : {
    1333                 :           0 :     return DeploymentActiveAfter(pindexPrev, chainman.GetConsensus(), dep, chainman.m_versionbitscache);
    1334                 :             : }
    1335                 :             : 
    1336                 :             : template<typename DEP>
    1337                 :           1 : bool DeploymentActiveAt(const CBlockIndex& index, const ChainstateManager& chainman, DEP dep)
    1338                 :             : {
    1339                 :           1 :     return DeploymentActiveAt(index, chainman.GetConsensus(), dep, chainman.m_versionbitscache);
    1340                 :             : }
    1341                 :             : 
    1342                 :             : template<typename DEP>
    1343                 :           0 : bool DeploymentEnabled(const ChainstateManager& chainman, DEP dep)
    1344                 :             : {
    1345                 :           0 :     return DeploymentEnabled(chainman.GetConsensus(), dep);
    1346                 :             : }
    1347                 :             : 
    1348                 :             : /** Identifies blocks that overwrote an existing coinbase output in the UTXO set (see BIP30) */
    1349                 :             : bool IsBIP30Repeat(const CBlockIndex& block_index);
    1350                 :             : 
    1351                 :             : /** Identifies blocks which coinbase output was subsequently overwritten in the UTXO set (see BIP30) */
    1352                 :             : bool IsBIP30Unspendable(const CBlockIndex& block_index);
    1353                 :             : 
    1354                 :             : #endif // BITCOIN_VALIDATION_H
        

Generated by: LCOV version 2.0-1