LCOV - code coverage report
Current view: top level - src/test/util - mining.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 90.7 % 75 68
Test Date: 2026-06-09 06:53:21 Functions: 88.9 % 9 8
Branches: 51.5 % 130 67

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

Generated by: LCOV version 2.0-1