Branch data Line data Source code
1 : : // Copyright (c) 2016-2022 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 : : #include <policy/rbf.h>
6 : :
7 : : #include <consensus/amount.h>
8 : : #include <kernel/mempool_entry.h>
9 : : #include <policy/feerate.h>
10 : : #include <primitives/transaction.h>
11 : : #include <sync.h>
12 : : #include <tinyformat.h>
13 : : #include <txmempool.h>
14 : : #include <uint256.h>
15 : : #include <util/check.h>
16 : : #include <util/moneystr.h>
17 : : #include <util/rbf.h>
18 : :
19 : : #include <limits>
20 : : #include <vector>
21 : :
22 : 0 : RBFTransactionState IsRBFOptIn(const CTransaction& tx, const CTxMemPool& pool)
23 : : {
24 : 0 : AssertLockHeld(pool.cs);
25 : :
26 : : // First check the transaction itself.
27 [ # # ]: 0 : if (SignalsOptInRBF(tx)) {
28 : 0 : return RBFTransactionState::REPLACEABLE_BIP125;
29 : : }
30 : :
31 : : // If this transaction is not in our mempool, then we can't be sure
32 : : // we will know about all its inputs.
33 [ # # ]: 0 : if (!pool.exists(GenTxid::Txid(tx.GetHash()))) {
34 : 0 : return RBFTransactionState::UNKNOWN;
35 : : }
36 : :
37 : : // If all the inputs have nSequence >= maxint-1, it still might be
38 : : // signaled for RBF if any unconfirmed parents have signaled.
39 : 0 : const auto& entry{*Assert(pool.GetEntry(tx.GetHash()))};
40 : 0 : auto ancestors{pool.AssumeCalculateMemPoolAncestors(__func__, entry, CTxMemPool::Limits::NoLimits(),
41 : : /*fSearchForParents=*/false)};
42 : :
43 [ # # ]: 0 : for (CTxMemPool::txiter it : ancestors) {
44 [ # # ][ # # ]: 0 : if (SignalsOptInRBF(it->GetTx())) {
[ # # ][ # # ]
45 : 0 : return RBFTransactionState::REPLACEABLE_BIP125;
46 : : }
47 : : }
48 : 0 : return RBFTransactionState::FINAL;
49 : 0 : }
50 : :
51 : 0 : RBFTransactionState IsRBFOptInEmptyMempool(const CTransaction& tx)
52 : : {
53 : : // If we don't have a local mempool we can only check the transaction itself.
54 : 0 : return SignalsOptInRBF(tx) ? RBFTransactionState::REPLACEABLE_BIP125 : RBFTransactionState::UNKNOWN;
55 : : }
56 : :
57 : 0 : std::optional<std::string> GetEntriesForConflicts(const CTransaction& tx,
58 : : CTxMemPool& pool,
59 : : const CTxMemPool::setEntries& iters_conflicting,
60 : : CTxMemPool::setEntries& all_conflicts)
61 : : {
62 : 0 : AssertLockHeld(pool.cs);
63 : 0 : const uint256 txid = tx.GetHash();
64 : 0 : uint64_t nConflictingCount = 0;
65 [ # # ]: 0 : for (const auto& mi : iters_conflicting) {
66 : 0 : nConflictingCount += mi->GetCountWithDescendants();
67 : : // Rule #5: don't consider replacing more than MAX_REPLACEMENT_CANDIDATES
68 : : // entries from the mempool. This potentially overestimates the number of actual
69 : : // descendants (i.e. if multiple conflicts share a descendant, it will be counted multiple
70 : : // times), but we just want to be conservative to avoid doing too much work.
71 [ # # ]: 0 : if (nConflictingCount > MAX_REPLACEMENT_CANDIDATES) {
72 [ # # ]: 0 : return strprintf("rejecting replacement %s; too many potential replacements (%d > %d)\n",
73 : 0 : txid.ToString(),
74 : : nConflictingCount,
75 : : MAX_REPLACEMENT_CANDIDATES);
76 : : }
77 : : }
78 : : // Calculate the set of all transactions that would have to be evicted.
79 [ # # ]: 0 : for (CTxMemPool::txiter it : iters_conflicting) {
80 : 0 : pool.CalculateDescendants(it, all_conflicts);
81 : : }
82 : 0 : return std::nullopt;
83 : 0 : }
84 : :
85 : 0 : std::optional<std::string> HasNoNewUnconfirmed(const CTransaction& tx,
86 : : const CTxMemPool& pool,
87 : : const CTxMemPool::setEntries& iters_conflicting)
88 : : {
89 : 0 : AssertLockHeld(pool.cs);
90 : 0 : std::set<uint256> parents_of_conflicts;
91 [ # # ]: 0 : for (const auto& mi : iters_conflicting) {
92 [ # # ][ # # ]: 0 : for (const CTxIn& txin : mi->GetTx().vin) {
[ # # ]
93 [ # # ][ # # ]: 0 : parents_of_conflicts.insert(txin.prevout.hash);
94 : : }
95 : : }
96 : :
97 [ # # ]: 0 : for (unsigned int j = 0; j < tx.vin.size(); j++) {
98 : : // Rule #2: We don't want to accept replacements that require low feerate junk to be
99 : : // mined first. Ideally we'd keep track of the ancestor feerates and make the decision
100 : : // based on that, but for now requiring all new inputs to be confirmed works.
101 : : //
102 : : // Note that if you relax this to make RBF a little more useful, this may break the
103 : : // CalculateMempoolAncestors RBF relaxation which subtracts the conflict count/size from the
104 : : // descendant limit.
105 [ # # ][ # # ]: 0 : if (!parents_of_conflicts.count(tx.vin[j].prevout.hash)) {
[ # # ]
106 : : // Rather than check the UTXO set - potentially expensive - it's cheaper to just check
107 : : // if the new input refers to a tx that's in the mempool.
108 [ # # ][ # # ]: 0 : if (pool.exists(GenTxid::Txid(tx.vin[j].prevout.hash))) {
[ # # ][ # # ]
109 [ # # ]: 0 : return strprintf("replacement %s adds unconfirmed input, idx %d",
110 [ # # ][ # # ]: 0 : tx.GetHash().ToString(), j);
111 : : }
112 : 0 : }
113 : 0 : }
114 : 0 : return std::nullopt;
115 : 0 : }
116 : :
117 : 0 : std::optional<std::string> EntriesAndTxidsDisjoint(const CTxMemPool::setEntries& ancestors,
118 : : const std::set<uint256>& direct_conflicts,
119 : : const uint256& txid)
120 : : {
121 [ # # ]: 0 : for (CTxMemPool::txiter ancestorIt : ancestors) {
122 : 0 : const uint256& hashAncestor = ancestorIt->GetTx().GetHash();
123 [ # # ]: 0 : if (direct_conflicts.count(hashAncestor)) {
124 [ # # ]: 0 : return strprintf("%s spends conflicting transaction %s",
125 : 0 : txid.ToString(),
126 [ # # ]: 0 : hashAncestor.ToString());
127 : : }
128 : : }
129 : 0 : return std::nullopt;
130 : 0 : }
131 : :
132 : 0 : std::optional<std::string> PaysMoreThanConflicts(const CTxMemPool::setEntries& iters_conflicting,
133 : : CFeeRate replacement_feerate,
134 : : const uint256& txid)
135 : : {
136 [ # # ]: 0 : for (const auto& mi : iters_conflicting) {
137 : : // Don't allow the replacement to reduce the feerate of the mempool.
138 : : //
139 : : // We usually don't want to accept replacements with lower feerates than what they replaced
140 : : // as that would lower the feerate of the next block. Requiring that the feerate always be
141 : : // increased is also an easy-to-reason about way to prevent DoS attacks via replacements.
142 : : //
143 : : // We only consider the feerates of transactions being directly replaced, not their indirect
144 : : // descendants. While that does mean high feerate children are ignored when deciding whether
145 : : // or not to replace, we do require the replacement to pay more overall fees too, mitigating
146 : : // most cases.
147 : 0 : CFeeRate original_feerate(mi->GetModifiedFee(), mi->GetTxSize());
148 [ # # ]: 0 : if (replacement_feerate <= original_feerate) {
149 [ # # ]: 0 : return strprintf("rejecting replacement %s; new feerate %s <= old feerate %s",
150 : 0 : txid.ToString(),
151 [ # # ]: 0 : replacement_feerate.ToString(),
152 [ # # ]: 0 : original_feerate.ToString());
153 : : }
154 : : }
155 : 0 : return std::nullopt;
156 : 0 : }
157 : :
158 : 0 : std::optional<std::string> PaysForRBF(CAmount original_fees,
159 : : CAmount replacement_fees,
160 : : size_t replacement_vsize,
161 : : CFeeRate relay_fee,
162 : : const uint256& txid)
163 : : {
164 : : // Rule #3: The replacement fees must be greater than or equal to fees of the
165 : : // transactions it replaces, otherwise the bandwidth used by those conflicting transactions
166 : : // would not be paid for.
167 [ # # ]: 0 : if (replacement_fees < original_fees) {
168 [ # # ]: 0 : return strprintf("rejecting replacement %s, less fees than conflicting txs; %s < %s",
169 [ # # ][ # # ]: 0 : txid.ToString(), FormatMoney(replacement_fees), FormatMoney(original_fees));
170 : : }
171 : :
172 : : // Rule #4: The new transaction must pay for its own bandwidth. Otherwise, we have a DoS
173 : : // vector where attackers can cause a transaction to be replaced (and relayed) repeatedly by
174 : : // increasing the fee by tiny amounts.
175 : 0 : CAmount additional_fees = replacement_fees - original_fees;
176 [ # # ]: 0 : if (additional_fees < relay_fee.GetFee(replacement_vsize)) {
177 [ # # ]: 0 : return strprintf("rejecting replacement %s, not enough additional fees to relay; %s < %s",
178 : 0 : txid.ToString(),
179 [ # # ]: 0 : FormatMoney(additional_fees),
180 [ # # ][ # # ]: 0 : FormatMoney(relay_fee.GetFee(replacement_vsize)));
181 : : }
182 : 0 : return std::nullopt;
183 : 0 : }
|