Coverage Report

Created: 2025-06-10 13:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/bitcoin/src/node/miner.cpp
Line
Count
Source
1
// Copyright (c) 2009-2010 Satoshi Nakamoto
2
// Copyright (c) 2009-2022 The Bitcoin Core developers
3
// Distributed under the MIT software license, see the accompanying
4
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6
#include <node/miner.h>
7
8
#include <chain.h>
9
#include <chainparams.h>
10
#include <coins.h>
11
#include <common/args.h>
12
#include <consensus/amount.h>
13
#include <consensus/consensus.h>
14
#include <consensus/merkle.h>
15
#include <consensus/tx_verify.h>
16
#include <consensus/validation.h>
17
#include <deploymentstatus.h>
18
#include <logging.h>
19
#include <node/context.h>
20
#include <node/kernel_notifications.h>
21
#include <policy/feerate.h>
22
#include <policy/policy.h>
23
#include <pow.h>
24
#include <primitives/transaction.h>
25
#include <util/moneystr.h>
26
#include <util/signalinterrupt.h>
27
#include <util/time.h>
28
#include <validation.h>
29
30
#include <algorithm>
31
#include <utility>
32
33
namespace node {
34
35
int64_t GetMinimumTime(const CBlockIndex* pindexPrev, const int64_t difficulty_adjustment_interval)
36
0
{
37
0
    int64_t min_time{pindexPrev->GetMedianTimePast() + 1};
38
    // Height of block to be mined.
39
0
    const int height{pindexPrev->nHeight + 1};
40
    // Account for BIP94 timewarp rule on all networks. This makes future
41
    // activation safer.
42
0
    if (height % difficulty_adjustment_interval == 0) {
  Branch (42:9): [True: 0, False: 0]
43
0
        min_time = std::max<int64_t>(min_time, pindexPrev->GetBlockTime() - MAX_TIMEWARP);
44
0
    }
45
0
    return min_time;
46
0
}
47
48
int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev)
49
0
{
50
0
    int64_t nOldTime = pblock->nTime;
51
0
    int64_t nNewTime{std::max<int64_t>(GetMinimumTime(pindexPrev, consensusParams.DifficultyAdjustmentInterval()),
52
0
                                       TicksSinceEpoch<std::chrono::seconds>(NodeClock::now()))};
53
54
0
    if (nOldTime < nNewTime) {
  Branch (54:9): [True: 0, False: 0]
55
0
        pblock->nTime = nNewTime;
56
0
    }
57
58
    // Updating time can change work required on testnet:
59
0
    if (consensusParams.fPowAllowMinDifficultyBlocks) {
  Branch (59:9): [True: 0, False: 0]
60
0
        pblock->nBits = GetNextWorkRequired(pindexPrev, pblock, consensusParams);
61
0
    }
62
63
0
    return nNewTime - nOldTime;
64
0
}
65
66
void RegenerateCommitments(CBlock& block, ChainstateManager& chainman)
67
0
{
68
0
    CMutableTransaction tx{*block.vtx.at(0)};
69
0
    tx.vout.erase(tx.vout.begin() + GetWitnessCommitmentIndex(block));
70
0
    block.vtx.at(0) = MakeTransactionRef(tx);
71
72
0
    const CBlockIndex* prev_block = WITH_LOCK(::cs_main, return chainman.m_blockman.LookupBlockIndex(block.hashPrevBlock));
73
0
    chainman.GenerateCoinbaseCommitment(block, prev_block);
74
75
0
    block.hashMerkleRoot = BlockMerkleRoot(block);
76
0
}
77
78
static BlockAssembler::Options ClampOptions(BlockAssembler::Options options)
79
0
{
80
0
    Assert(options.block_reserved_weight <= MAX_BLOCK_WEIGHT);
81
0
    Assert(options.block_reserved_weight >= MINIMUM_BLOCK_RESERVED_WEIGHT);
82
0
    Assert(options.coinbase_output_max_additional_sigops <= MAX_BLOCK_SIGOPS_COST);
83
    // Limit weight to between block_reserved_weight and MAX_BLOCK_WEIGHT for sanity:
84
    // block_reserved_weight can safely exceed -blockmaxweight, but the rest of the block template will be empty.
85
0
    options.nBlockMaxWeight = std::clamp<size_t>(options.nBlockMaxWeight, options.block_reserved_weight, MAX_BLOCK_WEIGHT);
86
0
    return options;
87
0
}
88
89
BlockAssembler::BlockAssembler(Chainstate& chainstate, const CTxMemPool* mempool, const Options& options)
90
0
    : chainparams{chainstate.m_chainman.GetParams()},
