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