LCOV - code coverage report
Current view: top level - src/node - miner.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 93.2 % 220 205
Test Date: 2025-11-28 04:30:17 Functions: 95.2 % 21 20
Branches: 55.4 % 278 154

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2009-2010 Satoshi Nakamoto
       2                 :             : // Copyright (c) 2009-2022 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                 :             : #include <node/miner.h>
       7                 :             : 
       8                 :             : #include <chain.h>
       9                 :             : #include <chainparams.h>
      10                 :             : #include <coins.h>
      11                 :             : #include <common/args.h>
      12                 :             : #include <consensus/amount.h>
      13                 :             : #include <consensus/consensus.h>
      14                 :             : #include <consensus/merkle.h>
      15                 :             : #include <consensus/tx_verify.h>
      16                 :             : #include <consensus/validation.h>
      17                 :             : #include <deploymentstatus.h>
      18                 :             : #include <logging.h>
      19                 :             : #include <node/context.h>
      20                 :             : #include <node/kernel_notifications.h>
      21                 :             : #include <policy/feerate.h>
      22                 :             : #include <policy/policy.h>
      23                 :             : #include <pow.h>
      24                 :             : #include <primitives/transaction.h>
      25                 :             : #include <util/moneystr.h>
      26                 :             : #include <util/signalinterrupt.h>
      27                 :             : #include <util/time.h>
      28                 :             : #include <validation.h>
      29                 :             : 
      30                 :             : #include <algorithm>
      31                 :             : #include <utility>
      32                 :             : #include <numeric>
      33                 :             : 
      34                 :             : namespace node {
      35                 :             : 
      36                 :        7328 : int64_t GetMinimumTime(const CBlockIndex* pindexPrev, const int64_t difficulty_adjustment_interval)
      37                 :             : {
      38                 :        7328 :     int64_t min_time{pindexPrev->GetMedianTimePast() + 1};
      39                 :             :     // Height of block to be mined.
      40                 :        7328 :     const int height{pindexPrev->nHeight + 1};
      41                 :             :     // Account for BIP94 timewarp rule on all networks. This makes future
      42                 :             :     // activation safer.
      43         [ +  + ]:        7328 :     if (height % difficulty_adjustment_interval == 0) {
      44         [ +  - ]:          12 :         min_time = std::max<int64_t>(min_time, pindexPrev->GetBlockTime() - MAX_TIMEWARP);
      45                 :             :     }
      46                 :        7328 :     return min_time;
      47                 :             : }
      48                 :             : 
      49                 :        7328 : int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev)
      50                 :             : {
      51                 :        7328 :     int64_t nOldTime = pblock->nTime;
      52         [ +  + ]:        7328 :     int64_t nNewTime{std::max<int64_t>(GetMinimumTime(pindexPrev, consensusParams.DifficultyAdjustmentInterval()),
      53                 :        7328 :                                        TicksSinceEpoch<std::chrono::seconds>(NodeClock::now()))};
      54                 :             : 
      55         [ +  + ]:        7328 :     if (nOldTime < nNewTime) {
      56                 :          18 :         pblock->nTime = nNewTime;
      57                 :             :     }
      58                 :             : 
      59                 :             :     // Updating time can change work required on testnet:
      60         [ +  + ]:        7328 :     if (consensusParams.fPowAllowMinDifficultyBlocks) {
      61                 :        7193 :         pblock->nBits = GetNextWorkRequired(pindexPrev, pblock, consensusParams);
      62                 :             :     }
      63                 :             : 
      64                 :        7328 :     return nNewTime - nOldTime;
      65                 :             : }
      66                 :             : 
      67                 :        6310 : void RegenerateCommitments(CBlock& block, ChainstateManager& chainman)
      68                 :             : {
      69                 :        6310 :     CMutableTransaction tx{*block.vtx.at(0)};
      70                 :        6310 :     tx.vout.erase(tx.vout.begin() + GetWitnessCommitmentIndex(block));
      71   [ +  -  +  -  :       12620 :     block.vtx.at(0) = MakeTransactionRef(tx);
                   -  + ]
      72                 :             : 
      73   [ +  -  +  -  :       18930 :     const CBlockIndex* prev_block = WITH_LOCK(::cs_main, return chainman.m_blockman.LookupBlockIndex(block.hashPrevBlock));
                   +  - ]
      74         [ +  - ]:        6310 :     chainman.GenerateCoinbaseCommitment(block, prev_block);
      75                 :             : 
      76         [ +  - ]:        6310 :     block.hashMerkleRoot = BlockMerkleRoot(block);
      77                 :        6310 : }
      78                 :             : 
      79                 :        7328 : static BlockAssembler::Options ClampOptions(BlockAssembler::Options options)
      80                 :             : {
      81         [ +  - ]:        7328 :     options.block_reserved_weight = std::clamp<size_t>(options.block_reserved_weight, MINIMUM_BLOCK_RESERVED_WEIGHT, MAX_BLOCK_WEIGHT);
      82         [ -  + ]:        7328 :     options.coinbase_output_max_additional_sigops = std::clamp<size_t>(options.coinbase_output_max_additional_sigops, 0, MAX_BLOCK_SIGOPS_COST);
      83                 :             :     // Limit weight to between block_reserved_weight and MAX_BLOCK_WEIGHT for sanity:
      84                 :             :     // block_reserved_weight can safely exceed -blockmaxweight, but the rest of the block template will be empty.
      85         [ +  - ]:        7328 :     options.nBlockMaxWeight = std::clamp<size_t>(options.nBlockMaxWeight, options.block_reserved_weight, MAX_BLOCK_WEIGHT);
      86                 :        7328 :     return options;
      87                 :             : }
      88                 :             : 
      89                 :        7328 : BlockAssembler::BlockAssembler(Chainstate& chainstate, const CTxMemPool* mempool, const Options& options)
      90         [ -  + ]:        7328 :     : chainparams{chainstate.m_chainman.GetParams()},
      91         [ -  + ]:        7328 :       m_mempool{options.use_mempool ? mempool : nullptr},
      92                 :        7328 :       m_chainstate{chainstate},
      93         [ -  + ]:        7328 :       m_options{ClampOptions(options)}
      94                 :             : {
      95                 :        7328 : }
      96                 :             : 
      97                 :          76 : void ApplyArgsManOptions(const ArgsManager& args, BlockAssembler::Options& options)
      98                 :             : {
      99                 :             :     // Block resource limits
     100         [ +  - ]:          76 :     options.nBlockMaxWeight = args.GetIntArg("-blockmaxweight", options.nBlockMaxWeight);
     101   [ +  -  -  + ]:         152 :     if (const auto blockmintxfee{args.GetArg("-blockmintxfee")}) {
     102   [ #  #  #  # ]:           0 :         if (const auto parsed{ParseMoney(*blockmintxfee)}) options.blockMinFeeRate = CFeeRate{*parsed};
     103                 :           0 :     }
     104         [ +  - ]:          76 :     options.print_modified_fee = args.GetBoolArg("-printpriority", options.print_modified_fee);
     105         [ +  - ]:          76 :     options.block_reserved_weight = args.GetIntArg("-blockreservedweight", options.block_reserved_weight);
     106                 :          76 : }
     107                 :             : 
     108                 :        7328 : void BlockAssembler::resetBlock()
     109                 :             : {
     110                 :             :     // Reserve space for fixed-size block header, txs count, and coinbase tx.
     111                 :        7328 :     nBlockWeight = m_options.block_reserved_weight;
     112                 :        7328 :     nBlockSigOpsCost = m_options.coinbase_output_max_additional_sigops;
     113                 :             : 
     114                 :             :     // These counters do not include coinbase tx
     115                 :        7328 :     nBlockTx = 0;
     116                 :        7328 :     nFees = 0;
     117                 :        7328 : }
     118                 :             : 
     119                 :        7328 : std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock()
     120                 :             : {
     121                 :        7328 :     const auto time_start{SteadyClock::now()};
     122                 :             : 
     123                 :        7328 :     resetBlock();
     124                 :             : 
     125         [ -  + ]:        7328 :     pblocktemplate.reset(new CBlockTemplate());
     126                 :        7328 :     CBlock* const pblock = &pblocktemplate->block; // pointer for convenience
     127                 :             : 
     128                 :             :     // Add dummy coinbase tx as first transaction. It is skipped by the
     129                 :             :     // getblocktemplate RPC and mining interface consumers must not use it.
     130                 :        7328 :     pblock->vtx.emplace_back();
     131                 :             : 
     132                 :        7328 :     LOCK(::cs_main);
     133         [ -  + ]:        7328 :     CBlockIndex* pindexPrev = m_chainstate.m_chain.Tip();
     134         [ -  + ]:        7328 :     assert(pindexPrev != nullptr);
     135                 :        7328 :     nHeight = pindexPrev->nHeight + 1;
     136                 :             : 
     137         [ +  - ]:        7328 :     pblock->nVersion = m_chainstate.m_chainman.m_versionbitscache.ComputeBlockVersion(pindexPrev, chainparams.GetConsensus());
     138                 :             :     // -regtest only: allow overriding block.nVersion with
     139                 :             :     // -blockversion=N to test forking scenarios
     140         [ +  + ]:        7328 :     if (chainparams.MineBlocksOnDemand()) {
     141   [ +  -  +  - ]:        7189 :         pblock->nVersion = gArgs.GetIntArg("-blockversion", pblock->nVersion);
     142                 :             :     }
     143                 :             : 
     144                 :        7328 :     pblock->nTime = TicksSinceEpoch<std::chrono::seconds>(NodeClock::now());
     145                 :        7328 :     m_lock_time_cutoff = pindexPrev->GetMedianTimePast();
     146                 :             : 
     147         [ +  + ]:        7328 :     if (m_mempool) {
     148         [ +  - ]:        1016 :         LOCK(m_mempool->cs);
     149                 :        1016 :         m_mempool->StartBlockBuilding();
     150         [ +  - ]:        1016 :         addChunks();
     151         [ +  - ]:        1016 :         m_mempool->StopBlockBuilding();
     152                 :        1016 :     }
     153                 :             : 
     154                 :        7328 :     const auto time_1{SteadyClock::now()};
     155                 :             : 
     156         [ +  + ]:        7328 :     m_last_block_num_txs = nBlockTx;
     157         [ +  + ]:        7328 :     m_last_block_weight = nBlockWeight;
     158                 :             : 
     159                 :             :     // Create coinbase transaction.
     160         [ +  - ]:        7328 :     CMutableTransaction coinbaseTx;
     161         [ +  - ]:        7328 :     coinbaseTx.vin.resize(1);
     162                 :        7328 :     coinbaseTx.vin[0].prevout.SetNull();
     163         [ +  - ]:        7328 :     coinbaseTx.vin[0].nSequence = CTxIn::MAX_SEQUENCE_NONFINAL; // Make sure timelock is enforced.
     164         [ +  - ]:        7328 :     coinbaseTx.vout.resize(1);
     165                 :        7328 :     coinbaseTx.vout[0].scriptPubKey = m_options.coinbase_output_script;
     166   [ +  -  +  - ]:        7328 :     coinbaseTx.vout[0].nValue = nFees + GetBlockSubsidy(nHeight, chainparams.GetConsensus());
     167   [ +  -  +  - ]:        7328 :     coinbaseTx.vin[0].scriptSig = CScript() << nHeight << OP_0;
     168         [ -  + ]:        7328 :     Assert(nHeight > 0);
     169                 :        7328 :     coinbaseTx.nLockTime = static_cast<uint32_t>(nHeight - 1);
     170   [ +  -  -  + ]:       14656 :     pblock->vtx[0] = MakeTransactionRef(std::move(coinbaseTx));
     171         [ +  - ]:        7328 :     pblocktemplate->vchCoinbaseCommitment = m_chainstate.m_chainman.GenerateCoinbaseCommitment(*pblock, pindexPrev);
     172                 :             : 
     173         [ +  - ]:        7328 :     LogPrintf("CreateNewBlock(): block weight: %u txs: %u fees: %ld sigops %d\n", GetBlockWeight(*pblock), nBlockTx, nFees, nBlockSigOpsCost);
     174                 :             : 
     175                 :             :     // Fill in header
     176                 :        7328 :     pblock->hashPrevBlock  = pindexPrev->GetBlockHash();
     177         [ +  - ]:        7328 :     UpdateTime(pblock, chainparams.GetConsensus(), pindexPrev);
     178         [ +  - ]:        7328 :     pblock->nBits          = GetNextWorkRequired(pindexPrev, pblock, chainparams.GetConsensus());
     179                 :        7328 :     pblock->nNonce         = 0;
     180                 :             : 
     181         [ +  - ]:        7328 :     if (m_options.test_block_validity) {
     182   [ +  -  +  + ]:        7328 :         if (BlockValidationState state{TestBlockValidity(m_chainstate, *pblock, /*check_pow=*/false, /*check_merkle_root=*/false)}; !state.IsValid()) {
     183   [ +  -  +  -  :          10 :             throw std::runtime_error(strprintf("TestBlockValidity failed: %s", state.ToString()));
                   +  - ]
     184                 :        7328 :         }
     185                 :             :     }
     186                 :        7323 :     const auto time_2{SteadyClock::now()};
     187                 :             : 
     188   [ +  -  +  -  :        7323 :     LogDebug(BCLog::BENCH, "CreateNewBlock() chunks: %.2fms, validity: %.2fms (total %.2fms)\n",
                   +  - ]
     189                 :             :              Ticks<MillisecondsDouble>(time_1 - time_start),
     190                 :             :              Ticks<MillisecondsDouble>(time_2 - time_1),
     191                 :             :              Ticks<MillisecondsDouble>(time_2 - time_start));
     192                 :             : 
     193                 :        7323 :     return std::move(pblocktemplate);
     194         [ +  - ]:       14651 : }
     195                 :             : 
     196                 :         138 : bool BlockAssembler::TestPackage(FeePerWeight package_feerate, int64_t packageSigOpsCost) const
     197                 :             : {
     198         [ +  - ]:         138 :     if (nBlockWeight + package_feerate.size >= m_options.nBlockMaxWeight) {
     199                 :             :         return false;
     200                 :             :     }
     201         [ +  + ]:         138 :     if (nBlockSigOpsCost + packageSigOpsCost >= MAX_BLOCK_SIGOPS_COST) {
     202                 :           2 :         return false;
     203                 :             :     }
     204                 :             :     return true;
     205                 :             : }
     206                 :             : 
     207                 :             : // Perform transaction-level checks before adding to block:
     208                 :             : // - transaction finality (locktime)
     209                 :         136 : bool BlockAssembler::TestPackageTransactions(const std::vector<CTxMemPoolEntryRef>& txs) const
     210                 :             : {
     211         [ +  + ]:         332 :     for (const auto tx : txs) {
     212         [ +  + ]:         198 :         if (!IsFinalTx(tx.get().GetTx(), nHeight, m_lock_time_cutoff)) {
     213                 :             :             return false;
     214                 :             :         }
     215                 :             :     }
     216                 :             :     return true;
     217                 :             : }
     218                 :             : 
     219                 :         196 : void BlockAssembler::AddToBlock(const CTxMemPoolEntry& entry)
     220                 :             : {
     221   [ +  -  +  - ]:         392 :     pblocktemplate->block.vtx.emplace_back(entry.GetSharedTx());
     222                 :         196 :     pblocktemplate->vTxFees.push_back(entry.GetFee());
     223                 :         196 :     pblocktemplate->vTxSigOpsCost.push_back(entry.GetSigOpCost());
     224         [ -  + ]:         196 :     nBlockWeight += entry.GetTxWeight();
     225                 :         196 :     ++nBlockTx;
     226         [ -  + ]:         196 :     nBlockSigOpsCost += entry.GetSigOpCost();
     227                 :         196 :     nFees += entry.GetFee();
     228                 :             : 
     229         [ -  + ]:         196 :     if (m_options.print_modified_fee) {
     230   [ #  #  #  #  :           0 :         LogPrintf("fee rate %s txid %s\n",
             #  #  #  # ]
     231                 :             :                   CFeeRate(entry.GetModifiedFee(), entry.GetTxSize()).ToString(),
     232                 :             :                   entry.GetTx().GetHash().ToString());
     233                 :             :     }
     234                 :         196 : }
     235                 :             : 
     236                 :        1016 : void BlockAssembler::addChunks()
     237                 :             : {
     238                 :             :     // Limit the number of attempts to add transactions to the block when it is
     239                 :             :     // close to full; this is just a simple heuristic to finish quickly if the
     240                 :             :     // mempool has a lot of entries.
     241                 :        1016 :     const int64_t MAX_CONSECUTIVE_FAILURES = 1000;
     242                 :        1016 :     constexpr int32_t BLOCK_FULL_ENOUGH_WEIGHT_DELTA = 4000;
     243                 :        1016 :     int64_t nConsecutiveFailed = 0;
     244                 :             : 
     245                 :        1016 :     std::vector<CTxMemPoolEntry::CTxMemPoolEntryRef> selected_transactions;
     246         [ +  - ]:        1016 :     selected_transactions.reserve(MAX_CLUSTER_COUNT_LIMIT);
     247                 :        1016 :     FeePerWeight chunk_feerate;
     248                 :             : 
     249                 :             :     // This fills selected_transactions
     250         [ +  - ]:        1016 :     chunk_feerate = m_mempool->GetBlockBuilderChunk(selected_transactions);
     251                 :        1016 :     FeePerVSize chunk_feerate_vsize = ToFeePerVSize(chunk_feerate);
     252                 :             : 
     253   [ -  +  +  + ]:        1154 :     while (selected_transactions.size() > 0) {
     254                 :             :         // Check to see if min fee rate is still respected.
     255   [ +  -  +  + ]:         141 :         if (chunk_feerate.fee < m_options.blockMinFeeRate.GetFee(chunk_feerate_vsize.size)) {
     256                 :             :             // Everything else we might consider has a lower feerate
     257                 :             :             return;
     258                 :             :         }
     259                 :             : 
     260                 :         138 :         int64_t package_sig_ops = 0;
     261         [ +  + ]:         338 :         for (const auto& tx : selected_transactions) {
     262                 :         200 :             package_sig_ops += tx.get().GetSigOpCost();
     263                 :             :         }
     264                 :             : 
     265                 :             :         // Check to see if this chunk will fit.
     266   [ +  -  +  +  :         138 :         if (!TestPackage(chunk_feerate, package_sig_ops) || !TestPackageTransactions(selected_transactions)) {
             +  -  +  + ]
     267                 :             :             // This chunk won't fit, so we skip it and will try the next best one.
     268                 :           4 :             m_mempool->SkipBuilderChunk();
     269                 :           4 :             ++nConsecutiveFailed;
     270                 :             : 
     271         [ -  + ]:           4 :             if (nConsecutiveFailed > MAX_CONSECUTIVE_FAILURES && nBlockWeight +
     272         [ #  # ]:           0 :                     BLOCK_FULL_ENOUGH_WEIGHT_DELTA > m_options.nBlockMaxWeight) {
     273                 :             :                 // Give up if we're close to full and haven't succeeded in a while
     274                 :             :                 return;
     275                 :             :             }
     276                 :             :         } else {
     277                 :         134 :             m_mempool->IncludeBuilderChunk();
     278                 :             : 
     279                 :             :             // This chunk will fit, so add it to the block.
     280                 :         134 :             nConsecutiveFailed = 0;
     281         [ +  + ]:         330 :             for (const auto& tx : selected_transactions) {
     282         [ +  - ]:         196 :                 AddToBlock(tx);
     283                 :             :             }
     284         [ +  - ]:         134 :             pblocktemplate->m_package_feerates.emplace_back(chunk_feerate_vsize);
     285                 :             :         }
     286                 :             : 
     287         [ +  - ]:         138 :         selected_transactions.clear();
     288         [ +  - ]:         138 :         chunk_feerate = m_mempool->GetBlockBuilderChunk(selected_transactions);
     289                 :         138 :         chunk_feerate_vsize = ToFeePerVSize(chunk_feerate);
     290                 :             :     }
     291                 :        1016 : }
     292                 :             : 
     293                 :          55 : void AddMerkleRootAndCoinbase(CBlock& block, CTransactionRef coinbase, uint32_t version, uint32_t timestamp, uint32_t nonce)
     294                 :             : {
     295   [ -  +  -  + ]:          55 :     if (block.vtx.size() == 0) {
     296                 :           0 :         block.vtx.emplace_back(coinbase);
     297                 :             :     } else {
     298                 :          55 :         block.vtx[0] = coinbase;
     299                 :             :     }
     300                 :          55 :     block.nVersion = version;
     301                 :          55 :     block.nTime = timestamp;
     302                 :          55 :     block.nNonce = nonce;
     303                 :          55 :     block.hashMerkleRoot = BlockMerkleRoot(block);
     304                 :             : 
     305                 :             :     // Reset cached checks
     306                 :          55 :     block.m_checked_witness_commitment = false;
     307                 :          55 :     block.m_checked_merkle_root = false;
     308                 :          55 :     block.fChecked = false;
     309                 :          55 : }
     310                 :             : 
     311                 :           0 : void InterruptWait(KernelNotifications& kernel_notifications, bool& interrupt_wait)
     312                 :             : {
     313                 :           0 :     LOCK(kernel_notifications.m_tip_block_mutex);
     314                 :           0 :     interrupt_wait = true;
     315         [ #  # ]:           0 :     kernel_notifications.m_tip_block_cv.notify_all();
     316                 :           0 : }
     317                 :             : 
     318                 :          62 : std::unique_ptr<CBlockTemplate> WaitAndCreateNewBlock(ChainstateManager& chainman,
     319                 :             :                                                       KernelNotifications& kernel_notifications,
     320                 :             :                                                       CTxMemPool* mempool,
     321                 :             :                                                       const std::unique_ptr<CBlockTemplate>& block_template,
     322                 :             :                                                       const BlockWaitOptions& options,
     323                 :             :                                                       const BlockAssembler::Options& assemble_options,
     324                 :             :                                                       bool& interrupt_wait)
     325                 :             : {
     326                 :             :     // Delay calculating the current template fees, just in case a new block
     327                 :             :     // comes in before the next tick.
     328                 :          62 :     CAmount current_fees = -1;
     329                 :             : 
     330                 :             :     // Alternate waiting for a new tip and checking if fees have risen.
     331                 :             :     // The latter check is expensive so we only run it once per second.
     332                 :          62 :     auto now{NodeClock::now()};
     333                 :          62 :     const auto deadline = now + options.timeout;
     334                 :          62 :     const MillisecondsDouble tick{1000};
     335                 :          62 :     const bool allow_min_difficulty{chainman.GetParams().GetConsensus().fPowAllowMinDifficultyBlocks};
     336                 :             : 
     337                 :          62 :     do {
     338                 :          62 :         bool tip_changed{false};
     339                 :          62 :         {
     340                 :          62 :             WAIT_LOCK(kernel_notifications.m_tip_block_mutex, lock);
     341                 :             :             // Note that wait_until() checks the predicate before waiting
     342         [ +  - ]:          62 :             kernel_notifications.m_tip_block_cv.wait_until(lock, std::min(now + tick, deadline), [&]() EXCLUSIVE_LOCKS_REQUIRED(kernel_notifications.m_tip_block_mutex) {
     343                 :          69 :                 AssertLockHeld(kernel_notifications.m_tip_block_mutex);
     344                 :          69 :                 const auto tip_block{kernel_notifications.TipBlock()};
     345                 :             :                 // We assume tip_block is set, because this is an instance
     346                 :             :                 // method on BlockTemplate and no template could have been
     347                 :             :                 // generated before a tip exists.
     348   [ +  -  +  + ]:          69 :                 tip_changed = Assume(tip_block) && tip_block != block_template->block.hashPrevBlock;
     349   [ +  +  +  -  :          69 :                 return tip_changed || chainman.m_interrupt || interrupt_wait;
                   -  + ]
     350                 :             :             });
     351         [ -  + ]:          62 :             if (interrupt_wait) {
     352                 :           0 :                 interrupt_wait = false;
     353         [ #  # ]:           0 :                 return nullptr;
     354                 :             :             }
     355                 :           0 :         }
     356                 :             : 
     357         [ -  + ]:          62 :         if (chainman.m_interrupt) return nullptr;
     358                 :             :         // At this point the tip changed, a full tick went by or we reached
     359                 :             :         // the deadline.
     360                 :             : 
     361                 :             :         // Must release m_tip_block_mutex before locking cs_main, to avoid deadlocks.
     362                 :          62 :         LOCK(::cs_main);
     363                 :             : 
     364                 :             :         // On test networks return a minimum difficulty block after 20 minutes
     365   [ +  +  +  + ]:          62 :         if (!tip_changed && allow_min_difficulty) {
     366   [ +  -  -  + ]:           6 :             const NodeClock::time_point tip_time{std::chrono::seconds{chainman.ActiveChain().Tip()->GetBlockTime()}};
     367         [ +  + ]:           3 :             if (now > tip_time + 20min) {
     368                 :           1 :                 tip_changed = true;
     369                 :             :             }
     370                 :             :         }
     371                 :             : 
     372                 :             :         /**
     373                 :             :          * We determine if fees increased compared to the previous template by generating
     374                 :             :          * a fresh template. There may be more efficient ways to determine how much
     375                 :             :          * (approximate) fees for the next block increased, perhaps more so after
     376                 :             :          * Cluster Mempool.
     377                 :             :          *
     378                 :             :          * We'll also create a new template if the tip changed during this iteration.
     379                 :             :          */
     380   [ +  +  +  - ]:          62 :         if (options.fee_threshold < MAX_MONEY || tip_changed) {
     381                 :           0 :             auto new_tmpl{BlockAssembler{
     382                 :             :                 chainman.ActiveChainstate(),
     383                 :             :                 mempool,
     384   [ +  -  +  - ]:          62 :                 assemble_options}
     385         [ +  - ]:          62 :                               .CreateNewBlock()};
     386                 :             : 
     387                 :             :             // If the tip changed, return the new template regardless of its fees.
     388         [ +  + ]:          62 :             if (tip_changed) return new_tmpl;
     389                 :             : 
     390                 :             :             // Calculate the original template total fees if we haven't already
     391         [ +  - ]:           6 :             if (current_fees == -1) {
     392                 :           6 :                 current_fees = std::accumulate(block_template->vTxFees.begin(), block_template->vTxFees.end(), CAmount{0});
     393                 :             :             }
     394                 :             : 
     395                 :             :             // Check if fees increased enough to return the new template
     396                 :           6 :             const CAmount new_fees = std::accumulate(new_tmpl->vTxFees.begin(), new_tmpl->vTxFees.end(), CAmount{0});
     397         [ +  + ]:           6 :             Assume(options.fee_threshold != MAX_MONEY);
     398         [ +  + ]:           6 :             if (new_fees >= current_fees + options.fee_threshold) return new_tmpl;
     399         [ +  - ]:          62 :         }
     400                 :             : 
     401         [ +  - ]:           4 :         now = NodeClock::now();
     402         [ -  + ]:          62 :     } while (now < deadline);
     403                 :             : 
     404                 :           4 :     return nullptr;
     405                 :             : }
     406                 :             : 
     407                 :         241 : std::optional<BlockRef> GetTip(ChainstateManager& chainman)
     408                 :             : {
     409                 :         241 :     LOCK(::cs_main);
     410   [ +  -  -  + ]:         241 :     CBlockIndex* tip{chainman.ActiveChain().Tip()};
     411         [ -  + ]:         241 :     if (!tip) return {};
     412                 :         241 :     return BlockRef{tip->GetBlockHash(), tip->nHeight};
     413                 :         241 : }
     414                 :             : 
     415                 :         131 : std::optional<BlockRef> WaitTipChanged(ChainstateManager& chainman, KernelNotifications& kernel_notifications, const uint256& current_tip, MillisecondsDouble& timeout)
     416                 :             : {
     417                 :         131 :     Assume(timeout >= 0ms); // No internal callers should use a negative timeout
     418         [ -  + ]:         131 :     if (timeout < 0ms) timeout = 0ms;
     419         [ +  - ]:         131 :     if (timeout > std::chrono::years{100}) timeout = std::chrono::years{100}; // Upper bound to avoid UB in std::chrono
     420                 :         131 :     auto deadline{std::chrono::steady_clock::now() + timeout};
     421                 :         131 :     {
     422                 :         131 :         WAIT_LOCK(kernel_notifications.m_tip_block_mutex, lock);
     423                 :             :         // For callers convenience, wait longer than the provided timeout
     424                 :             :         // during startup for the tip to be non-null. That way this function
     425                 :             :         // always returns valid tip information when possible and only
     426                 :             :         // returns null when shutting down, not when timing out.
     427         [ +  - ]:         131 :         kernel_notifications.m_tip_block_cv.wait(lock, [&]() EXCLUSIVE_LOCKS_REQUIRED(kernel_notifications.m_tip_block_mutex) {
     428   [ -  +  -  - ]:         131 :             return kernel_notifications.TipBlock() || chainman.m_interrupt;
     429                 :             :         });
     430   [ +  -  -  +  :         131 :         if (chainman.m_interrupt) return {};
                   -  - ]
     431                 :             :         // At this point TipBlock is set, so continue to wait until it is
     432                 :             :         // different then `current_tip` provided by caller.
     433         [ +  - ]:         131 :         kernel_notifications.m_tip_block_cv.wait_until(lock, deadline, [&]() EXCLUSIVE_LOCKS_REQUIRED(kernel_notifications.m_tip_block_mutex) {
     434   [ -  +  -  - ]:         131 :             return Assume(kernel_notifications.TipBlock()) != current_tip || chainman.m_interrupt;
     435                 :             :         });
     436                 :           0 :     }
     437         [ -  + ]:         131 :     if (chainman.m_interrupt) return {};
     438                 :             : 
     439                 :             :     // Must release m_tip_block_mutex before getTip() locks cs_main, to
     440                 :             :     // avoid deadlocks.
     441                 :         131 :     return GetTip(chainman);
     442                 :             : }
     443                 :             : } // namespace node
        

Generated by: LCOV version 2.0-1