91
0
      m_mempool{options.use_mempool ? mempool : nullptr},
  Branch (91:17): [True: 0, False: 0]
92
0
      m_chainstate{chainstate},
93
0
      m_options{ClampOptions(options)}
94
0
{
95
0
}
96
97
void ApplyArgsManOptions(const ArgsManager& args, BlockAssembler::Options& options)
98
0
{
99
    // Block resource limits
100
0
    options.nBlockMaxWeight = args.GetIntArg("-blockmaxweight", options.nBlockMaxWeight);
101
0
    if (const auto blockmintxfee{args.GetArg("-blockmintxfee")}) {
  Branch (101:20): [True: 0, False: 0]
102
0
        if (const auto parsed{ParseMoney(*blockmintxfee)}) options.blockMinFeeRate = CFeeRate{*parsed};
  Branch (102:24): [True: 0, False: 0]
103
0
    }
104
0
    options.print_modified_fee = args.GetBoolArg("-printpriority", options.print_modified_fee);
105
0
    options.block_reserved_weight = args.GetIntArg("-blockreservedweight", options.block_reserved_weight);
106
0
}
107
108
void BlockAssembler::resetBlock()
109
0
{
110
0
    inBlock.clear();
111
112
    // Reserve space for fixed-size block header, txs count, and coinbase tx.
113
0
    nBlockWeight = m_options.block_reserved_weight;
114
0
    nBlockSigOpsCost = m_options.coinbase_output_max_additional_sigops;
115
116
    // These counters do not include coinbase tx
117
0
    nBlockTx = 0;
118
0
    nFees = 0;
119
0
}
120
121
std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock()
122
0
{
123
0
    const auto time_start{SteadyClock::now()};
124
125
0
    resetBlock();
126
127
0
    pblocktemplate.reset(new CBlockTemplate());
128
0
    CBlock* const pblock = &pblocktemplate->block; // pointer for convenience
129
130
    // Add dummy coinbase tx as first transaction. It is skipped by the
131
    // getblocktemplate RPC and mining interface consumers must not use it.
132
0
    pblock->vtx.emplace_back();
133
134
0
    LOCK(::cs_main);
135
0
    CBlockIndex* pindexPrev = m_chainstate.m_chain.Tip();
136
0
    assert(pindexPrev != nullptr);
  Branch (136:5): [True: 0, False: 0]
137
0
    nHeight = pindexPrev->nHeight + 1;
138
139
0
    pblock->nVersion = m_chainstate.m_chainman.m_versionbitscache.ComputeBlockVersion(pindexPrev, chainparams.GetConsensus());
140
    // -regtest only: allow overriding block.nVersion with
141
    // -blockversion=N to test forking scenarios
142
0
    if (chainparams.MineBlocksOnDemand()) {
  Branch (142:9): [True: 0, False: 0]
143
0
        pblock->nVersion = gArgs.GetIntArg("-blockversion", pblock->nVersion);
144
0
    }
145
146
0
    pblock->nTime = TicksSinceEpoch<std::chrono::seconds>(NodeClock::now());
147
0
    m_lock_time_cutoff = pindexPrev->GetMedianTimePast();
148
149
0
    int nPackagesSelected = 0;
150
0
    int nDescendantsUpdated = 0;
151
0
    if (m_mempool) {
  Branch (151:9): [True: 0, False: 0]
152
0
        addPackageTxs(nPackagesSelected, nDescendantsUpdated);
153
0
    }
154
155
0
    const auto time_1{SteadyClock::now()};
156
157
0
    m_last_block_num_txs = nBlockTx;
158
0
    m_last_block_weight = nBlockWeight;
159
160
    // Create coinbase transaction.
161
0
    CMutableTransaction coinbaseTx;
162
0
    coinbaseTx.vin.resize(1);
163
0
    coinbaseTx.vin[0].prevout.SetNull();
164
0
    coinbaseTx.vin[0].nSequence = CTxIn::MAX_SEQUENCE_NONFINAL; // Make sure timelock is enforced.
165
0
    coinbaseTx.vout.resize(1);
166
0
    coinbaseTx.vout[0].scriptPubKey = m_options.coinbase_output_script;
167
0
    coinbaseTx.vout[0].nValue = nFees + GetBlockSubsidy(nHeight, chainparams.GetConsensus());
168
0
    coinbaseTx.vin[0].scriptSig = CScript() << nHeight << OP_0;
169
0
    Assert(nHeight > 0);
170
0
    coinbaseTx.nLockTime = static_cast<uint32_t>(nHeight - 1);
171
0
    pblock->vtx[0] = MakeTransactionRef(std::move(coinbaseTx));
172
0
    pblocktemplate->vchCoinbaseCommitment = m_chainstate.m_chainman.GenerateCoinbaseCommitment(*pblock, pindexPrev);
173
174
0
    LogPrintf("CreateNewBlock(): block weight: %u txs: %u fees: %ld sigops %d\n", GetBlockWeight(*pblock), nBlockTx, nFees, nBlockSigOpsCost);
175
176
    // Fill in header
177
0
    pblock->hashPrevBlock  = pindexPrev->GetBlockHash();
178
0
    UpdateTime(pblock, chainparams.GetConsensus(), pindexPrev);
179
0
    pblock->nBits          = GetNextWorkRequired(pindexPrev, pblock, chainparams.GetConsensus());
180
0
    pblock->nNonce         = 0;
181
182
0
    BlockValidationState state;
183
0
    if (m_options.test_block_validity && !TestBlockValidity(state, chainparams, m_chainstate, *pblock, pindexPrev,
  Branch (183:9): [True: 0, False: 0]
  Branch (183:42): [True: 0, False: 0]
184
0
                                                            /*fCheckPOW=*/false, /*fCheckMerkleRoot=*/false)) {
185
0
        throw std::runtime_error(strprintf("%s: TestBlockValidity failed: %s", __func__, state.ToString()));
186
0
    }
187
0
    const auto time_2{SteadyClock::now()};
188
189
0
    LogDebug(BCLog::BENCH, "CreateNewBlock() packages: %.2fms (%d packages, %d updated descendants), validity: %.2fms (total %.2fms)\n",
190
0
             Ticks<MillisecondsDouble>(time_1 - time_start), nPackagesSelected, nDescendantsUpdated,
191
0
             Ticks<MillisecondsDouble>(time_2 - time_1),
192
0
             Ticks<MillisecondsDouble>(time_2 - time_start));
193
194
0
    return std::move(pblocktemplate);
195
0
}
196
197
void BlockAssembler::onlyUnconfirmed(CTxMemPool::setEntries& testSet)
198
0
{
199
0
    for (CTxMemPool::setEntries::iterator iit = testSet.begin(); iit != testSet.end(); ) {
  Branch (199:66): [True: 0, False: 0]
200
        // Only test txs not already in the block
201
0
        if (inBlock.count((*iit)->GetSharedTx()->GetHash())) {
  Branch (201:13): [True: 0, False: 0]
202
0
            testSet.erase(iit++);
203
0
        } else {
204
0
            iit++;
205
0
        }
206
0
    }
207
0
}
208
209
bool BlockAssembler::TestPackage(uint64_t packageSize, int64_t packageSigOpsCost) const
210
0
{
211
    // TODO: switch to weight-based accounting for packages instead of vsize-based accounting.
212
0
    if (nBlockWeight + WITNESS_SCALE_FACTOR * packageSize >= m_options.nBlockMaxWeight) {
  Branch (212:9): [True: 0, False: 0]
213
0
        return false;
214
0
    }
215
0
    if (nBlockSigOpsCost + packageSigOpsCost >= MAX_BLOCK_SIGOPS_COST) {
  Branch (215:9): [True: 0, False: 0]
216
0
        return false;
217
0
    }
218
0
    return true;
219
0
}
220
221
// Perform transaction-level checks before adding to block:
222
// - transaction finality (locktime)
223
bool BlockAssembler::TestPackageTransactions(const CTxMemPool::setEntries& package) const
224
0
{
225
0
    for (CTxMemPool::txiter it : package) {
  Branch (225:32): [True: 0, False: 0]
226
0
        if (!IsFinalTx(it->GetTx(), nHeight, m_lock_time_cutoff)) {
  Branch (226:13): [True: 0, False: 0]
227
0
            return false;
228
0
        }
229
0
    }
230
0
    return true;
231
0
}
232
233
void BlockAssembler::AddToBlock(CTxMemPool::txiter iter)
234
0
{
235
0
    pblocktemplate->block.vtx.emplace_back(iter->GetSharedTx());
236
0
    pblocktemplate->vTxFees.push_back(iter->GetFee());
237
0
    pblocktemplate->vTxSigOpsCost.push_back(iter->GetSigOpCost());
238
0
    nBlockWeight += iter->GetTxWeight();
239
0
    ++nBlockTx;
240
0
    nBlockSigOpsCost += iter->GetSigOpCost();
241
0
    nFees += iter->GetFee();
242
0
    inBlock.insert(iter->GetSharedTx()->GetHash());
243
244
0
    if (m_options.print_modified_fee) {
  Branch (244:9): [True: 0, False: 0]
245
0
        LogPrintf("fee rate %s txid %s\n",
246
0
                  CFeeRate(iter->GetModifiedFee(), iter->GetTxSize()).ToString(),
247
0
                  iter->GetTx().GetHash().ToString());
248
0
    }
249
0
}
250
251
/** Add descendants of given transactions to mapModifiedTx with ancestor
252
 * state updated assuming given transactions are inBlock. Returns number
253
 * of updated descendants. */
