LCOV - code coverage report
Current view: top level - src/node - miner.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 98.6 % 208 205
Test Date: 2025-02-22 05:08:25 Functions: 100.0 % 16 16
Branches: 65.0 % 286 186

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

Generated by: LCOV version 2.0-1