LCOV - code coverage report
Current view: top level - src/test/fuzz - utxo_total_supply.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 98.6 % 73 72
Test Date: 2024-09-01 05:20:30 Functions: 100.0 % 10 10
Branches: 57.8 % 90 52

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2020 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 <chainparams.h>
       6                 :             : #include <consensus/consensus.h>
       7                 :             : #include <consensus/merkle.h>
       8                 :             : #include <kernel/coinstats.h>
       9                 :             : #include <node/miner.h>
      10                 :             : #include <script/interpreter.h>
      11                 :             : #include <streams.h>
      12                 :             : #include <test/fuzz/FuzzedDataProvider.h>
      13                 :             : #include <test/fuzz/fuzz.h>
      14                 :             : #include <test/fuzz/util.h>
      15                 :             : #include <test/util/mining.h>
      16                 :             : #include <test/util/setup_common.h>
      17                 :             : #include <util/chaintype.h>
      18                 :             : #include <validation.h>
      19                 :             : 
      20   [ +  -  +  - ]:        1829 : FUZZ_TARGET(utxo_total_supply)
      21                 :             : {
      22                 :             :     /** The testing setup that creates a chainman only (no chainstate) */
      23   [ +  -  +  - ]:        1826 :     ChainTestingSetup test_setup{
      24                 :             :         ChainType::REGTEST,
      25                 :        1826 :         {
      26         [ +  - ]:        1826 :             .extra_args = {"-testactivationheight=bip34@2"},
      27                 :             :         },
      28                 :             :     };
      29                 :             :     // Create chainstate
      30                 :             :     test_setup.LoadVerifyActivateChainstate();
      31                 :             :     auto& node{test_setup.m_node};
      32                 :             :     auto& chainman{*Assert(test_setup.m_node.chainman)};
      33                 :             :     FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
      34                 :             : 
      35                 :      250588 :     const auto ActiveHeight = [&]() {
      36                 :      250588 :         LOCK(chainman.GetMutex());
      37         [ +  - ]:      250588 :         return chainman.ActiveHeight();
      38                 :      250588 :     };
      39                 :      170746 :     const auto PrepareNextBlock = [&]() {
      40                 :             :         // Use OP_FALSE to avoid BIP30 check from hitting early
      41   [ +  -  +  - ]:      170746 :         auto block = PrepareBlock(node, CScript{} << OP_FALSE);
      42                 :             :         // Replace OP_FALSE with OP_TRUE
      43                 :             :         {
      44         [ +  - ]:      170746 :             CMutableTransaction tx{*block->vtx.back()};
      45   [ +  -  +  -  :      170746 :             tx.vout.at(0).scriptPubKey = CScript{} << OP_TRUE;
                   +  - ]
      46         [ +  - ]:      170746 :             block->vtx.back() = MakeTransactionRef(tx);
      47                 :      170746 :         }
      48                 :      170746 :         return block;
      49         [ +  - ]:      170746 :     };
      50                 :             : 
      51                 :             :     /** The block template this fuzzer is working on */
      52                 :             :     auto current_block = PrepareNextBlock();
      53                 :             :     /** Append-only set of tx outpoints, entries are not removed when spent */
      54                 :             :     std::vector<std::pair<COutPoint, CTxOut>> txos;
      55                 :             :     /** The utxo stats at the chain tip */
      56                 :             :     kernel::CCoinsStats utxo_stats;
      57                 :             :     /** The total amount of coins in the utxo set */
      58                 :             :     CAmount circulation{0};
      59                 :             : 
      60                 :             : 
      61                 :             :     // Store the tx out in the txo map
      62                 :      223043 :     const auto StoreLastTxo = [&]() {
      63                 :             :         // get last tx
      64                 :      223043 :         const CTransaction& tx = *current_block->vtx.back();
      65                 :             :         // get last out
      66                 :      223043 :         const uint32_t i = tx.vout.size() - 1;
      67                 :             :         // store it
      68                 :      223043 :         txos.emplace_back(COutPoint{tx.GetHash(), i}, tx.vout.at(i));
      69   [ +  +  +  + ]:      223043 :         if (current_block->vtx.size() == 1 && tx.vout.at(i).scriptPubKey[0] == OP_RETURN) {
      70                 :             :             // also store coinbase
      71                 :      178941 :             const uint32_t i = tx.vout.size() - 2;
      72                 :      178941 :             txos.emplace_back(COutPoint{tx.GetHash(), i}, tx.vout.at(i));
      73                 :      178941 :         }
      74                 :      223043 :     };
      75                 :       52297 :     const auto AppendRandomTxo = [&](CMutableTransaction& tx) {
      76                 :       52297 :         const auto& txo = txos.at(fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, txos.size() - 1));
      77                 :       52297 :         tx.vin.emplace_back(txo.first);
      78                 :       52297 :         tx.vout.emplace_back(txo.second.nValue, txo.second.scriptPubKey); // "Forward" coin with no fee
      79                 :       52297 :     };
      80                 :      168920 :     const auto UpdateUtxoStats = [&]() {
      81                 :      168920 :         LOCK(chainman.GetMutex());
      82   [ +  -  +  - ]:      168920 :         chainman.ActiveChainstate().ForceFlushStateToDisk();
      83                 :      168920 :         utxo_stats = std::move(
      84   [ +  -  +  -  :      168920 :             *Assert(kernel::ComputeUTXOStats(kernel::CoinStatsHashType::NONE, &chainman.ActiveChainstate().CoinsDB(), chainman.m_blockman, {})));
             +  -  +  - ]
      85                 :             :         // Check that miner can't print more money than they are allowed to
      86   [ +  -  +  - ]:      168920 :         assert(circulation == utxo_stats.total_amount);
      87                 :      168920 :     };
      88                 :             : 
      89                 :             : 
      90                 :             :     // Update internal state to chain tip
      91                 :             :     StoreLastTxo();
      92                 :             :     UpdateUtxoStats();
      93                 :             :     assert(ActiveHeight() == 0);
      94                 :             :     // Get at which height we duplicate the coinbase
      95                 :             :     // Assuming that the fuzzer will mine relatively short chains (less than 200 blocks), we want the duplicate coinbase to be not too high.
      96                 :             :     // Up to 300 seems reasonable.
      97                 :             :     int64_t duplicate_coinbase_height = fuzzed_data_provider.ConsumeIntegralInRange(0, 300);
      98                 :             :     // Always pad with OP_0 at the end to avoid bad-cb-length error
      99                 :             :     const CScript duplicate_coinbase_script = CScript() << duplicate_coinbase_height << OP_0;
     100                 :             :     // Mine the first block with this duplicate
     101                 :             :     current_block = PrepareNextBlock();
     102                 :             :     StoreLastTxo();
     103                 :             : 
     104                 :             :     {
     105                 :             :         // Create duplicate (CScript should match exact format as in CreateNewBlock)
     106         [ +  - ]:        1826 :         CMutableTransaction tx{*current_block->vtx.front()};
     107   [ +  -  +  - ]:        1826 :         tx.vin.at(0).scriptSig = duplicate_coinbase_script;
     108                 :             : 
     109                 :             :         // Mine block and create next block template
     110   [ +  -  +  - ]:        1826 :         current_block->vtx.front() = MakeTransactionRef(tx);
     111                 :             :     }
     112                 :             :     current_block->hashMerkleRoot = BlockMerkleRoot(*current_block);
     113                 :             :     assert(!MineBlock(node, current_block).IsNull());
     114                 :             :     circulation += GetBlockSubsidy(ActiveHeight(), Params().GetConsensus());
     115                 :             : 
     116                 :             :     assert(ActiveHeight() == 1);
     117                 :             :     UpdateUtxoStats();
     118                 :             :     current_block = PrepareNextBlock();
     119                 :             :     StoreLastTxo();
     120                 :             : 
     121                 :             :     // Limit to avoid timeout, but enough to cover duplicate_coinbase_height
     122                 :             :     // and CVE-2018-17144.
     123   [ +  -  +  +  :      219391 :     LIMITED_WHILE(fuzzed_data_provider.remaining_bytes(), 2'00)
                   +  + ]
     124                 :             :     {
     125         [ +  - ]:      217565 :         CallOneOf(
     126                 :             :             fuzzed_data_provider,
     127                 :      242097 :             [&] {
     128                 :             :                 // Append an input-output pair to the last tx in the current block
     129                 :       24532 :                 CMutableTransaction tx{*current_block->vtx.back()};
     130         [ +  - ]:       24532 :                 AppendRandomTxo(tx);
     131         [ +  - ]:       24532 :                 current_block->vtx.back() = MakeTransactionRef(tx);
     132         [ +  - ]:       24532 :                 StoreLastTxo();
     133                 :       24532 :             },
     134                 :      245330 :             [&] {
     135                 :             :                 // Append a tx to the list of txs in the current block
     136                 :       27765 :                 CMutableTransaction tx{};
     137         [ +  - ]:       27765 :                 AppendRandomTxo(tx);
     138   [ +  -  +  - ]:       27765 :                 current_block->vtx.push_back(MakeTransactionRef(tx));
     139         [ +  - ]:       27765 :                 StoreLastTxo();
     140                 :       27765 :             },
     141                 :      382833 :             [&] {
     142                 :             :                 // Append the current block to the active chain
     143                 :      165268 :                 node::RegenerateCommitments(*current_block, chainman);
     144                 :      165268 :                 const bool was_valid = !MineBlock(node, current_block).IsNull();
     145                 :             : 
     146                 :      165268 :                 const auto prev_utxo_stats = utxo_stats;
     147         [ +  + ]:      165268 :                 if (was_valid) {
     148         [ +  + ]:      122555 :                     if (duplicate_coinbase_height == ActiveHeight()) {
     149                 :             :                         // we mined the duplicate coinbase
     150         [ +  - ]:          25 :                         assert(current_block->vtx.at(0)->vin.at(0).scriptSig == duplicate_coinbase_script);
     151                 :          25 :                     }
     152                 :             : 
     153                 :      122555 :                     circulation += GetBlockSubsidy(ActiveHeight(), Params().GetConsensus());
     154                 :      122555 :                 }
     155                 :             : 
     156                 :      165268 :                 UpdateUtxoStats();
     157                 :             : 
     158         [ +  + ]:      165268 :                 if (!was_valid) {
     159                 :             :                     // utxo stats must not change
     160         [ +  - ]:       42713 :                     assert(prev_utxo_stats.hashSerialized == utxo_stats.hashSerialized);
     161                 :       42713 :                 }
     162                 :             : 
     163                 :      165268 :                 current_block = PrepareNextBlock();
     164                 :      165268 :                 StoreLastTxo();
     165                 :      165268 :             });
     166                 :      217565 :     }
     167                 :           0 : }
        

Generated by: LCOV version 2.0-1