254
static int UpdatePackagesForAdded(const CTxMemPool& mempool,
255
                                  const CTxMemPool::setEntries& alreadyAdded,
256
                                  indexed_modified_transaction_set& mapModifiedTx) EXCLUSIVE_LOCKS_REQUIRED(mempool.cs)
257
0
{
258
0
    AssertLockHeld(mempool.cs);
259
260
0
    int nDescendantsUpdated = 0;
261
0
    for (CTxMemPool::txiter it : alreadyAdded) {
  Branch (261:32): [True: 0, False: 0]
262
0
        CTxMemPool::setEntries descendants;
263
0
        mempool.CalculateDescendants(it, descendants);
264
        // Insert all descendants (not yet in block) into the modified set
265
0
        for (CTxMemPool::txiter desc : descendants) {
  Branch (265:38): [True: 0, False: 0]
266
0
            if (alreadyAdded.count(desc)) {
  Branch (266:17): [True: 0, False: 0]
267
0
                continue;
268
0
            }
269
0
            ++nDescendantsUpdated;
270
0
            modtxiter mit = mapModifiedTx.find(desc);
271
0
            if (mit == mapModifiedTx.end()) {
  Branch (271:17): [True: 0, False: 0]
272
0
                CTxMemPoolModifiedEntry modEntry(desc);
273
0
                mit = mapModifiedTx.insert(modEntry).first;
274
0
            }
275
0
            mapModifiedTx.modify(mit, update_for_parent_inclusion(it));
276
0
        }
277
0
    }
278
0
    return nDescendantsUpdated;
279
0
}
280
281
void BlockAssembler::SortForBlock(const CTxMemPool::setEntries& package, std::vector<CTxMemPool::txiter>& sortedEntries)
282
0
{
283
    // Sort package by ancestor count
284
    // If a transaction A depends on transaction B, then A's ancestor count
285
    // must be greater than B's.  So this is sufficient to validly order the
286
    // transactions for block inclusion.
287
0
    sortedEntries.clear();
288
0
    sortedEntries.insert(sortedEntries.begin(), package.begin(), package.end());
289
0
    std::sort(sortedEntries.begin(), sortedEntries.end(), CompareTxIterByAncestorCount());
290
0
}
291
292
// This transaction selection algorithm orders the mempool based
293
// on feerate of a transaction including all unconfirmed ancestors.
294
// Since we don't remove transactions from the mempool as we select them
295
// for block inclusion, we need an alternate method of updating the feerate
296
// of a transaction with its not-yet-selected ancestors as we go.
297
// This is accomplished by walking the in-mempool descendants of selected
298
// transactions and storing a temporary modified state in mapModifiedTxs.
299
// Each time through the loop, we compare the best transaction in
300
// mapModifiedTxs with the next transaction in the mempool to decide what
301
// transaction package to work on next.
302
void BlockAssembler::addPackageTxs(int& nPackagesSelected, int& nDescendantsUpdated)
303
0
{
304
0
    const auto& mempool{*Assert(m_mempool)};
305
0
    LOCK(mempool.cs);
306
307
    // mapModifiedTx will store sorted packages after they are modified
308
    // because some of their txs are already in the block
309
0
    indexed_modified_transaction_set mapModifiedTx;
310
    // Keep track of entries that failed inclusion, to avoid duplicate work
311
0
    std::set<Txid> failedTx;
312
313
0
    CTxMemPool::indexed_transaction_set::index<ancestor_score>::type::iterator mi = mempool.mapTx.get<ancestor_score>().begin();
314
0
    CTxMemPool::txiter iter;
315
316
    // Limit the number of attempts to add transactions to the block when it is
317
    // close to full; this is just a simple heuristic to finish quickly if the
318
    // mempool has a lot of entries.
319
0
    const int64_t MAX_CONSECUTIVE_FAILURES = 1000;
320
0
    constexpr int32_t BLOCK_FULL_ENOUGH_WEIGHT_DELTA = 4000;
321
0
    int64_t nConsecutiveFailed = 0;
322
323
0
    while (mi != mempool.mapTx.get<ancestor_score>().end() || !mapModifiedTx.empty()) {
  Branch (323:12): [True: 0, False: 0]
  Branch (323:12): [True: 0, False: 0]
  Branch (323:63): [True: 0, False: 0]
324
        // First try to find a new transaction in mapTx to evaluate.
325
        //
326
        // Skip entries in mapTx that are already in a block or are present
327
        // in mapModifiedTx (which implies that the mapTx ancestor state is
328
        // stale due to ancestor inclusion in the block)
329
        // Also skip transactions that we've already failed to add. This can happen if
330
        // we consider a transaction in mapModifiedTx and it fails: we can then
331
        // potentially consider it again while walking mapTx.  It's currently
332
        // guaranteed to fail again, but as a belt-and-suspenders check we put it in
333
        // failedTx and avoid re-evaluation, since the re-evaluation would be using
334
        // cached size/sigops/fee values that are not actually correct.
335
        /** Return true if given transaction from mapTx has already been evaluated,
336
         * or if the transaction's cached data in mapTx is incorrect. */
337
0
        if (mi != mempool.mapTx.get<ancestor_score>().end()) {
  Branch (337:13): [True: 0, False: 0]
338
0
            auto it = mempool.mapTx.project<0>(mi);
339
0
            assert(it != mempool.mapTx.end());
  Branch (339:13): [True: 0, False: 0]
340
0
            if (mapModifiedTx.count(it) || inBlock.count(it->GetSharedTx()->GetHash()) || failedTx.count(it->GetSharedTx()->GetHash())) {
  Branch (340:17): [True: 0, False: 0]
  Branch (340:17): [True: 0, False: 0]
  Branch (340:44): [True: 0, False: 0]
  Branch (340:91): [True: 0, False: 0]
341
0
                ++mi;
342
0
                continue;
343
0
            }
344
0
        }
345
346
        // Now that mi is not stale, determine which transaction to evaluate:
347
        // the next entry from mapTx, or the best from mapModifiedTx?
348
0
        bool fUsingModified = false;
349
350
0
        modtxscoreiter modit = mapModifiedTx.get<ancestor_score>().begin();
351
0
        if (mi == mempool.mapTx.get<ancestor_score>().end()) {
  Branch (351:13): [True: 0, False: 0]
352
            // We're out of entries in mapTx; use the entry from mapModifiedTx
353
0
            iter = modit->iter;
354
0
            fUsingModified = true;
355
0
        } else {
356
            // Try to compare the mapTx entry to the mapModifiedTx entry
357
0
            iter = mempool.mapTx.project<0>(mi);
358
0
            if (modit != mapModifiedTx.get<ancestor_score>().end() &&
  Branch (358:17): [True: 0, False: 0]
  Branch (358:17): [True: 0, False: 0]
359
0
                    CompareTxMemPoolEntryByAncestorFee()(*modit, CTxMemPoolModifiedEntry(iter))) {
  Branch (359:21): [True: 0, False: 0]
360
                // The best entry in mapModifiedTx has higher score
361
                // than the one from mapTx.
362
                // Switch which transaction (package) to consider
363
0
                iter = modit->iter;
364
0
                fUsingModified = true;
365
0
            } else {
366
                // Either no entry in mapModifiedTx, or it's worse than mapTx.
367
                // Increment mi for the next loop iteration.
368
0
                ++mi;
369
0
            }
370
0
        }
371
372
        // We skip mapTx entries that are inBlock, and mapModifiedTx shouldn't
373
        // contain anything that is inBlock.
374
0
        assert(!inBlock.count(iter->GetSharedTx()->GetHash()));
  Branch (374:9): [True: 0, False: 0]
375
376
0
        uint64_t packageSize = iter->GetSizeWithAncestors();
377
0
        CAmount packageFees = iter->GetModFeesWithAncestors();
378
0
        int64_t packageSigOpsCost = iter->GetSigOpCostWithAncestors();
379
0
        if (fUsingModified) {
  Branch (379:13): [True: 0, False: 0]
380
0
            packageSize = modit->nSizeWithAncestors;
381
0
            packageFees = modit->nModFeesWithAncestors;
382
0
            packageSigOpsCost = modit->nSigOpCostWithAncestors;
383
0
        }
384
385
0
        if (packageFees < m_options.blockMinFeeRate.GetFee(packageSize)) {
  Branch (385:13): [True: 0, False: 0]
386
            // Everything else we might consider has a lower fee rate
387
0
            return;
388
0
        }
389
390
0
        if (!TestPackage(packageSize, packageSigOpsCost)) {
  Branch (390:13): [True: 0, False: 0]
391
0
            if (fUsingModified) {
  Branch (391:17): [True: 0, False: 0]
392
                // Since we always look at the best entry in mapModifiedTx,
393
                // we must erase failed entries so that we can consider the
394
                // next best entry on the next loop iteration
395
0
                mapModifiedTx.get<ancestor_score>().erase(modit);
396
0
                failedTx.insert(iter->GetSharedTx()->GetHash());
397
0
            }
398
399
0
            ++nConsecutiveFailed;
400
401
0
            if (nConsecutiveFailed > MAX_CONSECUTIVE_FAILURES && nBlockWeight >
  Branch (401:17): [True: 0, False: 0]
  Branch (401:66): [True: 0, False: 0]
402
0
                    m_options.nBlockMaxWeight - BLOCK_FULL_ENOUGH_WEIGHT_DELTA) {
403
                // Give up if we're close to full and haven't succeeded in a while
404
0
                break;
405
0
            }
406
0
            continue;
407
0
        }
408
409
0
        auto ancestors{mempool.AssumeCalculateMemPoolAncestors(__func__, *iter, CTxMemPool::Limits::NoLimits(), /*fSearchForParents=*/false)};
410
411
0
        onlyUnconfirmed(ancestors);
412
0
        ancestors.insert(iter);
413
414
        // Test if all tx's are Final
415
0
        if (!TestPackageTransactions(ancestors)) {
  Branch (415:13): [True: 0, False: 0]
416
0
            if (fUsingModified) {
  Branch (416:17): [True: 0, False: 0]
417
0
                mapModifiedTx.get<ancestor_score>().erase(modit);
418
0
                failedTx.insert(iter->GetSharedTx()->GetHash());
419
0
            }
420
0
            continue;
421
0
        }
422
423
        // This transaction will make it in; reset the failed counter.
424
0
        nConsecutiveFailed = 0;
425
426
        // Package can be added. Sort the entries in a valid order.
427
0
        std::vector<CTxMemPool::txiter> sortedEntries;
428
0
        SortForBlock(ancestors, sortedEntries);
429
430
0
        for (size_t i = 0; i < sortedEntries.size(); ++i) {
  Branch (430:28): [True: 0, False: 0]
431
0
            AddToBlock(sortedEntries[i]);
432
            // Erase from the modified set, if present
433
0
            mapModifiedTx.erase(sortedEntries[i]);
434
0
        }
435
436
0
        ++nPackagesSelected;
437
0
        pblocktemplate->m_package_feerates.emplace_back(packageFees, static_cast<int32_t>(packageSize));
438
439
        // Update transactions that depend on each of these
440
0
        nDescendantsUpdated += UpdatePackagesForAdded(mempool, ancestors, mapModifiedTx);
441
0
    }
442
0
}
443
444
void AddMerkleRootAndCoinbase(CBlock& block, CTransactionRef coinbase, uint32_t version, uint32_t timestamp, uint32_t nonce)
445
0
{
446
0
    if (block.vtx.size() == 0) {
  Branch (446:9): [True: 0, False: 0]
447
0
        block.vtx.emplace_back(coinbase);
448
0
    } else {
449
0
        block.vtx[0] = coinbase;
450
0
    }
451
0
    block.nVersion = version;
452
0
    block.nTime = timestamp;
453
0
    block.nNonce = nonce;
454
0
    block.hashMerkleRoot = BlockMerkleRoot(block);
455
0
}
456
457
std::unique_ptr<CBlockTemplate> WaitAndCreateNewBlock(ChainstateManager& chainman,
458
                                                      KernelNotifications& kernel_notifications,
459
                                                      CTxMemPool* mempool,
460
                                                      const std::unique_ptr<CBlockTemplate>& block_template,
461
                                                      const BlockWaitOptions& options,
462
                                                      const BlockAssembler::Options& assemble_options)
