LCOV - code coverage report
Current view: top level - src/node - miner.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 93.5 % 231 216
Test Date: 2024-09-01 05:20:30 Functions: 100.0 % 15 15
Branches: 51.7 % 406 210

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

Generated by: LCOV version 2.0-1