rbf.h raw

   1  // Copyright (c) 2016-present The Bitcoin Core developers
   2  // Distributed under the MIT software license, see the accompanying
   3  // file COPYING or http://www.opensource.org/licenses/mit-license.php.
   4  
   5  #ifndef BITCOIN_POLICY_RBF_H
   6  #define BITCOIN_POLICY_RBF_H
   7  
   8  #include <consensus/amount.h>
   9  #include <primitives/transaction.h>
  10  #include <sync.h>
  11  #include <txmempool.h>
  12  #include <util/feefrac.h>
  13  
  14  #include <compare>
  15  #include <cstddef>
  16  #include <cstdint>
  17  #include <optional>
  18  #include <set>
  19  #include <string>
  20  
  21  class CFeeRate;
  22  class uint256;
  23  
  24  /** Maximum number of unique clusters that can be affected by an RBF (Rule #5);
  25   * see GetEntriesForConflicts() */
  26  static constexpr uint32_t MAX_REPLACEMENT_CANDIDATES{100};
  27  
  28  /** The rbf state of unconfirmed transactions */
  29  enum class RBFTransactionState {
  30      /** Unconfirmed tx that does not signal rbf and is not in the mempool */
  31      UNKNOWN,
  32      /** Either this tx or a mempool ancestor signals rbf */
  33      REPLACEABLE_BIP125,
  34      /** Neither this tx nor a mempool ancestor signals rbf */
  35      FINAL,
  36  };
  37  
  38  enum class DiagramCheckError {
  39      /** Unable to calculate due to topology or other reason */
  40      UNCALCULABLE,
  41      /** New diagram wasn't strictly superior  */
  42      FAILURE,
  43  };
  44  
  45  /**
  46   * Determine whether an unconfirmed transaction is signaling opt-in to RBF
  47   * according to BIP 125
  48   * This involves checking sequence numbers of the transaction, as well
  49   * as the sequence numbers of all in-mempool ancestors.
  50   *
  51   * @param tx   The unconfirmed transaction
  52   * @param pool The mempool, which may contain the tx
  53   *
  54   * @return     The rbf state
  55   */
  56  RBFTransactionState IsRBFOptIn(const CTransaction& tx, const CTxMemPool& pool) EXCLUSIVE_LOCKS_REQUIRED(pool.cs);
  57  RBFTransactionState IsRBFOptInEmptyMempool(const CTransaction& tx);
  58  
  59  /** Get all descendants of iters_conflicting. Checks that there are no more than
  60   * MAX_REPLACEMENT_CANDIDATES distinct clusters affected.
  61   *
  62   * @param[in]   iters_conflicting   The set of iterators to mempool entries.
  63   * @param[out]  all_conflicts       Populated with all the mempool entries that would be replaced,
  64   *                                  which includes iters_conflicting and all entries' descendants.
  65   *                                  Not cleared at the start; any existing mempool entries will
  66   *                                  remain in the set.
  67   * @returns an error message if the number of affected clusters would exceed MAX_REPLACEMENT_CANDIDATES, std::nullopt otherwise
  68   */
  69  std::optional<std::string> GetEntriesForConflicts(const CTransaction& tx, CTxMemPool& pool,
  70                                                    const CTxMemPool::setEntries& iters_conflicting,
  71                                                    CTxMemPool::setEntries& all_conflicts)
  72      EXCLUSIVE_LOCKS_REQUIRED(pool.cs);
  73  
  74  /** Check the intersection between two sets of transactions (a set of mempool entries and a set of
  75   * txids) to make sure they are disjoint.
  76   * @param[in]   ancestors           Set of mempool entries corresponding to ancestors of the
  77   *                                  replacement transactions.
  78   * @param[in]   direct_conflicts    Set of txids corresponding to the mempool conflicts
  79   *                                  (candidates to be replaced).
  80   * @param[in]   txid                Transaction ID, included in the error message if violation occurs.
  81   * @returns error message if the sets intersect, std::nullopt if they are disjoint.
  82   */
  83  std::optional<std::string> EntriesAndTxidsDisjoint(const CTxMemPool::setEntries& ancestors,
  84                                                     const std::set<Txid>& direct_conflicts,
  85                                                     const Txid& txid);
  86  
  87  /** The replacement transaction must pay more fees than the original transactions. The additional
  88   * fees must pay for the replacement's bandwidth at or above the incremental relay feerate.
  89   * @param[in]   original_fees       Total modified fees of original transaction(s).
  90   * @param[in]   replacement_fees    Total modified fees of replacement transaction(s).
  91   * @param[in]   replacement_vsize   Total virtual size of replacement transaction(s).
  92   * @param[in]   relay_fee           The node's minimum feerate for transaction relay.
  93   * @param[in]   txid                Transaction ID, included in the error message if violation occurs.
  94   * @returns error string if fees are insufficient, otherwise std::nullopt.
  95   */
  96  std::optional<std::string> PaysForRBF(CAmount original_fees,
  97                                        CAmount replacement_fees,
  98                                        size_t replacement_vsize,
  99                                        CFeeRate relay_fee,
 100                                        const Txid& txid);
 101  
 102  /**
 103   * The replacement transaction must improve the feerate diagram of the mempool.
 104   * @param[in]   changeset           The changeset containing proposed additions/removals
 105   * @returns error type and string if mempool diagram doesn't improve, otherwise std::nullopt.
 106   */
 107  std::optional<std::pair<DiagramCheckError, std::string>> ImprovesFeerateDiagram(CTxMemPool::ChangeSet& changeset);
 108  
 109  #endif // BITCOIN_POLICY_RBF_H
 110