2022-12-24 23:49:50 +00:00
|
|
|
// Copyright (c) 2016-2022 The Bitcoin Core developers
|
2015-12-16 14:57:54 -05:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#ifndef BITCOIN_POLICY_RBF_H
|
|
|
|
#define BITCOIN_POLICY_RBF_H
|
|
|
|
|
2022-05-31 13:30:23 +02:00
|
|
|
#include <consensus/amount.h>
|
2021-09-20 13:34:48 +01:00
|
|
|
#include <primitives/transaction.h>
|
2022-05-31 13:30:23 +02:00
|
|
|
#include <threadsafety.h>
|
2017-11-10 13:57:53 +13:00
|
|
|
#include <txmempool.h>
|
2024-01-19 15:20:33 -05:00
|
|
|
#include <util/feefrac.h>
|
2021-09-20 13:34:48 +01:00
|
|
|
|
2024-01-19 15:20:33 -05:00
|
|
|
#include <compare>
|
2022-05-31 13:30:23 +02:00
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdint>
|
2021-09-20 13:34:48 +01:00
|
|
|
#include <optional>
|
2022-05-31 13:30:23 +02:00
|
|
|
#include <set>
|
2021-09-20 13:34:48 +01:00
|
|
|
#include <string>
|
2015-12-16 14:57:54 -05:00
|
|
|
|
2022-05-31 13:30:23 +02:00
|
|
|
class CFeeRate;
|
|
|
|
class uint256;
|
|
|
|
|
2022-08-22 14:57:39 +01:00
|
|
|
/** Maximum number of transactions that can be replaced by RBF (Rule #5). This includes all
|
2021-07-27 10:03:49 +01:00
|
|
|
* mempool conflicts and their descendants. */
|
2022-07-19 09:47:45 +01:00
|
|
|
static constexpr uint32_t MAX_REPLACEMENT_CANDIDATES{100};
|
2021-07-27 10:03:49 +01:00
|
|
|
|
2020-07-19 11:01:05 +02:00
|
|
|
/** The rbf state of unconfirmed transactions */
|
2018-03-09 15:03:40 +01:00
|
|
|
enum class RBFTransactionState {
|
2020-07-19 11:01:05 +02:00
|
|
|
/** Unconfirmed tx that does not signal rbf and is not in the mempool */
|
2018-03-09 15:03:40 +01:00
|
|
|
UNKNOWN,
|
2020-07-19 11:01:05 +02:00
|
|
|
/** Either this tx or a mempool ancestor signals rbf */
|
2018-03-09 15:03:40 +01:00
|
|
|
REPLACEABLE_BIP125,
|
2020-07-19 11:01:05 +02:00
|
|
|
/** Neither this tx nor a mempool ancestor signals rbf */
|
|
|
|
FINAL,
|
2016-04-05 14:20:49 +02:00
|
|
|
};
|
|
|
|
|
2024-01-19 15:20:33 -05:00
|
|
|
enum class DiagramCheckError {
|
|
|
|
/** Unable to calculate due to topology or other reason */
|
|
|
|
UNCALCULABLE,
|
|
|
|
/** New diagram wasn't strictly superior */
|
|
|
|
FAILURE,
|
|
|
|
};
|
|
|
|
|
2020-07-19 11:01:05 +02:00
|
|
|
/**
|
|
|
|
* Determine whether an unconfirmed transaction is signaling opt-in to RBF
|
|
|
|
* according to BIP 125
|
|
|
|
* This involves checking sequence numbers of the transaction, as well
|
|
|
|
* as the sequence numbers of all in-mempool ancestors.
|
|
|
|
*
|
|
|
|
* @param tx The unconfirmed transaction
|
|
|
|
* @param pool The mempool, which may contain the tx
|
|
|
|
*
|
|
|
|
* @return The rbf state
|
|
|
|
*/
|
2019-02-23 11:04:20 -05:00
|
|
|
RBFTransactionState IsRBFOptIn(const CTransaction& tx, const CTxMemPool& pool) EXCLUSIVE_LOCKS_REQUIRED(pool.cs);
|
2020-07-19 11:09:59 +02:00
|
|
|
RBFTransactionState IsRBFOptInEmptyMempool(const CTransaction& tx);
|
2015-12-16 14:57:54 -05:00
|
|
|
|
2022-07-19 09:52:55 +01:00
|
|
|
/** Get all descendants of iters_conflicting. Checks that there are no more than
|
|
|
|
* MAX_REPLACEMENT_CANDIDATES potential entries. May overestimate if the entries in
|
|
|
|
* iters_conflicting have overlapping descendants.
|
2021-09-01 09:54:04 +01:00
|
|
|
* @param[in] iters_conflicting The set of iterators to mempool entries.
|
|
|
|
* @param[out] all_conflicts Populated with all the mempool entries that would be replaced,
|
2022-07-19 09:52:55 +01:00
|
|
|
* which includes iters_conflicting and all entries' descendants.
|
|
|
|
* Not cleared at the start; any existing mempool entries will
|
|
|
|
* remain in the set.
|
|
|
|
* @returns an error message if MAX_REPLACEMENT_CANDIDATES may be exceeded, otherwise a std::nullopt.
|
2021-07-27 14:23:40 +01:00
|
|
|
*/
|
2021-08-16 10:19:15 +01:00
|
|
|
std::optional<std::string> GetEntriesForConflicts(const CTransaction& tx, CTxMemPool& pool,
|
|
|
|
const CTxMemPool::setEntries& iters_conflicting,
|
|
|
|
CTxMemPool::setEntries& all_conflicts)
|
2021-08-16 10:40:46 +01:00
|
|
|
EXCLUSIVE_LOCKS_REQUIRED(pool.cs);
|
2021-08-03 13:13:43 +01:00
|
|
|
|
2022-07-19 09:52:55 +01:00
|
|
|
/** The replacement transaction may only include an unconfirmed input if that input was included in
|
|
|
|
* one of the original transactions.
|
|
|
|
* @returns error message if tx spends unconfirmed inputs not also spent by iters_conflicting,
|
|
|
|
* otherwise std::nullopt. */
|
2021-08-16 10:19:15 +01:00
|
|
|
std::optional<std::string> HasNoNewUnconfirmed(const CTransaction& tx, const CTxMemPool& pool,
|
|
|
|
const CTxMemPool::setEntries& iters_conflicting)
|
2021-08-16 10:40:46 +01:00
|
|
|
EXCLUSIVE_LOCKS_REQUIRED(pool.cs);
|
2021-07-27 15:55:25 +01:00
|
|
|
|
|
|
|
/** Check the intersection between two sets of transactions (a set of mempool entries and a set of
|
|
|
|
* txids) to make sure they are disjoint.
|
2021-09-01 09:54:04 +01:00
|
|
|
* @param[in] ancestors Set of mempool entries corresponding to ancestors of the
|
|
|
|
* replacement transactions.
|
2021-08-16 10:19:15 +01:00
|
|
|
* @param[in] direct_conflicts Set of txids corresponding to the mempool conflicts
|
2021-09-01 09:54:04 +01:00
|
|
|
* (candidates to be replaced).
|
|
|
|
* @param[in] txid Transaction ID, included in the error message if violation occurs.
|
2021-07-27 15:55:25 +01:00
|
|
|
* @returns error message if the sets intersect, std::nullopt if they are disjoint.
|
|
|
|
*/
|
2021-08-16 10:19:15 +01:00
|
|
|
std::optional<std::string> EntriesAndTxidsDisjoint(const CTxMemPool::setEntries& ancestors,
|
2023-11-27 16:29:59 +00:00
|
|
|
const std::set<Txid>& direct_conflicts,
|
2021-07-27 15:55:25 +01:00
|
|
|
const uint256& txid);
|
2021-08-11 15:51:27 +01:00
|
|
|
|
|
|
|
/** Check that the feerate of the replacement transaction(s) is higher than the feerate of each
|
2021-08-16 10:19:15 +01:00
|
|
|
* of the transactions in iters_conflicting.
|
|
|
|
* @param[in] iters_conflicting The set of mempool entries.
|
2021-08-11 15:51:27 +01:00
|
|
|
* @returns error message if fees insufficient, otherwise std::nullopt.
|
|
|
|
*/
|
2021-08-16 10:19:15 +01:00
|
|
|
std::optional<std::string> PaysMoreThanConflicts(const CTxMemPool::setEntries& iters_conflicting,
|
|
|
|
CFeeRate replacement_feerate, const uint256& txid);
|
2021-08-11 15:51:27 +01:00
|
|
|
|
2022-07-19 09:52:55 +01:00
|
|
|
/** The replacement transaction must pay more fees than the original transactions. The additional
|
|
|
|
* fees must pay for the replacement's bandwidth at or above the incremental relay feerate.
|
2021-09-01 09:54:04 +01:00
|
|
|
* @param[in] original_fees Total modified fees of original transaction(s).
|
|
|
|
* @param[in] replacement_fees Total modified fees of replacement transaction(s).
|
|
|
|
* @param[in] replacement_vsize Total virtual size of replacement transaction(s).
|
2021-09-08 09:44:39 +01:00
|
|
|
* @param[in] relay_fee The node's minimum feerate for transaction relay.
|
2021-08-16 10:19:15 +01:00
|
|
|
* @param[in] txid Transaction ID, included in the error message if violation occurs.
|
2021-08-11 15:51:41 +01:00
|
|
|
* @returns error string if fees are insufficient, otherwise std::nullopt.
|
|
|
|
*/
|
2021-08-16 10:19:15 +01:00
|
|
|
std::optional<std::string> PaysForRBF(CAmount original_fees,
|
|
|
|
CAmount replacement_fees,
|
|
|
|
size_t replacement_vsize,
|
2021-09-08 09:44:39 +01:00
|
|
|
CFeeRate relay_fee,
|
2021-08-16 10:19:15 +01:00
|
|
|
const uint256& txid);
|
2021-08-11 15:51:41 +01:00
|
|
|
|
2024-01-19 15:20:33 -05:00
|
|
|
/**
|
|
|
|
* The replacement transaction must improve the feerate diagram of the mempool.
|
|
|
|
* @param[in] pool The mempool.
|
|
|
|
* @param[in] direct_conflicts Set of in-mempool txids corresponding to the direct conflicts i.e.
|
|
|
|
* input double-spends with the proposed transaction
|
|
|
|
* @param[in] all_conflicts Set of mempool entries corresponding to all transactions to be evicted
|
|
|
|
* @param[in] replacement_fees Fees of proposed replacement package
|
|
|
|
* @param[in] replacement_vsize Size of proposed replacement package
|
|
|
|
* @returns error type and string if mempool diagram doesn't improve, otherwise std::nullopt.
|
|
|
|
*/
|
|
|
|
std::optional<std::pair<DiagramCheckError, std::string>> ImprovesFeerateDiagram(CTxMemPool& pool,
|
|
|
|
const CTxMemPool::setEntries& direct_conflicts,
|
|
|
|
const CTxMemPool::setEntries& all_conflicts,
|
|
|
|
CAmount replacement_fees,
|
|
|
|
int64_t replacement_vsize)
|
|
|
|
EXCLUSIVE_LOCKS_REQUIRED(pool.cs);
|
|
|
|
|
2015-12-16 14:57:54 -05:00
|
|
|
#endif // BITCOIN_POLICY_RBF_H
|