Branch data Line data Source code
1 : : // Copyright (c) 2020-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 : : #include <node/mempool_args.h>
6 : : #include <policy/rbf.h>
7 : : #include <primitives/transaction.h>
8 : : #include <sync.h>
9 : : #include <test/fuzz/FuzzedDataProvider.h>
10 : : #include <test/fuzz/fuzz.h>
11 : : #include <test/fuzz/util.h>
12 : : #include <test/fuzz/util/mempool.h>
13 : : #include <test/util/setup_common.h>
14 : : #include <test/util/time.h>
15 : : #include <test/util/txmempool.h>
16 : : #include <txmempool.h>
17 : : #include <util/check.h>
18 : : #include <util/translation.h>
19 : :
20 : : #include <cstdint>
21 : : #include <optional>
22 : : #include <string>
23 : : #include <vector>
24 : :
25 : : namespace {
26 : : const BasicTestingSetup* g_setup;
27 : : } // namespace
28 : :
29 : : const int NUM_ITERS = 10000;
30 : :
31 : : std::vector<COutPoint> g_outpoints;
32 : :
33 : 1 : void initialize_rbf()
34 : : {
35 [ + - + - : 2 : static const auto testing_setup = MakeNoLogFileContext<>();
+ - ]
36 : 1 : g_setup = testing_setup.get();
37 : 1 : }
38 : :
39 : 1 : void initialize_package_rbf()
40 : : {
41 [ + - + - : 2 : static const auto testing_setup = MakeNoLogFileContext<>();
+ - ]
42 : 1 : g_setup = testing_setup.get();
43 : :
44 : : // Create a fixed set of unique "UTXOs" to source parents from
45 : : // to avoid fuzzer giving circular references
46 [ + + ]: 10001 : for (int i = 0; i < NUM_ITERS; ++i) {
47 : 10000 : g_outpoints.emplace_back();
48 : 10000 : g_outpoints.back().n = i;
49 : : }
50 : :
51 : 1 : }
52 : :
53 [ + - ]: 1566 : FUZZ_TARGET(rbf, .init = initialize_rbf)
54 : : {
55 : 1092 : SeedRandomStateForTest(SeedRand::ZEROS);
56 : 1092 : FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
57 : 1092 : FakeNodeClock clock{ConsumeTime(fuzzed_data_provider)};
58 : 1092 : std::optional<CMutableTransaction> mtx = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider, TX_WITH_WITNESS);
59 [ + + ]: 1092 : if (!mtx) {
60 : 35 : return;
61 : : }
62 : :
63 [ + - ]: 1057 : bilingual_str error;
64 [ + - + - ]: 1057 : CTxMemPool pool{MemPoolOptionsForTest(g_setup->m_node), error};
65 [ - + ]: 1057 : Assert(error.empty());
66 : :
67 [ + + + + ]: 324976 : LIMITED_WHILE (fuzzed_data_provider.ConsumeBool(), NUM_ITERS) {
68 : 324290 : const std::optional<CMutableTransaction> another_mtx = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider, TX_WITH_WITNESS);
69 [ + + ]: 324290 : if (!another_mtx) {
70 : : break;
71 : : }
72 [ + - ]: 323919 : const CTransaction another_tx{*another_mtx};
73 [ + + + + ]: 323919 : if (fuzzed_data_provider.ConsumeBool() && !mtx->vin.empty()) {
74 : 21384 : mtx->vin[0].prevout = COutPoint{another_tx.GetHash(), 0};
75 : : }
76 [ + - + - ]: 323919 : LOCK2(cs_main, pool.cs);
77 [ + - + + ]: 323919 : if (!pool.GetIter(another_tx.GetHash())) {
78 [ + - ]: 212435 : TryAddToMempool(pool, ConsumeTxMemPoolEntry(fuzzed_data_provider, another_tx));
79 : : }
80 [ + - + - ]: 1296047 : }
81 [ + - ]: 1057 : const CTransaction tx{*mtx};
82 [ + + ]: 1057 : if (fuzzed_data_provider.ConsumeBool()) {
83 [ + - + - ]: 349 : LOCK2(cs_main, pool.cs);
84 [ + - + + ]: 349 : if (!pool.GetIter(tx.GetHash())) {
85 [ + - ]: 305 : TryAddToMempool(pool, ConsumeTxMemPoolEntry(fuzzed_data_provider, tx));
86 : : }
87 [ + - ]: 698 : }
88 : 1057 : {
89 [ + - ]: 1057 : LOCK(pool.cs);
90 [ + - ]: 1057 : (void)IsRBFOptIn(tx, pool);
91 : 1057 : }
92 [ + - ]: 3206 : }
93 : :
94 [ + - ]: 1585 : FUZZ_TARGET(package_rbf, .init = initialize_package_rbf)
95 : : {
96 : 1111 : SeedRandomStateForTest(SeedRand::ZEROS);
97 : 1111 : FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
98 : 1111 : FakeNodeClock clock{ConsumeTime(fuzzed_data_provider)};
99 : :
100 : : // "Real" virtual size is not important for this test since ConsumeTxMemPoolEntry generates its own virtual size values
101 : : // so we construct small transactions for performance reasons. Child simply needs an input for later to perhaps connect to parent.
102 [ + - ]: 1111 : CMutableTransaction child;
103 [ + - ]: 1111 : child.vin.resize(1);
104 : :
105 [ + - ]: 1111 : bilingual_str error;
106 [ + - + - ]: 1111 : CTxMemPool pool{MemPoolOptionsForTest(g_setup->m_node), error};
107 [ - + ]: 1111 : Assert(error.empty());
108 : :
109 : : // Add a bunch of parent-child pairs to the mempool, and remember them.
110 : 1111 : std::vector<CTransaction> mempool_txs;
111 : 1111 : uint32_t iter{0};
112 : :
113 : : // Keep track of the total vsize of CTxMemPoolEntry's being added to the mempool to avoid overflow
114 : : // Add replacement_vsize since this is added to new diagram during RBF check
115 : 1111 : std::optional<CMutableTransaction> replacement_tx = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider, TX_WITH_WITNESS);
116 [ + + ]: 1111 : if (!replacement_tx) {
117 : 107 : return;
118 : : }
119 [ + - ]: 1004 : replacement_tx->vin.resize(1);
120 [ + - + - ]: 1004 : replacement_tx->vin[0].prevout = g_outpoints.at(iter++);
121 [ + - ]: 1004 : CTransaction replacement_tx_final{*replacement_tx};
122 : 1004 : auto replacement_entry = ConsumeTxMemPoolEntry(fuzzed_data_provider, replacement_tx_final);
123 [ + - ]: 1004 : int32_t replacement_weight = replacement_entry.GetAdjustedWeight();
124 : : // Ensure that we don't hit FeeFrac limits, as we store TxGraph entries in terms of FeePerWeight
125 [ + - ]: 1004 : int64_t running_vsize_total{replacement_entry.GetTxSize()};
126 : :
127 [ + - + - ]: 1004 : LOCK2(cs_main, pool.cs);
128 : :
129 [ + + ]: 1104702 : while (fuzzed_data_provider.ConsumeBool()) {
130 [ + + ]: 1103804 : if (iter >= NUM_ITERS) break;
131 : :
132 : : // Make sure txns only have one input, and that a unique input is given to avoid circular references
133 [ + - ]: 1103802 : CMutableTransaction parent;
134 [ + - ]: 1103802 : parent.vin.resize(1);
135 [ + - ]: 1103802 : parent.vin[0].prevout = g_outpoints.at(iter++);
136 [ + - ]: 1103802 : parent.vout.emplace_back(0, CScript());
137 : :
138 [ + - ]: 1103802 : mempool_txs.emplace_back(parent);
139 : 1103802 : const auto parent_entry = ConsumeTxMemPoolEntry(fuzzed_data_provider, mempool_txs.back());
140 [ + - ]: 1103802 : running_vsize_total += parent_entry.GetTxSize();
141 [ + + ]: 1103802 : if (running_vsize_total * WITNESS_SCALE_FACTOR > std::numeric_limits<int32_t>::max()) {
142 : : // We aren't adding this final tx to mempool, so we don't want to conflict with it
143 : 70 : mempool_txs.pop_back();
144 : 70 : break;
145 : : }
146 [ + - - + ]: 1103732 : assert(!pool.GetIter(parent_entry.GetTx().GetHash()));
147 [ + - ]: 1103732 : TryAddToMempool(pool, parent_entry);
148 : :
149 : : // It's possible that adding this to the mempool failed due to cluster
150 : : // size limits; if so bail out.
151 [ + - + + ]: 1103732 : if(!pool.GetIter(parent_entry.GetTx().GetHash())) {
152 : 316653 : mempool_txs.pop_back();
153 : 316653 : continue;
154 : : }
155 : :
156 [ + - ]: 787079 : child.vin[0].prevout = COutPoint{mempool_txs.back().GetHash(), 0};
157 [ + - ]: 787079 : mempool_txs.emplace_back(child);
158 : 787079 : const auto child_entry = ConsumeTxMemPoolEntry(fuzzed_data_provider, mempool_txs.back());
159 [ + - ]: 787079 : running_vsize_total += child_entry.GetTxSize();
160 [ + + ]: 787079 : if (running_vsize_total * WITNESS_SCALE_FACTOR > std::numeric_limits<int32_t>::max()) {
161 : : // We aren't adding this final tx to mempool, so we don't want to conflict with it
162 : 34 : mempool_txs.pop_back();
163 : 34 : break;
164 : : }
165 [ + - + - ]: 787045 : if (!pool.GetIter(child_entry.GetTx().GetHash())) {
166 [ + - ]: 787045 : TryAddToMempool(pool, child_entry);
167 : : // Adding this transaction to the mempool may fail due to cluster
168 : : // size limits; if so bail out.
169 [ + - + + ]: 787045 : if(!pool.GetIter(child_entry.GetTx().GetHash())) {
170 : 295888 : mempool_txs.pop_back();
171 : 295888 : continue;
172 : : }
173 : : }
174 : :
175 [ + + ]: 491157 : if (fuzzed_data_provider.ConsumeBool()) {
176 [ + - ]: 490642 : pool.PrioritiseTransaction(mempool_txs.back().GetHash(), fuzzed_data_provider.ConsumeIntegralInRange<int32_t>(-100000, 100000));
177 : : }
178 : 2207604 : }
179 : :
180 : : // Pick some transactions at random to be the direct conflicts
181 : 1004 : CTxMemPool::setEntries direct_conflicts;
182 [ + + ]: 1279240 : for (auto& tx : mempool_txs) {
183 [ + + + - : 1278236 : if (fuzzed_data_provider.ConsumeBool() && pool.GetIter(tx.GetHash())) {
+ - ]
184 [ + - + - ]: 989188 : direct_conflicts.insert(*pool.GetIter(tx.GetHash()));
185 : : }
186 : : }
187 : :
188 : : // Calculate all conflicts:
189 : 1004 : CTxMemPool::setEntries all_conflicts;
190 [ + + ]: 990192 : for (auto& txiter : direct_conflicts) {
191 [ + - ]: 989188 : pool.CalculateDescendants(txiter, all_conflicts);
192 : : }
193 : :
194 : 1004 : CAmount replacement_fees = ConsumeMoney(fuzzed_data_provider);
195 [ + - ]: 1004 : auto changeset = pool.GetChangeSet();
196 [ + + ]: 1018614 : for (auto& txiter : all_conflicts) {
197 [ + - ]: 1017610 : changeset->StageRemoval(txiter);
198 : : }
199 [ + - + - ]: 2008 : changeset->StageAddition(replacement_entry.GetSharedTx(), replacement_fees,
200 [ + - ]: 1004 : replacement_entry.GetTime().count(), replacement_entry.GetHeight(),
201 [ + - ]: 1004 : replacement_entry.GetSequence(), replacement_entry.GetSpendsCoinbase(),
202 [ + - ]: 1004 : replacement_entry.GetSigOpCost(), replacement_entry.GetLockPoints());
203 : : // Calculate the chunks for a replacement.
204 [ + - ]: 1004 : auto calc_results{changeset->CalculateChunksForRBF()};
205 : :
206 [ + + ]: 1004 : if (calc_results.has_value()) {
207 : : // Sanity checks on the chunks.
208 : :
209 : : // Feerates are monotonically decreasing.
210 : 810 : FeeFrac first_sum;
211 [ - + + + ]: 952050 : for (size_t i = 0; i < calc_results->first.size(); ++i) {
212 [ + + ]: 951240 : first_sum += calc_results->first[i];
213 [ + + - + ]: 951240 : if (i) assert(ByRatio{calc_results->first[i - 1]} >= ByRatio{calc_results->first[i]});
214 : : }
215 : 810 : FeeFrac second_sum;
216 [ - + + + ]: 47920 : for (size_t i = 0; i < calc_results->second.size(); ++i) {
217 [ + + ]: 47110 : second_sum += calc_results->second[i];
218 [ + + - + ]: 47110 : if (i) assert(ByRatio{calc_results->second[i - 1]} >= ByRatio{calc_results->second[i]});
219 : : }
220 : :
221 : 810 : FeeFrac replaced;
222 [ + + ]: 992614 : for (auto txiter : all_conflicts) {
223 [ + - ]: 991804 : replaced.fee += txiter->GetModifiedFee();
224 [ + - ]: 991804 : replaced.size += txiter->GetAdjustedWeight();
225 : : }
226 : : // The total fee & size of the new diagram minus replaced fee & size should be the total
227 : : // fee & size of the old diagram minus replacement fee & size.
228 [ + - ]: 810 : assert((first_sum - replaced) == (second_sum - FeeFrac{replacement_fees, replacement_weight}));
229 : : }
230 : :
231 : : // If internals report error, wrapper should too
232 [ + - ]: 1004 : auto err_tuple{ImprovesFeerateDiagram(*changeset)};
233 [ + + ]: 1004 : if (!calc_results.has_value()) {
234 [ + - - + ]: 194 : assert(err_tuple.value().first == DiagramCheckError::UNCALCULABLE);
235 : : } else {
236 : : // Diagram check succeeded
237 : 810 : auto old_sum = std::accumulate(calc_results->first.begin(), calc_results->first.end(), FeeFrac{});
238 : 810 : auto new_sum = std::accumulate(calc_results->second.begin(), calc_results->second.end(), FeeFrac{});
239 [ + + ]: 810 : if (!err_tuple.has_value()) {
240 : : // New diagram's final fee should always match or exceed old diagram's
241 [ - + ]: 51 : assert(old_sum.fee <= new_sum.fee);
242 [ + + ]: 759 : } else if (old_sum.fee > new_sum.fee) {
243 : : // Or it failed, and if old diagram had higher fees, it should be a failure
244 [ - + ]: 618 : assert(err_tuple.value().first == DiagramCheckError::FAILURE);
245 : : }
246 : : }
247 [ + - + - : 7349 : }
+ - ]
|