Branch data Line data Source code
1 : : // Copyright (c) 2023 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 <consensus/validation.h>
6 : : #include <node/context.h>
7 : : #include <node/mempool_args.h>
8 : : #include <node/miner.h>
9 : : #include <node/txdownloadman.h>
10 : : #include <node/txdownloadman_impl.h>
11 : : #include <test/fuzz/FuzzedDataProvider.h>
12 : : #include <test/fuzz/fuzz.h>
13 : : #include <test/fuzz/util.h>
14 : : #include <test/fuzz/util/mempool.h>
15 : : #include <test/util/mining.h>
16 : : #include <test/util/script.h>
17 : : #include <test/util/setup_common.h>
18 : : #include <test/util/txmempool.h>
19 : : #include <util/hasher.h>
20 : : #include <util/rbf.h>
21 : : #include <util/time.h>
22 : : #include <txmempool.h>
23 : : #include <validation.h>
24 : : #include <validationinterface.h>
25 : :
26 : : namespace {
27 : :
28 : : const TestingSetup* g_setup;
29 : :
30 : : constexpr size_t NUM_COINS{50};
31 : : COutPoint COINS[NUM_COINS];
32 : :
33 : : static TxValidationResult TESTED_TX_RESULTS[] = {
34 : : // Skip TX_RESULT_UNSET
35 : : TxValidationResult::TX_CONSENSUS,
36 : : TxValidationResult::TX_INPUTS_NOT_STANDARD,
37 : : TxValidationResult::TX_NOT_STANDARD,
38 : : TxValidationResult::TX_MISSING_INPUTS,
39 : : TxValidationResult::TX_PREMATURE_SPEND,
40 : : TxValidationResult::TX_WITNESS_MUTATED,
41 : : TxValidationResult::TX_WITNESS_STRIPPED,
42 : : TxValidationResult::TX_CONFLICT,
43 : : TxValidationResult::TX_MEMPOOL_POLICY,
44 : : // Skip TX_NO_MEMPOOL
45 : : TxValidationResult::TX_RECONSIDERABLE,
46 : : TxValidationResult::TX_UNKNOWN,
47 : : };
48 : :
49 : : // Precomputed transactions. Some may conflict with each other.
50 : : std::vector<CTransactionRef> TRANSACTIONS;
51 : :
52 : : // Limit the total number of peers because we don't expect coverage to change much with lots more peers.
53 : : constexpr int NUM_PEERS = 16;
54 : :
55 : : // Precomputed random durations (positive and negative, each ~exponentially distributed).
56 : : std::chrono::microseconds TIME_SKIPS[128];
57 : :
58 : 43119 : static CTransactionRef MakeTransactionSpending(const std::vector<COutPoint>& outpoints, size_t num_outputs, bool add_witness)
59 : : {
60 : 43119 : CMutableTransaction tx;
61 : : // If no outpoints are given, create a random one.
62 [ + + ]: 477206 : for (const auto& outpoint : outpoints) {
63 [ + - ]: 434087 : tx.vin.emplace_back(outpoint);
64 : : }
65 [ + + ]: 43119 : if (add_witness) {
66 [ + - ]: 82438 : tx.vin[0].scriptWitness.stack.push_back({1});
67 : : }
68 [ + - + + ]: 12026824 : for (size_t o = 0; o < num_outputs; ++o) tx.vout.emplace_back(CENT, P2WSH_OP_TRUE);
69 [ + - ]: 86238 : return MakeTransactionRef(tx);
70 : 43119 : }
71 : 42983 : static std::vector<COutPoint> PickCoins(FuzzedDataProvider& fuzzed_data_provider)
72 : : {
73 : 42983 : std::vector<COutPoint> ret;
74 [ + - ]: 42983 : ret.push_back(fuzzed_data_provider.PickValueInArray(COINS));
75 [ + + + + ]: 433949 : LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10) {
76 [ + - ]: 390966 : ret.push_back(fuzzed_data_provider.PickValueInArray(COINS));
77 : : }
78 : 42983 : return ret;
79 : 0 : }
80 : :
81 : 2 : void initialize()
82 : : {
83 [ + - + - ]: 4 : static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();
84 : 2 : g_setup = testing_setup.get();
85 [ + + ]: 102 : for (uint32_t i = 0; i < uint32_t{NUM_COINS}; ++i) {
86 : 100 : COINS[i] = COutPoint{Txid::FromUint256((HashWriter() << i).GetHash()), i};
87 : : }
88 : 2 : size_t outpoints_index = 0;
89 : : // 2 transactions same txid different witness
90 : 2 : {
91 [ + - + - ]: 4 : auto tx1{MakeTransactionSpending({COINS[outpoints_index]}, /*num_outputs=*/5, /*add_witness=*/false)};
92 [ + - + - : 4 : auto tx2{MakeTransactionSpending({COINS[outpoints_index]}, /*num_outputs=*/5, /*add_witness=*/true)};
+ - ]
93 [ + - ]: 2 : Assert(tx1->GetHash() == tx2->GetHash());
94 [ + - ]: 2 : TRANSACTIONS.emplace_back(tx1);
95 [ + - ]: 2 : TRANSACTIONS.emplace_back(tx2);
96 [ + - ]: 2 : outpoints_index += 1;
97 [ + - ]: 2 : }
98 : : // 2 parents 1 child
99 : 2 : {
100 [ + - + - ]: 4 : auto tx_parent_1{MakeTransactionSpending({COINS[outpoints_index++]}, /*num_outputs=*/1, /*add_witness=*/true)};
101 [ + - ]: 2 : TRANSACTIONS.emplace_back(tx_parent_1);
102 [ + - + - : 4 : auto tx_parent_2{MakeTransactionSpending({COINS[outpoints_index++]}, /*num_outputs=*/1, /*add_witness=*/false)};
+ - ]
103 [ + - ]: 2 : TRANSACTIONS.emplace_back(tx_parent_2);
104 [ + - + - : 4 : TRANSACTIONS.emplace_back(MakeTransactionSpending({COutPoint{tx_parent_1->GetHash(), 0}, COutPoint{tx_parent_2->GetHash(), 0}},
+ - + - ]
105 : : /*num_outputs=*/1, /*add_witness=*/true));
106 [ + - ]: 2 : }
107 : : // 1 parent 2 children
108 : 2 : {
109 [ + - + - ]: 4 : auto tx_parent{MakeTransactionSpending({COINS[outpoints_index++]}, /*num_outputs=*/2, /*add_witness=*/true)};
110 [ + - ]: 2 : TRANSACTIONS.emplace_back(tx_parent);
111 [ + - + - : 4 : TRANSACTIONS.emplace_back(MakeTransactionSpending({COutPoint{tx_parent->GetHash(), 0}},
+ - + - ]
112 : : /*num_outputs=*/1, /*add_witness=*/true));
113 [ + - + - : 4 : TRANSACTIONS.emplace_back(MakeTransactionSpending({COutPoint{tx_parent->GetHash(), 1}},
+ - + - ]
114 : : /*num_outputs=*/1, /*add_witness=*/true));
115 : 0 : }
116 : : // chain of 5 segwit
117 : 2 : {
118 : 2 : COutPoint& last_outpoint = COINS[outpoints_index++];
119 [ + + ]: 12 : for (auto i{0}; i < 5; ++i) {
120 [ + - + - ]: 20 : auto tx{MakeTransactionSpending({last_outpoint}, /*num_outputs=*/1, /*add_witness=*/true)};
121 [ + - ]: 10 : TRANSACTIONS.emplace_back(tx);
122 [ + - ]: 10 : last_outpoint = COutPoint{tx->GetHash(), 0};
123 : 10 : }
124 : : }
125 : : // chain of 5 non-segwit
126 : : {
127 : 12 : COutPoint& last_outpoint = COINS[outpoints_index++];
128 [ + + ]: 12 : for (auto i{0}; i < 5; ++i) {
129 [ + - + - ]: 20 : auto tx{MakeTransactionSpending({last_outpoint}, /*num_outputs=*/1, /*add_witness=*/false)};
130 [ + - ]: 10 : TRANSACTIONS.emplace_back(tx);
131 [ + - ]: 10 : last_outpoint = COutPoint{tx->GetHash(), 0};
132 : 10 : }
133 : : }
134 : : // Also create a loose tx for each outpoint. Some of these transactions conflict with the above
135 : : // or have the same txid.
136 [ + + ]: 102 : for (const auto& outpoint : COINS) {
137 [ + - + - ]: 200 : TRANSACTIONS.emplace_back(MakeTransactionSpending({outpoint}, /*num_outputs=*/1, /*add_witness=*/true));
138 : : }
139 : :
140 : : // Create random-looking time jumps
141 : : int i = 0;
142 : : // TIME_SKIPS[N] for N=0..15 is just N microseconds.
143 [ + + ]: 34 : for (; i < 16; ++i) {
144 : 32 : TIME_SKIPS[i] = std::chrono::microseconds{i};
145 : : }
146 : : // TIME_SKIPS[N] for N=16..127 has randomly-looking but roughly exponentially increasing values up to
147 : : // 198.416453 seconds.
148 [ + + ]: 226 : for (; i < 128; ++i) {
149 : 224 : int diff_bits = ((i - 10) * 2) / 9;
150 : 224 : uint64_t diff = 1 + (CSipHasher(0, 0).Write(i).Finalize() >> (64 - diff_bits));
151 : 224 : TIME_SKIPS[i] = TIME_SKIPS[i - 1] + std::chrono::microseconds{diff};
152 : : }
153 [ + - ]: 4 : }
154 : :
155 : 0 : void CheckPackageToValidate(const node::PackageToValidate& package_to_validate, NodeId peer)
156 : : {
157 : 0 : Assert(package_to_validate.m_senders.size() == 2);
158 : 0 : Assert(package_to_validate.m_senders.front() == peer);
159 : 0 : Assert(package_to_validate.m_senders.back() < NUM_PEERS);
160 : :
161 : : // Package is a 1p1c
162 : 0 : const auto& package = package_to_validate.m_txns;
163 : 0 : Assert(IsChildWithParents(package));
164 : 0 : Assert(package.size() == 2);
165 : 0 : }
166 : :
167 [ + - ]: 1682 : FUZZ_TARGET(txdownloadman, .init = initialize)
168 : : {
169 : 1268 : SeedRandomStateForTest(SeedRand::ZEROS);
170 : 1268 : FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
171 : 1268 : SetMockTime(ConsumeTime(fuzzed_data_provider));
172 : :
173 : : // Initialize txdownloadman
174 [ + - ]: 1268 : bilingual_str error;
175 [ + - + - ]: 1268 : CTxMemPool pool{MemPoolOptionsForTest(g_setup->m_node), error};
176 : 1268 : const auto max_orphan_count = fuzzed_data_provider.ConsumeIntegralInRange<unsigned int>(0, 300);
177 : 1268 : FastRandomContext det_rand{true};
178 [ + - ]: 1268 : node::TxDownloadManager txdownloadman{node::TxDownloadOptions{pool, det_rand, max_orphan_count, true}};
179 : :
180 : 1268 : std::chrono::microseconds time{244466666};
181 : :
182 [ + + + + ]: 24217 : LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000)
183 : : {
184 : 22949 : NodeId rand_peer = fuzzed_data_provider.ConsumeIntegralInRange<int64_t>(0, NUM_PEERS - 1);
185 : :
186 : : // Transaction can be one of the premade ones or a randomly generated one
187 [ + + ]: 22949 : auto rand_tx = fuzzed_data_provider.ConsumeBool() ?
188 [ - - ]: 21980 : MakeTransactionSpending(PickCoins(fuzzed_data_provider),
189 : 21980 : /*num_outputs=*/fuzzed_data_provider.ConsumeIntegralInRange(1, 500),
190 : 21980 : /*add_witness=*/fuzzed_data_provider.ConsumeBool()) :
191 [ + - + - : 22949 : TRANSACTIONS.at(fuzzed_data_provider.ConsumeIntegralInRange<unsigned>(0, TRANSACTIONS.size() - 1));
+ - + - ]
192 : :
193 [ + - ]: 22949 : CallOneOf(
194 : : fuzzed_data_provider,
195 : 426 : [&] {
196 : 426 : node::TxDownloadConnectionInfo info{
197 : 426 : .m_preferred = fuzzed_data_provider.ConsumeBool(),
198 : 852 : .m_relay_permissions = fuzzed_data_provider.ConsumeBool(),
199 : 852 : .m_wtxid_relay = fuzzed_data_provider.ConsumeBool()
200 : 426 : };
201 : 426 : txdownloadman.ConnectedPeer(rand_peer, info);
202 : 426 : },
203 : 7223 : [&] {
204 : 7223 : txdownloadman.DisconnectedPeer(rand_peer);
205 : 7223 : txdownloadman.CheckIsEmpty(rand_peer);
206 : 7223 : },
207 : 66 : [&] {
208 : 66 : txdownloadman.ActiveTipChange();
209 : 66 : },
210 : 4012 : [&] {
211 : 4012 : CBlock block;
212 [ + - ]: 4012 : block.vtx.push_back(rand_tx);
213 [ + - + - : 8024 : txdownloadman.BlockConnected(std::make_shared<CBlock>(block));
- + ]
214 : 4012 : },
215 : 196 : [&] {
216 : 196 : txdownloadman.BlockDisconnected();
217 : 196 : },
218 : 425 : [&] {
219 : 425 : txdownloadman.MempoolAcceptedTx(rand_tx);
220 : 425 : },
221 : 481 : [&] {
222 [ + - ]: 481 : TxValidationState state;
223 [ + - + - : 962 : state.Invalid(fuzzed_data_provider.PickValueInArray(TESTED_TX_RESULTS), "");
+ - ]
224 : 481 : bool first_time_failure{fuzzed_data_provider.ConsumeBool()};
225 : :
226 [ + - ]: 481 : node::RejectedTxTodo todo = txdownloadman.MempoolRejectedTx(rand_tx, state, rand_peer, first_time_failure);
227 [ + + - + : 481 : Assert(first_time_failure || !todo.m_should_add_extra_compact_tx);
+ - ]
228 : 962 : },
229 : 686 : [&] {
230 [ + + ]: 686 : GenTxid gtxid = fuzzed_data_provider.ConsumeBool() ?
231 : 611 : GenTxid::Txid(rand_tx->GetHash()) :
232 : 75 : GenTxid::Wtxid(rand_tx->GetWitnessHash());
233 : 686 : txdownloadman.AddTxAnnouncement(rand_peer, gtxid, time);
234 : 686 : },
235 : 79 : [&] {
236 : 79 : txdownloadman.GetRequestsToSend(rand_peer, time);
237 : 79 : },
238 : 9093 : [&] {
239 [ - + ]: 9093 : txdownloadman.ReceivedTx(rand_peer, rand_tx);
240 [ + + ]: 9093 : const auto& [should_validate, maybe_package] = txdownloadman.ReceivedTx(rand_peer, rand_tx);
241 : : // The only possible results should be:
242 : : // - Don't validate the tx, no package.
243 : : // - Don't validate the tx, package.
244 : : // - Validate the tx, no package.
245 : : // The only combination that doesn't make sense is validate both tx and package.
246 [ + + + - : 17805 : Assert(!(should_validate && maybe_package.has_value()));
+ - ]
247 [ - + - - ]: 9093 : if (maybe_package.has_value()) CheckPackageToValidate(*maybe_package, rand_peer);
248 : 9093 : },
249 : 111 : [&] {
250 [ + - ]: 222 : txdownloadman.ReceivedNotFound(rand_peer, {rand_tx->GetWitnessHash()});
251 : 111 : },
252 : 151 : [&] {
253 : 151 : const bool expect_work{txdownloadman.HaveMoreWork(rand_peer)};
254 : 151 : const auto ptx = txdownloadman.GetTxToReconsider(rand_peer);
255 : : // expect_work=true doesn't necessarily mean the next item from the workset isn't a
256 : : // nullptr, as the transaction could have been removed from orphanage without being
257 : : // removed from the peer's workset.
258 [ + + ]: 151 : if (ptx) {
259 : : // However, if there was a non-null tx in the workset, HaveMoreWork should have
260 : : // returned true.
261 [ + - ]: 1 : Assert(expect_work);
262 : : }
263 : 151 : }
264 : : );
265 : : // Jump forwards or backwards
266 : 22949 : auto time_skip = fuzzed_data_provider.PickValueInArray(TIME_SKIPS);
267 [ + + ]: 22949 : if (fuzzed_data_provider.ConsumeBool()) time_skip *= -1;
268 [ + - ]: 22949 : time += time_skip;
269 : 22949 : }
270 : : // Disconnect everybody, check that all data structures are empty.
271 [ + + ]: 21556 : for (NodeId nodeid = 0; nodeid < NUM_PEERS; ++nodeid) {
272 [ + - ]: 20288 : txdownloadman.DisconnectedPeer(nodeid);
273 [ + - ]: 20288 : txdownloadman.CheckIsEmpty(nodeid);
274 : : }
275 [ + - ]: 1268 : txdownloadman.CheckIsEmpty();
276 : 2536 : }
277 : :
278 : : // Give node 0 relay permissions, and nobody else. This helps us remember who is a RelayPermissions
279 : : // peer without tracking anything (this is only for the txdownload_impl target).
280 : 363700 : static bool HasRelayPermissions(NodeId peer) { return peer == 0; }
281 : :
282 : 22697 : static void CheckInvariants(const node::TxDownloadManagerImpl& txdownload_impl, size_t max_orphan_count)
283 : : {
284 : 22697 : const TxOrphanage& orphanage = txdownload_impl.m_orphanage;
285 : :
286 : : // Orphanage usage should never exceed what is allowed
287 : 22697 : Assert(orphanage.Size() <= max_orphan_count);
288 : :
289 : : // We should never have more than the maximum in-flight requests out for a peer.
290 [ + + ]: 385849 : for (NodeId peer = 0; peer < NUM_PEERS; ++peer) {
291 [ + + ]: 363152 : if (!HasRelayPermissions(peer)) {
292 : 340455 : Assert(txdownload_impl.m_txrequest.Count(peer) <= node::MAX_PEER_TX_ANNOUNCEMENTS);
293 : : }
294 : : }
295 : 22697 : txdownload_impl.m_txrequest.SanityCheck();
296 : 22697 : }
297 : :
298 [ + - ]: 1807 : FUZZ_TARGET(txdownloadman_impl, .init = initialize)
299 : : {
300 : 1393 : SeedRandomStateForTest(SeedRand::ZEROS);
301 : 1393 : FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
302 : 1393 : SetMockTime(ConsumeTime(fuzzed_data_provider));
303 : :
304 : : // Initialize a TxDownloadManagerImpl
305 [ + - ]: 1393 : bilingual_str error;
306 [ + - + - ]: 1393 : CTxMemPool pool{MemPoolOptionsForTest(g_setup->m_node), error};
307 : 1393 : const auto max_orphan_count = fuzzed_data_provider.ConsumeIntegralInRange<unsigned int>(0, 300);
308 : 1393 : FastRandomContext det_rand{true};
309 [ + - ]: 1393 : node::TxDownloadManagerImpl txdownload_impl{node::TxDownloadOptions{pool, det_rand, max_orphan_count, true}};
310 : :
311 : 1393 : std::chrono::microseconds time{244466666};
312 : :
313 [ + + + + ]: 24090 : LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000)
314 : : {
315 : 22697 : NodeId rand_peer = fuzzed_data_provider.ConsumeIntegralInRange<int64_t>(0, NUM_PEERS - 1);
316 : :
317 : : // Transaction can be one of the premade ones or a randomly generated one
318 [ + + ]: 22697 : auto rand_tx = fuzzed_data_provider.ConsumeBool() ?
319 [ - - ]: 21003 : MakeTransactionSpending(PickCoins(fuzzed_data_provider),
320 : 21003 : /*num_outputs=*/fuzzed_data_provider.ConsumeIntegralInRange(1, 500),
321 : 21003 : /*add_witness=*/fuzzed_data_provider.ConsumeBool()) :
322 [ + - + - : 22697 : TRANSACTIONS.at(fuzzed_data_provider.ConsumeIntegralInRange<unsigned>(0, TRANSACTIONS.size() - 1));
+ - + - ]
323 : :
324 [ + - ]: 22697 : CallOneOf(
325 : : fuzzed_data_provider,
326 : 548 : [&] {
327 : 548 : node::TxDownloadConnectionInfo info{
328 : 548 : .m_preferred = fuzzed_data_provider.ConsumeBool(),
329 : 548 : .m_relay_permissions = HasRelayPermissions(rand_peer),
330 : 1096 : .m_wtxid_relay = fuzzed_data_provider.ConsumeBool()
331 : 548 : };
332 : 548 : txdownload_impl.ConnectedPeer(rand_peer, info);
333 : 548 : },
334 : 4985 : [&] {
335 : 4985 : txdownload_impl.DisconnectedPeer(rand_peer);
336 : 4985 : txdownload_impl.CheckIsEmpty(rand_peer);
337 : 4985 : },
338 : 1303 : [&] {
339 : 1303 : txdownload_impl.ActiveTipChange();
340 : : // After a block update, nothing should be in the rejection caches
341 [ + + ]: 89907 : for (const auto& tx : TRANSACTIONS) {
342 : 88604 : Assert(!txdownload_impl.RecentRejectsFilter().contains(tx->GetWitnessHash().ToUint256()));
343 : 88604 : Assert(!txdownload_impl.RecentRejectsFilter().contains(tx->GetHash().ToUint256()));
344 : 88604 : Assert(!txdownload_impl.RecentRejectsReconsiderableFilter().contains(tx->GetWitnessHash().ToUint256()));
345 : 88604 : Assert(!txdownload_impl.RecentRejectsReconsiderableFilter().contains(tx->GetHash().ToUint256()));
346 : : }
347 : 1303 : },
348 : 3427 : [&] {
349 : 3427 : CBlock block;
350 [ + - ]: 3427 : block.vtx.push_back(rand_tx);
351 [ + - + - : 6854 : txdownload_impl.BlockConnected(std::make_shared<CBlock>(block));
- + ]
352 : : // Block transactions must be removed from orphanage
353 [ + - + - ]: 3427 : Assert(!txdownload_impl.m_orphanage.HaveTx(rand_tx->GetWitnessHash()));
354 : 3427 : },
355 : 127 : [&] {
356 : 127 : txdownload_impl.BlockDisconnected();
357 : 127 : Assert(!txdownload_impl.RecentConfirmedTransactionsFilter().contains(rand_tx->GetWitnessHash().ToUint256()));
358 : 127 : Assert(!txdownload_impl.RecentConfirmedTransactionsFilter().contains(rand_tx->GetHash().ToUint256()));
359 : 127 : },
360 : 245 : [&] {
361 : 245 : txdownload_impl.MempoolAcceptedTx(rand_tx);
362 : 245 : },
363 : 143 : [&] {
364 [ + - ]: 143 : TxValidationState state;
365 [ + - + - : 286 : state.Invalid(fuzzed_data_provider.PickValueInArray(TESTED_TX_RESULTS), "");
+ - ]
366 : 143 : bool first_time_failure{fuzzed_data_provider.ConsumeBool()};
367 : :
368 [ + - + - ]: 143 : bool reject_contains_wtxid{txdownload_impl.RecentRejectsFilter().contains(rand_tx->GetWitnessHash().ToUint256())};
369 : :
370 [ + - ]: 143 : node::RejectedTxTodo todo = txdownload_impl.MempoolRejectedTx(rand_tx, state, rand_peer, first_time_failure);
371 [ + + - + : 143 : Assert(first_time_failure || !todo.m_should_add_extra_compact_tx);
+ - ]
372 [ + + + - ]: 143 : if (!reject_contains_wtxid) Assert(todo.m_unique_parents.size() <= rand_tx->vin.size());
373 : 286 : },
374 : 1930 : [&] {
375 [ + + ]: 1930 : GenTxid gtxid = fuzzed_data_provider.ConsumeBool() ?
376 : 1834 : GenTxid::Txid(rand_tx->GetHash()) :
377 : 96 : GenTxid::Wtxid(rand_tx->GetWitnessHash());
378 : 1930 : txdownload_impl.AddTxAnnouncement(rand_peer, gtxid, time);
379 : 1930 : },
380 : 1207 : [&] {
381 : 1207 : const auto getdata_requests = txdownload_impl.GetRequestsToSend(rand_peer, time);
382 : : // TxDownloadManager should not be telling us to request things we already have.
383 : : // Exclude m_lazy_recent_rejects_reconsiderable because it may request low-feerate parent of orphan.
384 [ - + ]: 1207 : for (const auto& gtxid : getdata_requests) {
385 [ # # # # ]: 0 : Assert(!txdownload_impl.AlreadyHaveTx(gtxid, /*include_reconsiderable=*/false));
386 : : }
387 : 1207 : },
388 : 8407 : [&] {
389 [ + + ]: 8407 : const auto& [should_validate, maybe_package] = txdownload_impl.ReceivedTx(rand_peer, rand_tx);
390 : : // The only possible results should be:
391 : : // - Don't validate the tx, no package.
392 : : // - Don't validate the tx, package.
393 : : // - Validate the tx, no package.
394 : : // The only combination that doesn't make sense is validate both tx and package.
395 [ + + + - : 16580 : Assert(!(should_validate && maybe_package.has_value()));
+ - ]
396 [ + + ]: 8407 : if (should_validate) {
397 [ + - + - ]: 8173 : Assert(!txdownload_impl.AlreadyHaveTx(GenTxid::Wtxid(rand_tx->GetWitnessHash()), /*include_reconsiderable=*/true));
398 : : }
399 [ - + ]: 8407 : if (maybe_package.has_value()) {
400 [ # # ]: 0 : CheckPackageToValidate(*maybe_package, rand_peer);
401 : :
402 [ # # ]: 0 : const auto& package = maybe_package->m_txns;
403 : : // Parent is in m_lazy_recent_rejects_reconsiderable and child is in m_orphanage
404 [ # # # # : 0 : Assert(txdownload_impl.RecentRejectsReconsiderableFilter().contains(rand_tx->GetWitnessHash().ToUint256()));
# # ]
405 [ # # # # ]: 0 : Assert(txdownload_impl.m_orphanage.HaveTx(maybe_package->m_txns.back()->GetWitnessHash()));
406 : : // Package has not been rejected
407 [ # # # # : 0 : Assert(!txdownload_impl.RecentRejectsReconsiderableFilter().contains(GetPackageHash(package)));
# # # # ]
408 : : // Neither is in m_lazy_recent_rejects
409 [ # # # # : 0 : Assert(!txdownload_impl.RecentRejectsFilter().contains(package.front()->GetWitnessHash().ToUint256()));
# # ]
410 [ # # # # : 0 : Assert(!txdownload_impl.RecentRejectsFilter().contains(package.back()->GetWitnessHash().ToUint256()));
# # ]
411 : : }
412 : 8407 : },
413 : 109 : [&] {
414 [ + - ]: 218 : txdownload_impl.ReceivedNotFound(rand_peer, {rand_tx->GetWitnessHash()});
415 : 109 : },
416 : 266 : [&] {
417 : 266 : const bool expect_work{txdownload_impl.HaveMoreWork(rand_peer)};
418 : 266 : const auto ptx{txdownload_impl.GetTxToReconsider(rand_peer)};
419 : : // expect_work=true doesn't necessarily mean the next item from the workset isn't a
420 : : // nullptr, as the transaction could have been removed from orphanage without being
421 : : // removed from the peer's workset.
422 [ - + ]: 266 : if (ptx) {
423 : : // However, if there was a non-null tx in the workset, HaveMoreWork should have
424 : : // returned true.
425 [ # # ]: 0 : Assert(expect_work);
426 [ # # # # ]: 0 : Assert(txdownload_impl.AlreadyHaveTx(GenTxid::Wtxid(ptx->GetWitnessHash()), /*include_reconsiderable=*/false));
427 : : // Presumably we have validated this tx. Use "missing inputs" to keep it in the
428 : : // orphanage longer. Later iterations might call MempoolAcceptedTx or
429 : : // MempoolRejectedTx with a different error.
430 [ # # ]: 0 : TxValidationState state_missing_inputs;
431 [ # # # # : 0 : state_missing_inputs.Invalid(TxValidationResult::TX_MISSING_INPUTS, "");
# # ]
432 [ # # ]: 0 : txdownload_impl.MempoolRejectedTx(ptx, state_missing_inputs, rand_peer, fuzzed_data_provider.ConsumeBool());
433 : 0 : }
434 : 266 : }
435 : : );
436 : :
437 : 22697 : auto time_skip = fuzzed_data_provider.PickValueInArray(TIME_SKIPS);
438 [ + + ]: 22697 : if (fuzzed_data_provider.ConsumeBool()) time_skip *= -1;
439 [ + - ]: 22697 : time += time_skip;
440 [ + - ]: 22697 : CheckInvariants(txdownload_impl, max_orphan_count);
441 : 22697 : }
442 : : // Disconnect everybody, check that all data structures are empty.
443 [ + + ]: 23681 : for (NodeId nodeid = 0; nodeid < NUM_PEERS; ++nodeid) {
444 [ + - ]: 22288 : txdownload_impl.DisconnectedPeer(nodeid);
445 [ + - ]: 22288 : txdownload_impl.CheckIsEmpty(nodeid);
446 : : }
447 [ + - ]: 1393 : txdownload_impl.CheckIsEmpty();
448 : 2786 : }
449 : :
450 : : } // namespace
|