LCOV - code coverage report
Current view: top level - src/test/util - mining.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 21.2 % 99 21
Test Date: 2026-10-03 06:57:10 Functions: 10.0 % 10 1
Branches: 14.6 % 178 26

             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 <key_io.h>
      13                 :             : #include <node/block_template_manager.h>
      14                 :             : #include <node/context.h>
      15                 :             : #include <node/miner.h>
      16                 :             : #include <pow.h>
      17                 :             : #include <primitives/block.h>
      18                 :             : #include <primitives/transaction.h>
      19                 :             : #include <script/script.h>
      20                 :             : #include <sync.h>
      21                 :             : #include <test/util/script.h>
      22                 :             : #include <uint256.h>
      23                 :             : #include <util/check.h>
      24                 :             : #include <validation.h>
      25                 :             : #include <validationinterface.h>
      26                 :             : #include <versionbits.h>
      27                 :             : 
      28                 :             : #include <cstdint>
      29                 :             : #include <memory>
      30                 :             : #include <optional>
      31                 :             : #include <utility>
      32                 :             : 
      33                 :             : using node::NodeContext;
      34                 :             : 
      35                 :           0 : COutPoint generatetoaddress(const NodeContext& node, const std::string& address)
      36                 :             : {
      37                 :           0 :     const auto dest = DecodeDestination(address);
      38   [ #  #  #  # ]:           0 :     assert(IsValidDestination(dest));
      39         [ #  # ]:           0 :     return MineBlock(node, {
      40                 :             :         .coinbase_output_script = GetScriptForDestination(dest),
      41                 :           0 :     });
      42         [ #  # ]:           0 : }
      43                 :             : 
      44                 :           0 : std::vector<std::shared_ptr<CBlock>> CreateBlockChain(size_t total_height, const CChainParams& params)
      45                 :             : {
      46                 :           0 :     std::vector<std::shared_ptr<CBlock>> ret{total_height};
      47                 :           0 :     auto time{params.GenesisBlock().nTime};
      48                 :             :     // NOTE: here `height` does not correspond to the block height but the block height - 1.
      49         [ #  # ]:           0 :     for (size_t height{0}; height < total_height; ++height) {
      50   [ #  #  #  # ]:           0 :         CBlock& block{*(ret.at(height) = std::make_shared<CBlock>())};
      51                 :             : 
      52         [ #  # ]:           0 :         CMutableTransaction coinbase_tx;
      53                 :           0 :         coinbase_tx.nLockTime = static_cast<uint32_t>(height);
      54         [ #  # ]:           0 :         coinbase_tx.vin.resize(1);
      55                 :           0 :         coinbase_tx.vin[0].prevout.SetNull();
      56         [ #  # ]:           0 :         coinbase_tx.vin[0].nSequence = CTxIn::MAX_SEQUENCE_NONFINAL; // Make sure timelock is enforced.
      57         [ #  # ]:           0 :         coinbase_tx.vout.resize(1);
      58                 :           0 :         coinbase_tx.vout[0].scriptPubKey = P2WSH_OP_TRUE;
      59   [ #  #  #  # ]:           0 :         coinbase_tx.vout[0].nValue = GetBlockSubsidy(height + 1, params.GetConsensus());
      60                 :             :         // Always include OP_0 as a dummy extraNonce.
      61   [ #  #  #  # ]:           0 :         coinbase_tx.vin[0].scriptSig = CScript() << (height + 1) << OP_0;
      62   [ #  #  #  #  :           0 :         block.vtx = {MakeTransactionRef(std::move(coinbase_tx))};
             #  #  #  # ]
      63                 :             : 
      64                 :           0 :         block.nVersion = VERSIONBITS_LAST_OLD_BLOCK_VERSION;
      65   [ #  #  #  #  :           0 :         block.hashPrevBlock = (height >= 1 ? *ret.at(height - 1) : params.GenesisBlock()).GetHash();
                   #  # ]
      66         [ #  # ]:           0 :         block.hashMerkleRoot = BlockMerkleRoot(block);
      67                 :           0 :         block.nTime = ++time;
      68                 :           0 :         block.nBits = params.GenesisBlock().nBits;
      69                 :           0 :         block.nNonce = 0;
      70                 :             : 
      71   [ #  #  #  #  :           0 :         while (!CheckProofOfWork(block.GetHash(), block.nBits, params.GetConsensus())) {
                   #  # ]
      72                 :           0 :             ++block.nNonce;
      73         [ #  # ]:           0 :             assert(block.nNonce);
      74                 :             :         }
      75                 :           0 :     }
      76                 :           0 :     return ret;
      77   [ #  #  #  # ]:           0 : }
      78                 :             : 
      79                 :           3 : bool BuildChain(const NodeContext& node, const CBlockIndex* pindex,
      80                 :             :     const CScript& coinbase_script_pub_key,
      81                 :             :     size_t length,
      82                 :             :     std::vector<std::shared_ptr<CBlock>>& chain)
      83                 :             : {
      84   [ -  +  -  + ]:           3 :     auto& block_template_manager{*Assert(node.block_template_manager)};
      85         [ -  + ]:           3 :     const Consensus::Params& consensus{Assert(node.chainman)->GetConsensus()};
      86                 :             : 
      87                 :           3 :     chain.resize(length);
      88         [ +  + ]:          26 :     for (auto& chain_block : chain) {
      89                 :          23 :         auto block_template{block_template_manager.CreateNewTemplate({
      90                 :             :             .use_mempool = false,
      91                 :             :             .coinbase_output_script = coinbase_script_pub_key,
      92                 :           0 :         })};
      93   [ -  +  +  - ]:          23 :         CBlock block{Assert(block_template)->block};
      94                 :             : 
      95                 :             :         // The template is built on the active tip, so repoint it at pindex and
      96                 :             :         // redo the fields that depend on the predecessor.
      97                 :          23 :         block.hashPrevBlock = pindex->GetBlockHash();
      98                 :          23 :         block.nTime = pindex->nTime + 1;
      99                 :          23 :         {
     100   [ +  -  +  - ]:          23 :             CMutableTransaction tx_coinbase{*block.vtx.at(0)};
     101                 :          23 :             tx_coinbase.nLockTime = static_cast<uint32_t>(pindex->nHeight);
     102   [ +  -  +  - ]:          23 :             tx_coinbase.vin.at(0).scriptSig = CScript{} << pindex->nHeight + 1;
     103   [ +  -  +  -  :          46 :             block.vtx.at(0) = MakeTransactionRef(std::move(tx_coinbase));
                   -  + ]
     104         [ +  - ]:          23 :             block.hashMerkleRoot = BlockMerkleRoot(block);
     105                 :           0 :         }
     106                 :             : 
     107   [ +  -  +  -  :          51 :         while (!CheckProofOfWork(block.GetHash(), block.nBits, consensus)) ++block.nNonce;
                   +  + ]
     108                 :             : 
     109   [ +  -  -  + ]:          23 :         chain_block = std::make_shared<CBlock>(std::move(block));
     110                 :             : 
     111         [ -  + ]:          23 :         BlockValidationState state;
     112   [ -  +  +  -  :          23 :         if (!Assert(node.chainman)->ProcessNewBlockHeaders({{*chain_block}}, true, state, &pindex)) {
                   -  + ]
     113                 :           0 :             return false;
     114                 :             :         }
     115                 :          23 :     }
     116                 :             : 
     117                 :             :     return true;
     118         [ +  - ]:          23 : }
     119                 :             : 
     120                 :           0 : COutPoint MineBlock(const NodeContext& node, const node::BlockCreateOptions& assembler_options)
     121                 :             : {
     122                 :           0 :     auto block = PrepareBlock(node, assembler_options);
     123         [ #  # ]:           0 :     auto valid = MineBlock(node, block);
     124         [ #  # ]:           0 :     assert(!valid.IsNull());
     125         [ #  # ]:           0 :     return valid;
     126                 :           0 : }
     127                 :             : 
     128                 :             : struct BlockValidationStateCatcher : public CValidationInterface {
     129                 :             :     const uint256 m_hash;
     130                 :             :     std::optional<BlockValidationState> m_state;
     131                 :             : 
     132                 :           0 :     BlockValidationStateCatcher(const uint256& hash)
     133                 :           0 :         : m_hash{hash},
     134                 :           0 :           m_state{} {}
     135                 :             : 
     136                 :             : protected:
     137                 :           0 :     void BlockChecked(const std::shared_ptr<const CBlock>& block, const BlockValidationState& state) override
     138                 :             :     {
     139         [ #  # ]:           0 :         if (block->GetHash() != m_hash) return;
     140                 :           0 :         m_state = state;
     141                 :             :     }
     142                 :             : };
     143                 :             : 
     144                 :           0 : COutPoint MineBlock(const NodeContext& node, std::shared_ptr<CBlock>& block)
     145                 :             : {
     146         [ #  # ]:           0 :     while (!CheckProofOfWork(block->GetHash(), block->nBits, Params().GetConsensus())) {
     147         [ #  # ]:           0 :         ++block->nNonce;
     148         [ #  # ]:           0 :         assert(block->nNonce);
     149                 :             :     }
     150                 :             : 
     151                 :           0 :     return ProcessBlock(node, block);
     152                 :             : }
     153                 :             : 
     154                 :           0 : COutPoint ProcessBlock(const NodeContext& node, const std::shared_ptr<CBlock>& block)
     155                 :             : {
     156         [ #  # ]:           0 :     auto& chainman{*Assert(node.chainman)};
     157   [ #  #  #  # ]:           0 :     const auto old_height = WITH_LOCK(chainman.GetMutex(), return chainman.ActiveHeight());
     158                 :           0 :     bool new_block;
     159         [ #  # ]:           0 :     BlockValidationStateCatcher bvsc{block->GetHash()};
     160         [ #  # ]:           0 :     node.validation_signals->RegisterValidationInterface(&bvsc);
     161   [ #  #  #  #  :           0 :     const bool processed{chainman.ProcessNewBlock(block, true, true, &new_block)};
                   #  # ]
     162   [ #  #  #  # ]:           0 :     const bool duplicate{!new_block && processed};
     163                 :           0 :     assert(!duplicate);
     164         [ #  # ]:           0 :     node.validation_signals->UnregisterValidationInterface(&bvsc);
     165         [ #  # ]:           0 :     node.validation_signals->SyncWithValidationInterfaceQueue();
     166   [ #  #  #  # ]:           0 :     const bool was_valid{bvsc.m_state && bvsc.m_state->IsValid()};
     167   [ #  #  #  #  :           0 :     assert(old_height + was_valid == WITH_LOCK(chainman.GetMutex(), return chainman.ActiveHeight()));
             #  #  #  # ]
     168                 :             : 
     169         [ #  # ]:           0 :     if (was_valid) return {block->vtx[0]->GetHash(), 0};
     170                 :           0 :     return {};
     171                 :           0 : }
     172                 :             : 
     173                 :           0 : std::shared_ptr<CBlock> PrepareBlock(const NodeContext& node,
     174                 :             :                                      const node::BlockCreateOptions& assembler_options)
     175                 :             : {
     176         [ #  # ]:           0 :     auto& block_template_manager = *Assert(node.block_template_manager);
     177                 :           0 :     auto block_template = block_template_manager.CreateNewTemplate(assembler_options);
     178   [ #  #  #  # ]:           0 :     auto block = std::make_shared<CBlock>(Assert(block_template)->block);
     179                 :             : 
     180         [ #  # ]:           0 :     LOCK(cs_main);
     181   [ #  #  #  #  :           0 :     block->nTime = Assert(node.chainman)->ActiveChain().Tip()->GetMedianTimePast() + 1;
             #  #  #  # ]
     182         [ #  # ]:           0 :     block->hashMerkleRoot = BlockMerkleRoot(*block);
     183                 :             : 
     184         [ #  # ]:           0 :     return block;
     185                 :           0 : }
        

Generated by: LCOV version 2.0-1