txmempool.h raw

   1  // Copyright (c) 2009-2010 Satoshi Nakamoto
   2  // Copyright (c) 2009-2022 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  #ifndef LIMENKA_TXMEMPOOL_H
   7  #define LIMENKA_TXMEMPOOL_H
   8  
   9  #include <coins.h>
  10  #include <consensus/amount.h>
  11  #include <consensus/params.h>
  12  #include <indirectmap.h>
  13  #include <kernel/cs_main.h>
  14  #include <kernel/mempool_entry.h>          // IWYU pragma: export
  15  #include <kernel/mempool_limits.h>         // IWYU pragma: export
  16  #include <kernel/mempool_options.h>        // IWYU pragma: export
  17  #include <kernel/mempool_removal_reason.h> // IWYU pragma: export
  18  #include <policy/feerate.h>
  19  #include <policy/packages.h>
  20  #include <primitives/transaction.h>
  21  #include <script/script.h>
  22  #include <sync.h>
  23  #include <util/epochguard.h>
  24  #include <util/hasher.h>
  25  #include <util/result.h>
  26  #include <util/feefrac.h>
  27  
  28  #include <boost/multi_index/hashed_index.hpp>
  29  #include <boost/multi_index/identity.hpp>
  30  #include <boost/multi_index/indexed_by.hpp>
  31  #include <boost/multi_index/ordered_index.hpp>
  32  #include <boost/multi_index/sequenced_index.hpp>
  33  #include <boost/multi_index/tag.hpp>
  34  #include <boost/multi_index_container.hpp>
  35  
  36  #include <atomic>
  37  #include <map>
  38  #include <optional>
  39  #include <set>
  40  #include <string>
  41  #include <string_view>
  42  #include <utility>
  43  #include <vector>
  44  
  45  class CChain;
  46  class CScript;
  47  class ValidationSignals;
  48  
  49  struct bilingual_str;
  50  
  51  static constexpr std::chrono::minutes DYNAMIC_DUST_FEERATE_UPDATE_INTERVAL{15};
  52  
  53  /** Fake height value used in Coin to signify they are only in the memory pool (since 0.8) */
  54  static const uint32_t MEMPOOL_HEIGHT = 0x7FFFFFFF;
  55  
  56  inline int64_t maxmempoolMinimumBytes(const int64_t descendant_size_vbytes) {
  57      return descendant_size_vbytes * 40;
  58  }
  59  inline int64_t limitdescendantsizeMaximumVBytes(const int64_t maxmempool) {
  60      return maxmempool / 40;
  61  }
  62  
  63  /**
  64   * Test whether the LockPoints height and time are still valid on the current chain
  65   */
  66  bool TestLockPointValidity(CChain& active_chain, const LockPoints& lp) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
  67  
  68  // extracts a transaction hash from CTxMemPoolEntry or CTransactionRef
  69  struct mempoolentry_txid
  70  {
  71      typedef uint256 result_type;
  72      result_type operator() (const CTxMemPoolEntry &entry) const
  73      {
  74          return entry.GetTx().GetHash();
  75      }
  76  
  77      result_type operator() (const CTransactionRef& tx) const
  78      {
  79          return tx->GetHash();
  80      }
  81  };
  82  
  83  // extracts a transaction witness-hash from CTxMemPoolEntry or CTransactionRef
  84  struct mempoolentry_wtxid
  85  {
  86      typedef uint256 result_type;
  87      result_type operator() (const CTxMemPoolEntry &entry) const
  88      {
  89          return entry.GetTx().GetWitnessHash();
  90      }
  91  
  92      result_type operator() (const CTransactionRef& tx) const
  93      {
  94          return tx->GetWitnessHash();
  95      }
  96  };
  97  
  98  
  99  /** \class CompareTxMemPoolEntryByDescendantScore
 100   *
 101   *  Sort an entry by max(score/size of entry's tx, score/size with all descendants).
 102   */
 103  class CompareTxMemPoolEntryByDescendantScore
 104  {
 105  public:
 106      bool operator()(const CTxMemPoolEntry& a, const CTxMemPoolEntry& b) const
 107      {
 108          double a_mod_fee, a_size, b_mod_fee, b_size;
 109  
 110          GetModFeeAndSize(a, a_mod_fee, a_size);
 111          GetModFeeAndSize(b, b_mod_fee, b_size);
 112  
 113          // Avoid division by rewriting (a/b > c/d) as (a*d > c*b).
 114          double f1 = a_mod_fee * b_size;
 115          double f2 = a_size * b_mod_fee;
 116  
 117          if (f1 == f2) {
 118              return a.GetTime() >= b.GetTime();
 119          }
 120          return f1 < f2;
 121      }
 122  
 123      // Return the fee/size we're using for sorting this entry.
 124      void GetModFeeAndSize(const CTxMemPoolEntry &a, double &mod_fee, double &size) const
 125      {
 126          // Compare feerate with descendants to feerate of the transaction, and
 127          // return the fee/size for the max.
 128          double f1 = (double)a.GetModifiedFee() * a.GetSizeWithDescendants();
 129          double f2 = (double)a.GetModFeesWithDescendants() * a.GetTxSize();
 130  
 131          if (f2 > f1) {
 132              mod_fee = a.GetModFeesWithDescendants();
 133              size = a.GetSizeWithDescendants();
 134          } else {
 135              mod_fee = a.GetModifiedFee();
 136              size = a.GetTxSize();
 137          }
 138      }
 139  };
 140  
 141  /** \class CompareTxMemPoolEntryByScore
 142   *
 143   *  Sort by feerate of entry (fee/size) in descending order
 144   *  This is only used for transaction relay, so we use GetFee()
 145   *  instead of GetModifiedFee() to avoid leaking prioritization
 146   *  information via the sort order.
 147   */
 148  class CompareTxMemPoolEntryByScore
 149  {
 150  public:
 151      bool operator()(const CTxMemPoolEntry& a, const CTxMemPoolEntry& b) const
 152      {
 153          double f1 = (double)a.GetFee() * b.GetTxSize();
 154          double f2 = (double)b.GetFee() * a.GetTxSize();
 155          if (f1 == f2) {
 156              return b.GetTx().GetHash() < a.GetTx().GetHash();
 157          }
 158          return f1 > f2;
 159      }
 160  };
 161  
 162  class CompareTxMemPoolEntryByEntryTime
 163  {
 164  public:
 165      bool operator()(const CTxMemPoolEntry& a, const CTxMemPoolEntry& b) const
 166      {
 167          return a.GetTime() < b.GetTime();
 168      }
 169  };
 170  
 171  /** \class CompareTxMemPoolEntryByAncestorScore
 172   *
 173   *  Sort an entry by min(score/size of entry's tx, score/size with all ancestors).
 174   */
 175  class CompareTxMemPoolEntryByAncestorFee
 176  {
 177  public:
 178      template<typename T>
 179      bool operator()(const T& a, const T& b) const
 180      {
 181          double a_mod_fee, a_size, b_mod_fee, b_size;
 182  
 183          GetModFeeAndSize(a, a_mod_fee, a_size);
 184          GetModFeeAndSize(b, b_mod_fee, b_size);
 185  
 186          // Avoid division by rewriting (a/b > c/d) as (a*d > c*b).
 187          double f1 = a_mod_fee * b_size;
 188          double f2 = a_size * b_mod_fee;
 189  
 190          if (f1 == f2) {
 191              return a.GetTx().GetHash() < b.GetTx().GetHash();
 192          }
 193          return f1 > f2;
 194      }
 195  
 196      // Return the fee/size we're using for sorting this entry.
 197      template <typename T>
 198      void GetModFeeAndSize(const T &a, double &mod_fee, double &size) const
 199      {
 200          // Compare feerate with ancestors to feerate of the transaction, and
 201          // return the fee/size for the min.
 202          double f1 = (double)a.GetModifiedFee() * a.GetSizeWithAncestors();
 203          double f2 = (double)a.GetModFeesWithAncestors() * a.GetTxSize();
 204  
 205          if (f1 > f2) {
 206              mod_fee = a.GetModFeesWithAncestors();
 207              size = a.GetSizeWithAncestors();
 208          } else {
 209              mod_fee = a.GetModifiedFee();
 210              size = a.GetTxSize();
 211          }
 212      }
 213  };
 214  
 215  uint160 ScriptHashkey(const CScript& script);
 216  
 217  // Multi_index tag names
 218  struct descendant_score {};
 219  struct entry_time {};
 220  struct ancestor_score {};
 221  struct index_by_wtxid {};
 222  
 223  class CBlockPolicyEstimator;
 224  
 225  /**
 226   * Information about a mempool transaction.
 227   */
 228  struct TxMempoolInfo
 229  {
 230      /** The transaction itself */
 231      CTransactionRef tx;
 232  
 233      /** Time the transaction entered the mempool. */
 234      std::chrono::seconds m_time;
 235  
 236      /** Fee of the transaction. */
 237      CAmount fee;
 238  
 239      /** Virtual size of the transaction. */
 240      int32_t vsize;
 241  
 242      /** The fee delta. */
 243      int64_t nFeeDelta;
 244  };
 245  
 246  /**
 247   * CTxMemPool stores valid-according-to-the-current-best-chain transactions
 248   * that may be included in the next block.
 249   *
 250   * Transactions are added when they are seen on the network (or created by the
 251   * local node), but not all transactions seen are added to the pool. For
 252   * example, the following new transactions will not be added to the mempool:
 253   * - a transaction which doesn't meet the minimum fee requirements.
 254   * - a new transaction that double-spends an input of a transaction already in
 255   * the pool where the new transaction does not meet the Replace-By-Fee
 256   * requirements as defined in doc/policy/mempool-replacements.md.
 257   * - a non-standard transaction.
 258   *
 259   * CTxMemPool::mapTx, and CTxMemPoolEntry bookkeeping:
 260   *
 261   * mapTx is a boost::multi_index that sorts the mempool on 5 criteria:
 262   * - transaction hash (txid)
 263   * - witness-transaction hash (wtxid)
 264   * - descendant feerate [we use max(feerate of tx, feerate of tx with all descendants)]
 265   * - time in mempool
 266   * - ancestor feerate [we use min(feerate of tx, feerate of tx with all unconfirmed ancestors)]
 267   *
 268   * Note: the term "descendant" refers to in-mempool transactions that depend on
 269   * this one, while "ancestor" refers to in-mempool transactions that a given
 270   * transaction depends on.
 271   *
 272   * In order for the feerate sort to remain correct, we must update transactions
 273   * in the mempool when new descendants arrive.  To facilitate this, we track
 274   * the set of in-mempool direct parents and direct children in mapLinks.  Within
 275   * each CTxMemPoolEntry, we track the size and fees of all descendants.
 276   *
 277   * Usually when a new transaction is added to the mempool, it has no in-mempool
 278   * children (because any such children would be an orphan).  So in
 279   * addNewTransaction(), we:
 280   * - update a new entry's m_parents to include all in-mempool parents
 281   * - update each of those parent entries to include the new tx as a child
 282   * - update all ancestors of the transaction to include the new tx's size/fee
 283   *
 284   * When a transaction is removed from the mempool, we must:
 285   * - update all in-mempool parents to not track the tx in their m_children
 286   * - update all ancestors to not include the tx's size/fees in descendant state
 287   * - update all in-mempool children to not include it as a parent
 288   *
 289   * These happen in UpdateForRemoveFromMempool().  (Note that when removing a
 290   * transaction along with its descendants, we must calculate that set of
 291   * transactions to be removed before doing the removal, or else the mempool can
 292   * be in an inconsistent state where it's impossible to walk the ancestors of
 293   * a transaction.)
 294   *
 295   * In the event of a reorg, the assumption that a newly added tx has no
 296   * in-mempool children is false.  In particular, the mempool is in an
 297   * inconsistent state while new transactions are being added, because there may
 298   * be descendant transactions of a tx coming from a disconnected block that are
 299   * unreachable from just looking at transactions in the mempool (the linking
 300   * transactions may also be in the disconnected block, waiting to be added).
 301   * Because of this, there's not much benefit in trying to search for in-mempool
 302   * children in addNewTransaction().  Instead, in the special case of transactions
 303   * being added from a disconnected block, we require the caller to clean up the
 304   * state, to account for in-mempool, out-of-block descendants for all the
 305   * in-block transactions by calling UpdateTransactionsFromBlock().  Note that
 306   * until this is called, the mempool state is not consistent, and in particular
 307   * mapLinks may not be correct (and therefore functions like
 308   * CalculateMemPoolAncestors() and CalculateDescendants() that rely
 309   * on them to walk the mempool are not generally safe to use).
 310   *
 311   * Computational limits:
 312   *
 313   * Updating all in-mempool ancestors of a newly added transaction can be slow,
 314   * if no bound exists on how many in-mempool ancestors there may be.
 315   * CalculateMemPoolAncestors() takes configurable limits that are designed to
 316   * prevent these calculations from being too CPU intensive.
 317   *
 318   */
 319  class CTxMemPool
 320  {
 321  protected:
 322      std::atomic<unsigned int> nTransactionsUpdated{0}; //!< Used by getblocktemplate to trigger CreateNewBlock() invocation
 323  
 324      uint64_t totalTxSize GUARDED_BY(cs){0};      //!< sum of all mempool tx's virtual sizes. Differs from serialized tx size since witness data is discounted. Defined in BIP 141.
 325      CAmount m_total_fee GUARDED_BY(cs){0};       //!< sum of all mempool tx's fees (NOT modified fee)
 326      uint64_t cachedInnerUsage GUARDED_BY(cs){0}; //!< sum of dynamic memory usage of all the map elements (NOT the maps themselves)
 327  
 328      mutable int64_t lastRollingFeeUpdate GUARDED_BY(cs){GetTime()};
 329      mutable bool blockSinceLastRollingFeeBump GUARDED_BY(cs){false};
 330      mutable double rollingMinimumFeeRate GUARDED_BY(cs){0}; //!< minimum fee to get into the pool, decreases exponentially
 331      mutable Epoch m_epoch GUARDED_BY(cs){};
 332  
 333      // In-memory counter for external mempool tracking purposes.
 334      // This number is incremented once every time a transaction
 335      // is added or removed from the mempool for any reason.
 336      mutable uint64_t m_sequence_number GUARDED_BY(cs){1};
 337  
 338      void trackPackageRemoved(const CFeeRate& rate) EXCLUSIVE_LOCKS_REQUIRED(cs);
 339  
 340      bool m_load_tried GUARDED_BY(cs){false};
 341  
 342      CFeeRate GetMinFee(size_t sizelimit) const;
 343  
 344  public:
 345  
 346      static const int ROLLING_FEE_HALFLIFE = 60 * 60 * 12; // public only for testing
 347  
 348      using CTxMemPoolEntry_Indices_ = boost::multi_index::indexed_by<
 349              // sorted by txid
 350              boost::multi_index::hashed_unique<mempoolentry_txid, SaltedTxidHasher>,
 351              // sorted by wtxid
 352              boost::multi_index::hashed_unique<
 353                  boost::multi_index::tag<index_by_wtxid>,
 354                  mempoolentry_wtxid,
 355                  SaltedTxidHasher
 356              >,
 357              // sorted by fee rate
 358              boost::multi_index::ordered_non_unique<
 359                  boost::multi_index::tag<descendant_score>,
 360                  boost::multi_index::identity<CTxMemPoolEntry>,
 361                  CompareTxMemPoolEntryByDescendantScore
 362              >,
 363              // sorted by entry time
 364              boost::multi_index::ordered_non_unique<
 365                  boost::multi_index::tag<entry_time>,
 366                  boost::multi_index::identity<CTxMemPoolEntry>,
 367                  CompareTxMemPoolEntryByEntryTime
 368              >,
 369              // sorted by fee rate with ancestors
 370              boost::multi_index::ordered_non_unique<
 371                  boost::multi_index::tag<ancestor_score>,
 372                  boost::multi_index::identity<CTxMemPoolEntry>,
 373                  CompareTxMemPoolEntryByAncestorFee
 374              >
 375          >;
 376  #if BOOST_VERSION >= 109100
 377      using CTxMemPoolEntry_Indices = CTxMemPoolEntry_Indices_;
 378  #else
 379      struct CTxMemPoolEntry_Indices final : CTxMemPoolEntry_Indices_{};
 380  #endif
 381      typedef boost::multi_index_container<
 382          CTxMemPoolEntry,
 383          CTxMemPoolEntry_Indices
 384      > indexed_transaction_set;
 385  
 386      /**
 387       * This mutex needs to be locked when accessing `mapTx` or other members
 388       * that are guarded by it.
 389       *
 390       * @par Consistency guarantees
 391       * By design, it is guaranteed that:
 392       * 1. Locking both `cs_main` and `mempool.cs` will give a view of mempool
 393       *    that is consistent with current chain tip (`ActiveChain()` and
 394       *    `CoinsTip()`) and is fully populated. Fully populated means that if the
 395       *    current active chain is missing transactions that were present in a
 396       *    previously active chain, all the missing transactions will have been
 397       *    re-added to the mempool and should be present if they meet size and
 398       *    consistency constraints.
 399       * 2. Locking `mempool.cs` without `cs_main` will give a view of a mempool
 400       *    consistent with some chain that was active since `cs_main` was last
 401       *    locked, and that is fully populated as described above. It is ok for
 402       *    code that only needs to query or remove transactions from the mempool
 403       *    to lock just `mempool.cs` without `cs_main`.
 404       *
 405       * To provide these guarantees, it is necessary to lock both `cs_main` and
 406       * `mempool.cs` whenever adding transactions to the mempool and whenever
 407       * changing the chain tip. It's necessary to keep both mutexes locked until
 408       * the mempool is consistent with the new chain tip and fully populated.
 409       */
 410      mutable RecursiveMutex cs;
 411      indexed_transaction_set mapTx GUARDED_BY(cs);
 412  
 413      using txiter = indexed_transaction_set::nth_index<0>::type::const_iterator;
 414      std::vector<CTransactionRef> txns_randomized GUARDED_BY(cs); //!< All transactions in mapTx, in random order
 415  
 416      typedef std::set<txiter, CompareIteratorByHash> setEntries;
 417  
 418      using Limits = kernel::MemPoolLimits;
 419  
 420      uint64_t CalculateDescendantMaximum(txiter entry) const EXCLUSIVE_LOCKS_REQUIRED(cs);
 421  
 422      std::map<uint160, std::pair<const CTransaction *, const CTransaction *>> mapUsedSPK;
 423  
 424  private:
 425      typedef std::map<txiter, setEntries, CompareIteratorByHash> cacheMap;
 426  
 427  
 428      void UpdateParent(txiter entry, txiter parent, bool add) EXCLUSIVE_LOCKS_REQUIRED(cs);
 429      void UpdateChild(txiter entry, txiter child, bool add) EXCLUSIVE_LOCKS_REQUIRED(cs);
 430  
 431      std::vector<indexed_transaction_set::const_iterator> GetSortedDepthAndScore() const EXCLUSIVE_LOCKS_REQUIRED(cs);
 432  
 433      /**
 434       * Track locally submitted transactions to periodically retry initial broadcast.
 435       */
 436      std::set<uint256> m_unbroadcast_txids GUARDED_BY(cs);
 437  
 438  
 439      /**
 440       * Helper function to calculate all in-mempool ancestors of staged_ancestors and apply ancestor
 441       * and descendant limits (including staged_ancestors themselves, entry_size and entry_count).
 442       *
 443       * @param[in]   entry_size          Virtual size to include in the limits.
 444       * @param[in]   entry_count         How many entries to include in the limits.
 445       * @param[in]   staged_ancestors    Should contain entries in the mempool.
 446       * @param[in]   limits              Maximum number and size of ancestors and descendants
 447       *
 448       * @return all in-mempool ancestors, or an error if any ancestor or descendant limits were hit
 449       */
 450      util::Result<setEntries> CalculateAncestorsAndCheckLimits(int64_t entry_size,
 451                                                                size_t entry_count,
 452                                                                CTxMemPoolEntry::Parents &staged_ancestors,
 453                                                                const Limits& limits
 454                                                                ) const EXCLUSIVE_LOCKS_REQUIRED(cs);
 455  
 456  public:
 457      indirectmap<COutPoint, const CTransaction*> mapNextTx GUARDED_BY(cs);
 458      std::map<uint256, std::pair<double, CAmount> > mapDeltas GUARDED_BY(cs);
 459  
 460      using Options = kernel::MemPoolOptions;
 461  
 462      Options m_opts;
 463  
 464      /** Create a new CTxMemPool.
 465       * Sanity checks will be off by default for performance, because otherwise
 466       * accepting transactions becomes O(N^2) where N is the number of transactions
 467       * in the pool.
 468       */
 469      explicit CTxMemPool(Options opts, bilingual_str& error);
 470  
 471      /**
 472       * If sanity-checking is turned on, check makes sure the pool is
 473       * consistent (does not contain two transactions that spend the same inputs,
 474       * all inputs are in the mapNextTx array). If sanity-checking is turned off,
 475       * check does nothing.
 476       */
 477      void check(const CCoinsViewCache& active_coins_tip, int64_t spendheight, const Consensus::Params& consensusParams, bool fork_active = false) const EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
 478  
 479  
 480      void removeRecursive(const CTransaction& tx, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs);
 481      /** After reorg, filter the entries that would no longer be valid in the next block, and update
 482       * the entries' cached LockPoints if needed.  The mempool does not have any knowledge of
 483       * consensus rules. It just applies the callable function and removes the ones for which it
 484       * returns true.
 485       * @param[in]   filter_final_and_mature   Predicate that checks the relevant validation rules
 486       *                                        and updates an entry's LockPoints.
 487       * */
 488      void removeForReorg(CChain& chain, std::function<bool(txiter)> filter_final_and_mature) EXCLUSIVE_LOCKS_REQUIRED(cs, cs_main);
 489      void removeConflicts(const CTransaction& tx) EXCLUSIVE_LOCKS_REQUIRED(cs);
 490      void removeForBlock(const std::vector<CTransactionRef>& vtx, unsigned int nBlockHeight) EXCLUSIVE_LOCKS_REQUIRED(cs);
 491  
 492      bool CompareDepthAndScore(const uint256& hasha, const uint256& hashb, bool wtxid=false);
 493      bool isSpent(const COutPoint& outpoint) const;
 494      unsigned int GetTransactionsUpdated() const;
 495      void AddTransactionsUpdated(unsigned int n);
 496      /**
 497       * Check that none of this transactions inputs are in the mempool, and thus
 498       * the tx is not dependent on other mempool transactions to be included in a block.
 499       */
 500      bool HasNoInputsOf(const CTransaction& tx) const EXCLUSIVE_LOCKS_REQUIRED(cs);
 501      /**
 502       * Update all transactions in the mempool which depend on tx to recalculate their priority
 503       * and adjust the input value that will age to reflect that the inputs from this transaction have
 504       * either just been added to the chain or just been removed.
 505       */
 506      void UpdateDependentPriorities(const CTransaction &tx, unsigned int nBlockHeight, bool addToChain);
 507  
 508      void UpdateDynamicDustFeerate();
 509  
 510      /** Affect CreateNewBlock prioritisation of transactions */
 511      void PrioritiseTransaction(const uint256& hash, double dPriorityDelta, const CAmount& nFeeDelta);
 512      void PrioritiseTransaction(const uint256& hash, const CAmount& nFeeDelta) { PrioritiseTransaction(hash, 0., nFeeDelta); }
 513      void ApplyDeltas(const uint256& hash, double &dPriorityDelta, CAmount &nFeeDelta) const EXCLUSIVE_LOCKS_REQUIRED(cs);
 514      void ClearPrioritisation(const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs);
 515  
 516      struct delta_info {
 517          /** Whether this transaction is in the mempool. */
 518          const bool in_mempool;
 519          /** The fee delta added using PrioritiseTransaction(). */
 520          const CAmount delta;
 521          const double priority_delta;
 522          /** The modified fee (base fee + delta) of this entry. Only present if in_mempool=true. */
 523          std::optional<CAmount> modified_fee;
 524          /** The prioritised transaction's txid. */
 525          const uint256 txid;
 526      };
 527      /** Return a vector of all entries in mapDeltas with their corresponding delta_info. */
 528      std::vector<delta_info> GetPrioritisedTransactions() const EXCLUSIVE_LOCKS_REQUIRED(!cs);
 529  
 530      /** Get the transaction in the pool that spends the same prevout */
 531      const CTransaction* GetConflictTx(const COutPoint& prevout) const EXCLUSIVE_LOCKS_REQUIRED(cs);
 532  
 533      /** Returns an iterator to the given hash, if found */
 534      std::optional<txiter> GetIter(const uint256& txid) const EXCLUSIVE_LOCKS_REQUIRED(cs);
 535  
 536      /** Translate a set of hashes into a set of pool iterators to avoid repeated lookups.
 537       * Does not require that all of the hashes correspond to actual transactions in the mempool,
 538       * only returns the ones that exist. */
 539      setEntries GetIterSet(const std::set<Txid>& hashes) const EXCLUSIVE_LOCKS_REQUIRED(cs);
 540  
 541      /** Translate a list of hashes into a list of mempool iterators to avoid repeated lookups.
 542       * The nth element in txids becomes the nth element in the returned vector. If any of the txids
 543       * don't actually exist in the mempool, returns an empty vector. */
 544      std::vector<txiter> GetIterVec(const std::vector<uint256>& txids) const EXCLUSIVE_LOCKS_REQUIRED(cs);
 545  
 546      /** UpdateTransactionsFromBlock is called when adding transactions from a
 547       * disconnected block back to the mempool, new mempool entries may have
 548       * children in the mempool (which is generally not the case when otherwise
 549       * adding transactions).
 550       *  @post updated descendant state for descendants of each transaction in
 551       *        vHashesToUpdate (excluding any child transactions present in
 552       *        vHashesToUpdate, which are already accounted for). Updated state
 553       *        includes add fee/size information for such descendants to the
 554       *        parent and updated ancestor state to include the parent.
 555       *
 556       * @param[in] vHashesToUpdate          The set of txids from the
 557       *     disconnected block that have been accepted back into the mempool.
 558       */
 559      void UpdateTransactionsFromBlock(const std::vector<uint256>& vHashesToUpdate) EXCLUSIVE_LOCKS_REQUIRED(cs, cs_main) LOCKS_EXCLUDED(m_epoch);
 560  
 561      /**
 562       * Try to calculate all in-mempool ancestors of entry.
 563       * (these are all calculated including the tx itself)
 564       *
 565       * @param[in]   entry               CTxMemPoolEntry of which all in-mempool ancestors are calculated
 566       * @param[in]   limits              Maximum number and size of ancestors and descendants
 567       * @param[in]   fSearchForParents   Whether to search a tx's vin for in-mempool parents, or look
 568       *                                  up parents from mapLinks. Must be true for entries not in
 569       *                                  the mempool
 570       *
 571       * @return all in-mempool ancestors, or an error if any ancestor or descendant limits were hit
 572       */
 573      util::Result<setEntries> CalculateMemPoolAncestors(const CTxMemPoolEntry& entry,
 574                                     const Limits& limits,
 575                                     bool fSearchForParents = true) const EXCLUSIVE_LOCKS_REQUIRED(cs);
 576  
 577      /**
 578       * Same as CalculateMemPoolAncestors, but always returns a (non-optional) setEntries.
 579       * Should only be used when it is assumed CalculateMemPoolAncestors would not fail. If
 580       * CalculateMemPoolAncestors does unexpectedly fail, an empty setEntries is returned and the
 581       * error is logged to BCLog::MEMPOOL with level BCLog::Level::Error. In debug builds, failure
 582       * of CalculateMemPoolAncestors will lead to shutdown due to assertion failure.
 583       *
 584       * @param[in]   calling_fn_name     Name of calling function so we can properly log the call site
 585       *
 586       * @return a setEntries corresponding to the result of CalculateMemPoolAncestors or an empty
 587       *         setEntries if it failed
 588       *
 589       * @see CTXMemPool::CalculateMemPoolAncestors()
 590       */
 591      setEntries AssumeCalculateMemPoolAncestors(
 592          std::string_view calling_fn_name,
 593          const CTxMemPoolEntry &entry,
 594          const Limits& limits,
 595          bool fSearchForParents = true) const EXCLUSIVE_LOCKS_REQUIRED(cs);
 596  
 597      /** Collect the entire cluster of connected transactions for each transaction in txids.
 598       * All txids must correspond to transaction entries in the mempool, otherwise this returns an
 599       * empty vector. This call will also exit early and return an empty vector if it collects 500 or
 600       * more transactions as a DoS protection. */
 601      std::vector<txiter> GatherClusters(const std::vector<uint256>& txids) const EXCLUSIVE_LOCKS_REQUIRED(cs);
 602  
 603      /** Calculate all in-mempool ancestors of a set of transactions not already in the mempool and
 604       * check ancestor and descendant limits. Heuristics are used to estimate the ancestor and
 605       * descendant count of all entries if the package were to be added to the mempool.  The limits
 606       * are applied to the union of all package transactions. For example, if the package has 3
 607       * transactions and limits.ancestor_count = 25, the union of all 3 sets of ancestors (including the
 608       * transactions themselves) must be <= 22.
 609       * @param[in]       package                 Transaction package being evaluated for acceptance
 610       *                                          to mempool. The transactions need not be direct
 611       *                                          ancestors/descendants of each other.
 612       * @param[in]       total_vsize             Sum of virtual sizes for all transactions in package.
 613       * @returns {} or the error reason if a limit is hit.
 614       */
 615      util::Result<void> CheckPackageLimits(const Package& package,
 616                                            int64_t total_vsize) const EXCLUSIVE_LOCKS_REQUIRED(cs);
 617  
 618      /** Populate setDescendants with all in-mempool descendants of hash.
 619       *  Assumes that setDescendants includes all in-mempool descendants of anything
 620       *  already in it.  */
 621      void CalculateDescendants(txiter it, setEntries& setDescendants) const EXCLUSIVE_LOCKS_REQUIRED(cs);
 622  
 623      /** The minimum fee to get into the mempool, which may itself not be enough
 624       *  for larger-sized transactions.
 625       *  The m_incremental_relay_feerate policy variable is used to bound the time it
 626       *  takes the fee rate to go back down all the way to 0. When the feerate
 627       *  would otherwise be half of this, it is set to 0 instead.
 628       */
 629      CFeeRate GetMinFee() const {
 630          return GetMinFee(m_opts.max_size_bytes);
 631      }
 632  
 633      /** Remove transactions from the mempool until its dynamic size is <= sizelimit.
 634        *  pvNoSpendsRemaining, if set, will be populated with the list of outpoints
 635        *  which are not in mempool which no longer have any spends in this mempool.
 636        */
 637      void TrimToSize(size_t sizelimit, std::vector<COutPoint>* pvNoSpendsRemaining = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs);
 638  
 639      /** Expire all transaction (and their dependencies) in the mempool older than time. Return the number of removed transactions. */
 640      int Expire(std::chrono::seconds time) EXCLUSIVE_LOCKS_REQUIRED(cs);
 641  
 642      /**
 643       * Calculate the ancestor and descendant count for the given transaction.
 644       * The counts include the transaction itself.
 645       * When ancestors is non-zero (ie, the transaction itself is in the mempool),
 646       * ancestorsize and ancestorfees will also be set to the appropriate values.
 647       */
 648      void GetTransactionAncestry(const uint256& txid, size_t& ancestors, size_t& descendants, size_t* ancestorsize = nullptr, CAmount* ancestorfees = nullptr) const;
 649  
 650      /**
 651       * @returns true if an initial attempt to load the persisted mempool was made, regardless of
 652       *          whether the attempt was successful or not
 653       */
 654      bool GetLoadTried() const;
 655  
 656      /**
 657       * Set whether or not an initial attempt to load the persisted mempool was made (regardless
 658       * of whether the attempt was successful or not)
 659       */
 660      void SetLoadTried(bool load_tried);
 661  
 662      unsigned long size() const
 663      {
 664          LOCK(cs);
 665          return mapTx.size();
 666      }
 667  
 668      uint64_t GetTotalTxSize() const EXCLUSIVE_LOCKS_REQUIRED(cs)
 669      {
 670          AssertLockHeld(cs);
 671          return totalTxSize;
 672      }
 673  
 674      CAmount GetTotalFee() const EXCLUSIVE_LOCKS_REQUIRED(cs)
 675      {
 676          AssertLockHeld(cs);
 677          return m_total_fee;
 678      }
 679  
 680      bool exists(const GenTxid& gtxid) const
 681      {
 682          LOCK(cs);
 683          if (gtxid.IsWtxid()) {
 684              return (mapTx.get<index_by_wtxid>().count(gtxid.GetHash()) != 0);
 685          }
 686          return (mapTx.count(gtxid.GetHash()) != 0);
 687      }
 688  
 689      const CTxMemPoolEntry* GetEntry(const Txid& txid) const LIFETIMEBOUND EXCLUSIVE_LOCKS_REQUIRED(cs);
 690  
 691      CTransactionRef get(const uint256& hash) const;
 692      txiter get_iter_from_wtxid(const uint256& wtxid) const EXCLUSIVE_LOCKS_REQUIRED(cs)
 693      {
 694          AssertLockHeld(cs);
 695          return mapTx.project<0>(mapTx.get<index_by_wtxid>().find(wtxid));
 696      }
 697      TxMempoolInfo info(const GenTxid& gtxid) const;
 698  
 699      /** Returns info for a transaction if its entry_sequence < last_sequence */
 700      TxMempoolInfo info_for_relay(const GenTxid& gtxid, uint64_t last_sequence) const;
 701  
 702      std::vector<CTxMemPoolEntryRef> entryAll() const EXCLUSIVE_LOCKS_REQUIRED(cs);
 703      std::vector<TxMempoolInfo> infoAll() const;
 704  
 705      void FindScriptPubKey(const std::set<CScript>& needles, std::map<COutPoint, Coin>& out_results);
 706  
 707      size_t DynamicMemoryUsage() const;
 708  
 709      /** Adds a transaction to the unbroadcast set */
 710      void AddUnbroadcastTx(const uint256& txid)
 711      {
 712          LOCK(cs);
 713          // Sanity check the transaction is in the mempool & insert into
 714          // unbroadcast set.
 715          if (exists(GenTxid::Txid(txid))) m_unbroadcast_txids.insert(txid);
 716      };
 717  
 718      /** Removes a transaction from the unbroadcast set */
 719      void RemoveUnbroadcastTx(const uint256& txid, const bool unchecked = false);
 720  
 721      /** Returns transactions in unbroadcast set */
 722      std::set<uint256> GetUnbroadcastTxs() const
 723      {
 724          LOCK(cs);
 725          return m_unbroadcast_txids;
 726      }
 727  
 728      /** Returns whether a txid is in the unbroadcast set */
 729      bool IsUnbroadcastTx(const uint256& txid) const EXCLUSIVE_LOCKS_REQUIRED(cs)
 730      {
 731          AssertLockHeld(cs);
 732          return m_unbroadcast_txids.count(txid) != 0;
 733      }
 734  
 735      /** Guards this internal counter for external reporting */
 736      uint64_t GetAndIncrementSequence() const EXCLUSIVE_LOCKS_REQUIRED(cs) {
 737          return m_sequence_number++;
 738      }
 739  
 740      uint64_t GetSequence() const EXCLUSIVE_LOCKS_REQUIRED(cs) {
 741          return m_sequence_number;
 742      }
 743  
 744      /* Check that all direct conflicts are in a cluster size of two or less. Each
 745       * direct conflict may be in a separate cluster.
 746       */
 747      std::optional<std::string> CheckConflictTopology(const setEntries& direct_conflicts);
 748  
 749  private:
 750      /** Remove a set of transactions from the mempool.
 751       *  If a transaction is in this set, then all in-mempool descendants must
 752       *  also be in the set, unless this transaction is being removed for being
 753       *  in a block.
 754       *  Set updateDescendants to true when removing a tx that was in a block, so
 755       *  that any in-mempool descendants have their ancestor state updated.
 756       */
 757      void RemoveStaged(setEntries& stage, bool updateDescendants, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs);
 758  
 759      /** UpdateForDescendants is used by UpdateTransactionsFromBlock to update
 760       *  the descendants for a single transaction that has been added to the
 761       *  mempool but may have child transactions in the mempool, eg during a
 762       *  chain reorg.
 763       *
 764       * @pre CTxMemPoolEntry::m_children is correct for the given tx and all
 765       *      descendants.
 766       * @pre cachedDescendants is an accurate cache where each entry has all
 767       *      descendants of the corresponding key, including those that should
 768       *      be removed for violation of ancestor limits.
 769       * @post if updateIt has any non-excluded descendants, cachedDescendants has
 770       *       a new cache line for updateIt.
 771       * @post descendants_to_remove has a new entry for any descendant which exceeded
 772       *       ancestor limits relative to updateIt.
 773       *
 774       * @param[in] updateIt the entry to update for its descendants
 775       * @param[in,out] cachedDescendants a cache where each line corresponds to all
 776       *     descendants. It will be updated with the descendants of the transaction
 777       *     being updated, so that future invocations don't need to walk the same
 778       *     transaction again, if encountered in another transaction chain.
 779       * @param[in] setExclude the set of descendant transactions in the mempool
 780       *     that must not be accounted for (because any descendants in setExclude
 781       *     were added to the mempool after the transaction being updated and hence
 782       *     their state is already reflected in the parent state).
 783       * @param[out] descendants_to_remove Populated with the txids of entries that
 784       *     exceed ancestor limits. It's the responsibility of the caller to
 785       *     removeRecursive them.
 786       */
 787      void UpdateForDescendants(txiter updateIt, cacheMap& cachedDescendants,
 788                                const std::set<uint256>& setExclude, std::set<uint256>& descendants_to_remove) EXCLUSIVE_LOCKS_REQUIRED(cs);
 789      /** Update ancestors of hash to add/remove it as a descendant transaction. */
 790      void UpdateAncestorsOf(bool add, txiter hash, setEntries &setAncestors) EXCLUSIVE_LOCKS_REQUIRED(cs);
 791      /** Set ancestor state for an entry */
 792      void UpdateEntryForAncestors(txiter it, const setEntries &setAncestors) EXCLUSIVE_LOCKS_REQUIRED(cs);
 793      /** For each transaction being removed, update ancestors and any direct children.
 794        * If updateDescendants is true, then also update in-mempool descendants'
 795        * ancestor state. */
 796      void UpdateForRemoveFromMempool(const setEntries &entriesToRemove, bool updateDescendants) EXCLUSIVE_LOCKS_REQUIRED(cs);
 797      /** Sever link between specified transaction and direct children. */
 798      void UpdateChildrenForRemoval(txiter entry) EXCLUSIVE_LOCKS_REQUIRED(cs);
 799  
 800      /** Before calling removeUnchecked for a given transaction,
 801       *  UpdateForRemoveFromMempool must be called on the entire (dependent) set
 802       *  of transactions being removed at the same time.  We use each
 803       *  CTxMemPoolEntry's m_parents in order to walk ancestors of a
 804       *  given transaction that is removed, so we can't remove intermediate
 805       *  transactions in a chain before we've updated all the state for the
 806       *  removal.
 807       */
 808      void removeUnchecked(txiter entry, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs);
 809  public:
 810      /** visited marks a CTxMemPoolEntry as having been traversed
 811       * during the lifetime of the most recently created Epoch::Guard
 812       * and returns false if we are the first visitor, true otherwise.
 813       *
 814       * An Epoch::Guard must be held when visited is called or an assert will be
 815       * triggered.
 816       *
 817       */
 818      bool visited(const txiter it) const EXCLUSIVE_LOCKS_REQUIRED(cs, m_epoch)
 819      {
 820          return m_epoch.visited(it->m_epoch_marker);
 821      }
 822  
 823      bool visited(std::optional<txiter> it) const EXCLUSIVE_LOCKS_REQUIRED(cs, m_epoch)
 824      {
 825          assert(m_epoch.guarded()); // verify guard even when it==nullopt
 826          return !it || visited(*it);
 827      }
 828  
 829      /*
 830       * CTxMemPool::ChangeSet:
 831       *
 832       * This class is used for all mempool additions and associated removals (eg
 833       * due to rbf). Removals that don't need to be evaluated for acceptance,
 834       * such as removing transactions that appear in a block, or due to reorg,
 835       * or removals related to mempool limiting or expiry do not need to use
 836       * this.
 837       *
 838       * Callers can interleave calls to StageAddition()/StageRemoval(), and
 839       * removals may be invoked in any order, but additions must be done in a
 840       * topological order in the case of transaction packages (ie, parents must
 841       * be added before children).
 842       *
 843       * CalculateChunksForRBF() can be used to calculate the feerate diagram of
 844       * the proposed set of new transactions and compare with the existing
 845       * mempool.
 846       *
 847       * CalculateMemPoolAncestors() calculates the in-mempool (not including
 848       * what is in the change set itself) ancestors of a given transaction.
 849       *
 850       * Apply() will apply the removals and additions that are staged into the
 851       * mempool.
 852       *
 853       * Only one changeset may exist at a time. While a changeset is
 854       * outstanding, no removals or additions may be made directly to the
 855       * mempool.
 856       */
 857      class ChangeSet {
 858      public:
 859          explicit ChangeSet(CTxMemPool* pool) : m_pool(pool) {}
 860          ~ChangeSet() EXCLUSIVE_LOCKS_REQUIRED(m_pool->cs) { m_pool->m_have_changeset = false; }
 861  
 862          ChangeSet(const ChangeSet&) = delete;
 863          ChangeSet& operator=(const ChangeSet&) = delete;
 864  
 865          using TxHandle = CTxMemPool::txiter;
 866  
 867          TxHandle StageAddition(const CTransactionRef& tx, const CAmount fee, int64_t time, unsigned int entry_height, uint64_t entry_sequence, CoinAgeCache coin_age_cache, bool spends_coinbase, int32_t extra_weight, int64_t sigops_cost, LockPoints lp);
 868          void StageRemoval(CTxMemPool::txiter it) { m_to_remove.insert(it); }
 869  
 870          const CTxMemPool::setEntries& GetRemovals() const { return m_to_remove; }
 871  
 872          util::Result<CTxMemPool::setEntries> CalculateMemPoolAncestors(TxHandle tx, const Limits& limits)
 873          {
 874              // Look up transaction in our cache first
 875              auto it = m_ancestors.find(tx);
 876              if (it != m_ancestors.end()) return it->second;
 877  
 878              // If not found, try to have the mempool calculate it, and cache
 879              // for later.
 880              LOCK(m_pool->cs);
 881              auto ret{m_pool->CalculateMemPoolAncestors(*tx, limits)};
 882              if (ret) m_ancestors.try_emplace(tx, *ret);
 883              return ret;
 884          }
 885  
 886          std::vector<CTransactionRef> GetAddedTxns() const {
 887              std::vector<CTransactionRef> ret;
 888              ret.reserve(m_entry_vec.size());
 889              for (const auto& entry : m_entry_vec) {
 890                  ret.emplace_back(entry->GetSharedTx());
 891              }
 892              return ret;
 893          }
 894  
 895          /**
 896           * Calculate the sorted chunks for the old and new mempool relating to the
 897           * clusters that would be affected by a potential replacement transaction.
 898           *
 899           * @return old and new diagram pair respectively, or an error string if the conflicts don't match a calculable topology
 900           */
 901          util::Result<std::pair<std::vector<FeeFrac>, std::vector<FeeFrac>>> CalculateChunksForRBF();
 902  
 903          size_t GetTxCount() const { return m_entry_vec.size(); }
 904          const CTransaction& GetAddedTxn(size_t index) const { return m_entry_vec.at(index)->GetTx(); }
 905  
 906          void Apply() EXCLUSIVE_LOCKS_REQUIRED(cs_main);
 907  
 908          CTxMemPool* m_pool;
 909          CTxMemPool::indexed_transaction_set m_to_add;
 910      private:
 911          std::vector<CTxMemPool::txiter> m_entry_vec; // track the added transactions' insertion order
 912          // map from the m_to_add index to the ancestors for the transaction
 913          std::map<CTxMemPool::txiter, CTxMemPool::setEntries, CompareIteratorByHash> m_ancestors;
 914          CTxMemPool::setEntries m_to_remove;
 915  
 916          friend class CTxMemPool;
 917      };
 918  
 919      std::unique_ptr<ChangeSet> GetChangeSet() EXCLUSIVE_LOCKS_REQUIRED(cs) {
 920          Assume(!m_have_changeset);
 921          m_have_changeset = true;
 922          return std::make_unique<ChangeSet>(this);
 923      }
 924  
 925      bool m_have_changeset GUARDED_BY(cs){false};
 926  
 927      friend class CTxMemPool::ChangeSet;
 928  
 929  private:
 930      // Apply the given changeset to the mempool, by removing transactions in
 931      // the to_remove set and adding transactions in the to_add set.
 932      void Apply(CTxMemPool::ChangeSet* changeset) EXCLUSIVE_LOCKS_REQUIRED(cs);
 933  
 934      // addNewTransaction must update state for all ancestors of a given transaction,
 935      // to track size/count of descendant transactions.  First version of
 936      // addNewTransaction can be used to have it call CalculateMemPoolAncestors(), and
 937      // then invoke the second version.
 938      // Note that addNewTransaction is ONLY called (via Apply()) from ATMP
 939      // outside of tests and any other callers may break wallet's in-mempool
 940      // tracking (due to lack of CValidationInterface::TransactionAddedToMempool
 941      // callbacks).
 942      void addNewTransaction(CTxMemPool::txiter it) EXCLUSIVE_LOCKS_REQUIRED(cs);
 943      void addNewTransaction(CTxMemPool::txiter it, CTxMemPool::setEntries& setAncestors) EXCLUSIVE_LOCKS_REQUIRED(cs);
 944  };
 945  
 946  /**
 947   * CCoinsView that brings transactions from a mempool into view.
 948   * It does not check for spendings by memory pool transactions.
 949   * Instead, it provides access to all Coins which are either unspent in the
 950   * base CCoinsView, are outputs from any mempool transaction, or are
 951   * tracked temporarily to allow transaction dependencies in package validation.
 952   * This allows transaction replacement to work as expected, as you want to
 953   * have all inputs "available" to check signatures, and any cycles in the
 954   * dependency graph are checked directly in AcceptToMemoryPool.
 955   * It also allows you to sign a double-spend directly in
 956   * signrawtransactionwithkey and signrawtransactionwithwallet,
 957   * as long as the conflicting transaction is not yet confirmed.
 958   *
 959   * Its Cursor also doesn't work. In general, it is broken as a CCoinsView
 960   * implementation outside of a few use cases.
 961   */
 962  class CCoinsViewMemPool : public CCoinsViewBacked
 963  {
 964      /**
 965      * Coins made available by transactions being validated. Tracking these allows for package
 966      * validation, since we can access transaction outputs without submitting them to mempool.
 967      */
 968      std::unordered_map<COutPoint, Coin, SaltedOutpointHasher> m_temp_added;
 969  
 970      /**
 971       * Set of all coins that have been fetched from mempool or created using PackageAddTransaction
 972       * (not base). Used to track the origin of a coin, see GetNonBaseCoins().
 973       */
 974      mutable std::unordered_set<COutPoint, SaltedOutpointHasher> m_non_base_coins;
 975  protected:
 976      const CTxMemPool& mempool;
 977  
 978  public:
 979      CCoinsViewMemPool(CCoinsView* baseIn, const CTxMemPool& mempoolIn);
 980      /** GetCoin, returning whether it exists and is not spent. Also updates m_non_base_coins if the
 981       * coin is not fetched from base. */
 982      std::optional<Coin> GetCoin(const COutPoint& outpoint) const override;
 983      /** Add the coins created by this transaction. These coins are only temporarily stored in
 984       * m_temp_added and cannot be flushed to the back end. Only used for package validation. */
 985      void PackageAddTransaction(const CTransactionRef& tx);
 986      /** Get all coins in m_non_base_coins. */
 987      std::unordered_set<COutPoint, SaltedOutpointHasher> GetNonBaseCoins() const { return m_non_base_coins; }
 988      /** Clear m_temp_added and m_non_base_coins. */
 989      void Reset();
 990  };
 991  #endif // LIMENKA_TXMEMPOOL_H
 992