Bitcoin Core Fuzz Coverage Report for wallet_tx_can_be_bumped

Coverage Report

Created: 2025-11-19 11:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/Users/brunogarcia/projects/bitcoin-core-dev/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
#include <numeric>
33
34
namespace node {
35
36
int64_t GetMinimumTime(const CBlockIndex* pindexPrev, const int64_t difficulty_adjustment_interval)
37
0
{
38
0
    int64_t min_time{pindexPrev->GetMedianTimePast() + 1};
39
    // Height of block to be mined.
40
0
    const int height{pindexPrev->nHeight + 1};
41
    // Account for BIP94 timewarp rule on all networks. This makes future
42
    // activation safer.
43
0
    if (height % difficulty_adjustment_interval == 0) {
44
0
        min_time = std::max<int64_t>(min_time, pindexPrev->GetBlockTime() - MAX_TIMEWARP);
45
0
    }
46
0
    return min_time;
47
0
}
48
49
int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev)
50
0
{
51
0
    int64_t nOldTime = pblock->nTime;
52
0
    int64_t nNewTime{std::max<int64_t>(GetMinimumTime(pindexPrev, consensusParams.DifficultyAdjustmentInterval()),
53
0
                                       TicksSinceEpoch<std::chrono::seconds>(NodeClock::now()))};
54
55
0
    if (nOldTime < nNewTime) {
56
0
        pblock->nTime = nNewTime;
57
0
    }
58
59
    // Updating time can change work required on testnet:
60
0
    if (consensusParams.fPowAllowMinDifficultyBlocks) {
61
0
        pblock->nBits = GetNextWorkRequired(pindexPrev, pblock, consensusParams);
62
0
    }
63
64
0
    return nNewTime - nOldTime;
65
0
}
66
67
void RegenerateCommitments(CBlock& block, ChainstateManager& chainman)
68
0
{
69
0
    CMutableTransaction tx{*block.vtx.at(0)};
70
0
    tx.vout.erase(tx.vout.begin() + GetWitnessCommitmentIndex(block));
71
0
    block.vtx.at(0) = MakeTransactionRef(tx);
72
73
0
    const CBlockIndex* prev_block = WITH_LOCK(::cs_main, return chainman.m_blockman.LookupBlockIndex(block.hashPrevBlock));
Line
Count
Source
290
0
#define WITH_LOCK(cs, code) (MaybeCheckNotHeld(cs), [&]() -> decltype(auto) { LOCK(cs); code; }())
74
0
    chainman.GenerateCoinbaseCommitment(block, prev_block);
75
76
0
    block.hashMerkleRoot = BlockMerkleRoot(block);
77
0
}
78
79
static BlockAssembler::Options ClampOptions(BlockAssembler::Options options)
80
0
{
81
0
    options.block_reserved_weight = std::clamp<size_t>(options.block_reserved_weight, MINIMUM_BLOCK_RESERVED_WEIGHT, MAX_BLOCK_WEIGHT);
82
0
    options.coinbase_output_max_additional_sigops = std::clamp<size_t>(options.coinbase_output_max_additional_sigops, 0, MAX_BLOCK_SIGOPS_COST);
83
    // Limit weight to between block_reserved_weight and MAX_BLOCK_WEIGHT for sanity:
84
    // block_reserved_weight can safely exceed -blockmaxweight, but the rest of the block template will be empty.
85
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},
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")}) {
102
0
        if (const auto parsed{ParseMoney(*blockmintxfee)}) options.blockMinFeeRate = CFeeRate{*parsed};
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);
Line
Count
Source
259
0
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
Line
Count
Source
11
0
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
0
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
0
#define PASTE(x, y) x ## y
135
0
    CBlockIndex* pindexPrev = m_chainstate.m_chain.Tip();
