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 : 27878 : static CTransactionRef MakeTransactionSpending(const std::vector<COutPoint>& outpoints, size_t num_outputs, bool add_witness)
59 : : {
60 : 27878 : CMutableTransaction tx;
61 : : // If no outpoints are given, create a random one.
62 [ + + ]: 254826 : for (const auto& outpoint : outpoints) {
63 [ + - ]: 226948 : tx.vin.emplace_back(outpoint);
64 : : }
65 [ + + ]: 27878 : if (add_witness) {
66 [ + - ]: 49576 : tx.vin[0].scriptWitness.stack.push_back({1});
67 : : }
68 [ + - + + ]: 7133812 : for (size_t o = 0; o < num_outputs; ++o) tx.vout.emplace_back(CENT, P2WSH_OP_TRUE);
69 [ + - ]: 55756 : return MakeTransactionRef(tx);
70 : 27878 : }
71 : 27742 : static std::vector<COutPoint> PickCoins(FuzzedDataProvider& fuzzed_data_provider)
72 : : {
73 : 27742 : std::vector<COutPoint> ret;
74 [ + - ]: 27742 : ret.push_back(fuzzed_data_provider.PickValueInArray(COINS));
75 [ + + + + ]: 226810 : LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10) {
76 [ + - ]: 199068 : ret.push_back(fuzzed_data_provider.PickValueInArray(COINS));
77 : : }
78 : 27742 : 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 : 1 : void CheckPackageToValidate(const node::PackageToValidate& package_to_validate, NodeId peer)
156 : : {
157 : 1 : Assert(package_to_validate.m_senders.size() == 2);
158 : 1 : Assert(package_to_validate.m_senders.front() == peer);
159 : 1 : Assert(package_to_validate.m_senders.back() < NUM_PEERS);
160 : :
161 : : // Package is a 1p1c
162 : 1 : const auto& package = package_to_validate.m_txns;
163 : 1 : Assert(IsChildWithParents(package));
164 : 1 : Assert(package.size() == 2);
165 : 1 : }
166 : :
167 [ + - ]: 1369 : FUZZ_TARGET(txdownloadman, .init = initialize)
168 : : {
169 : 915 : SeedRandomStateForTest(SeedRand::ZEROS);
170 : 915 : FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
171 : 915 : SetMockTime(ConsumeTime(fuzzed_data_provider));
172 : :
173 : : // Initialize txdownloadman
174 [ + - ]: 915 : bilingual_str error;
175 [ + - + - ]: 915 : CTxMemPool pool{MemPoolOptionsForTest(g_setup->m_node), error};
176 : 915 : FastRandomContext det_rand{true};
177 [ + - ]: 915 : node::TxDownloadManager txdownloadman{node::TxDownloadOptions{pool, det_rand, true}};
178 : :
179 : 915 : std::chrono::microseconds time{244466666};
180 : :
181 [ + + + - ]: 7693 : LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000)
182 : : {
183 : 6778 : NodeId rand_peer = fuzzed_data_provider.ConsumeIntegralInRange<int64_t>(0, NUM_PEERS - 1);
184 : :
185 : : // Transaction can be one of the premade ones or a randomly generated one
186 [ + + ]: 6778 : auto rand_tx = fuzzed_data_provider.ConsumeBool() ?
187 [ - - ]: 6263 : MakeTransactionSpending(PickCoins(fuzzed_data_provider),
188 : 6263 : /*num_outputs=*/fuzzed_data_provider.ConsumeIntegralInRange(1, 500),
189 : 6263 : /*add_witness=*/fuzzed_data_provider.ConsumeBool()) :
190 [ + - + - : 6778 : TRANSACTIONS.at(fuzzed_data_provider.ConsumeIntegralInRange<unsigned>(0, TRANSACTIONS.size() - 1));
+ - + - ]
191 : :
192 [ + - ]: 6778 : CallOneOf(
193 : : fuzzed_data_provider,
194 : 796 : [&] {
195 : 796 : node::TxDownloadConnectionInfo info{
196 : 796 : .m_preferred = fuzzed_data_provider.ConsumeBool(),
197 : 1592 : .m_relay_permissions = fuzzed_data_provider.ConsumeBool(),
198 : 1592 : .m_wtxid_relay = fuzzed_data_provider.ConsumeBool()
199 : 796 : };
200 : 796 : txdownloadman.ConnectedPeer(rand_peer, info);
201 : 796 : },
202 : 1865 : [&] {
203 : 1865 : txdownloadman.DisconnectedPeer(rand_peer);
204 : 1865 : txdownloadman.CheckIsEmpty(rand_peer);
205 : 1865 : },
206 : 48 : [&] {
207 : 48 : txdownloadman.ActiveTipChange();
208 : 48 : },
209 : 481 : [&] {
210 : 481 : CBlock block;
211 [ + - ]: 481 : block.vtx.push_back(rand_tx);
212 [ + - + - : 962 : txdownloadman.BlockConnected(std::make_shared<CBlock>(block));
- + ]
213 : 481 : },
214 : 317 : [&] {
215 : 317 : txdownloadman.BlockDisconnected();
216 : 317 : },
217 : 413 : [&] {
218 : 413 : txdownloadman.MempoolAcceptedTx(rand_tx);
219 : 413 : },
220 : 244 : [&] {
221 [ + - ]: 244 : TxValidationState state;
222 [ + - + - : 488 : state.Invalid(fuzzed_data_provider.PickValueInArray(TESTED_TX_RESULTS), "");
+ - ]
223 : 244 : bool first_time_failure{fuzzed_data_provider.ConsumeBool()};
224 : :
225 [ + - ]: 244 : node::RejectedTxTodo todo = txdownloadman.MempoolRejectedTx(rand_tx, state, rand_peer, first_time_failure);
226 [ + + - + : 244 : Assert(first_time_failure || !todo.m_should_add_extra_compact_tx);
+ - ]
227 : 488 : },
228 : 2098 : [&] {
229 [ + + ]: 2098 : auto gtxid = fuzzed_data_provider.ConsumeBool() ?
230 : 2026 : GenTxid{rand_tx->GetHash()} :
231 : 2098 : GenTxid{rand_tx->GetWitnessHash()};
232 : 2098 : txdownloadman.AddTxAnnouncement(rand_peer, gtxid, time);
233 : 2098 : },
234 : 204 : [&] {
235 : 204 : txdownloadman.GetRequestsToSend(rand_peer, time);
236 : 204 : },
237 : 147 : [&] {
238 [ + + ]: 147 : txdownloadman.ReceivedTx(rand_peer, rand_tx);
239 [ + + ]: 147 : const auto& [should_validate, maybe_package] = txdownloadman.ReceivedTx(rand_peer, rand_tx);
240 : : // The only possible results should be:
241 : : // - Don't validate the tx, no package.
242 : : // - Don't validate the tx, package.
243 : : // - Validate the tx, no package.
244 : : // The only combination that doesn't make sense is validate both tx and package.
245 [ + + + - : 291 : Assert(!(should_validate && maybe_package.has_value()));
+ - ]
246 [ + + + - ]: 147 : if (maybe_package.has_value()) CheckPackageToValidate(*maybe_package, rand_peer);
247 : 147 : },
248 : 125 : [&] {
249 [ + - ]: 250 : txdownloadman.ReceivedNotFound(rand_peer, {rand_tx->GetWitnessHash()});
250 : 125 : },
251 : 40 : [&] {
252 : 40 : const bool expect_work{txdownloadman.HaveMoreWork(rand_peer)};
253 : 40 : const auto ptx = txdownloadman.GetTxToReconsider(rand_peer);
254 : : // expect_work=true doesn't necessarily mean the next item from the workset isn't a
255 : : // nullptr, as the transaction could have been removed from orphanage without being
256 : : // removed from the peer's workset.
257 [ - + ]: 40 : if (ptx) {
258 : : // However, if there was a non-null tx in the workset, HaveMoreWork should have
259 : : // returned true.
260 [ # # ]: 0 : Assert(expect_work);
261 : : }
262 : 40 : });
263 : : // Jump forwards or backwards
264 : 6778 : auto time_skip = fuzzed_data_provider.PickValueInArray(TIME_SKIPS);
265 [ + + ]: 6778 : if (fuzzed_data_provider.ConsumeBool()) time_skip *= -1;
266 [ + - ]: 6778 : time += time_skip;
267 : 6778 : }
268 : : // Disconnect everybody, check that all data structures are empty.
269 [ + + ]: 15555 : for (NodeId nodeid = 0; nodeid < NUM_PEERS; ++nodeid) {
270 [ + - ]: 14640 : txdownloadman.DisconnectedPeer(nodeid);
271 [ + - ]: 14640 : txdownloadman.CheckIsEmpty(nodeid);
272 : : }
273 [ + - ]: 915 : txdownloadman.CheckIsEmpty();
274 : 1830 : }
275 : :
276 : : // Give node 0 relay permissions, and nobody else. This helps us remember who is a RelayPermissions
277 : : // peer without tracking anything (this is only for the txdownload_impl target).
278 : 20767 : static bool HasRelayPermissions(NodeId peer) { return peer == 0; }
279 : :
280 : 1165 : static void CheckInvariants(const node::TxDownloadManagerImpl& txdownload_impl)
281 : : {
282 : 1165 : txdownload_impl.m_orphanage->SanityCheck();
283 : : // We should never have more than the maximum in-flight requests out for a peer.
284 [ + + ]: 19805 : for (NodeId peer = 0; peer < NUM_PEERS; ++peer) {
285 [ + + ]: 18640 : if (!HasRelayPermissions(peer)) {
286 : 17475 : Assert(txdownload_impl.m_txrequest.Count(peer) <= node::MAX_PEER_TX_ANNOUNCEMENTS);
287 : : }
288 : : }
289 : 1165 : txdownload_impl.m_txrequest.SanityCheck();
290 : 1165 : }
291 : :
292 [ + - ]: 1619 : FUZZ_TARGET(txdownloadman_impl, .init = initialize)
293 : : {
294 : 1165 : SeedRandomStateForTest(SeedRand::ZEROS);
295 : 1165 : FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
296 : 1165 : SetMockTime(ConsumeTime(fuzzed_data_provider));
297 : :
298 : : // Initialize a TxDownloadManagerImpl
299 [ + - ]: 1165 : bilingual_str error;
300 [ + - + - ]: 1165 : CTxMemPool pool{MemPoolOptionsForTest(g_setup->m_node), error};
301 : 1165 : FastRandomContext det_rand{true};
302 [ + - ]: 1165 : node::TxDownloadManagerImpl txdownload_impl{node::TxDownloadOptions{pool, det_rand, true}};
303 : :
304 : 1165 : std::chrono::microseconds time{244466666};
305 : :
306 [ + + + + ]: 25403 : LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000)
307 : : {
308 : 24238 : NodeId rand_peer = fuzzed_data_provider.ConsumeIntegralInRange<int64_t>(0, NUM_PEERS - 1);
309 : :
310 : : // Transaction can be one of the premade ones or a randomly generated one
311 [ + + ]: 24238 : auto rand_tx = fuzzed_data_provider.ConsumeBool() ?
312 [ - - ]: 21479 : MakeTransactionSpending(PickCoins(fuzzed_data_provider),
313 : 21479 : /*num_outputs=*/fuzzed_data_provider.ConsumeIntegralInRange(1, 500),
314 : 21479 : /*add_witness=*/fuzzed_data_provider.ConsumeBool()) :
315 [ + - + - : 24238 : TRANSACTIONS.at(fuzzed_data_provider.ConsumeIntegralInRange<unsigned>(0, TRANSACTIONS.size() - 1));
+ - + - ]
316 : :
317 [ + - ]: 24238 : CallOneOf(
318 : : fuzzed_data_provider,
319 : 2127 : [&] {
320 : 2127 : node::TxDownloadConnectionInfo info{
321 : 2127 : .m_preferred = fuzzed_data_provider.ConsumeBool(),
322 : 2127 : .m_relay_permissions = HasRelayPermissions(rand_peer),
323 : 4254 : .m_wtxid_relay = fuzzed_data_provider.ConsumeBool()
324 : 2127 : };
325 : 2127 : txdownload_impl.ConnectedPeer(rand_peer, info);
326 : 2127 : },
327 : 4486 : [&] {
328 : 4486 : txdownload_impl.DisconnectedPeer(rand_peer);
329 : 4486 : txdownload_impl.CheckIsEmpty(rand_peer);
330 : 4486 : },
331 : 1825 : [&] {
332 : 1825 : txdownload_impl.ActiveTipChange();
333 : : // After a block update, nothing should be in the rejection caches
334 [ + + ]: 125925 : for (const auto& tx : TRANSACTIONS) {
335 : 124100 : Assert(!txdownload_impl.RecentRejectsFilter().contains(tx->GetWitnessHash().ToUint256()));
336 : 124100 : Assert(!txdownload_impl.RecentRejectsFilter().contains(tx->GetHash().ToUint256()));
337 : 124100 : Assert(!txdownload_impl.RecentRejectsReconsiderableFilter().contains(tx->GetWitnessHash().ToUint256()));
338 : 124100 : Assert(!txdownload_impl.RecentRejectsReconsiderableFilter().contains(tx->GetHash().ToUint256()));
339 : : }
340 : 1825 : },
341 : 3565 : [&] {
342 : 3565 : CBlock block;
343 [ + - ]: 3565 : block.vtx.push_back(rand_tx);
344 [ + - + - : 7130 : txdownload_impl.BlockConnected(std::make_shared<CBlock>(block));
- + ]
345 : : // Block transactions must be removed from orphanage
346 [ + - + - ]: 3565 : Assert(!txdownload_impl.m_orphanage->HaveTx(rand_tx->GetWitnessHash()));
347 : 3565 : },
348 : 185 : [&] {
349 : 185 : txdownload_impl.BlockDisconnected();
350 : 185 : Assert(!txdownload_impl.RecentConfirmedTransactionsFilter().contains(rand_tx->GetWitnessHash().ToUint256()));
351 : 185 : Assert(!txdownload_impl.RecentConfirmedTransactionsFilter().contains(rand_tx->GetHash().ToUint256()));
352 : 185 : },
353 : 266 : [&] {
354 : 266 : txdownload_impl.MempoolAcceptedTx(rand_tx);
355 : 266 : },
356 : 261 : [&] {
357 [ + - ]: 261 : TxValidationState state;
358 [ + - + - : 522 : state.Invalid(fuzzed_data_provider.PickValueInArray(TESTED_TX_RESULTS), "");
+ - ]
359 : 261 : bool first_time_failure{fuzzed_data_provider.ConsumeBool()};
360 : :
361 [ + - + - ]: 261 : bool reject_contains_wtxid{txdownload_impl.RecentRejectsFilter().contains(rand_tx->GetWitnessHash().ToUint256())};
362 : :
363 [ + - ]: 261 : node::RejectedTxTodo todo = txdownload_impl.MempoolRejectedTx(rand_tx, state, rand_peer, first_time_failure);
364 [ + + - + : 261 : Assert(first_time_failure || !todo.m_should_add_extra_compact_tx);
+ - ]
365 [ + + + - ]: 261 : if (!reject_contains_wtxid) Assert(todo.m_unique_parents.size() <= rand_tx->vin.size());
366 : 522 : },
367 : 2046 : [&] {
368 [ + + ]: 2046 : auto gtxid = fuzzed_data_provider.ConsumeBool() ?
369 : 1922 : GenTxid{rand_tx->GetHash()} :
370 : 2046 : GenTxid{rand_tx->GetWitnessHash()};
371 : 2046 : txdownload_impl.AddTxAnnouncement(rand_peer, gtxid, time);
372 : 2046 : },
373 : 2229 : [&] {
374 : 2229 : const auto getdata_requests = txdownload_impl.GetRequestsToSend(rand_peer, time);
375 : : // TxDownloadManager should not be telling us to request things we already have.
376 : : // Exclude m_lazy_recent_rejects_reconsiderable because it may request low-feerate parent of orphan.
377 [ + + ]: 2232 : for (const auto& gtxid : getdata_requests) {
378 [ + - + - ]: 3 : Assert(!txdownload_impl.AlreadyHaveTx(gtxid, /*include_reconsiderable=*/false));
379 : : }
380 : 2229 : },
381 : 6339 : [&] {
382 [ + + ]: 6339 : const auto& [should_validate, maybe_package] = txdownload_impl.ReceivedTx(rand_peer, rand_tx);
383 : : // The only possible results should be:
384 : : // - Don't validate the tx, no package.
385 : : // - Don't validate the tx, package.
386 : : // - Validate the tx, no package.
387 : : // The only combination that doesn't make sense is validate both tx and package.
388 [ + + + - : 12475 : Assert(!(should_validate && maybe_package.has_value()));
+ - ]
389 [ + + ]: 6339 : if (should_validate) {
390 [ + - + - ]: 6136 : Assert(!txdownload_impl.AlreadyHaveTx(rand_tx->GetWitnessHash(), /*include_reconsiderable=*/true));
391 : : }
392 [ - + ]: 6339 : if (maybe_package.has_value()) {
393 [ # # ]: 0 : CheckPackageToValidate(*maybe_package, rand_peer);
394 : :
395 [ # # ]: 0 : const auto& package = maybe_package->m_txns;
396 : : // Parent is in m_lazy_recent_rejects_reconsiderable and child is in m_orphanage
397 [ # # # # : 0 : Assert(txdownload_impl.RecentRejectsReconsiderableFilter().contains(rand_tx->GetWitnessHash().ToUint256()));
# # ]
398 [ # # # # ]: 0 : Assert(txdownload_impl.m_orphanage->HaveTx(maybe_package->m_txns.back()->GetWitnessHash()));
399 : : // Package has not been rejected
400 [ # # # # : 0 : Assert(!txdownload_impl.RecentRejectsReconsiderableFilter().contains(GetPackageHash(package)));
# # # # ]
401 : : // Neither is in m_lazy_recent_rejects
402 [ # # # # : 0 : Assert(!txdownload_impl.RecentRejectsFilter().contains(package.front()->GetWitnessHash().ToUint256()));
# # ]
403 [ # # # # : 0 : Assert(!txdownload_impl.RecentRejectsFilter().contains(package.back()->GetWitnessHash().ToUint256()));
# # ]
404 : : }
405 : 6339 : },
406 : 627 : [&] {
407 [ + - ]: 1254 : txdownload_impl.ReceivedNotFound(rand_peer, {rand_tx->GetWitnessHash()});
408 : 627 : },
409 : 282 : [&] {
410 : 282 : const bool expect_work{txdownload_impl.HaveMoreWork(rand_peer)};
411 : 282 : const auto ptx{txdownload_impl.GetTxToReconsider(rand_peer)};
412 : : // expect_work=true doesn't necessarily mean the next item from the workset isn't a
413 : : // nullptr, as the transaction could have been removed from orphanage without being
414 : : // removed from the peer's workset.
415 [ - + ]: 282 : if (ptx) {
416 : : // However, if there was a non-null tx in the workset, HaveMoreWork should have
417 : : // returned true.
418 [ # # ]: 0 : Assert(expect_work);
419 [ # # # # ]: 0 : Assert(txdownload_impl.AlreadyHaveTx(ptx->GetWitnessHash(), /*include_reconsiderable=*/false));
420 : : // Presumably we have validated this tx. Use "missing inputs" to keep it in the
421 : : // orphanage longer. Later iterations might call MempoolAcceptedTx or
422 : : // MempoolRejectedTx with a different error.
423 [ # # ]: 0 : TxValidationState state_missing_inputs;
424 [ # # # # : 0 : state_missing_inputs.Invalid(TxValidationResult::TX_MISSING_INPUTS, "");
# # ]
425 [ # # ]: 0 : txdownload_impl.MempoolRejectedTx(ptx, state_missing_inputs, rand_peer, fuzzed_data_provider.ConsumeBool());
426 : 0 : }
427 : 282 : });
428 : :
429 : 24238 : auto time_skip = fuzzed_data_provider.PickValueInArray(TIME_SKIPS);
430 [ + + ]: 24238 : if (fuzzed_data_provider.ConsumeBool()) time_skip *= -1;
431 [ + - ]: 24238 : time += time_skip;
432 : 24238 : }
433 [ + - ]: 1165 : CheckInvariants(txdownload_impl);
434 : : // Disconnect everybody, check that all data structures are empty.
435 [ + + ]: 19805 : for (NodeId nodeid = 0; nodeid < NUM_PEERS; ++nodeid) {
436 [ + - ]: 18640 : txdownload_impl.DisconnectedPeer(nodeid);
437 [ + - ]: 18640 : txdownload_impl.CheckIsEmpty(nodeid);
438 : : }
439 [ + - ]: 1165 : txdownload_impl.CheckIsEmpty();
440 : 2330 : }
441 : :
442 : : } // namespace
|