LCOV - code coverage report
Current view: top level - src/node - miner.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 98.5 % 204 201
Test Date: 2025-03-28 05:06:44 Functions: 100.0 % 16 16
Branches: 65.2 % 282 184

             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 <policy/feerate.h>
      20                 :             : #include <policy/policy.h>
      21                 :             : #include <pow.h>
      22                 :             : #include <primitives/transaction.h>
      23                 :             : #include <util/moneystr.h>
      24                 :             : #include <util/time.h>
      25                 :             : #include <validation.h>
      26                 :             : 
      27                 :             : #include <algorithm>
      28                 :             : #include <utility>
      29                 :             : 
      30                 :             : namespace node {
      31                 :             : 
      32                 :       49951 : int64_t GetMinimumTime(const CBlockIndex* pindexPrev, const int64_t difficulty_adjustment_interval)
      33                 :             : {
      34                 :       49951 :     int64_t min_time{pindexPrev->GetMedianTimePast() + 1};
      35                 :             :     // Height of block to be mined.
      36                 :       49951 :     const int height{pindexPrev->nHeight + 1};
      37                 :             :     // Account for BIP94 timewarp rule on all networks. This makes future
      38                 :             :     // activation safer.
      39         [ +  + ]:       49951 :     if (height % difficulty_adjustment_interval == 0) {
      40         [ +  + ]:         441 :         min_time = std::max<int64_t>(min_time, pindexPrev->GetBlockTime() - MAX_TIMEWARP);
      41                 :             :     }
      42                 :       49951 :     return min_time;
      43                 :             : }
      44                 :             : 
      45                 :       47881 : int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev)
      46                 :             : {
      47                 :       47881 :     int64_t nOldTime = pblock->nTime;
      48         [ +  + ]:       47881 :     int64_t nNewTime{std::max<int64_t>(GetMinimumTime(pindexPrev, consensusParams.DifficultyAdjustmentInterval()),
      49                 :       47881 :                                        TicksSinceEpoch<std::chrono::seconds>(NodeClock::now()))};
      50                 :             : 
      51         [ +  + ]:       47881 :     if (nOldTime < nNewTime) {
      52                 :       36334 :         pblock->nTime = nNewTime;
      53                 :             :     }
      54                 :             : 
      55                 :             :     // Updating time can change work required on testnet:
      56         [ +  + ]:       47881 :     if (consensusParams.fPowAllowMinDifficultyBlocks) {
      57                 :       47744 :         pblock->nBits = GetNextWorkRequired(pindexPrev, pblock, consensusParams);
      58                 :             :     }
      59                 :             : 
      60                 :       47881 :     return nNewTime - nOldTime;
      61                 :             : }
      62                 :             : 
      63                 :        6649 : void RegenerateCommitments(CBlock& block, ChainstateManager& chainman)
      64                 :             : {
      65                 :        6649 :     CMutableTransaction tx{*block.vtx.at(0)};
      66                 :        6649 :     tx.vout.erase(tx.vout.begin() + GetWitnessCommitmentIndex(block));
      67   [ +  -  +  -  :       13298 :     block.vtx.at(0) = MakeTransactionRef(tx);
                   -  + ]
      68                 :             : 
      69   [ +  -  +  -  :       19947 :     const CBlockIndex* prev_block = WITH_LOCK(::cs_main, return chainman.m_blockman.LookupBlockIndex(block.hashPrevBlock));
                   +  - ]
      70         [ +  - ]:        6649 :     chainman.GenerateCoinbaseCommitment(block, prev_block);
      71                 :             : 
      72         [ +  - ]:        6649 :     block.hashMerkleRoot = BlockMerkleRoot(block);
      73                 :        6649 : }
      74                 :             : 
      75                 :       45801 : static BlockAssembler::Options ClampOptions(BlockAssembler::Options options)
      76                 :             : {
      77                 :       45801 :     Assert(options.block_reserved_weight <= MAX_BLOCK_WEIGHT);
      78                 :       45801 :     Assert(options.block_reserved_weight >= MINIMUM_BLOCK_RESERVED_WEIGHT);
      79                 :       45801 :     Assert(options.coinbase_output_max_additional_sigops <= MAX_BLOCK_SIGOPS_COST);
      80                 :             :     // Limit weight to between block_reserved_weight and MAX_BLOCK_WEIGHT for sanity:
      81                 :             :     // block_reserved_weight can safely exceed -blockmaxweight, but the rest of the block template will be empty.
      82         [ +  - ]:       45801 :     options.nBlockMaxWeight = std::clamp<size_t>(options.nBlockMaxWeight, options.block_reserved_weight, MAX_BLOCK_WEIGHT);
      83                 :       45801 :     return options;
      84                 :             : }
      85                 :             : 
      86                 :       45801 : BlockAssembler::BlockAssembler(Chainstate& chainstate, const CTxMemPool* mempool, const Options& options)
      87                 :       45801 :     : chainparams{chainstate.m_chainman.GetParams()},
      88         [ +  + ]:       45801 :       m_mempool{options.use_mempool ? mempool : nullptr},
      89                 :       45801 :       m_chainstate{chainstate},
      90   [ +  -  +  +  :       46139 :       m_options{ClampOptions(options)}
                   +  - ]
      91                 :             : {
      92                 :       45801 : }
      93                 :             : 
      94                 :       38416 : void ApplyArgsManOptions(const ArgsManager& args, BlockAssembler::Options& options)
      95                 :             : {
      96                 :             :     // Block resource limits
      97         [ +  - ]:       38416 :     options.nBlockMaxWeight = args.GetIntArg("-blockmaxweight", options.nBlockMaxWeight);
      98   [ +  -  +  + ]:       76832 :     if (const auto blockmintxfee{args.GetArg("-blockmintxfee")}) {
      99   [ +  -  +  - ]:          18 :         if (const auto parsed{ParseMoney(*blockmintxfee)}) options.blockMinFeeRate = CFeeRate{*parsed};
     100                 :           0 :     }
     101         [ +  - ]:       38416 :     options.print_modified_fee = args.GetBoolArg("-printpriority", options.print_modified_fee);
     102         [ +  - ]:       38416 :     options.block_reserved_weight = args.GetIntArg("-blockreservedweight", options.block_reserved_weight);
     103                 :       38416 : }
     104                 :             : 
     105                 :       45801 : void BlockAssembler::resetBlock()
     106                 :             : {
     107                 :       45801 :     inBlock.clear();
     108                 :             : 
     109                 :             :     // Reserve space for fixed-size block header, txs count, and coinbase tx.
     110                 :       45801 :     nBlockWeight = m_options.block_reserved_weight;
     111                 :       45801 :     nBlockSigOpsCost = m_options.coinbase_output_max_additional_sigops;
     112                 :             : 
     113                 :             :     // These counters do not include coinbase tx
     114                 :       45801 :     nBlockTx = 0;
     115                 :       45801 :     nFees = 0;
     116                 :       45801 : }
     117                 :             : 
     118                 :       45801 : std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock()
     119                 :             : {
     120                 :       45801 :     const auto time_start{SteadyClock::now()};
     121                 :             : 
     122                 :       45801 :     resetBlock();
     123                 :             : 
     124         [ -  + ]:       45801 :     pblocktemplate.reset(new CBlockTemplate());
     125                 :       45801 :     CBlock* const pblock = &pblocktemplate->block; // pointer for convenience
     126                 :             : 
     127                 :             :     // Add dummy coinbase tx as first transaction. It is skipped by the
     128                 :             :     // getblocktemplate RPC and mining interface consumers must not use it.
     129                 :       45801 :     pblock->vtx.emplace_back();
     130                 :             : 
     131                 :       45801 :     LOCK(::cs_main);
     132         [ +  - ]:       45801 :     CBlockIndex* pindexPrev = m_chainstate.m_chain.Tip();
     133         [ -  + ]:       45801 :     assert(pindexPrev != nullptr);
     134                 :       45801 :     nHeight = pindexPrev->nHeight + 1;
     135                 :             : 
     136         [ +  - ]:       45801 :     pblock->nVersion = m_chainstate.m_chainman.m_versionbitscache.ComputeBlockVersion(pindexPrev, chainparams.GetConsensus());
     137                 :             :     // -regtest only: allow overriding block.nVersion with
     138                 :             :     // -blockversion=N to test forking scenarios
     139         [ +  + ]:       45801 :     if (chainparams.MineBlocksOnDemand()) {
     140   [ +  -  +  - ]:       45665 :         pblock->nVersion = gArgs.GetIntArg("-blockversion", pblock->nVersion);
     141                 :             :     }
     142                 :             : 
     143                 :       45801 :     pblock->nTime = TicksSinceEpoch<std::chrono::seconds>(NodeClock::now());
     144                 :       45801 :     m_lock_time_cutoff = pindexPrev->GetMedianTimePast();
     145                 :             : 
     146                 :       45801 :     int nPackagesSelected = 0;
     147                 :       45801 :     int nDescendantsUpdated = 0;
     148         [ +  + ]:       45801 :     if (m_mempool) {
     149         [ +  - ]:       39150 :         addPackageTxs(nPackagesSelected, nDescendantsUpdated);
     150                 :             :     }
     151                 :             : 
     152                 :       45801 :     const auto time_1{SteadyClock::now()};
     153                 :             : 
     154         [ +  + ]:       45801 :     m_last_block_num_txs = nBlockTx;
     155         [ +  + ]:       45801 :     m_last_block_weight = nBlockWeight;
     156                 :             : 
     157                 :             :     // Create coinbase transaction.
     158         [ +  - ]:       45801 :     CMutableTransaction coinbaseTx;
     159         [ +  - ]:       45801 :     coinbaseTx.vin.resize(1);
     160                 :       45801 :     coinbaseTx.vin[0].prevout.SetNull();
     161         [ +  - ]:       45801 :     coinbaseTx.vout.resize(1);
     162                 :       45801 :     coinbaseTx.vout[0].scriptPubKey = m_options.coinbase_output_script;
     163   [ +  -  +  - ]:       45801 :     coinbaseTx.vout[0].nValue = nFees + GetBlockSubsidy(nHeight, chainparams.GetConsensus());
     164   [ +  -  +  - ]:       45801 :     coinbaseTx.vin[0].scriptSig = CScript() << nHeight << OP_0;
     165   [ +  -  -  + ]:       91602 :     pblock->vtx[0] = MakeTransactionRef(std::move(coinbaseTx));
     166         [ +  - ]:       45801 :     pblocktemplate->vchCoinbaseCommitment = m_chainstate.m_chainman.GenerateCoinbaseCommitment(*pblock, pindexPrev);
     167                 :             : 
     168         [ +  - ]:       45801 :     LogPrintf("CreateNewBlock(): block weight: %u txs: %u fees: %ld sigops %d\n", GetBlockWeight(*pblock), nBlockTx, nFees, nBlockSigOpsCost);
     169                 :             : 
     170                 :             :     // Fill in header
     171                 :       45801 :     pblock->hashPrevBlock  = pindexPrev->GetBlockHash();
     172         [ +  - ]:       45801 :     UpdateTime(pblock, chainparams.GetConsensus(), pindexPrev);
     173         [ +  - ]:       45801 :     pblock->nBits          = GetNextWorkRequired(pindexPrev, pblock, chainparams.GetConsensus());
     174                 :       45801 :     pblock->nNonce         = 0;
     175                 :             : 
     176         [ +  - ]:       45801 :     BlockValidationState state;
     177   [ +  -  +  -  :       45801 :     if (m_options.test_block_validity && !TestBlockValidity(state, chainparams, m_chainstate, *pblock, pindexPrev,
                   +  + ]
     178                 :             :                                                             /*fCheckPOW=*/false, /*fCheckMerkleRoot=*/false)) {
     179   [ +  -  +  -  :          10 :         throw std::runtime_error(strprintf("%s: TestBlockValidity failed: %s", __func__, state.ToString()));
                   +  - ]
     180                 :             :     }
     181                 :       45796 :     const auto time_2{SteadyClock::now()};
     182                 :             : 
     183   [ +  -  +  -  :       45796 :     LogDebug(BCLog::BENCH, "CreateNewBlock() packages: %.2fms (%d packages, %d updated descendants), validity: %.2fms (total %.2fms)\n",
                   +  - ]
     184                 :             :              Ticks<MillisecondsDouble>(time_1 - time_start), nPackagesSelected, nDescendantsUpdated,
     185                 :             :              Ticks<MillisecondsDouble>(time_2 - time_1),
     186                 :             :              Ticks<MillisecondsDouble>(time_2 - time_start));
     187                 :             : 
     188                 :       45796 :     return std::move(pblocktemplate);
     189         [ +  - ]:      137398 : }
     190                 :             : 
     191                 :        8164 : void BlockAssembler::onlyUnconfirmed(CTxMemPool::setEntries& testSet)
     192                 :             : {
     193         [ +  + ]:       19815 :     for (CTxMemPool::setEntries::iterator iit = testSet.begin(); iit != testSet.end(); ) {
     194                 :             :         // Only test txs not already in the block
     195   [ +  -  +  -  :       34953 :         if (inBlock.count((*iit)->GetSharedTx()->GetHash())) {
             +  -  +  + ]
     196                 :        8773 :             testSet.erase(iit++);
     197                 :             :         } else {
     198                 :        2878 :             iit++;
     199                 :             :         }
     200                 :             :     }
     201                 :        8164 : }
     202                 :             : 
     203                 :       48252 : bool BlockAssembler::TestPackage(uint64_t packageSize, int64_t packageSigOpsCost) const
     204                 :             : {
     205                 :             :     // TODO: switch to weight-based accounting for packages instead of vsize-based accounting.
     206         [ +  + ]:       48252 :     if (nBlockWeight + WITNESS_SCALE_FACTOR * packageSize >= m_options.nBlockMaxWeight) {
     207                 :             :         return false;
     208                 :             :     }
     209         [ +  + ]:        8178 :     if (nBlockSigOpsCost + packageSigOpsCost >= MAX_BLOCK_SIGOPS_COST) {
     210                 :          14 :         return false;
     211                 :             :     }
     212                 :             :     return true;
     213                 :             : }
     214                 :             : 
     215                 :             : // Perform transaction-level checks before adding to block:
     216                 :             : // - transaction finality (locktime)
     217                 :        8164 : bool BlockAssembler::TestPackageTransactions(const CTxMemPool::setEntries& package) const
     218                 :             : {
     219         [ +  + ]:       19204 :     for (CTxMemPool::txiter it : package) {
     220         [ +  + ]:       11042 :         if (!IsFinalTx(it->GetTx(), nHeight, m_lock_time_cutoff)) {
     221                 :             :             return false;
     222                 :             :         }
     223                 :             :     }
     224                 :             :     return true;
     225                 :             : }
     226                 :             : 
     227                 :       11040 : void BlockAssembler::AddToBlock(CTxMemPool::txiter iter)
     228                 :             : {
     229   [ +  -  +  - ]:       22080 :     pblocktemplate->block.vtx.emplace_back(iter->GetSharedTx());
     230                 :       11040 :     pblocktemplate->vTxFees.push_back(iter->GetFee());
     231                 :       11040 :     pblocktemplate->vTxSigOpsCost.push_back(iter->GetSigOpCost());
     232         [ +  - ]:       11040 :     nBlockWeight += iter->GetTxWeight();
     233                 :       11040 :     ++nBlockTx;
     234         [ +  - ]:       11040 :     nBlockSigOpsCost += iter->GetSigOpCost();
     235                 :       11040 :     nFees += iter->GetFee();
     236   [ +  -  +  -  :       22080 :     inBlock.insert(iter->GetSharedTx()->GetHash());
                   +  - ]
     237                 :             : 
     238         [ +  + ]:       11040 :     if (m_options.print_modified_fee) {
     239   [ +  -  +  -  :         174 :         LogPrintf("fee rate %s txid %s\n",
             +  -  +  - ]
     240                 :             :                   CFeeRate(iter->GetModifiedFee(), iter->GetTxSize()).ToString(),
     241                 :             :                   iter->GetTx().GetHash().ToString());
     242                 :             :     }
     243                 :       11040 : }
     244                 :             : 
     245                 :             : /** Add descendants of given transactions to mapModifiedTx with ancestor
     246                 :             :  * state updated assuming given transactions are inBlock. Returns number
     247                 :             :  * of updated descendants. */
     248                 :        8162 : static int UpdatePackagesForAdded(const CTxMemPool& mempool,
     249                 :             :                                   const CTxMemPool::setEntries& alreadyAdded,
     250                 :             :                                   indexed_modified_transaction_set& mapModifiedTx) EXCLUSIVE_LOCKS_REQUIRED(mempool.cs)
     251                 :             : {
     252                 :        8162 :     AssertLockHeld(mempool.cs);
     253                 :             : 
     254                 :        8162 :     int nDescendantsUpdated = 0;
     255         [ +  + ]:       19202 :     for (CTxMemPool::txiter it : alreadyAdded) {
     256         [ +  - ]:       11040 :         CTxMemPool::setEntries descendants;
     257         [ +  - ]:       11040 :         mempool.CalculateDescendants(it, descendants);
     258                 :             :         // Insert all descendants (not yet in block) into the modified set
     259         [ +  + ]:     1038209 :         for (CTxMemPool::txiter desc : descendants) {
     260         [ +  + ]:     1027169 :             if (alreadyAdded.count(desc)) {
     261                 :      775429 :                 continue;
     262                 :             :             }
     263                 :      251740 :             ++nDescendantsUpdated;
     264                 :      251740 :             modtxiter mit = mapModifiedTx.find(desc);
     265         [ +  + ]:      251740 :             if (mit == mapModifiedTx.end()) {
     266         [ +  - ]:        1818 :                 CTxMemPoolModifiedEntry modEntry(desc);
     267         [ +  - ]:        1818 :                 mit = mapModifiedTx.insert(modEntry).first;
     268                 :             :             }
     269         [ +  - ]:     1278909 :             mapModifiedTx.modify(mit, update_for_parent_inclusion(it));
     270                 :             :         }
     271                 :       11040 :     }
     272                 :        8162 :     return nDescendantsUpdated;
     273                 :             : }
     274                 :             : 
     275                 :        8162 : void BlockAssembler::SortForBlock(const CTxMemPool::setEntries& package, std::vector<CTxMemPool::txiter>& sortedEntries)
     276                 :             : {
     277                 :             :     // Sort package by ancestor count
     278                 :             :     // If a transaction A depends on transaction B, then A's ancestor count
     279                 :             :     // must be greater than B's.  So this is sufficient to validly order the
     280                 :             :     // transactions for block inclusion.
     281         [ -  + ]:        8162 :     sortedEntries.clear();
     282                 :        8162 :     sortedEntries.insert(sortedEntries.begin(), package.begin(), package.end());
     283                 :        8162 :     std::sort(sortedEntries.begin(), sortedEntries.end(), CompareTxIterByAncestorCount());
     284                 :        8162 : }
     285                 :             : 
     286                 :             : // This transaction selection algorithm orders the mempool based
     287                 :             : // on feerate of a transaction including all unconfirmed ancestors.
     288                 :             : // Since we don't remove transactions from the mempool as we select them
     289                 :             : // for block inclusion, we need an alternate method of updating the feerate
     290                 :             : // of a transaction with its not-yet-selected ancestors as we go.
     291                 :             : // This is accomplished by walking the in-mempool descendants of selected
     292                 :             : // transactions and storing a temporary modified state in mapModifiedTxs.
     293                 :             : // Each time through the loop, we compare the best transaction in
     294                 :             : // mapModifiedTxs with the next transaction in the mempool to decide what
     295                 :             : // transaction package to work on next.
     296                 :       39150 : void BlockAssembler::addPackageTxs(int& nPackagesSelected, int& nDescendantsUpdated)
     297                 :             : {
     298                 :       39150 :     const auto& mempool{*Assert(m_mempool)};
     299                 :       39150 :     LOCK(mempool.cs);
     300                 :             : 
     301                 :             :     // mapModifiedTx will store sorted packages after they are modified
     302                 :             :     // because some of their txs are already in the block
     303         [ +  - ]:       39150 :     indexed_modified_transaction_set mapModifiedTx;
     304                 :             :     // Keep track of entries that failed inclusion, to avoid duplicate work
     305                 :       39150 :     std::set<Txid> failedTx;
     306                 :             : 
     307                 :       39150 :     CTxMemPool::indexed_transaction_set::index<ancestor_score>::type::iterator mi = mempool.mapTx.get<ancestor_score>().begin();
     308                 :       39150 :     CTxMemPool::txiter iter;
     309                 :             : 
     310                 :             :     // Limit the number of attempts to add transactions to the block when it is
     311                 :             :     // close to full; this is just a simple heuristic to finish quickly if the
     312                 :             :     // mempool has a lot of entries.
     313                 :       39150 :     const int64_t MAX_CONSECUTIVE_FAILURES = 1000;
     314                 :       39150 :     int64_t nConsecutiveFailed = 0;
     315                 :             : 
     316   [ +  +  +  + ]:       91015 :     while (mi != mempool.mapTx.get<ancestor_score>().end() || !mapModifiedTx.empty()) {
     317                 :             :         // First try to find a new transaction in mapTx to evaluate.
     318                 :             :         //
     319                 :             :         // Skip entries in mapTx that are already in a block or are present
     320                 :             :         // in mapModifiedTx (which implies that the mapTx ancestor state is
     321                 :             :         // stale due to ancestor inclusion in the block)
     322                 :             :         // Also skip transactions that we've already failed to add. This can happen if
     323                 :             :         // we consider a transaction in mapModifiedTx and it fails: we can then
     324                 :             :         // potentially consider it again while walking mapTx.  It's currently
     325                 :             :         // guaranteed to fail again, but as a belt-and-suspenders check we put it in
     326                 :             :         // failedTx and avoid re-evaluation, since the re-evaluation would be using
     327                 :             :         // cached size/sigops/fee values that are not actually correct.
     328                 :             :         /** Return true if given transaction from mapTx has already been evaluated,
     329                 :             :          * or if the transaction's cached data in mapTx is incorrect. */
     330         [ +  + ]:       51926 :         if (mi != mempool.mapTx.get<ancestor_score>().end()) {
     331         [ -  + ]:       51396 :             auto it = mempool.mapTx.project<0>(mi);
     332         [ -  + ]:       51396 :             assert(it != mempool.mapTx.end());
     333   [ +  +  +  -  :      246356 :             if (mapModifiedTx.count(it) || inBlock.count(it->GetSharedTx()->GetHash()) || failedTx.count(it->GetSharedTx()->GetHash())) {
          +  -  +  +  +  
          -  +  -  +  +  
                   +  + ]
     334                 :        3634 :                 ++mi;
     335                 :        3634 :                 continue;
     336                 :             :             }
     337                 :             :         }
     338                 :             : 
     339                 :             :         // Now that mi is not stale, determine which transaction to evaluate:
     340                 :             :         // the next entry from mapTx, or the best from mapModifiedTx?
     341                 :       48292 :         bool fUsingModified = false;
     342                 :             : 
     343                 :       48292 :         modtxscoreiter modit = mapModifiedTx.get<ancestor_score>().begin();
     344         [ +  + ]:       48292 :         if (mi == mempool.mapTx.get<ancestor_score>().end()) {
     345                 :             :             // We're out of entries in mapTx; use the entry from mapModifiedTx
     346                 :         530 :             iter = modit->iter;
     347                 :         530 :             fUsingModified = true;
     348                 :             :         } else {
     349                 :             :             // Try to compare the mapTx entry to the mapModifiedTx entry
     350         [ +  + ]:       47762 :             iter = mempool.mapTx.project<0>(mi);
     351   [ +  +  +  + ]:       48729 :             if (modit != mapModifiedTx.get<ancestor_score>().end() &&
     352         [ +  - ]:         967 :                     CompareTxMemPoolEntryByAncestorFee()(*modit, CTxMemPoolModifiedEntry(iter))) {
     353                 :             :                 // The best entry in mapModifiedTx has higher score
     354                 :             :                 // than the one from mapTx.
     355                 :             :                 // Switch which transaction (package) to consider
     356                 :         265 :                 iter = modit->iter;
     357                 :         265 :                 fUsingModified = true;
     358                 :             :             } else {
     359                 :             :                 // Either no entry in mapModifiedTx, or it's worse than mapTx.
     360                 :             :                 // Increment mi for the next loop iteration.
     361                 :       47497 :                 ++mi;
     362                 :             :             }
     363                 :             :         }
     364                 :             : 
     365                 :             :         // We skip mapTx entries that are inBlock, and mapModifiedTx shouldn't
     366                 :             :         // contain anything that is inBlock.
     367   [ +  -  +  -  :       96584 :         assert(!inBlock.count(iter->GetSharedTx()->GetHash()));
                   -  + ]
     368                 :             : 
     369         [ +  + ]:       48292 :         uint64_t packageSize = iter->GetSizeWithAncestors();
     370         [ +  + ]:       48292 :         CAmount packageFees = iter->GetModFeesWithAncestors();
     371         [ +  + ]:       48292 :         int64_t packageSigOpsCost = iter->GetSigOpCostWithAncestors();
     372         [ +  + ]:       48292 :         if (fUsingModified) {
     373                 :         795 :             packageSize = modit->nSizeWithAncestors;
     374                 :         795 :             packageFees = modit->nModFeesWithAncestors;
     375                 :         795 :             packageSigOpsCost = modit->nSigOpCostWithAncestors;
     376                 :             :         }
     377                 :             : 
     378   [ +  -  +  + ]:       48292 :         if (packageFees < m_options.blockMinFeeRate.GetFee(packageSize)) {
     379                 :             :             // Everything else we might consider has a lower fee rate
     380                 :          40 :             return;
     381                 :             :         }
     382                 :             : 
     383   [ +  -  +  + ]:       48252 :         if (!TestPackage(packageSize, packageSigOpsCost)) {
     384         [ +  + ]:       40088 :             if (fUsingModified) {
     385                 :             :                 // Since we always look at the best entry in mapModifiedTx,
     386                 :             :                 // we must erase failed entries so that we can consider the
     387                 :             :                 // next best entry on the next loop iteration
     388                 :          66 :                 mapModifiedTx.get<ancestor_score>().erase(modit);
     389   [ +  -  +  - ]:         198 :                 failedTx.insert(iter->GetSharedTx()->GetHash());
     390                 :             :             }
     391                 :             : 
     392                 :       40088 :             ++nConsecutiveFailed;
     393                 :             : 
     394         [ +  + ]:       40088 :             if (nConsecutiveFailed > MAX_CONSECUTIVE_FAILURES && nBlockWeight >
     395         [ -  + ]:          21 :                     m_options.nBlockMaxWeight - m_options.block_reserved_weight) {
     396                 :             :                 // Give up if we're close to full and haven't succeeded in a while
     397                 :             :                 break;
     398                 :             :             }
     399                 :       40067 :             continue;
     400                 :             :         }
     401                 :             : 
     402         [ +  - ]:        8164 :         auto ancestors{mempool.AssumeCalculateMemPoolAncestors(__func__, *iter, CTxMemPool::Limits::NoLimits(), /*fSearchForParents=*/false)};
     403                 :             : 
     404         [ +  - ]:        8164 :         onlyUnconfirmed(ancestors);
     405         [ +  - ]:        8164 :         ancestors.insert(iter);
     406                 :             : 
     407                 :             :         // Test if all tx's are Final
     408   [ +  -  +  + ]:        8164 :         if (!TestPackageTransactions(ancestors)) {
     409         [ -  + ]:           2 :             if (fUsingModified) {
     410                 :           0 :                 mapModifiedTx.get<ancestor_score>().erase(modit);
     411   [ #  #  #  # ]:           0 :                 failedTx.insert(iter->GetSharedTx()->GetHash());
     412                 :             :             }
     413                 :           2 :             continue;
     414                 :             :         }
     415                 :             : 
     416                 :             :         // This transaction will make it in; reset the failed counter.
     417                 :        8162 :         nConsecutiveFailed = 0;
     418                 :             : 
     419                 :             :         // Package can be added. Sort the entries in a valid order.
     420                 :        8162 :         std::vector<CTxMemPool::txiter> sortedEntries;
     421         [ +  - ]:        8162 :         SortForBlock(ancestors, sortedEntries);
     422                 :             : 
     423         [ +  + ]:       19202 :         for (size_t i = 0; i < sortedEntries.size(); ++i) {
     424         [ +  - ]:       11040 :             AddToBlock(sortedEntries[i]);
     425                 :             :             // Erase from the modified set, if present
     426                 :       11040 :             mapModifiedTx.erase(sortedEntries[i]);
     427                 :             :         }
     428                 :             : 
     429                 :        8162 :         ++nPackagesSelected;
     430         [ +  - ]:        8162 :         pblocktemplate->m_package_feerates.emplace_back(packageFees, static_cast<int32_t>(packageSize));
     431                 :             : 
     432                 :             :         // Update transactions that depend on each of these
     433         [ +  - ]:        8162 :         nDescendantsUpdated += UpdatePackagesForAdded(mempool, ancestors, mapModifiedTx);
     434                 :        8164 :     }
     435   [ +  -  +  - ]:       78300 : }
     436                 :             : } // namespace node
        

Generated by: LCOV version 2.0-1