validation.cpp raw

   1  // Copyright (c) 2009-2010 Satoshi Nakamoto
   2  // Copyright (c) 2009-present The Limenka 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  #include <limenka-build-config.h> // IWYU pragma: keep
   7  
   8  #include <validation.h>
   9  
  10  #include <arith_uint256.h>
  11  #include <chain.h>
  12  #include <chainparamsbase.h>
  13  #include <checkqueue.h>
  14  #include <clientversion.h>
  15  #include <common/args.h>
  16  #include <consensus/amount.h>
  17  #include <consensus/consensus.h>
  18  #include <consensus/merkle.h>
  19  #include <consensus/tx_check.h>
  20  #include <consensus/tx_verify.h>
  21  #include <consensus/validation.h>
  22  #include <consensus/delay.h>
  23  #include <cuckoocache.h>
  24  #include <deploymentinfo.h>
  25  #include <flatfile.h>
  26  #include <hash.h>
  27  #include <kernel/chain.h>
  28  #include <kernel/chainparams.h>
  29  #include <kernel/coinstats.h>
  30  #include <kernel/disconnected_transactions.h>
  31  #include <kernel/mempool_entry.h>
  32  #include <kernel/messagestartchars.h>
  33  #include <kernel/notifications_interface.h>
  34  #include <kernel/warning.h>
  35  #include <logging.h>
  36  #include <logging/timer.h>
  37  #include <node/blockstorage.h>
  38  #include <node/utxo_snapshot.h>
  39  #include <policy/coin_age_priority.h>
  40  #include <policy/ephemeral_policy.h>
  41  #include <policy/policy.h>
  42  #include <policy/rbf.h>
  43  #include <policy/settings.h>
  44  #include <policy/truc_policy.h>
  45  #include <pow.h>
  46  #include <pow_fork.h>
  47  #include <primitives/block.h>
  48  #include <primitives/transaction.h>
  49  #include <random.h>
  50  #include <script/script.h>
  51  #include <script/sigcache.h>
  52  #include <signet.h>
  53  #include <stats/stats.h>
  54  #include <tinyformat.h>
  55  #include <txdb.h>
  56  #include <txmempool.h>
  57  #include <uint256.h>
  58  #include <undo.h>
  59  #include <util/check.h>
  60  #include <util/fs.h>
  61  #include <util/fs_helpers.h>
  62  #include <util/hasher.h>
  63  #include <util/ioprio.h>
  64  #include <util/mempressure.h>
  65  #include <util/moneystr.h>
  66  #include <util/overflow.h>
  67  #include <util/rbf.h>
  68  #include <util/result.h>
  69  #include <util/signalinterrupt.h>
  70  #include <util/strencodings.h>
  71  #include <util/string.h>
  72  #include <util/time.h>
  73  #include <util/trace.h>
  74  #include <util/translation.h>
  75  #include <validationinterface.h>
  76  
  77  #include <algorithm>
  78  #include <cassert>
  79  #include <chrono>
  80  #include <deque>
  81  #include <numeric>
  82  #include <optional>
  83  #include <ranges>
  84  #include <span>
  85  #include <string>
  86  #include <tuple>
  87  #include <utility>
  88  
  89  using kernel::CCoinsStats;
  90  using kernel::CoinStatsHashType;
  91  using kernel::ComputeUTXOStats;
  92  using kernel::Notifications;
  93  
  94  using fsbridge::FopenFn;
  95  using node::BlockManager;
  96  using node::BlockMap;
  97  using node::CBlockIndexHeightOnlyComparator;
  98  using node::CBlockIndexWorkComparator;
  99  using node::SnapshotMetadata;
 100  
 101  /** Size threshold for warning about slow UTXO set flush to disk. */
 102  static constexpr size_t WARN_FLUSH_COINS_SIZE = 1 << 30; // 1 GiB
 103  /** Time window to wait between writing blocks/block index and chainstate to disk.
 104   *  Randomize writing time inside the window to prevent a situation where the
 105   *  network over time settles into a few cohorts of synchronized writers.
 106  */
 107  static constexpr auto DATABASE_WRITE_INTERVAL_MIN{50min};
 108  static constexpr auto DATABASE_WRITE_INTERVAL_MAX{70min};
 109  /** Maximum age of our tip for us to be considered current for fee estimation */
 110  static constexpr std::chrono::hours MAX_FEE_ESTIMATION_TIP_AGE{3};
 111  const std::vector<std::string> CHECKLEVEL_DOC {
 112      "level 0 reads the blocks from disk",
 113      "level 1 verifies block validity",
 114      "level 2 verifies undo data",
 115      "level 3 checks disconnection of tip blocks",
 116      "level 4 tries to reconnect the blocks",
 117      "each level includes the checks of the previous levels",
 118  };
 119  
 120  SpkReuseModes SpkReuseMode;
 121  
 122  TRACEPOINT_SEMAPHORE(validation, block_connected);
 123  TRACEPOINT_SEMAPHORE(utxocache, flush);
 124  TRACEPOINT_SEMAPHORE(mempool, replaced);
 125  TRACEPOINT_SEMAPHORE(mempool, rejected);
 126  
 127  const CBlockIndex* Chainstate::FindForkInGlobalIndex(const CBlockLocator& locator) const
 128  {
 129      AssertLockHeld(cs_main);
 130  
 131      // Find the latest block common to locator and chain - we expect that
 132      // locator.vHave is sorted descending by height.
 133      for (const uint256& hash : locator.vHave) {
 134          const CBlockIndex* pindex{m_blockman.LookupBlockIndex(hash)};
 135          if (pindex) {
 136              if (m_chain.Contains(pindex)) {
 137                  return pindex;
 138              }
 139              if (pindex->GetAncestor(m_chain.Height()) == m_chain.Tip()) {
 140                  return m_chain.Tip();
 141              }
 142          }
 143      }
 144      return m_chain.Genesis();
 145  }
 146  
 147  bool CheckInputScripts(const CTransaction& tx, TxValidationState& state,
 148                         const CCoinsViewCache& inputs, unsigned int flags, bool cacheSigStore,
 149                         bool cacheFullScriptStore, PrecomputedTransactionData& txdata,
 150                         ValidationCache& validation_cache,
 151                         std::vector<CScriptCheck>* pvChecks = nullptr,
 152                         const std::vector<unsigned int>& flags_per_input = {})
 153                         EXCLUSIVE_LOCKS_REQUIRED(cs_main);
 154  
 155  bool CheckFinalTxAtTip(const CBlockIndex& active_chain_tip, const CTransaction& tx)
 156  {
 157      AssertLockHeld(cs_main);
 158  
 159      // CheckFinalTxAtTip() uses active_chain_tip.Height()+1 to evaluate
 160      // nLockTime because when IsFinalTx() is called within
 161      // AcceptBlock(), the height of the block *being*
 162      // evaluated is what is used. Thus if we want to know if a
 163      // transaction can be part of the *next* block, we need to call
 164      // IsFinalTx() with one more than active_chain_tip.Height().
 165      const int nBlockHeight = active_chain_tip.nHeight + 1;
 166  
 167      // BIP113 requires that time-locked transactions have nLockTime set to
 168      // less than the median time of the previous block they're contained in.
 169      // When the next block is created its previous block will be the current
 170      // chain tip, so we use that to calculate the median time passed to
 171      // IsFinalTx().
 172      const int64_t nBlockTime{active_chain_tip.GetMedianTimePast()};
 173  
 174      return IsFinalTx(tx, nBlockHeight, nBlockTime);
 175  }
 176  
 177  namespace {
 178  /**
 179   * A helper which calculates heights of inputs of a given transaction.
 180   *
 181   * @param[in] tip    The current chain tip. If an input belongs to a mempool
 182   *                   transaction, we assume it will be confirmed in the next block.
 183   * @param[in] coins  Any CCoinsView that provides access to the relevant coins.
 184   * @param[in] tx     The transaction being evaluated.
 185   *
 186   * @returns A vector of input heights or nullopt, in case of an error.
 187   */
 188  std::optional<std::vector<int>> CalculatePrevHeights(
 189      const CBlockIndex& tip,
 190      const CCoinsView& coins,
 191      const CTransaction& tx)
 192  {
 193      std::vector<int> prev_heights;
 194      prev_heights.resize(tx.vin.size());
 195      for (size_t i = 0; i < tx.vin.size(); ++i) {
 196          if (auto coin{coins.GetCoin(tx.vin[i].prevout)}) {
 197              prev_heights[i] = coin->nHeight == MEMPOOL_HEIGHT
 198                                ? tip.nHeight + 1 // Assume all mempool transaction confirm in the next block.
 199                                : coin->nHeight;
 200          } else {
 201              LogPrintf("ERROR: %s: Missing input %d in transaction \'%s\'\n", __func__, i, tx.GetHash().GetHex());
 202              return std::nullopt;
 203          }
 204      }
 205      return prev_heights;
 206  }
 207  } // namespace
 208  
 209  std::optional<LockPoints> CalculateLockPointsAtTip(
 210      CBlockIndex* tip,
 211      const CCoinsView& coins_view,
 212      const CTransaction& tx)
 213  {
 214      assert(tip);
 215  
 216      auto prev_heights{CalculatePrevHeights(*tip, coins_view, tx)};
 217      if (!prev_heights.has_value()) return std::nullopt;
 218  
 219      CBlockIndex next_tip;
 220      next_tip.pprev = tip;
 221      // When SequenceLocks() is called within ConnectBlock(), the height
 222      // of the block *being* evaluated is what is used.
 223      // Thus if we want to know if a transaction can be part of the
 224      // *next* block, we need to use one more than active_chainstate.m_chain.Height()
 225      next_tip.nHeight = tip->nHeight + 1;
 226      const auto [min_height, min_time] = CalculateSequenceLocks(tx, STANDARD_LOCKTIME_VERIFY_FLAGS, prev_heights.value(), next_tip);
 227  
 228      // Also store the hash of the block with the highest height of
 229      // all the blocks which have sequence locked prevouts.
 230      // This hash needs to still be on the chain
 231      // for these LockPoint calculations to be valid
 232      // Note: It is impossible to correctly calculate a maxInputBlock
 233      // if any of the sequence locked inputs depend on unconfirmed txs,
 234      // except in the special case where the relative lock time/height
 235      // is 0, which is equivalent to no sequence lock. Since we assume
 236      // input height of tip+1 for mempool txs and test the resulting
 237      // min_height and min_time from CalculateSequenceLocks against tip+1.
 238      int max_input_height{0};
 239      for (const int height : prev_heights.value()) {
 240          // Can ignore mempool inputs since we'll fail if they had non-zero locks
 241          if (height != next_tip.nHeight) {
 242              max_input_height = std::max(max_input_height, height);
 243          }
 244      }
 245  
 246      // tip->GetAncestor(max_input_height) should never return a nullptr
 247      // because max_input_height is always less than the tip height.
 248      // It would, however, be a bad bug to continue execution, since a
 249      // LockPoints object with the maxInputBlock member set to nullptr
 250      // signifies no relative lock time.
 251      return LockPoints{min_height, min_time, Assert(tip->GetAncestor(max_input_height))};
 252  }
 253  
 254  bool CheckSequenceLocksAtTip(CBlockIndex* tip,
 255                               const LockPoints& lock_points)
 256  {
 257      assert(tip != nullptr);
 258  
 259      CBlockIndex index;
 260      index.pprev = tip;
 261      // CheckSequenceLocksAtTip() uses active_chainstate.m_chain.Height()+1 to evaluate
 262      // height based locks because when SequenceLocks() is called within
 263      // ConnectBlock(), the height of the block *being*
 264      // evaluated is what is used.
 265      // Thus if we want to know if a transaction can be part of the
 266      // *next* block, we need to use one more than active_chainstate.m_chain.Height()
 267      index.nHeight = tip->nHeight + 1;
 268  
 269      return EvaluateSequenceLocks(index, {lock_points.height, lock_points.time});
 270  }
 271  
 272  // Returns the script flags which should be checked for a given block
 273  static unsigned int GetBlockScriptFlags(const CBlockIndex& block_index, const ChainstateManager& chainman);
 274  
 275  /** Compute accurate total signature operation cost of a transaction.
 276   *  Not consensus-critical, since legacy sigops counting is always used in the protocol.
 277   */
 278  int64_t GetAccurateTransactionSigOpCost(const CTransaction& tx, const CCoinsViewCache& inputs, int flags)
 279  {
 280      if (tx.IsCoinBase()) {
 281          return 0;
 282      }
 283  
 284      unsigned int nSigOps = 0;
 285      for (const auto& txin : tx.vin) {
 286          nSigOps += txin.scriptSig.GetSigOpCount(false);
 287      }
 288  
 289      if (flags & SCRIPT_VERIFY_P2SH) {
 290          nSigOps += GetP2SHSigOpCount(tx, inputs);
 291      }
 292  
 293      nSigOps *= WITNESS_SCALE_FACTOR;
 294  
 295      if (flags & SCRIPT_VERIFY_WITNESS) {
 296          for (const auto& txin : tx.vin) {
 297              const Coin& coin = inputs.AccessCoin(txin.prevout);
 298              assert(!coin.IsSpent());
 299              const CTxOut &prevout = coin.out;
 300              nSigOps += CountWitnessSigOps(txin.scriptSig, prevout.scriptPubKey, &txin.scriptWitness, flags);
 301          }
 302      }
 303  
 304      return nSigOps;
 305  }
 306  
 307  void LimitMempoolSize(CTxMemPool& pool, CCoinsViewCache& coins_cache)
 308      EXCLUSIVE_LOCKS_REQUIRED(::cs_main, pool.cs)
 309  {
 310      AssertLockHeld(::cs_main);
 311      AssertLockHeld(pool.cs);
 312      int expired = pool.Expire(GetTime<std::chrono::seconds>() - pool.m_opts.expiry);
 313      if (expired != 0) {
 314          LogDebug(BCLog::MEMPOOL, "Expired %i transactions from the memory pool\n", expired);
 315      }
 316  
 317      std::vector<COutPoint> vNoSpendsRemaining;
 318      pool.TrimToSize(pool.m_opts.max_size_bytes, &vNoSpendsRemaining);
 319      for (const COutPoint& removed : vNoSpendsRemaining)
 320          coins_cache.Uncache(removed);
 321  }
 322  
 323  static bool IsCurrentForFeeEstimation(Chainstate& active_chainstate) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
 324  {
 325      AssertLockHeld(cs_main);
 326      if (active_chainstate.m_chainman.IsInitialBlockDownload()) {
 327          return false;
 328      }
 329      if (active_chainstate.m_chain.Tip()->GetBlockTime() < count_seconds(GetTime<std::chrono::seconds>() - MAX_FEE_ESTIMATION_TIP_AGE))
 330          return false;
 331      if (active_chainstate.m_chain.Height() < active_chainstate.m_chainman.m_best_header->nHeight - 1) {
 332          return false;
 333      }
 334      return true;
 335  }
 336  
 337  void Chainstate::MaybeUpdateMempoolForReorg(
 338      DisconnectedBlockTransactions& disconnectpool,
 339      bool fAddToMempool)
 340  {
 341      if (!m_mempool) return;
 342  
 343      AssertLockHeld(cs_main);
 344      AssertLockHeld(m_mempool->cs);
 345      std::vector<uint256> vHashUpdate;
 346      {
 347          // disconnectpool is ordered so that the front is the most recently-confirmed
 348          // transaction (the last tx of the block at the tip) in the disconnected chain.
 349          // Iterate disconnectpool in reverse, so that we add transactions
 350          // back to the mempool starting with the earliest transaction that had
 351          // been previously seen in a block.
 352          const auto queuedTx = disconnectpool.take();
 353          auto it = queuedTx.rbegin();
 354          while (it != queuedTx.rend()) {
 355              // ignore validation errors in resurrected transactions
 356              if (!fAddToMempool || (*it)->IsCoinBase() ||
 357                  AcceptToMemoryPool(*this, *it, GetTime(),
 358                      /*bypass_limits=*/true, /*test_accept=*/false).m_result_type !=
 359                          MempoolAcceptResult::ResultType::VALID) {
 360                  // If the transaction doesn't make it in to the mempool, remove any
 361                  // transactions that depend on it (which would now be orphans).
 362                  m_mempool->removeRecursive(**it, MemPoolRemovalReason::REORG);
 363              } else if (m_mempool->exists(GenTxid::Txid((*it)->GetHash()))) {
 364                  vHashUpdate.push_back((*it)->GetHash());
 365              }
 366              ++it;
 367          }
 368      }
 369  
 370      // AcceptToMemoryPool/addNewTransaction all assume that new mempool entries have
 371      // no in-mempool children, which is generally not true when adding
 372      // previously-confirmed transactions back to the mempool.
 373      // UpdateTransactionsFromBlock finds descendants of any transactions in
 374      // the disconnectpool that were added back and cleans up the mempool state.
 375      m_mempool->UpdateTransactionsFromBlock(vHashUpdate);
 376  
 377      // Predicate to use for filtering transactions in removeForReorg.
 378      // Checks whether the transaction is still final and, if it spends a coinbase output, mature.
 379      // Also updates valid entries' cached LockPoints if needed.
 380      // If false, the tx is still valid and its lockpoints are updated.
 381      // If true, the tx would be invalid in the next block; remove this entry and all of its descendants.
 382      // Note that TRUC rules are not applied here, so reorgs may cause violations of TRUC inheritance or
 383      // topology restrictions.
 384      const auto filter_final_and_mature = [&](CTxMemPool::txiter it)
 385          EXCLUSIVE_LOCKS_REQUIRED(m_mempool->cs, ::cs_main) {
 386          AssertLockHeld(m_mempool->cs);
 387          AssertLockHeld(::cs_main);
 388          const CTransaction& tx = it->GetTx();
 389  
 390          // The transaction must be final.
 391          if (!CheckFinalTxAtTip(*Assert(m_chain.Tip()), tx)) return true;
 392  
 393          const LockPoints& lp = it->GetLockPoints();
 394          // CheckSequenceLocksAtTip checks if the transaction will be final in the next block to be
 395          // created on top of the new chain.
 396          if (TestLockPointValidity(m_chain, lp)) {
 397              if (!CheckSequenceLocksAtTip(m_chain.Tip(), lp)) {
 398                  return true;
 399              }
 400          } else {
 401              const CCoinsViewMemPool view_mempool{&CoinsTip(), *m_mempool};
 402              const std::optional<LockPoints> new_lock_points{CalculateLockPointsAtTip(m_chain.Tip(), view_mempool, tx)};
 403              if (new_lock_points.has_value() && CheckSequenceLocksAtTip(m_chain.Tip(), *new_lock_points)) {
 404                  // Now update the mempool entry lockpoints as well.
 405                  it->UpdateLockPoints(*new_lock_points);
 406              } else {
 407                  return true;
 408              }
 409          }
 410  
 411          // If the transaction spends any coinbase outputs, it must be mature.
 412          if (it->GetSpendsCoinbase()) {
 413              for (const CTxIn& txin : tx.vin) {
 414                  if (m_mempool->exists(GenTxid::Txid(txin.prevout.hash))) continue;
 415                  const Coin& coin{CoinsTip().AccessCoin(txin.prevout)};
 416                  assert(!coin.IsSpent());
 417                  const auto mempool_spend_height{m_chain.Tip()->nHeight + 1};
 418                  if (coin.IsCoinBase() && mempool_spend_height - coin.nHeight < COINBASE_MATURITY) {
 419                      return true;
 420                  }
 421              }
 422          }
 423          // Transaction is still valid and cached LockPoints are updated.
 424          return false;
 425      };
 426  
 427      // We also need to remove any now-immature transactions
 428      m_mempool->removeForReorg(m_chain, filter_final_and_mature);
 429      // Re-limit mempool size, in case we added any transactions
 430      LimitMempoolSize(*m_mempool, this->CoinsTip());
 431  }
 432  
 433  /**
 434  * Checks to avoid mempool polluting consensus critical paths since cached
 435  * signature and script validity results will be reused if we validate this
 436  * transaction again during block validation.
 437  * */
 438  static bool CheckInputsFromMempoolAndCache(const CTransaction& tx, TxValidationState& state,
 439                  const CCoinsViewCache& view, const CTxMemPool& pool,
 440                  unsigned int flags, PrecomputedTransactionData& txdata, CCoinsViewCache& coins_tip,
 441                  ValidationCache& validation_cache)
 442                  EXCLUSIVE_LOCKS_REQUIRED(cs_main, pool.cs)
 443  {
 444      AssertLockHeld(cs_main);
 445      AssertLockHeld(pool.cs);
 446  
 447      assert(!tx.IsCoinBase());
 448      for (const CTxIn& txin : tx.vin) {
 449          const Coin& coin = view.AccessCoin(txin.prevout);
 450  
 451          // This coin was checked in PreChecks and MemPoolAccept
 452          // has been holding cs_main since then.
 453          Assume(!coin.IsSpent());
 454          if (coin.IsSpent()) return false;
 455  
 456          // If the Coin is available, there are 2 possibilities:
 457          // it is available in our current ChainstateActive UTXO set,
 458          // or it's a UTXO provided by a transaction in our mempool.
 459          // Ensure the scriptPubKeys in Coins from CoinsView are correct.
 460          const CTransactionRef& txFrom = pool.get(txin.prevout.hash);
 461          if (txFrom) {
 462              assert(txFrom->GetHash() == txin.prevout.hash);
 463              assert(txFrom->vout.size() > txin.prevout.n);
 464              assert(txFrom->vout[txin.prevout.n] == coin.out);
 465          } else {
 466              const Coin& coinFromUTXOSet = coins_tip.AccessCoin(txin.prevout);
 467              assert(!coinFromUTXOSet.IsSpent());
 468              assert(coinFromUTXOSet.out == coin.out);
 469          }
 470      }
 471  
 472      // Call CheckInputScripts() to cache signature and script validity against current tip consensus rules.
 473      return CheckInputScripts(tx, state, view, flags, /* cacheSigStore= */ true, /* cacheFullScriptStore= */ true, txdata, validation_cache);
 474  }
 475  
 476  namespace {
 477  
 478  class MemPoolAccept
 479  {
 480  public:
 481      explicit MemPoolAccept(CTxMemPool& mempool, Chainstate& active_chainstate) :
 482          m_pool(mempool),
 483          m_view(&m_dummy),
 484          m_viewmempool(&active_chainstate.CoinsTip(), m_pool),
 485          m_active_chainstate(active_chainstate)
 486      {
 487      }
 488  
 489      // We put the arguments we're handed into a struct, so we can pass them
 490      // around easier.
 491      struct ATMPArgs {
 492          const CChainParams& m_chainparams;
 493          const int64_t m_accept_time;
 494          const ignore_rejects_type& m_ignore_rejects;
 495          /*
 496           * Return any outpoints which were not previously present in the coins
 497           * cache, but were added as a result of validating the tx for mempool
 498           * acceptance. This allows the caller to optionally remove the cache
 499           * additions if the associated transaction ends up being rejected by
 500           * the mempool.
 501           */
 502          std::vector<COutPoint>& m_coins_to_uncache;
 503          /** When true, the transaction or package will not be submitted to the mempool. */
 504          const bool m_test_accept;
 505          /** Whether we allow transactions to replace mempool transactions. If false,
 506           * any transaction spending the same inputs as a transaction in the mempool is considered
 507           * a conflict. */
 508          const bool m_allow_replacement;
 509          /** When true, allow sibling eviction. This only occurs in single transaction package settings. */
 510          const bool m_allow_sibling_eviction;
 511          /** When true, the mempool will not be trimmed when any transactions are submitted in
 512           * Finalize(). Instead, limits should be enforced at the end to ensure the package is not
 513           * partially submitted.
 514           */
 515          const bool m_package_submission;
 516          /** When true, use package feerates instead of individual transaction feerates for fee-based
 517           * policies such as mempool min fee and min relay fee.
 518           */
 519          const bool m_package_feerates;
 520          /** Used for local submission of transactions to catch "absurd" fees
 521           * due to fee miscalculation by wallets. std:nullopt implies unset, allowing any feerates.
 522           * Any individual transaction failing this check causes immediate failure.
 523           */
 524          const std::optional<CFeeRate> m_client_maxfeerate;
 525  
 526          /** Whether CPFP carveout and RBF carveout are granted. */
 527          const bool m_allow_carveouts;
 528  
 529          /** Parameters for single transaction mempool validation. */
 530          static ATMPArgs SingleAccept(const CChainParams& chainparams, int64_t accept_time,
 531                                       const ignore_rejects_type& ignore_rejects, std::vector<COutPoint>& coins_to_uncache,
 532                                       bool test_accept) {
 533              return ATMPArgs{/* m_chainparams */ chainparams,
 534                              /* m_accept_time */ accept_time,
 535                              /* m_ignore_rejects */ ignore_rejects,
 536                              /* m_coins_to_uncache */ coins_to_uncache,
 537                              /* m_test_accept */ test_accept,
 538                              /* m_allow_replacement */ true,
 539                              /* m_allow_sibling_eviction */ true,
 540                              /* m_package_submission */ false,
 541                              /* m_package_feerates */ false,
 542                              /* m_client_maxfeerate */ {}, // checked by caller
 543                              /* m_allow_carveouts */ true,
 544              };
 545          }
 546  
 547          /** Parameters for test package mempool validation through testmempoolaccept. */
 548          static ATMPArgs PackageTestAccept(const CChainParams& chainparams, int64_t accept_time,
 549                                            const ignore_rejects_type& ignore_rejects, std::vector<COutPoint>& coins_to_uncache) {
 550              return ATMPArgs{/* m_chainparams */ chainparams,
 551                              /* m_accept_time */ accept_time,
 552                              /* m_ignore_rejects */ ignore_rejects,
 553                              /* m_coins_to_uncache */ coins_to_uncache,
 554                              /* m_test_accept */ true,
 555                              /* m_allow_replacement */ false,
 556                              /* m_allow_sibling_eviction */ false,
 557                              /* m_package_submission */ false, // not submitting to mempool
 558                              /* m_package_feerates */ false,
 559                              /* m_client_maxfeerate */ {}, // checked by caller
 560                              /* m_allow_carveouts */ false,
 561              };
 562          }
 563  
 564          /** Parameters for child-with-unconfirmed-parents package validation. */
 565          static ATMPArgs PackageChildWithParents(const CChainParams& chainparams, int64_t accept_time,
 566                                                  std::vector<COutPoint>& coins_to_uncache, const std::optional<CFeeRate>& client_maxfeerate, const ignore_rejects_type& ignore_rejects) {
 567              return ATMPArgs{/* m_chainparams */ chainparams,
 568                              /* m_accept_time */ accept_time,
 569                              /* m_ignore_rejects */ ignore_rejects,
 570                              /* m_coins_to_uncache */ coins_to_uncache,
 571                              /* m_test_accept */ false,
 572                              /* m_allow_replacement */ true,
 573                              /* m_allow_sibling_eviction */ false,
 574                              /* m_package_submission */ true,
 575                              /* m_package_feerates */ true,
 576                              /* m_client_maxfeerate */ client_maxfeerate,
 577                              /* m_allow_carveouts */ false,
 578              };
 579          }
 580  
 581          /** Parameters for a single transaction within a package. */
 582          static ATMPArgs SingleInPackageAccept(const ATMPArgs& package_args) {
 583              return ATMPArgs{/* m_chainparams */ package_args.m_chainparams,
 584                              /* m_accept_time */ package_args.m_accept_time,
 585                              empty_ignore_rejects,
 586                              /* m_coins_to_uncache */ package_args.m_coins_to_uncache,
 587                              /* m_test_accept */ package_args.m_test_accept,
 588                              /* m_allow_replacement */ true,
 589                              /* m_allow_sibling_eviction */ true,
 590                              /* m_package_submission */ true, // do not LimitMempoolSize in Finalize()
 591                              /* m_package_feerates */ false, // only 1 transaction
 592                              /* m_client_maxfeerate */ package_args.m_client_maxfeerate,
 593                              /* m_allow_carveouts */ false,
 594              };
 595          }
 596  
 597      private:
 598          // Private ctor to avoid exposing details to clients and allowing the possibility of
 599          // mixing up the order of the arguments. Use static functions above instead.
 600          ATMPArgs(const CChainParams& chainparams,
 601                   int64_t accept_time,
 602                   const ignore_rejects_type& ignore_rejects,
 603                   std::vector<COutPoint>& coins_to_uncache,
 604                   bool test_accept,
 605                   bool allow_replacement,
 606                   bool allow_sibling_eviction,
 607                   bool package_submission,
 608                   bool package_feerates,
 609                   std::optional<CFeeRate> client_maxfeerate,
 610                   bool allow_carveouts)
 611              : m_chainparams{chainparams},
 612                m_accept_time{accept_time},
 613                m_ignore_rejects{ignore_rejects},
 614                m_coins_to_uncache{coins_to_uncache},
 615                m_test_accept{test_accept},
 616                m_allow_replacement{allow_replacement},
 617                m_allow_sibling_eviction{allow_sibling_eviction},
 618                m_package_submission{package_submission},
 619                m_package_feerates{package_feerates},
 620                m_client_maxfeerate{client_maxfeerate},
 621                m_allow_carveouts{allow_carveouts}
 622          {
 623              // If we are using package feerates, we must be doing package submission.
 624              // It also means carveouts and sibling eviction are not permitted.
 625              if (m_package_feerates) {
 626                  Assume(m_package_submission);
 627                  Assume(!m_allow_carveouts);
 628                  Assume(!m_allow_sibling_eviction);
 629              }
 630              if (m_allow_sibling_eviction) Assume(m_allow_replacement);
 631          }
 632      };
 633  
 634      /** Clean up all non-chainstate coins from m_view and m_viewmempool. */
 635      void CleanupTemporaryCoins() EXCLUSIVE_LOCKS_REQUIRED(cs_main, m_pool.cs);
 636  
 637      // Single transaction acceptance
 638      MempoolAcceptResult AcceptSingleTransaction(const CTransactionRef& ptx, ATMPArgs& args) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
 639  
 640      /**
 641      * Multiple transaction acceptance. Transactions may or may not be interdependent, but must not
 642      * conflict with each other, and the transactions cannot already be in the mempool. Parents must
 643      * come before children if any dependencies exist.
 644      */
 645      PackageMempoolAcceptResult AcceptMultipleTransactions(const std::vector<CTransactionRef>& txns, ATMPArgs& args) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
 646  
 647      /**
 648       * Submission of a subpackage.
 649       * If subpackage size == 1, calls AcceptSingleTransaction() with adjusted ATMPArgs to avoid
 650       * package policy restrictions like no CPFP carve out (PackageMempoolChecks)
 651       * and creates a PackageMempoolAcceptResult wrapping the result.
 652       *
 653       * If subpackage size > 1, calls AcceptMultipleTransactions() with the provided ATMPArgs.
 654       *
 655       * Also cleans up all non-chainstate coins from m_view at the end.
 656      */
 657      PackageMempoolAcceptResult AcceptSubPackage(const std::vector<CTransactionRef>& subpackage, ATMPArgs& args)
 658          EXCLUSIVE_LOCKS_REQUIRED(cs_main, m_pool.cs);
 659  
 660      /**
 661       * Package (more specific than just multiple transactions) acceptance. Package must be a child
 662       * with all of its unconfirmed parents, and topologically sorted.
 663       */
 664      PackageMempoolAcceptResult AcceptPackage(const Package& package, ATMPArgs& args) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
 665  
 666  private:
 667      // All the intermediate state that gets passed between the various levels
 668      // of checking a given transaction.
 669      struct Workspace {
 670          explicit Workspace(const CTransactionRef& ptx) : m_ptx(ptx), m_hash(ptx->GetHash()) {}
 671          /** Txids of mempool transactions that this transaction directly conflicts with or may
 672           * replace via sibling eviction. */
 673          /** .second=true is a consensus conflict, and .second=false is a policy conflict. */
 674          std::map<Txid, bool> m_conflicts_incl_policy;
 675          /** Iterators to mempool entries that this transaction directly conflicts with or may
 676           * replace via sibling eviction. */
 677          CTxMemPool::setEntries m_iters_conflicting;
 678          /** All mempool ancestors of this transaction. */
 679          CTxMemPool::setEntries m_ancestors;
 680          /* Handle to the tx in the changeset */
 681          CTxMemPool::ChangeSet::TxHandle m_tx_handle;
 682          /** Whether RBF-related data structures (m_conflicts, m_iters_conflicting,
 683           * m_replaced_transactions) include a sibling in addition to txns with conflicting inputs. */
 684          bool m_sibling_eviction{false};
 685  
 686          /** Virtual size of the transaction as used by the mempool, calculated using serialized size
 687           * of the transaction and sigops. */
 688          int64_t m_vsize;
 689          /** Fees paid by this transaction: total input amounts subtracted by total output amounts. */
 690          CAmount m_base_fees;
 691          /** Base fees + any fee delta set by the user with prioritisetransaction. */
 692          CAmount m_modified_fees;
 693  
 694          /** If we're doing package validation (i.e. m_package_feerates=true), the "effective"
 695           * package feerate of this transaction is the total fees divided by the total size of
 696           * transactions (which may include its ancestors and/or descendants). */
 697          CFeeRate m_package_feerate{0};
 698  
 699          const CTransactionRef& m_ptx;
 700          /** Txid. */
 701          const Txid& m_hash;
 702          TxValidationState m_state;
 703          /** A temporary cache containing serialized transaction data for signature verification.
 704           * Reused across PolicyScriptChecks and ConsensusScriptChecks. */
 705          PrecomputedTransactionData m_precomputed_txdata;
 706      };
 707  
 708      static inline bool MaybeReject_(TxValidationResult reason, const std::string& reason_str, const std::string& debug_msg, const ignore_rejects_type& ignore_rejects, TxValidationState& state) {
 709          if (ignore_rejects.count(reason_str)) {
 710              return false;
 711          }
 712  
 713          state.Invalid(reason, reason_str, debug_msg);
 714          return true;
 715      }
 716  
 717  #define MaybeRejectDbg(reason, reason_str, debug_msg)  do {  \
 718      if (MaybeReject_(reason, reason_str, debug_msg, ignore_rejects, state)) {  \
 719          return false;  \
 720      }  \
 721  } while(0)
 722  
 723  #define MaybeReject(reason, reason_str)  MaybeRejectDbg(reason, reason_str, "")
 724  
 725      // Run the policy checks on a given transaction, excluding any script checks.
 726      // Looks up inputs, calculates feerate, considers replacement, evaluates
 727      // package limits, etc. As this function can be invoked for "free" by a peer,
 728      // only tests that are fast should be done here (to avoid CPU DoS).
 729      bool PreChecks(ATMPArgs& args, Workspace& ws) EXCLUSIVE_LOCKS_REQUIRED(cs_main, m_pool.cs);
 730  
 731      // Run checks for mempool replace-by-fee, only used in AcceptSingleTransaction.
 732      bool ReplacementChecks(ATMPArgs& args, Workspace& ws) EXCLUSIVE_LOCKS_REQUIRED(cs_main, m_pool.cs);
 733  
 734      // Enforce package mempool ancestor/descendant limits (distinct from individual
 735      // ancestor/descendant limits done in PreChecks) and run Package RBF checks.
 736      bool PackageMempoolChecks(const ATMPArgs& args, const std::vector<CTransactionRef>& txns,
 737                                std::vector<Workspace>& workspaces,
 738                                int64_t total_vsize,
 739                                PackageValidationState& package_state) EXCLUSIVE_LOCKS_REQUIRED(cs_main, m_pool.cs);
 740  
 741      // Run the script checks using our policy flags. As this can be slow, we should
 742      // only invoke this on transactions that have otherwise passed policy checks.
 743      bool PolicyScriptChecks(const ATMPArgs& args, Workspace& ws) EXCLUSIVE_LOCKS_REQUIRED(cs_main, m_pool.cs);
 744  
 745      // Re-run the script checks, using consensus flags, and try to cache the
 746      // result in the scriptcache. This should be done after
 747      // PolicyScriptChecks(). This requires that all inputs either be in our
 748      // utxo set or in the mempool.
 749      bool ConsensusScriptChecks(const ATMPArgs& args, Workspace& ws) EXCLUSIVE_LOCKS_REQUIRED(cs_main, m_pool.cs);
 750  
 751      // Try to add the transaction to the mempool, removing any conflicts first.
 752      void FinalizeSubpackage(const ATMPArgs& args) EXCLUSIVE_LOCKS_REQUIRED(cs_main, m_pool.cs);
 753  
 754      // Submit all transactions to the mempool and call ConsensusScriptChecks to add to the script
 755      // cache - should only be called after successful validation of all transactions in the package.
 756      // Does not call LimitMempoolSize(), so mempool max_size_bytes may be temporarily exceeded.
 757      bool SubmitPackage(const ATMPArgs& args, std::vector<Workspace>& workspaces, PackageValidationState& package_state,
 758                         std::map<Wtxid, MempoolAcceptResult>& results)
 759           EXCLUSIVE_LOCKS_REQUIRED(cs_main, m_pool.cs);
 760  
 761      // Compare a package's feerate against minimum allowed.
 762      bool CheckFeeRate(size_t package_size, CAmount package_fee, TxValidationState& state, const ignore_rejects_type& ignore_rejects) EXCLUSIVE_LOCKS_REQUIRED(::cs_main, m_pool.cs)
 763      {
 764          AssertLockHeld(::cs_main);
 765          AssertLockHeld(m_pool.cs);
 766          CAmount mempoolRejectFee = m_pool.GetMinFee().GetFee(package_size);
 767          if (mempoolRejectFee > 0 && package_fee < mempoolRejectFee && !ignore_rejects.count(rejectmsg_lowfee_mempool)) {
 768              return state.Invalid(TxValidationResult::TX_RECONSIDERABLE, "mempool min fee not met", strprintf("%d < %d", package_fee, mempoolRejectFee));
 769          }
 770  
 771          if (package_fee < m_pool.m_opts.min_relay_feerate.GetFee(package_size) && !ignore_rejects.count(rejectmsg_lowfee_relay)) {
 772              return state.Invalid(TxValidationResult::TX_RECONSIDERABLE, "min relay fee not met",
 773                                   strprintf("%d < %d", package_fee, m_pool.m_opts.min_relay_feerate.GetFee(package_size)));
 774          }
 775          return true;
 776      }
 777  
 778      ValidationCache& GetValidationCache()
 779      {
 780          return m_active_chainstate.m_chainman.m_validation_cache;
 781      }
 782  
 783  private:
 784      CTxMemPool& m_pool;
 785      CCoinsViewCache m_view;
 786      CCoinsViewMemPool m_viewmempool;
 787      CCoinsView m_dummy;
 788  
 789      Chainstate& m_active_chainstate;
 790  
 791      // Fields below are per *sub*package state and must be reset prior to subsequent
 792      // AcceptSingleTransaction and AcceptMultipleTransactions invocations
 793      struct SubPackageState {
 794          /** Aggregated modified fees of all transactions, used to calculate package feerate. */
 795          CAmount m_total_modified_fees{0};
 796          /** Aggregated virtual size of all transactions, used to calculate package feerate. */
 797          int64_t m_total_vsize{0};
 798  
 799          // RBF-related members
 800          /** Whether the transaction(s) would replace any mempool transactions and/or evict any siblings.
 801           * If so, RBF rules apply. */
 802          bool m_rbf{false};
 803          /** Mempool transactions that were replaced. */
 804          std::list<CTransactionRef> m_replaced_transactions;
 805          /* Changeset representing adding transactions and removing their conflicts. */
 806          std::unique_ptr<CTxMemPool::ChangeSet> m_changeset;
 807  
 808          /** Total modified fees of mempool transactions being replaced. */
 809          CAmount m_conflicting_fees{0};
 810          /** Total size (in virtual bytes) of mempool transactions being replaced. */
 811          size_t m_conflicting_size{0};
 812      };
 813  
 814      struct SubPackageState m_subpackage;
 815  
 816      /** Re-set sub-package state to not leak between evaluations */
 817      void ClearSubPackageState() EXCLUSIVE_LOCKS_REQUIRED(cs_main, m_pool.cs)
 818      {
 819          m_subpackage = SubPackageState{};
 820  
 821          // And clean coins while at it
 822          CleanupTemporaryCoins();
 823      }
 824  };
 825  
 826  bool MemPoolAccept::PreChecks(ATMPArgs& args, Workspace& ws)
 827  {
 828      AssertLockHeld(cs_main);
 829      AssertLockHeld(m_pool.cs);
 830      const CTransactionRef& ptx = ws.m_ptx;
 831      const CTransaction& tx = *ws.m_ptx;
 832      const Txid& hash = ws.m_hash;
 833  
 834      // Copy/alias what we need out of args
 835      const int64_t nAcceptTime = args.m_accept_time;
 836      const ignore_rejects_type& ignore_rejects = args.m_ignore_rejects;
 837      std::vector<COutPoint>& coins_to_uncache = args.m_coins_to_uncache;
 838  
 839      // Alias what we need out of ws
 840      TxValidationState& state = ws.m_state;
 841  
 842      if (!CheckTransaction(tx, state)) {
 843          return false; // state filled in by CheckTransaction
 844      }
 845  
 846      // Coinbase is only valid in a block, not as a loose transaction
 847      if (tx.IsCoinBase())
 848          return state.Invalid(TxValidationResult::TX_CONSENSUS, "coinbase");
 849  
 850      if (tx.version == TRUC_VERSION && m_pool.m_opts.truc_policy == TRUCPolicy::Reject) {
 851          return state.Invalid(TxValidationResult::TX_NOT_STANDARD, "version");
 852      }
 853  
 854      // Rather not work on nonstandard transactions (unless -testnet/-regtest)
 855      std::string reason;
 856  
 857      if (m_pool.m_opts.require_standard && !IsStandardTx(tx, m_pool.m_opts, reason, ignore_rejects)) {
 858          return state.Invalid(TxValidationResult::TX_NOT_STANDARD, reason);
 859      }
 860  
 861      // Transactions smaller than 65 non-witness bytes are not relayed to mitigate CVE-2017-12842.
 862      if (::GetSerializeSize(TX_NO_WITNESS(tx)) < MIN_STANDARD_TX_NONWITNESS_SIZE)
 863          MaybeReject(TxValidationResult::TX_NOT_STANDARD, "tx-size-small");
 864  
 865      // Only accept nLockTime-using transactions that can be mined in the next
 866      // block; we don't want our mempool filled up with transactions that can't
 867      // be mined yet.
 868      if (!CheckFinalTxAtTip(*Assert(m_active_chainstate.m_chain.Tip()), tx)) {
 869          MaybeReject(TxValidationResult::TX_PREMATURE_SPEND, "non-final");
 870      }
 871  
 872      if (m_pool.exists(GenTxid::Wtxid(tx.GetWitnessHash()))) {
 873          // Exact transaction already exists in the mempool.
 874          return state.Invalid(TxValidationResult::TX_CONFLICT, "txn-already-in-mempool");
 875      } else if (m_pool.exists(GenTxid::Txid(tx.GetHash()))) {
 876          // Transaction with the same non-witness data but different witness (same txid, different
 877          // wtxid) already exists in the mempool.
 878          return state.Invalid(TxValidationResult::TX_CONFLICT, "txn-same-nonwitness-data-in-mempool");
 879      }
 880  
 881      auto spk_reuse_mode = SpkReuseMode;
 882      if (ignore_rejects.count("txn-spk-reused")) {
 883          spk_reuse_mode = SRM_ALLOW;
 884      }
 885      SPKStates_t mapSPK;
 886  
 887      // Check for conflicts with in-memory transactions
 888      for (const CTxIn &txin : tx.vin)
 889      {
 890          const CTransaction* ptxConflicting = m_pool.GetConflictTx(txin.prevout);
 891          if (ptxConflicting) {
 892              if (!args.m_allow_replacement) {
 893                  // Transaction conflicts with a mempool tx, but we're not allowing replacements.
 894                  return state.Invalid(TxValidationResult::TX_MEMPOOL_POLICY, "bip125-replacement-disallowed");
 895              }
 896              if (!ws.m_conflicts_incl_policy.count(ptxConflicting->GetHash()))
 897              {
 898                  // Transactions that don't explicitly signal replaceability are
 899                  // *not* replaceable with the current logic, even if one of their
 900                  // unconfirmed ancestors signals replaceability. This diverges
 901                  // from BIP125's inherited signaling description (see CVE-2021-31876).
 902                  // Applications relying on first-seen mempool behavior should
 903                  // check all unconfirmed ancestors; otherwise an opt-in ancestor
 904                  // might be replaced, causing removal of this descendant.
 905                  //
 906                  // All TRUC transactions are considered replaceable.
 907                  //
 908                  // Replaceability signaling of the original transactions may be
 909                  // ignored due to node setting.
 910                  bool allow_rbf;
 911                  if (m_pool.m_opts.rbf_policy == RBFPolicy::Always || ignore_rejects.count("txn-mempool-conflict")) {
 912                      allow_rbf = true;
 913                  } else if (m_pool.m_opts.rbf_policy == RBFPolicy::Never) {
 914                      allow_rbf = false;
 915                  } else {
 916                      allow_rbf = SignalsOptInRBF(*ptxConflicting) || ptxConflicting->version == TRUC_VERSION;
 917                  }
 918                  if (!allow_rbf) {
 919                      return state.Invalid(TxValidationResult::TX_MEMPOOL_POLICY, "txn-mempool-conflict");
 920                  }
 921  
 922                  ws.m_conflicts_incl_policy.emplace(ptxConflicting->GetHash(), true);
 923              }
 924          }
 925      }
 926  
 927      if (spk_reuse_mode != SRM_ALLOW) {
 928          for (const CTxOut& txout : tx.vout) {
 929              uint160 hashSPK = ScriptHashkey(txout.scriptPubKey);
 930              const auto& SPKUsedIn = m_pool.mapUsedSPK.find(hashSPK);
 931              if (SPKUsedIn != m_pool.mapUsedSPK.end()) {
 932                  if (SPKUsedIn->second.first) {
 933                      ws.m_conflicts_incl_policy.emplace(SPKUsedIn->second.first->GetHash(), false);
 934                  }
 935                  if (SPKUsedIn->second.second) {
 936                      ws.m_conflicts_incl_policy.emplace(SPKUsedIn->second.second->GetHash(), false);
 937                  }
 938              }
 939              if (mapSPK.find(hashSPK) != mapSPK.end()) {
 940                  MaybeReject(TxValidationResult::TX_MEMPOOL_POLICY, "txn-spk-reused-twinoutputs");
 941              }
 942              mapSPK[hashSPK] = MemPool_SPK_State(mapSPK[hashSPK] | MSS_CREATED);
 943          }
 944      }
 945  
 946      m_view.SetBackend(m_viewmempool);
 947  
 948      const CCoinsViewCache& coins_cache = m_active_chainstate.CoinsTip();
 949      // do all inputs exist?
 950      for (const CTxIn& txin : tx.vin) {
 951          if (!coins_cache.HaveCoinInCache(txin.prevout)) {
 952              coins_to_uncache.push_back(txin.prevout);
 953          }
 954  
 955          // Note: this call may add txin.prevout to the coins cache
 956          // (coins_cache.cacheCoins) by way of FetchCoin(). It should be removed
 957          // later (via coins_to_uncache) if this tx turns out to be invalid.
 958          if (!m_view.HaveCoin(txin.prevout)) {
 959              // Are inputs missing because we already have the tx?
 960              for (size_t out = 0; out < tx.vout.size(); out++) {
 961                  // Optimistically just do efficient check of cache for outputs
 962                  if (coins_cache.HaveCoinInCache(COutPoint(hash, out))) {
 963                      return state.Invalid(TxValidationResult::TX_CONFLICT, "txn-already-known");
 964                  }
 965              }
 966              // Otherwise assume this might be an orphan tx for which we just haven't seen parents yet
 967              return state.Invalid(TxValidationResult::TX_MISSING_INPUTS, "bad-txns-inputs-missingorspent");
 968          }
 969      }
 970  
 971      // This is const, but calls into the back end CoinsViews. The CCoinsViewDB at the bottom of the
 972      // hierarchy brings the best block into scope. See CCoinsViewDB::GetBestBlock().
 973      m_view.GetBestBlock();
 974  
 975      // we have all inputs cached now, so switch back to dummy (to protect
 976      // against bugs where we pull more inputs from disk that miss being added
 977      // to coins_to_uncache)
 978      m_view.SetBackend(m_dummy);
 979  
 980      assert(m_active_chainstate.m_blockman.LookupBlockIndex(m_view.GetBestBlock()) == m_active_chainstate.m_chain.Tip());
 981  
 982      // Only accept BIP68 sequence locked transactions that can be mined in the next
 983      // block; we don't want our mempool filled up with transactions that can't
 984      // be mined yet.
 985      // Pass in m_view which has all of the relevant inputs cached. Note that, since m_view's
 986      // backend was removed, it no longer pulls coins from the mempool.
 987      const std::optional<LockPoints> lock_points{CalculateLockPointsAtTip(m_active_chainstate.m_chain.Tip(), m_view, tx)};
 988      // NOTE: The miner doesn't check this again, so for now it may not be overridden.
 989      if (!lock_points.has_value() || !CheckSequenceLocksAtTip(m_active_chainstate.m_chain.Tip(), *lock_points)) {
 990          return state.Invalid(TxValidationResult::TX_PREMATURE_SPEND, "non-BIP68-final");
 991      }
 992  
 993      // The mempool holds txs for the next block, so pass height+1 to CheckTxInputs
 994      const auto block_height_current = m_active_chainstate.m_chain.Height();
 995      const auto block_height_next = block_height_current + 1;
 996      const bool mempool_fork_active = IsForkActive(m_active_chainstate.m_chain.Tip(), Params().GetConsensus());
 997      if (mempool_fork_active) {
 998          // Delete taproot: reject transactions creating witness-v1 outputs
 999          // before the expensive checks (consensus ban is enforced in
1000          // ContextualCheckBlock; this is the mempool-side early reject).
1001          for (const auto& out : tx.vout) {
1002              int witver; std::vector<unsigned char> witprog;
1003              if (out.scriptPubKey.IsWitnessProgram(witver, witprog) && witver == 1) {
1004                  return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-taproot-output", "taproot outputs prohibited on the fork");
1005              }
1006          }
1007      }
1008      if (!Consensus::CheckTxInputs(tx, state, m_view, block_height_next, ws.m_base_fees,
1009          DeploymentActiveAfter(m_active_chainstate.m_chain.Tip(), Params().GetConsensus(), Consensus::DEPLOYMENT_REDUCED_DATA, m_active_chainstate.m_chainman.m_versionbitscache)
1010              ? CheckTxInputsRules::OutputSizeLimit : CheckTxInputsRules::None, Params().GetConsensus(),
1011          mempool_fork_active)) {
1012          return false; // state filled in by CheckTxInputs
1013      }
1014  
1015      if (m_pool.m_opts.minrelaymaturity) {
1016          auto max_coin_height = block_height_next - m_pool.m_opts.minrelaymaturity;
1017          static_assert(std::is_signed_v<decltype(max_coin_height)>, "Unsigned max_coin_height needs a range check");
1018          for (const CTxIn &txin : tx.vin) {
1019              const Coin &coin = m_view.AccessCoin(txin.prevout);
1020              if (coin.nHeight > max_coin_height) {
1021                  MaybeReject(TxValidationResult::TX_PREMATURE_SPEND, "bad-txns-input-immature-depth");
1022              }
1023          }
1024      }
1025  
1026      if (spk_reuse_mode != SRM_ALLOW) {
1027          for (const CTxIn& txin : tx.vin) {
1028              const Coin &coin = m_view.AccessCoin(txin.prevout);
1029              uint160 hashSPK = ScriptHashkey(coin.out.scriptPubKey);
1030  
1031              SPKStates_t::iterator mssit = mapSPK.find(hashSPK);
1032              if (mssit != mapSPK.end()) {
1033                  if (mssit->second & MSS_CREATED) {
1034                      MaybeReject(TxValidationResult::TX_MEMPOOL_POLICY, "txn-spk-reused-change");
1035                  }
1036              }
1037              const auto& SPKit = m_pool.mapUsedSPK.find(hashSPK);
1038              if (SPKit != m_pool.mapUsedSPK.end()) {
1039                  if (SPKit->second.second /* Spent */) {
1040                      ws.m_conflicts_incl_policy.emplace(SPKit->second.second->GetHash(), false);
1041                  }
1042              }
1043              mapSPK[hashSPK] = MemPool_SPK_State(mapSPK[hashSPK] | MSS_SPENT);
1044          }
1045      }
1046  
1047      if (m_pool.m_opts.require_standard && !AreInputsStandard(tx, m_view, m_pool.m_opts, "bad-txns-input-", reason, ignore_rejects)) {
1048          return state.Invalid(TxValidationResult::TX_INPUTS_NOT_STANDARD, reason);
1049      }
1050  
1051      if (m_pool.m_opts.datacarrier_fullcount || !m_pool.m_opts.accept_non_std_datacarrier) {
1052          const auto dcb = DatacarrierBytes(tx, m_view);
1053          if (dcb.second > 0 && !(m_pool.m_opts.accept_non_std_datacarrier || ignore_rejects.count("txn-datacarrier-nonstandard"))) {
1054              return state.Invalid(TxValidationResult::TX_INPUTS_NOT_STANDARD, "txn-datacarrier-nonstandard");
1055          }
1056          if (m_pool.m_opts.datacarrier_fullcount && (!ignore_rejects.count("txn-datacarrier-exceeded")) && dcb.first + dcb.second > m_pool.m_opts.max_datacarrier_bytes.value_or(0)) {
1057              return state.Invalid(TxValidationResult::TX_INPUTS_NOT_STANDARD, "txn-datacarrier-exceeded");
1058          }
1059      }
1060  
1061      // Check for non-standard witnesses.
1062      if (tx.HasWitness() && m_pool.m_opts.require_standard && !IsWitnessStandard(tx, m_view, "bad-witness-", reason, ignore_rejects, m_pool.m_opts.reject_taproot)) {
1063          return state.Invalid(TxValidationResult::TX_WITNESS_MUTATED, reason);
1064      }
1065  
1066      int64_t nSigOpsCost = GetTransactionSigOpCost(tx, m_view, STANDARD_SCRIPT_VERIFY_FLAGS);
1067  
1068      // Keep track of transactions that spend a coinbase, which we re-scan
1069      // during reorgs to ensure COINBASE_MATURITY is still met.
1070      bool fSpendsCoinbase = false;
1071      for (const CTxIn &txin : tx.vin) {
1072          const Coin &coin = m_view.AccessCoin(txin.prevout);
1073          if (coin.IsCoinBase()) {
1074              fSpendsCoinbase = true;
1075              break;
1076          }
1077      }
1078  
1079      // Since entries arrive *after* the tip's height, their priority is for the height+1
1080      const auto coin_age = GetCoinAge(tx, m_view, block_height_next);
1081  
1082      if (coin_age.inputs_coin_age < m_pool.m_opts.minrelaycoinblocks) {
1083          MaybeReject(TxValidationResult::TX_PREMATURE_SPEND, "bad-txns-input-immature-coinblocks");
1084      }
1085  
1086      // Set entry_sequence to 0 when rejectmsg_zero_mempool_entry_seq is used; this allows txs from a block
1087      // reorg to be marked earlier than any child txs that were already in the mempool.
1088      const uint64_t entry_sequence = args.m_ignore_rejects.count(rejectmsg_zero_mempool_entry_seq) ? 0 : m_pool.GetSequence();
1089      int32_t extra_weight = CalculateExtraTxWeight(*ptx, m_view, ::g_weight_per_data_byte);
1090      if (!m_subpackage.m_changeset) {
1091          m_subpackage.m_changeset = m_pool.GetChangeSet();
1092      }
1093      ws.m_tx_handle = m_subpackage.m_changeset->StageAddition(ptx, ws.m_base_fees, nAcceptTime, block_height_current, entry_sequence, coin_age, fSpendsCoinbase, /*extra_weight=*/ extra_weight, /*sigops_cost=*/ nSigOpsCost, lock_points.value());
1094  
1095      if (spk_reuse_mode != SRM_ALLOW) {
1096          m_subpackage.m_changeset->m_to_add.modify(ws.m_tx_handle, [=](CTxMemPoolEntry& e) {
1097              e.mapSPK = mapSPK;
1098          });
1099      }
1100  
1101      // ws.m_modified_fees includes any fee deltas from PrioritiseTransaction
1102      ws.m_modified_fees = ws.m_tx_handle->GetModifiedFee();
1103  
1104      ws.m_vsize = ws.m_tx_handle->GetTxSize();
1105  
1106      // Reduce effective fee by dust threshold for each sub-dust output
1107      if (m_pool.m_opts.subdustfeepenalty) {
1108          CAmount dust_penalty{0};
1109          for (const auto& txout : tx.vout) {
1110              const CAmount dust_threshold = GetDustThreshold(txout, m_pool.m_opts.dust_relay_feerate);
1111              if (txout.nValue < dust_threshold) {
1112                  dust_penalty = SaturatingAdd(dust_penalty, dust_threshold - txout.nValue);
1113              }
1114          }
1115          if (dust_penalty > 0) {
1116              m_subpackage.m_changeset->m_to_add.modify(ws.m_tx_handle, [&](CTxMemPoolEntry& e) {
1117                  e.UpdateModifiedFee(-dust_penalty);
1118              });
1119              ws.m_modified_fees = SaturatingAdd(ws.m_modified_fees, -dust_penalty);
1120          }
1121      }
1122  
1123      // Enforces 0-fee for dust transactions, no incentive to be mined alone
1124      if (m_pool.m_opts.require_standard && !ignore_rejects.count("dust")) {
1125          if (!PreCheckEphemeralTx(*ptx, m_pool.m_opts.dust_relay_feerate, ws.m_base_fees, ws.m_modified_fees, state)) {
1126              return false; // state filled in by PreCheckEphemeralTx
1127          }
1128      }
1129  
1130      // To avoid rejecting low-sigop bare-multisig transactions, the sigops
1131      // are counted a second time more accurately.
1132      if ((nSigOpsCost > MAX_STANDARD_TX_SIGOPS_COST) || (nBytesPerSigOpStrict && GetAccurateTransactionSigOpCost(tx, m_view, STANDARD_SCRIPT_VERIFY_FLAGS) > ws.m_vsize * WITNESS_SCALE_FACTOR / nBytesPerSigOpStrict)) {
1133          MaybeRejectDbg(TxValidationResult::TX_NOT_STANDARD, "bad-txns-too-many-sigops",
1134                  strprintf("%d", nSigOpsCost));
1135      }
1136  
1137      // No individual transactions are allowed below the min relay feerate except from disconnected blocks.
1138      // This requirement, unlike CheckFeeRate, cannot be bypassed using m_package_feerates because,
1139      // while a tx could be package CPFP'd when entering the mempool, we do not have a DoS-resistant
1140      // method of ensuring the tx remains bumped. For example, the fee-bumping child could disappear
1141      // due to a replacement.
1142      // The only exception is TRUC transactions.
1143      if ((ws.m_ptx->version != TRUC_VERSION || m_pool.m_opts.truc_policy != TRUCPolicy::Enforce) && ws.m_modified_fees < m_pool.m_opts.min_relay_feerate.GetFee(ws.m_vsize) && !args.m_ignore_rejects.count(rejectmsg_lowfee_relay)) {
1144          // Even though this is a fee-related failure, this result is TX_MEMPOOL_POLICY, not
1145          // TX_RECONSIDERABLE, because it cannot be bypassed using package validation.
1146          return state.Invalid(TxValidationResult::TX_MEMPOOL_POLICY, "min relay fee not met",
1147                               strprintf("%d < %d", ws.m_modified_fees, m_pool.m_opts.min_relay_feerate.GetFee(ws.m_vsize)));
1148      }
1149      // No individual transactions are allowed below the mempool min feerate except from disconnected
1150      // blocks and transactions in a package. Package transactions will be checked using package
1151      // feerate later.
1152      if (!args.m_package_feerates && !CheckFeeRate(ws.m_vsize, ws.m_modified_fees, state, args.m_ignore_rejects)) return false;
1153  
1154      std::set<Txid> conflicts_as_a_set;
1155      std::transform(ws.m_conflicts_incl_policy.begin(), ws.m_conflicts_incl_policy.end(),
1156                      std::inserter(conflicts_as_a_set, conflicts_as_a_set.end()),
1157                      [](const std::pair<Txid, bool>& pair){ return pair.first; });
1158      ws.m_iters_conflicting = m_pool.GetIterSet(conflicts_as_a_set);
1159  
1160      // Note that these modifications are only applicable to single transaction scenarios;
1161      // carve-outs are disabled for multi-transaction evaluations.
1162      CTxMemPool::Limits maybe_rbf_limits = m_pool.m_opts.limits;
1163  
1164      // Calculate in-mempool ancestors, up to a limit.
1165      if (ws.m_conflicts_incl_policy.size() == 1 && args.m_allow_carveouts) {
1166          // In general, when we receive an RBF transaction with mempool conflicts, we want to know whether we
1167          // would meet the chain limits after the conflicts have been removed. However, there isn't a practical
1168          // way to do this short of calculating the ancestor and descendant sets with an overlay cache of
1169          // changed mempool entries. Due to both implementation and runtime complexity concerns, this isn't
1170          // very realistic, thus we only ensure a limited set of transactions are RBF'able despite mempool
1171          // conflicts here. Importantly, we need to ensure that some transactions which were accepted using
1172          // the below carve-out are able to be RBF'ed, without impacting the security the carve-out provides
1173          // for off-chain contract systems (see link in the comment below).
1174          //
1175          // Specifically, the subset of RBF transactions which we allow despite chain limits are those which
1176          // conflict directly with exactly one other transaction (but may evict children of said transaction),
1177          // and which are not adding any new mempool dependencies. Note that the "no new mempool dependencies"
1178          // check is accomplished later, so we don't bother doing anything about it here, but if our
1179          // policy changes, we may need to move that check to here instead of removing it wholesale.
1180          //
1181          // Such transactions are clearly not merging any existing packages, so we are only concerned with
1182          // ensuring that (a) no package is growing past the package size (not count) limits and (b) we are
1183          // not allowing something to effectively use the (below) carve-out spot when it shouldn't be allowed
1184          // to.
1185          //
1186          // To check these we first check if we meet the RBF criteria, above, and increment the descendant
1187          // limits by the direct conflict and its descendants (as these are recalculated in
1188          // CalculateMempoolAncestors by assuming the new transaction being added is a new descendant, with no
1189          // removals, of each parent's existing dependent set). The ancestor count limits are unmodified (as
1190          // the ancestor limits should be the same for both our new transaction and any conflicts).
1191          // We don't bother incrementing m_limit_descendants by the full removal count as that limit never comes
1192          // into force here (as we're only adding a single transaction).
1193          assert(ws.m_iters_conflicting.size() == 1);
1194          CTxMemPool::txiter conflict = *ws.m_iters_conflicting.begin();
1195  
1196          maybe_rbf_limits.descendant_count += 1;
1197          maybe_rbf_limits.descendant_size_vbytes += conflict->GetSizeWithDescendants();
1198      }
1199  
1200      CTxMemPool::Limits limits;
1201      if (ignore_rejects.count("too-long-mempool-chain")) {
1202          limits = CTxMemPool::Limits::NoLimits();
1203      } else {
1204          limits = maybe_rbf_limits;
1205      }
1206      if (auto ancestors{m_subpackage.m_changeset->CalculateMemPoolAncestors(ws.m_tx_handle, limits)}) {
1207          ws.m_ancestors = std::move(*ancestors);
1208      } else {
1209          // If CalculateMemPoolAncestors fails second time, we want the original error string.
1210          const auto error_message{util::ErrorString(ancestors).original};
1211  
1212          // Carve-out is not allowed in this context; fail
1213          if (!args.m_allow_carveouts) {
1214              return state.Invalid(TxValidationResult::TX_MEMPOOL_POLICY, "too-long-mempool-chain", error_message);
1215          }
1216  
1217          // Contracting/payment channels CPFP carve-out:
1218          // If the new transaction is relatively small (up to 40k weight)
1219          // and has at most one ancestor (ie ancestor limit of 2, including
1220          // the new transaction), allow it if its parent has exactly the
1221          // descendant limit descendants. The transaction also cannot be TRUC,
1222          // as its topology restrictions do not allow a second child.
1223          //
1224          // This allows protocols which rely on distrusting counterparties
1225          // being able to broadcast descendants of an unconfirmed transaction
1226          // to be secure by simply only having two immediately-spendable
1227          // outputs - one for each counterparty. For more info on the uses for
1228          // this, see https://lists.linuxfoundation.org/pipermail/limenka-dev/2018-November/016518.html
1229          CTxMemPool::Limits cpfp_carve_out_limits{
1230              .ancestor_count = 2,
1231              .ancestor_size_vbytes = maybe_rbf_limits.ancestor_size_vbytes,
1232              .descendant_count = maybe_rbf_limits.descendant_count + 1,
1233              .descendant_size_vbytes = maybe_rbf_limits.descendant_size_vbytes + EXTRA_DESCENDANT_TX_SIZE_LIMIT,
1234          };
1235          if (ws.m_vsize > EXTRA_DESCENDANT_TX_SIZE_LIMIT || (ws.m_ptx->version == TRUC_VERSION && m_pool.m_opts.truc_policy == TRUCPolicy::Enforce)) {
1236              return state.Invalid(TxValidationResult::TX_MEMPOOL_POLICY, "too-long-mempool-chain", error_message);
1237          }
1238          if (auto ancestors_retry{m_subpackage.m_changeset->CalculateMemPoolAncestors(ws.m_tx_handle, cpfp_carve_out_limits)}) {
1239              ws.m_ancestors = std::move(*ancestors_retry);
1240          } else {
1241              return state.Invalid(TxValidationResult::TX_MEMPOOL_POLICY, "too-long-mempool-chain", error_message);
1242          }
1243      }
1244  
1245      // Even though just checking direct mempool parents for inheritance would be sufficient, we
1246      // check using the full ancestor set here because it's more convenient to use what we have
1247      // already calculated.
1248      if (m_pool.m_opts.truc_policy == TRUCPolicy::Enforce && !ignore_rejects.count("truc")) {
1249          if (const auto err{SingleTRUCChecks(ws.m_ptx, "truc-", reason, ignore_rejects, ws.m_ancestors, conflicts_as_a_set, ws.m_vsize)}) {
1250              // Single transaction contexts only.
1251              if (args.m_allow_sibling_eviction && err->second != nullptr) {
1252                  // We should only be considering where replacement is considered valid as well.
1253                  Assume(args.m_allow_replacement);
1254  
1255                  // Potential sibling eviction. Add the sibling to our list of mempool conflicts to be
1256                  // included in RBF checks.
1257                  ws.m_conflicts_incl_policy.emplace(err->second->GetHash(), false);
1258                  conflicts_as_a_set.insert(err->second->GetHash());
1259                  // Adding the sibling to m_iters_conflicting here means that it doesn't count towards
1260                  // RBF Carve Out above. This is correct, since removing to-be-replaced transactions from
1261                  // the descendant count is done separately in SingleTRUCChecks for TRUC transactions.
1262                  ws.m_iters_conflicting.insert(m_pool.GetIter(err->second->GetHash()).value());
1263                  ws.m_sibling_eviction = true;
1264                  // The sibling will be treated as part of the to-be-replaced set in ReplacementChecks.
1265                  // Note that we are not checking whether it opts in to replaceability via BIP125 or TRUC
1266                  // (which is normally done in PreChecks). However, the only way a TRUC transaction can
1267                  // have a non-TRUC and non-BIP125 descendant is due to a reorg.
1268              } else {
1269                  return state.Invalid(TxValidationResult::TX_MEMPOOL_POLICY, reason, err->first);
1270              }
1271          }
1272      }
1273  
1274      // A transaction that spends outputs that would be replaced by it is invalid. Now
1275      // that we have the set of all ancestors we can detect this
1276      // pathological case by making sure ws.m_conflicts and ws.m_ancestors don't
1277      // intersect.
1278      bool has_policy_conflict{false};
1279      if (const auto err_string{EntriesAndTxidsDisjoint(ws.m_ancestors, ws.m_conflicts_incl_policy, hash, &has_policy_conflict)}) {
1280          // We classify this as a consensus error because a transaction depending on something it
1281          // conflicts with would be inconsistent.
1282          return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-spends-conflicting-tx", *err_string);
1283      }
1284      if (has_policy_conflict) {
1285          MaybeReject(TxValidationResult::TX_MEMPOOL_POLICY, "txn-spk-reused-chained");
1286      }
1287  
1288      // We want to detect conflicts in any tx in a package to trigger package RBF logic
1289      m_subpackage.m_rbf |= !ws.m_conflicts_incl_policy.empty();
1290      return true;
1291  }
1292  
1293  bool MemPoolAccept::ReplacementChecks(ATMPArgs& args, Workspace& ws)
1294  {
1295      AssertLockHeld(cs_main);
1296      AssertLockHeld(m_pool.cs);
1297  
1298      const CTransaction& tx = *ws.m_ptx;
1299      const uint256& hash = ws.m_hash;
1300      TxValidationState& state = ws.m_state;
1301  
1302      CFeeRate newFeeRate(ws.m_modified_fees, ws.m_vsize);
1303      // Enforce Rule #6. The replacement transaction must have a higher feerate than its direct conflicts.
1304      // - The motivation for this check is to ensure that the replacement transaction is preferable for
1305      //   block-inclusion, compared to what would be removed from the mempool.
1306      // - This logic predates ancestor feerate-based transaction selection, which is why it doesn't
1307      //   consider feerates of descendants.
1308      // - Note: Ancestor feerate-based transaction selection has made this comparison insufficient to
1309      //   guarantee that this is incentive-compatible for miners, because it is possible for a
1310      //   descendant transaction of a direct conflict to pay a higher feerate than the transaction that
1311      //   might replace them, under these rules.
1312      if (!args.m_ignore_rejects.count("insufficient fee")) {
1313      if (const auto err_string{PaysMoreThanConflicts(ws.m_iters_conflicting, newFeeRate, hash)}) {
1314          // This fee-related failure is TX_RECONSIDERABLE because validating in a package may change
1315          // the result.
1316          return state.Invalid(TxValidationResult::TX_RECONSIDERABLE,
1317                               strprintf("insufficient fee%s", ws.m_sibling_eviction ? " (including sibling eviction)" : ""), *err_string);
1318      }
1319      }  // ignore_rejects
1320  
1321      CTxMemPool::setEntries all_conflicts;
1322  
1323      // Calculate all conflicting entries and enforce Rule #5.
1324      if (const auto err_string{GetEntriesForConflicts(tx, m_pool, ws.m_iters_conflicting, all_conflicts, args.m_ignore_rejects)}) {
1325          return state.Invalid(TxValidationResult::TX_MEMPOOL_POLICY,
1326                               strprintf("too many potential replacements%s", ws.m_sibling_eviction ? " (including sibling eviction)" : ""), *err_string);
1327      }
1328      // Enforce Rule #2.
1329      if (!args.m_ignore_rejects.count("replacement-adds-unconfirmed")) {
1330      if (const auto err_string{HasNoNewUnconfirmed(tx, m_pool, all_conflicts)}) {
1331          // Sibling eviction is only done for TRUC transactions, which cannot have multiple ancestors.
1332          Assume(!ws.m_sibling_eviction);
1333          return state.Invalid(TxValidationResult::TX_MEMPOOL_POLICY,
1334                               strprintf("replacement-adds-unconfirmed%s", ws.m_sibling_eviction ? " (including sibling eviction)" : ""), *err_string);
1335      }
1336      }  // ignore_rejects
1337  
1338      // Check if it's economically rational to mine this transaction rather than the ones it
1339      // replaces and pays for its own relay fees. Enforce Rules #3 and #4.
1340      for (CTxMemPool::txiter it : all_conflicts) {
1341          m_subpackage.m_conflicting_fees += it->GetModifiedFee();
1342          m_subpackage.m_conflicting_size += it->GetTxSize();
1343      }
1344      if (!args.m_ignore_rejects.count("insufficient fee")) {
1345      if (const auto err_string{PaysForRBF(m_subpackage.m_conflicting_fees, ws.m_modified_fees, ws.m_vsize,
1346                                           m_pool.m_opts.incremental_relay_feerate, hash)}) {
1347          // Result may change in a package context
1348          return state.Invalid(TxValidationResult::TX_RECONSIDERABLE,
1349                               strprintf("insufficient fee%s", ws.m_sibling_eviction ? " (including sibling eviction)" : ""), *err_string);
1350      }
1351      }  // ignore_rejects
1352  
1353      // Add all the to-be-removed transactions to the changeset.
1354      for (auto it : all_conflicts) {
1355          m_subpackage.m_changeset->StageRemoval(it);
1356      }
1357      return true;
1358  }
1359  
1360  bool MemPoolAccept::PackageMempoolChecks(const ATMPArgs& args, const std::vector<CTransactionRef>& txns,
1361                                           std::vector<Workspace>& workspaces,
1362                                           const int64_t total_vsize,
1363                                           PackageValidationState& package_state)
1364  {
1365      AssertLockHeld(cs_main);
1366      AssertLockHeld(m_pool.cs);
1367  
1368      // CheckPackageLimits expects the package transactions to not already be in the mempool.
1369      assert(std::all_of(txns.cbegin(), txns.cend(), [this](const auto& tx)
1370                         { return !m_pool.exists(GenTxid::Txid(tx->GetHash()));}));
1371  
1372      assert(txns.size() == workspaces.size());
1373  
1374      util::Result<void> result = [&]() EXCLUSIVE_LOCKS_REQUIRED(m_pool.cs) {
1375          if (args.m_ignore_rejects.count("package-mempool-limits")) {
1376              return util::Result<void>();
1377          } else {
1378              return m_pool.CheckPackageLimits(txns, total_vsize);
1379          }
1380      }();
1381      if (!result) {
1382          // This is a package-wide error, separate from an individual transaction error.
1383          return package_state.Invalid(PackageValidationResult::PCKG_POLICY, "package-mempool-limits", util::ErrorString(result).original);
1384      }
1385  
1386      // No conflicts means we're finished. Further checks are all RBF-only.
1387      if (!m_subpackage.m_rbf) return true;
1388  
1389      // We're in package RBF context; replacement proposal must be size 2
1390      if (workspaces.size() != 2 || !Assume(IsChildWithParents(txns))) {
1391          return package_state.Invalid(PackageValidationResult::PCKG_POLICY, "package RBF failed: package must be 1-parent-1-child");
1392      }
1393  
1394      // If the package has in-mempool ancestors, we won't consider a package RBF
1395      // since it would result in a cluster larger than 2.
1396      // N.B. To relax this constraint we will need to revisit how CCoinsViewMemPool::PackageAddTransaction
1397      // is being used inside AcceptMultipleTransactions to track available inputs while processing a package.
1398      for (const auto& ws : workspaces) {
1399          if (!ws.m_ancestors.empty()) {
1400              return package_state.Invalid(PackageValidationResult::PCKG_POLICY, "package RBF failed: new transaction cannot have mempool ancestors");
1401          }
1402      }
1403  
1404      // Aggregate all conflicts into one set.
1405      CTxMemPool::setEntries direct_conflict_iters;
1406      for (Workspace& ws : workspaces) {
1407          // Aggregate all conflicts into one set.
1408          direct_conflict_iters.merge(ws.m_iters_conflicting);
1409      }
1410  
1411      const auto& parent_ws = workspaces[0];
1412      const auto& child_ws = workspaces[1];
1413  
1414      // Don't consider replacements that would cause us to remove a large number of mempool entries.
1415      // This limit is not increased in a package RBF. Use the aggregate number of transactions.
1416      CTxMemPool::setEntries all_conflicts;
1417      if (const auto err_string{GetEntriesForConflicts(*child_ws.m_ptx, m_pool, direct_conflict_iters,
1418                                                       all_conflicts)}) {
1419          return package_state.Invalid(PackageValidationResult::PCKG_POLICY,
1420                                       "package RBF failed: too many potential replacements", *err_string);
1421      }
1422  
1423  
1424      for (CTxMemPool::txiter it : all_conflicts) {
1425          m_subpackage.m_changeset->StageRemoval(it);
1426          m_subpackage.m_conflicting_fees += it->GetModifiedFee();
1427          m_subpackage.m_conflicting_size += it->GetTxSize();
1428      }
1429  
1430      // Use the child as the transaction for attributing errors to.
1431      const Txid& child_hash = child_ws.m_ptx->GetHash();
1432      if (const auto err_string{PaysForRBF(/*original_fees=*/m_subpackage.m_conflicting_fees,
1433                                           /*replacement_fees=*/m_subpackage.m_total_modified_fees,
1434                                           /*replacement_vsize=*/m_subpackage.m_total_vsize,
1435                                           m_pool.m_opts.incremental_relay_feerate, child_hash)}) {
1436          return package_state.Invalid(PackageValidationResult::PCKG_POLICY,
1437                                       "package RBF failed: insufficient anti-DoS fees", *err_string);
1438      }
1439  
1440      // Ensure this two transaction package is a "chunk" on its own; we don't want the child
1441      // to be only paying anti-DoS fees
1442      const CFeeRate parent_feerate(parent_ws.m_modified_fees, parent_ws.m_vsize);
1443      const CFeeRate package_feerate(m_subpackage.m_total_modified_fees, m_subpackage.m_total_vsize);
1444      if (package_feerate <= parent_feerate) {
1445          return package_state.Invalid(PackageValidationResult::PCKG_POLICY,
1446                                       "package RBF failed: package feerate is less than or equal to parent feerate",
1447                                       strprintf("package feerate %s <= parent feerate is %s", package_feerate.ToString(), parent_feerate.ToString()));
1448      }
1449  
1450      // Check if it's economically rational to mine this package rather than the ones it replaces.
1451      // This takes the place of ReplacementChecks()'s PaysMoreThanConflicts() in the package RBF setting.
1452      if (const auto err_tup{ImprovesFeerateDiagram(*m_subpackage.m_changeset)}) {
1453          return package_state.Invalid(PackageValidationResult::PCKG_POLICY,
1454                                       "package RBF failed: " + err_tup.value().second, "");
1455      }
1456  
1457      LogDebug(BCLog::TXPACKAGES, "package RBF checks passed: parent %s (wtxid=%s), child %s (wtxid=%s), package hash (%s)\n",
1458          txns.front()->GetHash().ToString(), txns.front()->GetWitnessHash().ToString(),
1459          txns.back()->GetHash().ToString(), txns.back()->GetWitnessHash().ToString(),
1460          GetPackageHash(txns).ToString());
1461  
1462  
1463      return true;
1464  }
1465  
1466  unsigned int PolicyScriptVerifyFlags(const ignore_rejects_type& ignore_rejects)
1467  {
1468      if (ignore_rejects.empty()) {
1469          return STANDARD_SCRIPT_VERIFY_FLAGS;
1470      }
1471      if (ignore_rejects.count("non-mandatory-script-verify-flag")) {
1472          return MANDATORY_SCRIPT_VERIFY_FLAGS;
1473      }
1474  
1475      unsigned int flags = STANDARD_SCRIPT_VERIFY_FLAGS;
1476      if (ignore_rejects.count("non-mandatory-script-verify-flag-upgradable")) {
1477          constexpr unsigned int upgradable_policy_flags =
1478              SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS |
1479              SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM |
1480              SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_TAPROOT_VERSION |
1481              SCRIPT_VERIFY_DISCOURAGE_OP_SUCCESS |
1482              SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_PUBKEYTYPE;
1483          flags &= ~upgradable_policy_flags;
1484      } else {
1485          if (ignore_rejects.count("non-mandatory-script-verify-flag-upgradable-nops")) {
1486              flags &= ~SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS;
1487          }
1488          if (ignore_rejects.count("non-mandatory-script-verify-flag-upgradable-pubkeytype")) {
1489              flags &= ~SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_PUBKEYTYPE;
1490          }
1491      }
1492      if (ignore_rejects.count("non-mandatory-script-verify-flag-bip62")) {
1493          constexpr unsigned int bip62_policy_flags =
1494              SCRIPT_VERIFY_LOW_S |
1495              SCRIPT_VERIFY_SIGPUSHONLY |  // NOTE: not actually set ever
1496              SCRIPT_VERIFY_MINIMALDATA;
1497          flags &= ~bip62_policy_flags;
1498      } else {
1499          if (ignore_rejects.count("non-mandatory-script-verify-flag-low_s")) {
1500              flags &= ~SCRIPT_VERIFY_LOW_S;
1501          }
1502          if (ignore_rejects.count("non-mandatory-script-verify-flag-minimaldata")) {
1503              flags &= ~SCRIPT_VERIFY_MINIMALDATA;
1504          }
1505          if (ignore_rejects.count("non-mandatory-script-verify-flag-cleanstack")) {
1506              flags &= ~SCRIPT_VERIFY_CLEANSTACK;
1507          }
1508      }
1509      if (ignore_rejects.count("non-mandatory-script-verify-flag-strictenc")) {
1510          flags &= ~SCRIPT_VERIFY_STRICTENC;
1511      }
1512      if (ignore_rejects.count("non-mandatory-script-verify-flag-minimalif")) {
1513          flags &= ~SCRIPT_VERIFY_MINIMALIF;
1514      }
1515      if (ignore_rejects.count("non-mandatory-script-verify-flag-nullfail")) {
1516          flags &= ~SCRIPT_VERIFY_NULLFAIL;
1517      }
1518      if (ignore_rejects.count("non-mandatory-script-verify-flag-witness_pubkeytype")) {
1519          flags &= ~SCRIPT_VERIFY_WITNESS_PUBKEYTYPE;
1520      }
1521      if (ignore_rejects.count("non-mandatory-script-verify-flag-const_scriptcode")) {
1522          flags &= ~SCRIPT_VERIFY_CONST_SCRIPTCODE;
1523      }
1524      flags |= MANDATORY_SCRIPT_VERIFY_FLAGS;  // for safety
1525      return flags;
1526  }
1527  
1528  bool MemPoolAccept::PolicyScriptChecks(const ATMPArgs& args, Workspace& ws)
1529  {
1530      AssertLockHeld(cs_main);
1531      AssertLockHeld(m_pool.cs);
1532      const CTransaction& tx = *ws.m_ptx;
1533      TxValidationState& state = ws.m_state;
1534  
1535      unsigned int scriptVerifyFlags = PolicyScriptVerifyFlags(args.m_ignore_rejects);
1536      // Fork chain: match the tip's block flags so fork-activated rules
1537      // (P2BPCT witnesses) apply in the mempool identically to block
1538      // validation.
1539      if (const CBlockIndex* tip = m_active_chainstate.m_chain.Tip()) {
1540          if (IsForkActive(tip, Params().GetConsensus())) {
1541              scriptVerifyFlags |= SCRIPT_VERIFY_P2BPCT;
1542          }
1543      }
1544  
1545      // Check input scripts and signatures.
1546      // This is done last to help prevent CPU exhaustion denial-of-service attacks.
1547      if (!CheckInputScripts(tx, state, m_view, scriptVerifyFlags, true, false, ws.m_precomputed_txdata, GetValidationCache())) {
1548          // Detect a failure due to a missing witness so that p2p code can handle rejection caching appropriately.
1549          if (!tx.HasWitness() && SpendsNonAnchorWitnessProg(tx, m_view)) {
1550              state.Invalid(TxValidationResult::TX_WITNESS_STRIPPED,
1551                      state.GetRejectReason(), state.GetDebugMessage());
1552          }
1553          return false; // state filled in by CheckInputScripts
1554      }
1555  
1556      return true;
1557  }
1558  
1559  bool MemPoolAccept::ConsensusScriptChecks(const ATMPArgs& args, Workspace& ws)
1560  {
1561      AssertLockHeld(cs_main);
1562      AssertLockHeld(m_pool.cs);
1563      const CTransaction& tx = *ws.m_ptx;
1564      const uint256& hash = ws.m_hash;
1565      TxValidationState& state = ws.m_state;
1566  
1567      // Check again against the current block tip's script verification
1568      // flags to cache our script execution flags. This is, of course,
1569      // useless if the next block has different script flags from the
1570      // previous one, but because the cache tracks script flags for us it
1571      // will auto-invalidate and we'll just have a few blocks of extra
1572      // misses on soft-fork activation.
1573      //
1574      // This is also useful in case of bugs in the standard flags that cause
1575      // transactions to pass as valid when they're actually invalid. For
1576      // instance the STRICTENC flag was incorrectly allowing certain
1577      // CHECKSIG NOT scripts to pass, even though they were invalid.
1578      //
1579      // There is a similar check in CreateNewBlock() to prevent creating
1580      // invalid blocks (using TestBlockValidity), however allowing such
1581      // transactions into the mempool can be exploited as a DoS attack.
1582      unsigned int currentBlockScriptVerifyFlags{GetBlockScriptFlags(*m_active_chainstate.m_chain.Tip(), m_active_chainstate.m_chainman)};
1583      if (!CheckInputsFromMempoolAndCache(tx, state, m_view, m_pool, currentBlockScriptVerifyFlags,
1584                                          ws.m_precomputed_txdata, m_active_chainstate.CoinsTip(), GetValidationCache())) {
1585          LogError("BUG! PLEASE REPORT THIS! CheckInputScripts failed against latest-block but not STANDARD flags %s, %s", hash.ToString(), state.ToString());
1586          return Assume(false);
1587      }
1588  
1589      return true;
1590  }
1591  
1592  void MemPoolAccept::FinalizeSubpackage(const ATMPArgs& args)
1593  {
1594      AssertLockHeld(cs_main);
1595      AssertLockHeld(m_pool.cs);
1596  
1597      if (!m_subpackage.m_changeset->GetRemovals().empty()) Assume(args.m_allow_replacement);
1598      // Remove conflicting transactions from the mempool
1599      for (CTxMemPool::txiter it : m_subpackage.m_changeset->GetRemovals())
1600      {
1601          std::string log_string = strprintf("replacing mempool tx %s (wtxid=%s, fees=%s, vsize=%s). ",
1602                                        it->GetTx().GetHash().ToString(),
1603                                        it->GetTx().GetWitnessHash().ToString(),
1604                                        it->GetFee(),
1605                                        it->GetTxSize());
1606          FeeFrac feerate{m_subpackage.m_total_modified_fees, int32_t(m_subpackage.m_total_vsize)};
1607          uint256 tx_or_package_hash{};
1608          const bool replaced_with_tx{m_subpackage.m_changeset->GetTxCount() == 1};
1609          if (replaced_with_tx) {
1610              const CTransaction& tx = m_subpackage.m_changeset->GetAddedTxn(0);
1611              tx_or_package_hash = tx.GetHash();
1612              log_string += strprintf("New tx %s (wtxid=%s, fees=%s, vsize=%s)",
1613                                      tx.GetHash().ToString(),
1614                                      tx.GetWitnessHash().ToString(),
1615                                      feerate.fee,
1616                                      feerate.size);
1617          } else {
1618              tx_or_package_hash = GetPackageHash(m_subpackage.m_changeset->GetAddedTxns());
1619              log_string += strprintf("New package %s with %lu txs, fees=%s, vsize=%s",
1620                                      tx_or_package_hash.ToString(),
1621                                      m_subpackage.m_changeset->GetTxCount(),
1622                                      feerate.fee,
1623                                      feerate.size);
1624  
1625          }
1626          LogDebug(BCLog::MEMPOOL, "%s\n", log_string);
1627          TRACEPOINT(mempool, replaced,
1628                  it->GetTx().GetHash().data(),
1629                  it->GetTxSize(),
1630                  it->GetFee(),
1631                  std::chrono::duration_cast<std::chrono::duration<std::uint64_t>>(it->GetTime()).count(),
1632                  tx_or_package_hash.data(),
1633                  feerate.size,
1634                  feerate.fee,
1635                  replaced_with_tx
1636          );
1637          m_subpackage.m_replaced_transactions.push_back(it->GetSharedTx());
1638      }
1639      m_subpackage.m_changeset->Apply();
1640      m_subpackage.m_changeset.reset();
1641  }
1642  
1643  bool MemPoolAccept::SubmitPackage(const ATMPArgs& args, std::vector<Workspace>& workspaces,
1644                                    PackageValidationState& package_state,
1645                                    std::map<Wtxid, MempoolAcceptResult>& results)
1646  {
1647      AssertLockHeld(cs_main);
1648      AssertLockHeld(m_pool.cs);
1649      // Sanity check: none of the transactions should be in the mempool, and none of the transactions
1650      // should have a same-txid-different-witness equivalent in the mempool.
1651      assert(std::all_of(workspaces.cbegin(), workspaces.cend(), [this](const auto& ws){
1652          return !m_pool.exists(GenTxid::Txid(ws.m_ptx->GetHash())); }));
1653  
1654      bool all_submitted = true;
1655      FinalizeSubpackage(args);
1656      // ConsensusScriptChecks adds to the script cache and is therefore consensus-critical;
1657      // CheckInputsFromMempoolAndCache asserts that transactions only spend coins available from the
1658      // mempool or UTXO set. Submit each transaction to the mempool immediately after calling
1659      // ConsensusScriptChecks to make the outputs available for subsequent transactions.
1660      for (Workspace& ws : workspaces) {
1661          if (!ConsensusScriptChecks(args, ws)) {
1662              results.emplace(ws.m_ptx->GetWitnessHash(), MempoolAcceptResult::Failure(ws.m_state));
1663              // Since PolicyScriptChecks() passed, this should never fail.
1664              Assume(false);
1665              all_submitted = false;
1666              package_state.Invalid(PackageValidationResult::PCKG_MEMPOOL_ERROR,
1667                                    strprintf("BUG! PolicyScriptChecks succeeded but ConsensusScriptChecks failed: %s",
1668                                              ws.m_ptx->GetHash().ToString()));
1669              // Remove the transaction from the mempool.
1670              if (!m_subpackage.m_changeset) m_subpackage.m_changeset = m_pool.GetChangeSet();
1671              m_subpackage.m_changeset->StageRemoval(m_pool.GetIter(ws.m_ptx->GetHash()).value());
1672          }
1673      }
1674      if (!all_submitted) {
1675          Assume(m_subpackage.m_changeset);
1676          // This code should be unreachable; it's here as belt-and-suspenders
1677          // to try to ensure we have no consensus-invalid transactions in the
1678          // mempool.
1679          m_subpackage.m_changeset->Apply();
1680          m_subpackage.m_changeset.reset();
1681          return false;
1682      }
1683  
1684      std::vector<Wtxid> all_package_wtxids;
1685      all_package_wtxids.reserve(workspaces.size());
1686      std::transform(workspaces.cbegin(), workspaces.cend(), std::back_inserter(all_package_wtxids),
1687                     [](const auto& ws) { return ws.m_ptx->GetWitnessHash(); });
1688  
1689      if (!m_subpackage.m_replaced_transactions.empty()) {
1690          LogDebug(BCLog::MEMPOOL, "replaced %u mempool transactions with %u new one(s) for %s additional fees, %d delta bytes\n",
1691                   m_subpackage.m_replaced_transactions.size(), workspaces.size(),
1692                   m_subpackage.m_total_modified_fees - m_subpackage.m_conflicting_fees,
1693                   m_subpackage.m_total_vsize - static_cast<int>(m_subpackage.m_conflicting_size));
1694      }
1695  
1696      // Add successful results. The returned results may change later if LimitMempoolSize() evicts them.
1697      for (Workspace& ws : workspaces) {
1698          auto iter = m_pool.GetIter(ws.m_ptx->GetHash());
1699          Assume(iter.has_value());
1700          const auto effective_feerate = args.m_package_feerates ? ws.m_package_feerate :
1701              CFeeRate{ws.m_modified_fees, static_cast<uint32_t>(ws.m_vsize)};
1702          const auto effective_feerate_wtxids = args.m_package_feerates ? all_package_wtxids :
1703              std::vector<Wtxid>{ws.m_ptx->GetWitnessHash()};
1704          results.emplace(ws.m_ptx->GetWitnessHash(),
1705                          MempoolAcceptResult::Success(std::move(m_subpackage.m_replaced_transactions), ws.m_vsize,
1706                                           ws.m_base_fees, effective_feerate, effective_feerate_wtxids));
1707          if (!m_pool.m_opts.signals) continue;
1708          const CTransaction& tx = *ws.m_ptx;
1709          const auto tx_info = NewMempoolTransactionInfo(ws.m_ptx, ws.m_base_fees,
1710                                                         ws.m_vsize, (*iter)->GetHeight(),
1711                                                         args.m_ignore_rejects, args.m_package_submission,
1712                                                         IsCurrentForFeeEstimation(m_active_chainstate),
1713                                                         m_pool.HasNoInputsOf(tx));
1714          m_pool.m_opts.signals->TransactionAddedToMempool(tx_info, m_pool.GetAndIncrementSequence());
1715      }
1716      return all_submitted;
1717  }
1718  
1719  MempoolAcceptResult MemPoolAccept::AcceptSingleTransaction(const CTransactionRef& ptx, ATMPArgs& args)
1720  {
1721      AssertLockHeld(cs_main);
1722      LOCK(m_pool.cs); // mempool "read lock" (held through m_pool.m_opts.signals->TransactionAddedToMempool())
1723  
1724      Workspace ws(ptx);
1725      const std::vector<Wtxid> single_wtxid{ws.m_ptx->GetWitnessHash()};
1726  
1727      if (!PreChecks(args, ws)) {
1728          if (ws.m_state.GetResult() == TxValidationResult::TX_RECONSIDERABLE) {
1729              // Failed for fee reasons. Provide the effective feerate and which tx was included.
1730              return MempoolAcceptResult::FeeFailure(ws.m_state, CFeeRate(ws.m_modified_fees, ws.m_vsize), single_wtxid);
1731          }
1732          return MempoolAcceptResult::Failure(ws.m_state);
1733      }
1734  
1735      m_subpackage.m_total_vsize = ws.m_vsize;
1736      m_subpackage.m_total_modified_fees = ws.m_modified_fees;
1737  
1738      // Individual modified feerate exceeded caller-defined max; abort
1739      if (args.m_client_maxfeerate && CFeeRate(ws.m_modified_fees, ws.m_vsize) > args.m_client_maxfeerate.value()) {
1740          ws.m_state.Invalid(TxValidationResult::TX_MEMPOOL_POLICY, "max feerate exceeded", "");
1741          return MempoolAcceptResult::Failure(ws.m_state);
1742      }
1743  
1744      if (m_pool.m_opts.require_standard) {
1745          Wtxid dummy_wtxid;
1746          if (!CheckEphemeralSpends(/*package=*/{ptx}, m_pool.m_opts.dust_relay_feerate, m_pool, ws.m_state, dummy_wtxid)) {
1747              return MempoolAcceptResult::Failure(ws.m_state);
1748          }
1749      }
1750  
1751      if (m_subpackage.m_rbf && !ReplacementChecks(args, ws)) {
1752          if (ws.m_state.GetResult() == TxValidationResult::TX_RECONSIDERABLE) {
1753              // Failed for incentives-based fee reasons. Provide the effective feerate and which tx was included.
1754              return MempoolAcceptResult::FeeFailure(ws.m_state, CFeeRate(ws.m_modified_fees, ws.m_vsize), single_wtxid);
1755          }
1756          return MempoolAcceptResult::Failure(ws.m_state);
1757      }
1758  
1759      // Perform the inexpensive checks first and avoid hashing and signature verification unless
1760      // those checks pass, to mitigate CPU exhaustion denial-of-service attacks.
1761      if (!PolicyScriptChecks(args, ws)) return MempoolAcceptResult::Failure(ws.m_state);
1762  
1763      if (!ConsensusScriptChecks(args, ws)) return MempoolAcceptResult::Failure(ws.m_state);
1764  
1765      const CFeeRate effective_feerate{ws.m_modified_fees, static_cast<uint32_t>(ws.m_vsize)};
1766      // Tx was accepted, but not added
1767      if (args.m_test_accept) {
1768          return MempoolAcceptResult::Success(std::move(m_subpackage.m_replaced_transactions), ws.m_vsize,
1769                                              ws.m_base_fees, effective_feerate, single_wtxid);
1770      }
1771  
1772      FinalizeSubpackage(args);
1773  
1774      // Limit the mempool, if appropriate.
1775      if (!args.m_package_submission && !args.m_ignore_rejects.count(rejectmsg_mempoolfull)) {
1776          LimitMempoolSize(m_pool, m_active_chainstate.CoinsTip());
1777          if (!m_pool.exists(GenTxid::Txid(ws.m_hash))) {
1778              // The tx no longer meets our (new) mempool minimum feerate but could be reconsidered in a package.
1779              ws.m_state.Invalid(TxValidationResult::TX_RECONSIDERABLE, "mempool full");
1780              return MempoolAcceptResult::FeeFailure(ws.m_state, CFeeRate(ws.m_modified_fees, ws.m_vsize), {ws.m_ptx->GetWitnessHash()});
1781          }
1782      }
1783  
1784      if (m_pool.m_opts.signals) {
1785          const CTransaction& tx = *ws.m_ptx;
1786          auto iter = m_pool.GetIter(tx.GetHash());
1787          Assume(iter.has_value());
1788          const auto tx_info = NewMempoolTransactionInfo(ws.m_ptx, ws.m_base_fees,
1789                                                         ws.m_vsize, (*iter)->GetHeight(),
1790                                                         args.m_ignore_rejects, args.m_package_submission,
1791                                                         IsCurrentForFeeEstimation(m_active_chainstate),
1792                                                         m_pool.HasNoInputsOf(tx));
1793          m_pool.m_opts.signals->TransactionAddedToMempool(tx_info, m_pool.GetAndIncrementSequence());
1794      }
1795  
1796      if (!m_subpackage.m_replaced_transactions.empty()) {
1797          LogDebug(BCLog::MEMPOOL, "replaced %u mempool transactions with 1 new transaction for %s additional fees, %d delta bytes\n",
1798                   m_subpackage.m_replaced_transactions.size(),
1799                   ws.m_modified_fees - m_subpackage.m_conflicting_fees,
1800                   ws.m_vsize - static_cast<int>(m_subpackage.m_conflicting_size));
1801      }
1802  
1803      // update mempool stats cache
1804      const CFeeRate min_fee_rate = std::max(m_pool.GetMinFee(), m_pool.m_opts.min_relay_feerate);
1805      CStats::DefaultStats()->addMempoolSample(m_pool.size(), m_pool.DynamicMemoryUsage(), min_fee_rate.GetFeePerK());
1806  
1807      return MempoolAcceptResult::Success(std::move(m_subpackage.m_replaced_transactions), ws.m_vsize, ws.m_base_fees,
1808                                          effective_feerate, single_wtxid);
1809  }
1810  
1811  PackageMempoolAcceptResult MemPoolAccept::AcceptMultipleTransactions(const std::vector<CTransactionRef>& txns, ATMPArgs& args)
1812  {
1813      AssertLockHeld(cs_main);
1814  
1815      // These context-free package limits can be done before taking the mempool lock.
1816      PackageValidationState package_state;
1817      if (!IsWellFormedPackage(txns, package_state, /*require_sorted=*/true)) return PackageMempoolAcceptResult(package_state, {});
1818  
1819      std::vector<Workspace> workspaces{};
1820      workspaces.reserve(txns.size());
1821      std::transform(txns.cbegin(), txns.cend(), std::back_inserter(workspaces),
1822                     [](const auto& tx) { return Workspace(tx); });
1823      std::map<Wtxid, MempoolAcceptResult> results;
1824  
1825      LOCK(m_pool.cs);
1826  
1827      // Do all PreChecks first and fail fast to avoid running expensive script checks when unnecessary.
1828      for (Workspace& ws : workspaces) {
1829          if (!PreChecks(args, ws)) {
1830              package_state.Invalid(PackageValidationResult::PCKG_TX, "transaction failed");
1831              // Exit early to avoid doing pointless work. Update the failed tx result; the rest are unfinished.
1832              results.emplace(ws.m_ptx->GetWitnessHash(), MempoolAcceptResult::Failure(ws.m_state));
1833              return PackageMempoolAcceptResult(package_state, std::move(results));
1834          }
1835  
1836          // Individual modified feerate exceeded caller-defined max; abort
1837          // N.B. this doesn't take into account CPFPs. Chunk-aware validation may be more robust.
1838          if (args.m_client_maxfeerate && CFeeRate(ws.m_modified_fees, ws.m_vsize) > args.m_client_maxfeerate.value()) {
1839              // Need to set failure here both individually and at package level
1840              ws.m_state.Invalid(TxValidationResult::TX_MEMPOOL_POLICY, "max feerate exceeded", "");
1841              package_state.Invalid(PackageValidationResult::PCKG_TX, "transaction failed");
1842              // Exit early to avoid doing pointless work. Update the failed tx result; the rest are unfinished.
1843              results.emplace(ws.m_ptx->GetWitnessHash(), MempoolAcceptResult::Failure(ws.m_state));
1844              return PackageMempoolAcceptResult(package_state, std::move(results));
1845          }
1846  
1847          // Make the coins created by this transaction available for subsequent transactions in the
1848          // package to spend. If there are no conflicts within the package, no transaction can spend a coin
1849          // needed by another transaction in the package. We also need to make sure that no package
1850          // tx replaces (or replaces the ancestor of) the parent of another package tx. As long as we
1851          // check these two things, we don't need to track the coins spent.
1852          // If a package tx conflicts with a mempool tx, PackageMempoolChecks() ensures later that any package RBF attempt
1853          // has *no* in-mempool ancestors, so we don't have to worry about subsequent transactions in
1854          // same package spending the same in-mempool outpoints. This needs to be revisited for general
1855          // package RBF.
1856          m_viewmempool.PackageAddTransaction(ws.m_ptx);
1857      }
1858  
1859      // At this point we have all in-mempool ancestors, and we know every transaction's vsize.
1860      // Run the TRUC checks on the package.
1861      if (m_pool.m_opts.truc_policy == TRUCPolicy::Enforce) {
1862      std::string reason;
1863      for (Workspace& ws : workspaces) {
1864          if (auto err{PackageTRUCChecks(ws.m_ptx, ws.m_vsize, "truc-", reason, args.m_ignore_rejects, txns, ws.m_ancestors)}) {
1865              package_state.Invalid(PackageValidationResult::PCKG_POLICY, reason, err.value());
1866              return PackageMempoolAcceptResult(package_state, {});
1867          }
1868      }}
1869  
1870      // Transactions must meet two minimum feerates: the mempool minimum fee and min relay fee.
1871      // For transactions consisting of exactly one child and its parents, it suffices to use the
1872      // package feerate (total modified fees / total virtual size) to check this requirement.
1873      // Note that this is an aggregate feerate; this function has not checked that there are transactions
1874      // too low feerate to pay for themselves, or that the child transactions are higher feerate than
1875      // their parents. Using aggregate feerate may allow "parents pay for child" behavior and permit
1876      // a child that is below mempool minimum feerate. To avoid these behaviors, callers of
1877      // AcceptMultipleTransactions need to restrict txns topology (e.g. to ancestor sets) and check
1878      // the feerates of individuals and subsets.
1879      m_subpackage.m_total_vsize = std::accumulate(workspaces.cbegin(), workspaces.cend(), int64_t{0},
1880          [](int64_t sum, auto& ws) { return sum + ws.m_vsize; });
1881      m_subpackage.m_total_modified_fees = std::accumulate(workspaces.cbegin(), workspaces.cend(), CAmount{0},
1882          [](CAmount sum, auto& ws) { return sum + ws.m_modified_fees; });
1883      const CFeeRate package_feerate(m_subpackage.m_total_modified_fees, m_subpackage.m_total_vsize);
1884      std::vector<Wtxid> all_package_wtxids;
1885      all_package_wtxids.reserve(workspaces.size());
1886      std::transform(workspaces.cbegin(), workspaces.cend(), std::back_inserter(all_package_wtxids),
1887                     [](const auto& ws) { return ws.m_ptx->GetWitnessHash(); });
1888      TxValidationState placeholder_state;
1889      if (args.m_package_feerates &&
1890          (!args.m_ignore_rejects.count("package-fee-too-low")) &&
1891          !CheckFeeRate(m_subpackage.m_total_vsize, m_subpackage.m_total_modified_fees, placeholder_state, empty_ignore_rejects)) {
1892          package_state.Invalid(PackageValidationResult::PCKG_TX, "transaction failed");
1893          return PackageMempoolAcceptResult(package_state, {{workspaces.back().m_ptx->GetWitnessHash(),
1894              MempoolAcceptResult::FeeFailure(placeholder_state, CFeeRate(m_subpackage.m_total_modified_fees, m_subpackage.m_total_vsize), all_package_wtxids)}});
1895      }
1896  
1897      // Apply package mempool ancestor/descendant limits. Skip if there is only one transaction,
1898      // because it's unnecessary.
1899      if (txns.size() > 1 && !PackageMempoolChecks(args, txns, workspaces, m_subpackage.m_total_vsize, package_state)) {
1900          return PackageMempoolAcceptResult(package_state, std::move(results));
1901      }
1902  
1903      // Now that we've bounded the resulting possible ancestry count, check package for dust spends
1904      if (m_pool.m_opts.require_standard && !(args.m_ignore_rejects.count("dust") || args.m_ignore_rejects.count("unspent-dust") || args.m_ignore_rejects.count("missing-ephemeral-spends"))) {
1905          TxValidationState child_state;
1906          Wtxid child_wtxid;
1907          if (!CheckEphemeralSpends(txns, m_pool.m_opts.dust_relay_feerate, m_pool, child_state, child_wtxid)) {
1908              package_state.Invalid(PackageValidationResult::PCKG_TX, "unspent-dust");
1909              results.emplace(child_wtxid, MempoolAcceptResult::Failure(child_state));
1910              return PackageMempoolAcceptResult(package_state, std::move(results));
1911          }
1912      }
1913  
1914      for (Workspace& ws : workspaces) {
1915          ws.m_package_feerate = package_feerate;
1916          if (!PolicyScriptChecks(args, ws)) {
1917              // Exit early to avoid doing pointless work. Update the failed tx result; the rest are unfinished.
1918              package_state.Invalid(PackageValidationResult::PCKG_TX, "transaction failed");
1919              results.emplace(ws.m_ptx->GetWitnessHash(), MempoolAcceptResult::Failure(ws.m_state));
1920              return PackageMempoolAcceptResult(package_state, std::move(results));
1921          }
1922          if (args.m_test_accept) {
1923              const auto effective_feerate = args.m_package_feerates ? ws.m_package_feerate :
1924                  CFeeRate{ws.m_modified_fees, static_cast<uint32_t>(ws.m_vsize)};
1925              const auto effective_feerate_wtxids = args.m_package_feerates ? all_package_wtxids :
1926                  std::vector<Wtxid>{ws.m_ptx->GetWitnessHash()};
1927              results.emplace(ws.m_ptx->GetWitnessHash(),
1928                              MempoolAcceptResult::Success(std::move(m_subpackage.m_replaced_transactions),
1929                                                           ws.m_vsize, ws.m_base_fees, effective_feerate,
1930                                                           effective_feerate_wtxids));
1931          }
1932      }
1933  
1934      if (args.m_test_accept) return PackageMempoolAcceptResult(package_state, std::move(results));
1935  
1936      if (!SubmitPackage(args, workspaces, package_state, results)) {
1937          // PackageValidationState filled in by SubmitPackage().
1938          return PackageMempoolAcceptResult(package_state, std::move(results));
1939      }
1940  
1941      return PackageMempoolAcceptResult(package_state, std::move(results));
1942  }
1943  
1944  void MemPoolAccept::CleanupTemporaryCoins()
1945  {
1946      // There are 3 kinds of coins in m_view:
1947      // (1) Temporary coins from the transactions in subpackage, constructed by m_viewmempool.
1948      // (2) Mempool coins from transactions in the mempool, constructed by m_viewmempool.
1949      // (3) Confirmed coins fetched from our current UTXO set.
1950      //
1951      // (1) Temporary coins need to be removed, regardless of whether the transaction was submitted.
1952      // If the transaction was submitted to the mempool, m_viewmempool will be able to fetch them from
1953      // there. If it wasn't submitted to mempool, it is incorrect to keep them - future calls may try
1954      // to spend those coins that don't actually exist.
1955      // (2) Mempool coins also need to be removed. If the mempool contents have changed as a result
1956      // of submitting or replacing transactions, coins previously fetched from mempool may now be
1957      // spent or nonexistent. Those coins need to be deleted from m_view.
1958      // (3) Confirmed coins don't need to be removed. The chainstate has not changed (we are
1959      // holding cs_main and no blocks have been processed) so the confirmed tx cannot disappear like
1960      // a mempool tx can. The coin may now be spent after we submitted a tx to mempool, but
1961      // we have already checked that the package does not have 2 transactions spending the same coin.
1962      // Keeping them in m_view is an optimization to not re-fetch confirmed coins if we later look up
1963      // inputs for this transaction again.
1964      for (const auto& outpoint : m_viewmempool.GetNonBaseCoins()) {
1965          // In addition to resetting m_viewmempool, we also need to manually delete these coins from
1966          // m_view because it caches copies of the coins it fetched from m_viewmempool previously.
1967          m_view.Uncache(outpoint);
1968      }
1969      // This deletes the temporary and mempool coins.
1970      m_viewmempool.Reset();
1971  }
1972  
1973  PackageMempoolAcceptResult MemPoolAccept::AcceptSubPackage(const std::vector<CTransactionRef>& subpackage, ATMPArgs& args)
1974  {
1975      AssertLockHeld(::cs_main);
1976      AssertLockHeld(m_pool.cs);
1977      auto result = [&]() EXCLUSIVE_LOCKS_REQUIRED(::cs_main, m_pool.cs) {
1978          if (subpackage.size() > 1) {
1979              return AcceptMultipleTransactions(subpackage, args);
1980          }
1981          const auto& tx = subpackage.front();
1982          ATMPArgs single_args = ATMPArgs::SingleInPackageAccept(args);
1983          const auto single_res = AcceptSingleTransaction(tx, single_args);
1984          PackageValidationState package_state_wrapped;
1985          if (single_res.m_result_type != MempoolAcceptResult::ResultType::VALID) {
1986              package_state_wrapped.Invalid(PackageValidationResult::PCKG_TX, "transaction failed");
1987          }
1988          return PackageMempoolAcceptResult(package_state_wrapped, {{tx->GetWitnessHash(), single_res}});
1989      }();
1990  
1991      // Clean up m_view and m_viewmempool so that other subpackage evaluations don't have access to
1992      // coins they shouldn't. Keep some coins in order to minimize re-fetching coins from the UTXO set.
1993      // Clean up package feerate and rbf calculations
1994      ClearSubPackageState();
1995  
1996      return result;
1997  }
1998  
1999  PackageMempoolAcceptResult MemPoolAccept::AcceptPackage(const Package& package, ATMPArgs& args)
2000  {
2001      Assert(!package.empty());
2002      AssertLockHeld(cs_main);
2003      // Used if returning a PackageMempoolAcceptResult directly from this function.
2004      PackageValidationState package_state_quit_early;
2005  
2006      // There are two topologies we are able to handle through this function:
2007      // (1) A single transaction
2008      // (2) A child-with-unconfirmed-parents package.
2009      // Check that the package is well-formed. If it isn't, we won't try to validate any of the
2010      // transactions and thus won't return any MempoolAcceptResults, just a package-wide error.
2011  
2012      // Context-free package checks.
2013      if (!IsWellFormedPackage(package, package_state_quit_early, /*require_sorted=*/true)) {
2014          return PackageMempoolAcceptResult(package_state_quit_early, {});
2015      }
2016  
2017      if (package.size() > 1) {
2018          // All transactions in the package must be a parent of the last transaction. This is just an
2019          // opportunity for us to fail fast on a context-free check without taking the mempool lock.
2020          if (!IsChildWithParents(package)) {
2021              package_state_quit_early.Invalid(PackageValidationResult::PCKG_POLICY, "package-not-child-with-parents");
2022              return PackageMempoolAcceptResult(package_state_quit_early, {});
2023          }
2024  
2025          // IsChildWithParents() guarantees the package is > 1 transactions.
2026          assert(package.size() > 1);
2027          // The package must be 1 child with all of its unconfirmed parents. The package is expected to
2028          // be sorted, so the last transaction is the child.
2029          const auto& child = package.back();
2030          std::unordered_set<uint256, SaltedTxidHasher> unconfirmed_parent_txids;
2031          std::transform(package.cbegin(), package.cend() - 1,
2032                         std::inserter(unconfirmed_parent_txids, unconfirmed_parent_txids.end()),
2033                         [](const auto& tx) { return tx->GetHash(); });
2034  
2035          // All child inputs must refer to a preceding package transaction or a confirmed UTXO. The only
2036          // way to verify this is to look up the child's inputs in our current coins view (not including
2037          // mempool), and enforce that all parents not present in the package be available at chain tip.
2038          // Since this check can bring new coins into the coins cache, keep track of these coins and
2039          // uncache them if we don't end up submitting this package to the mempool.
2040          const CCoinsViewCache& coins_tip_cache = m_active_chainstate.CoinsTip();
2041          for (const auto& input : child->vin) {
2042              if (!coins_tip_cache.HaveCoinInCache(input.prevout)) {
2043                  args.m_coins_to_uncache.push_back(input.prevout);
2044              }
2045          }
2046          // Using the MemPoolAccept m_view cache allows us to look up these same coins faster later.
2047          // This should be connecting directly to CoinsTip, not to m_viewmempool, because we specifically
2048          // require inputs to be confirmed if they aren't in the package.
2049          m_view.SetBackend(m_active_chainstate.CoinsTip());
2050          const auto package_or_confirmed = [this, &unconfirmed_parent_txids](const auto& input) {
2051               return unconfirmed_parent_txids.count(input.prevout.hash) > 0 || m_view.HaveCoin(input.prevout);
2052          };
2053          if (!std::all_of(child->vin.cbegin(), child->vin.cend(), package_or_confirmed)) {
2054              package_state_quit_early.Invalid(PackageValidationResult::PCKG_POLICY, "package-not-child-with-unconfirmed-parents");
2055              return PackageMempoolAcceptResult(package_state_quit_early, {});
2056          }
2057          // Protect against bugs where we pull more inputs from disk that miss being added to
2058          // coins_to_uncache. The backend will be connected again when needed in PreChecks.
2059          m_view.SetBackend(m_dummy);
2060      }
2061  
2062      LOCK(m_pool.cs);
2063      // Stores results from which we will create the returned PackageMempoolAcceptResult.
2064      // A result may be changed if a mempool transaction is evicted later due to LimitMempoolSize().
2065      std::map<Wtxid, MempoolAcceptResult> results_final;
2066      // Results from individual validation which will be returned if no other result is available for
2067      // this transaction. "Nonfinal" because if a transaction fails by itself but succeeds later
2068      // (i.e. when evaluated with a fee-bumping child), the result in this map may be discarded.
2069      std::map<Wtxid, MempoolAcceptResult> individual_results_nonfinal;
2070      // Tracks whether we think package submission could result in successful entry to the mempool
2071      bool quit_early{false};
2072      std::vector<CTransactionRef> txns_package_eval;
2073      for (const auto& tx : package) {
2074          const auto& wtxid = tx->GetWitnessHash();
2075          const auto& txid = tx->GetHash();
2076          // There are 3 possibilities: already in mempool, same-txid-diff-wtxid already in mempool,
2077          // or not in mempool. An already confirmed tx is treated as one not in mempool, because all
2078          // we know is that the inputs aren't available.
2079          if (m_pool.exists(GenTxid::Wtxid(wtxid))) {
2080              // Exact transaction already exists in the mempool.
2081              // Node operators are free to set their mempool policies however they please, nodes may receive
2082              // transactions in different orders, and malicious counterparties may try to take advantage of
2083              // policy differences to pin or delay propagation of transactions. As such, it's possible for
2084              // some package transaction(s) to already be in the mempool, and we don't want to reject the
2085              // entire package in that case (as that could be a censorship vector). De-duplicate the
2086              // transactions that are already in the mempool, and only call AcceptMultipleTransactions() with
2087              // the new transactions. This ensures we don't double-count transaction counts and sizes when
2088              // checking ancestor/descendant limits, or double-count transaction fees for fee-related policy.
2089              const auto& entry{*Assert(m_pool.GetEntry(txid))};
2090              results_final.emplace(wtxid, MempoolAcceptResult::MempoolTx(entry.GetTxSize(), entry.GetFee()));
2091          } else if (m_pool.exists(GenTxid::Txid(txid))) {
2092              // Transaction with the same non-witness data but different witness (same txid,
2093              // different wtxid) already exists in the mempool.
2094              //
2095              // We don't allow replacement transactions right now, so just swap the package
2096              // transaction for the mempool one. Note that we are ignoring the validity of the
2097              // package transaction passed in.
2098              // TODO: allow witness replacement in packages.
2099              const auto& entry{*Assert(m_pool.GetEntry(txid))};
2100              // Provide the wtxid of the mempool tx so that the caller can look it up in the mempool.
2101              results_final.emplace(wtxid, MempoolAcceptResult::MempoolTxDifferentWitness(entry.GetTx().GetWitnessHash()));
2102          } else {
2103              // Transaction does not already exist in the mempool.
2104              // Try submitting the transaction on its own.
2105              const auto single_package_res = AcceptSubPackage({tx}, args);
2106              const auto& single_res = single_package_res.m_tx_results.at(wtxid);
2107              if (single_res.m_result_type == MempoolAcceptResult::ResultType::VALID) {
2108                  // The transaction succeeded on its own and is now in the mempool. Don't include it
2109                  // in package validation, because its fees should only be "used" once.
2110                  assert(m_pool.exists(GenTxid::Wtxid(wtxid)));
2111                  results_final.emplace(wtxid, single_res);
2112              } else if (package.size() == 1 || // If there is only one transaction, no need to retry it "as a package"
2113                         (single_res.m_state.GetResult() != TxValidationResult::TX_RECONSIDERABLE &&
2114                         single_res.m_state.GetResult() != TxValidationResult::TX_MISSING_INPUTS)) {
2115                  // Package validation policy only differs from individual policy in its evaluation
2116                  // of feerate. For example, if a transaction fails here due to violation of a
2117                  // consensus rule, the result will not change when it is submitted as part of a
2118                  // package. To minimize the amount of repeated work, unless the transaction fails
2119                  // due to feerate or missing inputs (its parent is a previous transaction in the
2120                  // package that failed due to feerate), don't run package validation. Note that this
2121                  // decision might not make sense if different types of packages are allowed in the
2122                  // future.  Continue individually validating the rest of the transactions, because
2123                  // some of them may still be valid.
2124                  quit_early = true;
2125                  package_state_quit_early.Invalid(PackageValidationResult::PCKG_TX, "transaction failed");
2126                  individual_results_nonfinal.emplace(wtxid, single_res);
2127              } else {
2128                  individual_results_nonfinal.emplace(wtxid, single_res);
2129                  txns_package_eval.push_back(tx);
2130              }
2131          }
2132      }
2133  
2134      auto multi_submission_result = quit_early || txns_package_eval.empty() ? PackageMempoolAcceptResult(package_state_quit_early, {}) :
2135          AcceptSubPackage(txns_package_eval, args);
2136      PackageValidationState& package_state_final = multi_submission_result.m_state;
2137  
2138      // This is invoked by AcceptSubPackage() already, so this is just here for
2139      // clarity (since it's not permitted to invoke LimitMempoolSize() while a
2140      // changeset is outstanding).
2141      ClearSubPackageState();
2142  
2143      // Make sure we haven't exceeded max mempool size.
2144      // Package transactions that were submitted to mempool or already in mempool may be evicted.
2145      LimitMempoolSize(m_pool, m_active_chainstate.CoinsTip());
2146  
2147      for (const auto& tx : package) {
2148          const auto& wtxid = tx->GetWitnessHash();
2149          if (multi_submission_result.m_tx_results.count(wtxid) > 0) {
2150              // We shouldn't have re-submitted if the tx result was already in results_final.
2151              Assume(results_final.count(wtxid) == 0);
2152              // If it was submitted, check to see if the tx is still in the mempool. It could have
2153              // been evicted due to LimitMempoolSize() above.
2154              const auto& txresult = multi_submission_result.m_tx_results.at(wtxid);
2155              if (txresult.m_result_type == MempoolAcceptResult::ResultType::VALID && !m_pool.exists(GenTxid::Wtxid(wtxid))) {
2156                  package_state_final.Invalid(PackageValidationResult::PCKG_TX, "transaction failed");
2157                  TxValidationState mempool_full_state;
2158                  mempool_full_state.Invalid(TxValidationResult::TX_MEMPOOL_POLICY, "mempool full");
2159                  results_final.emplace(wtxid, MempoolAcceptResult::Failure(mempool_full_state));
2160              } else {
2161                  results_final.emplace(wtxid, txresult);
2162              }
2163          } else if (const auto it{results_final.find(wtxid)}; it != results_final.end()) {
2164              // Already-in-mempool transaction. Check to see if it's still there, as it could have
2165              // been evicted when LimitMempoolSize() was called.
2166              Assume(it->second.m_result_type != MempoolAcceptResult::ResultType::INVALID);
2167              Assume(individual_results_nonfinal.count(wtxid) == 0);
2168              // Query by txid to include the same-txid-different-witness ones.
2169              if (!m_pool.exists(GenTxid::Txid(tx->GetHash()))) {
2170                  package_state_final.Invalid(PackageValidationResult::PCKG_TX, "transaction failed");
2171                  TxValidationState mempool_full_state;
2172                  mempool_full_state.Invalid(TxValidationResult::TX_MEMPOOL_POLICY, "mempool full");
2173                  // Replace the previous result.
2174                  results_final.erase(wtxid);
2175                  results_final.emplace(wtxid, MempoolAcceptResult::Failure(mempool_full_state));
2176              }
2177          } else if (const auto it{individual_results_nonfinal.find(wtxid)}; it != individual_results_nonfinal.end()) {
2178              Assume(it->second.m_result_type == MempoolAcceptResult::ResultType::INVALID);
2179              // Interesting result from previous processing.
2180              results_final.emplace(wtxid, it->second);
2181          }
2182      }
2183      Assume(results_final.size() == package.size());
2184      return PackageMempoolAcceptResult(package_state_final, std::move(results_final));
2185  }
2186  
2187  } // anon namespace
2188  
2189  MempoolAcceptResult AcceptToMemoryPool(Chainstate& active_chainstate, const CTransactionRef& tx,
2190                                         int64_t accept_time, const ignore_rejects_type& ignore_rejects, bool test_accept)
2191  {
2192      AssertLockHeld(::cs_main);
2193      const CChainParams& chainparams{active_chainstate.m_chainman.GetParams()};
2194      assert(active_chainstate.GetMempool() != nullptr);
2195      CTxMemPool& pool{*active_chainstate.GetMempool()};
2196  
2197      std::vector<COutPoint> coins_to_uncache;
2198      auto args = MemPoolAccept::ATMPArgs::SingleAccept(chainparams, accept_time, ignore_rejects, coins_to_uncache, test_accept);
2199      MempoolAcceptResult result = MemPoolAccept(pool, active_chainstate).AcceptSingleTransaction(tx, args);
2200      if (result.m_result_type != MempoolAcceptResult::ResultType::VALID) {
2201          // Remove coins that were not present in the coins cache before calling
2202          // AcceptSingleTransaction(); this is to prevent memory DoS in case we receive a large
2203          // number of invalid transactions that attempt to overrun the in-memory coins cache
2204          // (`CCoinsViewCache::cacheCoins`).
2205  
2206          for (const COutPoint& hashTx : coins_to_uncache)
2207              active_chainstate.CoinsTip().Uncache(hashTx);
2208          TRACEPOINT(mempool, rejected,
2209                  tx->GetHash().data(),
2210                  result.m_state.GetRejectReason().c_str()
2211          );
2212      }
2213      // After we've (potentially) uncached entries, ensure our coins cache is still within its size limits
2214      BlockValidationState state_dummy;
2215      active_chainstate.FlushStateToDisk(state_dummy, FlushStateMode::PERIODIC);
2216      return result;
2217  }
2218  
2219  PackageMempoolAcceptResult ProcessNewPackage(Chainstate& active_chainstate, CTxMemPool& pool,
2220                                                     const Package& package, bool test_accept, const std::optional<CFeeRate>& client_maxfeerate, const ignore_rejects_type& ignore_rejects)
2221  {
2222      AssertLockHeld(cs_main);
2223      assert(!package.empty());
2224      assert(std::all_of(package.cbegin(), package.cend(), [](const auto& tx){return tx != nullptr;}));
2225  
2226      std::vector<COutPoint> coins_to_uncache;
2227      const CChainParams& chainparams = active_chainstate.m_chainman.GetParams();
2228      auto result = [&]() EXCLUSIVE_LOCKS_REQUIRED(cs_main) {
2229          AssertLockHeld(cs_main);
2230          if (test_accept) {
2231              auto args = MemPoolAccept::ATMPArgs::PackageTestAccept(chainparams, GetTime(), ignore_rejects, coins_to_uncache);
2232              return MemPoolAccept(pool, active_chainstate).AcceptMultipleTransactions(package, args);
2233          } else {
2234              auto args = MemPoolAccept::ATMPArgs::PackageChildWithParents(chainparams, GetTime(), coins_to_uncache, client_maxfeerate, ignore_rejects);
2235              return MemPoolAccept(pool, active_chainstate).AcceptPackage(package, args);
2236          }
2237      }();
2238  
2239      // Uncache coins pertaining to transactions that were not submitted to the mempool.
2240      if (test_accept || result.m_state.IsInvalid()) {
2241          for (const COutPoint& hashTx : coins_to_uncache) {
2242              active_chainstate.CoinsTip().Uncache(hashTx);
2243          }
2244      }
2245      // Ensure the coins cache is still within limits.
2246      BlockValidationState state_dummy;
2247      active_chainstate.FlushStateToDisk(state_dummy, FlushStateMode::PERIODIC);
2248      return result;
2249  }
2250  
2251  CAmount GetBlockSubsidy(int nHeight, const Consensus::Params& consensusParams)
2252  {
2253      int halvings = nHeight / consensusParams.nSubsidyHalvingInterval;
2254      // Force block reward to zero when right shift is undefined.
2255      if (halvings >= 64)
2256          return 0;
2257  
2258      CAmount nSubsidy = 50 * COIN;
2259      // Subsidy is cut in half every 210,000 blocks which will occur approximately every 4 years.
2260      nSubsidy >>= halvings;
2261      return nSubsidy;
2262  }
2263  
2264  CoinsViews::CoinsViews(DBParams db_params, CoinsViewOptions options)
2265      : m_dbview{std::move(db_params), std::move(options)},
2266        m_catcherview(&m_dbview) {}
2267  
2268  void CoinsViews::InitCache()
2269  {
2270      AssertLockHeld(::cs_main);
2271      m_cacheview = std::make_unique<CCoinsViewCache>(&m_catcherview);
2272  }
2273  
2274  Chainstate::Chainstate(
2275      CTxMemPool* mempool,
2276      BlockManager& blockman,
2277      ChainstateManager& chainman,
2278      std::optional<uint256> from_snapshot_blockhash)
2279      : m_mempool(mempool),
2280        m_blockman(blockman),
2281        m_chainman(chainman),
2282        m_from_snapshot_blockhash(from_snapshot_blockhash) {}
2283  
2284  const CBlockIndex* Chainstate::SnapshotBase()
2285  {
2286      if (!m_from_snapshot_blockhash) return nullptr;
2287      if (!m_cached_snapshot_base) m_cached_snapshot_base = Assert(m_chainman.m_blockman.LookupBlockIndex(*m_from_snapshot_blockhash));
2288      return m_cached_snapshot_base;
2289  }
2290  
2291  void Chainstate::InitCoinsDB(
2292      size_t cache_size_bytes,
2293      bool in_memory,
2294      bool should_wipe,
2295      fs::path leveldb_name)
2296  {
2297      if (m_from_snapshot_blockhash) {
2298          leveldb_name += node::SNAPSHOT_CHAINSTATE_SUFFIX;
2299      }
2300  
2301      m_coins_views = std::make_unique<CoinsViews>(
2302          DBParams{
2303              .path = m_chainman.m_options.datadir / leveldb_name,
2304              .cache_bytes = cache_size_bytes,
2305              .memory_only = in_memory,
2306              .wipe_data = should_wipe,
2307              .obfuscate = true,
2308              .options = m_chainman.m_options.coins_db},
2309          m_chainman.m_options.coins_view);
2310  
2311      m_coinsdb_cache_size_bytes = cache_size_bytes;
2312  }
2313  
2314  void Chainstate::InitCoinsCache(size_t cache_size_bytes)
2315  {
2316      AssertLockHeld(::cs_main);
2317      assert(m_coins_views != nullptr);
2318      m_coinstip_cache_size_bytes = cache_size_bytes;
2319      m_coins_views->InitCache();
2320  }
2321  
2322  bool ChainstateManager::IsInitialBlockDownload() const
2323  {
2324      return !m_cached_finished_ibd.load(std::memory_order_relaxed);
2325  }
2326  
2327  bool ChainstateManager::UpdateIBDStatus()
2328  {
2329      if (m_cached_finished_ibd.load(std::memory_order_relaxed))
2330          return false;
2331      if (m_blockman.LoadingBlocks()) {
2332          return true;
2333      }
2334      CChain& chain{ActiveChain()};
2335      if (chain.Tip() == nullptr) {
2336          return true;
2337      }
2338      if (chain.Tip()->nChainWork < MinimumChainWork()) {
2339          return true;
2340      }
2341      if (chain.Tip()->Time() < Now<NodeSeconds>() - m_options.max_tip_age) {
2342          return true;
2343      }
2344      LogPrintf("Leaving InitialBlockDownload (latching to false)\n");
2345      m_cached_finished_ibd.store(true, std::memory_order_relaxed);
2346      return false;
2347  }
2348  
2349  void Chainstate::CheckForkWarningConditions()
2350  {
2351      AssertLockHeld(cs_main);
2352  
2353      // Before we get past initial download, we cannot reliably alert about forks
2354      // (we assume we don't get stuck on a fork before finishing our initial sync)
2355      // Also not applicable to the background chainstate
2356      if (m_chainman.IsInitialBlockDownload() || this->GetRole() == ChainstateRole::BACKGROUND) {
2357          return;
2358      }
2359  
2360      if (m_chainman.m_best_invalid && m_chainman.m_best_invalid->nChainWork > m_chain.Tip()->nChainWork + (GetBlockProof(*m_chain.Tip()) * 6)) {
2361          LogWarning("Found invalid chain at least ~6 blocks longer than our best chain. Chain state database corruption likely.");
2362          m_chainman.GetNotifications().warningSet(
2363              kernel::Warning::LARGE_WORK_INVALID_CHAIN,
2364              _("Warning: We do not appear to fully agree with our peers! You may need to upgrade, or other nodes may need to upgrade."));
2365      } else {
2366          m_chainman.GetNotifications().warningUnset(kernel::Warning::LARGE_WORK_INVALID_CHAIN);
2367      }
2368  }
2369  
2370  // Called both upon regular invalid block discovery *and* InvalidateBlock
2371  void Chainstate::InvalidChainFound(CBlockIndex* pindexNew)
2372  {
2373      AssertLockHeld(cs_main);
2374      if (!m_chainman.m_best_invalid || pindexNew->nChainWork > m_chainman.m_best_invalid->nChainWork) {
2375          m_chainman.m_best_invalid = pindexNew;
2376      }
2377      SetBlockFailureFlags(pindexNew);
2378      if (m_chainman.m_best_header != nullptr && m_chainman.m_best_header->GetAncestor(pindexNew->nHeight) == pindexNew) {
2379          m_chainman.RecalculateBestHeader();
2380      }
2381  
2382      LogPrintf("%s: invalid block=%s  height=%d  log2_work=%f  date=%s\n", __func__,
2383        pindexNew->GetBlockHash().ToString(), pindexNew->nHeight,
2384        log(pindexNew->nChainWork.getdouble())/log(2.0), FormatISO8601DateTime(pindexNew->GetBlockTime()));
2385      CBlockIndex *tip = m_chain.Tip();
2386      assert (tip);
2387      LogPrintf("%s:  current best=%s  height=%d  log2_work=%f  date=%s\n", __func__,
2388        tip->GetBlockHash().ToString(), m_chain.Height(), log(tip->nChainWork.getdouble())/log(2.0),
2389        FormatISO8601DateTime(tip->GetBlockTime()));
2390      CheckForkWarningConditions();
2391  }
2392  
2393  // Same as InvalidChainFound, above, except not called directly from InvalidateBlock,
2394  // which does its own setBlockIndexCandidates management.
2395  void Chainstate::InvalidBlockFound(CBlockIndex* pindex, const BlockValidationState& state)
2396  {
2397      AssertLockHeld(cs_main);
2398      if (state.GetResult() != BlockValidationResult::BLOCK_MUTATED) {
2399          pindex->nStatus |= BLOCK_FAILED_VALID;
2400          m_chainman.m_failed_blocks.insert(pindex);
2401          m_blockman.m_dirty_blockindex.insert(pindex);
2402          setBlockIndexCandidates.erase(pindex);
2403          InvalidChainFound(pindex);
2404      }
2405  }
2406  
2407  void UpdateCoins(const CTransaction& tx, CCoinsViewCache& inputs, CTxUndo &txundo, int nHeight)
2408  {
2409      // mark inputs spent
2410      if (!tx.IsCoinBase()) {
2411          txundo.vprevout.reserve(tx.vin.size());
2412          for (const CTxIn &txin : tx.vin) {
2413              txundo.vprevout.emplace_back();
2414              bool is_spent = inputs.SpendCoin(txin.prevout, &txundo.vprevout.back());
2415              assert(is_spent);
2416          }
2417      }
2418      // add outputs
2419      AddCoins(inputs, tx, nHeight);
2420  }
2421  
2422  std::optional<std::pair<ScriptError, std::string>> CScriptCheck::operator()() {
2423      const CScript &scriptSig = ptxTo->vin[nIn].scriptSig;
2424      const CScriptWitness *witness = &ptxTo->vin[nIn].scriptWitness;
2425      ScriptError error{SCRIPT_ERR_UNKNOWN_ERROR};
2426      if (VerifyScript(scriptSig, m_tx_out.scriptPubKey, witness, nFlags, CachingTransactionSignatureChecker(ptxTo, nIn, m_tx_out.nValue, cacheStore, *m_signature_cache, *txdata), &error)) {
2427          return std::nullopt;
2428      } else {
2429          auto debug_str = strprintf("input %i of %s (wtxid %s), spending %s:%i", nIn, ptxTo->GetHash().ToString(), ptxTo->GetWitnessHash().ToString(), ptxTo->vin[nIn].prevout.hash.ToString(), ptxTo->vin[nIn].prevout.n);
2430          return std::make_pair(error, std::move(debug_str));
2431      }
2432  }
2433  
2434  ValidationCache::ValidationCache(const size_t script_execution_cache_bytes, const size_t signature_cache_bytes)
2435      : m_signature_cache{signature_cache_bytes}
2436  {
2437      // Setup the salted hasher
2438      uint256 nonce = GetRandHash();
2439      // We want the nonce to be 64 bytes long to force the hasher to process
2440      // this chunk, which makes later hash computations more efficient. We
2441      // just write our 32-byte entropy twice to fill the 64 bytes.
2442      m_script_execution_cache_hasher.Write(nonce.begin(), 32);
2443      m_script_execution_cache_hasher.Write(nonce.begin(), 32);
2444  
2445      const auto [num_elems, approx_size_bytes] = m_script_execution_cache.setup_bytes(script_execution_cache_bytes);
2446      LogPrintf("Using %zu MiB out of %zu MiB requested for script execution cache, able to store %zu elements\n",
2447                approx_size_bytes >> 20, script_execution_cache_bytes >> 20, num_elems);
2448  }
2449  
2450  /**
2451   * Check whether all of this transaction's input scripts succeed.
2452   *
2453   * This involves ECDSA signature checks so can be computationally intensive. This function should
2454   * only be called after the cheap sanity checks in CheckTxInputs passed.
2455   *
2456   * WARNING: flags_per_input deviations from flags must be handled with care. It should only be more
2457   * relaxed than flags, never stricter (or a cached result could be wrong). Do not provide
2458   * flags_per_input if every input uses the same flags, or the result will not be cached.
2459   *
2460   * If pvChecks is not nullptr, script checks are pushed onto it instead of being performed inline. Any
2461   * script checks which are not necessary (eg due to script execution cache hits) are, obviously,
2462   * not pushed onto pvChecks/run.
2463   *
2464   * Setting cacheSigStore/cacheFullScriptStore to false will remove elements from the corresponding cache
2465   * which are matched. This is useful for checking blocks where we will likely never need the cache
2466   * entry again.
2467   *
2468   * Note that we may set state.reason to NOT_STANDARD for extra soft-fork flags in flags, block-checking
2469   * callers should probably reset it to CONSENSUS in such cases.
2470   *
2471   * Non-static (and redeclared) in src/test/txvalidationcache_tests.cpp
2472   */
2473  bool CheckInputScripts(const CTransaction& tx, TxValidationState& state,
2474                         const CCoinsViewCache& inputs, unsigned int flags, bool cacheSigStore,
2475                         bool cacheFullScriptStore, PrecomputedTransactionData& txdata,
2476                         ValidationCache& validation_cache,
2477                         std::vector<CScriptCheck>* pvChecks,
2478                         const std::vector<unsigned int>& flags_per_input)
2479  {
2480      if (tx.IsCoinBase()) return true;
2481  
2482      if (pvChecks) {
2483          pvChecks->reserve(tx.vin.size());
2484      }
2485  
2486      // First check if script executions have been cached with the same
2487      // flags. Note that this assumes that the inputs provided are
2488      // correct (ie that the transaction hash which is in tx's prevouts
2489      // properly commits to the scriptPubKey in the inputs view of that
2490      // transaction).
2491      uint256 hashCacheEntry;
2492      CSHA256 hasher = validation_cache.ScriptExecutionCacheHasher();
2493      hasher.Write(UCharCast(tx.GetWitnessHash().begin()), 32).Write((unsigned char*)&flags, sizeof(flags)).Finalize(hashCacheEntry.begin());
2494      AssertLockHeld(cs_main); //TODO: Remove this requirement by making CuckooCache not require external locks
2495      if (validation_cache.m_script_execution_cache.contains(hashCacheEntry, !cacheFullScriptStore)) {
2496          return true;
2497      }
2498  
2499      if (!txdata.m_spent_outputs_ready) {
2500          std::vector<CTxOut> spent_outputs;
2501          spent_outputs.reserve(tx.vin.size());
2502  
2503          for (const auto& txin : tx.vin) {
2504              const COutPoint& prevout = txin.prevout;
2505              const Coin& coin = inputs.AccessCoin(prevout);
2506              assert(!coin.IsSpent());
2507              spent_outputs.emplace_back(coin.out);
2508          }
2509          txdata.Init(tx, std::move(spent_outputs));
2510      }
2511      assert(txdata.m_spent_outputs.size() == tx.vin.size());
2512      assert(flags_per_input.empty() || flags_per_input.size() == tx.vin.size());
2513  
2514      for (unsigned int i = 0; i < tx.vin.size(); i++) {
2515          if (!flags_per_input.empty()) flags = flags_per_input[i];
2516  
2517          // We very carefully only pass in things to CScriptCheck which
2518          // are clearly committed to by tx' witness hash. This provides
2519          // a sanity check that our caching is not introducing consensus
2520          // failures through additional data in, eg, the coins being
2521          // spent being checked as a part of CScriptCheck.
2522  
2523          // Verify signature
2524          CScriptCheck check(txdata.m_spent_outputs[i], tx, validation_cache.m_signature_cache, i, flags, cacheSigStore, &txdata);
2525          if (pvChecks) {
2526              pvChecks->emplace_back(std::move(check));
2527          } else if (auto result = check(); result.has_value()) {
2528              // Tx failures never trigger disconnections/bans.
2529              // This is so that network splits aren't triggered
2530              // either due to non-consensus relay policies (such as
2531              // non-standard DER encodings or non-null dummy
2532              // arguments) or due to new consensus rules introduced in
2533              // soft forks.
2534              if (flags & STANDARD_NOT_MANDATORY_VERIFY_FLAGS) {
2535                  return state.Invalid(TxValidationResult::TX_NOT_STANDARD, strprintf("mempool-script-verify-flag-failed (%s)", ScriptErrorString(result->first)), result->second);
2536              } else {
2537                  return state.Invalid(TxValidationResult::TX_CONSENSUS, strprintf("mandatory-script-verify-flag-failed (%s)", ScriptErrorString(result->first)), result->second);
2538              }
2539          }
2540      }
2541  
2542      if (cacheFullScriptStore && (!pvChecks) && flags_per_input.empty()) {
2543          // We executed all of the provided scripts, and were told to
2544          // cache the result. Do so now.
2545          validation_cache.m_script_execution_cache.insert(hashCacheEntry);
2546      }
2547  
2548      return true;
2549  }
2550  
2551  bool FatalError(Notifications& notifications, BlockValidationState& state, const bilingual_str& message)
2552  {
2553      notifications.fatalError(message);
2554      return state.Error(message.original);
2555  }
2556  
2557  /**
2558   * Restore the UTXO in a Coin at a given COutPoint
2559   * @param undo The Coin to be restored.
2560   * @param view The coins view to which to apply the changes.
2561   * @param out The out point that corresponds to the tx input.
2562   * @return A DisconnectResult as an int
2563   */
2564  int ApplyTxInUndo(Coin&& undo, CCoinsViewCache& view, const COutPoint& out)
2565  {
2566      bool fClean = true;
2567  
2568      if (view.HaveCoin(out)) fClean = false; // overwriting transaction output
2569  
2570      if (undo.nHeight == 0) {
2571          // Missing undo metadata (height and coinbase). Older versions included this
2572          // information only in undo records for the last spend of a transactions'
2573          // outputs. This implies that it must be present for some other output of the same tx.
2574          const Coin& alternate = AccessByTxid(view, out.hash);
2575          if (!alternate.IsSpent()) {
2576              undo.nHeight = alternate.nHeight;
2577              undo.fCoinBase = alternate.fCoinBase;
2578          } else {
2579              return DISCONNECT_FAILED; // adding output for transaction without known metadata
2580          }
2581      }
2582      // If the coin already exists as an unspent coin in the cache, then the
2583      // possible_overwrite parameter to AddCoin must be set to true. We have
2584      // already checked whether an unspent coin exists above using HaveCoin, so
2585      // we don't need to guess. When fClean is false, an unspent coin already
2586      // existed and it is an overwrite.
2587      view.AddCoin(out, std::move(undo), !fClean);
2588  
2589      return fClean ? DISCONNECT_OK : DISCONNECT_UNCLEAN;
2590  }
2591  
2592  /** Undo the effects of this block (with given index) on the UTXO set represented by coins.
2593   *  When FAILED is returned, view is left in an indeterminate state. */
2594  DisconnectResult Chainstate::DisconnectBlock(const CBlock& block, const CBlockIndex* pindex, CCoinsViewCache& view)
2595  {
2596      AssertLockHeld(::cs_main);
2597      bool fClean = true;
2598  
2599      CBlockUndo blockUndo;
2600      if (!m_blockman.ReadBlockUndo(blockUndo, *pindex)) {
2601          LogError("DisconnectBlock(): failure reading undo data\n");
2602          return DISCONNECT_FAILED;
2603      }
2604  
2605      if (blockUndo.vtxundo.size() + 1 != block.vtx.size()) {
2606          LogError("DisconnectBlock(): block and undo data inconsistent\n");
2607          return DISCONNECT_FAILED;
2608      }
2609  
2610      // Ignore blocks that contain transactions which are 'overwritten' by later transactions,
2611      // unless those are already completely spent.
2612      // See https://github.com/limenka/limenka/issues/22596 for additional information.
2613      // Note: the blocks specified here are different than the ones used in ConnectBlock because DisconnectBlock
2614      // unwinds the blocks in reverse. As a result, the inconsistency is not discovered until the earlier
2615      // blocks with the duplicate coinbase transactions are disconnected.
2616      bool fEnforceBIP30 = !((pindex->nHeight==91722 && pindex->GetBlockHash() == uint256{"00000000000271a2dc26e7667f8419f2e15416dc6955e5a6c6cdf3f2574dd08e"}) ||
2617                             (pindex->nHeight==91812 && pindex->GetBlockHash() == uint256{"00000000000af0aed4792b1acee3d966af36cf5def14935db8de83d6f9306f2f"}));
2618  
2619      // undo transactions in reverse order
2620      for (int i = block.vtx.size() - 1; i >= 0; i--) {
2621          const CTransaction &tx = *(block.vtx[i]);
2622          Txid hash = tx.GetHash();
2623          bool is_coinbase = tx.IsCoinBase();
2624          bool is_bip30_exception = (is_coinbase && !fEnforceBIP30);
2625  
2626          // Check that all outputs are available and match the outputs in the block itself
2627          // exactly.
2628          for (size_t o = 0; o < tx.vout.size(); o++) {
2629              if (!tx.vout[o].scriptPubKey.IsUnspendable()) {
2630                  COutPoint out(hash, o);
2631                  Coin coin;
2632                  bool is_spent = view.SpendCoin(out, &coin);
2633                  if (!is_spent || tx.vout[o] != coin.out || pindex->nHeight != coin.nHeight || is_coinbase != coin.fCoinBase) {
2634                      if (!is_bip30_exception) {
2635                          fClean = false; // transaction output mismatch
2636                      }
2637                  }
2638              }
2639          }
2640  
2641          // restore inputs
2642          if (i > 0) { // not coinbases
2643              CTxUndo &txundo = blockUndo.vtxundo[i-1];
2644              if (txundo.vprevout.size() != tx.vin.size()) {
2645                  LogError("DisconnectBlock(): transaction and undo data inconsistent\n");
2646                  return DISCONNECT_FAILED;
2647              }
2648              for (unsigned int j = tx.vin.size(); j > 0;) {
2649                  --j;
2650                  const COutPoint& out = tx.vin[j].prevout;
2651                  int res = ApplyTxInUndo(std::move(txundo.vprevout[j]), view, out);
2652                  if (res == DISCONNECT_FAILED) return DISCONNECT_FAILED;
2653                  fClean = fClean && res != DISCONNECT_UNCLEAN;
2654              }
2655              // At this point, all of txundo.vprevout should have been moved out.
2656          }
2657      }
2658  
2659      // move best block pointer to prevout block
2660      view.SetBestBlock(pindex->pprev->GetBlockHash());
2661  
2662      return fClean ? DISCONNECT_OK : DISCONNECT_UNCLEAN;
2663  }
2664  
2665  /**
2666   * Threshold condition checker that triggers when unknown versionbits are seen on the network.
2667   */
2668  class WarningBitsConditionChecker : public AbstractThresholdConditionChecker
2669  {
2670  private:
2671      const ChainstateManager& m_chainman;
2672      int m_bit;
2673  
2674  public:
2675      explicit WarningBitsConditionChecker(const ChainstateManager& chainman, int bit) : m_chainman{chainman}, m_bit(bit) {}
2676  
2677      int64_t BeginTime(const Consensus::Params& params) const override { return 0; }
2678      int64_t EndTime(const Consensus::Params& params) const override { return std::numeric_limits<int64_t>::max(); }
2679      int Period(const Consensus::Params& params) const override { return params.nMinerConfirmationWindow; }
2680      int Threshold(const Consensus::Params& params) const override { return params.nRuleChangeActivationThreshold; }
2681  
2682      bool Condition(const CBlockIndex* pindex, const Consensus::Params& params) const override
2683      {
2684          return pindex->nHeight >= params.MinBIP9WarningHeight &&
2685                 ((pindex->nVersion & VERSIONBITS_TOP_MASK) == VERSIONBITS_TOP_BITS) &&
2686                 ((pindex->nVersion >> m_bit) & 1) != 0 &&
2687                 ((m_chainman.m_versionbitscache.ComputeBlockVersion(pindex->pprev, params) >> m_bit) & 1) == 0;
2688      }
2689  };
2690  
2691  static unsigned int GetBlockScriptFlags(const CBlockIndex& block_index, const ChainstateManager& chainman)
2692  {
2693      const Consensus::Params& consensusparams = chainman.GetConsensus();
2694  
2695      // BIP16 didn't become active until Apr 1 2012 (on mainnet, and
2696      // retroactively applied to testnet)
2697      // However, only one historical block violated the P2SH rules (on both
2698      // mainnet and testnet).
2699      // Similarly, only one historical block violated the TAPROOT rules on
2700      // mainnet.
2701      // For simplicity, always leave P2SH+WITNESS+TAPROOT on except for the two
2702      // violating blocks.
2703      uint32_t flags{SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_TAPROOT};
2704      const auto it{consensusparams.script_flag_exceptions.find(*Assert(block_index.phashBlock))};
2705      if (it != consensusparams.script_flag_exceptions.end()) {
2706          flags = it->second;
2707      }
2708  
2709      // Enforce the DERSIG (BIP66) rule
2710      if (DeploymentActiveAt(block_index, chainman, Consensus::DEPLOYMENT_DERSIG)) {
2711          flags |= SCRIPT_VERIFY_DERSIG;
2712      }
2713  
2714      // Enforce CHECKLOCKTIMEVERIFY (BIP65)
2715      if (DeploymentActiveAt(block_index, chainman, Consensus::DEPLOYMENT_CLTV)) {
2716          flags |= SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY;
2717      }
2718  
2719      // Enforce CHECKSEQUENCEVERIFY (BIP112)
2720      if (DeploymentActiveAt(block_index, chainman, Consensus::DEPLOYMENT_CSV)) {
2721          flags |= SCRIPT_VERIFY_CHECKSEQUENCEVERIFY;
2722      }
2723  
2724      // Enforce BIP147 NULLDUMMY (activated simultaneously with segwit)
2725      if (DeploymentActiveAt(block_index, chainman, Consensus::DEPLOYMENT_SEGWIT)) {
2726          flags |= SCRIPT_VERIFY_NULLDUMMY;
2727      }
2728  
2729      if (DeploymentActiveAt(block_index, chainman, Consensus::DEPLOYMENT_REDUCED_DATA)) {
2730          flags |= REDUCED_DATA_MANDATORY_VERIFY_FLAGS;
2731      }
2732  
2733      // Enforce P2SPKH (pay to schnorr public key hash) - UASF activated
2734      if (DeploymentActiveAt(block_index, chainman, Consensus::DEPLOYMENT_P2SPKH)) {
2735          flags |= SCRIPT_VERIFY_P2SPKH;
2736      }
2737  
2738      // Fork chain: enforce P2SPKH from the MTP activation gate.  The fork
2739      // activates in 2026, long after every inherited softfork, so its blocks
2740      // enforce the full set regardless of the (parent-chain) buried
2741      // activation heights.  No SIGHASH replay protection: limenka shares the
2742      // spamchain transaction set deliberately - the mempools and blocks
2743      // accept each other's transactions so the chains stay synchronized.
2744      if (block_index.pprev && IsForkActive(block_index.pprev, chainman.GetParams().GetConsensus())) {
2745          flags |= SCRIPT_VERIFY_P2SPKH;
2746          flags |= SCRIPT_VERIFY_P2BPCT;
2747          flags |= SCRIPT_VERIFY_DERSIG;
2748          flags |= SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY;
2749          flags |= SCRIPT_VERIFY_CHECKSEQUENCEVERIFY;
2750          flags |= SCRIPT_VERIFY_NULLDUMMY;
2751          flags |= REDUCED_DATA_MANDATORY_VERIFY_FLAGS;
2752      }
2753  
2754      return flags;
2755  }
2756  
2757  static bool ContextualCheckBlockHeaderVolatile(const CBlockHeader& block, BlockValidationState& state, const ChainstateManager& chainman, const CBlockIndex* pindexPrev) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
2758  
2759  /** Apply the effects of this block (with given index) on the UTXO set represented by coins.
2760   *  Validity checks that depend on the UTXO set are also done; ConnectBlock()
2761   *  can fail if those validity checks fail (among other reasons). */
2762  bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state, CBlockIndex* pindex,
2763                                 CCoinsViewCache& view, bool fJustCheck)
2764  {
2765      AssertLockHeld(cs_main);
2766      assert(pindex);
2767  
2768      uint256 block_hash{block.GetHash()};
2769      assert(*pindex->phashBlock == block_hash);
2770      const bool parallel_script_checks{m_chainman.m_script_check_queue_enabled && m_chainman.GetCheckQueue().HasThreads()};
2771  
2772      const auto time_start{SteadyClock::now()};
2773      const CChainParams& params{m_chainman.GetParams()};
2774  
2775      // Check it again in case a previous version let a bad block in
2776      // NOTE: We don't currently (re-)invoke ContextualCheckBlock() or
2777      // ContextualCheckBlockHeader() here. This means that if we add a new
2778      // consensus rule that is enforced in one of those two functions, then we
2779      // may have let in a block that violates the rule prior to updating the
2780      // software, and we would NOT be enforcing the rule here. Fully solving
2781      // upgrade from one software version to the next after a consensus rule
2782      // change is potentially tricky and issue-specific (see NeedsRedownload()
2783      // for one approach that was used for BIP 141 deployment).
2784      // Also, currently the rule against blocks more than 2 hours in the future
2785      // is enforced in ContextualCheckBlockHeader(); we wouldn't want to
2786      // re-enforce that rule here (at least until we make it impossible for
2787      // the clock to go backward).
2788      if (!CheckBlock(block, state, params.GetConsensus(), !fJustCheck, !fJustCheck, IsForkActive(pindex->pprev, params.GetConsensus()))) {
2789          if (state.GetResult() == BlockValidationResult::BLOCK_MUTATED) {
2790              // We don't write down blocks to disk if they may have been
2791              // corrupted, so this should be impossible unless we're having hardware
2792              // problems.
2793              return FatalError(m_chainman.GetNotifications(), state, _("Corrupt block found indicating potential hardware failure."));
2794          }
2795          LogError("%s: Consensus::CheckBlock: %s\n", __func__, state.ToString());
2796          return false;
2797      }
2798  
2799      // verify that the view's current state corresponds to the previous block
2800      uint256 hashPrevBlock = pindex->pprev == nullptr ? uint256() : pindex->pprev->GetBlockHash();
2801      assert(hashPrevBlock == view.GetBestBlock());
2802  
2803      if (!ContextualCheckBlockHeaderVolatile(block, state, m_chainman, pindex->pprev)) {
2804          LogError("%s: Consensus::ContextualCheckBlockHeaderVolatile: %s\n", __func__, state.ToString());
2805          return false;
2806      }
2807  
2808      m_chainman.num_blocks_total++;
2809  
2810      // Special case for the genesis block, skipping connection of its transactions
2811      // (its coinbase is unspendable)
2812      if (block_hash == params.GetConsensus().hashGenesisBlock) {
2813          if (!fJustCheck)
2814              view.SetBestBlock(pindex->GetBlockHash());
2815          return true;
2816      }
2817  
2818      bool fScriptChecks = true;
2819      if (!m_chainman.AssumedValidBlock().IsNull()) {
2820          // We've been configured with the hash of a block which has been externally verified to have a valid history.
2821          // A suitable default value is included with the software and updated from time to time.  Because validity
2822          //  relative to a piece of software is an objective fact these defaults can be easily reviewed.
2823          // This setting doesn't force the selection of any particular chain but makes validating some faster by
2824          //  effectively caching the result of part of the verification.
2825          BlockMap::const_iterator it{m_blockman.m_block_index.find(m_chainman.AssumedValidBlock())};
2826          if (it != m_blockman.m_block_index.end()) {
2827              if (it->second.GetAncestor(pindex->nHeight) == pindex &&
2828                  m_chainman.m_best_header->GetAncestor(pindex->nHeight) == pindex &&
2829                  m_chainman.m_best_header->nChainWork >= m_chainman.MinimumChainWork()) {
2830                  // This block is a member of the assumed verified chain and an ancestor of the best header.
2831                  // Script verification is skipped when connecting blocks under the
2832                  // assumevalid block. Assuming the assumevalid block is valid this
2833                  // is safe because block merkle hashes are still computed and checked,
2834                  // Of course, if an assumed valid block is invalid due to false scriptSigs
2835                  // this optimization would allow an invalid chain to be accepted.
2836                  // The equivalent time check discourages hash power from extorting the network via DOS attack
2837                  //  into accepting an invalid block through telling users they must manually set assumevalid.
2838                  //  Requiring a software change or burying the invalid block, regardless of the setting, makes
2839                  //  it hard to hide the implication of the demand.  This also avoids having release candidates
2840                  //  that are hardly doing any signature verification at all in testing without having to
2841                  //  artificially set the default assumed verified block further back.
2842                  // The test against the minimum chain work prevents the skipping when denied access to any chain at
2843                  //  least as good as the expected chain.
2844                  fScriptChecks = (GetBlockProofEquivalentTime(*m_chainman.m_best_header, *pindex, *m_chainman.m_best_header, params.GetConsensus()) <= 60 * 60 * 24 * 7 * 2);
2845              }
2846          }
2847      }
2848  
2849      const auto time_1{SteadyClock::now()};
2850      m_chainman.time_check += time_1 - time_start;
2851      LogDebug(BCLog::BENCH, "    - Sanity checks: %.2fms [%.2fs (%.2fms/blk)]\n",
2852               Ticks<MillisecondsDouble>(time_1 - time_start),
2853               Ticks<SecondsDouble>(m_chainman.time_check),
2854               Ticks<MillisecondsDouble>(m_chainman.time_check) / m_chainman.num_blocks_total);
2855  
2856      // Do not allow blocks that contain transactions which 'overwrite' older transactions,
2857      // unless those are already completely spent.
2858      // If such overwrites are allowed, coinbases and transactions depending upon those
2859      // can be duplicated to remove the ability to spend the first instance -- even after
2860      // being sent to another address.
2861      // See BIP30, CVE-2012-1909, and http://r6.ca/blog/20120206T005236Z.html for more information.
2862      // This rule was originally applied to all blocks with a timestamp after March 15, 2012, 0:00 UTC.
2863      // Now that the whole chain is irreversibly beyond that time it is applied to all blocks except the
2864      // two in the chain that violate it. This prevents exploiting the issue against nodes during their
2865      // initial block download.
2866      bool fEnforceBIP30 = !IsBIP30Repeat(*pindex);
2867  
2868      // Once BIP34 activated it was not possible to create new duplicate coinbases and thus other than starting
2869      // with the 2 existing duplicate coinbase pairs, not possible to create overwriting txs.  But by the
2870      // time BIP34 activated, in each of the existing pairs the duplicate coinbase had overwritten the first
2871      // before the first had been spent.  Since those coinbases are sufficiently buried it's no longer possible to create further
2872      // duplicate transactions descending from the known pairs either.
2873      // If we're on the known chain at height greater than where BIP34 activated, we can save the db accesses needed for the BIP30 check.
2874  
2875      // BIP34 requires that a block at height X (block X) has its coinbase
2876      // scriptSig start with a CScriptNum of X (indicated height X).  The above
2877      // logic of no longer requiring BIP30 once BIP34 activates is flawed in the
2878      // case that there is a block X before the BIP34 height of 227,931 which has
2879      // an indicated height Y where Y is greater than X.  The coinbase for block
2880      // X would also be a valid coinbase for block Y, which could be a BIP30
2881      // violation.  An exhaustive search of all mainnet coinbases before the
2882      // BIP34 height which have an indicated height greater than the block height
2883      // reveals many occurrences. The 3 lowest indicated heights found are
2884      // 209,921, 490,897, and 1,983,702 and thus coinbases for blocks at these 3
2885      // heights would be the first opportunity for BIP30 to be violated.
2886  
2887      // The search reveals a great many blocks which have an indicated height
2888      // greater than 1,983,702, so we simply remove the optimization to skip
2889      // BIP30 checking for blocks at height 1,983,702 or higher.  Before we reach
2890      // that block in another 25 years or so, we should take advantage of a
2891      // future consensus change to do a new and improved version of BIP34 that
2892      // will actually prevent ever creating any duplicate coinbases in the
2893      // future.
2894      static constexpr int BIP34_IMPLIES_BIP30_LIMIT = 1983702;
2895  
2896      // There is no potential to create a duplicate coinbase at block 209,921
2897      // because this is still before the BIP34 height and so explicit BIP30
2898      // checking is still active.
2899  
2900      // The final case is block 176,684 which has an indicated height of
2901      // 490,897. Unfortunately, this issue was not discovered until about 2 weeks
2902      // before block 490,897 so there was not much opportunity to address this
2903      // case other than to carefully analyze it and determine it would not be a
2904      // problem. Block 490,897 was, in fact, mined with a different coinbase than
2905      // block 176,684, but it is important to note that even if it hadn't been or
2906      // is remined on an alternate fork with a duplicate coinbase, we would still
2907      // not run into a BIP30 violation.  This is because the coinbase for 176,684
2908      // is spent in block 185,956 in transaction
2909      // d4f7fbbf92f4a3014a230b2dc70b8058d02eb36ac06b4a0736d9d60eaa9e8781.  This
2910      // spending transaction can't be duplicated because it also spends coinbase
2911      // 0328dd85c331237f18e781d692c92de57649529bd5edf1d01036daea32ffde29.  This
2912      // coinbase has an indicated height of over 4.2 billion, and wouldn't be
2913      // duplicatable until that height, and it's currently impossible to create a
2914      // chain that long. Nevertheless we may wish to consider a future soft fork
2915      // which retroactively prevents block 490,897 from creating a duplicate
2916      // coinbase. The two historical BIP30 violations often provide a confusing
2917      // edge case when manipulating the UTXO and it would be simpler not to have
2918      // another edge case to deal with.
2919  
2920      // testnet3 has no blocks before the BIP34 height with indicated heights
2921      // post BIP34 before approximately height 486,000,000. After block
2922      // 1,983,702 testnet3 starts doing unnecessary BIP30 checking again.
2923      assert(pindex->pprev);
2924      CBlockIndex* pindexBIP34height = pindex->pprev->GetAncestor(params.GetConsensus().BIP34Height);
2925      //Only continue to enforce if we're below BIP34 activation height or the block hash at that height doesn't correspond.
2926      fEnforceBIP30 = fEnforceBIP30 && (!pindexBIP34height || !(pindexBIP34height->GetBlockHash() == params.GetConsensus().BIP34Hash));
2927  
2928      // TODO: Remove BIP30 checking from block height 1,983,702 on, once we have a
2929      // consensus change that ensures coinbases at those heights cannot
2930      // duplicate earlier coinbases.
2931      if (fEnforceBIP30 || pindex->nHeight >= BIP34_IMPLIES_BIP30_LIMIT) {
2932          for (const auto& tx : block.vtx) {
2933              for (size_t o = 0; o < tx->vout.size(); o++) {
2934                  if (view.HaveCoin(COutPoint(tx->GetHash(), o))) {
2935                      state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-txns-BIP30",
2936                                    "tried to overwrite transaction");
2937                  }
2938              }
2939          }
2940      }
2941  
2942      // Enforce BIP68 (sequence locks)
2943      int nLockTimeFlags = 0;
2944      if (DeploymentActiveAt(*pindex, m_chainman, Consensus::DEPLOYMENT_CSV)) {
2945          nLockTimeFlags |= LOCKTIME_VERIFY_SEQUENCE;
2946      }
2947  
2948      // Get the script flags for this block
2949      unsigned int flags{GetBlockScriptFlags(*pindex, m_chainman)};
2950  
2951      const auto time_2{SteadyClock::now()};
2952      m_chainman.time_forks += time_2 - time_1;
2953      LogDebug(BCLog::BENCH, "    - Fork checks: %.2fms [%.2fs (%.2fms/blk)]\n",
2954               Ticks<MillisecondsDouble>(time_2 - time_1),
2955               Ticks<SecondsDouble>(m_chainman.time_forks),
2956               Ticks<MillisecondsDouble>(m_chainman.time_forks) / m_chainman.num_blocks_total);
2957  
2958      CBlockUndo blockundo;
2959  
2960      // Precomputed transaction data pointers must not be invalidated
2961      // until after `control` has run the script checks (potentially
2962      // in multiple threads). Preallocate the vector size so a new allocation
2963      // doesn't invalidate pointers into the vector, and keep txsdata in scope
2964      // for as long as `control`.
2965      std::vector<PrecomputedTransactionData> txsdata(block.vtx.size());
2966      CCheckQueueControl<CScriptCheck> control(fScriptChecks && parallel_script_checks ? &m_chainman.GetCheckQueue() : nullptr);
2967  
2968      // For BIP9 deployments, get the activation height dynamically
2969      const auto reduced_data_start_height = DeploymentActiveAt(*pindex, m_chainman, Consensus::DEPLOYMENT_REDUCED_DATA)
2970          ? m_chainman.m_versionbitscache.StateSinceHeight(pindex->pprev, params.GetConsensus(), Consensus::DEPLOYMENT_REDUCED_DATA)
2971          : std::numeric_limits<int>::max();
2972  
2973      const CheckTxInputsRules chk_input_rules{DeploymentActiveAt(*pindex, m_chainman, Consensus::DEPLOYMENT_REDUCED_DATA) ? CheckTxInputsRules::OutputSizeLimit : CheckTxInputsRules::None};
2974  
2975      // Check generation tx output sizes if REDUCED_DATA is active
2976      if (chk_input_rules.test(CheckTxInputsRules::OutputSizeLimit)) {
2977          TxValidationState tx_state;
2978          if (!Consensus::CheckOutputSizes(*block.vtx[0], tx_state)) {
2979              return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS,
2980                                   tx_state.GetRejectReason(),
2981                                   tx_state.GetDebugMessage() + " in generation tx " + block.vtx[0]->GetHash().ToString());
2982          }
2983      }
2984  
2985      std::vector<int> prevheights;
2986      CAmount nFees = 0;
2987      int nInputs = 0;
2988      int64_t nSigOpsCost = 0;
2989      blockundo.vtxundo.reserve(block.vtx.size() - 1);
2990      std::vector<unsigned int> flags_per_input;
2991      for (unsigned int i = 0; i < block.vtx.size(); i++)
2992      {
2993          if (!state.IsValid()) break;
2994          const CTransaction &tx = *(block.vtx[i]);
2995  
2996          nInputs += tx.vin.size();
2997  
2998          if (!tx.IsCoinBase())
2999          {
3000              CAmount txfee = 0;
3001              TxValidationState tx_state;
3002              if (!Consensus::CheckTxInputs(tx, tx_state, view, pindex->nHeight, txfee, chk_input_rules, m_chainman.GetParams().GetConsensus(), /*fork_active=*/IsForkActive(pindex->pprev, m_chainman.GetParams().GetConsensus()))) {
3003                  // Any transaction validation failure in ConnectBlock is a block consensus failure
3004                  state.Invalid(BlockValidationResult::BLOCK_CONSENSUS,
3005                                tx_state.GetRejectReason(),
3006                                tx_state.GetDebugMessage() + " in transaction " + tx.GetHash().ToString());
3007                  break;
3008              }
3009              nFees += txfee;
3010              if (!MoneyRange(nFees)) {
3011                  state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-txns-accumulated-fee-outofrange",
3012                                "accumulated fee in the block out of range");
3013                  break;
3014              }
3015  
3016              // Check that transaction is BIP68 final
3017              // BIP68 lock checks (as opposed to nLockTime checks) must
3018              // be in ConnectBlock because they require the UTXO set
3019              prevheights.resize(tx.vin.size());
3020              flags_per_input.clear();
3021              for (size_t j = 0; j < tx.vin.size(); j++) {
3022                  prevheights[j] = view.AccessCoin(tx.vin[j].prevout).nHeight;
3023                  if (prevheights[j] < reduced_data_start_height) {
3024                      flags_per_input.resize(tx.vin.size(), flags);
3025                      flags_per_input[j] = flags & ~REDUCED_DATA_MANDATORY_VERIFY_FLAGS;
3026                  }
3027              }
3028  
3029              if (!SequenceLocks(tx, nLockTimeFlags, prevheights, *pindex)) {
3030                  state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-txns-nonfinal",
3031                                "contains a non-BIP68-final transaction " + tx.GetHash().ToString());
3032                  break;
3033              }
3034          }
3035  
3036          // GetTransactionSigOpCost counts 3 types of sigops:
3037          // * legacy (always)
3038          // * p2sh (when P2SH enabled in flags and excludes coinbase)
3039          // * witness (when witness enabled in flags and excludes coinbase)
3040          nSigOpsCost += GetTransactionSigOpCost(tx, view, flags);
3041          if (nSigOpsCost > MAX_BLOCK_SIGOPS_COST) {
3042              state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-blk-sigops", "too many sigops");
3043              break;
3044          }
3045  
3046          if (!tx.IsCoinBase())
3047          {
3048              std::vector<CScriptCheck> vChecks;
3049              bool fCacheResults = fJustCheck; /* Don't cache results if we're actually connecting blocks (still consult the cache, though) */
3050              TxValidationState tx_state;
3051              if (fScriptChecks && !CheckInputScripts(tx, tx_state, view, flags, fCacheResults, fCacheResults, txsdata[i], m_chainman.m_validation_cache, parallel_script_checks ? &vChecks : nullptr, flags_per_input)) {
3052                  // Any transaction validation failure in ConnectBlock is a block consensus failure
3053                  state.Invalid(BlockValidationResult::BLOCK_CONSENSUS,
3054                                tx_state.GetRejectReason(), tx_state.GetDebugMessage());
3055                  break;
3056              }
3057              control.Add(std::move(vChecks));
3058          }
3059  
3060          CTxUndo undoDummy;
3061          if (i > 0) {
3062              blockundo.vtxundo.emplace_back();
3063          }
3064          UpdateCoins(tx, view, i == 0 ? undoDummy : blockundo.vtxundo.back(), pindex->nHeight);
3065      }
3066      const auto time_3{SteadyClock::now()};
3067      m_chainman.time_connect += time_3 - time_2;
3068      LogDebug(BCLog::BENCH, "      - Connect %u transactions: %.2fms (%.3fms/tx, %.3fms/txin) [%.2fs (%.2fms/blk)]\n", (unsigned)block.vtx.size(),
3069               Ticks<MillisecondsDouble>(time_3 - time_2), Ticks<MillisecondsDouble>(time_3 - time_2) / block.vtx.size(),
3070               nInputs <= 1 ? 0 : Ticks<MillisecondsDouble>(time_3 - time_2) / (nInputs - 1),
3071               Ticks<SecondsDouble>(m_chainman.time_connect),
3072               Ticks<MillisecondsDouble>(m_chainman.time_connect) / m_chainman.num_blocks_total);
3073  
3074      CAmount blockReward;
3075      if (IsForkActive(pindex->pprev, params.GetConsensus())) {
3076          // Fork blocks pay the time-proportional subsidy: R(e) =
3077          // R_full * e / 600, halved by aggregate fork seconds.  e is the
3078          // direct stamp delta measured against the previous block.  At
3079          // the activation boundary the aggregate-seconds epoch is seeded
3080          // from the chain height so the fork continues the parent's
3081          // halving schedule.
3082          const auto& cp = params.GetConsensus();
3083          int64_t e = pindex->pprev->nForkLastBlockTime != 0
3084              ? block.nTime - pindex->pprev->nForkLastBlockTime
3085              : block.nTime - pindex->pprev->GetBlockTime();  // first block: actual interval, matches parent subsidy
3086          if (e < 1) e = 1;
3087          int64_t agg_base = IsForkActive(pindex->pprev->pprev, cp)
3088              ? pindex->pprev->nForkAggregateSeconds
3089              : int64_t(pindex->nHeight) * 600;
3090          blockReward = nFees + GetForkBlockSubsidy(e, agg_base,
3091                                                    cp.nSubsidyHalvingInterval);
3092      } else {
3093          blockReward = nFees + GetBlockSubsidy(pindex->nHeight, params.GetConsensus());
3094      }
3095      if (block.vtx[0]->GetValueOut() > blockReward && state.IsValid()) {
3096          state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-cb-amount",
3097                        strprintf("coinbase pays too much (actual=%d vs limit=%d)", block.vtx[0]->GetValueOut(), blockReward));
3098      }
3099  
3100      // Accumulate the fork aggregate seconds and DAA state onto this block.
3101      // Done here (rather than in ContextualCheckBlock) so the values are stored
3102      // on the correct block index and only after the block is fully validated.
3103      if (IsForkActive(pindex->pprev, params.GetConsensus())) {
3104          const auto& cp = params.GetConsensus();
3105          // aggregate seconds advance by the ACTUAL elapsed time, so the
3106          // time-anchored halving tracks wall clock
3107          int64_t e = pindex->pprev->nForkLastBlockTime != 0
3108              ? block.nTime - pindex->pprev->nForkLastBlockTime
3109              : block.nTime - pindex->pprev->GetBlockTime();  // first block: actual interval
3110          if (e < 1) e = 1;
3111          // At the activation boundary (pprev is a parent-chain block),
3112          // seed the aggregate-seconds epoch from the chain height so the
3113          // fork continues the parent's halving schedule instead of
3114          // restarting at 50 BTC.
3115          int64_t agg_base = IsForkActive(pindex->pprev->pprev, cp)
3116              ? pindex->pprev->nForkAggregateSeconds
3117              : int64_t(pindex->nHeight) * 600;
3118          pindex->nForkAggregateSeconds = agg_base + e;
3119  
3120          ForkDAAState daa;
3121          CalculateForkTarget(pindex->pprev, &block, cp, &daa);
3122          pindex->nForkTarget        = daa.nForkTarget;
3123          pindex->nForkAvgError      = daa.nForkAvgError;
3124          pindex->nForkLastBlockTime = daa.nForkLastBlockTime;
3125      }
3126  
3127      auto parallel_result = control.Complete();
3128      if (parallel_result.has_value() && state.IsValid()) {
3129          state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, strprintf("mandatory-script-verify-flag-failed (%s)", ScriptErrorString(parallel_result->first)), parallel_result->second);
3130      }
3131      if (!state.IsValid()) {
3132          LogInfo("Block validation error: %s", state.ToString());
3133          return false;
3134      }
3135      const auto time_4{SteadyClock::now()};
3136      m_chainman.time_verify += time_4 - time_2;
3137      LogDebug(BCLog::BENCH, "    - Verify %u txins: %.2fms (%.3fms/txin) [%.2fs (%.2fms/blk)]\n", nInputs - 1,
3138               Ticks<MillisecondsDouble>(time_4 - time_2),
3139               nInputs <= 1 ? 0 : Ticks<MillisecondsDouble>(time_4 - time_2) / (nInputs - 1),
3140               Ticks<SecondsDouble>(m_chainman.time_verify),
3141               Ticks<MillisecondsDouble>(m_chainman.time_verify) / m_chainman.num_blocks_total);
3142  
3143      if (fJustCheck) {
3144          return true;
3145      }
3146  
3147      if (!m_blockman.WriteBlockUndo(blockundo, state, *pindex)) {
3148          return false;
3149      }
3150  
3151      const auto time_5{SteadyClock::now()};
3152      m_chainman.time_undo += time_5 - time_4;
3153      LogDebug(BCLog::BENCH, "    - Write undo data: %.2fms [%.2fs (%.2fms/blk)]\n",
3154               Ticks<MillisecondsDouble>(time_5 - time_4),
3155               Ticks<SecondsDouble>(m_chainman.time_undo),
3156               Ticks<MillisecondsDouble>(m_chainman.time_undo) / m_chainman.num_blocks_total);
3157  
3158      if (!pindex->IsValid(BLOCK_VALID_SCRIPTS)) {
3159          pindex->RaiseValidity(BLOCK_VALID_SCRIPTS);
3160          m_blockman.m_dirty_blockindex.insert(pindex);
3161      }
3162  
3163      // add this block to the view's block chain
3164      view.SetBestBlock(pindex->GetBlockHash());
3165  
3166      const auto time_6{SteadyClock::now()};
3167      m_chainman.time_index += time_6 - time_5;
3168      LogDebug(BCLog::BENCH, "    - Index writing: %.2fms [%.2fs (%.2fms/blk)]\n",
3169               Ticks<MillisecondsDouble>(time_6 - time_5),
3170               Ticks<SecondsDouble>(m_chainman.time_index),
3171               Ticks<MillisecondsDouble>(m_chainman.time_index) / m_chainman.num_blocks_total);
3172  
3173      TRACEPOINT(validation, block_connected,
3174          block_hash.data(),
3175          pindex->nHeight,
3176          block.vtx.size(),
3177          nInputs,
3178          nSigOpsCost,
3179          Ticks<std::chrono::nanoseconds>(time_5 - time_start)
3180      );
3181  
3182      return true;
3183  }
3184  
3185  CoinsCacheSizeState Chainstate::GetCoinsCacheSizeState()
3186  {
3187      AssertLockHeld(::cs_main);
3188      return this->GetCoinsCacheSizeState(
3189          m_coinstip_cache_size_bytes,
3190          m_mempool ? m_mempool->m_opts.max_size_bytes : 0);
3191  }
3192  
3193  CoinsCacheSizeState Chainstate::GetCoinsCacheSizeState(
3194      size_t max_coins_cache_size_bytes,
3195      size_t max_mempool_size_bytes)
3196  {
3197      AssertLockHeld(::cs_main);
3198      const int64_t nMempoolUsage = m_mempool ? m_mempool->DynamicMemoryUsage() : 0;
3199      int64_t cacheSize = CoinsTip().DynamicMemoryUsage();
3200      int64_t nTotalSpace =
3201          max_coins_cache_size_bytes + std::max<int64_t>(int64_t(max_mempool_size_bytes) - nMempoolUsage, 0);
3202  
3203      //! No need to periodic flush if at least this much space still available.
3204      static constexpr int64_t MAX_BLOCK_COINSDB_USAGE_BYTES = 10 * 1024 * 1024;  // 10MB
3205      int64_t large_threshold =
3206          std::max((9 * nTotalSpace) / 10, nTotalSpace - MAX_BLOCK_COINSDB_USAGE_BYTES);
3207  
3208      if (cacheSize > nTotalSpace) {
3209          LogPrintf("Cache size (%s) exceeds total space (%s)\n", cacheSize, nTotalSpace);
3210          return CoinsCacheSizeState::CRITICAL;
3211      } else if (cacheSize > large_threshold) {
3212          return CoinsCacheSizeState::LARGE;
3213      }
3214      return CoinsCacheSizeState::OK;
3215  }
3216  
3217  bool Chainstate::FlushStateToDisk(
3218      BlockValidationState &state,
3219      FlushStateMode mode,
3220      int nManualPruneHeight)
3221  {
3222      LOCK(cs_main);
3223      assert(this->CanFlushToDisk());
3224      std::set<int> setFilesToPrune;
3225      bool full_flush_completed = false;
3226  
3227      const size_t coins_count = CoinsTip().GetCacheSize();
3228      const size_t coins_mem_usage = CoinsTip().DynamicMemoryUsage();
3229  
3230      try {
3231      {
3232          bool fFlushForPrune = false;
3233  
3234          CoinsCacheSizeState cache_state = GetCoinsCacheSizeState();
3235          LOCK(m_blockman.cs_LastBlockFile);
3236          if (m_blockman.IsPruneMode() && (m_blockman.m_check_for_pruning || nManualPruneHeight > 0) && m_chainman.m_blockman.m_blockfiles_indexed) {
3237              // make sure we don't prune above any of the prune locks bestblocks
3238              // pruning is height-based
3239              int last_prune{m_chain.Height()}; // last height we can prune
3240  
3241              if (nManualPruneHeight > 0) {
3242                  LOG_TIME_MILLIS_WITH_CATEGORY("find files to prune (manual)", BCLog::BENCH);
3243  
3244                  m_blockman.FindFilesToPruneManual(
3245                      setFilesToPrune,
3246                      std::min(last_prune, nManualPruneHeight),
3247                      *this, m_chainman);
3248              } else {
3249                  LOG_TIME_MILLIS_WITH_CATEGORY("find files to prune", BCLog::BENCH);
3250  
3251                  m_blockman.FindFilesToPrune(setFilesToPrune, last_prune, *this, m_chainman);
3252                  m_blockman.m_check_for_pruning = false;
3253              }
3254              if (!setFilesToPrune.empty()) {
3255                  fFlushForPrune = true;
3256                  if (!m_blockman.m_have_pruned) {
3257                      m_blockman.m_block_tree_db->WriteFlag("prunedblockfiles", true);
3258                      m_blockman.m_have_pruned = true;
3259                  }
3260              }
3261          }
3262          const auto nNow{NodeClock::now()};
3263          // The cache is large and we're within 10% and 10 MiB of the limit, but we have time now (not in the middle of a block processing).
3264          bool fCacheLarge = mode == FlushStateMode::PERIODIC && cache_state >= CoinsCacheSizeState::LARGE;
3265          bool fCacheCritical = false;
3266          if (mode == FlushStateMode::IF_NEEDED) {
3267              if (cache_state >= CoinsCacheSizeState::CRITICAL) {
3268                  // The cache is over the limit, we have to write now.
3269                  fCacheCritical = true;
3270              } else if (SystemNeedsMemoryReleased()) {
3271                  fCacheCritical = true;
3272              }
3273          }
3274          // It's been a while since we wrote the block index and chain state to disk. Do this frequently, so we don't need to redownload or reindex after a crash.
3275          bool fPeriodicWrite = mode == FlushStateMode::PERIODIC && nNow >= m_next_write;
3276          // Combine all conditions that result in a write to disk.
3277          bool should_write = (mode == FlushStateMode::ALWAYS) || fCacheLarge || fCacheCritical || fPeriodicWrite || fFlushForPrune;
3278          // Write blocks, block index and best chain related state to disk.
3279          if (should_write) {
3280              // Ensure we can write block index
3281              if (!CheckDiskSpace(m_blockman.m_opts.blocks_dir)) {
3282                  return FatalError(m_chainman.GetNotifications(), state, _("Disk space is too low!"));
3283              }
3284              {
3285                  LOG_TIME_MILLIS_WITH_CATEGORY("write block and undo data to disk", BCLog::BENCH);
3286  
3287                  // First make sure all block and undo data is flushed to disk.
3288                  // TODO: Handle return error, or add detailed comment why it is
3289                  // safe to not return an error upon failure.
3290                  if (!m_blockman.FlushChainstateBlockFile(m_chain.Height())) {
3291                      return FatalError(m_chainman.GetNotifications(), state, _("Failed to flush block file to disk. Check disk space and filesystem integrity."));
3292                  }
3293              }
3294  
3295              // Then update all block file information (which may refer to block and undo files).
3296              {
3297                  LOG_TIME_MILLIS_WITH_CATEGORY("write block index to disk", BCLog::BENCH);
3298  
3299                  if (!m_blockman.WriteBlockIndexDB()) {
3300                      return FatalError(m_chainman.GetNotifications(), state, _("Failed to write to block index database."));
3301                  }
3302              }
3303              // Finally remove any pruned files
3304              if (fFlushForPrune) {
3305                  LOG_TIME_MILLIS_WITH_CATEGORY("unlink pruned files", BCLog::BENCH);
3306  
3307                  m_blockman.UnlinkPrunedFiles(setFilesToPrune);
3308              }
3309  
3310              if (!CoinsTip().GetBestBlock().IsNull()) {
3311  
3312              if (coins_mem_usage >= WARN_FLUSH_COINS_SIZE) LogWarning("Flushing large (%d GiB) UTXO set to disk, it may take several minutes", coins_mem_usage >> 30);
3313              LOG_TIME_MILLIS_WITH_CATEGORY(strprintf("write coins cache to disk (%d coins, %.2fKiB)",
3314                  coins_count, coins_mem_usage >> 10), BCLog::BENCH);
3315  
3316              // Typical Coin structures on disk are around 48 bytes in size.
3317              // Pushing a new one to the database can cause it to be written
3318              // twice (once in the log, and once in the tables). This is already
3319              // an overestimation, as most will delete an existing entry or
3320              // overwrite one. Still, use a conservative safety factor of 2.
3321              if (!CheckDiskSpace(m_chainman.m_options.datadir, 48 * 2 * 2 * CoinsTip().GetCacheSize())) {
3322                  return FatalError(m_chainman.GetNotifications(), state, _("Disk space is too low!"));
3323              }
3324              // Flush the chainstate (which may refer to block index entries).
3325              const auto empty_cache{(mode == FlushStateMode::ALWAYS) || fCacheLarge || fCacheCritical};
3326              if (empty_cache ? !CoinsTip().Flush() : !CoinsTip().Sync()) {
3327                  return FatalError(m_chainman.GetNotifications(), state, _("Failed to write to coin database."));
3328              }
3329              full_flush_completed = true;
3330              TRACEPOINT(utxocache, flush,
3331                      int64_t{Ticks<std::chrono::microseconds>(NodeClock::now() - nNow)},
3332                     (uint32_t)mode,
3333                     (uint64_t)coins_count,
3334                     (uint64_t)coins_mem_usage,
3335                     (bool)fFlushForPrune);
3336  
3337              }
3338          }
3339  
3340          if (should_write || m_next_write == NodeClock::time_point::max()) {
3341              constexpr auto range{DATABASE_WRITE_INTERVAL_MAX - DATABASE_WRITE_INTERVAL_MIN};
3342              m_next_write = FastRandomContext().rand_uniform_delay(NodeClock::now() + DATABASE_WRITE_INTERVAL_MIN, range);
3343          }
3344      }
3345      if (full_flush_completed && m_chainman.m_options.signals) {
3346          // Update best block in wallet (so we can detect restored wallets).
3347          m_chainman.m_options.signals->ChainStateFlushed(this->GetRole(), m_chain.GetLocator());
3348      }
3349      } catch (const std::runtime_error& e) {
3350          return FatalError(m_chainman.GetNotifications(), state, strprintf(_("System error while flushing: %s"), e.what()));
3351      }
3352      return true;
3353  }
3354  
3355  void Chainstate::ForceFlushStateToDisk()
3356  {
3357      BlockValidationState state;
3358      if (!this->FlushStateToDisk(state, FlushStateMode::ALWAYS)) {
3359          LogWarning("Failed to force flush state (%s)", state.ToString());
3360      }
3361  }
3362  
3363  void Chainstate::PruneAndFlush()
3364  {
3365      BlockValidationState state;
3366      m_blockman.m_check_for_pruning = true;
3367      if (!this->FlushStateToDisk(state, FlushStateMode::NONE)) {
3368          LogWarning("Failed to flush state (%s)", state.ToString());
3369      }
3370  }
3371  
3372  static void UpdateTipLog(
3373      const ChainstateManager& chainman,
3374      const CCoinsViewCache& coins_tip,
3375      const CBlockIndex* tip,
3376      const std::string& func_name,
3377      const std::string& prefix,
3378      const std::string& warning_messages) EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
3379  {
3380  
3381      AssertLockHeld(::cs_main);
3382  
3383      // Disable rate limiting in LogPrintLevel_ so this source location may log during IBD.
3384      LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Info, /*should_ratelimit=*/false, "%s%s: new best=%s height=%d version=0x%08x log2_work=%f tx=%lu date='%s' progress=%f cache=%.1fMiB(%utxo)%s\n",
3385                     prefix, func_name,
3386                     tip->GetBlockHash().ToString(), tip->nHeight, tip->nVersion,
3387                     log(tip->nChainWork.getdouble()) / log(2.0), tip->m_chain_tx_count,
3388                     FormatISO8601DateTime(tip->GetBlockTime()),
3389                     chainman.GuessVerificationProgress(tip),
3390                     coins_tip.DynamicMemoryUsage() * (1.0 / (1 << 20)),
3391                     coins_tip.GetCacheSize(),
3392                     !warning_messages.empty() ? strprintf(" warning='%s'", warning_messages) : "");
3393  }
3394  
3395  void Chainstate::UpdateTip(const CBlockIndex* pindexNew)
3396  {
3397      AssertLockHeld(::cs_main);
3398      const auto& coins_tip = this->CoinsTip();
3399  
3400      // The remainder of the function isn't relevant if we are not acting on
3401      // the active chainstate, so return if need be.
3402      if (this != &m_chainman.ActiveChainstate()) {
3403          // Only log every so often so that we don't bury log messages at the tip.
3404          constexpr int BACKGROUND_LOG_INTERVAL = 2000;
3405          if (pindexNew->nHeight % BACKGROUND_LOG_INTERVAL == 0) {
3406              UpdateTipLog(m_chainman, coins_tip, pindexNew, __func__, "[background validation] ", "");
3407          }
3408          return;
3409      }
3410  
3411      // New best block
3412      if (m_mempool) {
3413          m_mempool->AddTransactionsUpdated(1);
3414      }
3415  
3416      std::vector<bilingual_str> warning_messages;
3417      if (!m_chainman.IsInitialBlockDownload()) {
3418          const CBlockIndex* pindex = pindexNew;
3419          for (int bit = 0; bit < VERSIONBITS_NUM_BITS; bit++) {
3420              WarningBitsConditionChecker checker(m_chainman, bit);
3421              ThresholdState state = checker.GetStateFor(pindex, m_chainman.GetConsensus(), m_chainman.m_warningcache.at(bit));
3422              if (state == ThresholdState::ACTIVE || state == ThresholdState::LOCKED_IN) {
3423                  const bilingual_str warning = strprintf(_("WARNING: Unknown new rules activated (versionbit %i) - this software is not secure"), bit);
3424                  m_chainman.GetNotifications().warningSet(kernel::Warning::UNKNOWN_NEW_RULES_ACTIVATED, warning);
3425                  warning_messages.push_back(warning);
3426              }
3427          }
3428  
3429          // Check the version of the last 100 blocks to see if we need to upgrade:
3430          int unexpected_bit_count[VERSIONBITS_NUM_BITS], nonversionbit_count = 0;
3431          for (size_t i = 0; i < VERSIONBITS_NUM_BITS; ++i) unexpected_bit_count[i] = 0;
3432          // NOTE: The warning_threshold_hit* variables are static to ensure the warnings persist even after the condition changes, until the node is restarted
3433          static std::set<uint8_t> warning_threshold_hit_bits;
3434          static int32_t warning_threshold_hit_int{-1};
3435          for (int i = 0; i < 100 && pindex != nullptr; i++)
3436          {
3437              int32_t nExpectedVersion = m_chainman.m_versionbitscache.ComputeBlockVersion(pindex->pprev, m_chainman.GetConsensus());
3438              if (pindex->nVersion <= VERSIONBITS_LAST_OLD_BLOCK_VERSION) {
3439                  // We don't care
3440              } else if ((pindex->nVersion & VERSIONBITS_TOP_MASK) != VERSIONBITS_TOP_BITS) {
3441                  // Non-versionbits upgrade
3442                  static constexpr int WARNING_THRESHOLD = 100/2;
3443                  if (++nonversionbit_count > WARNING_THRESHOLD) {
3444                      if (warning_threshold_hit_int == -1) {
3445                          warning_threshold_hit_int = pindex->nVersion;
3446                      } else if (warning_threshold_hit_int != pindex->nVersion) {
3447                          warning_threshold_hit_int = -2;
3448                      }
3449                  }
3450              } else if ((pindex->nVersion & ~nExpectedVersion) != 0) {
3451                  for (int bit = 0; bit < VERSIONBITS_NUM_BITS; ++bit) {
3452                      const int32_t mask = 1 << bit;
3453                      if ((pindex->nVersion & mask) && !(nExpectedVersion & mask)) {
3454                          const int warning_threshold = (bit > 12 ? 75 : 50);
3455                          if (++unexpected_bit_count[bit] > warning_threshold) {
3456                              warning_threshold_hit_bits.insert(bit);
3457                          }
3458                      }
3459                  }
3460              }
3461              pindex = pindex->pprev;
3462          }
3463          if (!warning_threshold_hit_bits.empty()) {
3464              const auto warning = strprintf(_("Warning: Miners are attempting to activate unknown new rules (bit %s)! You may or may not need to act to remain secure"), util::Join(warning_threshold_hit_bits, ", ", [](const uint8_t bit){ return util::ToString(int(bit)); }));
3465              m_chainman.GetNotifications().warningSet(kernel::Warning::UNKNOWN_NEW_RULES_SIGNAL_VBITS, warning, /*update=*/true);
3466              warning_messages.push_back(warning);
3467          }
3468          if (warning_threshold_hit_int != -1) {
3469              bilingual_str warning;
3470              if (warning_threshold_hit_int == -2) {
3471                  warning = _("Warning: Unrecognised block versions are being mined! Unknown rules may or may not be in effect");
3472              } else {
3473                  warning = strprintf(_("Warning: Unrecognised block version (0x%08x) is being mined! Unknown rules may or may not be in effect"), warning_threshold_hit_int);
3474              }
3475              m_chainman.GetNotifications().warningSet(kernel::Warning::UNKNOWN_NEW_RULES_SIGNAL_INTVER, warning, /*update=*/true);
3476              warning_messages.push_back(warning);
3477          }
3478      }
3479  
3480      static constexpr int32_t BIP320_MASK = 0x1fffe000UL;
3481      if ((pindexNew->nVersion & BIP320_MASK) && pindexNew->nVersion != m_chainman.m_versionbitscache.ComputeBlockVersion(pindexNew->pprev, m_chainman.GetConsensus())) {
3482          const auto warning = _("Miner violated version bit protocol");
3483          warning_messages.push_back(warning);
3484      }
3485  
3486      UpdateTipLog(m_chainman, coins_tip, pindexNew, __func__, "",
3487                   util::Join(warning_messages, Untranslated(", ")).original);
3488  }
3489  
3490  /** Disconnect m_chain's tip.
3491    * After calling, the mempool will be in an inconsistent state, with
3492    * transactions from disconnected blocks being added to disconnectpool.  You
3493    * should make the mempool consistent again by calling MaybeUpdateMempoolForReorg.
3494    * with cs_main held.
3495    *
3496    * If disconnectpool is nullptr, then no disconnected transactions are added to
3497    * disconnectpool (note that the caller is responsible for mempool consistency
3498    * in any case).
3499    */
3500  bool Chainstate::DisconnectTip(BlockValidationState& state, DisconnectedBlockTransactions* disconnectpool)
3501  {
3502      AssertLockHeld(cs_main);
3503      if (m_mempool) AssertLockHeld(m_mempool->cs);
3504  
3505      CBlockIndex *pindexDelete = m_chain.Tip();
3506      assert(pindexDelete);
3507      assert(pindexDelete->pprev);
3508      // Read block from disk.
3509      std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>();
3510      CBlock& block = *pblock;
3511      if (!m_blockman.ReadBlock(block, *pindexDelete)) {
3512          LogError("DisconnectTip(): Failed to read block\n");
3513          return false;
3514      }
3515      // Apply the block atomically to the chain state.
3516      const auto time_start{SteadyClock::now()};
3517      {
3518          CCoinsViewCache view(&CoinsTip());
3519          assert(view.GetBestBlock() == pindexDelete->GetBlockHash());
3520          if (DisconnectBlock(block, pindexDelete, view) != DISCONNECT_OK) {
3521              LogError("DisconnectTip(): DisconnectBlock %s failed\n", pindexDelete->GetBlockHash().ToString());
3522              return false;
3523          }
3524          bool flushed = view.Flush();
3525          assert(flushed);
3526      }
3527      LogDebug(BCLog::BENCH, "- Disconnect block: %.2fms\n",
3528               Ticks<MillisecondsDouble>(SteadyClock::now() - time_start));
3529  
3530      {
3531          // Prune locks that began around the tip should be moved backward so they get a chance to reorg
3532          const uint64_t max_height_first{static_cast<uint64_t>(pindexDelete->nHeight - 1)};
3533          for (auto& prune_lock : m_blockman.m_prune_locks) {
3534              if (prune_lock.second.height_first < max_height_first) continue;
3535  
3536              --prune_lock.second.height_first;
3537              LogDebug(BCLog::PRUNE, "%s prune lock moved back to %d\n", prune_lock.first, prune_lock.second.height_first);
3538              // NOTE: Don't need to write to db here, since it will get synced with the rest of the chainstate
3539          }
3540      }
3541  
3542      // Write the chain state to disk, if necessary.
3543      if (!FlushStateToDisk(state, FlushStateMode::IF_NEEDED)) {
3544          return false;
3545      }
3546  
3547      if (disconnectpool && m_mempool) {
3548          for (auto it = block.vtx.rbegin(); it != block.vtx.rend(); ++it) {
3549              m_mempool->UpdateDependentPriorities(*(*it), pindexDelete->nHeight, false);
3550          }
3551          // Save transactions to re-add to mempool at end of reorg. If any entries are evicted for
3552          // exceeding memory limits, remove them and their descendants from the mempool.
3553          for (auto&& evicted_tx : disconnectpool->AddTransactionsFromBlock(block.vtx)) {
3554              m_mempool->removeRecursive(*evicted_tx, MemPoolRemovalReason::REORG);
3555          }
3556      }
3557  
3558      m_chain.SetTip(*pindexDelete->pprev);
3559      m_chainman.UpdateIBDStatus();
3560  
3561      UpdateTip(pindexDelete->pprev);
3562      // Let wallets know transactions went from 1-confirmed to
3563      // 0-confirmed or conflicted:
3564      if (m_chainman.m_options.signals) {
3565          m_chainman.m_options.signals->BlockDisconnected(pblock, pindexDelete);
3566      }
3567  
3568      if (m_mempool) {
3569          // add mempool stats sample
3570          const CFeeRate min_fee_rate = std::max(m_mempool->GetMinFee(), m_mempool->m_opts.min_relay_feerate);
3571          CStats::DefaultStats()->addMempoolSample(m_mempool->size(), m_mempool->DynamicMemoryUsage(), min_fee_rate.GetFeePerK());
3572      }
3573  
3574      return true;
3575  }
3576  
3577  struct PerBlockConnectTrace {
3578      CBlockIndex* pindex = nullptr;
3579      std::shared_ptr<const CBlock> pblock;
3580      PerBlockConnectTrace() = default;
3581  };
3582  /**
3583   * Used to track blocks whose transactions were applied to the UTXO state as a
3584   * part of a single ActivateBestChainStep call.
3585   *
3586   * This class is single-use, once you call GetBlocksConnected() you have to throw
3587   * it away and make a new one.
3588   */
3589  class ConnectTrace {
3590  private:
3591      std::vector<PerBlockConnectTrace> blocksConnected;
3592  
3593  public:
3594      explicit ConnectTrace() : blocksConnected(1) {}
3595  
3596      void BlockConnected(CBlockIndex* pindex, std::shared_ptr<const CBlock> pblock) {
3597          assert(!blocksConnected.back().pindex);
3598          assert(pindex);
3599          assert(pblock);
3600          blocksConnected.back().pindex = pindex;
3601          blocksConnected.back().pblock = std::move(pblock);
3602          blocksConnected.emplace_back();
3603      }
3604  
3605      std::vector<PerBlockConnectTrace>& GetBlocksConnected() {
3606          // We always keep one extra block at the end of our list because
3607          // blocks are added after all the conflicted transactions have
3608          // been filled in. Thus, the last entry should always be an empty
3609          // one waiting for the transactions from the next block. We pop
3610          // the last entry here to make sure the list we return is sane.
3611          assert(!blocksConnected.back().pindex);
3612          blocksConnected.pop_back();
3613          return blocksConnected;
3614      }
3615  };
3616  
3617  /**
3618   * Connect a new block to m_chain. pblock is either nullptr or a pointer to a CBlock
3619   * corresponding to pindexNew, to bypass loading it again from disk.
3620   *
3621   * The block is added to connectTrace if connection succeeds.
3622   */
3623  bool Chainstate::ConnectTip(BlockValidationState& state, CBlockIndex* pindexNew, const std::shared_ptr<const CBlock>& pblock, ConnectTrace& connectTrace, DisconnectedBlockTransactions& disconnectpool)
3624  {
3625      AssertLockHeld(cs_main);
3626      if (m_mempool) AssertLockHeld(m_mempool->cs);
3627  
3628      assert(pindexNew->pprev == m_chain.Tip());
3629      // Read block from disk.
3630      const auto time_1{SteadyClock::now()};
3631      std::shared_ptr<const CBlock> pthisBlock;
3632      if (!pblock) {
3633          std::shared_ptr<CBlock> pblockNew = std::make_shared<CBlock>();
3634          if (!m_blockman.ReadBlock(*pblockNew, *pindexNew)) {
3635              return FatalError(m_chainman.GetNotifications(), state, _("Failed to read block."));
3636          }
3637          pthisBlock = pblockNew;
3638      } else {
3639          LogDebug(BCLog::BENCH, "  - Using cached block\n");
3640          pthisBlock = pblock;
3641      }
3642      const CBlock& blockConnecting = *pthisBlock;
3643      // Apply the block atomically to the chain state.
3644      const auto time_2{SteadyClock::now()};
3645      SteadyClock::time_point time_3;
3646      // When adding aggregate statistics in the future, keep in mind that
3647      // num_blocks_total may be zero until the ConnectBlock() call below.
3648      LogDebug(BCLog::BENCH, "  - Load block from disk: %.2fms\n",
3649               Ticks<MillisecondsDouble>(time_2 - time_1));
3650      {
3651          CCoinsViewCache view(&CoinsTip());
3652          bool rv = ConnectBlock(blockConnecting, state, pindexNew, view);
3653          if (m_chainman.m_options.signals) {
3654              m_chainman.m_options.signals->BlockChecked(blockConnecting, state);
3655          }
3656          if (!rv) {
3657              if (state.IsInvalid())
3658                  InvalidBlockFound(pindexNew, state);
3659              LogError("%s: ConnectBlock %s failed, %s\n", __func__, pindexNew->GetBlockHash().ToString(), state.ToString());
3660              return false;
3661          }
3662          time_3 = SteadyClock::now();
3663          m_chainman.time_connect_total += time_3 - time_2;
3664          assert(m_chainman.num_blocks_total > 0);
3665          LogDebug(BCLog::BENCH, "  - Connect total: %.2fms [%.2fs (%.2fms/blk)]\n",
3666                   Ticks<MillisecondsDouble>(time_3 - time_2),
3667                   Ticks<SecondsDouble>(m_chainman.time_connect_total),
3668                   Ticks<MillisecondsDouble>(m_chainman.time_connect_total) / m_chainman.num_blocks_total);
3669          bool flushed = view.Flush();
3670          assert(flushed);
3671      }
3672      const auto time_4{SteadyClock::now()};
3673      m_chainman.time_flush += time_4 - time_3;
3674      LogDebug(BCLog::BENCH, "  - Flush: %.2fms [%.2fs (%.2fms/blk)]\n",
3675               Ticks<MillisecondsDouble>(time_4 - time_3),
3676               Ticks<SecondsDouble>(m_chainman.time_flush),
3677               Ticks<MillisecondsDouble>(m_chainman.time_flush) / m_chainman.num_blocks_total);
3678      // Write the chain state to disk, if necessary.
3679      if (!FlushStateToDisk(state, FlushStateMode::IF_NEEDED)) {
3680          return false;
3681      }
3682      const auto time_5{SteadyClock::now()};
3683      m_chainman.time_chainstate += time_5 - time_4;
3684      LogDebug(BCLog::BENCH, "  - Writing chainstate: %.2fms [%.2fs (%.2fms/blk)]\n",
3685               Ticks<MillisecondsDouble>(time_5 - time_4),
3686               Ticks<SecondsDouble>(m_chainman.time_chainstate),
3687               Ticks<MillisecondsDouble>(m_chainman.time_chainstate) / m_chainman.num_blocks_total);
3688      // Remove conflicting transactions from the mempool.;
3689      if (m_mempool) {
3690          m_mempool->removeForBlock(blockConnecting.vtx, pindexNew->nHeight);
3691          disconnectpool.removeForBlock(blockConnecting.vtx);
3692      }
3693      // Update m_chain & related variables.
3694      const CBlockIndex* old_tip = m_chain.Tip();
3695      m_chain.SetTip(*pindexNew);
3696      m_chainman.UpdateIBDStatus();
3697      UpdateTip(pindexNew);
3698  
3699      // Fork activation transition: pre-fork mempool transactions may create
3700      // now-banned outputs (e.g. taproot v1), which would poison block
3701      // assembly under the fork rules.  Drop the mempool once when the tip
3702      // crosses the gate.
3703      if (m_mempool && !m_mempool->mapTx.empty()) {
3704          const auto& cp = m_chainman.GetParams().GetConsensus();
3705          if (!IsForkActive(old_tip, cp) && IsForkActive(pindexNew, cp)) {
3706              std::vector<CTransactionRef> to_remove;
3707              to_remove.reserve(m_mempool->mapTx.size());
3708              for (const auto& entry : m_mempool->mapTx) {
3709                  to_remove.push_back(entry.GetSharedTx());
3710              }
3711              for (const auto& tx : to_remove) {
3712                  m_mempool->removeRecursive(*tx, MemPoolRemovalReason::BLOCK);
3713              }
3714              LogPrintf("%s: fork activated - cleared %d pre-fork mempool transactions\n",
3715                        __func__, to_remove.size());
3716          }
3717      }
3718  
3719      if (m_mempool) {
3720          // add mempool stats sample
3721          const CFeeRate min_fee_rate = std::max(m_mempool->GetMinFee(), m_mempool->m_opts.min_relay_feerate);
3722          CStats::DefaultStats()->addMempoolSample(m_mempool->size(), m_mempool->DynamicMemoryUsage(), min_fee_rate.GetFeePerK());
3723      }
3724  
3725      const auto time_6{SteadyClock::now()};
3726      m_chainman.time_post_connect += time_6 - time_5;
3727      m_chainman.time_total += time_6 - time_1;
3728      LogDebug(BCLog::BENCH, "  - Connect postprocess: %.2fms [%.2fs (%.2fms/blk)]\n",
3729               Ticks<MillisecondsDouble>(time_6 - time_5),
3730               Ticks<SecondsDouble>(m_chainman.time_post_connect),
3731               Ticks<MillisecondsDouble>(m_chainman.time_post_connect) / m_chainman.num_blocks_total);
3732      LogDebug(BCLog::BENCH, "- Connect block: %.2fms [%.2fs (%.2fms/blk)]\n",
3733               Ticks<MillisecondsDouble>(time_6 - time_1),
3734               Ticks<SecondsDouble>(m_chainman.time_total),
3735               Ticks<MillisecondsDouble>(m_chainman.time_total) / m_chainman.num_blocks_total);
3736  
3737      // If we are the background validation chainstate, check to see if we are done
3738      // validating the snapshot (i.e. our tip has reached the snapshot's base block).
3739      if (this != &m_chainman.ActiveChainstate()) {
3740          // This call may set `m_disabled`, which is referenced immediately afterwards in
3741          // ActivateBestChain, so that we stop connecting blocks past the snapshot base.
3742          m_chainman.MaybeCompleteSnapshotValidation();
3743      }
3744  
3745      connectTrace.BlockConnected(pindexNew, std::move(pthisBlock));
3746      return true;
3747  }
3748  
3749  /**
3750   * Return the tip of the chain with the most work in it, that isn't
3751   * known to be invalid (it's however far from certain to be valid).
3752   */
3753  CBlockIndex* Chainstate::FindMostWorkChain()
3754  {
3755      AssertLockHeld(::cs_main);
3756      do {
3757          CBlockIndex *pindexNew = nullptr;
3758  
3759          // Find the best candidate header.
3760          {
3761              std::set<CBlockIndex*, CBlockIndexWorkComparator>::reverse_iterator it = setBlockIndexCandidates.rbegin();
3762              if (it == setBlockIndexCandidates.rend())
3763                  return nullptr;
3764              pindexNew = *it;
3765          }
3766  
3767          // Check whether all blocks on the path between the currently active chain and the candidate are valid.
3768          // Just going until the active chain is an optimization, as we know all blocks in it are valid already.
3769          CBlockIndex *pindexTest = pindexNew;
3770          bool fInvalidAncestor = false;
3771          while (pindexTest && !m_chain.Contains(pindexTest)) {
3772              assert(pindexTest->HaveNumChainTxs() || pindexTest->nHeight == 0);
3773  
3774              // Pruned nodes may have entries in setBlockIndexCandidates for
3775              // which block files have been deleted.  Remove those as candidates
3776              // for the most work chain if we come across them; we can't switch
3777              // to a chain unless we have all the non-active-chain parent blocks.
3778              bool fFailedChain = pindexTest->nStatus & BLOCK_FAILED_MASK;
3779              bool fMissingData = !(pindexTest->nStatus & BLOCK_HAVE_DATA);
3780              if (fFailedChain || fMissingData) {
3781                  // Candidate chain is not usable (either invalid or missing data)
3782                  if (fFailedChain && (m_chainman.m_best_invalid == nullptr || pindexNew->nChainWork > m_chainman.m_best_invalid->nChainWork)) {
3783                      m_chainman.m_best_invalid = pindexNew;
3784                  }
3785                  CBlockIndex *pindexFailed = pindexNew;
3786                  // Remove the entire chain from the set.
3787                  while (pindexTest != pindexFailed) {
3788                      if (fFailedChain) {
3789                          pindexFailed->nStatus |= BLOCK_FAILED_CHILD;
3790                          m_blockman.m_dirty_blockindex.insert(pindexFailed);
3791                      } else if (fMissingData) {
3792                          // If we're missing data, then add back to m_blocks_unlinked,
3793                          // so that if the block arrives in the future we can try adding
3794                          // to setBlockIndexCandidates again.
3795                          m_blockman.m_blocks_unlinked.insert(
3796                              std::make_pair(pindexFailed->pprev, pindexFailed));
3797                      }
3798                      setBlockIndexCandidates.erase(pindexFailed);
3799                      pindexFailed = pindexFailed->pprev;
3800                  }
3801                  setBlockIndexCandidates.erase(pindexTest);
3802                  fInvalidAncestor = true;
3803                  break;
3804              }
3805              pindexTest = pindexTest->pprev;
3806          }
3807          if (!fInvalidAncestor)
3808              return pindexNew;
3809      } while(true);
3810  }
3811  
3812  /** Delete all entries in setBlockIndexCandidates that are worse than the current tip. */
3813  void Chainstate::PruneBlockIndexCandidates() {
3814      // Note that we can't delete the current block itself, as we may need to return to it later in case a
3815      // reorganization to a better block fails.
3816      std::set<CBlockIndex*, CBlockIndexWorkComparator>::iterator it = setBlockIndexCandidates.begin();
3817      while (it != setBlockIndexCandidates.end() && setBlockIndexCandidates.value_comp()(*it, m_chain.Tip())) {
3818          setBlockIndexCandidates.erase(it++);
3819      }
3820      // Either the current tip or a successor of it we're working towards is left in setBlockIndexCandidates.
3821      assert(!setBlockIndexCandidates.empty());
3822  }
3823  
3824  /**
3825   * Try to make some progress towards making pindexMostWork the active block.
3826   * pblock is either nullptr or a pointer to a CBlock corresponding to pindexMostWork.
3827   *
3828   * @returns true unless a system error occurred
3829   */
3830  bool Chainstate::ActivateBestChainStep(BlockValidationState& state, CBlockIndex* pindexMostWork, const std::shared_ptr<const CBlock>& pblock, bool& fInvalidFound, ConnectTrace& connectTrace)
3831  {
3832      AssertLockHeld(cs_main);
3833      if (m_mempool) AssertLockHeld(m_mempool->cs);
3834  
3835      const CBlockIndex* pindexOldTip = m_chain.Tip();
3836      const CBlockIndex* pindexFork = m_chain.FindFork(pindexMostWork);
3837  
3838      // Disconnect active blocks which are no longer in the best chain.
3839      bool fBlocksDisconnected = false;
3840      DisconnectedBlockTransactions disconnectpool{MAX_DISCONNECTED_TX_POOL_BYTES};
3841      while (m_chain.Tip() && m_chain.Tip() != pindexFork) {
3842          if (!DisconnectTip(state, &disconnectpool)) {
3843              // This is likely a fatal error, but keep the mempool consistent,
3844              // just in case. Only remove from the mempool in this case.
3845              MaybeUpdateMempoolForReorg(disconnectpool, false);
3846  
3847              // If we're unable to disconnect a block during normal operation,
3848              // then that is a failure of our local system -- we should abort
3849              // rather than stay on a less work chain.
3850              FatalError(m_chainman.GetNotifications(), state, _("Failed to disconnect block."));
3851              return false;
3852          }
3853          fBlocksDisconnected = true;
3854      }
3855  
3856      // Build list of new blocks to connect (in descending height order).
3857      std::vector<CBlockIndex*> vpindexToConnect;
3858      bool fContinue = true;
3859      int nHeight = pindexFork ? pindexFork->nHeight : -1;
3860      while (fContinue && nHeight != pindexMostWork->nHeight) {
3861          // Don't iterate the entire list of potential improvements toward the best tip, as we likely only need
3862          // a few blocks along the way.
3863          int nTargetHeight = std::min(nHeight + 32, pindexMostWork->nHeight);
3864          vpindexToConnect.clear();
3865          vpindexToConnect.reserve(nTargetHeight - nHeight);
3866          CBlockIndex* pindexIter = pindexMostWork->GetAncestor(nTargetHeight);
3867          while (pindexIter && pindexIter->nHeight != nHeight) {
3868              vpindexToConnect.push_back(pindexIter);
3869              pindexIter = pindexIter->pprev;
3870          }
3871          nHeight = nTargetHeight;
3872  
3873          // Connect new blocks.
3874          for (CBlockIndex* pindexConnect : vpindexToConnect | std::views::reverse) {
3875              if (!ConnectTip(state, pindexConnect, pindexConnect == pindexMostWork ? pblock : std::shared_ptr<const CBlock>(), connectTrace, disconnectpool)) {
3876                  if (state.IsInvalid()) {
3877                      // The block violates a consensus rule.
3878                      if (state.GetResult() != BlockValidationResult::BLOCK_MUTATED) {
3879                          InvalidChainFound(vpindexToConnect.front());
3880                      }
3881                      state = BlockValidationState();
3882                      fInvalidFound = true;
3883                      fContinue = false;
3884                      break;
3885                  } else {
3886                      // A system error occurred (disk space, database error, ...).
3887                      // Make the mempool consistent with the current tip, just in case
3888                      // any observers try to use it before shutdown.
3889                      MaybeUpdateMempoolForReorg(disconnectpool, false);
3890                      return false;
3891                  }
3892              } else {
3893                  PruneBlockIndexCandidates();
3894                  if (!pindexOldTip || m_chain.Tip()->nChainWork > pindexOldTip->nChainWork) {
3895                      // We're in a better position than we were. Return temporarily to release the lock.
3896                      fContinue = false;
3897                      break;
3898                  }
3899              }
3900          }
3901      }
3902  
3903      if (fBlocksDisconnected) {
3904          // If any blocks were disconnected, disconnectpool may be non empty.  Add
3905          // any disconnected transactions back to the mempool.
3906          MaybeUpdateMempoolForReorg(disconnectpool, true);
3907      }
3908      if (m_mempool) m_mempool->check(this->CoinsTip(), this->m_chain.Height() + 1, m_chainman.GetParams().GetConsensus(), IsForkActive(this->m_chain.Tip(), m_chainman.GetParams().GetConsensus()));
3909  
3910      CheckForkWarningConditions();
3911  
3912      return true;
3913  }
3914  
3915  static SynchronizationState GetSynchronizationState(bool init, bool blockfiles_indexed)
3916  {
3917      if (!init) return SynchronizationState::POST_INIT;
3918      if (!blockfiles_indexed) return SynchronizationState::INIT_REINDEX;
3919      return SynchronizationState::INIT_DOWNLOAD;
3920  }
3921  
3922  bool ChainstateManager::NotifyHeaderTip()
3923  {
3924      bool fNotify = false;
3925      bool fInitialBlockDownload = false;
3926      CBlockIndex* pindexHeader = nullptr;
3927      {
3928          LOCK(GetMutex());
3929          pindexHeader = m_best_header;
3930  
3931          if (pindexHeader != m_last_notified_header) {
3932              fNotify = true;
3933              fInitialBlockDownload = IsInitialBlockDownload();
3934              m_last_notified_header = pindexHeader;
3935          }
3936      }
3937      // Send block tip changed notifications without the lock held
3938      if (fNotify) {
3939          GetNotifications().headerTip(GetSynchronizationState(fInitialBlockDownload, m_blockman.m_blockfiles_indexed), pindexHeader->nHeight, pindexHeader->nTime, false);
3940      }
3941      return fNotify;
3942  }
3943  
3944  static void LimitValidationInterfaceQueue(ValidationSignals& signals) LOCKS_EXCLUDED(cs_main) {
3945      AssertLockNotHeld(cs_main);
3946  
3947      if (signals.CallbacksPending() > 10) {
3948          signals.SyncWithValidationInterfaceQueue();
3949      }
3950  }
3951  
3952  bool Chainstate::ActivateBestChain(BlockValidationState& state, std::shared_ptr<const CBlock> pblock)
3953  {
3954      AssertLockNotHeld(m_chainstate_mutex);
3955  
3956      // Note that while we're often called here from ProcessNewBlock, this is
3957      // far from a guarantee. Things in the P2P/RPC will often end up calling
3958      // us in the middle of ProcessNewBlock - do not assume pblock is set
3959      // sanely for performance or correctness!
3960      AssertLockNotHeld(::cs_main);
3961  
3962      // ABC maintains a fair degree of expensive-to-calculate internal state
3963      // because this function periodically releases cs_main so that it does not lock up other threads for too long
3964      // during large connects - and to allow for e.g. the callback queue to drain
3965      // we use m_chainstate_mutex to enforce mutual exclusion so that only one caller may execute this function at a time
3966      LOCK(m_chainstate_mutex);
3967  
3968      // Belt-and-suspenders check that we aren't attempting to advance the background
3969      // chainstate past the snapshot base block.
3970      if (WITH_LOCK(::cs_main, return m_disabled)) {
3971          LogError("m_disabled is set - this chainstate should not be in operation. "
3972              "Please report this as a bug. %s", CLIENT_BUGREPORT);
3973          return false;
3974      }
3975  
3976      CBlockIndex *pindexMostWork = nullptr;
3977      CBlockIndex *pindexNewTip = nullptr;
3978      bool exited_ibd{false};
3979      do {
3980          // Block until the validation queue drains. This should largely
3981          // never happen in normal operation, however may happen during
3982          // reindex, causing memory blowup if we run too far ahead.
3983          // Note that if a validationinterface callback ends up calling
3984          // ActivateBestChain this may lead to a deadlock! We should
3985          // probably have a DEBUG_LOCKORDER test for this in the future.
3986          if (m_chainman.m_options.signals) LimitValidationInterfaceQueue(*m_chainman.m_options.signals);
3987  
3988          {
3989              LOCK(cs_main);
3990              {
3991              // Lock transaction pool for at least as long as it takes for connectTrace to be consumed
3992              LOCK(MempoolMutex());
3993              const bool was_in_ibd = m_chainman.IsInitialBlockDownload();
3994              CBlockIndex* starting_tip = m_chain.Tip();
3995              bool blocks_connected = false;
3996              do {
3997                  // We absolutely may not unlock cs_main until we've made forward progress
3998                  // (with the exception of shutdown due to hardware issues, low disk space, etc).
3999                  ConnectTrace connectTrace; // Destructed before cs_main is unlocked
4000  
4001                  if (pindexMostWork == nullptr) {
4002                      pindexMostWork = FindMostWorkChain();
4003                  }
4004  
4005                  // Whether we have anything to do at all.
4006                  if (pindexMostWork == nullptr || pindexMostWork == m_chain.Tip()) {
4007                      break;
4008                  }
4009  
4010                  bool fInvalidFound = false;
4011                  std::shared_ptr<const CBlock> nullBlockPtr;
4012                  // BlockConnected signals must be sent for the original role;
4013                  // in case snapshot validation is completed during ActivateBestChainStep, the
4014                  // result of GetRole() changes from BACKGROUND to NORMAL.
4015                 const ChainstateRole chainstate_role{this->GetRole()};
4016                  if (!ActivateBestChainStep(state, pindexMostWork, pblock && pblock->GetHash() == pindexMostWork->GetBlockHash() ? pblock : nullBlockPtr, fInvalidFound, connectTrace)) {
4017                      // A system error occurred
4018                      return false;
4019                  }
4020                  blocks_connected = true;
4021  
4022                  if (fInvalidFound) {
4023                      // Wipe cache, we may need another branch now.
4024                      pindexMostWork = nullptr;
4025                  }
4026                  pindexNewTip = m_chain.Tip();
4027  
4028                  for (const PerBlockConnectTrace& trace : connectTrace.GetBlocksConnected()) {
4029                      assert(trace.pblock && trace.pindex);
4030                      if (m_chainman.m_options.signals) {
4031                          m_chainman.m_options.signals->BlockConnected(chainstate_role, trace.pblock, trace.pindex);
4032                      }
4033                  }
4034  
4035                  // This will have been toggled in
4036                  // ActivateBestChainStep -> ConnectTip -> MaybeCompleteSnapshotValidation,
4037                  // if at all, so we should catch it here.
4038                  //
4039                  // Break this do-while to ensure we don't advance past the base snapshot.
4040                  if (m_disabled) {
4041                      break;
4042                  }
4043              } while (!m_chain.Tip() || (starting_tip && CBlockIndexWorkComparator()(m_chain.Tip(), starting_tip)));
4044              if (!blocks_connected) return true;
4045  
4046              const CBlockIndex* pindexFork = m_chain.FindFork(starting_tip);
4047              bool still_in_ibd = m_chainman.IsInitialBlockDownload();
4048  
4049              if (was_in_ibd && !still_in_ibd) {
4050                  // Active chainstate has exited IBD.
4051                  exited_ibd = true;
4052              }
4053  
4054              // Notify external listeners about the new tip.
4055              // Enqueue while holding cs_main to ensure that UpdatedBlockTip is called in the order in which blocks are connected
4056              if (this == &m_chainman.ActiveChainstate() && pindexFork != pindexNewTip) {
4057                  // Notify ValidationInterface subscribers
4058                  if (m_chainman.m_options.signals) {
4059                      m_chainman.m_options.signals->UpdatedBlockTip(pindexNewTip, pindexFork, still_in_ibd);
4060                  }
4061  
4062                  if (kernel::IsInterrupted(m_chainman.GetNotifications().blockTip(GetSynchronizationState(still_in_ibd, m_chainman.m_blockman.m_blockfiles_indexed), *pindexNewTip))) {
4063                      // Just breaking and returning success for now. This could
4064                      // be changed to bubble up the kernel::Interrupted value to
4065                      // the caller so the caller could distinguish between
4066                      // completed and interrupted operations.
4067                      break;
4068                  }
4069              }
4070              } // release MempoolMutex
4071              // Notify external listeners about the new tip, even if pindexFork == pindexNewTip.
4072              if (m_chainman.m_options.signals && this == &m_chainman.ActiveChainstate()) {
4073                  m_chainman.m_options.signals->ActiveTipChange(*Assert(pindexNewTip), m_chainman.IsInitialBlockDownload());
4074              }
4075          } // release cs_main
4076          // When we reach this point, we switched to a new tip (stored in pindexNewTip).
4077  
4078          if (exited_ibd) {
4079              // If a background chainstate is in use, we may need to rebalance our
4080              // allocation of caches once a chainstate exits initial block download.
4081              LOCK(::cs_main);
4082              m_chainman.MaybeRebalanceCaches();
4083          }
4084  
4085          // Write changes periodically to disk, after relay.
4086          if (!FlushStateToDisk(state, FlushStateMode::PERIODIC)) {
4087              return false;
4088          }
4089  
4090          if (WITH_LOCK(::cs_main, return m_disabled)) {
4091              // Background chainstate has reached the snapshot base block, so exit.
4092  
4093              // Restart indexes to resume indexing for all blocks unique to the snapshot
4094              // chain. This resumes indexing "in order" from where the indexing on the
4095              // background validation chain left off.
4096              //
4097              // This cannot be done while holding cs_main (within
4098              // MaybeCompleteSnapshotValidation) or a cs_main deadlock will occur.
4099              if (m_chainman.snapshot_download_completed) {
4100                  m_chainman.snapshot_download_completed();
4101              }
4102              break;
4103          }
4104  
4105          // We check interrupt only after giving ActivateBestChainStep a chance to run once so that we
4106          // never interrupt before connecting the genesis block during LoadChainTip(). Previously this
4107          // caused an assert() failure during interrupt in such cases as the UTXO DB flushing checks
4108          // that the best block hash is non-null.
4109          if (m_chainman.m_interrupt) break;
4110      } while (pindexNewTip != pindexMostWork);
4111  
4112      m_chainman.CheckBlockIndex();
4113  
4114      return true;
4115  }
4116  
4117  bool Chainstate::PreciousBlock(BlockValidationState& state, CBlockIndex* pindex)
4118  {
4119      AssertLockNotHeld(m_chainstate_mutex);
4120      AssertLockNotHeld(::cs_main);
4121      {
4122          LOCK(cs_main);
4123          if (pindex->nChainWork < m_chain.Tip()->nChainWork) {
4124              // Nothing to do, this block is not at the tip.
4125              return true;
4126          }
4127          if (m_chain.Tip()->nChainWork > m_chainman.nLastPreciousChainwork) {
4128              // The chain has been extended since the last call, reset the counter.
4129              m_chainman.nBlockReverseSequenceId = -1;
4130          }
4131          m_chainman.nLastPreciousChainwork = m_chain.Tip()->nChainWork;
4132          setBlockIndexCandidates.erase(pindex);
4133          pindex->nSequenceId = m_chainman.nBlockReverseSequenceId;
4134          if (m_chainman.nBlockReverseSequenceId > std::numeric_limits<int32_t>::min()) {
4135              // We can't keep reducing the counter if somebody really wants to
4136              // call preciousblock 2**31-1 times on the same set of tips...
4137              m_chainman.nBlockReverseSequenceId--;
4138          }
4139          if (pindex->IsValid(BLOCK_VALID_TRANSACTIONS) && pindex->HaveNumChainTxs()) {
4140              setBlockIndexCandidates.insert(pindex);
4141              PruneBlockIndexCandidates();
4142          }
4143      }
4144  
4145      return ActivateBestChain(state, std::shared_ptr<const CBlock>());
4146  }
4147  
4148  bool Chainstate::InvalidateBlock(BlockValidationState& state, CBlockIndex* pindex)
4149  {
4150      AssertLockNotHeld(m_chainstate_mutex);
4151      AssertLockNotHeld(::cs_main);
4152  
4153      // Genesis block can't be invalidated
4154      assert(pindex);
4155      if (pindex->nHeight == 0) return false;
4156  
4157      CBlockIndex* to_mark_failed = pindex;
4158      bool pindex_was_in_chain = false;
4159      int disconnected = 0;
4160  
4161      // We do not allow ActivateBestChain() to run while InvalidateBlock() is
4162      // running, as that could cause the tip to change while we disconnect
4163      // blocks.
4164      LOCK(m_chainstate_mutex);
4165  
4166      // We'll be acquiring and releasing cs_main below, to allow the validation
4167      // callbacks to run. However, we should keep the block index in a
4168      // consistent state as we disconnect blocks -- in particular we need to
4169      // add equal-work blocks to setBlockIndexCandidates as we disconnect.
4170      // To avoid walking the block index repeatedly in search of candidates,
4171      // build a map once so that we can look up candidate blocks by chain
4172      // work as we go.
4173      std::multimap<const arith_uint256, CBlockIndex *> candidate_blocks_by_work;
4174  
4175      {
4176          LOCK(cs_main);
4177          for (auto& entry : m_blockman.m_block_index) {
4178              CBlockIndex* candidate = &entry.second;
4179              // We don't need to put anything in our active chain into the
4180              // multimap, because those candidates will be found and considered
4181              // as we disconnect.
4182              // Instead, consider only non-active-chain blocks that have at
4183              // least as much work as where we expect the new tip to end up.
4184              if (!m_chain.Contains(candidate) &&
4185                      !CBlockIndexWorkComparator()(candidate, pindex->pprev) &&
4186                      candidate->IsValid(BLOCK_VALID_TRANSACTIONS) &&
4187                      candidate->HaveNumChainTxs()) {
4188                  candidate_blocks_by_work.insert(std::make_pair(candidate->nChainWork, candidate));
4189              }
4190          }
4191      }
4192  
4193      // Disconnect (descendants of) pindex, and mark them invalid.
4194      while (true) {
4195          if (m_chainman.m_interrupt) break;
4196  
4197          // Make sure the queue of validation callbacks doesn't grow unboundedly.
4198          if (m_chainman.m_options.signals) LimitValidationInterfaceQueue(*m_chainman.m_options.signals);
4199  
4200          LOCK(cs_main);
4201          // Lock for as long as disconnectpool is in scope to make sure MaybeUpdateMempoolForReorg is
4202          // called after DisconnectTip without unlocking in between
4203          LOCK(MempoolMutex());
4204          if (!m_chain.Contains(pindex)) break;
4205          pindex_was_in_chain = true;
4206          CBlockIndex *invalid_walk_tip = m_chain.Tip();
4207  
4208          // ActivateBestChain considers blocks already in m_chain
4209          // unconditionally valid already, so force disconnect away from it.
4210          DisconnectedBlockTransactions disconnectpool{MAX_DISCONNECTED_TX_POOL_BYTES};
4211          bool ret = DisconnectTip(state, &disconnectpool);
4212          // DisconnectTip will add transactions to disconnectpool.
4213          // Adjust the mempool to be consistent with the new tip, adding
4214          // transactions back to the mempool if disconnecting was successful,
4215          // and we're not doing a very deep invalidation (in which case
4216          // keeping the mempool up to date is probably futile anyway).
4217          MaybeUpdateMempoolForReorg(disconnectpool, /* fAddToMempool = */ (++disconnected <= 10) && ret);
4218          if (!ret) return false;
4219          assert(invalid_walk_tip->pprev == m_chain.Tip());
4220  
4221          // We immediately mark the disconnected blocks as invalid.
4222          // This prevents a case where pruned nodes may fail to invalidateblock
4223          // and be left unable to start as they have no tip candidates (as there
4224          // are no blocks that meet the "have data and are not invalid per
4225          // nStatus" criteria for inclusion in setBlockIndexCandidates).
4226          invalid_walk_tip->nStatus |= BLOCK_FAILED_VALID;
4227          m_blockman.m_dirty_blockindex.insert(invalid_walk_tip);
4228          setBlockIndexCandidates.erase(invalid_walk_tip);
4229          setBlockIndexCandidates.insert(invalid_walk_tip->pprev);
4230          if (invalid_walk_tip->pprev == to_mark_failed && (to_mark_failed->nStatus & BLOCK_FAILED_VALID)) {
4231              // We only want to mark the last disconnected block as BLOCK_FAILED_VALID; its children
4232              // need to be BLOCK_FAILED_CHILD instead.
4233              to_mark_failed->nStatus = (to_mark_failed->nStatus ^ BLOCK_FAILED_VALID) | BLOCK_FAILED_CHILD;
4234              m_blockman.m_dirty_blockindex.insert(to_mark_failed);
4235          }
4236  
4237          // Add any equal or more work headers to setBlockIndexCandidates
4238          auto candidate_it = candidate_blocks_by_work.lower_bound(invalid_walk_tip->pprev->nChainWork);
4239          while (candidate_it != candidate_blocks_by_work.end()) {
4240              if (!CBlockIndexWorkComparator()(candidate_it->second, invalid_walk_tip->pprev)) {
4241                  setBlockIndexCandidates.insert(candidate_it->second);
4242                  candidate_it = candidate_blocks_by_work.erase(candidate_it);
4243              } else {
4244                  ++candidate_it;
4245              }
4246          }
4247  
4248          // Track the last disconnected block, so we can correct its BLOCK_FAILED_CHILD status in future
4249          // iterations, or, if it's the last one, call InvalidChainFound on it.
4250          to_mark_failed = invalid_walk_tip;
4251      }
4252  
4253      m_chainman.CheckBlockIndex();
4254  
4255      {
4256          LOCK(cs_main);
4257          if (m_chain.Contains(to_mark_failed)) {
4258              // If the to-be-marked invalid block is in the active chain, something is interfering and we can't proceed.
4259              return false;
4260          }
4261  
4262          // Mark pindex (or the last disconnected block) as invalid, even when it never was in the main chain
4263          to_mark_failed->nStatus |= BLOCK_FAILED_VALID;
4264          m_blockman.m_dirty_blockindex.insert(to_mark_failed);
4265          setBlockIndexCandidates.erase(to_mark_failed);
4266          m_chainman.m_failed_blocks.insert(to_mark_failed);
4267  
4268          // If any new blocks somehow arrived while we were disconnecting
4269          // (above), then the pre-calculation of what should go into
4270          // setBlockIndexCandidates may have missed entries. This would
4271          // technically be an inconsistency in the block index, but if we clean
4272          // it up here, this should be an essentially unobservable error.
4273          // Loop back over all block index entries and add any missing entries
4274          // to setBlockIndexCandidates.
4275          for (auto& [_, block_index] : m_blockman.m_block_index) {
4276              if (block_index.IsValid(BLOCK_VALID_TRANSACTIONS) && block_index.HaveNumChainTxs() && !setBlockIndexCandidates.value_comp()(&block_index, m_chain.Tip())) {
4277                  setBlockIndexCandidates.insert(&block_index);
4278              }
4279          }
4280  
4281          InvalidChainFound(to_mark_failed);
4282      }
4283  
4284      // Only notify about a new block tip if the active chain was modified.
4285      if (pindex_was_in_chain) {
4286          // Ignoring return value for now, this could be changed to bubble up
4287          // kernel::Interrupted value to the caller so the caller could
4288          // distinguish between completed and interrupted operations. It might
4289          // also make sense for the blockTip notification to have an enum
4290          // parameter indicating the source of the tip change so hooks can
4291          // distinguish user-initiated invalidateblock changes from other
4292          // changes.
4293          (void)m_chainman.GetNotifications().blockTip(GetSynchronizationState(m_chainman.IsInitialBlockDownload(), m_chainman.m_blockman.m_blockfiles_indexed), *to_mark_failed->pprev);
4294  
4295          // Fire ActiveTipChange now for the current chain tip to make sure clients are notified.
4296          // ActivateBestChain may call this as well, but not necessarily.
4297          if (m_chainman.m_options.signals) {
4298              m_chainman.m_options.signals->ActiveTipChange(*Assert(m_chain.Tip()), m_chainman.IsInitialBlockDownload());
4299          }
4300      }
4301      return true;
4302  }
4303  
4304  void Chainstate::SetBlockFailureFlags(CBlockIndex* invalid_block)
4305  {
4306      AssertLockHeld(cs_main);
4307  
4308      for (auto& [_, block_index] : m_blockman.m_block_index) {
4309          if (block_index.GetAncestor(invalid_block->nHeight) == invalid_block && !(block_index.nStatus & BLOCK_FAILED_MASK)) {
4310              block_index.nStatus |= BLOCK_FAILED_CHILD;
4311          }
4312      }
4313  }
4314  
4315  void Chainstate::ResetBlockFailureFlags(CBlockIndex *pindex) {
4316      AssertLockHeld(cs_main);
4317  
4318      int nHeight = pindex->nHeight;
4319  
4320      // Remove the invalidity flag from this block and all its descendants.
4321      for (auto& [_, block_index] : m_blockman.m_block_index) {
4322          if (!block_index.IsValid() && block_index.GetAncestor(nHeight) == pindex) {
4323              block_index.nStatus &= ~BLOCK_FAILED_MASK;
4324              m_blockman.m_dirty_blockindex.insert(&block_index);
4325              if (block_index.IsValid(BLOCK_VALID_TRANSACTIONS) && block_index.HaveNumChainTxs() && setBlockIndexCandidates.value_comp()(m_chain.Tip(), &block_index)) {
4326                  setBlockIndexCandidates.insert(&block_index);
4327              }
4328              if (&block_index == m_chainman.m_best_invalid) {
4329                  // Reset invalid block marker if it was pointing to one of those.
4330                  m_chainman.m_best_invalid = nullptr;
4331              }
4332              m_chainman.m_failed_blocks.erase(&block_index);
4333          }
4334      }
4335  
4336      // Remove the invalidity flag from all ancestors too.
4337      while (pindex != nullptr) {
4338          if (pindex->nStatus & BLOCK_FAILED_MASK) {
4339              pindex->nStatus &= ~BLOCK_FAILED_MASK;
4340              m_blockman.m_dirty_blockindex.insert(pindex);
4341              m_chainman.m_failed_blocks.erase(pindex);
4342          }
4343          pindex = pindex->pprev;
4344      }
4345  }
4346  
4347  void Chainstate::TryAddBlockIndexCandidate(CBlockIndex* pindex)
4348  {
4349      AssertLockHeld(cs_main);
4350      // The block only is a candidate for the most-work-chain if it has the same
4351      // or more work than our current tip.
4352      if (m_chain.Tip() != nullptr && setBlockIndexCandidates.value_comp()(pindex, m_chain.Tip())) {
4353          return;
4354      }
4355  
4356      bool is_active_chainstate = this == &m_chainman.ActiveChainstate();
4357      if (is_active_chainstate) {
4358          // The active chainstate should always add entries that have more
4359          // work than the tip.
4360          setBlockIndexCandidates.insert(pindex);
4361      } else if (!m_disabled) {
4362          // For the background chainstate, we only consider connecting blocks
4363          // towards the snapshot base (which can't be nullptr or else we'll
4364          // never make progress).
4365          const CBlockIndex* snapshot_base{Assert(m_chainman.GetSnapshotBaseBlock())};
4366          if (snapshot_base->GetAncestor(pindex->nHeight) == pindex) {
4367              setBlockIndexCandidates.insert(pindex);
4368          }
4369      }
4370  }
4371  
4372  /** Mark a block as having its data received and checked (up to BLOCK_VALID_TRANSACTIONS). */
4373  void ChainstateManager::ReceivedBlockTransactions(const CBlock& block, CBlockIndex* pindexNew, const FlatFilePos& pos)
4374  {
4375      AssertLockHeld(cs_main);
4376      pindexNew->nTx = block.vtx.size();
4377      // Typically m_chain_tx_count will be 0 at this point, but it can be nonzero if this
4378      // is a pruned block which is being downloaded again, or if this is an
4379      // assumeutxo snapshot block which has a hardcoded m_chain_tx_count value from the
4380      // snapshot metadata. If the pindex is not the snapshot block and the
4381      // m_chain_tx_count value is not zero, assert that value is actually correct.
4382      auto prev_tx_sum = [](CBlockIndex& block) { return block.nTx + (block.pprev ? block.pprev->m_chain_tx_count : 0); };
4383      if (!Assume(pindexNew->m_chain_tx_count == 0 || pindexNew->m_chain_tx_count == prev_tx_sum(*pindexNew) ||
4384                  pindexNew == GetSnapshotBaseBlock())) {
4385          LogWarning("Internal bug detected: block %d has unexpected m_chain_tx_count %i that should be %i (%s %s). Please report this issue here: %s\n",
4386              pindexNew->nHeight, pindexNew->m_chain_tx_count, prev_tx_sum(*pindexNew), CLIENT_NAME, FormatFullVersion(), CLIENT_BUGREPORT);
4387          pindexNew->m_chain_tx_count = 0;
4388      }
4389      pindexNew->nFile = pos.nFile;
4390      pindexNew->nDataPos = pos.nPos;
4391      pindexNew->nUndoPos = 0;
4392      pindexNew->nStatus |= BLOCK_HAVE_DATA;
4393      if (DeploymentActiveAt(*pindexNew, *this, Consensus::DEPLOYMENT_SEGWIT)) {
4394          pindexNew->nStatus |= BLOCK_OPT_WITNESS;
4395      }
4396      pindexNew->RaiseValidity(BLOCK_VALID_TRANSACTIONS);
4397      m_blockman.m_dirty_blockindex.insert(pindexNew);
4398  
4399      if (pindexNew->pprev == nullptr || pindexNew->pprev->HaveNumChainTxs()) {
4400          // If pindexNew is the genesis block or all parents are BLOCK_VALID_TRANSACTIONS.
4401          std::deque<CBlockIndex*> queue;
4402          queue.push_back(pindexNew);
4403  
4404          // Recursively process any descendant blocks that now may be eligible to be connected.
4405          while (!queue.empty()) {
4406              CBlockIndex *pindex = queue.front();
4407              queue.pop_front();
4408              // Before setting m_chain_tx_count, assert that it is 0 or already set to
4409              // the correct value. This assert will fail after receiving the
4410              // assumeutxo snapshot block if assumeutxo snapshot metadata has an
4411              // incorrect hardcoded AssumeutxoData::m_chain_tx_count value.
4412              if (!Assume(pindex->m_chain_tx_count == 0 || pindex->m_chain_tx_count == prev_tx_sum(*pindex))) {
4413                  LogWarning("Internal bug detected: block %d has unexpected m_chain_tx_count %i that should be %i (%s %s). Please report this issue here: %s\n",
4414                     pindex->nHeight, pindex->m_chain_tx_count, prev_tx_sum(*pindex), CLIENT_NAME, FormatFullVersion(), CLIENT_BUGREPORT);
4415              }
4416              pindex->m_chain_tx_count = prev_tx_sum(*pindex);
4417              pindex->nSequenceId = nBlockSequenceId++;
4418              for (Chainstate *c : GetAll()) {
4419                  c->TryAddBlockIndexCandidate(pindex);
4420              }
4421              std::pair<std::multimap<CBlockIndex*, CBlockIndex*>::iterator, std::multimap<CBlockIndex*, CBlockIndex*>::iterator> range = m_blockman.m_blocks_unlinked.equal_range(pindex);
4422              while (range.first != range.second) {
4423                  std::multimap<CBlockIndex*, CBlockIndex*>::iterator it = range.first;
4424                  queue.push_back(it->second);
4425                  range.first++;
4426                  m_blockman.m_blocks_unlinked.erase(it);
4427              }
4428          }
4429      } else {
4430          if (pindexNew->pprev && pindexNew->pprev->IsValid(BLOCK_VALID_TREE)) {
4431              m_blockman.m_blocks_unlinked.insert(std::make_pair(pindexNew->pprev, pindexNew));
4432          }
4433      }
4434  }
4435  
4436  static bool CheckBlockHeader(const CBlockHeader& block, BlockValidationState& state, const Consensus::Params& consensusParams, bool fCheckPOW = true)
4437  {
4438      // Check proof of work matches claimed amount
4439      if (fCheckPOW && !CheckProofOfWork(block.GetHash(), block.nBits, consensusParams))
4440          return state.Invalid(BlockValidationResult::BLOCK_INVALID_HEADER, "high-hash", "proof of work failed");
4441  
4442      return true;
4443  }
4444  
4445  static bool CheckMerkleRoot(const CBlock& block, BlockValidationState& state)
4446  {
4447      if (block.m_checked_merkle_root) return true;
4448  
4449      bool mutated;
4450      uint256 merkle_root = BlockMerkleRoot(block, &mutated);
4451      if (block.hashMerkleRoot != merkle_root) {
4452          return state.Invalid(
4453              /*result=*/BlockValidationResult::BLOCK_MUTATED,
4454              /*reject_reason=*/"bad-txnmrklroot",
4455              /*debug_message=*/"hashMerkleRoot mismatch");
4456      }
4457  
4458      // Check for merkle tree malleability (CVE-2012-2459): repeating sequences
4459      // of transactions in a block without affecting the merkle root of a block,
4460      // while still invalidating it.
4461      if (mutated) {
4462          return state.Invalid(
4463              /*result=*/BlockValidationResult::BLOCK_MUTATED,
4464              /*reject_reason=*/"bad-txns-duplicate",
4465              /*debug_message=*/"duplicate transaction");
4466      }
4467  
4468      block.m_checked_merkle_root = true;
4469      return true;
4470  }
4471  
4472  /** CheckWitnessMalleation performs checks for block malleation with regard to
4473   * its witnesses.
4474   *
4475   * Note: If the witness commitment is expected (i.e. `expect_witness_commitment
4476   * = true`), then the block is required to have at least one transaction and the
4477   * first transaction needs to have at least one input. */
4478  static bool CheckWitnessMalleation(const CBlock& block, bool expect_witness_commitment, BlockValidationState& state)
4479  {
4480      if (expect_witness_commitment) {
4481          if (block.m_checked_witness_commitment) return true;
4482  
4483          int commitpos = GetWitnessCommitmentIndex(block);
4484          if (commitpos != NO_WITNESS_COMMITMENT) {
4485              assert(!block.vtx.empty() && !block.vtx[0]->vin.empty());
4486              const auto& witness_stack{block.vtx[0]->vin[0].scriptWitness.stack};
4487  
4488              if (witness_stack.size() != 1 || witness_stack[0].size() != 32) {
4489                  return state.Invalid(
4490                      /*result=*/BlockValidationResult::BLOCK_MUTATED,
4491                      /*reject_reason=*/"bad-witness-nonce-size",
4492                      /*debug_message=*/strprintf("%s : invalid witness reserved value size", __func__));
4493              }
4494  
4495              // The malleation check is ignored; as the transaction tree itself
4496              // already does not permit it, it is impossible to trigger in the
4497              // witness tree.
4498              uint256 hash_witness = BlockWitnessMerkleRoot(block, /*mutated=*/nullptr);
4499  
4500              CHash256().Write(hash_witness).Write(witness_stack[0]).Finalize(hash_witness);
4501              if (memcmp(hash_witness.begin(), &block.vtx[0]->vout[commitpos].scriptPubKey[6], 32)) {
4502                  return state.Invalid(
4503                      /*result=*/BlockValidationResult::BLOCK_MUTATED,
4504                      /*reject_reason=*/"bad-witness-merkle-match",
4505                      /*debug_message=*/strprintf("%s : witness merkle commitment mismatch", __func__));
4506              }
4507  
4508              block.m_checked_witness_commitment = true;
4509              return true;
4510          }
4511      }
4512  
4513      // No witness data is allowed in blocks that don't commit to witness data, as this would otherwise leave room for spam
4514      for (const auto& tx : block.vtx) {
4515          if (tx->HasWitness()) {
4516              return state.Invalid(
4517                  /*result=*/BlockValidationResult::BLOCK_MUTATED,
4518                  /*reject_reason=*/"unexpected-witness",
4519                  /*debug_message=*/strprintf("%s : unexpected witness data found", __func__));
4520          }
4521      }
4522  
4523      return true;
4524  }
4525  
4526  bool CheckBlock(const CBlock& block, BlockValidationState& state, const Consensus::Params& consensusParams, bool fCheckPOW, bool fCheckMerkleRoot, bool fork_active)
4527  {
4528      // These are checks that are independent of context.
4529  
4530      if (block.fChecked)
4531          return true;
4532  
4533      // Check that the header is valid (particularly PoW).  This is mostly
4534      // redundant with the call in AcceptBlockHeader.
4535      if (!CheckBlockHeader(block, state, consensusParams, fCheckPOW))
4536          return false;
4537  
4538      // Signet only: check block solution
4539      if (consensusParams.signet_blocks && fCheckPOW && !CheckSignetBlockSolution(block, consensusParams)) {
4540          return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-signet-blksig", "signet block signature validation failure");
4541      }
4542  
4543      // Check the merkle root.
4544      if (fCheckMerkleRoot && !CheckMerkleRoot(block, state)) {
4545          return false;
4546      }
4547  
4548      // All potential-corruption validation must be done before we do any
4549      // transaction validation, as otherwise we may mark the header as invalid
4550      // because we receive the wrong transactions for it.
4551      // Note that witness malleability is checked in ContextualCheckBlock, so no
4552      // checks that use witness data may be performed here.
4553  
4554      // Size limits.
4555      // Fork blocks have no fixed cap: the time-proportional payload
4556      // limit in ContextualCheckBlock is the consensus bound (decision 5 -
4557      // unbounded, a huge first-recovery block after a stall is
4558      // legitimate).  Parent blocks keep the 4MW cap.
4559      if (!fork_active) {
4560          if (block.vtx.empty() || block.vtx.size() * WITNESS_SCALE_FACTOR > MAX_BLOCK_WEIGHT || ::GetSerializeSize(TX_NO_WITNESS(block)) * WITNESS_SCALE_FACTOR > MAX_BLOCK_WEIGHT)
4561              return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-blk-length", "size limits failed");
4562      }
4563  
4564      // First transaction must be coinbase, the rest must not be
4565      if (block.vtx.empty() || !block.vtx[0]->IsCoinBase())
4566          return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-cb-missing", "first tx is not coinbase");
4567      for (unsigned int i = 1; i < block.vtx.size(); i++)
4568          if (block.vtx[i]->IsCoinBase())
4569              return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-cb-multiple", "more than one coinbase");
4570  
4571      // Check transactions
4572      // Must check for duplicate inputs (see CVE-2018-17144)
4573      for (const auto& tx : block.vtx) {
4574          TxValidationState tx_state;
4575          if (!CheckTransaction(*tx, tx_state)) {
4576              // CheckBlock() does context-free validation checks. The only
4577              // possible failures are consensus failures.
4578              assert(tx_state.GetResult() == TxValidationResult::TX_CONSENSUS);
4579              return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, tx_state.GetRejectReason(),
4580                                   strprintf("Transaction check failed (tx hash %s) %s", tx->GetHash().ToString(), tx_state.GetDebugMessage()));
4581          }
4582      }
4583      unsigned int nSigOps = 0;
4584      for (const auto& tx : block.vtx)
4585      {
4586          nSigOps += GetLegacySigOpCount(*tx);
4587      }
4588      if (nSigOps * WITNESS_SCALE_FACTOR > MAX_BLOCK_SIGOPS_COST)
4589          return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-blk-sigops", "out-of-bounds SigOpCount");
4590  
4591      if (fCheckPOW && fCheckMerkleRoot)
4592          block.fChecked = true;
4593  
4594      return true;
4595  }
4596  
4597  void ChainstateManager::UpdateUncommittedBlockStructures(CBlock& block, const CBlockIndex* pindexPrev) const
4598  {
4599      int commitpos = GetWitnessCommitmentIndex(block);
4600      static const std::vector<unsigned char> nonce(32, 0x00);
4601      if (commitpos != NO_WITNESS_COMMITMENT && DeploymentActiveAfter(pindexPrev, *this, Consensus::DEPLOYMENT_SEGWIT) && !block.vtx[0]->HasWitness()) {
4602          CMutableTransaction tx(*block.vtx[0]);
4603          tx.vin[0].scriptWitness.stack.resize(1);
4604          tx.vin[0].scriptWitness.stack[0] = nonce;
4605          block.vtx[0] = MakeTransactionRef(std::move(tx));
4606      }
4607  }
4608  
4609  std::vector<unsigned char> ChainstateManager::GenerateCoinbaseCommitment(CBlock& block, const CBlockIndex* pindexPrev) const
4610  {
4611      std::vector<unsigned char> commitment;
4612      int commitpos = GetWitnessCommitmentIndex(block);
4613      std::vector<unsigned char> ret(32, 0x00);
4614      if (commitpos == NO_WITNESS_COMMITMENT) {
4615          uint256 witnessroot = BlockWitnessMerkleRoot(block, nullptr);
4616          CHash256().Write(witnessroot).Write(ret).Finalize(witnessroot);
4617          CTxOut out;
4618          out.nValue = 0;
4619          out.scriptPubKey.resize(MINIMUM_WITNESS_COMMITMENT);
4620          out.scriptPubKey[0] = OP_RETURN;
4621          out.scriptPubKey[1] = 0x24;
4622          out.scriptPubKey[2] = 0xaa;
4623          out.scriptPubKey[3] = 0x21;
4624          out.scriptPubKey[4] = 0xa9;
4625          out.scriptPubKey[5] = 0xed;
4626          memcpy(&out.scriptPubKey[6], witnessroot.begin(), 32);
4627          commitment = std::vector<unsigned char>(out.scriptPubKey.begin(), out.scriptPubKey.end());
4628          CMutableTransaction tx(*block.vtx[0]);
4629          tx.vout.push_back(out);
4630          block.vtx[0] = MakeTransactionRef(std::move(tx));
4631      }
4632      UpdateUncommittedBlockStructures(block, pindexPrev);
4633      return commitment;
4634  }
4635  
4636  bool HasValidProofOfWork(const std::vector<CBlockHeader>& headers, const Consensus::Params& consensusParams)
4637  {
4638      return std::all_of(headers.cbegin(), headers.cend(),
4639              [&](const auto& header) { return CheckProofOfWork(header.GetHash(), header.nBits, consensusParams);});
4640  }
4641  
4642  bool IsBlockMutated(const CBlock& block, bool check_witness_root)
4643  {
4644      BlockValidationState state;
4645      if (!CheckMerkleRoot(block, state)) {
4646          LogDebug(BCLog::VALIDATION, "Block mutated: %s\n", state.ToString());
4647          return true;
4648      }
4649  
4650      if (block.vtx.empty() || !block.vtx[0]->IsCoinBase()) {
4651          // Consider the block mutated if any transaction is 64 bytes in size (see 3.1
4652          // in "Weaknesses in Limenka’s Merkle Root Construction":
4653          // https://lists.linuxfoundation.org/pipermail/limenka-dev/attachments/20190225/a27d8837/attachment-0001.pdf).
4654          //
4655          // Note: This is not a consensus change as this only applies to blocks that
4656          // don't have a coinbase transaction and would therefore already be invalid.
4657          return std::any_of(block.vtx.begin(), block.vtx.end(),
4658                             [](auto& tx) { return GetSerializeSize(TX_NO_WITNESS(tx)) == 64; });
4659      } else {
4660          // Theoretically it is still possible for a block with a 64 byte
4661          // coinbase transaction to be mutated but we neglect that possibility
4662          // here as it requires at least 224 bits of work.
4663      }
4664  
4665      if (!CheckWitnessMalleation(block, check_witness_root, state)) {
4666          LogDebug(BCLog::VALIDATION, "Block mutated: %s\n", state.ToString());
4667          return true;
4668      }
4669  
4670      return false;
4671  }
4672  
4673  arith_uint256 CalculateClaimedHeadersWork(std::span<const CBlockHeader> headers)
4674  {
4675      arith_uint256 total_work{0};
4676      for (const CBlockHeader& header : headers) {
4677          CBlockIndex dummy(header);
4678          total_work += GetBlockProof(dummy);
4679      }
4680      return total_work;
4681  }
4682  
4683  /** Context-dependent validity checks.
4684   *  By "context", we mean only the previous block headers, but not the UTXO
4685   *  set; UTXO-related validity checks are done in ConnectBlock().
4686   *  NOTE: This function is not currently invoked by ConnectBlock(), so we
4687   *  should consider upgrade issues if we change which consensus rules are
4688   *  enforced in this function (eg by adding a new consensus rule). See comment
4689   *  in ConnectBlock().
4690   *  Note that -reindex-chainstate skips the validation that happens here!
4691   */
4692  bool ContextualCheckBlockHeader(const CBlockHeader& block, BlockValidationState& state, BlockManager& blockman, const ChainstateManager& chainman, const CBlockIndex* pindexPrev) EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
4693  {
4694      AssertLockHeld(::cs_main);
4695      assert(pindexPrev != nullptr);
4696      const int nHeight = pindexPrev->nHeight + 1;
4697  
4698      // Check proof of work
4699      const Consensus::Params& consensusParams = chainman.GetConsensus();
4700      if (block.nBits != GetNextWorkRequired(pindexPrev, &block, consensusParams))
4701          return state.Invalid(BlockValidationResult::BLOCK_INVALID_HEADER, "bad-diffbits", "incorrect proof of work");
4702  
4703      // Check against checkpoints
4704      if (chainman.m_options.checkpoints_enabled) {
4705          // Don't accept any forks from the main chain prior to last checkpoint.
4706          // GetLastCheckpoint finds the last checkpoint in MapCheckpoints that's in our
4707          // BlockIndex().
4708          const auto& checkpoint_data = chainman.GetParams().Checkpoints();
4709          const CBlockIndex* pcheckpoint = blockman.GetLastCheckpoint(checkpoint_data);
4710          if (pcheckpoint && nHeight < pcheckpoint->nHeight) {
4711              LogPrintf("ERROR: %s: forked chain older than last checkpoint (height %d)\n", __func__, nHeight);
4712              return state.Invalid(BlockValidationResult::BLOCK_CHECKPOINT, "bad-fork-prior-to-checkpoint");
4713          }
4714  
4715          // Check that the block chain matches the known block chain up to a checkpoint
4716          if (!checkpoint_data.CheckBlock(nHeight, block.GetHash())) {
4717              LogPrintf("ERROR: %s: rejected by checkpoint lock-in at %d\n", __func__, nHeight);
4718              return state.Invalid(BlockValidationResult::BLOCK_CHECKPOINT, "checkpoint-mismatch");
4719          }
4720      }
4721  
4722      // Check timestamp against prev
4723      if (block.GetBlockTime() <= pindexPrev->GetMedianTimePast())
4724          return state.Invalid(BlockValidationResult::BLOCK_INVALID_HEADER, "time-too-old", "block's timestamp is too early");
4725  
4726      // Fork timestamp rules: strict monotonicity and a 60s future limit.
4727      const bool fork_active = IsForkActive(pindexPrev, consensusParams);
4728      if (fork_active) {
4729          // Strictly increasing stamps from the second fork block onward.
4730          // The first fork block gets the transition rule: the parent's
4731          // last stamp may legally sit up to +2h ahead under parent
4732          // rules, so monotonicity is enforced against the previous FORK
4733          // block only.
4734          if (IsForkActive(pindexPrev->pprev, consensusParams) &&
4735              block.GetBlockTime() <= pindexPrev->GetBlockTime()) {
4736              return state.Invalid(BlockValidationResult::BLOCK_INVALID_HEADER, "time-nonmonotonic", "fork block timestamp not strictly greater than previous");
4737          }
4738          if (block.Time() > NodeClock::now() + std::chrono::seconds{consensusParams.nForkFutureLimit}) {
4739              return state.Invalid(BlockValidationResult::BLOCK_TIME_FUTURE, "time-too-new-fork", "fork block timestamp too far in the future");
4740          }
4741      } else {
4742          // Parent-chain rules pre-activation.
4743          if (consensusParams.enforce_BIP94) {
4744              // Check timestamp for the first block of each difficulty adjustment
4745              // interval, except the genesis block.
4746              if (nHeight % consensusParams.DifficultyAdjustmentInterval() == 0) {
4747                  if (block.GetBlockTime() < pindexPrev->GetBlockTime() - MAX_TIMEWARP) {
4748                      return state.Invalid(BlockValidationResult::BLOCK_INVALID_HEADER, "time-timewarp-attack", "block's timestamp is too early on diff adjustment block");
4749                  }
4750              }
4751          }
4752          if (block.Time() > NodeClock::now() + std::chrono::seconds{MAX_FUTURE_BLOCK_TIME}) {
4753              return state.Invalid(BlockValidationResult::BLOCK_TIME_FUTURE, "time-too-new", "block timestamp too far in the future");
4754          }
4755      }
4756  
4757      // Reject blocks with outdated version
4758      if ((block.nVersion < 2 && DeploymentActiveAfter(pindexPrev, chainman, Consensus::DEPLOYMENT_HEIGHTINCB)) ||
4759          (block.nVersion < 3 && DeploymentActiveAfter(pindexPrev, chainman, Consensus::DEPLOYMENT_DERSIG)) ||
4760          (block.nVersion < 4 && DeploymentActiveAfter(pindexPrev, chainman, Consensus::DEPLOYMENT_CLTV))) {
4761              return state.Invalid(BlockValidationResult::BLOCK_INVALID_HEADER, strprintf("bad-version(0x%08x)", block.nVersion),
4762                                   strprintf("rejected nVersion=0x%08x block", block.nVersion));
4763      }
4764  
4765  
4766      if (!ContextualCheckBlockHeaderVolatile(block, state, chainman, pindexPrev)) return false;
4767  
4768      return true;
4769  }
4770  
4771  /** Context-dependent validity checks, but rechecked in ConnectBlock().
4772   *  Note that -reindex-chainstate skips the validation that happens here!
4773   */
4774  static bool ContextualCheckBlockHeaderVolatile(const CBlockHeader& block, BlockValidationState& state, const ChainstateManager& chainman, const CBlockIndex* pindexPrev) EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
4775  {
4776      const Consensus::Params& consensusParams = chainman.GetConsensus();
4777  
4778      // Mandatory signaling for deployments approaching max_activation_height
4779      for (int i = 0; i < (int)Consensus::MAX_VERSION_BITS_DEPLOYMENTS; i++) {
4780          const Consensus::DeploymentPos pos = static_cast<Consensus::DeploymentPos>(i);
4781          const ThresholdState deployment_state = chainman.m_versionbitscache.State(pindexPrev, consensusParams, pos);
4782  
4783          if (DeploymentMustSignalAfter(pindexPrev, consensusParams, pos, deployment_state)) {
4784              const auto& deployment = consensusParams.vDeployments[pos];
4785              const bool fVersionBits = (block.nVersion & VERSIONBITS_TOP_MASK) == VERSIONBITS_TOP_BITS;
4786              const bool fDeploymentBit = (block.nVersion & (uint32_t{1} << deployment.bit)) != 0;
4787  
4788              if (!(fVersionBits && fDeploymentBit)) {
4789                  const std::string deployment_name = VersionBitsDeploymentInfo[i].name;
4790                  return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS,
4791                                     "bad-version-" + deployment_name,
4792                                     strprintf("Block must signal for %s approaching max_activation_height=%d",
4793                                             deployment_name, deployment.max_activation_height));
4794              }
4795          }
4796      }
4797  
4798      return true;
4799  }
4800  
4801  /** NOTE: This function is not currently invoked by ConnectBlock(), so we
4802   *  should consider upgrade issues if we change which consensus rules are
4803   *  enforced in this function (eg by adding a new consensus rule). See comment
4804   *  in ConnectBlock().
4805   *  Note that -reindex-chainstate skips the validation that happens here!
4806   */
4807  /** Fork delay commitment: the coinbase must commit the long-division
4808   *  remainder over the previous block hash.  Pure function of the block and
4809   *  the previous index entry - safe to call without cs_main (the expensive
4810   *  recomputation is cached inside ComputeDelay). */
4811  static bool CheckForkDelayCommitment(const CBlock& block, BlockValidationState& state,
4812                                       const CBlockIndex* pindexPrev, const Consensus::Params& cp)
4813  {
4814      const int delay_pos = GetDelayOutputIndex(*block.vtx[0]);
4815      if (delay_pos == NO_DELAY_OUTPUT) {
4816          return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-delay-missing", "missing delay commitment in coinbase");
4817      }
4818      const std::optional<uint64_t> delay_value = GetDelayOutputValue(block.vtx[0]->vout[delay_pos]);
4819      if (!delay_value) {
4820          return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-delay-format", "malformed delay commitment in coinbase");
4821      }
4822      if (*delay_value != ComputeDelay(pindexPrev->GetBlockHash(), cp.nForkDelaySteps)) {
4823          return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-delay-invalid", "invalid delay commitment");
4824      }
4825      return true;
4826  }
4827  
4828  static bool ContextualCheckBlock(const CBlock& block, BlockValidationState& state, const ChainstateManager& chainman, const CBlockIndex* pindexPrev)
4829  {
4830      const int nHeight = pindexPrev == nullptr ? 0 : pindexPrev->nHeight + 1;
4831  
4832      // Enforce BIP113 (Median Time Past).
4833      bool enforce_locktime_median_time_past{false};
4834      if (DeploymentActiveAfter(pindexPrev, chainman, Consensus::DEPLOYMENT_CSV)) {
4835          assert(pindexPrev != nullptr);
4836          enforce_locktime_median_time_past = true;
4837      }
4838  
4839      const int64_t nLockTimeCutoff{enforce_locktime_median_time_past ?
4840                                        pindexPrev->GetMedianTimePast() :
4841                                        block.GetBlockTime()};
4842  
4843      // Check that all transactions are finalized
4844      for (const auto& tx : block.vtx) {
4845          if (!IsFinalTx(*tx, nHeight, nLockTimeCutoff)) {
4846              return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-txns-nonfinal", "non-final transaction");
4847          }
4848      }
4849  
4850      // Enforce rule that the coinbase starts with serialized block height
4851      if (DeploymentActiveAfter(pindexPrev, chainman, Consensus::DEPLOYMENT_HEIGHTINCB))
4852      {
4853          CScript expect = CScript() << nHeight;
4854          if (block.vtx[0]->vin[0].scriptSig.size() < expect.size() ||
4855              !std::equal(expect.begin(), expect.end(), block.vtx[0]->vin[0].scriptSig.begin())) {
4856              return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-cb-height", "block height mismatch in coinbase");
4857          }
4858      }
4859  
4860      // Fork block validation.
4861      if (IsForkActive(pindexPrev, chainman.GetParams().GetConsensus()) && pindexPrev != nullptr) {
4862          const auto& cp = chainman.GetParams().GetConsensus();
4863  
4864          // Time-proportional payload limit: payload weight (block minus
4865          // header+coinbase) <= S_max * e / 600, uncapped.  Witness
4866          // discount is removed, so weight is raw serialized size.
4867          int64_t e = pindexPrev->nForkLastBlockTime != 0
4868              ? block.nTime - pindexPrev->nForkLastBlockTime
4869              : block.nTime - pindexPrev->GetBlockTime();  // first block: actual interval
4870          if (e < 1) e = 1;
4871          int64_t base_weight = ::GetSerializeSize(TX_WITH_WITNESS(block.vtx[0])) + 80; // coinbase + header
4872          int64_t payload_weight = GetBlockWeight(block, /*fork_active=*/true) - base_weight;
4873          int64_t payload_limit = GetForkPayloadWeightLimit(e, MAX_BLOCK_WEIGHT);
4874          if (payload_weight > payload_limit) {
4875              return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-blk-weight-fork",
4876                  strprintf("fork payload weight %d exceeds time-proportional limit %d (e=%d)", payload_weight, payload_limit, e));
4877          }
4878  
4879          // Sequential delay: the coinbase must commit the long-division
4880          // remainder over the previous block hash.  Verification is by
4881          // recomputation (~60s at 2^33 steps), cached per parent - the
4882          // expensive first computation happens in the ProcessNewBlock
4883          // pre-check, outside cs_main.
4884          if (!CheckForkDelayCommitment(block, state, pindexPrev, cp)) {
4885              return false;
4886          }
4887  
4888          // Delete taproot: no new witness-v1 outputs in fork blocks.
4889          // Spending pre-existing taproot UTXOs into non-taproot outputs
4890          // remains legal.
4891          for (const auto& tx : block.vtx) {
4892              for (const auto& out : tx->vout) {
4893                  int witver; std::vector<unsigned char> witprog;
4894                  if (out.scriptPubKey.IsWitnessProgram(witver, witprog) && witver == 1) {
4895                      return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS,
4896                          "bad-taproot-output", "taproot outputs prohibited on the fork");
4897                  }
4898              }
4899          }
4900  
4901          // Reject P2BPCT outputs in coinbase (auditability requirement).
4902          for (const auto& out : block.vtx[0]->vout) {
4903              int witver; std::vector<unsigned char> witprog;
4904              if (out.scriptPubKey.IsWitnessProgram(witver, witprog) &&
4905                  witver == 4 && witprog.size() == WITNESS_V4_BPCT_SIZE) {
4906                  return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS,
4907                      "bad-p2bpct-coinbase", "P2BPCT outputs prohibited in coinbase");
4908              }
4909          }
4910      }
4911  
4912      // Validation for witness commitments.
4913      // * We compute the witness hash (which is the hash including witnesses) of all the block's transactions, except the
4914      //   coinbase (where 0x0000....0000 is used instead).
4915      // * The coinbase scriptWitness is a stack of a single 32-byte vector, containing a witness reserved value (unconstrained).
4916      // * We build a merkle tree with all those witness hashes as leaves (similar to the hashMerkleRoot in the block header).
4917      // * There must be at least one output whose scriptPubKey is a single 36-byte push, the first 4 bytes of which are
4918      //   {0xaa, 0x21, 0xa9, 0xed}, and the following 32 bytes are SHA256^2(witness root, witness reserved value). In case there are
4919      //   multiple, the last one is used.
4920      const bool segwit_expected = DeploymentActiveAfter(pindexPrev, chainman, Consensus::DEPLOYMENT_SEGWIT) ||
4921          (pindexPrev && IsForkActive(pindexPrev, chainman.GetConsensus()));
4922      if (!CheckWitnessMalleation(block, segwit_expected, state)) {
4923          return false;
4924      }
4925  
4926      // After the coinbase witness reserved value and commitment are verified,
4927      // we can check if the block weight passes (before we've checked the
4928      // coinbase witness, it would be possible for the weight to be too
4929      // large by filling up the coinbase witness, which doesn't change
4930      // the block hash, so we couldn't mark the block as permanently
4931      // failed).
4932      // Fork blocks have no fixed weight cap: the time-proportional
4933      // payload limit above is the consensus bound (decision 5 -
4934      // unbounded; a huge first-recovery block after a long stall is
4935      // legitimate).  Parent-chain blocks keep the 4MW cap.
4936      if (!(pindexPrev && IsForkActive(pindexPrev, chainman.GetParams().GetConsensus())) &&
4937          GetBlockWeight(block) > MAX_BLOCK_WEIGHT) {
4938          return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-blk-weight", strprintf("%s : weight limit failed", __func__));
4939      }
4940  
4941      return true;
4942  }
4943  
4944  bool ChainstateManager::AcceptBlockHeader(const CBlockHeader& block, BlockValidationState& state, CBlockIndex** ppindex, bool min_pow_checked)
4945  {
4946      AssertLockHeld(cs_main);
4947  
4948      // Check for duplicate
4949      uint256 hash = block.GetHash();
4950      BlockMap::iterator miSelf{m_blockman.m_block_index.find(hash)};
4951      if (hash != GetConsensus().hashGenesisBlock) {
4952          if (miSelf != m_blockman.m_block_index.end()) {
4953              // Block header is already known.
4954              CBlockIndex* pindex = &(miSelf->second);
4955              if (ppindex)
4956                  *ppindex = pindex;
4957              if (pindex->nStatus & BLOCK_FAILED_MASK) {
4958                  LogDebug(BCLog::VALIDATION, "%s: block %s is marked invalid\n", __func__, hash.ToString());
4959                  return state.Invalid(BlockValidationResult::BLOCK_CACHED_INVALID, "duplicate-invalid");
4960              }
4961              return true;
4962          }
4963  
4964          // Get prev block index (needed to determine fork activation for the
4965          // proof-of-work limit).
4966          CBlockIndex* pindexPrev = nullptr;
4967          BlockMap::iterator mi{m_blockman.m_block_index.find(block.hashPrevBlock)};
4968          if (mi == m_blockman.m_block_index.end()) {
4969              LogDebug(BCLog::VALIDATION, "header %s has prev block not found: %s\n", hash.ToString(), block.hashPrevBlock.ToString());
4970              return state.Invalid(BlockValidationResult::BLOCK_MISSING_PREV, "prev-blk-not-found");
4971          }
4972          pindexPrev = &((*mi).second);
4973          if (pindexPrev->nStatus & BLOCK_FAILED_MASK) {
4974              LogDebug(BCLog::VALIDATION, "header %s has prev block invalid: %s\n", hash.ToString(), block.hashPrevBlock.ToString());
4975              return state.Invalid(BlockValidationResult::BLOCK_INVALID_PREV, "bad-prevblk");
4976          }
4977  
4978          if (!CheckBlockHeader(block, state, GetConsensus(), true)) {
4979              LogDebug(BCLog::VALIDATION, "%s: Consensus::CheckBlockHeader: %s, %s\n", __func__, hash.ToString(), state.ToString());
4980              return false;
4981          }
4982  
4983          if (!ContextualCheckBlockHeader(block, state, m_blockman, *this, pindexPrev)) {
4984              LogDebug(BCLog::VALIDATION, "%s: Consensus::ContextualCheckBlockHeader: %s, %s\n", __func__, hash.ToString(), state.ToString());
4985              return false;
4986          }
4987  
4988          /* Determine if this block descends from any block which has been found
4989           * invalid (m_failed_blocks), then mark pindexPrev and any blocks between
4990           * them as failed. For example:
4991           *
4992           *                D3
4993           *              /
4994           *      B2 - C2
4995           *    /         \
4996           *  A             D2 - E2 - F2
4997           *    \
4998           *      B1 - C1 - D1 - E1
4999           *
5000           * In the case that we attempted to reorg from E1 to F2, only to find
5001           * C2 to be invalid, we would mark D2, E2, and F2 as BLOCK_FAILED_CHILD
5002           * but NOT D3 (it was not in any of our candidate sets at the time).
5003           *
5004           * In any case D3 will also be marked as BLOCK_FAILED_CHILD at restart
5005           * in LoadBlockIndex.
5006           */
5007          if (!pindexPrev->IsValid(BLOCK_VALID_SCRIPTS)) {
5008              // The above does not mean "invalid": it checks if the previous block
5009              // hasn't been validated up to BLOCK_VALID_SCRIPTS. This is a performance
5010              // optimization, in the common case of adding a new block to the tip,
5011              // we don't need to iterate over the failed blocks list.
5012              for (const CBlockIndex* failedit : m_failed_blocks) {
5013                  if (pindexPrev->GetAncestor(failedit->nHeight) == failedit) {
5014                      assert(failedit->nStatus & BLOCK_FAILED_VALID);
5015                      CBlockIndex* invalid_walk = pindexPrev;
5016                      while (invalid_walk != failedit) {
5017                          invalid_walk->nStatus |= BLOCK_FAILED_CHILD;
5018                          m_blockman.m_dirty_blockindex.insert(invalid_walk);
5019                          invalid_walk = invalid_walk->pprev;
5020                      }
5021                      LogDebug(BCLog::VALIDATION, "header %s has prev block invalid: %s\n", hash.ToString(), block.hashPrevBlock.ToString());
5022                      return state.Invalid(BlockValidationResult::BLOCK_INVALID_PREV, "bad-prevblk");
5023                  }
5024              }
5025          }
5026      }
5027      if (!min_pow_checked) {
5028          LogDebug(BCLog::VALIDATION, "%s: not adding new block header %s, missing anti-dos proof-of-work validation\n", __func__, hash.ToString());
5029          return state.Invalid(BlockValidationResult::BLOCK_HEADER_LOW_WORK, "too-little-chainwork");
5030      }
5031      CBlockIndex* pindex{m_blockman.AddToBlockIndex(block, m_best_header)};
5032  
5033      if (ppindex)
5034          *ppindex = pindex;
5035  
5036      // Since this is the earliest point at which we have determined that a
5037      // header is both new and valid, log here.
5038      //
5039      // These messages are valuable for detecting potential selfish mining behavior;
5040      // if multiple displacing headers are seen near simultaneously across many
5041      // nodes in the network, this might be an indication of selfish mining. Having
5042      // this log by default when not in IBD ensures broad availability of this data
5043      // in case investigation is merited.
5044      const auto msg = strprintf(
5045          "Saw new header hash=%s height=%d", hash.ToString(), pindex->nHeight);
5046  
5047      if (IsInitialBlockDownload()) {
5048          LogPrintLevel(BCLog::VALIDATION, BCLog::Level::Debug, "%s\n", msg);
5049      } else {
5050          LogPrintf("%s\n", msg);
5051      }
5052  
5053      return true;
5054  }
5055  
5056  // Exposed wrapper for AcceptBlockHeader
5057  bool ChainstateManager::ProcessNewBlockHeaders(std::span<const CBlockHeader> headers, bool min_pow_checked, BlockValidationState& state, const CBlockIndex** ppindex)
5058  {
5059      AssertLockNotHeld(cs_main);
5060      {
5061          LOCK(cs_main);
5062          for (const CBlockHeader& header : headers) {
5063              CBlockIndex *pindex = nullptr; // Use a temp pindex instead of ppindex to avoid a const_cast
5064              bool accepted{AcceptBlockHeader(header, state, &pindex, min_pow_checked)};
5065              CheckBlockIndex();
5066  
5067              if (!accepted) {
5068                  return false;
5069              }
5070              if (ppindex) {
5071                  *ppindex = pindex;
5072              }
5073          }
5074      }
5075      if (NotifyHeaderTip()) {
5076          if (IsInitialBlockDownload() && ppindex && *ppindex) {
5077              const CBlockIndex& last_accepted{**ppindex};
5078              int64_t blocks_left{(NodeClock::now() - last_accepted.Time()) / GetConsensus().PowTargetSpacing()};
5079              blocks_left = std::max<int64_t>(0, blocks_left);
5080              const int progress = last_accepted.nHeight ? static_cast<int>(1000LL * last_accepted.nHeight / (last_accepted.nHeight + blocks_left)) : 0;
5081              LogInfo("Synchronizing blockheaders, height: %d (~%d.%d%%)\n", last_accepted.nHeight, progress / 10, progress % 10);
5082          }
5083      }
5084      return true;
5085  }
5086  
5087  void ChainstateManager::ReportHeadersPresync(const arith_uint256& work, int64_t height, int64_t timestamp)
5088  {
5089      AssertLockNotHeld(cs_main);
5090      {
5091          LOCK(cs_main);
5092          // Don't report headers presync progress if we already have a post-minchainwork header chain.
5093          // This means we lose reporting for potentially legitimate, but unlikely, deep reorgs, but
5094          // prevent attackers that spam low-work headers from filling our logs.
5095          if (m_best_header->nChainWork >= UintToArith256(GetConsensus().nMinimumChainWork)) return;
5096          // Rate limit headers presync updates to 4 per second, as these are not subject to DoS
5097          // protection.
5098          auto now = std::chrono::steady_clock::now();
5099          if (now < m_last_presync_update + std::chrono::milliseconds{250}) return;
5100          m_last_presync_update = now;
5101      }
5102      bool initial_download = IsInitialBlockDownload();
5103      GetNotifications().headerTip(GetSynchronizationState(initial_download, m_blockman.m_blockfiles_indexed), height, timestamp, /*presync=*/true);
5104      if (initial_download) {
5105          int64_t blocks_left{(NodeClock::now() - NodeSeconds{std::chrono::seconds{timestamp}}) / GetConsensus().PowTargetSpacing()};
5106          blocks_left = std::max<int64_t>(0, blocks_left);
5107          const int progress = height ? static_cast<int>(1000LL * height / (height + blocks_left)) : 0;
5108          LogInfo("Pre-synchronizing blockheaders, height: %d (~%d.%d%%)\n", height, progress / 10, progress % 10);
5109      }
5110  }
5111  
5112  /** Store block on disk. If dbp is non-nullptr, the file is known to already reside on disk */
5113  bool ChainstateManager::AcceptBlock(const std::shared_ptr<const CBlock>& pblock, BlockValidationState& state, CBlockIndex** ppindex, bool fRequested, const FlatFilePos* dbp, bool* fNewBlock, bool min_pow_checked)
5114  {
5115      const CBlock& block = *pblock;
5116  
5117      if (fNewBlock) *fNewBlock = false;
5118      AssertLockHeld(cs_main);
5119  
5120      CBlockIndex *pindexDummy = nullptr;
5121      CBlockIndex *&pindex = ppindex ? *ppindex : pindexDummy;
5122  
5123      bool accepted_header{AcceptBlockHeader(block, state, &pindex, min_pow_checked)};
5124      CheckBlockIndex();
5125  
5126      if (!accepted_header)
5127          return false;
5128  
5129      // Check all requested blocks that we do not already have for validity and
5130      // save them to disk. Skip processing of unrequested blocks as an anti-DoS
5131      // measure, unless the blocks have more work than the active chain tip, and
5132      // aren't too far ahead of it, so are likely to be attached soon.
5133      bool fAlreadyHave = pindex->nStatus & BLOCK_HAVE_DATA;
5134      bool fHasMoreOrSameWork = (ActiveTip() ? pindex->nChainWork >= ActiveTip()->nChainWork : true);
5135      // Blocks that are too out-of-order needlessly limit the effectiveness of
5136      // pruning, because pruning will not delete block files that contain any
5137      // blocks which are too close in height to the tip.  Apply this test
5138      // regardless of whether pruning is enabled; it should generally be safe to
5139      // not process unrequested blocks.
5140      bool fTooFarAhead{pindex->nHeight > ActiveHeight() + int(MIN_BLOCKS_TO_KEEP)};
5141  
5142      // TODO: Decouple this function from the block download logic by removing fRequested
5143      // This requires some new chain data structure to efficiently look up if a
5144      // block is in a chain leading to a candidate for best tip, despite not
5145      // being such a candidate itself.
5146      // Note that this would break the getblockfrompeer RPC
5147  
5148      // TODO: deal better with return value and error conditions for duplicate
5149      // and unrequested blocks.
5150      if (fAlreadyHave) return true;
5151      if (!fRequested) {  // If we didn't ask for it:
5152          if (pindex->nTx != 0) return true;    // This is a previously-processed block that was pruned
5153          if (!fHasMoreOrSameWork) return true; // Don't process less-work chains
5154          if (fTooFarAhead) return true;        // Block height is too high
5155  
5156          // Protect against DoS attacks from low-work chains.
5157          // If our tip is behind, a peer could try to send us
5158          // low-work blocks on a fake chain that we would never
5159          // request; don't process these.
5160          if (pindex->nChainWork < MinimumChainWork()) return true;
5161      }
5162  
5163      const CChainParams& params{GetParams()};
5164  
5165      if (!CheckBlock(block, state, params.GetConsensus(), true, true, IsForkActive(pindex->pprev, params.GetConsensus())) ||
5166          !ContextualCheckBlock(block, state, *this, pindex->pprev)) {
5167          if (state.IsInvalid() && state.GetResult() != BlockValidationResult::BLOCK_MUTATED) {
5168              pindex->nStatus |= BLOCK_FAILED_VALID;
5169              m_blockman.m_dirty_blockindex.insert(pindex);
5170          }
5171          LogError("%s: %s\n", __func__, state.ToString());
5172          return false;
5173      }
5174  
5175      // Header is valid/has work, merkle tree and segwit merkle tree are good...RELAY NOW
5176      // (but if it does not build on our best tip, let the SendMessages loop relay it)
5177      if (!IsInitialBlockDownload() && ActiveTip() == pindex->pprev && m_options.signals) {
5178          m_options.signals->NewPoWValidBlock(pindex, pblock);
5179      }
5180  
5181      // Write block to history file
5182      if (fNewBlock) *fNewBlock = true;
5183      try {
5184          FlatFilePos blockPos{};
5185          if (dbp) {
5186              blockPos = *dbp;
5187              m_blockman.UpdateBlockInfo(block, pindex->nHeight, blockPos);
5188          } else {
5189              blockPos = m_blockman.WriteBlock(block, pindex->nHeight);
5190              if (blockPos.IsNull()) {
5191                  state.Error(strprintf("%s: Failed to find position to write new block to disk", __func__));
5192                  return false;
5193              }
5194          }
5195          ReceivedBlockTransactions(block, pindex, blockPos);
5196      } catch (const std::runtime_error& e) {
5197          return FatalError(GetNotifications(), state, strprintf(_("System error while saving block to disk: %s"), e.what()));
5198      }
5199  
5200      // TODO: FlushStateToDisk() handles flushing of both block and chainstate
5201      // data, so we should move this to ChainstateManager so that we can be more
5202      // intelligent about how we flush.
5203      // For now, since FlushStateMode::NONE is used, all that can happen is that
5204      // the block files may be pruned, so we can just call this on one
5205      // chainstate (particularly if we haven't implemented pruning with
5206      // background validation yet).
5207      ActiveChainstate().FlushStateToDisk(state, FlushStateMode::NONE);
5208  
5209      CheckBlockIndex();
5210  
5211      return true;
5212  }
5213  
5214  bool ChainstateManager::ProcessNewBlock(const std::shared_ptr<const CBlock>& block, bool force_processing, bool min_pow_checked, bool* new_block)
5215  {
5216      AssertLockNotHeld(cs_main);
5217  
5218      // Fork delay pre-check, outside cs_main: the ~60s recomputation must
5219      // not stall the validation pipeline.  The parent is a PoW-verified
5220      // header already in the index (headers-first), and the computation is
5221      // cached per parent, so this cannot be amplified.  Failures are not
5222      // marked invalid here (same policy as CheckBlock pre-check failures).
5223      {
5224          const CBlockIndex* pindex = nullptr;
5225          {
5226              LOCK(cs_main);
5227              pindex = m_blockman.LookupBlockIndex(block->GetHash());
5228          }
5229          if (pindex && pindex->pprev && IsForkActive(pindex->pprev, GetConsensus())) {
5230              BlockValidationState state;
5231              if (!CheckForkDelayCommitment(*block, state, pindex->pprev, GetConsensus())) {
5232                  if (m_options.signals) m_options.signals->BlockChecked(*block, state);
5233                  LogError("%s: fork delay pre-check FAILED (%s)\n", __func__, state.ToString());
5234                  return false;
5235              }
5236          }
5237      }
5238  
5239      {
5240          CBlockIndex *pindex = nullptr;
5241          if (new_block) *new_block = false;
5242          BlockValidationState state;
5243  
5244          // CheckBlock() does not support multi-threaded block validation because CBlock::fChecked can cause data race.
5245          // Therefore, the following critical section must include the CheckBlock() call as well.
5246          LOCK(cs_main);
5247  
5248          // Skipping AcceptBlock() for CheckBlock() failures means that we will never mark a block as invalid if
5249          // CheckBlock() fails.  This is protective against consensus failure if there are any unknown forms of block
5250          // malleability that cause CheckBlock() to fail; see e.g. CVE-2012-2459 and
5251          // https://lists.linuxfoundation.org/pipermail/limenka-dev/2019-February/016697.html.  Because CheckBlock() is
5252          // not very expensive, the anti-DoS benefits of caching failure (of a definitely-invalid block) are not substantial.
5253          const bool fork_active{IsForkActive(ActiveChain().Tip(), GetConsensus())};
5254          bool ret = CheckBlock(*block, state, GetConsensus(), /*fCheckPOW=*/true, /*fCheckMerkleRoot=*/true, fork_active);
5255          if (ret) {
5256              // Store to disk
5257              ret = AcceptBlock(block, state, &pindex, force_processing, nullptr, new_block, min_pow_checked);
5258          }
5259          if (!ret) {
5260              if (m_options.signals) {
5261                  m_options.signals->BlockChecked(*block, state);
5262              }
5263              LogError("%s: AcceptBlock FAILED (%s)\n", __func__, state.ToString());
5264              return false;
5265          }
5266      }
5267  
5268      NotifyHeaderTip();
5269  
5270      BlockValidationState state; // Only used to report errors, not invalidity - ignore it
5271      if (!ActiveChainstate().ActivateBestChain(state, block)) {
5272          LogError("%s: ActivateBestChain failed (%s)\n", __func__, state.ToString());
5273          return false;
5274      }
5275  
5276      Chainstate* bg_chain{WITH_LOCK(cs_main, return BackgroundSyncInProgress() ? m_ibd_chainstate.get() : nullptr)};
5277      BlockValidationState bg_state;
5278      if (bg_chain && !bg_chain->ActivateBestChain(bg_state, block)) {
5279          LogError("%s: [background] ActivateBestChain failed (%s)\n", __func__, bg_state.ToString());
5280          return false;
5281       }
5282  
5283      return true;
5284  }
5285  
5286  MempoolAcceptResult ChainstateManager::ProcessTransaction(const CTransactionRef& tx, bool test_accept, const ignore_rejects_type& ignore_rejects)
5287  {
5288      AssertLockHeld(cs_main);
5289      Chainstate& active_chainstate = ActiveChainstate();
5290      if (!active_chainstate.GetMempool()) {
5291          TxValidationState state;
5292          state.Invalid(TxValidationResult::TX_NO_MEMPOOL, "no-mempool");
5293          return MempoolAcceptResult::Failure(state);
5294      }
5295      auto result = AcceptToMemoryPool(active_chainstate, tx, GetTime(), ignore_rejects, test_accept);
5296      active_chainstate.GetMempool()->check(active_chainstate.CoinsTip(), active_chainstate.m_chain.Height() + 1, active_chainstate.m_chainman.GetParams().GetConsensus());
5297      return result;
5298  }
5299  
5300  bool TestBlockValidity(BlockValidationState& state,
5301                         const CChainParams& chainparams,
5302                         Chainstate& chainstate,
5303                         const CBlock& block,
5304                         CBlockIndex* pindexPrev,
5305                         bool fCheckPOW,
5306                         bool fCheckMerkleRoot)
5307  {
5308      AssertLockHeld(cs_main);
5309      assert(pindexPrev && pindexPrev == chainstate.m_chain.Tip());
5310      CCoinsViewCache viewNew(&chainstate.CoinsTip());
5311      uint256 block_hash(block.GetHash());
5312      CBlockIndex indexDummy(block);
5313      indexDummy.pprev = pindexPrev;
5314      indexDummy.nHeight = pindexPrev->nHeight + 1;
5315      indexDummy.phashBlock = &block_hash;
5316  
5317      // NOTE: CheckBlockHeader is called by CheckBlock
5318      if (!ContextualCheckBlockHeader(block, state, chainstate.m_blockman, chainstate.m_chainman, pindexPrev)) {
5319          LogError("%s: Consensus::ContextualCheckBlockHeader: %s\n", __func__, state.ToString());
5320          return false;
5321      }
5322      if (!CheckBlock(block, state, chainparams.GetConsensus(), fCheckPOW, fCheckMerkleRoot, IsForkActive(pindexPrev, chainparams.GetConsensus()))) {
5323          LogError("%s: Consensus::CheckBlock: %s\n", __func__, state.ToString());
5324          return false;
5325      }
5326      if (!ContextualCheckBlock(block, state, chainstate.m_chainman, pindexPrev)) {
5327          LogError("%s: Consensus::ContextualCheckBlock: %s\n", __func__, state.ToString());
5328          return false;
5329      }
5330      if (!chainstate.ConnectBlock(block, state, &indexDummy, viewNew, true)) {
5331          return false;
5332      }
5333      assert(state.IsValid());
5334  
5335      return true;
5336  }
5337  
5338  /* This function is called from the RPC code for pruneblockchain */
5339  void PruneBlockFilesManual(Chainstate& active_chainstate, int nManualPruneHeight)
5340  {
5341      BlockValidationState state;
5342      if (!active_chainstate.FlushStateToDisk(
5343              state, FlushStateMode::NONE, nManualPruneHeight)) {
5344          LogWarning("Failed to flush state after manual prune (%s)", state.ToString());
5345      }
5346  }
5347  
5348  bool Chainstate::LoadChainTip()
5349  {
5350      AssertLockHeld(cs_main);
5351      const CCoinsViewCache& coins_cache = CoinsTip();
5352      assert(!coins_cache.GetBestBlock().IsNull()); // Never called when the coins view is empty
5353      CBlockIndex* tip = m_chain.Tip();
5354  
5355      if (tip && tip->GetBlockHash() == coins_cache.GetBestBlock()) {
5356          return true;
5357      }
5358  
5359      // Load pointer to end of best chain
5360      CBlockIndex* pindex = m_blockman.LookupBlockIndex(coins_cache.GetBestBlock());
5361      if (!pindex) {
5362          return false;
5363      }
5364      m_chain.SetTip(*pindex);
5365      m_chainman.UpdateIBDStatus();
5366      tip = m_chain.Tip();
5367  
5368      // Make sure our chain tip before shutting down scores better than any other candidate
5369      // to maintain a consistent best tip over reboots in case of a tie.
5370      auto target = tip;
5371      while (target) {
5372          const bool is_candidate{setBlockIndexCandidates.contains(target)};
5373          if (is_candidate) setBlockIndexCandidates.erase(target);
5374          target->nSequenceId = SEQ_ID_BEST_CHAIN_FROM_DISK;
5375          if (is_candidate) setBlockIndexCandidates.insert(target);
5376          target = target->pprev;
5377      }
5378      PruneBlockIndexCandidates();
5379  
5380      tip = m_chain.Tip();
5381      LogPrintf("Loaded best chain: hashBestChain=%s height=%d date=%s progress=%f\n",
5382                tip->GetBlockHash().ToString(),
5383                m_chain.Height(),
5384                FormatISO8601DateTime(tip->GetBlockTime()),
5385                m_chainman.GuessVerificationProgress(tip));
5386  
5387      // Ensure KernelNotifications m_tip_block is set even if no new block arrives.
5388      if (this->GetRole() != ChainstateRole::BACKGROUND) {
5389          // Ignoring return value for now.
5390          (void)m_chainman.GetNotifications().blockTip(GetSynchronizationState(/*init=*/true, m_chainman.m_blockman.m_blockfiles_indexed), *pindex);
5391      }
5392  
5393      return true;
5394  }
5395  
5396  CVerifyDB::CVerifyDB(Notifications& notifications)
5397      : m_notifications{notifications}
5398  {
5399      m_notifications.progress(_("Verifying blocks…"), 0, false);
5400  }
5401  
5402  CVerifyDB::~CVerifyDB()
5403  {
5404      m_notifications.progress(bilingual_str{}, 100, false);
5405  }
5406  
5407  VerifyDBResult CVerifyDB::VerifyDB(
5408      Chainstate& chainstate,
5409      const Consensus::Params& consensus_params,
5410      CCoinsView& coinsview,
5411      int nCheckLevel, int nCheckDepth)
5412  {
5413      AssertLockHeld(cs_main);
5414  
5415      if (chainstate.m_chain.Tip() == nullptr || chainstate.m_chain.Tip()->pprev == nullptr) {
5416          return VerifyDBResult::SUCCESS;
5417      }
5418  
5419      // Verify blocks in the best chain
5420      if (nCheckDepth <= 0 || nCheckDepth > chainstate.m_chain.Height()) {
5421          nCheckDepth = chainstate.m_chain.Height();
5422      }
5423      nCheckLevel = std::max(0, std::min(4, nCheckLevel));
5424      LogPrintf("Verifying last %i blocks at level %i\n", nCheckDepth, nCheckLevel);
5425      CCoinsViewCache coins(&coinsview);
5426      CBlockIndex* pindex;
5427      CBlockIndex* pindexFailure = nullptr;
5428      int nGoodTransactions = 0;
5429      BlockValidationState state;
5430      int reportDone = 0;
5431      bool skipped_no_block_data{false};
5432      bool skipped_l3_checks{false};
5433      LogPrintf("Verification progress: 0%%\n");
5434  
5435      const bool is_snapshot_cs{chainstate.m_from_snapshot_blockhash};
5436  
5437      for (pindex = chainstate.m_chain.Tip(); pindex && pindex->pprev; pindex = pindex->pprev) {
5438          const int percentageDone = std::max(1, std::min(99, (int)(((double)(chainstate.m_chain.Height() - pindex->nHeight)) / (double)nCheckDepth * (nCheckLevel >= 4 ? 50 : 100))));
5439          if (reportDone < percentageDone / 10) {
5440              // report every 10% step
5441              LogPrintf("Verification progress: %d%%\n", percentageDone);
5442              reportDone = percentageDone / 10;
5443          }
5444          m_notifications.progress(_("Verifying blocks…"), percentageDone, false);
5445          if (pindex->nHeight <= chainstate.m_chain.Height() - nCheckDepth) {
5446              break;
5447          }
5448          if ((chainstate.m_blockman.IsPruneMode() || is_snapshot_cs) && !(pindex->nStatus & BLOCK_HAVE_DATA)) {
5449              // If pruning or running under an assumeutxo snapshot, only go
5450              // back as far as we have data.
5451              LogPrintf("VerifyDB(): block verification stopping at height %d (no data). This could be due to pruning or use of an assumeutxo snapshot.\n", pindex->nHeight);
5452              skipped_no_block_data = true;
5453              break;
5454          }
5455          CBlock block;
5456          // check level 0: read from disk
5457          if (!chainstate.m_blockman.ReadBlock(block, *pindex, /*lowprio=*/true)) {
5458              LogError("Verification error: ReadBlock failed at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
5459              return VerifyDBResult::CORRUPTED_BLOCK_DB;
5460          }
5461          // check level 1: verify block validity
5462          if (nCheckLevel >= 1 && !CheckBlock(block, state, consensus_params, /*fCheckPOW=*/true, /*fCheckMerkleRoot=*/true, IsForkActive(pindex->pprev, consensus_params))) {
5463              LogError("Verification error: found bad block at %d, hash=%s (%s)",
5464                        pindex->nHeight, pindex->GetBlockHash().ToString(), state.ToString());
5465              return VerifyDBResult::CORRUPTED_BLOCK_DB;
5466          }
5467          // check level 2: verify undo validity
5468          if (nCheckLevel >= 2 && pindex) {
5469              CBlockUndo undo;
5470              if (!pindex->GetUndoPos().IsNull()) {
5471                  if (!chainstate.m_blockman.ReadBlockUndo(undo, *pindex)) {
5472                      LogError("Verification error: found bad undo data at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
5473                      return VerifyDBResult::CORRUPTED_BLOCK_DB;
5474                  }
5475              }
5476          }
5477          // check level 3: check for inconsistencies during memory-only disconnect of tip blocks
5478          size_t curr_coins_usage = coins.DynamicMemoryUsage() + chainstate.CoinsTip().DynamicMemoryUsage();
5479  
5480          if (nCheckLevel >= 3) {
5481              if (curr_coins_usage <= chainstate.m_coinstip_cache_size_bytes) {
5482                  assert(coins.GetBestBlock() == pindex->GetBlockHash());
5483                  DisconnectResult res = chainstate.DisconnectBlock(block, pindex, coins);
5484                  if (res == DISCONNECT_FAILED) {
5485                      LogError("Verification error: irrecoverable inconsistency in block data at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
5486                      return VerifyDBResult::CORRUPTED_BLOCK_DB;
5487                  }
5488                  if (res == DISCONNECT_UNCLEAN) {
5489                      nGoodTransactions = 0;
5490                      pindexFailure = pindex;
5491                  } else {
5492                      nGoodTransactions += block.vtx.size();
5493                  }
5494              } else {
5495                  skipped_l3_checks = true;
5496              }
5497          }
5498          if (chainstate.m_chainman.m_interrupt) return VerifyDBResult::INTERRUPTED;
5499      }
5500      if (pindexFailure) {
5501          LogError("Verification error: coin database inconsistencies found (last %i blocks, %i good transactions before that)", chainstate.m_chain.Height() - pindexFailure->nHeight + 1, nGoodTransactions);
5502          return VerifyDBResult::CORRUPTED_BLOCK_DB;
5503      }
5504      if (skipped_l3_checks) {
5505          LogWarning("Skipped verification of level >=3 (insufficient database cache size). Consider increasing -dbcache.");
5506      }
5507  
5508      // store block count as we move pindex at check level >= 4
5509      int block_count = chainstate.m_chain.Height() - pindex->nHeight;
5510  
5511      // check level 4: try reconnecting blocks
5512      if (nCheckLevel >= 4 && !skipped_l3_checks) {
5513          while (pindex != chainstate.m_chain.Tip()) {
5514              const int percentageDone = std::max(1, std::min(99, 100 - (int)(((double)(chainstate.m_chain.Height() - pindex->nHeight)) / (double)nCheckDepth * 50)));
5515              if (reportDone < percentageDone / 10) {
5516                  // report every 10% step
5517                  LogPrintf("Verification progress: %d%%\n", percentageDone);
5518                  reportDone = percentageDone / 10;
5519              }
5520              m_notifications.progress(_("Verifying blocks…"), percentageDone, false);
5521              pindex = chainstate.m_chain.Next(pindex);
5522              CBlock block;
5523              if (!chainstate.m_blockman.ReadBlock(block, *pindex, /*lowprio=*/true)) {
5524                  LogError("Verification error: ReadBlock failed at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
5525                  return VerifyDBResult::CORRUPTED_BLOCK_DB;
5526              }
5527              if (!chainstate.ConnectBlock(block, state, pindex, coins)) {
5528                  LogError("Verification error: found unconnectable block at %d, hash=%s (%s)", pindex->nHeight, pindex->GetBlockHash().ToString(), state.ToString());
5529                  return VerifyDBResult::CORRUPTED_BLOCK_DB;
5530              }
5531              if (chainstate.m_chainman.m_interrupt) return VerifyDBResult::INTERRUPTED;
5532          }
5533      }
5534  
5535      LogPrintf("Verification: No coin database inconsistencies in last %i blocks (%i transactions)\n", block_count, nGoodTransactions);
5536  
5537      if (skipped_l3_checks) {
5538          return VerifyDBResult::SKIPPED_L3_CHECKS;
5539      }
5540      if (skipped_no_block_data) {
5541          return VerifyDBResult::SKIPPED_MISSING_BLOCKS;
5542      }
5543      return VerifyDBResult::SUCCESS;
5544  }
5545  
5546  /** Apply the effects of a block on the utxo cache, ignoring that it may already have been applied. */
5547  bool Chainstate::RollforwardBlock(const CBlockIndex* pindex, CCoinsViewCache& inputs)
5548  {
5549      AssertLockHeld(cs_main);
5550      // TODO: merge with ConnectBlock
5551      CBlock block;
5552      if (!m_blockman.ReadBlock(block, *pindex, /*lowprio=*/true)) {
5553          LogError("ReplayBlock(): ReadBlock failed at %d, hash=%s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
5554          return false;
5555      }
5556  
5557      for (const CTransactionRef& tx : block.vtx) {
5558          if (!tx->IsCoinBase()) {
5559              for (const CTxIn &txin : tx->vin) {
5560                  inputs.SpendCoin(txin.prevout);
5561              }
5562          }
5563          // Pass check = true as every addition may be an overwrite.
5564          AddCoins(inputs, *tx, pindex->nHeight, true);
5565      }
5566      return true;
5567  }
5568  
5569  bool Chainstate::ReplayBlocks()
5570  {
5571      LOCK(cs_main);
5572  
5573      CCoinsView& db = this->CoinsDB();
5574      CCoinsViewCache cache(&db);
5575  
5576      std::vector<uint256> hashHeads = db.GetHeadBlocks();
5577      if (hashHeads.empty()) return true; // We're already in a consistent state.
5578      if (hashHeads.size() != 2) {
5579          LogError("ReplayBlocks(): unknown inconsistent state\n");
5580          return false;
5581      }
5582  
5583      m_chainman.GetNotifications().progress(_("Replaying blocks…"), 0, false);
5584      LogPrintf("Replaying blocks\n");
5585  
5586      const CBlockIndex* pindexOld = nullptr;  // Old tip during the interrupted flush.
5587      const CBlockIndex* pindexNew;            // New tip during the interrupted flush.
5588      const CBlockIndex* pindexFork = nullptr; // Latest block common to both the old and the new tip.
5589  
5590      if (m_blockman.m_block_index.count(hashHeads[0]) == 0) {
5591          LogError("ReplayBlocks(): reorganization to unknown block requested\n");
5592          return false;
5593      }
5594      pindexNew = &(m_blockman.m_block_index[hashHeads[0]]);
5595  
5596      if (!hashHeads[1].IsNull()) { // The old tip is allowed to be 0, indicating it's the first flush.
5597          if (m_blockman.m_block_index.count(hashHeads[1]) == 0) {
5598              LogError("ReplayBlocks(): reorganization from unknown block requested\n");
5599              return false;
5600          }
5601          pindexOld = &(m_blockman.m_block_index[hashHeads[1]]);
5602          pindexFork = LastCommonAncestor(pindexOld, pindexNew);
5603          assert(pindexFork != nullptr);
5604      }
5605  
5606      // Rollback along the old branch.
5607      while (pindexOld != pindexFork) {
5608          if (pindexOld->nHeight > 0) { // Never disconnect the genesis block.
5609              CBlock block;
5610              if (!m_blockman.ReadBlock(block, *pindexOld, /*lowprio=*/true)) {
5611                  LogError("RollbackBlock(): ReadBlock() failed at %d, hash=%s\n", pindexOld->nHeight, pindexOld->GetBlockHash().ToString());
5612                  return false;
5613              }
5614              LogPrintf("Rolling back %s (%i)\n", pindexOld->GetBlockHash().ToString(), pindexOld->nHeight);
5615              DisconnectResult res = DisconnectBlock(block, pindexOld, cache);
5616              if (res == DISCONNECT_FAILED) {
5617                  LogError("RollbackBlock(): DisconnectBlock failed at %d, hash=%s\n", pindexOld->nHeight, pindexOld->GetBlockHash().ToString());
5618                  return false;
5619              }
5620              // If DISCONNECT_UNCLEAN is returned, it means a non-existing UTXO was deleted, or an existing UTXO was
5621              // overwritten. It corresponds to cases where the block-to-be-disconnect never had all its operations
5622              // applied to the UTXO set. However, as both writing a UTXO and deleting a UTXO are idempotent operations,
5623              // the result is still a version of the UTXO set with the effects of that block undone.
5624          }
5625          pindexOld = pindexOld->pprev;
5626      }
5627  
5628      // Roll forward from the forking point to the new tip.
5629      int nForkHeight = pindexFork ? pindexFork->nHeight : 0;
5630      for (int nHeight = nForkHeight + 1; nHeight <= pindexNew->nHeight; ++nHeight) {
5631          const CBlockIndex& pindex{*Assert(pindexNew->GetAncestor(nHeight))};
5632  
5633          LogPrintf("Rolling forward %s (%i)\n", pindex.GetBlockHash().ToString(), nHeight);
5634          m_chainman.GetNotifications().progress(_("Replaying blocks…"), (int)((nHeight - nForkHeight) * 100.0 / (pindexNew->nHeight - nForkHeight)), false);
5635          if (!RollforwardBlock(&pindex, cache)) return false;
5636      }
5637  
5638      cache.SetBestBlock(pindexNew->GetBlockHash());
5639      cache.Flush();
5640      m_chainman.GetNotifications().progress(bilingual_str{}, 100, false);
5641      return true;
5642  }
5643  
5644  bool Chainstate::NeedsRedownload() const
5645  {
5646      AssertLockHeld(cs_main);
5647  
5648      // At and above m_params.SegwitHeight, segwit consensus rules must be validated
5649      CBlockIndex* block{m_chain.Tip()};
5650  
5651      while (block != nullptr && DeploymentActiveAt(*block, m_chainman, Consensus::DEPLOYMENT_SEGWIT)) {
5652          if (!(block->nStatus & BLOCK_OPT_WITNESS)) {
5653              // block is insufficiently validated for a segwit client
5654              return true;
5655          }
5656          block = block->pprev;
5657      }
5658  
5659      return false;
5660  }
5661  
5662  void Chainstate::ClearBlockIndexCandidates()
5663  {
5664      AssertLockHeld(::cs_main);
5665      setBlockIndexCandidates.clear();
5666  }
5667  
5668  bool ChainstateManager::LoadBlockIndex()
5669  {
5670      AssertLockHeld(cs_main);
5671      // Load block index from databases
5672      if (m_blockman.m_blockfiles_indexed) {
5673          bool ret{m_blockman.LoadBlockIndexDB(SnapshotBlockhash())};
5674          if (!ret) return false;
5675  
5676          m_blockman.ScanAndUnlinkAlreadyPrunedFiles();
5677  
5678          std::vector<CBlockIndex*> vSortedByHeight{m_blockman.GetAllBlockIndices()};
5679          std::sort(vSortedByHeight.begin(), vSortedByHeight.end(),
5680                    CBlockIndexHeightOnlyComparator());
5681  
5682          for (CBlockIndex* pindex : vSortedByHeight) {
5683              if (m_interrupt) return false;
5684              // If we have an assumeutxo-based chainstate, then the snapshot
5685              // block will be a candidate for the tip, but it may not be
5686              // VALID_TRANSACTIONS (eg if we haven't yet downloaded the block),
5687              // so we special-case the snapshot block as a potential candidate
5688              // here.
5689              if (pindex == GetSnapshotBaseBlock() ||
5690                      (pindex->IsValid(BLOCK_VALID_TRANSACTIONS) &&
5691                       (pindex->HaveNumChainTxs() || pindex->pprev == nullptr))) {
5692  
5693                  for (Chainstate* chainstate : GetAll()) {
5694                      chainstate->TryAddBlockIndexCandidate(pindex);
5695                  }
5696              }
5697              if (pindex->nStatus & BLOCK_FAILED_MASK && (!m_best_invalid || pindex->nChainWork > m_best_invalid->nChainWork)) {
5698                  m_best_invalid = pindex;
5699              }
5700              if (pindex->IsValid(BLOCK_VALID_TREE) && (m_best_header == nullptr || CBlockIndexWorkComparator()(m_best_header, pindex)))
5701                  m_best_header = pindex;
5702          }
5703      }
5704      return true;
5705  }
5706  
5707  bool Chainstate::LoadGenesisBlock()
5708  {
5709      LOCK(cs_main);
5710  
5711      const CChainParams& params{m_chainman.GetParams()};
5712  
5713      // Check whether we're already initialized by checking for genesis in
5714      // m_blockman.m_block_index. Note that we can't use m_chain here, since it is
5715      // set based on the coins db, not the block index db, which is the only
5716      // thing loaded at this point.
5717      if (m_blockman.m_block_index.count(params.GenesisBlock().GetHash()))
5718          return true;
5719  
5720      try {
5721          const CBlock& block = params.GenesisBlock();
5722          FlatFilePos blockPos{m_blockman.WriteBlock(block, 0)};
5723          if (blockPos.IsNull()) {
5724              LogError("%s: writing genesis block to disk failed\n", __func__);
5725              return false;
5726          }
5727          CBlockIndex* pindex = m_blockman.AddToBlockIndex(block, m_chainman.m_best_header);
5728          m_chainman.ReceivedBlockTransactions(block, pindex, blockPos);
5729      } catch (const std::runtime_error& e) {
5730          LogError("%s: failed to write genesis block: %s\n", __func__, e.what());
5731          return false;
5732      }
5733  
5734      return true;
5735  }
5736  
5737  void ChainstateManager::LoadExternalBlockFile(
5738      AutoFile& file_in,
5739      FlatFilePos* dbp,
5740      std::multimap<uint256, FlatFilePos>* blocks_with_unknown_parent)
5741  {
5742      // Either both should be specified (-reindex), or neither (-loadblock).
5743      assert(!dbp == !blocks_with_unknown_parent);
5744  
5745      const auto start{SteadyClock::now()};
5746      const CChainParams& params{GetParams()};
5747  
5748      int nLoaded = 0;
5749      try {
5750          IOPRIO_IDLER(/*lowprio=*/true);
5751          file_in.SetIdlePriority();
5752  
5753          BufferedFile blkdat{file_in, 2 * MAX_BLOCK_SERIALIZED_SIZE, MAX_BLOCK_SERIALIZED_SIZE + 8};
5754          // nRewind indicates where to resume scanning in case something goes wrong,
5755          // such as a block fails to deserialize.
5756          uint64_t nRewind = blkdat.GetPos();
5757          while (!blkdat.eof()) {
5758              if (m_interrupt) return;
5759  
5760              blkdat.SetPos(nRewind);
5761              nRewind++; // start one byte further next time, in case of failure
5762              blkdat.SetLimit(); // remove former limit
5763              unsigned int nSize = 0;
5764              try {
5765                  // locate a header
5766                  MessageStartChars buf;
5767                  blkdat.FindByte(std::byte(params.MessageStart()[0]));
5768                  nRewind = blkdat.GetPos() + 1;
5769                  blkdat >> buf;
5770                  if (buf != params.MessageStart()) {
5771                      continue;
5772                  }
5773                  // read size
5774                  blkdat >> nSize;
5775                  if (nSize < 80 || nSize > MAX_BLOCK_SERIALIZED_SIZE)
5776                      continue;
5777              } catch (const std::exception&) {
5778                  // no valid block header found; don't complain
5779                  // (this happens at the end of every blk.dat file)
5780                  break;
5781              }
5782              try {
5783                  // read block header
5784                  const uint64_t nBlockPos{blkdat.GetPos()};
5785                  if (dbp)
5786                      dbp->nPos = nBlockPos;
5787                  blkdat.SetLimit(nBlockPos + nSize);
5788                  CBlockHeader header;
5789                  blkdat >> header;
5790                  const uint256 hash{header.GetHash()};
5791                  // Skip the rest of this block (this may read from disk into memory); position to the marker before the
5792                  // next block, but it's still possible to rewind to the start of the current block (without a disk read).
5793                  nRewind = nBlockPos + nSize;
5794                  blkdat.SkipTo(nRewind);
5795  
5796                  std::shared_ptr<CBlock> pblock{}; // needs to remain available after the cs_main lock is released to avoid duplicate reads from disk
5797  
5798                  {
5799                      LOCK(cs_main);
5800                      // detect out of order blocks, and store them for later
5801                      if (hash != params.GetConsensus().hashGenesisBlock && !m_blockman.LookupBlockIndex(header.hashPrevBlock)) {
5802                          LogDebug(BCLog::REINDEX, "%s: Out of order block %s, parent %s not known\n", __func__, hash.ToString(),
5803                                   header.hashPrevBlock.ToString());
5804                          if (dbp && blocks_with_unknown_parent) {
5805                              blocks_with_unknown_parent->emplace(header.hashPrevBlock, *dbp);
5806                          }
5807                          continue;
5808                      }
5809  
5810                      // process in case the block isn't known yet
5811                      const CBlockIndex* pindex = m_blockman.LookupBlockIndex(hash);
5812                      if (!pindex || (pindex->nStatus & BLOCK_HAVE_DATA) == 0) {
5813                          // This block can be processed immediately; rewind to its start, read and deserialize it.
5814                          blkdat.SetPos(nBlockPos);
5815                          pblock = std::make_shared<CBlock>();
5816                          blkdat >> TX_WITH_WITNESS(*pblock);
5817                          nRewind = blkdat.GetPos();
5818  
5819                          BlockValidationState state;
5820                          if (AcceptBlock(pblock, state, nullptr, true, dbp, nullptr, true)) {
5821                              nLoaded++;
5822                          }
5823                          if (state.IsError()) {
5824                              break;
5825                          }
5826                      } else if (hash != params.GetConsensus().hashGenesisBlock && pindex->nHeight % 1000 == 0) {
5827                          LogDebug(BCLog::REINDEX, "Block Import: already had block %s at height %d\n", hash.ToString(), pindex->nHeight);
5828                      }
5829                  }
5830  
5831                  // Activate the genesis block so normal node progress can continue
5832                  // During first -reindex, this will only connect Genesis since
5833                  // ActivateBestChain only connects blocks which are in the block tree db,
5834                  // which only contains blocks whose parents are in it.
5835                  // But do this only if genesis isn't activated yet, to avoid connecting many blocks
5836                  // without assumevalid in the case of a continuation of a reindex that
5837                  // was interrupted by the user.
5838                  if (hash == params.GetConsensus().hashGenesisBlock && WITH_LOCK(::cs_main, return ActiveHeight()) == -1) {
5839                      BlockValidationState state;
5840                      if (!ActiveChainstate().ActivateBestChain(state, nullptr)) {
5841                          break;
5842                      }
5843                  }
5844  
5845                  if (m_blockman.IsPruneMode() && m_blockman.m_blockfiles_indexed && pblock) {
5846                      // must update the tip for pruning to work while importing with -loadblock.
5847                      // this is a tradeoff to conserve disk space at the expense of time
5848                      // spent updating the tip to be able to prune.
5849                      // otherwise, ActivateBestChain won't be called by the import process
5850                      // until after all of the block files are loaded. ActivateBestChain can be
5851                      // called by concurrent network message processing. but, that is not
5852                      // reliable for the purpose of pruning while importing.
5853                      bool activation_failure = false;
5854                      for (auto c : GetAll()) {
5855                          BlockValidationState state;
5856                          if (!c->ActivateBestChain(state, pblock)) {
5857                              LogDebug(BCLog::REINDEX, "failed to activate chain (%s)\n", state.ToString());
5858                              activation_failure = true;
5859                              break;
5860                          }
5861                      }
5862                      if (activation_failure) {
5863                          break;
5864                      }
5865                  }
5866  
5867                  NotifyHeaderTip();
5868  
5869                  if (!blocks_with_unknown_parent) continue;
5870  
5871                  // Recursively process earlier encountered successors of this block
5872                  std::deque<uint256> queue;
5873                  queue.push_back(hash);
5874                  while (!queue.empty()) {
5875                      uint256 head = queue.front();
5876                      queue.pop_front();
5877                      auto range = blocks_with_unknown_parent->equal_range(head);
5878                      while (range.first != range.second) {
5879                          std::multimap<uint256, FlatFilePos>::iterator it = range.first;
5880                          std::shared_ptr<CBlock> pblockrecursive = std::make_shared<CBlock>();
5881                          if (m_blockman.ReadBlock(*pblockrecursive, it->second)) {
5882                              LogDebug(BCLog::REINDEX, "%s: Processing out of order child %s of %s\n", __func__, pblockrecursive->GetHash().ToString(),
5883                                      head.ToString());
5884                              LOCK(cs_main);
5885                              BlockValidationState dummy;
5886                              if (AcceptBlock(pblockrecursive, dummy, nullptr, true, &it->second, nullptr, true)) {
5887                                  nLoaded++;
5888                                  queue.push_back(pblockrecursive->GetHash());
5889                              }
5890                          }
5891                          range.first++;
5892                          blocks_with_unknown_parent->erase(it);
5893                          NotifyHeaderTip();
5894                      }
5895                  }
5896              } catch (const std::exception& e) {
5897                  // historical bugs added extra data to the block files that does not deserialize cleanly.
5898                  // commonly this data is between readable blocks, but it does not really matter. such data is not fatal to the import process.
5899                  // the code that reads the block files deals with invalid data by simply ignoring it.
5900                  // it continues to search for the next {4 byte magic message start bytes + 4 byte length + block} that does deserialize cleanly
5901                  // and passes all of the other block validation checks dealing with POW and the merkle root, etc...
5902                  // we merely note with this informational log message when unexpected data is encountered.
5903                  // we could also be experiencing a storage system read error, or a read of a previous bad write. these are possible, but
5904                  // less likely scenarios. we don't have enough information to tell a difference here.
5905                  // the reindex process is not the place to attempt to clean and/or compact the block files. if so desired, a studious node operator
5906                  // may use knowledge of the fact that the block files are not entirely pristine in order to prepare a set of pristine, and
5907                  // perhaps ordered, block files for later reindexing.
5908                  LogDebug(BCLog::REINDEX, "%s: unexpected data at file offset 0x%x - %s. continuing\n", __func__, (nRewind - 1), e.what());
5909              }
5910          }
5911      } catch (const std::runtime_error& e) {
5912          GetNotifications().fatalError(strprintf(_("System error while loading external block file: %s"), e.what()));
5913      }
5914      LogPrintf("Loaded %i blocks from external file in %dms\n", nLoaded, Ticks<std::chrono::milliseconds>(SteadyClock::now() - start));
5915  }
5916  
5917  bool ChainstateManager::ShouldCheckBlockIndex() const
5918  {
5919      // Assert to verify Flatten() has been called.
5920      if (!*Assert(m_options.check_block_index)) return false;
5921      if (FastRandomContext().randrange(*m_options.check_block_index) >= 1) return false;
5922      return true;
5923  }
5924  
5925  void ChainstateManager::CheckBlockIndex()
5926  {
5927      if (!ShouldCheckBlockIndex()) {
5928          return;
5929      }
5930  
5931      LOCK(cs_main);
5932  
5933      // During a reindex, we read the genesis block and call CheckBlockIndex before ActivateBestChain,
5934      // so we have the genesis block in m_blockman.m_block_index but no active chain. (A few of the
5935      // tests when iterating the block tree require that m_chain has been initialized.)
5936      if (ActiveChain().Height() < 0) {
5937          assert(m_blockman.m_block_index.size() <= 1);
5938          return;
5939      }
5940  
5941      // Build forward-pointing data structure for the entire block tree.
5942      // For performance reasons, indexes of the best header chain are stored in a vector (within CChain).
5943      // All remaining blocks are stored in a multimap.
5944      // The best header chain can differ from the active chain: E.g. its entries may belong to blocks that
5945      // are not yet validated.
5946      CChain best_hdr_chain;
5947      assert(m_best_header);
5948      best_hdr_chain.SetTip(*m_best_header);
5949  
5950      std::multimap<CBlockIndex*,CBlockIndex*> forward;
5951      for (auto& [_, block_index] : m_blockman.m_block_index) {
5952          // Only save indexes in forward that are not part of the best header chain.
5953          if (!best_hdr_chain.Contains(&block_index)) {
5954              // Only genesis, which must be part of the best header chain, can have a nullptr parent.
5955              assert(block_index.pprev);
5956              forward.emplace(block_index.pprev, &block_index);
5957          }
5958      }
5959      assert(forward.size() + best_hdr_chain.Height() + 1 == m_blockman.m_block_index.size());
5960  
5961      CBlockIndex* pindex = best_hdr_chain[0];
5962      assert(pindex);
5963      // Iterate over the entire block tree, using depth-first search.
5964      // Along the way, remember whether there are blocks on the path from genesis
5965      // block being explored which are the first to have certain properties.
5966      size_t nNodes = 0;
5967      int nHeight = 0;
5968      CBlockIndex* pindexFirstInvalid = nullptr; // Oldest ancestor of pindex which is invalid.
5969      CBlockIndex* pindexFirstMissing = nullptr; // Oldest ancestor of pindex which does not have BLOCK_HAVE_DATA, since assumeutxo snapshot if used.
5970      CBlockIndex* pindexFirstNeverProcessed = nullptr; // Oldest ancestor of pindex for which nTx == 0, since assumeutxo snapshot if used.
5971      CBlockIndex* pindexFirstNotTreeValid = nullptr; // Oldest ancestor of pindex which does not have BLOCK_VALID_TREE (regardless of being valid or not).
5972      CBlockIndex* pindexFirstNotTransactionsValid = nullptr; // Oldest ancestor of pindex which does not have BLOCK_VALID_TRANSACTIONS (regardless of being valid or not), since assumeutxo snapshot if used.
5973      CBlockIndex* pindexFirstNotChainValid = nullptr; // Oldest ancestor of pindex which does not have BLOCK_VALID_CHAIN (regardless of being valid or not), since assumeutxo snapshot if used.
5974      CBlockIndex* pindexFirstNotScriptsValid = nullptr; // Oldest ancestor of pindex which does not have BLOCK_VALID_SCRIPTS (regardless of being valid or not), since assumeutxo snapshot if used.
5975  
5976      // After checking an assumeutxo snapshot block, reset pindexFirst pointers
5977      // to earlier blocks that have not been downloaded or validated yet, so
5978      // checks for later blocks can assume the earlier blocks were validated and
5979      // be stricter, testing for more requirements.
5980      const CBlockIndex* snap_base{GetSnapshotBaseBlock()};
5981      CBlockIndex *snap_first_missing{}, *snap_first_notx{}, *snap_first_notv{}, *snap_first_nocv{}, *snap_first_nosv{};
5982      auto snap_update_firsts = [&] {
5983          if (pindex == snap_base) {
5984              std::swap(snap_first_missing, pindexFirstMissing);
5985              std::swap(snap_first_notx, pindexFirstNeverProcessed);
5986              std::swap(snap_first_notv, pindexFirstNotTransactionsValid);
5987              std::swap(snap_first_nocv, pindexFirstNotChainValid);
5988              std::swap(snap_first_nosv, pindexFirstNotScriptsValid);
5989          }
5990      };
5991  
5992      while (pindex != nullptr) {
5993          nNodes++;
5994          if (pindexFirstInvalid == nullptr && pindex->nStatus & BLOCK_FAILED_VALID) pindexFirstInvalid = pindex;
5995          if (pindexFirstMissing == nullptr && !(pindex->nStatus & BLOCK_HAVE_DATA)) {
5996              pindexFirstMissing = pindex;
5997          }
5998          if (pindexFirstNeverProcessed == nullptr && pindex->nTx == 0) pindexFirstNeverProcessed = pindex;
5999          if (pindex->pprev != nullptr && pindexFirstNotTreeValid == nullptr && (pindex->nStatus & BLOCK_VALID_MASK) < BLOCK_VALID_TREE) pindexFirstNotTreeValid = pindex;
6000  
6001          if (pindex->pprev != nullptr) {
6002              if (pindexFirstNotTransactionsValid == nullptr &&
6003                      (pindex->nStatus & BLOCK_VALID_MASK) < BLOCK_VALID_TRANSACTIONS) {
6004                  pindexFirstNotTransactionsValid = pindex;
6005              }
6006  
6007              if (pindexFirstNotChainValid == nullptr &&
6008                      (pindex->nStatus & BLOCK_VALID_MASK) < BLOCK_VALID_CHAIN) {
6009                  pindexFirstNotChainValid = pindex;
6010              }
6011  
6012              if (pindexFirstNotScriptsValid == nullptr &&
6013                      (pindex->nStatus & BLOCK_VALID_MASK) < BLOCK_VALID_SCRIPTS) {
6014                  pindexFirstNotScriptsValid = pindex;
6015              }
6016          }
6017  
6018          // Begin: actual consistency checks.
6019          if (pindex->pprev == nullptr) {
6020              // Genesis block checks.
6021              assert(pindex->GetBlockHash() == GetConsensus().hashGenesisBlock); // Genesis block's hash must match.
6022              for (auto c : GetAll()) {
6023                  if (c->m_chain.Genesis() != nullptr) {
6024                      assert(pindex == c->m_chain.Genesis()); // The chain's genesis block must be this block.
6025                  }
6026              }
6027          }
6028          // nSequenceId can't be set higher than SEQ_ID_INIT_FROM_DISK{1} for blocks that aren't linked
6029          // (negative is used for preciousblock, SEQ_ID_BEST_CHAIN_FROM_DISK{0} for active chain when loaded from disk)
6030          if (!pindex->HaveNumChainTxs()) assert(pindex->nSequenceId <= SEQ_ID_INIT_FROM_DISK);
6031          // VALID_TRANSACTIONS is equivalent to nTx > 0 for all nodes (whether or not pruning has occurred).
6032          // HAVE_DATA is only equivalent to nTx > 0 (or VALID_TRANSACTIONS) if no pruning has occurred.
6033          if (!m_blockman.m_have_pruned) {
6034              // If we've never pruned, then HAVE_DATA should be equivalent to nTx > 0
6035              assert(!(pindex->nStatus & BLOCK_HAVE_DATA) == (pindex->nTx == 0));
6036              assert(pindexFirstMissing == pindexFirstNeverProcessed);
6037          } else {
6038              // If we have pruned, then we can only say that HAVE_DATA implies nTx > 0
6039              if (pindex->nStatus & BLOCK_HAVE_DATA) assert(pindex->nTx > 0);
6040          }
6041          if (pindex->nStatus & BLOCK_HAVE_UNDO) assert(pindex->nStatus & BLOCK_HAVE_DATA);
6042          if (snap_base && snap_base->GetAncestor(pindex->nHeight) == pindex) {
6043              // Assumed-valid blocks should connect to the main chain.
6044              assert((pindex->nStatus & BLOCK_VALID_MASK) >= BLOCK_VALID_TREE);
6045          }
6046          // There should only be an nTx value if we have
6047          // actually seen a block's transactions.
6048          assert(((pindex->nStatus & BLOCK_VALID_MASK) >= BLOCK_VALID_TRANSACTIONS) == (pindex->nTx > 0)); // This is pruning-independent.
6049          // All parents having had data (at some point) is equivalent to all parents being VALID_TRANSACTIONS, which is equivalent to HaveNumChainTxs().
6050          // HaveNumChainTxs will also be set in the assumeutxo snapshot block from snapshot metadata.
6051          assert((pindexFirstNeverProcessed == nullptr || pindex == snap_base) == pindex->HaveNumChainTxs());
6052          assert((pindexFirstNotTransactionsValid == nullptr || pindex == snap_base) == pindex->HaveNumChainTxs());
6053          assert(pindex->nHeight == nHeight); // nHeight must be consistent.
6054          assert(pindex->pprev == nullptr || pindex->nChainWork >= pindex->pprev->nChainWork); // For every block except the genesis block, the chainwork must be larger than the parent's.
6055          assert(nHeight < 2 || (pindex->pskip && (pindex->pskip->nHeight < nHeight))); // The pskip pointer must point back for all but the first 2 blocks.
6056          assert(pindexFirstNotTreeValid == nullptr); // All m_blockman.m_block_index entries must at least be TREE valid
6057          if ((pindex->nStatus & BLOCK_VALID_MASK) >= BLOCK_VALID_TREE) assert(pindexFirstNotTreeValid == nullptr); // TREE valid implies all parents are TREE valid
6058          if ((pindex->nStatus & BLOCK_VALID_MASK) >= BLOCK_VALID_CHAIN) assert(pindexFirstNotChainValid == nullptr); // CHAIN valid implies all parents are CHAIN valid
6059          if ((pindex->nStatus & BLOCK_VALID_MASK) >= BLOCK_VALID_SCRIPTS) assert(pindexFirstNotScriptsValid == nullptr); // SCRIPTS valid implies all parents are SCRIPTS valid
6060          if (pindexFirstInvalid == nullptr) {
6061              // Checks for not-invalid blocks.
6062              assert((pindex->nStatus & BLOCK_FAILED_MASK) == 0); // The failed mask cannot be set for blocks without invalid parents.
6063          }
6064          // Make sure m_chain_tx_count sum is correctly computed.
6065          if (!pindex->pprev) {
6066              // If no previous block, nTx and m_chain_tx_count must be the same.
6067              assert(pindex->m_chain_tx_count == pindex->nTx);
6068          } else if (pindex->pprev->m_chain_tx_count > 0 && pindex->nTx > 0) {
6069              // If previous m_chain_tx_count is set and number of transactions in block is known, sum must be set.
6070              assert(pindex->m_chain_tx_count == pindex->nTx + pindex->pprev->m_chain_tx_count);
6071          } else {
6072              // Otherwise m_chain_tx_count should only be set if this is a snapshot
6073              // block, and must be set if it is.
6074              assert((pindex->m_chain_tx_count != 0) == (pindex == snap_base));
6075          }
6076  
6077          // Chainstate-specific checks on setBlockIndexCandidates
6078          for (auto c : GetAll()) {
6079              if (c->m_chain.Tip() == nullptr) continue;
6080              // Two main factors determine whether pindex is a candidate in
6081              // setBlockIndexCandidates:
6082              //
6083              // - If pindex has less work than the chain tip, it should not be a
6084              //   candidate, and this will be asserted below. Otherwise it is a
6085              //   potential candidate.
6086              //
6087              // - If pindex or one of its parent blocks back to the genesis block
6088              //   or an assumeutxo snapshot never downloaded transactions
6089              //   (pindexFirstNeverProcessed is non-null), it should not be a
6090              //   candidate, and this will be asserted below. The only exception
6091              //   is if pindex itself is an assumeutxo snapshot block. Then it is
6092              //   also a potential candidate.
6093              if (!CBlockIndexWorkComparator()(pindex, c->m_chain.Tip()) && (pindexFirstNeverProcessed == nullptr || pindex == snap_base)) {
6094                  // If pindex was detected as invalid (pindexFirstInvalid is
6095                  // non-null), it is not required to be in
6096                  // setBlockIndexCandidates.
6097                  if (pindexFirstInvalid == nullptr) {
6098                      // If pindex and all its parents back to the genesis block
6099                      // or an assumeutxo snapshot block downloaded transactions,
6100                      // and the transactions were not pruned (pindexFirstMissing
6101                      // is null), it is a potential candidate. The check
6102                      // excludes pruned blocks, because if any blocks were
6103                      // pruned between pindex and the current chain tip, pindex will
6104                      // only temporarily be added to setBlockIndexCandidates,
6105                      // before being moved to m_blocks_unlinked. This check
6106                      // could be improved to verify that if all blocks between
6107                      // the chain tip and pindex have data, pindex must be a
6108                      // candidate.
6109                      //
6110                      // If pindex is the chain tip, it also is a potential
6111                      // candidate.
6112                      //
6113                      // If the chainstate was loaded from a snapshot and pindex
6114                      // is the base of the snapshot, pindex is also a potential
6115                      // candidate.
6116                      if (pindexFirstMissing == nullptr || pindex == c->m_chain.Tip() || pindex == c->SnapshotBase()) {
6117                          // If this chainstate is the active chainstate, pindex
6118                          // must be in setBlockIndexCandidates. Otherwise, this
6119                          // chainstate is a background validation chainstate, and
6120                          // pindex only needs to be added if it is an ancestor of
6121                          // the snapshot that is being validated.
6122                          if (c == &ActiveChainstate() || snap_base->GetAncestor(pindex->nHeight) == pindex) {
6123                              assert(c->setBlockIndexCandidates.count(pindex));
6124                          }
6125                      }
6126                      // If some parent is missing, then it could be that this block was in
6127                      // setBlockIndexCandidates but had to be removed because of the missing data.
6128                      // In this case it must be in m_blocks_unlinked -- see test below.
6129                  }
6130              } else { // If this block sorts worse than the current tip or some ancestor's block has never been seen, it cannot be in setBlockIndexCandidates.
6131                  assert(c->setBlockIndexCandidates.count(pindex) == 0);
6132              }
6133          }
6134          // Check whether this block is in m_blocks_unlinked.
6135          std::pair<std::multimap<CBlockIndex*,CBlockIndex*>::iterator,std::multimap<CBlockIndex*,CBlockIndex*>::iterator> rangeUnlinked = m_blockman.m_blocks_unlinked.equal_range(pindex->pprev);
6136          bool foundInUnlinked = false;
6137          while (rangeUnlinked.first != rangeUnlinked.second) {
6138              assert(rangeUnlinked.first->first == pindex->pprev);
6139              if (rangeUnlinked.first->second == pindex) {
6140                  foundInUnlinked = true;
6141                  break;
6142              }
6143              rangeUnlinked.first++;
6144          }
6145          if (pindex->pprev && (pindex->nStatus & BLOCK_HAVE_DATA) && pindexFirstNeverProcessed != nullptr && pindexFirstInvalid == nullptr) {
6146              // If this block has block data available, some parent was never received, and has no invalid parents, it must be in m_blocks_unlinked.
6147              assert(foundInUnlinked);
6148          }
6149          if (!(pindex->nStatus & BLOCK_HAVE_DATA)) assert(!foundInUnlinked); // Can't be in m_blocks_unlinked if we don't HAVE_DATA
6150          if (pindexFirstMissing == nullptr) assert(!foundInUnlinked); // We aren't missing data for any parent -- cannot be in m_blocks_unlinked.
6151          if (pindex->pprev && (pindex->nStatus & BLOCK_HAVE_DATA) && pindexFirstNeverProcessed == nullptr && pindexFirstMissing != nullptr) {
6152              // We HAVE_DATA for this block, have received data for all parents at some point, but we're currently missing data for some parent.
6153              assert(m_blockman.m_have_pruned);
6154              // This block may have entered m_blocks_unlinked if:
6155              //  - it has a descendant that at some point had more work than the
6156              //    tip, and
6157              //  - we tried switching to that descendant but were missing
6158              //    data for some intermediate block between m_chain and the
6159              //    tip.
6160              // So if this block is itself better than any m_chain.Tip() and it wasn't in
6161              // setBlockIndexCandidates, then it must be in m_blocks_unlinked.
6162              for (auto c : GetAll()) {
6163                  const bool is_active = c == &ActiveChainstate();
6164                  if (!CBlockIndexWorkComparator()(pindex, c->m_chain.Tip()) && c->setBlockIndexCandidates.count(pindex) == 0) {
6165                      if (pindexFirstInvalid == nullptr) {
6166                          if (is_active || snap_base->GetAncestor(pindex->nHeight) == pindex) {
6167                              assert(foundInUnlinked);
6168                          }
6169                      }
6170                  }
6171              }
6172          }
6173          // assert(pindex->GetBlockHash() == pindex->GetBlockHeader().GetHash()); // Perhaps too slow
6174          // End: actual consistency checks.
6175  
6176  
6177          // Try descending into the first subnode. Always process forks first and the best header chain after.
6178          snap_update_firsts();
6179          std::pair<std::multimap<CBlockIndex*,CBlockIndex*>::iterator,std::multimap<CBlockIndex*,CBlockIndex*>::iterator> range = forward.equal_range(pindex);
6180          if (range.first != range.second) {
6181              // A subnode not part of the best header chain was found.
6182              pindex = range.first->second;
6183              nHeight++;
6184              continue;
6185          } else if (best_hdr_chain.Contains(pindex)) {
6186              // Descend further into best header chain.
6187              nHeight++;
6188              pindex = best_hdr_chain[nHeight];
6189              if (!pindex) break; // we are finished, since the best header chain is always processed last
6190              continue;
6191          }
6192          // This is a leaf node.
6193          // Move upwards until we reach a node of which we have not yet visited the last child.
6194          while (pindex) {
6195              // We are going to either move to a parent or a sibling of pindex.
6196              snap_update_firsts();
6197              // If pindex was the first with a certain property, unset the corresponding variable.
6198              if (pindex == pindexFirstInvalid) pindexFirstInvalid = nullptr;
6199              if (pindex == pindexFirstMissing) pindexFirstMissing = nullptr;
6200              if (pindex == pindexFirstNeverProcessed) pindexFirstNeverProcessed = nullptr;
6201              if (pindex == pindexFirstNotTreeValid) pindexFirstNotTreeValid = nullptr;
6202              if (pindex == pindexFirstNotTransactionsValid) pindexFirstNotTransactionsValid = nullptr;
6203              if (pindex == pindexFirstNotChainValid) pindexFirstNotChainValid = nullptr;
6204              if (pindex == pindexFirstNotScriptsValid) pindexFirstNotScriptsValid = nullptr;
6205              // Find our parent.
6206              CBlockIndex* pindexPar = pindex->pprev;
6207              // Find which child we just visited.
6208              std::pair<std::multimap<CBlockIndex*,CBlockIndex*>::iterator,std::multimap<CBlockIndex*,CBlockIndex*>::iterator> rangePar = forward.equal_range(pindexPar);
6209              while (rangePar.first->second != pindex) {
6210                  assert(rangePar.first != rangePar.second); // Our parent must have at least the node we're coming from as child.
6211                  rangePar.first++;
6212              }
6213              // Proceed to the next one.
6214              rangePar.first++;
6215              if (rangePar.first != rangePar.second) {
6216                  // Move to a sibling not part of the best header chain.
6217                  pindex = rangePar.first->second;
6218                  break;
6219              } else if (pindexPar == best_hdr_chain[nHeight - 1]) {
6220                  // Move to pindex's sibling on the best-chain, if it has one.
6221                  pindex = best_hdr_chain[nHeight];
6222                  // There will not be a next block if (and only if) parent block is the best header.
6223                  assert((pindex == nullptr) == (pindexPar == best_hdr_chain.Tip()));
6224                  break;
6225              } else {
6226                  // Move up further.
6227                  pindex = pindexPar;
6228                  nHeight--;
6229                  continue;
6230              }
6231          }
6232      }
6233  
6234      // Check that we actually traversed the entire block index.
6235      assert(nNodes == forward.size() + best_hdr_chain.Height() + 1);
6236  }
6237  
6238  std::string Chainstate::ToString()
6239  {
6240      AssertLockHeld(::cs_main);
6241      CBlockIndex* tip = m_chain.Tip();
6242      return strprintf("Chainstate [%s] @ height %d (%s)",
6243                       m_from_snapshot_blockhash ? "snapshot" : "ibd",
6244                       tip ? tip->nHeight : -1, tip ? tip->GetBlockHash().ToString() : "null");
6245  }
6246  
6247  bool Chainstate::ResizeCoinsCaches(size_t coinstip_size, size_t coinsdb_size)
6248  {
6249      AssertLockHeld(::cs_main);
6250      if (coinstip_size == m_coinstip_cache_size_bytes &&
6251              coinsdb_size == m_coinsdb_cache_size_bytes) {
6252          // Cache sizes are unchanged, no need to continue.
6253          return true;
6254      }
6255      size_t old_coinstip_size = m_coinstip_cache_size_bytes;
6256      m_coinstip_cache_size_bytes = coinstip_size;
6257      m_coinsdb_cache_size_bytes = coinsdb_size;
6258      CoinsDB().ResizeCache(coinsdb_size);
6259  
6260      LogPrintf("[%s] resized coinsdb cache to %.1f MiB\n",
6261          this->ToString(), coinsdb_size * (1.0 / 1024 / 1024));
6262      LogPrintf("[%s] resized coinstip cache to %.1f MiB\n",
6263          this->ToString(), coinstip_size * (1.0 / 1024 / 1024));
6264  
6265      BlockValidationState state;
6266      bool ret;
6267  
6268      if (coinstip_size > old_coinstip_size) {
6269          // Likely no need to flush if cache sizes have grown.
6270          ret = FlushStateToDisk(state, FlushStateMode::IF_NEEDED);
6271      } else {
6272          // Otherwise, flush state to disk and deallocate the in-memory coins map.
6273          ret = FlushStateToDisk(state, FlushStateMode::ALWAYS);
6274      }
6275      return ret;
6276  }
6277  
6278  //! Guess how far we are in the verification process at the given block index
6279  //! require cs_main if pindex has not been validated yet (because m_chain_tx_count might be unset)
6280  std::optional<int64_t> ChainstateManager::GetForkMinInterval(int window) const
6281  {
6282      AssertLockHeld(::cs_main);
6283      if (GetParams().GetChainType() != ChainType::FORK) return std::nullopt;
6284      const CBlockIndex* pindex = ActiveChain().Tip();
6285      if (pindex == nullptr) return std::nullopt;
6286      std::optional<int64_t> best;
6287      int seen = 0;
6288      for (; pindex && pindex->pprev && seen < window; pindex = pindex->pprev) {
6289          if (!IsForkActive(pindex->pprev, GetConsensus())) break;
6290          int64_t delta = pindex->nTime - pindex->pprev->nTime;
6291          if (delta < 1) delta = 1;
6292          if (!best || delta < *best) best = delta;
6293          seen++;
6294      }
6295      return best;
6296  }
6297  
6298  double ChainstateManager::GuessVerificationProgress(const CBlockIndex* pindex) const
6299  {
6300      const ChainTxData& data{GetParams().TxData()};
6301      if (pindex == nullptr) {
6302          return 0.0;
6303      }
6304  
6305      if (pindex->m_chain_tx_count == 0) {
6306          LogDebug(BCLog::VALIDATION, "Block %d has unset m_chain_tx_count. Unable to estimate verification progress.\n", pindex->nHeight);
6307          return 0.0;
6308      }
6309  
6310      int64_t nNow = time(nullptr);
6311  
6312      double fTxTotal;
6313  
6314      if (pindex->m_chain_tx_count <= data.tx_count) {
6315          fTxTotal = data.tx_count + (nNow - data.nTime) * data.dTxRate;
6316      } else {
6317          fTxTotal = pindex->m_chain_tx_count + (nNow - pindex->GetBlockTime()) * data.dTxRate;
6318      }
6319  
6320      return std::min<double>(pindex->m_chain_tx_count / fTxTotal, 1.0);
6321  }
6322  
6323  std::optional<uint256> ChainstateManager::SnapshotBlockhash() const
6324  {
6325      LOCK(::cs_main);
6326      if (m_active_chainstate && m_active_chainstate->m_from_snapshot_blockhash) {
6327          // If a snapshot chainstate exists, it will always be our active.
6328          return m_active_chainstate->m_from_snapshot_blockhash;
6329      }
6330      return std::nullopt;
6331  }
6332  
6333  std::vector<Chainstate*> ChainstateManager::GetAll()
6334  {
6335      LOCK(::cs_main);
6336      std::vector<Chainstate*> out;
6337  
6338      for (Chainstate* cs : {m_ibd_chainstate.get(), m_snapshot_chainstate.get()}) {
6339          if (this->IsUsable(cs)) out.push_back(cs);
6340      }
6341  
6342      return out;
6343  }
6344  
6345  Chainstate& ChainstateManager::InitializeChainstate(CTxMemPool* mempool)
6346  {
6347      AssertLockHeld(::cs_main);
6348      assert(!m_ibd_chainstate);
6349      assert(!m_active_chainstate);
6350  
6351      m_ibd_chainstate = std::make_unique<Chainstate>(mempool, m_blockman, *this);
6352      m_active_chainstate = m_ibd_chainstate.get();
6353      return *m_active_chainstate;
6354  }
6355  
6356  [[nodiscard]] static bool DeleteCoinsDBFromDisk(const fs::path db_path, bool is_snapshot)
6357      EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
6358  {
6359      AssertLockHeld(::cs_main);
6360  
6361      if (is_snapshot) {
6362          fs::path base_blockhash_path = db_path / node::SNAPSHOT_BLOCKHASH_FILENAME;
6363  
6364          try {
6365              bool existed = fs::remove(base_blockhash_path);
6366              if (!existed) {
6367                  LogWarning("[snapshot] snapshot chainstate dir being removed lacks %s file",
6368                            fs::PathToString(node::SNAPSHOT_BLOCKHASH_FILENAME));
6369              }
6370          } catch (const fs::filesystem_error& e) {
6371              LogPrintf("[snapshot] failed to remove file %s: %s\n",
6372                      fs::PathToString(base_blockhash_path), fsbridge::get_filesystem_error_message(e));
6373          }
6374      }
6375  
6376      std::string path_str = fs::PathToString(db_path);
6377      LogPrintf("Removing leveldb dir at %s\n", path_str);
6378  
6379      // We have to destruct before this call leveldb::DB in order to release the db
6380      // lock, otherwise `DestroyDB` will fail. See `leveldb::~DBImpl()`.
6381      const bool destroyed = DestroyDB(path_str);
6382  
6383      if (!destroyed) {
6384          LogError("leveldb DestroyDB call failed on %s", path_str);
6385      }
6386  
6387      // Datadir should be removed from filesystem; otherwise initialization may detect
6388      // it on subsequent statups and get confused.
6389      //
6390      // If the base_blockhash_path removal above fails in the case of snapshot
6391      // chainstates, this will return false since leveldb won't remove a non-empty
6392      // directory.
6393      return destroyed && !fs::exists(db_path);
6394  }
6395  
6396  util::Result<CBlockIndex*> ChainstateManager::ActivateSnapshot(
6397          AutoFile& coins_file,
6398          const SnapshotMetadata& metadata,
6399          bool in_memory)
6400  {
6401      uint256 base_blockhash = metadata.m_base_blockhash;
6402  
6403      if (this->SnapshotBlockhash()) {
6404          return util::Error{Untranslated("Can't activate a snapshot-based chainstate more than once")};
6405      }
6406  
6407      CBlockIndex* snapshot_start_block{};
6408  
6409      {
6410          LOCK(::cs_main);
6411  
6412          if (!GetParams().AssumeutxoForBlockhash(base_blockhash).has_value()) {
6413              auto available_heights = GetParams().GetAvailableSnapshotHeights();
6414              std::string heights_formatted = util::Join(available_heights, ", ", [&](const auto& i) { return util::ToString(i); });
6415              return util::Error{Untranslated(strprintf("assumeutxo block hash in snapshot metadata not recognized (hash: %s). The following snapshot heights are available: %s",
6416                  base_blockhash.ToString(),
6417                  heights_formatted))};
6418          }
6419  
6420          snapshot_start_block = m_blockman.LookupBlockIndex(base_blockhash);
6421          if (!snapshot_start_block) {
6422              return util::Error{Untranslated(strprintf("The base block header (%s) must appear in the headers chain. Make sure all headers are syncing, and call loadtxoutset again",
6423                            base_blockhash.ToString()))};
6424          }
6425  
6426          bool start_block_invalid = snapshot_start_block->nStatus & BLOCK_FAILED_MASK;
6427          if (start_block_invalid) {
6428              return util::Error{Untranslated(strprintf("The base block header (%s) is part of an invalid chain", base_blockhash.ToString()))};
6429          }
6430  
6431          if (!m_best_header || m_best_header->GetAncestor(snapshot_start_block->nHeight) != snapshot_start_block) {
6432              return util::Error{Untranslated("A forked headers-chain with more work than the chain with the snapshot base block header exists. Please proceed to sync without AssumeUtxo.")};
6433          }
6434  
6435          auto mempool{m_active_chainstate->GetMempool()};
6436          if (mempool && mempool->size() > 0) {
6437              return util::Error{Untranslated("Can't activate a snapshot when mempool not empty")};
6438          }
6439      }
6440  
6441      int64_t current_coinsdb_cache_size{0};
6442      int64_t current_coinstip_cache_size{0};
6443  
6444      // Cache percentages to allocate to each chainstate.
6445      //
6446      // These particular percentages don't matter so much since they will only be
6447      // relevant during snapshot activation; caches are rebalanced at the conclusion of
6448      // this function. We want to give (essentially) all available cache capacity to the
6449      // snapshot to aid the bulk load later in this function.
6450      static constexpr double IBD_CACHE_PERC = 0.01;
6451      static constexpr double SNAPSHOT_CACHE_PERC = 0.99;
6452  
6453      {
6454          LOCK(::cs_main);
6455          // Resize the coins caches to ensure we're not exceeding memory limits.
6456          //
6457          // Allocate the majority of the cache to the incoming snapshot chainstate, since
6458          // (optimistically) getting to its tip will be the top priority. We'll need to call
6459          // `MaybeRebalanceCaches()` once we're done with this function to ensure
6460          // the right allocation (including the possibility that no snapshot was activated
6461          // and that we should restore the active chainstate caches to their original size).
6462          //
6463          current_coinsdb_cache_size = this->ActiveChainstate().m_coinsdb_cache_size_bytes;
6464          current_coinstip_cache_size = this->ActiveChainstate().m_coinstip_cache_size_bytes;
6465  
6466          // Temporarily resize the active coins cache to make room for the newly-created
6467          // snapshot chain.
6468          this->ActiveChainstate().ResizeCoinsCaches(
6469              static_cast<size_t>(current_coinstip_cache_size * IBD_CACHE_PERC),
6470              static_cast<size_t>(current_coinsdb_cache_size * IBD_CACHE_PERC));
6471      }
6472  
6473      auto snapshot_chainstate = WITH_LOCK(::cs_main,
6474          return std::make_unique<Chainstate>(
6475              /*mempool=*/nullptr, m_blockman, *this, base_blockhash));
6476  
6477      {
6478          LOCK(::cs_main);
6479          snapshot_chainstate->InitCoinsDB(
6480              static_cast<size_t>(current_coinsdb_cache_size * SNAPSHOT_CACHE_PERC),
6481              in_memory, false, "chainstate");
6482          snapshot_chainstate->InitCoinsCache(
6483              static_cast<size_t>(current_coinstip_cache_size * SNAPSHOT_CACHE_PERC));
6484      }
6485  
6486      auto cleanup_bad_snapshot = [&](bilingual_str reason) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
6487          this->MaybeRebalanceCaches();
6488  
6489          // PopulateAndValidateSnapshot can return (in error) before the leveldb datadir
6490          // has been created, so only attempt removal if we got that far.
6491          if (auto snapshot_datadir = node::FindSnapshotChainstateDir(m_options.datadir)) {
6492              // We have to destruct leveldb::DB in order to release the db lock, otherwise
6493              // DestroyDB() (in DeleteCoinsDBFromDisk()) will fail. See `leveldb::~DBImpl()`.
6494              // Destructing the chainstate (and so resetting the coinsviews object) does this.
6495              snapshot_chainstate.reset();
6496              bool removed = DeleteCoinsDBFromDisk(*snapshot_datadir, /*is_snapshot=*/true);
6497              if (!removed) {
6498                  GetNotifications().fatalError(strprintf(_("Failed to remove snapshot chainstate dir (%s). "
6499                      "Manually remove it before restarting.\n"), fs::PathToString(*snapshot_datadir)));
6500              }
6501          }
6502          return util::Error{std::move(reason)};
6503      };
6504  
6505      if (auto res{this->PopulateAndValidateSnapshot(*snapshot_chainstate, coins_file, metadata)}; !res) {
6506          LOCK(::cs_main);
6507          return cleanup_bad_snapshot(Untranslated(strprintf("Population failed: %s", util::ErrorString(res).original)));
6508      }
6509  
6510      LOCK(::cs_main);  // cs_main required for rest of snapshot activation.
6511  
6512      // Do a final check to ensure that the snapshot chainstate is actually a more
6513      // work chain than the active chainstate; a user could have loaded a snapshot
6514      // very late in the IBD process, and we wouldn't want to load a useless chainstate.
6515      if (!CBlockIndexWorkComparator()(ActiveTip(), snapshot_chainstate->m_chain.Tip())) {
6516          return cleanup_bad_snapshot(Untranslated("work does not exceed active chainstate"));
6517      }
6518      // If not in-memory, persist the base blockhash for use during subsequent
6519      // initialization.
6520      if (!in_memory) {
6521          if (!node::WriteSnapshotBaseBlockhash(*snapshot_chainstate)) {
6522              return cleanup_bad_snapshot(Untranslated("could not write base blockhash"));
6523          }
6524      }
6525  
6526      assert(!m_snapshot_chainstate);
6527      m_snapshot_chainstate.swap(snapshot_chainstate);
6528      const bool chaintip_loaded = m_snapshot_chainstate->LoadChainTip();
6529      assert(chaintip_loaded);
6530  
6531      // Transfer possession of the mempool to the snapshot chainstate.
6532      // Mempool is empty at this point because we're still in IBD.
6533      Assert(m_active_chainstate->m_mempool->size() == 0);
6534      Assert(!m_snapshot_chainstate->m_mempool);
6535      m_snapshot_chainstate->m_mempool = m_active_chainstate->m_mempool;
6536      m_active_chainstate->m_mempool = nullptr;
6537      m_active_chainstate = m_snapshot_chainstate.get();
6538      m_blockman.m_snapshot_height = this->GetSnapshotBaseHeight();
6539  
6540      LogPrintf("[snapshot] successfully activated snapshot %s\n", base_blockhash.ToString());
6541      LogPrintf("[snapshot] (%.2f MB)\n",
6542          m_snapshot_chainstate->CoinsTip().DynamicMemoryUsage() / (1000 * 1000));
6543  
6544      this->MaybeRebalanceCaches();
6545      return snapshot_start_block;
6546  }
6547  
6548  static void FlushSnapshotToDisk(CCoinsViewCache& coins_cache, bool snapshot_loaded)
6549  {
6550      LOG_TIME_MILLIS_WITH_CATEGORY_MSG_ONCE(
6551          strprintf("%s (%.2f MB)",
6552                    snapshot_loaded ? "saving snapshot chainstate" : "flushing coins cache",
6553                    coins_cache.DynamicMemoryUsage() / (1000 * 1000)),
6554          BCLog::LogFlags::ALL);
6555  
6556      coins_cache.Flush();
6557  }
6558  
6559  struct StopHashingException : public std::exception
6560  {
6561      const char* what() const noexcept override
6562      {
6563          return "ComputeUTXOStats interrupted.";
6564      }
6565  };
6566  
6567  static void SnapshotUTXOHashBreakpoint(const util::SignalInterrupt& interrupt)
6568  {
6569      if (interrupt) throw StopHashingException();
6570  }
6571  
6572  util::Result<void> ChainstateManager::PopulateAndValidateSnapshot(
6573      Chainstate& snapshot_chainstate,
6574      AutoFile& coins_file,
6575      const SnapshotMetadata& metadata)
6576  {
6577      // It's okay to release cs_main before we're done using `coins_cache` because we know
6578      // that nothing else will be referencing the newly created snapshot_chainstate yet.
6579      CCoinsViewCache& coins_cache = *WITH_LOCK(::cs_main, return &snapshot_chainstate.CoinsTip());
6580  
6581      uint256 base_blockhash = metadata.m_base_blockhash;
6582  
6583      CBlockIndex* snapshot_start_block = WITH_LOCK(::cs_main, return m_blockman.LookupBlockIndex(base_blockhash));
6584  
6585      if (!snapshot_start_block) {
6586          // Needed for ComputeUTXOStats to determine the
6587          // height and to avoid a crash when base_blockhash.IsNull()
6588          return util::Error{Untranslated(strprintf("Did not find snapshot start blockheader %s",
6589                    base_blockhash.ToString()))};
6590      }
6591  
6592      int base_height = snapshot_start_block->nHeight;
6593      const auto& maybe_au_data = GetParams().AssumeutxoForHeight(base_height);
6594  
6595      if (!maybe_au_data) {
6596          return util::Error{Untranslated(strprintf("Assumeutxo height in snapshot metadata not recognized "
6597                    "(%d) - refusing to load snapshot", base_height))};
6598      }
6599  
6600      const AssumeutxoData& au_data = *maybe_au_data;
6601  
6602      // This work comparison is a duplicate check with the one performed later in
6603      // ActivateSnapshot(), but is done so that we avoid doing the long work of staging
6604      // a snapshot that isn't actually usable.
6605      if (WITH_LOCK(::cs_main, return !CBlockIndexWorkComparator()(ActiveTip(), snapshot_start_block))) {
6606          return util::Error{Untranslated("Work does not exceed active chainstate")};
6607      }
6608  
6609      const uint64_t coins_count = metadata.m_coins_count;
6610      uint64_t coins_left = metadata.m_coins_count;
6611  
6612      LogPrintf("[snapshot] loading %d coins from snapshot %s\n", coins_left, base_blockhash.ToString());
6613      int64_t coins_processed{0};
6614  
6615      while (coins_left > 0) {
6616          try {
6617              Txid txid;
6618              coins_file >> txid;
6619              size_t coins_per_txid{0};
6620              coins_per_txid = ReadCompactSize(coins_file);
6621  
6622              if (coins_per_txid > coins_left) {
6623                  return util::Error{Untranslated("Mismatch in coins count in snapshot metadata and actual snapshot data")};
6624              }
6625  
6626              for (size_t i = 0; i < coins_per_txid; i++) {
6627                  COutPoint outpoint;
6628                  Coin coin;
6629                  outpoint.n = static_cast<uint32_t>(ReadCompactSize(coins_file));
6630                  outpoint.hash = txid;
6631                  coins_file >> coin;
6632                  if (coin.nHeight > base_height ||
6633                      outpoint.n >= std::numeric_limits<decltype(outpoint.n)>::max() // Avoid integer wrap-around in coinstats.cpp:ApplyHash
6634                  ) {
6635                      return util::Error{Untranslated(strprintf("Bad snapshot data after deserializing %d coins",
6636                                coins_count - coins_left))};
6637                  }
6638                  if (!MoneyRange(coin.out.nValue)) {
6639                      return util::Error{Untranslated(strprintf("Bad snapshot data after deserializing %d coins - bad tx out value",
6640                                coins_count - coins_left))};
6641                  }
6642                  coins_cache.EmplaceCoinInternalDANGER(std::move(outpoint), std::move(coin));
6643  
6644                  --coins_left;
6645                  ++coins_processed;
6646  
6647                  if (coins_processed % 1000000 == 0) {
6648                      LogPrintf("[snapshot] %d coins loaded (%.2f%%, %.2f MB)\n",
6649                          coins_processed,
6650                          static_cast<float>(coins_processed) * 100 / static_cast<float>(coins_count),
6651                          coins_cache.DynamicMemoryUsage() / (1000 * 1000));
6652                  }
6653  
6654                  // Batch write and flush (if we need to) every so often.
6655                  //
6656                  // If our average Coin size is roughly 41 bytes, checking every 120,000 coins
6657                  // means <5MB of memory imprecision.
6658                  if (coins_processed % 120000 == 0) {
6659                      if (m_interrupt) {
6660                          return util::Error{Untranslated("Aborting after an interrupt was requested")};
6661                      }
6662  
6663                      const auto snapshot_cache_state = WITH_LOCK(::cs_main,
6664                          return snapshot_chainstate.GetCoinsCacheSizeState());
6665  
6666                      if (snapshot_cache_state >= CoinsCacheSizeState::CRITICAL) {
6667                          // This is a hack - we don't know what the actual best block is, but that
6668                          // doesn't matter for the purposes of flushing the cache here. We'll set this
6669                          // to its correct value (`base_blockhash`) below after the coins are loaded.
6670                          coins_cache.SetBestBlock(GetRandHash());
6671  
6672                          // No need to acquire cs_main since this chainstate isn't being used yet.
6673                          FlushSnapshotToDisk(coins_cache, /*snapshot_loaded=*/false);
6674                      }
6675                  }
6676              }
6677          } catch (const std::ios_base::failure&) {
6678              return util::Error{Untranslated(strprintf("Bad snapshot format or truncated snapshot after deserializing %d coins",
6679                        coins_processed))};
6680          }
6681      }
6682  
6683      // Important that we set this. This and the coins_cache accesses above are
6684      // sort of a layer violation, but either we reach into the innards of
6685      // CCoinsViewCache here or we have to invert some of the Chainstate to
6686      // embed them in a snapshot-activation-specific CCoinsViewCache bulk load
6687      // method.
6688      coins_cache.SetBestBlock(base_blockhash);
6689  
6690      bool out_of_coins{false};
6691      try {
6692          std::byte left_over_byte;
6693          coins_file >> left_over_byte;
6694      } catch (const std::ios_base::failure&) {
6695          // We expect an exception since we should be out of coins.
6696          out_of_coins = true;
6697      }
6698      if (!out_of_coins) {
6699          return util::Error{Untranslated(strprintf("Bad snapshot - coins left over after deserializing %d coins",
6700              coins_count))};
6701      }
6702  
6703      LogPrintf("[snapshot] loaded %d (%.2f MB) coins from snapshot %s\n",
6704          coins_count,
6705          coins_cache.DynamicMemoryUsage() / (1000 * 1000),
6706          base_blockhash.ToString());
6707  
6708      // No need to acquire cs_main since this chainstate isn't being used yet.
6709      FlushSnapshotToDisk(coins_cache, /*snapshot_loaded=*/true);
6710  
6711      assert(coins_cache.GetBestBlock() == base_blockhash);
6712  
6713      // As above, okay to immediately release cs_main here since no other context knows
6714      // about the snapshot_chainstate.
6715      CCoinsViewDB* snapshot_coinsdb = WITH_LOCK(::cs_main, return &snapshot_chainstate.CoinsDB());
6716  
6717      std::optional<CCoinsStats> maybe_stats;
6718  
6719      try {
6720          maybe_stats = ComputeUTXOStats(
6721              CoinStatsHashType::HASH_SERIALIZED, snapshot_coinsdb, m_blockman, [&interrupt = m_interrupt] { SnapshotUTXOHashBreakpoint(interrupt); });
6722      } catch (StopHashingException const&) {
6723          return util::Error{Untranslated("Aborting after an interrupt was requested")};
6724      }
6725      if (!maybe_stats.has_value()) {
6726          return util::Error{Untranslated("Failed to generate coins stats")};
6727      }
6728  
6729      // Assert that the deserialized chainstate contents match the expected assumeutxo value.
6730      if (AssumeutxoHash{maybe_stats->hashSerialized} != au_data.hash_serialized) {
6731          return util::Error{Untranslated(strprintf("Bad snapshot content hash: expected %s, got %s",
6732              au_data.hash_serialized.ToString(), maybe_stats->hashSerialized.ToString()))};
6733      }
6734  
6735      snapshot_chainstate.m_chain.SetTip(*snapshot_start_block);
6736  
6737      // The remainder of this function requires modifying data protected by cs_main.
6738      LOCK(::cs_main);
6739  
6740      // Fake various pieces of CBlockIndex state:
6741      CBlockIndex* index = nullptr;
6742  
6743      // Don't make any modifications to the genesis block since it shouldn't be
6744      // necessary, and since the genesis block doesn't have normal flags like
6745      // BLOCK_VALID_SCRIPTS set.
6746      constexpr int AFTER_GENESIS_START{1};
6747  
6748      for (int i = AFTER_GENESIS_START; i <= snapshot_chainstate.m_chain.Height(); ++i) {
6749          index = snapshot_chainstate.m_chain[i];
6750  
6751          // Fake BLOCK_OPT_WITNESS so that Chainstate::NeedsRedownload()
6752          // won't ask for -reindex on startup.
6753          if (DeploymentActiveAt(*index, *this, Consensus::DEPLOYMENT_SEGWIT)) {
6754              index->nStatus |= BLOCK_OPT_WITNESS;
6755          }
6756  
6757          m_blockman.m_dirty_blockindex.insert(index);
6758          // Changes to the block index will be flushed to disk after this call
6759          // returns in `ActivateSnapshot()`, when `MaybeRebalanceCaches()` is
6760          // called, since we've added a snapshot chainstate and therefore will
6761          // have to downsize the IBD chainstate, which will result in a call to
6762          // `FlushStateToDisk(ALWAYS)`.
6763      }
6764  
6765      assert(index);
6766      assert(index == snapshot_start_block);
6767      index->m_chain_tx_count = au_data.m_chain_tx_count;
6768      snapshot_chainstate.setBlockIndexCandidates.insert(snapshot_start_block);
6769  
6770      LogPrintf("[snapshot] validated snapshot (%.2f MB)\n",
6771          coins_cache.DynamicMemoryUsage() / (1000 * 1000));
6772      return {};
6773  }
6774  
6775  // Currently, this function holds cs_main for its duration, which could be for
6776  // multiple minutes due to the ComputeUTXOStats call. This hold is necessary
6777  // because we need to avoid advancing the background validation chainstate
6778  // farther than the snapshot base block - and this function is also invoked
6779  // from within ConnectTip, i.e. from within ActivateBestChain, so cs_main is
6780  // held anyway.
6781  //
6782  // Eventually (TODO), we could somehow separate this function's runtime from
6783  // maintenance of the active chain, but that will either require
6784  //
6785  //  (i) setting `m_disabled` immediately and ensuring all chainstate accesses go
6786  //      through IsUsable() checks, or
6787  //
6788  //  (ii) giving each chainstate its own lock instead of using cs_main for everything.
6789  SnapshotCompletionResult ChainstateManager::MaybeCompleteSnapshotValidation()
6790  {
6791      AssertLockHeld(cs_main);
6792      if (m_ibd_chainstate.get() == &this->ActiveChainstate() ||
6793              !this->IsUsable(m_snapshot_chainstate.get()) ||
6794              !this->IsUsable(m_ibd_chainstate.get()) ||
6795              !m_ibd_chainstate->m_chain.Tip()) {
6796         // Nothing to do - this function only applies to the background
6797         // validation chainstate.
6798         return SnapshotCompletionResult::SKIPPED;
6799      }
6800      const int snapshot_tip_height = this->ActiveHeight();
6801      const int snapshot_base_height = *Assert(this->GetSnapshotBaseHeight());
6802      const CBlockIndex& index_new = *Assert(m_ibd_chainstate->m_chain.Tip());
6803  
6804      if (index_new.nHeight < snapshot_base_height) {
6805          // Background IBD not complete yet.
6806          return SnapshotCompletionResult::SKIPPED;
6807      }
6808  
6809      assert(SnapshotBlockhash());
6810      uint256 snapshot_blockhash = *Assert(SnapshotBlockhash());
6811  
6812      auto handle_invalid_snapshot = [&]() EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
6813          bilingual_str user_error = strprintf(_(
6814              "%s failed to validate the -assumeutxo snapshot state. "
6815              "This indicates a hardware problem, or a bug in the software, or a "
6816              "bad software modification that allowed an invalid snapshot to be "
6817              "loaded. As a result of this, the node will shut down and stop using any "
6818              "state that was built on the snapshot, resetting the chain height "
6819              "from %d to %d. On the next "
6820              "restart, the node will resume syncing from %d "
6821              "without using any snapshot data. "
6822              "Please report this incident to %s, including how you obtained the snapshot. "
6823              "The invalid snapshot chainstate will be left on disk in case it is "
6824              "helpful in diagnosing the issue that caused this error."),
6825              CLIENT_NAME, snapshot_tip_height, snapshot_base_height, snapshot_base_height, CLIENT_BUGREPORT
6826          );
6827  
6828          LogError("[snapshot] !!! %s\n", user_error.original);
6829          LogError("[snapshot] deleting snapshot, reverting to validated chain, and stopping node\n");
6830  
6831          m_active_chainstate = m_ibd_chainstate.get();
6832          m_snapshot_chainstate->m_disabled = true;
6833          assert(!this->IsUsable(m_snapshot_chainstate.get()));
6834          assert(this->IsUsable(m_ibd_chainstate.get()));
6835  
6836          auto rename_result = m_snapshot_chainstate->InvalidateCoinsDBOnDisk();
6837          if (!rename_result) {
6838              user_error += Untranslated("\n") + util::ErrorString(rename_result);
6839          }
6840  
6841          GetNotifications().fatalError(user_error);
6842      };
6843  
6844      if (index_new.GetBlockHash() != snapshot_blockhash) {
6845          LogWarning("[snapshot] supposed base block %s does not match the "
6846            "snapshot base block %s (height %d). Snapshot is not valid.",
6847            index_new.ToString(), snapshot_blockhash.ToString(), snapshot_base_height);
6848          handle_invalid_snapshot();
6849          return SnapshotCompletionResult::BASE_BLOCKHASH_MISMATCH;
6850      }
6851  
6852      assert(index_new.nHeight == snapshot_base_height);
6853  
6854      int curr_height = m_ibd_chainstate->m_chain.Height();
6855  
6856      assert(snapshot_base_height == curr_height);
6857      assert(snapshot_base_height == index_new.nHeight);
6858      assert(this->IsUsable(m_snapshot_chainstate.get()));
6859      assert(this->GetAll().size() == 2);
6860  
6861      CCoinsViewDB& ibd_coins_db = m_ibd_chainstate->CoinsDB();
6862      m_ibd_chainstate->ForceFlushStateToDisk();
6863  
6864      const auto& maybe_au_data = m_options.chainparams.AssumeutxoForHeight(curr_height);
6865      if (!maybe_au_data) {
6866          LogWarning("[snapshot] assumeutxo data not found for height "
6867              "(%d) - refusing to validate snapshot", curr_height);
6868          handle_invalid_snapshot();
6869          return SnapshotCompletionResult::MISSING_CHAINPARAMS;
6870      }
6871  
6872      const AssumeutxoData& au_data = *maybe_au_data;
6873      std::optional<CCoinsStats> maybe_ibd_stats;
6874      LogPrintf("[snapshot] computing UTXO stats for background chainstate to validate "
6875          "snapshot - this could take a few minutes\n");
6876      try {
6877          maybe_ibd_stats = ComputeUTXOStats(
6878              CoinStatsHashType::HASH_SERIALIZED,
6879              &ibd_coins_db,
6880              m_blockman,
6881              [&interrupt = m_interrupt] { SnapshotUTXOHashBreakpoint(interrupt); });
6882      } catch (StopHashingException const&) {
6883          return SnapshotCompletionResult::STATS_FAILED;
6884      }
6885  
6886      // XXX note that this function is slow and will hold cs_main for potentially minutes.
6887      if (!maybe_ibd_stats) {
6888          LogWarning("[snapshot] failed to generate stats for validation coins db");
6889          // While this isn't a problem with the snapshot per se, this condition
6890          // prevents us from validating the snapshot, so we should shut down and let the
6891          // user handle the issue manually.
6892          handle_invalid_snapshot();
6893          return SnapshotCompletionResult::STATS_FAILED;
6894      }
6895      const auto& ibd_stats = *maybe_ibd_stats;
6896  
6897      // Compare the background validation chainstate's UTXO set hash against the hard-coded
6898      // assumeutxo hash we expect.
6899      //
6900      // TODO: For belt-and-suspenders, we could cache the UTXO set
6901      // hash for the snapshot when it's loaded in its chainstate's leveldb. We could then
6902      // reference that here for an additional check.
6903      if (AssumeutxoHash{ibd_stats.hashSerialized} != au_data.hash_serialized) {
6904          LogWarning("[snapshot] hash mismatch: actual=%s, expected=%s",
6905              ibd_stats.hashSerialized.ToString(),
6906              au_data.hash_serialized.ToString());
6907          handle_invalid_snapshot();
6908          return SnapshotCompletionResult::HASH_MISMATCH;
6909      }
6910  
6911      LogPrintf("[snapshot] snapshot beginning at %s has been fully validated\n",
6912          snapshot_blockhash.ToString());
6913  
6914      m_ibd_chainstate->m_disabled = true;
6915      this->MaybeRebalanceCaches();
6916  
6917      return SnapshotCompletionResult::SUCCESS;
6918  }
6919  
6920  Chainstate& ChainstateManager::ActiveChainstate() const
6921  {
6922      LOCK(::cs_main);
6923      assert(m_active_chainstate);
6924      return *m_active_chainstate;
6925  }
6926  
6927  bool ChainstateManager::IsSnapshotActive() const
6928  {
6929      LOCK(::cs_main);
6930      return m_snapshot_chainstate && m_active_chainstate == m_snapshot_chainstate.get();
6931  }
6932  
6933  void ChainstateManager::MaybeRebalanceCaches()
6934  {
6935      AssertLockHeld(::cs_main);
6936      bool ibd_usable = this->IsUsable(m_ibd_chainstate.get());
6937      bool snapshot_usable = this->IsUsable(m_snapshot_chainstate.get());
6938      assert(ibd_usable || snapshot_usable);
6939  
6940      if (ibd_usable && !snapshot_usable) {
6941          // Allocate everything to the IBD chainstate. This will always happen
6942          // when we are not using a snapshot.
6943          m_ibd_chainstate->ResizeCoinsCaches(m_total_coinstip_cache, m_total_coinsdb_cache);
6944      }
6945      else if (snapshot_usable && !ibd_usable) {
6946          // If background validation has completed and snapshot is our active chain...
6947          LogPrintf("[snapshot] allocating all cache to the snapshot chainstate\n");
6948          // Allocate everything to the snapshot chainstate.
6949          m_snapshot_chainstate->ResizeCoinsCaches(m_total_coinstip_cache, m_total_coinsdb_cache);
6950      }
6951      else if (ibd_usable && snapshot_usable) {
6952          // If both chainstates exist, determine who needs more cache based on IBD status.
6953          //
6954          // Note: shrink caches first so that we don't inadvertently overwhelm available memory.
6955          if (IsInitialBlockDownload()) {
6956              m_ibd_chainstate->ResizeCoinsCaches(
6957                  m_total_coinstip_cache * 0.05, m_total_coinsdb_cache * 0.05);
6958              m_snapshot_chainstate->ResizeCoinsCaches(
6959                  m_total_coinstip_cache * 0.95, m_total_coinsdb_cache * 0.95);
6960          } else {
6961              m_snapshot_chainstate->ResizeCoinsCaches(
6962                  m_total_coinstip_cache * 0.05, m_total_coinsdb_cache * 0.05);
6963              m_ibd_chainstate->ResizeCoinsCaches(
6964                  m_total_coinstip_cache * 0.95, m_total_coinsdb_cache * 0.95);
6965          }
6966      }
6967  }
6968  
6969  void ChainstateManager::ResetChainstates()
6970  {
6971      m_ibd_chainstate.reset();
6972      m_snapshot_chainstate.reset();
6973      m_active_chainstate = nullptr;
6974  }
6975  
6976  /**
6977   * Apply default chain params to nullopt members.
6978   * This helps to avoid coding errors around the accidental use of the compare
6979   * operators that accept nullopt, thus ignoring the intended default value.
6980   */
6981  static ChainstateManager::Options&& Flatten(ChainstateManager::Options&& opts)
6982  {
6983      if (!opts.check_block_index.has_value()) opts.check_block_index = opts.chainparams.DefaultConsistencyChecks();
6984      if (!opts.minimum_chain_work.has_value()) opts.minimum_chain_work = UintToArith256(opts.chainparams.GetConsensus().nMinimumChainWork);
6985      if (!opts.assumed_valid_block.has_value()) opts.assumed_valid_block = opts.chainparams.GetConsensus().defaultAssumeValid;
6986      return std::move(opts);
6987  }
6988  
6989  ChainstateManager::ChainstateManager(const util::SignalInterrupt& interrupt, Options options, node::BlockManager::Options blockman_options)
6990      : m_script_check_queue{/*batch_size=*/128, std::clamp(options.worker_threads_num, 0, MAX_SCRIPTCHECK_THREADS)},
6991        m_interrupt{interrupt},
6992        m_options{Flatten(std::move(options))},
6993        m_blockman{interrupt, std::move(blockman_options)},
6994        m_validation_cache{m_options.script_execution_cache_bytes, m_options.signature_cache_bytes}
6995  {
6996      if (GetParams().IsTestChain()
6997          ? (!g_enable_rdts)
6998          : GetConsensus().vDeployments[Consensus::DEPLOYMENT_REDUCED_DATA].nStartTime == Consensus::BIP9Deployment::NEVER_ACTIVE) {
6999          m_options.notifications.warningSet(kernel::Warning::RULES_NOT_CONSENTED,
7000              strprintf(_("Warning: RDTS is not enabled. This node is therefore vulnerable to displaying fake or fraudulent transactions. To enable RDTS enforcement and disable this warning, add %s to your %s file."),
7001                  CONSENSUSRULES_CONFIG_NAME + "=" + CONSENSUSRULES_REQUIRED,
7002  #ifdef BUILDING_FOR_LIBLIMENKAKERNEL
7003                  "limenka.conf"
7004  #else
7005                  gArgs.GetPathArg("-conf", LIMENKA_CONF_FILENAME).utf8string()
7006  #endif
7007              )
7008          );
7009      } else if (g_rdts_warning) {
7010          m_options.notifications.warningSet(kernel::Warning::RULES_NOT_CONSENTED,
7011              strprintf(_("Warning: This software applies the BIP110/RDTS network upgrade, but explicit confirmation has not been configured. To confirm this upgrade and dismiss this warning, add %s to your %s file."),
7012                  CONSENSUSRULES_CONFIG_NAME + "=" + CONSENSUSRULES_REQUIRED,
7013  #ifdef BUILDING_FOR_LIBLIMENKAKERNEL
7014                  "limenka.conf"
7015  #else
7016                  gArgs.GetPathArg("-conf", LIMENKA_CONF_FILENAME).utf8string()
7017  #endif
7018              )
7019          );
7020      }
7021  }
7022  
7023  ChainstateManager::~ChainstateManager()
7024  {
7025      LOCK(::cs_main);
7026  
7027      m_versionbitscache.Clear();
7028  }
7029  
7030  bool ChainstateManager::DetectSnapshotChainstate()
7031  {
7032      assert(!m_snapshot_chainstate);
7033      std::optional<fs::path> path = node::FindSnapshotChainstateDir(m_options.datadir);
7034      if (!path) {
7035          return false;
7036      }
7037      std::optional<uint256> base_blockhash = node::ReadSnapshotBaseBlockhash(*path);
7038      if (!base_blockhash) {
7039          return false;
7040      }
7041      LogPrintf("[snapshot] detected active snapshot chainstate (%s) - loading\n",
7042          fs::PathToString(*path));
7043  
7044      this->ActivateExistingSnapshot(*base_blockhash);
7045      return true;
7046  }
7047  
7048  Chainstate& ChainstateManager::ActivateExistingSnapshot(uint256 base_blockhash)
7049  {
7050      assert(!m_snapshot_chainstate);
7051      m_snapshot_chainstate =
7052          std::make_unique<Chainstate>(nullptr, m_blockman, *this, base_blockhash);
7053      LogPrintf("[snapshot] switching active chainstate to %s\n", m_snapshot_chainstate->ToString());
7054  
7055      // Mempool is empty at this point because we're still in IBD.
7056      Assert(m_active_chainstate->m_mempool->size() == 0);
7057      Assert(!m_snapshot_chainstate->m_mempool);
7058      m_snapshot_chainstate->m_mempool = m_active_chainstate->m_mempool;
7059      m_active_chainstate->m_mempool = nullptr;
7060      m_active_chainstate = m_snapshot_chainstate.get();
7061      return *m_snapshot_chainstate;
7062  }
7063  
7064  bool IsBIP30Repeat(const CBlockIndex& block_index)
7065  {
7066      return (block_index.nHeight==91842 && block_index.GetBlockHash() == uint256{"00000000000a4d0a398161ffc163c503763b1f4360639393e0e4c8e300e0caec"}) ||
7067             (block_index.nHeight==91880 && block_index.GetBlockHash() == uint256{"00000000000743f190a18c5577a3c2d2a1f610ae9601ac046a38084ccb7cd721"});
7068  }
7069  
7070  bool IsBIP30Unspendable(const CBlockIndex& block_index)
7071  {
7072      return (block_index.nHeight==91722 && block_index.GetBlockHash() == uint256{"00000000000271a2dc26e7667f8419f2e15416dc6955e5a6c6cdf3f2574dd08e"}) ||
7073             (block_index.nHeight==91812 && block_index.GetBlockHash() == uint256{"00000000000af0aed4792b1acee3d966af36cf5def14935db8de83d6f9306f2f"});
7074  }
7075  
7076  static fs::path GetSnapshotCoinsDBPath(Chainstate& cs) EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
7077  {
7078      AssertLockHeld(::cs_main);
7079      // Should never be called on a non-snapshot chainstate.
7080      assert(cs.m_from_snapshot_blockhash);
7081      auto storage_path_maybe = cs.CoinsDB().StoragePath();
7082      // Should never be called with a non-existent storage path.
7083      assert(storage_path_maybe);
7084      return *storage_path_maybe;
7085  }
7086  
7087  util::Result<void> Chainstate::InvalidateCoinsDBOnDisk()
7088  {
7089      fs::path snapshot_datadir = GetSnapshotCoinsDBPath(*this);
7090  
7091      // Coins views no longer usable.
7092      m_coins_views.reset();
7093  
7094      auto invalid_path = snapshot_datadir + "_INVALID";
7095      std::string dbpath = fs::PathToString(snapshot_datadir);
7096      std::string target = fs::PathToString(invalid_path);
7097      LogPrintf("[snapshot] renaming snapshot datadir %s to %s\n", dbpath, target);
7098  
7099      // The invalid snapshot datadir is simply moved and not deleted because we may
7100      // want to do forensics later during issue investigation. The user is instructed
7101      // accordingly in MaybeCompleteSnapshotValidation().
7102      try {
7103          fs::rename(snapshot_datadir, invalid_path);
7104      } catch (const fs::filesystem_error& e) {
7105          auto src_str = fs::PathToString(snapshot_datadir);
7106          auto dest_str = fs::PathToString(invalid_path);
7107  
7108          LogError("While invalidating the coins db: Error renaming file '%s' -> '%s': %s",
7109                   src_str, dest_str, e.what());
7110          return util::Error{strprintf(_(
7111              "Rename of '%s' -> '%s' failed. "
7112              "You should resolve this by manually moving or deleting the invalid "
7113              "snapshot directory %s, otherwise you will encounter the same error again "
7114              "on the next startup."),
7115              src_str, dest_str, src_str)};
7116      }
7117      return {};
7118  }
7119  
7120  bool ChainstateManager::DeleteSnapshotChainstate()
7121  {
7122      AssertLockHeld(::cs_main);
7123      Assert(m_snapshot_chainstate);
7124      Assert(m_ibd_chainstate);
7125  
7126      fs::path snapshot_datadir = Assert(node::FindSnapshotChainstateDir(m_options.datadir)).value();
7127      if (!DeleteCoinsDBFromDisk(snapshot_datadir, /*is_snapshot=*/ true)) {
7128          LogError("Deletion of %s failed. Please remove it manually to continue reindexing.",
7129                    fs::PathToString(snapshot_datadir));
7130          return false;
7131      }
7132      m_active_chainstate = m_ibd_chainstate.get();
7133      m_active_chainstate->m_mempool = m_snapshot_chainstate->m_mempool;
7134      m_snapshot_chainstate.reset();
7135      return true;
7136  }
7137  
7138  ChainstateRole Chainstate::GetRole() const
7139  {
7140      if (m_chainman.GetAll().size() <= 1) {
7141          return ChainstateRole::NORMAL;
7142      }
7143      return (this != &m_chainman.ActiveChainstate()) ?
7144                 ChainstateRole::BACKGROUND :
7145                 ChainstateRole::ASSUMEDVALID;
7146  }
7147  
7148  const CBlockIndex* ChainstateManager::GetSnapshotBaseBlock() const
7149  {
7150      return m_active_chainstate ? m_active_chainstate->SnapshotBase() : nullptr;
7151  }
7152  
7153  std::optional<int> ChainstateManager::GetSnapshotBaseHeight() const
7154  {
7155      const CBlockIndex* base = this->GetSnapshotBaseBlock();
7156      return base ? std::make_optional(base->nHeight) : std::nullopt;
7157  }
7158  
7159  void ChainstateManager::RecalculateBestHeader()
7160  {
7161      AssertLockHeld(cs_main);
7162      m_best_header = ActiveChain().Tip();
7163      for (auto& entry : m_blockman.m_block_index) {
7164          if (!(entry.second.nStatus & BLOCK_FAILED_MASK) && m_best_header->nChainWork < entry.second.nChainWork) {
7165              m_best_header = &entry.second;
7166          }
7167      }
7168  }
7169  
7170  bool ChainstateManager::ValidatedSnapshotCleanup()
7171  {
7172      AssertLockHeld(::cs_main);
7173      auto get_storage_path = [](auto& chainstate) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) -> std::optional<fs::path> {
7174          if (!(chainstate && chainstate->HasCoinsViews())) {
7175              return {};
7176          }
7177          return chainstate->CoinsDB().StoragePath();
7178      };
7179      std::optional<fs::path> ibd_chainstate_path_maybe = get_storage_path(m_ibd_chainstate);
7180      std::optional<fs::path> snapshot_chainstate_path_maybe = get_storage_path(m_snapshot_chainstate);
7181  
7182      if (!this->IsSnapshotValidated()) {
7183          // No need to clean up.
7184          return false;
7185      }
7186      // If either path doesn't exist, that means at least one of the chainstates
7187      // is in-memory, in which case we can't do on-disk cleanup. You'd better be
7188      // in a unittest!
7189      if (!ibd_chainstate_path_maybe || !snapshot_chainstate_path_maybe) {
7190          LogError("[snapshot] snapshot chainstate cleanup cannot happen with "
7191                   "in-memory chainstates. You are testing, right?");
7192          return false;
7193      }
7194  
7195      const auto& snapshot_chainstate_path = *snapshot_chainstate_path_maybe;
7196      const auto& ibd_chainstate_path = *ibd_chainstate_path_maybe;
7197  
7198      // Since we're going to be moving around the underlying leveldb filesystem content
7199      // for each chainstate, make sure that the chainstates (and their constituent
7200      // CoinsViews members) have been destructed first.
7201      //
7202      // The caller of this method will be responsible for reinitializing chainstates
7203      // if they want to continue operation.
7204      this->ResetChainstates();
7205  
7206      // No chainstates should be considered usable.
7207      assert(this->GetAll().size() == 0);
7208  
7209      LogPrintf("[snapshot] deleting background chainstate directory (now unnecessary) (%s)\n",
7210                fs::PathToString(ibd_chainstate_path));
7211  
7212      fs::path tmp_old{ibd_chainstate_path + "_todelete"};
7213  
7214      auto rename_failed_abort = [this](
7215                                     fs::path p_old,
7216                                     fs::path p_new,
7217                                     const fs::filesystem_error& err) {
7218          LogError("[snapshot] Error renaming path (%s) -> (%s): %s\n",
7219                    fs::PathToString(p_old), fs::PathToString(p_new), err.what());
7220          GetNotifications().fatalError(strprintf(_(
7221              "Rename of '%s' -> '%s' failed. "
7222              "Cannot clean up the background chainstate leveldb directory."),
7223              fs::PathToString(p_old), fs::PathToString(p_new)));
7224      };
7225  
7226      try {
7227          fs::rename(ibd_chainstate_path, tmp_old);
7228      } catch (const fs::filesystem_error& e) {
7229          rename_failed_abort(ibd_chainstate_path, tmp_old, e);
7230          throw;
7231      }
7232  
7233      LogPrintf("[snapshot] moving snapshot chainstate (%s) to "
7234                "default chainstate directory (%s)\n",
7235                fs::PathToString(snapshot_chainstate_path), fs::PathToString(ibd_chainstate_path));
7236  
7237      try {
7238          fs::rename(snapshot_chainstate_path, ibd_chainstate_path);
7239      } catch (const fs::filesystem_error& e) {
7240          rename_failed_abort(snapshot_chainstate_path, ibd_chainstate_path, e);
7241          throw;
7242      }
7243  
7244      if (!DeleteCoinsDBFromDisk(tmp_old, /*is_snapshot=*/false)) {
7245          // No need to FatalError because once the unneeded bg chainstate data is
7246          // moved, it will not interfere with subsequent initialization.
7247          LogWarning("Deletion of %s failed. Please remove it manually, as the "
7248                     "directory is now unnecessary.",
7249                    fs::PathToString(tmp_old));
7250      } else {
7251          LogPrintf("[snapshot] deleted background chainstate directory (%s)\n",
7252                    fs::PathToString(ibd_chainstate_path));
7253      }
7254      return true;
7255  }
7256  
7257  Chainstate& ChainstateManager::GetChainstateForIndexing()
7258  {
7259      // We can't always return `m_ibd_chainstate` because after background validation
7260      // has completed, `m_snapshot_chainstate == m_active_chainstate`, but it can be
7261      // indexed.
7262      return (this->GetAll().size() > 1) ? *m_ibd_chainstate : *m_active_chainstate;
7263  }
7264  
7265  std::pair<int, int> ChainstateManager::GetPruneRange(const Chainstate& chainstate, int last_height_can_prune)
7266  {
7267      if (chainstate.m_chain.Height() <= 0) {
7268          return {0, 0};
7269      }
7270      int prune_start{0};
7271  
7272      if (this->GetAll().size() > 1 && m_snapshot_chainstate.get() == &chainstate) {
7273          // Leave the blocks in the background IBD chain alone if we're pruning
7274          // the snapshot chain.
7275          prune_start = *Assert(GetSnapshotBaseHeight()) + 1;
7276      }
7277  
7278      int max_prune = std::max<int>(
7279          0, chainstate.m_chain.Height() - static_cast<int>(MIN_BLOCKS_TO_KEEP));
7280  
7281      // last block to prune is the lesser of (caller-specified height, MIN_BLOCKS_TO_KEEP from the tip)
7282      //
7283      // While you might be tempted to prune the background chainstate more
7284      // aggressively (i.e. fewer MIN_BLOCKS_TO_KEEP), this won't work with index
7285      // building - specifically blockfilterindex requires undo data, and if
7286      // we don't maintain this trailing window, we hit indexing failures.
7287      int prune_end = std::min(last_height_can_prune, max_prune);
7288  
7289      return {prune_start, prune_end};
7290  }
7291