136
0
    assert(pindexPrev != nullptr);
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()) {
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) {
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);
Line
Count
Source
113
0
#define Assert(val) inline_assertion_check<true>(val, std::source_location::current(), #val)
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);
Line
Count
Source
373
0
#define LogPrintf(...) LogInfo(__VA_ARGS__)
Line
Count
Source
368
0
#define LogInfo(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Info, /*should_ratelimit=*/true, __VA_ARGS__)
Line
Count
Source
362
0
#define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(std::source_location::current(), category, level, should_ratelimit, __VA_ARGS__)
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
    if (m_options.test_block_validity) {
183
0
        if (BlockValidationState state{TestBlockValidity(m_chainstate, *pblock, /*check_pow=*/false, /*check_merkle_root=*/false)}; !state.IsValid()) {
184
0
            throw std::runtime_error(strprintf("TestBlockValidity failed: %s", state.ToString()));
Line
Count
Source
1172
0
#define strprintf tfm::format
185
0
        }
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",
Line
Count
Source
393
0
#define LogDebug(category, ...) LogPrintLevel(category, BCLog::Level::Debug, __VA_ARGS__)
Line
Count
Source
385
0
    do {                                                              \
386
0
        if (LogAcceptCategory((category), (level))) {                 \
387
0
            bool rate_limit{level >= BCLog::Level::Info};             \
388
0
            LogPrintLevel_(category, level, rate_limit, __VA_ARGS__); \
Line
Count
Source
362
0
#define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(std::source_location::current(), category, level, should_ratelimit, __VA_ARGS__)
389
0
        }                                                             \
390
0
    } while (0)
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(); ) {
200
        // Only test txs not already in the block
201
0
        if (inBlock.count((*iit)->GetSharedTx()->GetHash())) {
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) {
213
0
        return false;
214
0
    }
215
0
    if (nBlockSigOpsCost + packageSigOpsCost >= MAX_BLOCK_SIGOPS_COST) {
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) {
226
0
        if (!IsFinalTx(it->GetTx(), nHeight, m_lock_time_cutoff)) {
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) {
245
0
        LogPrintf("fee rate %s txid %s\n",
Line
Count
Source
373
0
#define LogPrintf(...) LogInfo(__VA_ARGS__)
Line
Count
Source
368
0
#define LogInfo(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Info, /*should_ratelimit=*/true, __VA_ARGS__)
Line
Count
Source
362
0
#define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(std::source_location::current(), category, level, should_ratelimit, __VA_ARGS__)
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);
Line
Count
Source
137
0
#define AssertLockHeld(cs) AssertLockHeldInternal(#cs, __FILE__, __LINE__, &cs)
259
260
0
    int nDescendantsUpdated = 0;
261
0
    for (CTxMemPool::txiter it : alreadyAdded) {
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) {
266
0
            if (alreadyAdded.count(desc)) {
267
0
                continue;
268
0
            }
269
0
            ++nDescendantsUpdated;
270
0
            modtxiter mit = mapModifiedTx.find(desc);
271
0
            if (mit == mapModifiedTx.end()) {
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)};
Line
Count
Source
113
0
#define Assert(val) inline_assertion_check<true>(val, std::source_location::current(), #val)
305
0
    LOCK(mempool.cs);
Line
Count
Source
259
0
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
Line
Count
Source
11
0
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
0
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
0
#define PASTE(x, y) x ## y
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()) {
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()) {
338
0
            auto it = mempool.mapTx.project<0>(mi);
339
0
            assert(it != mempool.mapTx.end());
340
0
            if (mapModifiedTx.count(it) || inBlock.count(it->GetSharedTx()->GetHash()) || failedTx.count(it->GetSharedTx()->GetHash())) {
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()) {
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() &&
359
0
                    CompareTxMemPoolEntryByAncestorFee()(*modit, CTxMemPoolModifiedEntry(iter))) {
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()));
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) {
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)) {
386
            // Everything else we might consider has a lower fee rate
387
0
            return;
388
0
        }
