LCOV - code coverage report
Current view: top level - src/test/fuzz - txorphan.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 100.0 % 130 130
Test Date: 2025-02-22 04:11:21 Functions: 100.0 % 12 12
Branches: 69.3 % 114 79

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2022-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/amount.h>
       6                 :             : #include <consensus/validation.h>
       7                 :             : #include <net_processing.h>
       8                 :             : #include <node/eviction.h>
       9                 :             : #include <policy/policy.h>
      10                 :             : #include <primitives/transaction.h>
      11                 :             : #include <script/script.h>
      12                 :             : #include <sync.h>
      13                 :             : #include <test/fuzz/FuzzedDataProvider.h>
      14                 :             : #include <test/fuzz/fuzz.h>
      15                 :             : #include <test/fuzz/util.h>
      16                 :             : #include <test/util/setup_common.h>
      17                 :             : #include <txorphanage.h>
      18                 :             : #include <uint256.h>
      19                 :             : #include <util/check.h>
      20                 :             : #include <util/time.h>
      21                 :             : 
      22                 :             : #include <cstdint>
      23                 :             : #include <memory>
      24                 :             : #include <set>
      25                 :             : #include <utility>
      26                 :             : #include <vector>
      27                 :             : 
      28                 :           1 : void initialize_orphanage()
      29                 :             : {
      30   [ +  -  +  - ]:           2 :     static const auto testing_setup = MakeNoLogFileContext();
      31         [ +  - ]:           2 : }
      32                 :             : 
      33         [ +  - ]:        1089 : FUZZ_TARGET(txorphan, .init = initialize_orphanage)
      34                 :             : {
      35                 :         657 :     FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
      36                 :         657 :     FastRandomContext orphanage_rng{/*fDeterministic=*/true};
      37         [ +  - ]:         657 :     SetMockTime(ConsumeTime(fuzzed_data_provider));
      38                 :             : 
      39                 :         657 :     TxOrphanage orphanage;
      40                 :         657 :     std::vector<COutPoint> outpoints; // Duplicates are tolerated
      41         [ +  - ]:         657 :     outpoints.reserve(200'000);
      42                 :             : 
      43                 :             :     // initial outpoints used to construct transactions later
      44         [ +  + ]:        3285 :     for (uint8_t i = 0; i < 4; i++) {
      45         [ +  - ]:        2628 :         outpoints.emplace_back(Txid::FromUint256(uint256{i}), 0);
      46                 :             :     }
      47                 :             : 
      48                 :         657 :     CTransactionRef ptx_potential_parent = nullptr;
      49                 :             : 
      50                 :         657 :     std::vector<CTransactionRef> tx_history;
      51                 :             : 
      52   [ +  -  +  +  :       11632 :     LIMITED_WHILE(outpoints.size() < 200'000 && fuzzed_data_provider.ConsumeBool(), 10 * DEFAULT_MAX_ORPHAN_TRANSACTIONS)
                   -  + ]
      53                 :             :     {
      54                 :             :         // construct transaction
      55                 :       10318 :         const CTransactionRef tx = [&] {
      56                 :        5159 :             CMutableTransaction tx_mut;
      57                 :        5159 :             const auto num_in = fuzzed_data_provider.ConsumeIntegralInRange<uint32_t>(1, outpoints.size());
      58                 :        5159 :             const auto num_out = fuzzed_data_provider.ConsumeIntegralInRange<uint32_t>(1, 256);
      59                 :             :             // pick outpoints from outpoints as input. We allow input duplicates on purpose, given we are not
      60                 :             :             // running any transaction validation logic before adding transactions to the orphanage
      61         [ +  - ]:        5159 :             tx_mut.vin.reserve(num_in);
      62         [ +  + ]:      894392 :             for (uint32_t i = 0; i < num_in; i++) {
      63                 :      889233 :                 auto& prevout = PickValue(fuzzed_data_provider, outpoints);
      64                 :             :                 // try making transactions unique by setting a random nSequence, but allow duplicate transactions if they happen
      65         [ +  - ]:     1778466 :                 tx_mut.vin.emplace_back(prevout, CScript{}, fuzzed_data_provider.ConsumeIntegralInRange<uint32_t>(0, CTxIn::SEQUENCE_FINAL));
      66                 :             :             }
      67                 :             :             // output amount will not affect txorphanage
      68         [ +  - ]:        5159 :             tx_mut.vout.reserve(num_out);
      69         [ +  + ]:      292450 :             for (uint32_t i = 0; i < num_out; i++) {
      70         [ +  - ]:      574582 :                 tx_mut.vout.emplace_back(CAmount{0}, CScript{});
      71                 :             :             }
      72         [ +  - ]:        5159 :             auto new_tx = MakeTransactionRef(tx_mut);
      73                 :             :             // add newly constructed outpoints to the coin pool
      74         [ +  + ]:      292450 :             for (uint32_t i = 0; i < num_out; i++) {
      75         [ +  - ]:      287291 :                 outpoints.emplace_back(new_tx->GetHash(), i);
      76                 :             :             }
      77                 :        5159 :             return new_tx;
      78         [ +  - ]:       10318 :         }();
      79                 :             : 
      80         [ +  - ]:        5159 :         tx_history.push_back(tx);
      81                 :             : 
      82         [ +  + ]:        5159 :         const auto wtxid{tx->GetWitnessHash()};
      83                 :             : 
      84                 :             :         // Trigger orphanage functions that are called using parents. ptx_potential_parent is a tx we constructed in a
      85                 :             :         // previous loop and potentially the parent of this tx.
      86         [ +  + ]:        5159 :         if (ptx_potential_parent) {
      87                 :             :             // Set up future GetTxToReconsider call.
      88         [ +  - ]:        4512 :             orphanage.AddChildrenToWorkSet(*ptx_potential_parent, orphanage_rng);
      89                 :             : 
      90                 :             :             // Check that all txns returned from GetChildrenFrom* are indeed a direct child of this tx.
      91                 :        4512 :             NodeId peer_id = fuzzed_data_provider.ConsumeIntegral<NodeId>();
      92   [ +  -  +  + ]:        5913 :             for (const auto& child : orphanage.GetChildrenFromSamePeer(ptx_potential_parent, peer_id)) {
      93         [ -  + ]:        3428 :                 assert(std::any_of(child->vin.cbegin(), child->vin.cend(), [&](const auto& input) {
      94                 :             :                     return input.prevout.hash == ptx_potential_parent->GetHash();
      95                 :             :                 }));
      96                 :        4512 :             }
      97                 :             :         }
      98                 :             : 
      99                 :             :         // trigger orphanage functions
     100   [ +  +  +  + ]:      260303 :         LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10 * DEFAULT_MAX_ORPHAN_TRANSACTIONS)
     101                 :             :         {
     102                 :      255144 :             NodeId peer_id = fuzzed_data_provider.ConsumeIntegral<NodeId>();
     103                 :      255144 :             const auto total_bytes_start{orphanage.TotalOrphanUsage()};
     104                 :      255144 :             const auto total_peer_bytes_start{orphanage.UsageByPeer(peer_id)};
     105                 :      255144 :             const auto tx_weight{GetTransactionWeight(*tx)};
     106                 :             : 
     107         [ +  - ]:      255144 :             CallOneOf(
     108                 :             :                 fuzzed_data_provider,
     109                 :        2881 :                 [&] {
     110                 :        2881 :                     {
     111                 :        2881 :                         CTransactionRef ref = orphanage.GetTxToReconsider(peer_id);
     112         [ +  + ]:        2881 :                         if (ref) {
     113   [ +  -  +  - ]:         302 :                             Assert(orphanage.HaveTx(ref->GetWitnessHash()));
     114                 :             :                         }
     115                 :        2881 :                     }
     116                 :        2881 :                 },
     117                 :      122065 :                 [&] {
     118                 :      122065 :                     bool have_tx = orphanage.HaveTx(tx->GetWitnessHash());
     119                 :             :                     // AddTx should return false if tx is too big or already have it
     120                 :             :                     // tx weight is unknown, we only check when tx is already in orphanage
     121                 :      122065 :                     {
     122                 :      122065 :                         bool add_tx = orphanage.AddTx(tx, peer_id);
     123                 :             :                         // have_tx == true -> add_tx == false
     124                 :      122065 :                         Assert(!have_tx || !add_tx);
     125                 :             : 
     126         [ +  + ]:      122065 :                         if (add_tx) {
     127                 :       26832 :                             Assert(orphanage.UsageByPeer(peer_id) == tx_weight + total_peer_bytes_start);
     128                 :       26832 :                             Assert(orphanage.TotalOrphanUsage() == tx_weight + total_bytes_start);
     129                 :       26832 :                             Assert(tx_weight <= MAX_STANDARD_TX_WEIGHT);
     130                 :             :                         } else {
     131                 :             :                             // Peer may have been added as an announcer.
     132         [ +  + ]:       95233 :                             if (orphanage.UsageByPeer(peer_id) == tx_weight + total_peer_bytes_start) {
     133                 :        2547 :                                 Assert(orphanage.HaveTxFromPeer(wtxid, peer_id));
     134                 :             :                             } else {
     135                 :             :                                 // Otherwise, there must not be any change to the peer byte count.
     136                 :       92686 :                                 Assert(orphanage.UsageByPeer(peer_id) == total_peer_bytes_start);
     137                 :             :                             }
     138                 :             : 
     139                 :             :                             // Regardless, total bytes should not have changed.
     140                 :       95233 :                             Assert(orphanage.TotalOrphanUsage() == total_bytes_start);
     141                 :             :                         }
     142                 :             :                     }
     143                 :      122065 :                     have_tx = orphanage.HaveTx(tx->GetWitnessHash());
     144                 :      122065 :                     {
     145                 :      122065 :                         bool add_tx = orphanage.AddTx(tx, peer_id);
     146                 :             :                         // if have_tx is still false, it must be too big
     147                 :      122065 :                         Assert(!have_tx == (tx_weight > MAX_STANDARD_TX_WEIGHT));
     148                 :      122065 :                         Assert(!have_tx || !add_tx);
     149                 :             :                     }
     150                 :      122065 :                 },
     151                 :        4563 :                 [&] {
     152                 :        4563 :                     bool have_tx = orphanage.HaveTx(tx->GetWitnessHash());
     153                 :        4563 :                     bool have_tx_and_peer = orphanage.HaveTxFromPeer(tx->GetWitnessHash(), peer_id);
     154                 :             :                     // AddAnnouncer should return false if tx doesn't exist or we already HaveTxFromPeer.
     155                 :        4563 :                     {
     156                 :        4563 :                         bool added_announcer = orphanage.AddAnnouncer(tx->GetWitnessHash(), peer_id);
     157                 :             :                         // have_tx == false -> added_announcer == false
     158                 :        4563 :                         Assert(have_tx || !added_announcer);
     159                 :             :                         // have_tx_and_peer == true -> added_announcer == false
     160                 :        4563 :                         Assert(!have_tx_and_peer || !added_announcer);
     161                 :             : 
     162                 :             :                         // Total bytes should not have changed. If peer was added as announcer, byte
     163                 :             :                         // accounting must have been updated.
     164                 :        4563 :                         Assert(orphanage.TotalOrphanUsage() == total_bytes_start);
     165         [ +  + ]:        4563 :                         if (added_announcer) {
     166                 :         708 :                             Assert(orphanage.UsageByPeer(peer_id) == tx_weight + total_peer_bytes_start);
     167                 :             :                         } else {
     168                 :        3855 :                             Assert(orphanage.UsageByPeer(peer_id) == total_peer_bytes_start);
     169                 :             :                         }
     170                 :             :                     }
     171                 :        4563 :                 },
     172                 :       32068 :                 [&] {
     173                 :       32068 :                     bool have_tx = orphanage.HaveTx(tx->GetWitnessHash());
     174                 :       32068 :                     bool have_tx_and_peer{orphanage.HaveTxFromPeer(wtxid, peer_id)};
     175                 :             :                     // EraseTx should return 0 if m_orphans doesn't have the tx
     176                 :       32068 :                     {
     177                 :       32068 :                         auto bytes_from_peer_before{orphanage.UsageByPeer(peer_id)};
     178                 :       32068 :                         Assert(have_tx == orphanage.EraseTx(tx->GetWitnessHash()));
     179         [ +  + ]:       32068 :                         if (have_tx) {
     180                 :       24328 :                             Assert(orphanage.TotalOrphanUsage() == total_bytes_start - tx_weight);
     181         [ +  + ]:       24328 :                             if (have_tx_and_peer) {
     182                 :           7 :                                 Assert(orphanage.UsageByPeer(peer_id) == bytes_from_peer_before - tx_weight);
     183                 :             :                             } else {
     184                 :       24321 :                                 Assert(orphanage.UsageByPeer(peer_id) == bytes_from_peer_before);
     185                 :             :                             }
     186                 :             :                         } else {
     187                 :        7740 :                             Assert(orphanage.TotalOrphanUsage() == total_bytes_start);
     188                 :             :                         }
     189                 :             :                     }
     190                 :       32068 :                     have_tx = orphanage.HaveTx(tx->GetWitnessHash());
     191                 :       32068 :                     have_tx_and_peer = orphanage.HaveTxFromPeer(wtxid, peer_id);
     192                 :             :                     // have_tx should be false and EraseTx should fail
     193                 :       32068 :                     {
     194   [ +  -  -  + ]:       32068 :                         Assert(!have_tx && !have_tx_and_peer && !orphanage.EraseTx(wtxid));
     195                 :             :                     }
     196                 :       32068 :                 },
     197                 :        2089 :                 [&] {
     198                 :        2089 :                     orphanage.EraseForPeer(peer_id);
     199                 :        2089 :                     Assert(!orphanage.HaveTxFromPeer(tx->GetWitnessHash(), peer_id));
     200                 :        2089 :                     Assert(orphanage.UsageByPeer(peer_id) == 0);
     201                 :        2089 :                 },
     202                 :       88863 :                 [&] {
     203                 :             :                     // Make a block out of txs and then EraseForBlock
     204                 :       88863 :                     CBlock block;
     205                 :       88863 :                     int num_txs = fuzzed_data_provider.ConsumeIntegralInRange<unsigned int>(0, 1000);
     206         [ +  + ]:     7141345 :                     for (int i{0}; i < num_txs; ++i) {
     207                 :     7052482 :                         auto& tx_to_remove = PickValue(fuzzed_data_provider, tx_history);
     208         [ +  - ]:     7052482 :                         block.vtx.push_back(tx_to_remove);
     209                 :             :                     }
     210         [ +  - ]:       88863 :                     orphanage.EraseForBlock(block);
     211         [ +  + ]:     7141345 :                     for (const auto& tx_removed : block.vtx) {
     212   [ +  -  +  - ]:     7052482 :                         Assert(!orphanage.HaveTx(tx_removed->GetWitnessHash()));
     213   [ +  -  +  - ]:     7052482 :                         Assert(!orphanage.HaveTxFromPeer(tx_removed->GetWitnessHash(), peer_id));
     214                 :             :                     }
     215                 :       88863 :                 },
     216                 :        2615 :                 [&] {
     217                 :             :                     // test mocktime and expiry
     218                 :        2615 :                     SetMockTime(ConsumeTime(fuzzed_data_provider));
     219                 :        2615 :                     auto limit = fuzzed_data_provider.ConsumeIntegral<unsigned int>();
     220                 :        2615 :                     orphanage.LimitOrphans(limit, orphanage_rng);
     221                 :        2615 :                     Assert(orphanage.Size() <= limit);
     222                 :        2615 :                 });
     223                 :             : 
     224                 :             :         }
     225                 :             : 
     226                 :             :         // Set tx as potential parent to be used for future GetChildren() calls.
     227   [ +  +  +  + ]:        9671 :         if (!ptx_potential_parent || fuzzed_data_provider.ConsumeBool()) {
     228                 :        2369 :             ptx_potential_parent = tx;
     229                 :             :         }
     230                 :             : 
     231         [ +  - ]:        5159 :         const bool have_tx{orphanage.HaveTx(tx->GetWitnessHash())};
     232   [ +  -  +  + ]:        5159 :         const bool get_tx_nonnull{orphanage.GetTx(tx->GetWitnessHash()) != nullptr};
     233         [ +  - ]:        5159 :         Assert(have_tx == get_tx_nonnull);
     234                 :        5159 :     }
     235         [ +  - ]:         657 :     orphanage.SanityCheck();
     236         [ +  + ]:        1304 : }
        

Generated by: LCOV version 2.0-1