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: 2025-05-10 04:08:03 Functions: 80.0 % 10 8
Branches: 49.2 % 132 65

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

Generated by: LCOV version 2.0-1