389
390
0
        if (!TestPackage(packageSize, packageSigOpsCost)) {
391
0
            if (fUsingModified) {
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 +
402
0
                    BLOCK_FULL_ENOUGH_WEIGHT_DELTA > m_options.nBlockMaxWeight) {
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)) {
416
0
            if (fUsingModified) {
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) {
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) {
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
456
    // Reset cached checks
457
0
    block.m_checked_witness_commitment = false;
458
0
    block.m_checked_merkle_root = false;
459
0
    block.fChecked = false;
460
0
}
461
462
void InterruptWait(KernelNotifications& kernel_notifications, bool& interrupt_wait)
463
0
{
464
0
    LOCK(kernel_notifications.m_tip_block_mutex);
Line
Count
Source
259
0
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
Line
Count
Source
11
0
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
0
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
0
#define PASTE(x, y) x ## y
465
0
    interrupt_wait = true;
466
0
    kernel_notifications.m_tip_block_cv.notify_all();
467
0
}
468
469
std::unique_ptr<CBlockTemplate> WaitAndCreateNewBlock(ChainstateManager& chainman,
470
                                                      KernelNotifications& kernel_notifications,
471
                                                      CTxMemPool* mempool,
472
                                                      const std::unique_ptr<CBlockTemplate>& block_template,
473
                                                      const BlockWaitOptions& options,
474
                                                      const BlockAssembler::Options& assemble_options,
475
                                                      bool& interrupt_wait)
476
0
{
477
    // Delay calculating the current template fees, just in case a new block
478
    // comes in before the next tick.
479
0
    CAmount current_fees = -1;
480
481
    // Alternate waiting for a new tip and checking if fees have risen.
482
    // The latter check is expensive so we only run it once per second.
483
0
    auto now{NodeClock::now()};
484
0
    const auto deadline = now + options.timeout;
485
0
    const MillisecondsDouble tick{1000};
486
0
    const bool allow_min_difficulty{chainman.GetParams().GetConsensus().fPowAllowMinDifficultyBlocks};
487
488
0
    do {
489
0
        bool tip_changed{false};
490
0
        {
491
0
            WAIT_LOCK(kernel_notifications.m_tip_block_mutex, lock);
Line
Count
Source
265
0
#define WAIT_LOCK(cs, name) UniqueLock name(LOCK_ARGS(cs))
Line
Count
Source
263
0
#define LOCK_ARGS(cs) MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__
492
            // Note that wait_until() checks the predicate before waiting
493
0
            kernel_notifications.m_tip_block_cv.wait_until(lock, std::min(now + tick, deadline), [&]() EXCLUSIVE_LOCKS_REQUIRED(kernel_notifications.m_tip_block_mutex) {
494
0
                AssertLockHeld(kernel_notifications.m_tip_block_mutex);
Line
Count
Source
137
0
#define AssertLockHeld(cs) AssertLockHeldInternal(#cs, __FILE__, __LINE__, &cs)
495
0
                const auto tip_block{kernel_notifications.TipBlock()};
496
                // We assume tip_block is set, because this is an instance
497
                // method on BlockTemplate and no template could have been
498
                // generated before a tip exists.
499
0
                tip_changed = Assume(tip_block) && tip_block != block_template->block.hashPrevBlock;
Line
Count
Source
125
0
#define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val)
500
0
                return tip_changed || chainman.m_interrupt || interrupt_wait;
501
0
            });
502
0
            if (interrupt_wait) {
503
0
                interrupt_wait = false;
504
0
                return nullptr;
505
0
            }
506
0
        }
507
508
0
        if (chainman.m_interrupt) return nullptr;
509
        // At this point the tip changed, a full tick went by or we reached
510
        // the deadline.
511
512
        // Must release m_tip_block_mutex before locking cs_main, to avoid deadlocks.
513
0
        LOCK(::cs_main);
Line
Count
Source
259
0
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
Line
Count
Source
11
0
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
0
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
0
#define PASTE(x, y) x ## y
514
515
        // On test networks return a minimum difficulty block after 20 minutes
516
0
        if (!tip_changed && allow_min_difficulty) {
517
0
            const NodeClock::time_point tip_time{std::chrono::seconds{chainman.ActiveChain().Tip()->GetBlockTime()}};
518
0
            if (now > tip_time + 20min) {
519
0
                tip_changed = true;
520
0
            }
521
0
        }
522
523
        /**
524
         * We determine if fees increased compared to the previous template by generating
525
         * a fresh template. There may be more efficient ways to determine how much
526
         * (approximate) fees for the next block increased, perhaps more so after
527
         * Cluster Mempool.
528
         *
529
         * We'll also create a new template if the tip changed during this iteration.
530
         */
531
0
        if (options.fee_threshold < MAX_MONEY || tip_changed) {
532
0
            auto new_tmpl{BlockAssembler{
533
0
                chainman.ActiveChainstate(),
534
0
                mempool,
535
0
                assemble_options}
536
0
                              .CreateNewBlock()};
537
538
            // If the tip changed, return the new template regardless of its fees.
539
0
            if (tip_changed) return new_tmpl;
540
541
            // Calculate the original template total fees if we haven't already
542
0
            if (current_fees == -1) {
543
0
                current_fees = std::accumulate(block_template->vTxFees.begin(), block_template->vTxFees.end(), CAmount{0});
544
0
            }
545
546
            // Check if fees increased enough to return the new template
547
0
            const CAmount new_fees = std::accumulate(new_tmpl->vTxFees.begin(), new_tmpl->vTxFees.end(), CAmount{0});
548
0
            Assume(options.fee_threshold != MAX_MONEY);
Line
Count
Source
125
0
#define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val)
549
0
            if (new_fees >= current_fees + options.fee_threshold) return new_tmpl;
550
0
        }
551
552
0
        now = NodeClock::now();
553
0
    } while (now < deadline);
554
555
0
    return nullptr;
556
0
}
557
558
std::optional<BlockRef> GetTip(ChainstateManager& chainman)
559
0
{
560
0
    LOCK(::cs_main);
Line
Count
Source
259
0
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
Line
Count
Source
11
0
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
0
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
0
#define PASTE(x, y) x ## y
561
0
    CBlockIndex* tip{chainman.ActiveChain().Tip()};
562
0
    if (!tip) return {};
563
0
    return BlockRef{tip->GetBlockHash(), tip->nHeight};
564
0
}
565
566
std::optional<BlockRef> WaitTipChanged(ChainstateManager& chainman, KernelNotifications& kernel_notifications, const uint256& current_tip, MillisecondsDouble& timeout)
567
0
{
568
0
    Assume(timeout >= 0ms); // No internal callers should use a negative timeout
Line
Count
Source
125
0
#define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val)
569
0
    if (timeout < 0ms) timeout = 0ms;
570
0
    if (timeout > std::chrono::years{100}) timeout = std::chrono::years{100}; // Upper bound to avoid UB in std::chrono
571
0
    auto deadline{std::chrono::steady_clock::now() + timeout};
572
0
    {
573
0
        WAIT_LOCK(kernel_notifications.m_tip_block_mutex, lock);
Line
Count
Source
265
0
#define WAIT_LOCK(cs, name) UniqueLock name(LOCK_ARGS(cs))
Line
Count
Source
263
0
#define LOCK_ARGS(cs) MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__
574
        // For callers convenience, wait longer than the provided timeout
575
        // during startup for the tip to be non-null. That way this function
576
        // always returns valid tip information when possible and only
577
        // returns null when shutting down, not when timing out.
578
0
        kernel_notifications.m_tip_block_cv.wait(lock, [&]() EXCLUSIVE_LOCKS_REQUIRED(kernel_notifications.m_tip_block_mutex) {
579
0
            return kernel_notifications.TipBlock() || chainman.m_interrupt;
580
0
        });
581
0
        if (chainman.m_interrupt) return {};
582
        // At this point TipBlock is set, so continue to wait until it is
583
        // different then `current_tip` provided by caller.
584
0
        kernel_notifications.m_tip_block_cv.wait_until(lock, deadline, [&]() EXCLUSIVE_LOCKS_REQUIRED(kernel_notifications.m_tip_block_mutex) {
585
0
            return Assume(kernel_notifications.TipBlock()) != current_tip || chainman.m_interrupt;
Line
Count
Source
125
0
#define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val)
586
0
        });
587
0
    }
588
0
    if (chainman.m_interrupt) return {};
589
590
    // Must release m_tip_block_mutex before getTip() locks cs_main, to
591
    // avoid deadlocks.
592
0
    return GetTip(chainman);
593
0
}
594
} // namespace node