LCOV - code coverage report
Current view: top level - src/test/util - mining.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 83.1 % 83 69
Test Date: 2026-05-19 07:27:20 Functions: 80.0 % 10 8
Branches: 49.3 % 138 68

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2019-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 <test/util/mining.h>
       6                 :             : 
       7                 :             : #include <chainparams.h>
       8                 :             : #include <consensus/merkle.h>
       9                 :             : #include <consensus/validation.h>
      10                 :             : #include <key_io.h>
      11                 :             : #include <node/context.h>
      12                 :             : #include <pow.h>
      13                 :             : #include <primitives/transaction.h>
      14                 :             : #include <test/util/script.h>
      15                 :             : #include <util/check.h>
      16                 :             : #include <validation.h>
      17                 :             : #include <validationinterface.h>
      18                 :             : #include <versionbits.h>
      19                 :             : 
      20                 :             : #include <algorithm>
      21                 :             : #include <memory>
      22                 :             : 
      23                 :             : using node::BlockAssembler;
      24                 :             : using node::NodeContext;
      25                 :             : 
      26                 :           0 : COutPoint generatetoaddress(const NodeContext& node, const std::string& address)
      27                 :             : {
      28                 :           0 :     const auto dest = DecodeDestination(address);
      29   [ #  #  #  # ]:           0 :     assert(IsValidDestination(dest));
      30         [ #  # ]:           0 :     BlockAssembler::Options assembler_options;
      31         [ #  # ]:           0 :     assembler_options.coinbase_output_script = GetScriptForDestination(dest);
      32                 :             : 
      33         [ #  # ]:           0 :     return MineBlock(node, assembler_options);
      34                 :           0 : }
      35                 :             : 
      36                 :           2 : std::vector<std::shared_ptr<CBlock>> CreateBlockChain(size_t total_height, const CChainParams& params)
      37                 :             : {
      38                 :           2 :     std::vector<std::shared_ptr<CBlock>> ret{total_height};
      39                 :           2 :     auto time{params.GenesisBlock().nTime};
      40                 :             :     // NOTE: here `height` does not correspond to the block height but the block height - 1.
      41         [ +  + ]:         402 :     for (size_t height{0}; height < total_height; ++height) {
      42   [ +  -  -  + ]:         800 :         CBlock& block{*(ret.at(height) = std::make_shared<CBlock>())};
      43                 :             : 
      44         [ +  - ]:         400 :         CMutableTransaction coinbase_tx;
      45                 :         400 :         coinbase_tx.nLockTime = static_cast<uint32_t>(height);
      46         [ +  - ]:         400 :         coinbase_tx.vin.resize(1);
      47                 :         400 :         coinbase_tx.vin[0].prevout.SetNull();
      48         [ +  - ]:         400 :         coinbase_tx.vin[0].nSequence = CTxIn::MAX_SEQUENCE_NONFINAL; // Make sure timelock is enforced.
      49         [ +  - ]:         400 :         coinbase_tx.vout.resize(1);
      50                 :         400 :         coinbase_tx.vout[0].scriptPubKey = P2WSH_OP_TRUE;
      51   [ +  -  +  - ]:         400 :         coinbase_tx.vout[0].nValue = GetBlockSubsidy(height + 1, params.GetConsensus());
      52                 :             :         // Always include OP_0 as a dummy extraNonce.
      53   [ +  -  +  - ]:         400 :         coinbase_tx.vin[0].scriptSig = CScript() << (height + 1) << OP_0;
      54   [ +  +  +  -  :         800 :         block.vtx = {MakeTransactionRef(std::move(coinbase_tx))};
             -  -  -  - ]
      55                 :             : 
      56                 :         400 :         block.nVersion = VERSIONBITS_LAST_OLD_BLOCK_VERSION;
      57   [ +  +  +  -  :         400 :         block.hashPrevBlock = (height >= 1 ? *ret.at(height - 1) : params.GenesisBlock()).GetHash();
                   +  - ]
      58         [ +  - ]:         400 :         block.hashMerkleRoot = BlockMerkleRoot(block);
      59                 :         400 :         block.nTime = ++time;
      60                 :         400 :         block.nBits = params.GenesisBlock().nBits;
      61                 :         400 :         block.nNonce = 0;
      62                 :             : 
      63   [ +  -  +  -  :         762 :         while (!CheckProofOfWork(block.GetHash(), block.nBits, params.GetConsensus())) {
                   +  + ]
      64                 :         362 :             ++block.nNonce;
      65         [ -  + ]:         362 :             assert(block.nNonce);
      66                 :             :         }
      67                 :         400 :     }
      68                 :           2 :     return ret;
      69   [ +  -  +  - ]:         800 : }
      70                 :             : 
      71                 :      231600 : COutPoint MineBlock(const NodeContext& node, const node::BlockAssembler::Options& assembler_options)
      72                 :             : {
      73                 :      231600 :     auto block = PrepareBlock(node, assembler_options);
      74         [ +  - ]:      231600 :     auto valid = MineBlock(node, block);
      75         [ -  + ]:      231600 :     assert(!valid.IsNull());
      76         [ +  - ]:      231600 :     return valid;
      77                 :      231600 : }
      78                 :             : 
      79                 :             : struct BlockValidationStateCatcher : public CValidationInterface {
      80                 :             :     const uint256 m_hash;
      81                 :             :     std::optional<BlockValidationState> m_state;
      82                 :             : 
      83                 :      323613 :     BlockValidationStateCatcher(const uint256& hash)
      84                 :      323613 :         : m_hash{hash},
      85                 :      323613 :           m_state{} {}
      86                 :             : 
      87                 :             : protected:
      88                 :      323613 :     void BlockChecked(const std::shared_ptr<const CBlock>& block, const BlockValidationState& state) override
      89                 :             :     {
      90         [ +  - ]:      323613 :         if (block->GetHash() != m_hash) return;
      91                 :      323613 :         m_state = state;
      92                 :             :     }
      93                 :             : };
      94                 :             : 
      95                 :      323213 : COutPoint MineBlock(const NodeContext& node, std::shared_ptr<CBlock>& block)
      96                 :             : {
      97         [ +  + ]:      607276 :     while (!CheckProofOfWork(block->GetHash(), block->nBits, Params().GetConsensus())) {
      98         [ -  + ]:      284063 :         ++block->nNonce;
      99         [ -  + ]:      284063 :         assert(block->nNonce);
     100                 :             :     }
     101                 :             : 
     102                 :      323213 :     return ProcessBlock(node, block);
     103                 :             : }
     104                 :             : 
     105                 :      323613 : COutPoint ProcessBlock(const NodeContext& node, const std::shared_ptr<CBlock>& block)
     106                 :             : {
     107         [ -  + ]:      323613 :     auto& chainman{*Assert(node.chainman)};
     108   [ +  -  +  - ]:      970839 :     const auto old_height = WITH_LOCK(chainman.GetMutex(), return chainman.ActiveHeight());
     109                 :      323613 :     bool new_block;
     110         [ +  - ]:      323613 :     BlockValidationStateCatcher bvsc{block->GetHash()};
     111         [ +  - ]:      323613 :     node.validation_signals->RegisterValidationInterface(&bvsc);
     112   [ +  -  +  -  :      647226 :     const bool processed{chainman.ProcessNewBlock(block, true, true, &new_block)};
                   +  - ]
     113   [ +  +  -  + ]:      323613 :     const bool duplicate{!new_block && processed};
     114                 :           0 :     assert(!duplicate);
     115         [ +  - ]:      323613 :     node.validation_signals->UnregisterValidationInterface(&bvsc);
     116         [ +  - ]:      323613 :     node.validation_signals->SyncWithValidationInterfaceQueue();
     117   [ +  -  +  + ]:      323613 :     const bool was_valid{bvsc.m_state && bvsc.m_state->IsValid()};
     118   [ +  -  -  +  :      970839 :     assert(old_height + was_valid == WITH_LOCK(chainman.GetMutex(), return chainman.ActiveHeight()));
             +  -  +  - ]
     119                 :             : 
     120         [ +  + ]:      323613 :     if (was_valid) return {block->vtx[0]->GetHash(), 0};
     121                 :       22862 :     return {};
     122                 :      323613 : }
     123                 :             : 
     124                 :      325505 : std::shared_ptr<CBlock> PrepareBlock(const NodeContext& node,
     125                 :             :                                      const BlockAssembler::Options& assembler_options)
     126                 :             : {
     127                 :      325505 :     auto block = std::make_shared<CBlock>(
     128   [ -  +  -  + ]:      325505 :         BlockAssembler{Assert(node.chainman)->ActiveChainstate(), Assert(node.mempool.get()), assembler_options}
     129         [ +  - ]:      651010 :             .CreateNewBlock()
     130         [ +  - ]:      325505 :             ->block);
     131                 :             : 
     132         [ +  - ]:      325505 :     LOCK(cs_main);
     133   [ -  +  +  -  :      651010 :     block->nTime = Assert(node.chainman)->ActiveChain().Tip()->GetMedianTimePast() + 1;
             -  +  +  - ]
     134         [ +  - ]:      325505 :     block->hashMerkleRoot = BlockMerkleRoot(*block);
     135                 :             : 
     136         [ +  - ]:      325505 :     return block;
     137                 :      325505 : }
     138                 :           0 : std::shared_ptr<CBlock> PrepareBlock(const NodeContext& node, const CScript& coinbase_scriptPubKey)
     139                 :             : {
     140                 :           0 :     BlockAssembler::Options assembler_options;
     141                 :           0 :     assembler_options.coinbase_output_script = coinbase_scriptPubKey;
     142         [ #  # ]:           0 :     ApplyArgsManOptions(*node.args, assembler_options);
     143         [ #  # ]:           0 :     return PrepareBlock(node, assembler_options);
     144                 :           0 : }
        

Generated by: LCOV version 2.0-1