463
0
{
464
    // Delay calculating the current template fees, just in case a new block
465
    // comes in before the next tick.
466
0
    CAmount current_fees = -1;
467
468
    // Alternate waiting for a new tip and checking if fees have risen.
469
    // The latter check is expensive so we only run it once per second.
470
0
    auto now{NodeClock::now()};
471
0
    const auto deadline = now + options.timeout;
472
0
    const MillisecondsDouble tick{1000};
473
0
    const bool allow_min_difficulty{chainman.GetParams().GetConsensus().fPowAllowMinDifficultyBlocks};
474
475
0
    do {
476
0
        bool tip_changed{false};
477
0
        {
478
0
            WAIT_LOCK(kernel_notifications.m_tip_block_mutex, lock);
479
            // Note that wait_until() checks the predicate before waiting
480
0
            kernel_notifications.m_tip_block_cv.wait_until(lock, std::min(now + tick, deadline), [&]() EXCLUSIVE_LOCKS_REQUIRED(kernel_notifications.m_tip_block_mutex) {
481
0
                AssertLockHeld(kernel_notifications.m_tip_block_mutex);
482
0
                const auto tip_block{kernel_notifications.TipBlock()};
483
                // We assume tip_block is set, because this is an instance
484
                // method on BlockTemplate and no template could have been
485
                // generated before a tip exists.
486
0
                tip_changed = Assume(tip_block) && tip_block != block_template->block.hashPrevBlock;
  Branch (486:52): [True: 0, False: 0]
487
0
                return tip_changed || chainman.m_interrupt;
  Branch (487:24): [True: 0, False: 0]
  Branch (487:39): [True: 0, False: 0]
488
0
            });
489
0
        }
490
491
0
        if (chainman.m_interrupt) return nullptr;
  Branch (491:13): [True: 0, False: 0]
492
        // At this point the tip changed, a full tick went by or we reached
493
        // the deadline.
494
495
        // Must release m_tip_block_mutex before locking cs_main, to avoid deadlocks.
496
0
        LOCK(::cs_main);
497
498
        // On test networks return a minimum difficulty block after 20 minutes
499
0
        if (!tip_changed && allow_min_difficulty) {
  Branch (499:13): [True: 0, False: 0]
  Branch (499:29): [True: 0, False: 0]
500
0
            const NodeClock::time_point tip_time{std::chrono::seconds{chainman.ActiveChain().Tip()->GetBlockTime()}};
501
0
            if (now > tip_time + 20min) {
  Branch (501:17): [True: 0, False: 0]
502
0
                tip_changed = true;
503
0
            }
504
0
        }
505
506
        /**
507
         * We determine if fees increased compared to the previous template by generating
508
         * a fresh template. There may be more efficient ways to determine how much
509
         * (approximate) fees for the next block increased, perhaps more so after
510
         * Cluster Mempool.
511
         *
512
         * We'll also create a new template if the tip changed during this iteration.
513
         */
514
0
        if (options.fee_threshold < MAX_MONEY || tip_changed) {
  Branch (514:13): [True: 0, False: 0]
  Branch (514:50): [True: 0, False: 0]
515
0
            auto new_tmpl{BlockAssembler{
516
0
                chainman.ActiveChainstate(),
517
0
                mempool,
518
0
                assemble_options}
519
0
                              .CreateNewBlock()};
520
521
            // If the tip changed, return the new template regardless of its fees.
522
0
            if (tip_changed) return new_tmpl;
  Branch (522:17): [True: 0, False: 0]
523
524
            // Calculate the original template total fees if we haven't already
525
0
            if (current_fees == -1) {
  Branch (525:17): [True: 0, False: 0]
526
0
                current_fees = 0;
527
0
                for (CAmount fee : block_template->vTxFees) {
  Branch (527:34): [True: 0, False: 0]
528
0
                    current_fees += fee;
529
0
                }
530
0
            }
531
532
0
            CAmount new_fees = 0;
533
0
            for (CAmount fee : new_tmpl->vTxFees) {
  Branch (533:30): [True: 0, False: 0]
534
0
                new_fees += fee;
535
0
                Assume(options.fee_threshold != MAX_MONEY);
536
0
                if (new_fees >= current_fees + options.fee_threshold) return new_tmpl;
  Branch (536:21): [True: 0, False: 0]
537
0
            }
538
0
        }
539
540
0
        now = NodeClock::now();
541
0
    } while (now < deadline);
  Branch (541:14): [True: 0, False: 0]
542
543
0
    return nullptr;
544
0
}
545
546
std::optional<BlockRef> GetTip(ChainstateManager& chainman)
547
0
{
548
0
    LOCK(::cs_main);
549
0
    CBlockIndex* tip{chainman.ActiveChain().Tip()};
550
0
    if (!tip) return {};
  Branch (550:9): [True: 0, False: 0]
551
0
    return BlockRef{tip->GetBlockHash(), tip->nHeight};
552
0
}
553
554
std::optional<BlockRef> WaitTipChanged(ChainstateManager& chainman, KernelNotifications& kernel_notifications, const uint256& current_tip, MillisecondsDouble& timeout)
555
0
{
556
0
    Assume(timeout >= 0ms); // No internal callers should use a negative timeout
557
0
    if (timeout < 0ms) timeout = 0ms;
  Branch (557:9): [True: 0, False: 0]
558
0
    if (timeout > std::chrono::years{100}) timeout = std::chrono::years{100}; // Upper bound to avoid UB in std::chrono
  Branch (558:9): [True: 0, False: 0]
559
0
    auto deadline{std::chrono::steady_clock::now() + timeout};
560
0
    {
561
0
        WAIT_LOCK(kernel_notifications.m_tip_block_mutex, lock);
562
        // For callers convenience, wait longer than the provided timeout
563
        // during startup for the tip to be non-null. That way this function
564
        // always returns valid tip information when possible and only
565
        // returns null when shutting down, not when timing out.
566
0
        kernel_notifications.m_tip_block_cv.wait(lock, [&]() EXCLUSIVE_LOCKS_REQUIRED(kernel_notifications.m_tip_block_mutex) {
567
0
            return kernel_notifications.TipBlock() || chainman.m_interrupt;
  Branch (567:20): [True: 0, False: 0]
  Branch (567:55): [True: 0, False: 0]
568
0
        });
569
0
        if (chainman.m_interrupt) return {};
  Branch (569:13): [True: 0, False: 0]
570
        // At this point TipBlock is set, so continue to wait until it is
571
        // different then `current_tip` provided by caller.
572
0
        kernel_notifications.m_tip_block_cv.wait_until(lock, deadline, [&]() EXCLUSIVE_LOCKS_REQUIRED(kernel_notifications.m_tip_block_mutex) {
573
0
            return Assume(kernel_notifications.TipBlock()) != current_tip || chainman.m_interrupt;
  Branch (573:20): [True: 0, False: 0]
  Branch (573:78): [True: 0, False: 0]
574
0
        });
575
0
    }
576
0
    if (chainman.m_interrupt) return {};
  Branch (576:9): [True: 0, False: 0]
577
578
    // Must release m_tip_block_mutex before getTip() locks cs_main, to
579
    // avoid deadlocks.
580
0
    return GetTip(chainman);
581
0
}
582
} // namespace node