LCOV - code coverage report
Current view: top level - src/test/fuzz - txdownloadman.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 99.3 % 271 269
Test Date: 2026-08-29 05:53:03 Functions: 100.0 % 30 30
Branches: 58.7 % 412 242

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

Generated by: LCOV version 2.5.0-full