LCOV - code coverage report
Current view: top level - src/node - miner.h Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 100.0 % 2 2
Test Date: 2026-02-25 05:21:33 Functions: - 0 0
Branches: 42.3 % 26 11

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2009-2010 Satoshi Nakamoto
       2                 :             : // Copyright (c) 2009-present The Bitcoin Core developers
       3                 :             : // Distributed under the MIT software license, see the accompanying
       4                 :             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       5                 :             : 
       6                 :             : #ifndef BITCOIN_NODE_MINER_H
       7                 :             : #define BITCOIN_NODE_MINER_H
       8                 :             : 
       9                 :             : #include <interfaces/types.h>
      10                 :             : #include <node/types.h>
      11                 :             : #include <policy/policy.h>
      12                 :             : #include <primitives/block.h>
      13                 :             : #include <txmempool.h>
      14                 :             : #include <util/feefrac.h>
      15                 :             : 
      16                 :             : #include <cstdint>
      17                 :             : #include <memory>
      18                 :             : #include <optional>
      19                 :             : 
      20                 :             : #include <boost/multi_index/identity.hpp>
      21                 :             : #include <boost/multi_index/indexed_by.hpp>
      22                 :             : #include <boost/multi_index/ordered_index.hpp>
      23                 :             : #include <boost/multi_index/tag.hpp>
      24                 :             : #include <boost/multi_index_container.hpp>
      25                 :             : 
      26                 :             : class ArgsManager;
      27                 :             : class CBlockIndex;
      28                 :             : class CChainParams;
      29                 :             : class CScript;
      30                 :             : class Chainstate;
      31                 :             : class ChainstateManager;
      32                 :             : 
      33                 :             : namespace Consensus { struct Params; };
      34                 :             : 
      35                 :             : using interfaces::BlockRef;
      36                 :             : 
      37                 :             : namespace node {
      38                 :             : class KernelNotifications;
      39                 :             : 
      40                 :             : static const bool DEFAULT_PRINT_MODIFIED_FEE = false;
      41                 :             : 
      42                 :             : struct CBlockTemplate
      43                 :             : {
      44                 :             :     CBlock block;
      45                 :             :     // Fees per transaction, not including coinbase transaction (unlike CBlock::vtx).
      46                 :             :     std::vector<CAmount> vTxFees;
      47                 :             :     // Sigops per transaction, not including coinbase transaction (unlike CBlock::vtx).
      48                 :             :     std::vector<int64_t> vTxSigOpsCost;
      49                 :             :     /* A vector of package fee rates, ordered by the sequence in which
      50                 :             :      * packages are selected for inclusion in the block template.*/
      51                 :             :     std::vector<FeePerVSize> m_package_feerates;
      52                 :             :     /*
      53                 :             :      * Template containing all coinbase transaction fields that are set by our
      54                 :             :      * miner code.
      55                 :             :      */
      56                 :             :     CoinbaseTx m_coinbase_tx;
      57                 :             : };
      58                 :             : 
      59                 :             : /** Generate a new block, without valid proof-of-work */
      60         [ +  + ]:        8103 : class BlockAssembler
           [ +  -  +  - ]
      61                 :             : {
      62                 :             : private:
      63                 :             :     // The constructed block template
      64                 :             :     std::unique_ptr<CBlockTemplate> pblocktemplate;
      65                 :             : 
      66                 :             :     // Information on the current status of the block
      67                 :             :     uint64_t nBlockWeight;
      68                 :             :     uint64_t nBlockTx;
      69                 :             :     uint64_t nBlockSigOpsCost;
      70                 :             :     CAmount nFees;
      71                 :             : 
      72                 :             :     // Chain context for the block
      73                 :             :     int nHeight;
      74                 :             :     int64_t m_lock_time_cutoff;
      75                 :             : 
      76                 :             :     const CChainParams& chainparams;
      77                 :             :     const CTxMemPool* const m_mempool;
      78                 :             :     Chainstate& m_chainstate;
      79                 :             : 
      80                 :             : public:
      81   [ +  -  +  - ]:       32278 :     struct Options : BlockCreateOptions {
           [ +  -  +  -  
             +  -  +  - ]
         [ +  - ][ #  #  
             #  #  #  # ]
      82                 :             :         // Configuration parameters for the block size
      83                 :             :         size_t nBlockMaxWeight{DEFAULT_BLOCK_MAX_WEIGHT};
      84                 :             :         CFeeRate blockMinFeeRate{DEFAULT_BLOCK_MIN_TX_FEE};
      85                 :             :         // Whether to call TestBlockValidity() at the end of CreateNewBlock().
      86                 :             :         bool test_block_validity{true};
      87                 :             :         bool print_modified_fee{DEFAULT_PRINT_MODIFIED_FEE};
      88                 :             :     };
      89                 :             : 
      90                 :             :     explicit BlockAssembler(Chainstate& chainstate, const CTxMemPool* mempool, const Options& options);
      91                 :             : 
      92                 :             :     /** Construct a new block template */
      93                 :             :     std::unique_ptr<CBlockTemplate> CreateNewBlock();
      94                 :             : 
      95                 :             :     /** The number of transactions in the last assembled block (excluding coinbase transaction) */
      96                 :             :     inline static std::optional<int64_t> m_last_block_num_txs{};
      97                 :             :     /** The weight of the last assembled block (including reserved weight for block header, txs count and coinbase tx) */
      98                 :             :     inline static std::optional<int64_t> m_last_block_weight{};
      99                 :             : 
     100                 :             : private:
     101                 :             :     const Options m_options;
     102                 :             : 
     103                 :             :     // utility functions
     104                 :             :     /** Clear the block's state and prepare for assembling a new block */
     105                 :             :     void resetBlock();
     106                 :             :     /** Add a tx to the block */
     107                 :             :     void AddToBlock(const CTxMemPoolEntry& entry);
     108                 :             : 
     109                 :             :     // Methods for how to add transactions to a block.
     110                 :             :     /** Add transactions based on chunk feerate
     111                 :             :       *
     112                 :             :       * @pre BlockAssembler::m_mempool must not be nullptr
     113                 :             :     */
     114                 :             :     void addChunks() EXCLUSIVE_LOCKS_REQUIRED(m_mempool->cs);
     115                 :             : 
     116                 :             :     // helper functions for addChunks()
     117                 :             :     /** Test if a new chunk would "fit" in the block */
     118                 :             :     bool TestChunkBlockLimits(FeePerWeight chunk_feerate, int64_t chunk_sigops_cost) const;
     119                 :             :     /** Perform locktime checks on each transaction in a chunk:
     120                 :             :       * This check should always succeed, and is here
     121                 :             :       * only as an extra check in case of a bug */
     122                 :             :     bool TestChunkTransactions(const std::vector<CTxMemPoolEntryRef>& txs) const;
     123                 :             : };
     124                 :             : 
     125                 :             : /**
     126                 :             :  * Get the minimum time a miner should use in the next block. This always
     127                 :             :  * accounts for the BIP94 timewarp rule, so does not necessarily reflect the
     128                 :             :  * consensus limit.
     129                 :             :  */
     130                 :             : int64_t GetMinimumTime(const CBlockIndex* pindexPrev, int64_t difficulty_adjustment_interval);
     131                 :             : 
     132                 :             : int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev);
     133                 :             : 
     134                 :             : /** Update an old GenerateCoinbaseCommitment from CreateNewBlock after the block txs have changed */
     135                 :             : void RegenerateCommitments(CBlock& block, ChainstateManager& chainman);
     136                 :             : 
     137                 :             : /** Apply -blockmintxfee and -blockmaxweight options from ArgsManager to BlockAssembler options. */
     138                 :             : void ApplyArgsManOptions(const ArgsManager& gArgs, BlockAssembler::Options& options);
     139                 :             : 
     140                 :             : /* Compute the block's merkle root, insert or replace the coinbase transaction and the merkle root into the block */
     141                 :             : void AddMerkleRootAndCoinbase(CBlock& block, CTransactionRef coinbase, uint32_t version, uint32_t timestamp, uint32_t nonce);
     142                 :             : 
     143                 :             : 
     144                 :             : /* Interrupt a blocking call. */
     145                 :             : void InterruptWait(KernelNotifications& kernel_notifications, bool& interrupt_wait);
     146                 :             : /**
     147                 :             :  * Return a new block template when fees rise to a certain threshold or after a
     148                 :             :  * new tip; return nullopt if timeout is reached.
     149                 :             :  */
     150                 :             : std::unique_ptr<CBlockTemplate> WaitAndCreateNewBlock(ChainstateManager& chainman,
     151                 :             :                                                       KernelNotifications& kernel_notifications,
     152                 :             :                                                       CTxMemPool* mempool,
     153                 :             :                                                       const std::unique_ptr<CBlockTemplate>& block_template,
     154                 :             :                                                       const BlockWaitOptions& options,
     155                 :             :                                                       const BlockAssembler::Options& assemble_options,
     156                 :             :                                                       bool& interrupt_wait);
     157                 :             : 
     158                 :             : /* Locks cs_main and returns the block hash and block height of the active chain if it exists; otherwise, returns nullopt.*/
     159                 :             : std::optional<BlockRef> GetTip(ChainstateManager& chainman);
     160                 :             : 
     161                 :             : /* Waits for the connected tip to change until timeout has elapsed. During node initialization, this will wait until the tip is connected (regardless of `timeout`).
     162                 :             :  * Returns the current tip, or nullopt if the node is shutting down or interrupt()
     163                 :             :  * is called.
     164                 :             :  */
     165                 :             : std::optional<BlockRef> WaitTipChanged(ChainstateManager& chainman, KernelNotifications& kernel_notifications, const uint256& current_tip, MillisecondsDouble& timeout, bool& interrupt);
     166                 :             : 
     167                 :             : /**
     168                 :             :  * Wait while the best known header extends the current chain tip AND at least
     169                 :             :  * one block is being added to the tip every 3 seconds. If the tip is
     170                 :             :  * sufficiently far behind, allow up to 20 seconds for the next tip update.
     171                 :             :  *
     172                 :             :  * It’s not safe to keep waiting, because a malicious miner could announce a
     173                 :             :  * header and delay revealing the block, causing all other miners using this
     174                 :             :  * software to stall. At the same time, we need to balance between the default
     175                 :             :  * waiting time being brief, but not ending the cooldown prematurely when a
     176                 :             :  * random block is slow to download (or process).
     177                 :             :  *
     178                 :             :  * The cooldown only applies to createNewBlock(), which is typically called
     179                 :             :  * once per connected client. Subsequent templates are provided by waitNext().
     180                 :             :  *
     181                 :             :  * @param last_tip tip at the start of the cooldown window.
     182                 :             :  * @param interrupt_mining set to true to interrupt the cooldown.
     183                 :             :  *
     184                 :             :  * @returns false if interrupted.
     185                 :             :  */
     186                 :             : bool CooldownIfHeadersAhead(ChainstateManager& chainman, KernelNotifications& kernel_notifications, const BlockRef& last_tip, bool& interrupt_mining);
     187                 :             : } // namespace node
     188                 :             : 
     189                 :             : #endif // BITCOIN_NODE_MINER_H
        

Generated by: LCOV version 2.0-1