Coverage Report

Created: 2026-09-01 13:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/bitcoin/src/validation.cpp
Line
Count
Source
1
// Copyright (c) 2009-2010 Satoshi Nakamoto
2
// Copyright (c) 2009-present 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 <bitcoin-build-config.h> // IWYU pragma: keep
7
8
#include <validation.h>
9
10
#include <arith_uint256.h>
11
#include <chain.h>
12
#include <checkqueue.h>
13
#include <clientversion.h>
14
#include <consensus/amount.h>
15
#include <consensus/consensus.h>
16
#include <consensus/merkle.h>
17
#include <consensus/tx_check.h>
18
#include <consensus/tx_verify.h>
19
#include <consensus/validation.h>
20
#include <cuckoocache.h>
21
#include <flatfile.h>
22
#include <hash.h>
23
#include <kernel/chainparams.h>
24
#include <kernel/coinstats.h>
25
#include <kernel/disconnected_transactions.h>
26
#include <kernel/mempool_entry.h>
27
#include <kernel/messagestartchars.h>
28
#include <kernel/notifications_interface.h>
29
#include <kernel/types.h>
30
#include <kernel/warning.h>
31
#include <logging/timer.h>
32
#include <node/blockstorage.h>
33
#include <node/utxo_snapshot.h>
34
#include <policy/ephemeral_policy.h>
35
#include <policy/policy.h>
36
#include <policy/rbf.h>
37
#include <policy/settings.h>
38
#include <policy/truc_policy.h>
39
#include <pow.h>
40
#include <primitives/block.h>
41
#include <primitives/transaction.h>
42
#include <random.h>
43
#include <script/script.h>
44
#include <script/sigcache.h>
45
#include <signet.h>
46
#include <tinyformat.h>
47
#include <txdb.h>
48
#include <txmempool.h>
49
#include <uint256.h>
50
#include <undo.h>
51
#include <util/byte_units.h>
52
#include <util/check.h>
53
#include <util/fs.h>
54
#include <util/fs_helpers.h>
55
#include <util/hasher.h>
56
#include <util/log.h>
57
#include <util/moneystr.h>
58
#include <util/rbf.h>
59
#include <util/result.h>
60
#include <util/signalinterrupt.h>
61
#include <util/strencodings.h>
62
#include <util/string.h>
63
#include <util/threadpool.h>
64
#include <util/time.h>
65
#include <util/trace.h>
66
#include <util/translation.h>
67
#include <validationinterface.h>
68
69
#include <algorithm>
70
#include <cassert>
71
#include <chrono>
72
#include <deque>
73
#include <numeric>
74
#include <optional>
75
#include <ranges>
76
#include <span>
77
#include <string>
78
#include <tuple>
79
#include <utility>
80
81
using kernel::CCoinsStats;
82
using kernel::ChainstateRole;
83
using kernel::CoinStatsHashType;
84
using kernel::ComputeUTXOStats;
85
using kernel::Notifications;
86
87
using fsbridge::FopenFn;
88
using node::BlockManager;
89
using node::BlockMap;
90
using node::CBlockIndexHeightOnlyComparator;
91
using node::CBlockIndexWorkComparator;
92
using node::SnapshotMetadata;
93
94
/** Time window to wait between writing blocks/block index and chainstate to disk.
95
 *  Randomize writing time inside the window to prevent a situation where the
96
 *  network over time settles into a few cohorts of synchronized writers.
97
*/
98
static constexpr auto DATABASE_WRITE_INTERVAL_MIN{50min};
99
static constexpr auto DATABASE_WRITE_INTERVAL_MAX{70min};
100
/** Maximum age of our tip for us to be considered current for fee estimation */
101
static constexpr std::chrono::hours MAX_FEE_ESTIMATION_TIP_AGE{3};
102
const std::vector<std::string> CHECKLEVEL_DOC {
103
    "level 0 reads the blocks from disk",
104
    "level 1 verifies block validity",
105
    "level 2 verifies undo data",
106
    "level 3 checks disconnection of tip blocks",
107
    "level 4 tries to reconnect the blocks",
108
    "each level includes the checks of the previous levels",
109
};
110
/** The number of blocks to keep below the deepest prune lock.
111
 *  There is nothing special about this number. It is higher than what we
112
 *  expect to see in regular mainnet reorgs, but not so high that it would
113
 *  noticeably interfere with the pruning mechanism.
114
 * */
115
static constexpr int PRUNE_LOCK_BUFFER{10};
116
117
// Return whether the completed full flush should compact chainstate
118
static bool ShouldCompactChainstate(bool in_ibd)
119
130k
{
120
130k
    static constexpr uint32_t flush_ratio{320}; // Roughly every 2 weeks with hourly flushes
121
130k
    return !in_ibd && FastRandomContext().randrange(flush_ratio) == 0;
  Branch (121:12): [True: 64.3k, False: 65.6k]
  Branch (121:23): [True: 587, False: 63.7k]
122
130k
}
123
124
TRACEPOINT_SEMAPHORE(validation, block_connected);
125
TRACEPOINT_SEMAPHORE(utxocache, flush);
126
TRACEPOINT_SEMAPHORE(mempool, replaced);
127
TRACEPOINT_SEMAPHORE(mempool, rejected);
128
129
const CBlockIndex* Chainstate::FindForkInGlobalIndex(const CBlockLocator& locator) const
130
728
{
131
728
    AssertLockHeld(cs_main);
132
133
    // Find the latest block common to locator and chain - we expect that
134
    // locator.vHave is sorted descending by height.
135
1.87k
    for (const uint256& hash : locator.vHave) {
  Branch (135:30): [True: 1.87k, False: 695]
136
1.87k
        const CBlockIndex* pindex{m_blockman.LookupBlockIndex(hash)};
137
1.87k
        if (pindex) {
  Branch (137:13): [True: 33, False: 1.83k]
138
33
            if (m_chain.Contains(*pindex)) {
  Branch (138:17): [True: 33, False: 0]
139
33
                return pindex;
140
33
            }
141
0
            if (pindex->GetAncestor(m_chain.Height()) == m_chain.Tip()) {
  Branch (141:17): [True: 0, False: 0]
142
0
                return m_chain.Tip();
143
0
            }
144
0
        }
145
1.87k
    }
146
695
    return m_chain.Genesis();
147
728
}
148
149
bool CheckInputScripts(const CTransaction& tx, TxValidationState& state,
150
                       const CCoinsViewCache& inputs, script_verify_flags flags, bool cacheSigStore,
151
                       bool cacheFullScriptStore, PrecomputedTransactionData& txdata,
152
                       ValidationCache& validation_cache,
153
                       std::vector<CScriptCheck>* pvChecks = nullptr)
154
                       EXCLUSIVE_LOCKS_REQUIRED(cs_main);
155
156
bool CheckFinalTxAtTip(const CBlockIndex& active_chain_tip, const CTransaction& tx)
157
2.06M
{
158
2.06M
    AssertLockHeld(cs_main);
159
160
    // CheckFinalTxAtTip() uses active_chain_tip.Height()+1 to evaluate
161
    // nLockTime because when IsFinalTx() is called within
162
    // AcceptBlock(), the height of the block *being*
163
    // evaluated is what is used. Thus if we want to know if a
164
    // transaction can be part of the *next* block, we need to call
165
    // IsFinalTx() with one more than active_chain_tip.Height().
166
2.06M
    const int nBlockHeight = active_chain_tip.nHeight + 1;
167
168
    // BIP113 requires that time-locked transactions have nLockTime set to
169
    // less than the median time of the previous block they're contained in.
170
    // When the next block is created its previous block will be the current
171
    // chain tip, so we use that to calculate the median time passed to
172
    // IsFinalTx().
173
2.06M
    const int64_t nBlockTime{active_chain_tip.GetMedianTimePast()};
174
175
2.06M
    return IsFinalTx(tx, nBlockHeight, nBlockTime);
176
2.06M
}
177
178
namespace {
179
/**
180
 * A helper which calculates heights of inputs of a given transaction.
181
 *
182
 * @param[in] tip    The current chain tip. If an input belongs to a mempool
183
 *                   transaction, we assume it will be confirmed in the next block.
184
 * @param[in] coins  Any CCoinsView that provides access to the relevant coins.
185
 * @param[in] tx     The transaction being evaluated.
186
 *
187
 * @returns A vector of input heights or nullopt, in case of an error.
188
 */
189
std::optional<std::vector<int>> CalculatePrevHeights(
190
    const CBlockIndex& tip,
191
    const CCoinsView& coins,
192
    const CTransaction& tx)
193
1.29M
{
194
1.29M
    std::vector<int> prev_heights;
195
1.29M
    prev_heights.resize(tx.vin.size());
196
6.48M
    for (size_t i = 0; i < tx.vin.size(); ++i) {
  Branch (196:24): [True: 5.19M, False: 1.29M]
197
5.19M
        if (auto coin{coins.GetCoin(tx.vin[i].prevout)}) {
  Branch (197:18): [True: 5.19M, False: 0]
198
5.19M
            prev_heights[i] = coin->nHeight == MEMPOOL_HEIGHT
  Branch (198:31): [True: 2.63M, False: 2.55M]
199
5.19M
                              ? tip.nHeight + 1 // Assume all mempool transaction confirm in the next block.
200
5.19M
                              : coin->nHeight;
201
5.19M
        } else {
202
0
            LogInfo("ERROR: %s: Missing input %d in transaction \'%s\'\n", __func__, i, tx.GetHash().GetHex());
203
0
            return std::nullopt;
204
0
        }
205
5.19M
    }
206
1.29M
    return prev_heights;
207
1.29M
}
208
} // namespace
209
210
std::optional<LockPoints> CalculateLockPointsAtTip(
211
    CBlockIndex* tip,
212
    const CCoinsView& coins_view,
213
    const CTransaction& tx)
214
1.29M
{
215
1.29M
    assert(tip);
  Branch (215:5): [True: 1.29M, False: 0]
216
217
1.29M
    auto prev_heights{CalculatePrevHeights(*tip, coins_view, tx)};
218
1.29M
    if (!prev_heights.has_value()) return std::nullopt;
  Branch (218:9): [True: 0, False: 1.29M]
219
220
1.29M
    CBlockIndex next_tip;
221
1.29M
    next_tip.pprev = tip;
222
    // When SequenceLocks() is called within ConnectBlock(), the height
223
    // of the block *being* evaluated is what is used.
224
    // Thus if we want to know if a transaction can be part of the
225
    // *next* block, we need to use one more than active_chainstate.m_chain.Height()
226
1.29M
    next_tip.nHeight = tip->nHeight + 1;
227
1.29M
    const auto [min_height, min_time] = CalculateSequenceLocks(tx, STANDARD_LOCKTIME_VERIFY_FLAGS, prev_heights.value(), next_tip);
228
229
    // Also store the hash of the block with the highest height of
230
    // all the blocks which have sequence locked prevouts.
231
    // This hash needs to still be on the chain
232
    // for these LockPoint calculations to be valid
233
    // Note: It is impossible to correctly calculate a maxInputBlock
234
    // if any of the sequence locked inputs depend on unconfirmed txs,
235
    // except in the special case where the relative lock time/height
236
    // is 0, which is equivalent to no sequence lock. Since we assume
237
    // input height of tip+1 for mempool txs and test the resulting
238
    // min_height and min_time from CalculateSequenceLocks against tip+1.
239
1.29M
    int max_input_height{0};
240
5.19M
    for (const int height : prev_heights.value()) {
  Branch (240:27): [True: 5.19M, False: 1.29M]
241
        // Can ignore mempool inputs since we'll fail if they had non-zero locks
242
5.19M
        if (height != next_tip.nHeight) {
  Branch (242:13): [True: 4.90M, False: 289k]
243
4.90M
            max_input_height = std::max(max_input_height, height);
244
4.90M
        }
245
5.19M
    }
246
247
    // tip->GetAncestor(max_input_height) should never return a nullptr
248
    // because max_input_height is always less than the tip height.
249
    // It would, however, be a bad bug to continue execution, since a
250
    // LockPoints object with the maxInputBlock member set to nullptr
251
    // signifies no relative lock time.
252
1.29M
    return LockPoints{min_height, min_time, Assert(tip->GetAncestor(max_input_height))};
253
1.29M
}
254
255
bool CheckSequenceLocksAtTip(CBlockIndex* tip,
256
                             const LockPoints& lock_points)
257
1.29M
{
258
1.29M
    assert(tip != nullptr);
  Branch (258:5): [True: 1.29M, False: 0]
259
260
1.29M
    CBlockIndex index;
261
1.29M
    index.pprev = tip;
262
    // CheckSequenceLocksAtTip() uses active_chainstate.m_chain.Height()+1 to evaluate
263
    // height based locks because when SequenceLocks() is called within
264
    // ConnectBlock(), the height of the block *being*
265
    // evaluated is what is used.
266
    // Thus if we want to know if a transaction can be part of the
267
    // *next* block, we need to use one more than active_chainstate.m_chain.Height()
268
1.29M
    index.nHeight = tip->nHeight + 1;
269
270
1.29M
    return EvaluateSequenceLocks(index, {lock_points.height, lock_points.time});
271
1.29M
}
272
273
static void LimitMempoolSize(CTxMemPool& pool, CCoinsViewCache& coins_cache)
274
    EXCLUSIVE_LOCKS_REQUIRED(::cs_main, pool.cs)
275
476k
{
276
476k
    AssertLockHeld(::cs_main);
277
476k
    AssertLockHeld(pool.cs);
278
476k
    int expired = pool.Expire(GetTime<std::chrono::seconds>() - pool.m_opts.expiry);
279
476k
    if (expired != 0) {
  Branch (279:9): [True: 9.34k, False: 466k]
280
9.34k
        LogDebug(BCLog::MEMPOOL, "Expired %i transactions from the memory pool\n", expired);
281
9.34k
    }
282
283
476k
    std::vector<COutPoint> vNoSpendsRemaining;
284
476k
    pool.TrimToSize(pool.m_opts.max_size_bytes, &vNoSpendsRemaining);
285
476k
    for (const COutPoint& removed : vNoSpendsRemaining)
  Branch (285:35): [True: 80.2k, False: 476k]
286
80.2k
        coins_cache.Uncache(removed);
287
476k
}
288
289
static bool IsCurrentForFeeEstimation(Chainstate& active_chainstate) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
290
354k
{
291
354k
    AssertLockHeld(cs_main);
292
354k
    if (active_chainstate.m_chainman.IsInitialBlockDownload()) {
  Branch (292:9): [True: 0, False: 354k]
293
0
        return false;
294
0
    }
295
354k
    if (active_chainstate.m_chain.Tip()->GetBlockTime() < count_seconds(GetTime<std::chrono::seconds>() - MAX_FEE_ESTIMATION_TIP_AGE))
  Branch (295:9): [True: 323k, False: 30.1k]
296
323k
        return false;
297
30.1k
    if (active_chainstate.m_chain.Height() < active_chainstate.m_chainman.m_best_header->nHeight - 1) {
  Branch (297:9): [True: 82, False: 30.0k]
298
82
        return false;
299
82
    }
300
30.0k
    return true;
301
30.1k
}
302
303
void Chainstate::MaybeUpdateMempoolForReorg(
304
    DisconnectedBlockTransactions& disconnectpool,
305
    bool fAddToMempool)
306
0
{
307
0
    if (!m_mempool) return;
  Branch (307:9): [True: 0, False: 0]
308
309
0
    AssertLockHeld(cs_main);
310
0
    AssertLockHeld(m_mempool->cs);
311
0
    std::vector<Txid> vHashUpdate;
312
0
    {
313
        // disconnectpool is ordered so that the front is the most recently-confirmed
314
        // transaction (the last tx of the block at the tip) in the disconnected chain.
315
        // Iterate disconnectpool in reverse, so that we add transactions
316
        // back to the mempool starting with the earliest transaction that had
317
        // been previously seen in a block.
318
0
        const auto queuedTx = disconnectpool.take();
319
0
        auto it = queuedTx.rbegin();
320
0
        while (it != queuedTx.rend()) {
  Branch (320:16): [True: 0, False: 0]
321
            // ignore validation errors in resurrected transactions
322
0
            if (!fAddToMempool || (*it)->IsCoinBase() ||
  Branch (322:17): [True: 0, False: 0]
  Branch (322:17): [True: 0, False: 0]
  Branch (322:35): [True: 0, False: 0]
323
0
                AcceptToMemoryPool(*this, *it, GetTime(),
  Branch (323:17): [True: 0, False: 0]
324
0
                    /*bypass_limits=*/true, /*test_accept=*/false).m_result_type !=
325
0
                        MempoolAcceptResult::ResultType::VALID) {
326
                // If the transaction doesn't make it in to the mempool, remove any
327
                // transactions that depend on it (which would now be orphans).
328
0
                m_mempool->removeRecursive(**it, MemPoolRemovalReason::REORG);
329
0
            } else if (m_mempool->exists((*it)->GetHash())) {
  Branch (329:24): [True: 0, False: 0]
330
0
                vHashUpdate.push_back((*it)->GetHash());
331
0
            }
332
0
            ++it;
333
0
        }
334
0
    }
335
336
    // AcceptToMemoryPool/addNewTransaction all assume that new mempool entries have
337
    // no in-mempool children, which is generally not true when adding
338
    // previously-confirmed transactions back to the mempool.
339
    // UpdateTransactionsFromBlock finds descendants of any transactions in
340
    // the disconnectpool that were added back and cleans up the mempool state.
341
0
    m_mempool->UpdateTransactionsFromBlock(vHashUpdate);
342
343
    // Predicate to use for filtering transactions in removeForReorg.
344
    // Checks whether the transaction is still final and, if it spends a coinbase output, mature.
345
    // Also updates valid entries' cached LockPoints if needed.
346
    // If false, the tx is still valid and its lockpoints are updated.
347
    // If true, the tx would be invalid in the next block; remove this entry and all of its descendants.
348
    // Note that TRUC rules are not applied here, so reorgs may cause violations of TRUC inheritance or
349
    // topology restrictions.
350
0
    const auto filter_final_and_mature = [&](CTxMemPool::txiter it)
351
0
        EXCLUSIVE_LOCKS_REQUIRED(m_mempool->cs, ::cs_main) {
352
0
        AssertLockHeld(m_mempool->cs);
353
0
        AssertLockHeld(::cs_main);
354
0
        const CTransaction& tx = it->GetTx();
355
356
        // The transaction must be final.
357
0
        if (!CheckFinalTxAtTip(*Assert(m_chain.Tip()), tx)) return true;
  Branch (357:13): [True: 0, False: 0]
358
359
0
        const LockPoints& lp = it->GetLockPoints();
360
        // CheckSequenceLocksAtTip checks if the transaction will be final in the next block to be
361
        // created on top of the new chain.
362
0
        if (TestLockPointValidity(m_chain, lp)) {
  Branch (362:13): [True: 0, False: 0]
363
0
            if (!CheckSequenceLocksAtTip(m_chain.Tip(), lp)) {
  Branch (363:17): [True: 0, False: 0]
364
0
                return true;
365
0
            }
366
0
        } else {
367
0
            const CCoinsViewMemPool view_mempool{&CoinsTip(), *m_mempool};
368
0
            const std::optional<LockPoints> new_lock_points{CalculateLockPointsAtTip(m_chain.Tip(), view_mempool, tx)};
369
0
            if (new_lock_points.has_value() && CheckSequenceLocksAtTip(m_chain.Tip(), *new_lock_points)) {
  Branch (369:17): [True: 0, False: 0]
  Branch (369:48): [True: 0, False: 0]
370
                // Now update the mempool entry lockpoints as well.
371
0
                it->UpdateLockPoints(*new_lock_points);
372
0
            } else {
373
0
                return true;
374
0
            }
375
0
        }
376
377
        // If the transaction spends any coinbase outputs, it must be mature.
378
0
        if (it->GetSpendsCoinbase()) {
  Branch (378:13): [True: 0, False: 0]
379
0
            for (const CTxIn& txin : tx.vin) {
  Branch (379:36): [True: 0, False: 0]
380
0
                if (m_mempool->exists(txin.prevout.hash)) continue;
  Branch (380:21): [True: 0, False: 0]
381
0
                const Coin& coin{CoinsTip().AccessCoin(txin.prevout)};
382
0
                assert(!coin.IsSpent());
  Branch (382:17): [True: 0, False: 0]
383
0
                const auto mempool_spend_height{m_chain.Tip()->nHeight + 1};
384
0
                if (coin.IsCoinBase() && mempool_spend_height - coin.nHeight < COINBASE_MATURITY) {
  Branch (384:21): [True: 0, False: 0]
  Branch (384:42): [True: 0, False: 0]
385
0
                    return true;
386
0
                }
387
0
            }
388
0
        }
389
        // Transaction is still valid and cached LockPoints are updated.
390
0
        return false;
391
0
    };
392
393
    // We also need to remove any now-immature transactions
394
0
    m_mempool->removeForReorg(m_chain, filter_final_and_mature);
395
    // Re-limit mempool size, in case we added any transactions
396
0
    LimitMempoolSize(*m_mempool, this->CoinsTip());
397
0
}
398
399
/**
400
* Checks to avoid mempool polluting consensus critical paths since cached
401
* signature and script validity results will be reused if we validate this
402
* transaction again during block validation.
403
* */
404
static bool CheckInputsFromMempoolAndCache(const CTransaction& tx, TxValidationState& state,
405
                const CCoinsViewCache& view, const CTxMemPool& pool,
406
                script_verify_flags flags, PrecomputedTransactionData& txdata, CCoinsViewCache& coins_tip,
407
                ValidationCache& validation_cache)
408
                EXCLUSIVE_LOCKS_REQUIRED(cs_main, pool.cs)
409
368k
{
410
368k
    AssertLockHeld(cs_main);
411
368k
    AssertLockHeld(pool.cs);
412
413
368k
    assert(!tx.IsCoinBase());
  Branch (413:5): [True: 368k, False: 0]
414
631k
    for (const CTxIn& txin : tx.vin) {
  Branch (414:28): [True: 631k, False: 368k]
415
631k
        const Coin& coin = view.AccessCoin(txin.prevout);
416
417
        // This coin was checked in PreChecks and MemPoolAccept
418
        // has been holding cs_main since then.
419
631k
        Assume(!coin.IsSpent());
420
631k
        if (coin.IsSpent()) return false;
  Branch (420:13): [True: 0, False: 631k]
421
422
        // If the Coin is available, there are 2 possibilities:
423
        // it is available in our current ChainstateActive UTXO set,
424
        // or it's a UTXO provided by a transaction in our mempool.
425
        // Ensure the scriptPubKeys in Coins from CoinsView are correct.
426
631k
        const CTransactionRef& txFrom = pool.get(txin.prevout.hash);
427
631k
        if (txFrom) {
  Branch (427:13): [True: 233k, False: 397k]
428
233k
            assert(txFrom->GetHash() == txin.prevout.hash);
  Branch (428:13): [True: 233k, False: 0]
429
233k
            assert(txFrom->vout.size() > txin.prevout.n);
  Branch (429:13): [True: 233k, False: 0]
430
233k
            assert(txFrom->vout[txin.prevout.n] == coin.out);
  Branch (430:13): [True: 233k, False: 0]
431
397k
        } else {
432
397k
            const Coin& coinFromUTXOSet = coins_tip.AccessCoin(txin.prevout);
433
397k
            assert(!coinFromUTXOSet.IsSpent());
  Branch (433:13): [True: 397k, False: 0]
434
397k
            assert(coinFromUTXOSet.out == coin.out);
  Branch (434:13): [True: 397k, False: 0]
435
397k
        }
436
631k
    }
437
438
    // Call CheckInputScripts() to cache signature and script validity against current tip consensus rules.
439
368k
    return CheckInputScripts(tx, state, view, flags, /* cacheSigStore= */ true, /* cacheFullScriptStore= */ true, txdata, validation_cache);
440
368k
}
441
442
namespace {
443
444
class MemPoolAccept
445
{
446
public:
447
    explicit MemPoolAccept(CTxMemPool& mempool, Chainstate& active_chainstate) :
448
2.18M
        m_pool(mempool),
449
2.18M
        m_view(&CoinsViewEmpty::Get()),
450
2.18M
        m_viewmempool(&active_chainstate.CoinsTip(), m_pool),
451
2.18M
        m_active_chainstate(active_chainstate)
452
2.18M
    {
453
2.18M
    }
454
455
    // We put the arguments we're handed into a struct, so we can pass them
456
    // around easier.
457
    struct ATMPArgs {
458
        const int64_t m_accept_time;
459
        const bool m_bypass_limits;
460
        /*
461
         * Return any outpoints which were not previously present in the coins
462
         * cache, but were added as a result of validating the tx for mempool
463
         * acceptance. This allows the caller to optionally remove the cache
464
         * additions if the associated transaction ends up being rejected by
465
         * the mempool.
466
         */
467
        std::vector<COutPoint>& m_coins_to_uncache;
468
        /** When true, the transaction or package will not be submitted to the mempool. */
469
        const bool m_test_accept;
470
        /** Whether we allow transactions to replace mempool transactions. If false,
471
         * any transaction spending the same inputs as a transaction in the mempool is considered
472
         * a conflict. */
473
        const bool m_allow_replacement;
474
        /** When true, allow sibling eviction. This only occurs in single transaction package settings. */
475
        const bool m_allow_sibling_eviction;
476
        /** Used to skip the LimitMempoolSize() call within AcceptSingleTransaction(). This should be used when multiple
477
         * AcceptSubPackage calls are expected and the mempool will be trimmed at the end of AcceptPackage(). */
478
        const bool m_package_submission;
479
        /** When true, use package feerates instead of individual transaction feerates for fee-based
480
         * policies such as mempool min fee and min relay fee.
481
         */
482
        const bool m_package_feerates;
483
        /** Used for local submission of transactions to catch "absurd" fees
484
         * due to fee miscalculation by wallets. std:nullopt implies unset, allowing any feerates.
485
         * Any individual transaction failing this check causes immediate failure.
486
         */
487
        const std::optional<CFeeRate> m_client_maxfeerate;
488
489
        /** Parameters for single transaction mempool validation. */
490
        static ATMPArgs SingleAccept(int64_t accept_time,
491
                                     bool bypass_limits, std::vector<COutPoint>& coins_to_uncache,
492
1.48M
                                     bool test_accept) {
493
1.48M
            return ATMPArgs{/*accept_time=*/ accept_time,
494
1.48M
                            /*bypass_limits=*/ bypass_limits,
495
1.48M
                            /*coins_to_uncache=*/ coins_to_uncache,
496
1.48M
                            /*test_accept=*/ test_accept,
497
1.48M
                            /*allow_replacement=*/ true,
498
1.48M
                            /*allow_sibling_eviction=*/ true,
499
1.48M
                            /*package_submission=*/ false,
500
1.48M
                            /*package_feerates=*/ false,
501
1.48M
                            /*client_maxfeerate=*/ {}, // checked by caller
502
1.48M
            };
503
1.48M
        }
504
505
        /** Parameters for test package mempool validation through testmempoolaccept. */
506
        static ATMPArgs PackageTestAccept(int64_t accept_time,
507
245k
                                          std::vector<COutPoint>& coins_to_uncache) {
508
245k
            return ATMPArgs{/*accept_time=*/ accept_time,
509
245k
                            /*bypass_limits=*/ false,
510
245k
                            /*coins_to_uncache=*/ coins_to_uncache,
511
245k
                            /*test_accept=*/ true,
512
245k
                            /*allow_replacement=*/ false,
513
245k
                            /*allow_sibling_eviction=*/ false,
514
245k
                            /*package_submission=*/ false, // not submitting to mempool
515
245k
                            /*package_feerates=*/ false,
516
245k
                            /*client_maxfeerate=*/ {}, // checked by caller
517
245k
            };
518
245k
        }
519
520
        /** Parameters for child-with-parents package validation. */
521
        static ATMPArgs PackageChildWithParents(int64_t accept_time,
522
462k
                                                std::vector<COutPoint>& coins_to_uncache, const std::optional<CFeeRate>& client_maxfeerate) {
523
462k
            return ATMPArgs{/*accept_time=*/ accept_time,
524
462k
                            /*bypass_limits=*/ false,
525
462k
                            /*coins_to_uncache=*/ coins_to_uncache,
526
462k
                            /*test_accept=*/ false,
527
462k
                            /*allow_replacement=*/ true,
528
462k
                            /*allow_sibling_eviction=*/ false,
529
462k
                            /*package_submission=*/ true,
530
462k
                            /*package_feerates=*/ true,
531
462k
                            /*client_maxfeerate=*/ client_maxfeerate,
532
462k
            };
533
462k
        }
534
535
        /** Parameters for a single transaction within a package. */
536
712k
        static ATMPArgs SingleInPackageAccept(const ATMPArgs& package_args) {
537
712k
            return ATMPArgs{/*accept_time=*/ package_args.m_accept_time,
538
712k
                            /*bypass_limits=*/ false,
539
712k
                            /*coins_to_uncache=*/ package_args.m_coins_to_uncache,
540
712k
                            /*test_accept=*/ package_args.m_test_accept,
541
712k
                            /*allow_replacement=*/ true,
542
712k
                            /*allow_sibling_eviction=*/ true,
543
712k
                            /*package_submission=*/ true, // trim at the end of AcceptPackage()
544
712k
                            /*package_feerates=*/ false, // only 1 transaction
545
712k
                            /*client_maxfeerate=*/ package_args.m_client_maxfeerate,
546
712k
            };
547
712k
        }
548
549
    private:
550
        // Private ctor to avoid exposing details to clients and allowing the possibility of
551
        // mixing up the order of the arguments. Use static functions above instead.
552
        ATMPArgs(int64_t accept_time,
553
                 bool bypass_limits,
554
                 std::vector<COutPoint>& coins_to_uncache,
555
                 bool test_accept,
556
                 bool allow_replacement,
557
                 bool allow_sibling_eviction,
558
                 bool package_submission,
559
                 bool package_feerates,
560
                 std::optional<CFeeRate> client_maxfeerate)
561
2.90M
            : m_accept_time{accept_time},
562
2.90M
              m_bypass_limits{bypass_limits},
563
2.90M
              m_coins_to_uncache{coins_to_uncache},
564
2.90M
              m_test_accept{test_accept},
565
2.90M
              m_allow_replacement{allow_replacement},
566
2.90M
              m_allow_sibling_eviction{allow_sibling_eviction},
567
2.90M
              m_package_submission{package_submission},
568
2.90M
              m_package_feerates{package_feerates},
569
2.90M
              m_client_maxfeerate{client_maxfeerate}
570
2.90M
        {
571
            // If we are using package feerates, we must be doing package submission.
572
            // It also means sibling eviction is not permitted.
573
2.90M
            if (m_package_feerates) {
  Branch (573:17): [True: 462k, False: 2.43M]
574
462k
                Assume(m_package_submission);
575
462k
                Assume(!m_allow_sibling_eviction);
576
462k
            }
577
2.90M
            if (m_allow_sibling_eviction) Assume(m_allow_replacement);
  Branch (577:17): [True: 2.19M, False: 707k]
578
2.90M
        }
579
    };
580
581
    /** Clean up all non-chainstate coins from m_view and m_viewmempool. */
582
    void CleanupTemporaryCoins() EXCLUSIVE_LOCKS_REQUIRED(cs_main, m_pool.cs);
583
584
    // Single transaction acceptance
585
1.48M
    MempoolAcceptResult AcceptSingleTransactionAndCleanup(const CTransactionRef& ptx, ATMPArgs& args) EXCLUSIVE_LOCKS_REQUIRED(cs_main) {
586
1.48M
        LOCK(m_pool.cs);
587
1.48M
        MempoolAcceptResult result = AcceptSingleTransactionInternal(ptx, args);
588
1.48M
        ClearSubPackageState();
589
1.48M
        return result;
590
1.48M
    }
591
    MempoolAcceptResult AcceptSingleTransactionInternal(const CTransactionRef& ptx, ATMPArgs& args) EXCLUSIVE_LOCKS_REQUIRED(cs_main, m_pool.cs);
592
593
    /**
594
    * Multiple transaction acceptance. Transactions may or may not be interdependent, but must not
595
    * conflict with each other, and the transactions cannot already be in the mempool. Parents must
596
    * come before children if any dependencies exist.
597
    */
598
245k
    PackageMempoolAcceptResult AcceptMultipleTransactionsAndCleanup(const std::vector<CTransactionRef>& txns, ATMPArgs& args) EXCLUSIVE_LOCKS_REQUIRED(cs_main) {
599
245k
        LOCK(m_pool.cs);
600
245k
        PackageMempoolAcceptResult result = AcceptMultipleTransactionsInternal(txns, args);
601
245k
        ClearSubPackageState();
602
245k
        return result;
603
245k
    }
604
    PackageMempoolAcceptResult AcceptMultipleTransactionsInternal(const std::vector<CTransactionRef>& txns, ATMPArgs& args) EXCLUSIVE_LOCKS_REQUIRED(cs_main, m_pool.cs);
605
606
    /**
607
     * Submission of a subpackage.
608
     * If subpackage size == 1, calls AcceptSingleTransaction() with adjusted ATMPArgs to
609
     * enable sibling eviction and creates a PackageMempoolAcceptResult
610
     * wrapping the result.
611
     *
612
     * If subpackage size > 1, calls AcceptMultipleTransactions() with the provided ATMPArgs.
613
     *
614
     * Also cleans up all non-chainstate coins from m_view at the end.
615
    */
616
    PackageMempoolAcceptResult AcceptSubPackage(const std::vector<CTransactionRef>& subpackage, ATMPArgs& args)
617
        EXCLUSIVE_LOCKS_REQUIRED(cs_main, m_pool.cs);
618
619
    /**
620
     * Package (more specific than just multiple transactions) acceptance. Package must be a child
621
     * with all of its unconfirmed parents, and topologically sorted.
622
     */
623
    PackageMempoolAcceptResult AcceptPackage(const Package& package, ATMPArgs& args) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
624
625
private:
626
    // All the intermediate state that gets passed between the various levels
627
    // of checking a given transaction.
628
    struct Workspace {
629
2.64M
        explicit Workspace(const CTransactionRef& ptx) : m_ptx(ptx), m_hash(ptx->GetHash()) {}
630
        /** Txids of mempool transactions that this transaction directly conflicts with or may
631
         * replace via sibling eviction. */
632
        std::set<Txid> m_conflicts;
633
        /** Iterators to mempool entries that this transaction directly conflicts with or may
634
         * replace via sibling eviction. */
635
        CTxMemPool::setEntries m_iters_conflicting;
636
        /** All mempool parents of this transaction. */
637
        std::vector<CTxMemPoolEntry::CTxMemPoolEntryRef> m_parents;
638
        /* Handle to the tx in the changeset */
639
        CTxMemPool::ChangeSet::TxHandle m_tx_handle;
640
        /** Whether RBF-related data structures (m_conflicts, m_iters_conflicting,
641
         * m_replaced_transactions) include a sibling in addition to txns with conflicting inputs. */
642
        bool m_sibling_eviction{false};
643
644
        /** Virtual size of the transaction as used by the mempool, calculated using serialized size
645
         * of the transaction and sigops. */
646
        int64_t m_vsize;
647
        /** Fees paid by this transaction: total input amounts subtracted by total output amounts. */
648
        CAmount m_base_fees;
649
        /** Base fees + any fee delta set by the user with prioritisetransaction. */
650
        CAmount m_modified_fees;
651
652
        /** If we're doing package validation (i.e. m_package_feerates=true), the "effective"
653
         * package feerate of this transaction is the total fees divided by the total size of
654
         * transactions (which may include its ancestors and/or descendants). */
655
        CFeeRate m_package_feerate{0};
656
657
        const CTransactionRef& m_ptx;
658
        /** Txid. */
659
        const Txid& m_hash;
660
        TxValidationState m_state;
661
        /** A temporary cache containing serialized transaction data for signature verification.
662
         * Reused across PolicyScriptChecks and ConsensusScriptChecks. */
663
        PrecomputedTransactionData m_precomputed_txdata;
664
    };
665
666
    // Run the policy checks on a given transaction, excluding any script checks.
667
    // Looks up inputs, calculates feerate, considers replacement, evaluates
668
    // package limits, etc. As this function can be invoked for "free" by a peer,
669
    // only tests that are fast should be done here (to avoid CPU DoS).
670
    bool PreChecks(ATMPArgs& args, Workspace& ws) EXCLUSIVE_LOCKS_REQUIRED(cs_main, m_pool.cs);
671
672
    // Run checks for mempool replace-by-fee, only used in AcceptSingleTransaction.
673
    bool ReplacementChecks(Workspace& ws) EXCLUSIVE_LOCKS_REQUIRED(cs_main, m_pool.cs);
674
675
    bool PackageRBFChecks(const std::vector<CTransactionRef>& txns,
676
                          std::vector<Workspace>& workspaces,
677
                          PackageValidationState& package_state) EXCLUSIVE_LOCKS_REQUIRED(cs_main, m_pool.cs);
678
679
    // Run the script checks using our policy flags. As this can be slow, we should
680
    // only invoke this on transactions that have otherwise passed policy checks.
681
    bool PolicyScriptChecks(Workspace& ws) EXCLUSIVE_LOCKS_REQUIRED(cs_main, m_pool.cs);
682
683
    // Re-run the script checks, using consensus flags, and try to cache the
684
    // result in the scriptcache. This should be done after
685
    // PolicyScriptChecks(). This requires that all inputs either be in our
686
    // utxo set or in the mempool.
687
    bool ConsensusScriptChecks(Workspace& ws) EXCLUSIVE_LOCKS_REQUIRED(cs_main, m_pool.cs);
688
689
    // Try to add the transaction to the mempool, removing any conflicts first.
690
    void FinalizeSubpackage(const ATMPArgs& args) EXCLUSIVE_LOCKS_REQUIRED(cs_main, m_pool.cs);
691
692
    // Submit all transactions to the mempool and call ConsensusScriptChecks to add to the script
693
    // cache - should only be called after successful validation of all transactions in the package.
694
    // Does not call LimitMempoolSize(), so mempool max_size_bytes may be temporarily exceeded.
695
    bool SubmitPackage(const ATMPArgs& args, std::vector<Workspace>& workspaces, PackageValidationState& package_state,
696
                       std::map<Wtxid, MempoolAcceptResult>& results)
697
         EXCLUSIVE_LOCKS_REQUIRED(cs_main, m_pool.cs);
698
699
    // Compare a package's feerate against minimum allowed.
700
    bool CheckFeeRate(size_t package_size, CAmount package_fee, TxValidationState& state) EXCLUSIVE_LOCKS_REQUIRED(::cs_main, m_pool.cs)
701
795k
    {
702
795k
        AssertLockHeld(::cs_main);
703
795k
        AssertLockHeld(m_pool.cs);
704
795k
        CAmount mempoolRejectFee = m_pool.GetMinFee().GetFee(package_size);
705
795k
        if (mempoolRejectFee > 0 && package_fee < mempoolRejectFee) {
  Branch (705:13): [True: 38.9k, False: 756k]
  Branch (705:37): [True: 28.9k, False: 10.0k]
706
28.9k
            return state.Invalid(TxValidationResult::TX_RECONSIDERABLE, "mempool min fee not met", strprintf("%d < %d", package_fee, mempoolRejectFee));
707
28.9k
        }
708
709
766k
        if (package_fee < m_pool.m_opts.min_relay_feerate.GetFee(package_size)) {
  Branch (709:13): [True: 60.0k, False: 706k]
710
60.0k
            return state.Invalid(TxValidationResult::TX_RECONSIDERABLE, "min relay fee not met",
711
60.0k
                                 strprintf("%d < %d", package_fee, m_pool.m_opts.min_relay_feerate.GetFee(package_size)));
712
60.0k
        }
713
706k
        return true;
714
766k
    }
715
716
    ValidationCache& GetValidationCache()
717
866k
    {
718
866k
        return m_active_chainstate.m_chainman.m_validation_cache;
719
866k
    }
720
721
private:
722
    CTxMemPool& m_pool;
723
724
    /** Holds a cached view of available coins from the UTXO set, mempool, and artificial temporary coins (to enable package validation).
725
     * The view doesn't track whether a coin previously existed but has now been spent. We detect conflicts in other ways:
726
     * - conflicts within a transaction are checked in CheckTransaction (bad-txns-inputs-duplicate)
727
     * - conflicts within a package are checked in IsWellFormedPackage (conflict-in-package)
728
     * - conflicts with an existing mempool transaction are found in CTxMemPool::GetConflictTx and replacements are allowed
729
     * The temporary coins should persist between individual transaction checks so that package validation is possible,
730
     * but must be cleaned up when we finish validating a subpackage, whether accepted or rejected. The cache must also
731
     * be cleared when mempool contents change (when a changeset is applied or when the mempool trims itself) because it
732
     * can return cached coins that no longer exist in the backend. Use CleanupTemporaryCoins() anytime you are finished
733
     * with a SubPackageState or call LimitMempoolSize().
734
     */
735
    CCoinsViewCache m_view;
736
737
    // These are the two possible backends for m_view.
738
    /** When m_view is connected to m_viewmempool as its backend, it can pull coins from the mempool and from the UTXO
739
     * set. This is also where temporary coins are stored. */
740
    CCoinsViewMemPool m_viewmempool;
741
742
    Chainstate& m_active_chainstate;
743
744
    // Fields below are per *sub*package state and must be reset prior to subsequent
745
    // AcceptSingleTransaction and AcceptMultipleTransactions invocations
746
    struct SubPackageState {
747
        /** Aggregated modified fees of all transactions, used to calculate package feerate. */
748
        CAmount m_total_modified_fees{0};
749
        /** Aggregated virtual size of all transactions, used to calculate package feerate. */
750
        int64_t m_total_vsize{0};
751
752
        // RBF-related members
753
        /** Whether the transaction(s) would replace any mempool transactions and/or evict any siblings.
754
         * If so, RBF rules apply. */
755
        bool m_rbf{false};
756
        /** Mempool transactions that were replaced. */
757
        std::list<CTransactionRef> m_replaced_transactions;
758
        /* Changeset representing adding transactions and removing their conflicts. */
759
        std::unique_ptr<CTxMemPool::ChangeSet> m_changeset;
760
761
        /** Total modified fees of mempool transactions being replaced. */
762
        CAmount m_conflicting_fees{0};
763
        /** Total size (in virtual bytes) of mempool transactions being replaced. */
764
        size_t m_conflicting_size{0};
765
    };
766
767
    struct SubPackageState m_subpackage;
768
769
    /** Re-set sub-package state to not leak between evaluations */
770
    void ClearSubPackageState() EXCLUSIVE_LOCKS_REQUIRED(cs_main, m_pool.cs)
771
2.90M
    {
772
2.90M
        m_subpackage = SubPackageState{};
773
774
        // And clean coins while at it
775
2.90M
        CleanupTemporaryCoins();
776
2.90M
    }
777
};
778
779
bool MemPoolAccept::PreChecks(ATMPArgs& args, Workspace& ws)
780
2.63M
{
781
2.63M
    AssertLockHeld(cs_main);
782
2.63M
    AssertLockHeld(m_pool.cs);
783
2.63M
    const CTransactionRef& ptx = ws.m_ptx;
784
2.63M
    const CTransaction& tx = *ws.m_ptx;
785
2.63M
    const Txid& hash = ws.m_hash;
786
787
    // Copy/alias what we need out of args
788
2.63M
    const int64_t nAcceptTime = args.m_accept_time;
789
2.63M
    const bool bypass_limits = args.m_bypass_limits;
790
2.63M
    std::vector<COutPoint>& coins_to_uncache = args.m_coins_to_uncache;
791
792
    // Alias what we need out of ws
793
2.63M
    TxValidationState& state = ws.m_state;
794
795
2.63M
    if (!CheckTransaction(tx, state)) {
  Branch (795:9): [True: 496k, False: 2.13M]
796
496k
        return false; // state filled in by CheckTransaction
797
496k
    }
798
799
    // Coinbase is only valid in a block, not as a loose transaction
800
2.13M
    if (tx.IsCoinBase())
  Branch (800:9): [True: 7.19k, False: 2.12M]
801
7.19k
        return state.Invalid(TxValidationResult::TX_CONSENSUS, "coinbase");
802
803
    // Rather not work on nonstandard transactions (unless -testnet/-regtest)
804
2.12M
    std::string reason;
805
2.12M
    if (m_pool.m_opts.require_standard && !IsStandardTx(tx, m_pool.m_opts.max_datacarrier_bytes, m_pool.m_opts.permit_bare_multisig, m_pool.m_opts.dust_relay_feerate, reason)) {
  Branch (805:9): [True: 1.66M, False: 460k]
  Branch (805:43): [True: 59.1k, False: 1.61M]
806
59.1k
        return state.Invalid(TxValidationResult::TX_NOT_STANDARD, reason);
807
59.1k
    }
808
809
    // Transactions smaller than 65 non-witness bytes are not relayed to mitigate CVE-2017-12842.
810
2.07M
    if (::GetSerializeSize(TX_NO_WITNESS(tx)) < MIN_STANDARD_TX_NONWITNESS_SIZE)
  Branch (810:9): [True: 3.09k, False: 2.06M]
811
3.09k
        return state.Invalid(TxValidationResult::TX_NOT_STANDARD, "tx-size-small");
812
813
    // Only accept nLockTime-using transactions that can be mined in the next
814
    // block; we don't want our mempool filled up with transactions that can't
815
    // be mined yet.
816
2.06M
    if (!CheckFinalTxAtTip(*Assert(m_active_chainstate.m_chain.Tip()), tx)) {
  Branch (816:9): [True: 87.5k, False: 1.97M]
817
87.5k
        return state.Invalid(TxValidationResult::TX_PREMATURE_SPEND, "non-final");
818
87.5k
    }
819
820
1.97M
    if (m_pool.exists(tx.GetWitnessHash())) {
  Branch (820:9): [True: 90.1k, False: 1.88M]
821
        // Exact transaction already exists in the mempool.
822
90.1k
        return state.Invalid(TxValidationResult::TX_CONFLICT, "txn-already-in-mempool");
823
1.88M
    } else if (m_pool.exists(tx.GetHash())) {
  Branch (823:16): [True: 641, False: 1.88M]
824
        // Transaction with the same non-witness data but different witness (same txid, different
825
        // wtxid) already exists in the mempool.
826
641
        return state.Invalid(TxValidationResult::TX_CONFLICT, "txn-same-nonwitness-data-in-mempool");
827
641
    }
828
829
    // Check for conflicts with in-memory transactions
830
1.88M
    for (const CTxIn &txin : tx.vin)
  Branch (830:28): [True: 9.26M, False: 1.76M]
831
9.26M
    {
832
9.26M
        const CTransaction* ptxConflicting = m_pool.GetConflictTx(txin.prevout);
833
9.26M
        if (ptxConflicting) {
  Branch (833:13): [True: 1.18M, False: 8.08M]
834
1.18M
            if (!args.m_allow_replacement) {
  Branch (834:17): [True: 119k, False: 1.06M]
835
                // Transaction conflicts with a mempool tx, but we're not allowing replacements in this context.
836
119k
                return state.Invalid(TxValidationResult::TX_MEMPOOL_POLICY, "bip125-replacement-disallowed");
837
119k
            }
838
1.06M
            ws.m_conflicts.insert(ptxConflicting->GetHash());
839
1.06M
        }
840
9.26M
    }
841
842
1.76M
    m_view.SetBackend(m_viewmempool);
843
844
1.76M
    const CCoinsViewCache& coins_cache = m_active_chainstate.CoinsTip();
845
    // do all inputs exist?
846
5.83M
    for (const CTxIn& txin : tx.vin) {
  Branch (846:28): [True: 5.83M, False: 1.29M]
847
5.83M
        if (!coins_cache.HaveCoinInCache(txin.prevout)) {
  Branch (847:13): [True: 3.29M, False: 2.53M]
848
3.29M
            coins_to_uncache.push_back(txin.prevout);
849
3.29M
        }
850
851
        // Note: this call may add txin.prevout to the coins cache
852
        // (coins_cache.cacheCoins) by way of FetchCoin(). It should be removed
853
        // later (via coins_to_uncache) if this tx turns out to be invalid.
854
5.83M
        if (!m_view.HaveCoin(txin.prevout)) {
  Branch (854:13): [True: 476k, False: 5.36M]
855
            // Are inputs missing because we already have the tx?
856
2.83M
            for (size_t out = 0; out < tx.vout.size(); out++) {
  Branch (856:34): [True: 2.35M, False: 476k]
857
                // Optimistically just do efficient check of cache for outputs
858
2.35M
                if (coins_cache.HaveCoinInCache(COutPoint(hash, out))) {
  Branch (858:21): [True: 0, False: 2.35M]
859
0
                    return state.Invalid(TxValidationResult::TX_CONFLICT, "txn-already-known");
860
0
                }
861
2.35M
            }
862
            // Otherwise assume this might be an orphan tx for which we just haven't seen parents yet
863
476k
            return state.Invalid(TxValidationResult::TX_MISSING_INPUTS, "bad-txns-inputs-missingorspent");
864
476k
        }
865
5.83M
    }
866
867
    // This is const, but calls into `CCoinsViewCache::GetBestBlock()` to refresh
868
    // the cached best block through `m_viewmempool` after caching inputs.
869
1.29M
    (void)m_view.GetBestBlock();
870
871
    // All required inputs are cached now, so switch m_view to the empty backend.
872
    // This keeps already-fetched cache entries for later checks and prevents new
873
    // backend lookups (which would avoid coins_to_uncache tracking).
874
1.29M
    m_view.SetBackend(CoinsViewEmpty::Get());
875
876
1.29M
    assert(m_active_chainstate.m_blockman.LookupBlockIndex(m_view.GetBestBlock()) == m_active_chainstate.m_chain.Tip());
  Branch (876:5): [True: 1.29M, False: 0]
877
878
    // Only accept BIP68 sequence locked transactions that can be mined in the next
879
    // block; we don't want our mempool filled up with transactions that can't
880
    // be mined yet.
881
    // Pass in m_view which has all of the relevant inputs cached. Note that, since m_view's
882
    // backend was removed, it no longer pulls coins from the mempool.
883
1.29M
    const std::optional<LockPoints> lock_points{CalculateLockPointsAtTip(m_active_chainstate.m_chain.Tip(), m_view, tx)};
884
1.29M
    if (!lock_points.has_value() || !CheckSequenceLocksAtTip(m_active_chainstate.m_chain.Tip(), *lock_points)) {
  Branch (884:9): [True: 0, False: 1.29M]
  Branch (884:37): [True: 48.6k, False: 1.24M]
885
48.6k
        return state.Invalid(TxValidationResult::TX_PREMATURE_SPEND, "non-BIP68-final");
886
48.6k
    }
887
888
    // The mempool holds txs for the next block, so pass height+1 to CheckTxInputs
889
1.24M
    if (!Consensus::CheckTxInputs(tx, state, m_view, m_active_chainstate.m_chain.Height() + 1, ws.m_base_fees)) {
  Branch (889:9): [True: 17.9k, False: 1.22M]
890
17.9k
        return false; // state filled in by CheckTxInputs
891
17.9k
    }
892
893
1.22M
    if (m_pool.m_opts.require_standard) {
  Branch (893:9): [True: 925k, False: 300k]
894
925k
        state = ValidateInputsStandardness(tx, m_view);
895
925k
        if (state.IsInvalid()) {
  Branch (895:13): [True: 451, False: 924k]
896
451
            return false;
897
451
        }
898
925k
    }
899
900
    // Check for non-standard witnesses.
901
1.22M
    if (tx.HasWitness() && m_pool.m_opts.require_standard && !IsWitnessStandard(tx, m_view)) {
  Branch (901:9): [True: 1.18M, False: 38.4k]
  Branch (901:28): [True: 918k, False: 268k]
  Branch (901:62): [True: 5.42k, False: 913k]
902
5.42k
        return state.Invalid(TxValidationResult::TX_WITNESS_MUTATED, "bad-witness-nonstandard");
903
5.42k
    }
904
905
1.22M
    int64_t nSigOpsCost = GetTransactionSigOpCost(tx, m_view, STANDARD_SCRIPT_VERIFY_FLAGS);
906
907
    // Keep track of transactions that spend a coinbase, which we re-scan
908
    // during reorgs to ensure COINBASE_MATURITY is still met.
909
1.22M
    bool fSpendsCoinbase = false;
910
1.79M
    for (const CTxIn &txin : tx.vin) {
  Branch (910:28): [True: 1.79M, False: 464k]
911
1.79M
        const Coin &coin = m_view.AccessCoin(txin.prevout);
912
1.79M
        if (coin.IsCoinBase()) {
  Branch (912:13): [True: 755k, False: 1.04M]
913
755k
            fSpendsCoinbase = true;
914
755k
            break;
915
755k
        }
916
1.79M
    }
917
918
    // Set entry_sequence to 0 when bypass_limits is used; this allows txs from a block
919
    // reorg to be marked earlier than any child txs that were already in the mempool.
920
1.22M
    const uint64_t entry_sequence = bypass_limits ? 0 : m_pool.GetSequence();
  Branch (920:37): [True: 158k, False: 1.06M]
921
1.22M
    if (!m_subpackage.m_changeset) {
  Branch (921:9): [True: 1.11M, False: 102k]
922
1.11M
        m_subpackage.m_changeset = m_pool.GetChangeSet();
923
1.11M
    }
924
1.22M
    ws.m_tx_handle = m_subpackage.m_changeset->StageAddition(ptx, ws.m_base_fees, nAcceptTime, m_active_chainstate.m_chain.Height(), entry_sequence, fSpendsCoinbase, nSigOpsCost, lock_points.value());
925
926
    // ws.m_modified_fees includes any fee deltas from PrioritiseTransaction
927
1.22M
    ws.m_modified_fees = ws.m_tx_handle->GetModifiedFee();
928
929
1.22M
    ws.m_vsize = ws.m_tx_handle->GetTxSize();
930
931
    // Enforces 0-fee for dust transactions, no incentive to be mined alone
932
1.22M
    if (m_pool.m_opts.require_standard) {
  Branch (932:9): [True: 919k, False: 300k]
933
919k
        if (!PreCheckEphemeralTx(*ptx, m_pool.m_opts.dust_relay_feerate, ws.m_base_fees, ws.m_modified_fees, state)) {
  Branch (933:13): [True: 161k, False: 757k]
934
161k
            return false; // state filled in by PreCheckEphemeralTx
935
161k
        }
936
919k
    }
937
938
1.05M
    if (nSigOpsCost > MAX_STANDARD_TX_SIGOPS_COST)
  Branch (938:9): [True: 54, False: 1.05M]
939
54
        return state.Invalid(TxValidationResult::TX_NOT_STANDARD, "bad-txns-too-many-sigops",
940
54
                strprintf("%d", nSigOpsCost));
941
942
    // No individual transactions are allowed below the mempool min feerate except from disconnected
943
    // blocks and transactions in a package. Package transactions will be checked using package
944
    // feerate later.
945
1.05M
    if (!bypass_limits && !args.m_package_feerates && !CheckFeeRate(ws.m_vsize, ws.m_modified_fees, state)) return false;
  Branch (945:9): [True: 903k, False: 155k]
  Branch (945:27): [True: 743k, False: 159k]
  Branch (945:55): [True: 85.7k, False: 657k]
946
947
972k
    ws.m_iters_conflicting = m_pool.GetIterSet(ws.m_conflicts);
948
949
972k
    ws.m_parents = m_pool.GetParents(*ws.m_tx_handle);
950
951
972k
    if (!args.m_bypass_limits) {
  Branch (951:9): [True: 817k, False: 155k]
952
        // Perform the TRUC checks, using the in-mempool parents.
953
817k
        if (const auto err{SingleTRUCChecks(m_pool, ws.m_ptx, ws.m_parents, ws.m_conflicts, ws.m_vsize)}) {
  Branch (953:24): [True: 25.1k, False: 792k]
954
            // Single transaction contexts only.
955
25.1k
            if (args.m_allow_sibling_eviction && err->second != nullptr) {
  Branch (955:17): [True: 18.6k, False: 6.48k]
  Branch (955:50): [True: 3.44k, False: 15.2k]
956
                // We should only be considering where replacement is considered valid as well.
957
3.44k
                Assume(args.m_allow_replacement);
958
                // Potential sibling eviction. Add the sibling to our list of mempool conflicts to be
959
                // included in RBF checks.
960
3.44k
                ws.m_conflicts.insert(err->second->GetHash());
961
                // Adding the sibling to m_iters_conflicting here means that it doesn't count towards
962
                // RBF Carve Out above. This is correct, since removing to-be-replaced transactions from
963
                // the descendant count is done separately in SingleTRUCChecks for TRUC transactions.
964
3.44k
                ws.m_iters_conflicting.insert(m_pool.GetIter(err->second->GetHash()).value());
965
3.44k
                ws.m_sibling_eviction = true;
966
                // The sibling will be treated as part of the to-be-replaced set in ReplacementChecks.
967
                // Note that we are not checking whether it opts in to replaceability via BIP125 or TRUC
968
                // (which is normally done in PreChecks). However, the only way a TRUC transaction can
969
                // have a non-TRUC and non-BIP125 descendant is due to a reorg.
970
21.6k
            } else {
971
21.6k
                return state.Invalid(TxValidationResult::TX_MEMPOOL_POLICY, "TRUC-violation", err->first);
972
21.6k
            }
973
25.1k
        }
974
817k
    }
975
976
    // We want to detect conflicts in any tx in a package to trigger package RBF logic
977
951k
    m_subpackage.m_rbf |= !ws.m_conflicts.empty();
978
951k
    return true;
979
972k
}
980
981
bool MemPoolAccept::ReplacementChecks(Workspace& ws)
982
325k
{
983
325k
    AssertLockHeld(cs_main);
984
325k
    AssertLockHeld(m_pool.cs);
985
986
325k
    const CTransaction& tx = *ws.m_ptx;
987
325k
    const Txid& hash = ws.m_hash;
988
325k
    TxValidationState& state = ws.m_state;
989
990
325k
    CTxMemPool::setEntries all_conflicts;
991
992
    // Calculate all conflicting entries and enforce Rule #5.
993
325k
    if (const auto err_string{GetEntriesForConflicts(tx, m_pool, ws.m_iters_conflicting, all_conflicts)}) {
  Branch (993:20): [True: 0, False: 325k]
994
0
        return state.Invalid(TxValidationResult::TX_MEMPOOL_POLICY,
995
0
                             strprintf("too many potential replacements%s", ws.m_sibling_eviction ? " (including sibling eviction)" : ""), *err_string);
  Branch (995:77): [True: 0, False: 0]
996
0
    }
997
998
    // Check if it's economically rational to mine this transaction rather than the ones it
999
    // replaces and pays for its own relay fees. Enforce Rules #3 and #4.
1000
817k
    for (CTxMemPool::txiter it : all_conflicts) {
  Branch (1000:32): [True: 817k, False: 325k]
1001
817k
        m_subpackage.m_conflicting_fees += it->GetModifiedFee();
1002
817k
        m_subpackage.m_conflicting_size += it->GetTxSize();
1003
817k
    }
1004
1005
325k
    if (const auto err_string{PaysForRBF(m_subpackage.m_conflicting_fees, ws.m_modified_fees, ws.m_vsize,
  Branch (1005:20): [True: 232k, False: 93.0k]
1006
325k
                                         m_pool.m_opts.incremental_relay_feerate, hash)}) {
1007
        // Result may change in a package context
1008
232k
        return state.Invalid(TxValidationResult::TX_RECONSIDERABLE,
1009
232k
                             strprintf("insufficient fee%s", ws.m_sibling_eviction ? " (including sibling eviction)" : ""), *err_string);
  Branch (1009:62): [True: 1.82k, False: 230k]
1010
232k
    }
1011
1012
    // Add all the to-be-removed transactions to the changeset.
1013
237k
    for (auto it : all_conflicts) {
  Branch (1013:18): [True: 237k, False: 93.0k]
1014
237k
        m_subpackage.m_changeset->StageRemoval(it);
1015
237k
    }
1016
1017
    // Run cluster size limit checks and fail if we exceed them.
1018
93.0k
    if (!m_subpackage.m_changeset->CheckMemPoolPolicyLimits()) {
  Branch (1018:9): [True: 1.82k, False: 91.1k]
1019
1.82k
        return state.Invalid(TxValidationResult::TX_MEMPOOL_POLICY, "too-large-cluster", "");
1020
1.82k
    }
1021
1022
91.1k
    if (const auto err_string{ImprovesFeerateDiagram(*m_subpackage.m_changeset)}) {
  Branch (1022:20): [True: 36.8k, False: 54.3k]
1023
        // We checked above for the cluster size limits being respected, so a
1024
        // failure here can only be due to an insufficient fee.
1025
36.8k
        Assume(err_string->first == DiagramCheckError::FAILURE);
1026
36.8k
        return state.Invalid(TxValidationResult::TX_RECONSIDERABLE, "replacement-failed", err_string->second);
1027
36.8k
    }
1028
1029
54.3k
    return true;
1030
91.1k
}
1031
1032
bool MemPoolAccept::PackageRBFChecks(const std::vector<CTransactionRef>& txns,
1033
                                     std::vector<Workspace>& workspaces,
1034
                                     PackageValidationState& package_state)
1035
46.6k
{
1036
46.6k
    AssertLockHeld(cs_main);
1037
46.6k
    AssertLockHeld(m_pool.cs);
1038
1039
46.6k
    assert(std::all_of(txns.cbegin(), txns.cend(), [this](const auto& tx)
  Branch (1039:5): [True: 46.6k, False: 0]
1040
46.6k
                       { return !m_pool.exists(tx->GetHash());}));
1041
1042
46.6k
    assert(txns.size() == workspaces.size());
  Branch (1042:5): [True: 46.6k, False: 0]
1043
1044
    // We're in package RBF context; replacement proposal must be size 2
1045
46.6k
    if (workspaces.size() != 2 || !Assume(IsChildWithParents(txns))) {
  Branch (1045:9): [True: 10.5k, False: 36.0k]
  Branch (1045:9): [True: 10.5k, False: 36.0k]
  Branch (1045:35): [True: 0, False: 36.0k]
1046
10.5k
        return package_state.Invalid(PackageValidationResult::PCKG_POLICY, "package RBF failed: package must be 1-parent-1-child");
1047
10.5k
    }
1048
1049
    // If the package has in-mempool parents, we won't consider a package RBF
1050
    // since it would result in a cluster larger than 2.
1051
    // N.B. To relax this constraint we will need to revisit how CCoinsViewMemPool::PackageAddTransaction
1052
    // is being used inside AcceptMultipleTransactions to track available inputs while processing a package.
1053
    // Specifically we would need to check that the ancestors of the new
1054
    // transactions don't intersect with the set of transactions to be removed
1055
    // due to RBF, which is not checked at all in the package acceptance
1056
    // context.
1057
53.6k
    for (const auto& ws : workspaces) {
  Branch (1057:25): [True: 53.6k, False: 11.5k]
1058
53.6k
        if (!ws.m_parents.empty()) {
  Branch (1058:13): [True: 24.5k, False: 29.1k]
1059
24.5k
            return package_state.Invalid(PackageValidationResult::PCKG_POLICY, "package RBF failed: new transaction cannot have mempool ancestors");
1060
24.5k
        }
1061
53.6k
    }
1062
1063
    // Aggregate all conflicts into one set.
1064
11.5k
    CTxMemPool::setEntries direct_conflict_iters;
1065
23.0k
    for (Workspace& ws : workspaces) {
  Branch (1065:24): [True: 23.0k, False: 11.5k]
1066
        // Aggregate all conflicts into one set.
1067
23.0k
        direct_conflict_iters.merge(ws.m_iters_conflicting);
1068
23.0k
    }
1069
1070
11.5k
    const auto& parent_ws = workspaces[0];
1071
11.5k
    const auto& child_ws = workspaces[1];
1072
1073
    // Don't consider replacements that would cause us to remove a large number of mempool entries.
1074
    // This limit is not increased in a package RBF. Use the aggregate number of transactions.
1075
11.5k
    CTxMemPool::setEntries all_conflicts;
1076
11.5k
    if (const auto err_string{GetEntriesForConflicts(*child_ws.m_ptx, m_pool, direct_conflict_iters,
  Branch (1076:20): [True: 0, False: 11.5k]
1077
11.5k
                                                     all_conflicts)}) {
1078
0
        return package_state.Invalid(PackageValidationResult::PCKG_POLICY,
1079
0
                                     "package RBF failed: too many potential replacements", *err_string);
1080
0
    }
1081
1082
25.8k
    for (CTxMemPool::txiter it : all_conflicts) {
  Branch (1082:32): [True: 25.8k, False: 11.5k]
1083
25.8k
        m_subpackage.m_changeset->StageRemoval(it);
1084
25.8k
        m_subpackage.m_conflicting_fees += it->GetModifiedFee();
1085
25.8k
        m_subpackage.m_conflicting_size += it->GetTxSize();
1086
25.8k
    }
1087
1088
    // Use the child as the transaction for attributing errors to.
1089
11.5k
    const Txid& child_hash = child_ws.m_ptx->GetHash();
1090
11.5k
    if (const auto err_string{PaysForRBF(/*original_fees=*/m_subpackage.m_conflicting_fees,
  Branch (1090:20): [True: 7.42k, False: 4.12k]
1091
11.5k
                                         /*replacement_fees=*/m_subpackage.m_total_modified_fees,
1092
11.5k
                                         /*replacement_vsize=*/m_subpackage.m_total_vsize,
1093
11.5k
                                         m_pool.m_opts.incremental_relay_feerate, child_hash)}) {
1094
7.42k
        return package_state.Invalid(PackageValidationResult::PCKG_POLICY,
1095
7.42k
                                     "package RBF failed: insufficient anti-DoS fees", *err_string);
1096
7.42k
    }
1097
1098
    // Ensure this two transaction package is a "chunk" on its own; we don't want the child
1099
    // to be only paying anti-DoS fees
1100
4.12k
    const CFeeRate parent_feerate(parent_ws.m_modified_fees, parent_ws.m_vsize);
1101
4.12k
    const CFeeRate package_feerate(m_subpackage.m_total_modified_fees, m_subpackage.m_total_vsize);
1102
4.12k
    if (package_feerate <= parent_feerate) {
  Branch (1102:9): [True: 1.44k, False: 2.67k]
1103
1.44k
        return package_state.Invalid(PackageValidationResult::PCKG_POLICY,
1104
1.44k
                                     "package RBF failed: package feerate is less than or equal to parent feerate",
1105
1.44k
                                     strprintf("package feerate %s <= parent feerate is %s", package_feerate.ToString(), parent_feerate.ToString()));
1106
1.44k
    }
1107
1108
    // Run cluster size limit checks and fail if we exceed them.
1109
2.67k
    if (!m_subpackage.m_changeset->CheckMemPoolPolicyLimits()) {
  Branch (1109:9): [True: 0, False: 2.67k]
1110
0
        return package_state.Invalid(PackageValidationResult::PCKG_POLICY, "too-large-cluster", "");
1111
0
    }
1112
1113
    // Check if it's economically rational to mine this package rather than the ones it replaces.
1114
2.67k
    if (const auto err_tup{ImprovesFeerateDiagram(*m_subpackage.m_changeset)}) {
  Branch (1114:20): [True: 1.36k, False: 1.30k]
1115
1.36k
        Assume(err_tup->first == DiagramCheckError::FAILURE);
1116
1.36k
        return package_state.Invalid(PackageValidationResult::PCKG_POLICY,
1117
1.36k
                                     "package RBF failed: " + err_tup.value().second, "");
1118
1.36k
    }
1119
1120
1.30k
    LogDebug(BCLog::TXPACKAGES, "package RBF checks passed: parent %s (wtxid=%s), child %s (wtxid=%s), package hash (%s)\n",
1121
1.30k
        txns.front()->GetHash().ToString(), txns.front()->GetWitnessHash().ToString(),
1122
1.30k
        txns.back()->GetHash().ToString(), txns.back()->GetWitnessHash().ToString(),
1123
1.30k
        GetPackageHash(txns).ToString());
1124
1125
1126
1.30k
    return true;
1127
2.67k
}
1128
1129
bool MemPoolAccept::PolicyScriptChecks(Workspace& ws)
1130
498k
{
1131
498k
    AssertLockHeld(cs_main);
1132
498k
    AssertLockHeld(m_pool.cs);
1133
498k
    const CTransaction& tx = *ws.m_ptx;
1134
498k
    TxValidationState& state = ws.m_state;
1135
1136
498k
    constexpr script_verify_flags scriptVerifyFlags = STANDARD_SCRIPT_VERIFY_FLAGS;
1137
1138
    // Check input scripts and signatures.
1139
    // This is done last to help prevent CPU exhaustion denial-of-service attacks.
1140
498k
    if (!CheckInputScripts(tx, state, m_view, scriptVerifyFlags, true, false, ws.m_precomputed_txdata, GetValidationCache())) {
  Branch (1140:9): [True: 64.7k, False: 433k]
1141
        // Detect a failure due to a missing witness so that p2p code can handle rejection caching appropriately.
1142
64.7k
        if (!tx.HasWitness() && SpendsNonAnchorWitnessProg(tx, m_view)) {
  Branch (1142:13): [True: 35.7k, False: 29.0k]
  Branch (1142:33): [True: 12.7k, False: 22.9k]
1143
12.7k
            state.Invalid(TxValidationResult::TX_WITNESS_STRIPPED,
1144
12.7k
                    state.GetRejectReason(), state.GetDebugMessage());
1145
12.7k
        }
1146
64.7k
        return false; // state filled in by CheckInputScripts
1147
64.7k
    }
1148
1149
433k
    return true;
1150
498k
}
1151
1152
bool MemPoolAccept::ConsensusScriptChecks(Workspace& ws)
1153
368k
{
1154
368k
    AssertLockHeld(cs_main);
1155
368k
    AssertLockHeld(m_pool.cs);
1156
368k
    const CTransaction& tx = *ws.m_ptx;
1157
368k
    const Txid& hash = ws.m_hash;
1158
368k
    TxValidationState& state = ws.m_state;
1159
1160
    // Check again against the current block tip's script verification
1161
    // flags to cache our script execution flags. This is, of course,
1162
    // useless if the next block has different script flags from the
1163
    // previous one, but because the cache tracks script flags for us it
1164
    // will auto-invalidate and we'll just have a few blocks of extra
1165
    // misses on soft-fork activation.
1166
    //
1167
    // This is also useful in case of bugs in the standard flags that cause
1168
    // transactions to pass as valid when they're actually invalid. For
1169
    // instance the STRICTENC flag was incorrectly allowing certain
1170
    // CHECKSIG NOT scripts to pass, even though they were invalid.
1171
    //
1172
    // There is a similar check in CreateNewBlock() to prevent creating
1173
    // invalid blocks (using TestBlockValidity), however allowing such
1174
    // transactions into the mempool can be exploited as a DoS attack.
1175
368k
    script_verify_flags currentBlockScriptVerifyFlags{GetBlockScriptFlags(*m_active_chainstate.m_chain.Tip(), m_active_chainstate.m_chainman)};
1176
368k
    if (!CheckInputsFromMempoolAndCache(tx, state, m_view, m_pool, currentBlockScriptVerifyFlags,
  Branch (1176:9): [True: 0, False: 368k]
1177
368k
                                        ws.m_precomputed_txdata, m_active_chainstate.CoinsTip(), GetValidationCache())) {
1178
0
        LogError("BUG! PLEASE REPORT THIS! CheckInputScripts failed against latest-block but not STANDARD flags %s, %s", hash.ToString(), state.ToString());
1179
0
        return Assume(false);
1180
0
    }
1181
1182
368k
    return true;
1183
368k
}
1184
1185
void MemPoolAccept::FinalizeSubpackage(const ATMPArgs& args)
1186
355k
{
1187
355k
    AssertLockHeld(cs_main);
1188
355k
    AssertLockHeld(m_pool.cs);
1189
1190
355k
    if (!m_subpackage.m_changeset->GetRemovals().empty()) Assume(args.m_allow_replacement);
  Branch (1190:9): [True: 32.3k, False: 322k]
1191
    // Remove conflicting transactions from the mempool
1192
355k
    for (CTxMemPool::txiter it : m_subpackage.m_changeset->GetRemovals())
  Branch (1192:32): [True: 44.2k, False: 355k]
1193
44.2k
    {
1194
44.2k
        std::string log_string = strprintf("replacing mempool tx %s (wtxid=%s, fees=%s, vsize=%s). ",
1195
44.2k
                                      it->GetTx().GetHash().ToString(),
1196
44.2k
                                      it->GetTx().GetWitnessHash().ToString(),
1197
44.2k
                                      it->GetFee(),
1198
44.2k
                                      it->GetTxSize());
1199
44.2k
        FeeFrac feerate{m_subpackage.m_total_modified_fees, int32_t(m_subpackage.m_total_vsize)};
1200
44.2k
        uint256 tx_or_package_hash{};
1201
44.2k
        const bool replaced_with_tx{m_subpackage.m_changeset->GetTxCount() == 1};
1202
44.2k
        if (replaced_with_tx) {
  Branch (1202:13): [True: 42.3k, False: 1.94k]
1203
42.3k
            const CTransaction& tx = m_subpackage.m_changeset->GetAddedTxn(0);
1204
42.3k
            tx_or_package_hash = tx.GetHash().ToUint256();
1205
42.3k
            log_string += strprintf("New tx %s (wtxid=%s, fees=%s, vsize=%s)",
1206
42.3k
                                    tx.GetHash().ToString(),
1207
42.3k
                                    tx.GetWitnessHash().ToString(),
1208
42.3k
                                    feerate.fee,
1209
42.3k
                                    feerate.size);
1210
42.3k
        } else {
1211
1.94k
            tx_or_package_hash = GetPackageHash(m_subpackage.m_changeset->GetAddedTxns());
1212
1.94k
            log_string += strprintf("New package %s with %lu txs, fees=%s, vsize=%s",
1213
1.94k
                                    tx_or_package_hash.ToString(),
1214
1.94k
                                    m_subpackage.m_changeset->GetTxCount(),
1215
1.94k
                                    feerate.fee,
1216
1.94k
                                    feerate.size);
1217
1218
1.94k
        }
1219
44.2k
        LogDebug(BCLog::MEMPOOL, "%s\n", log_string);
1220
44.2k
        TRACEPOINT(mempool, replaced,
1221
44.2k
                it->GetTx().GetHash().data(),
1222
44.2k
                it->GetTxSize(),
1223
44.2k
                it->GetFee(),
1224
44.2k
                std::chrono::duration_cast<std::chrono::duration<std::uint64_t>>(it->GetTime()).count(),
1225
44.2k
                tx_or_package_hash.data(),
1226
44.2k
                feerate.size,
1227
44.2k
                feerate.fee,
1228
44.2k
                replaced_with_tx
1229
44.2k
        );
1230
44.2k
        m_subpackage.m_replaced_transactions.push_back(it->GetSharedTx());
1231
44.2k
    }
1232
355k
    m_subpackage.m_changeset->Apply();
1233
355k
    m_subpackage.m_changeset.reset();
1234
355k
}
1235
1236
bool MemPoolAccept::SubmitPackage(const ATMPArgs& args, std::vector<Workspace>& workspaces,
1237
                                  PackageValidationState& package_state,
1238
                                  std::map<Wtxid, MempoolAcceptResult>& results)
1239
2.89k
{
1240
2.89k
    AssertLockHeld(cs_main);
1241
2.89k
    AssertLockHeld(m_pool.cs);
1242
    // Sanity check: none of the transactions should be in the mempool, and none of the transactions
1243
    // should have a same-txid-different-witness equivalent in the mempool.
1244
2.89k
    assert(std::all_of(workspaces.cbegin(), workspaces.cend(), [this](const auto& ws) { return !m_pool.exists(ws.m_ptx->GetHash()); }));
  Branch (1244:5): [True: 2.89k, False: 0]
1245
1246
2.89k
    bool all_submitted = true;
1247
2.89k
    FinalizeSubpackage(args);
1248
    // ConsensusScriptChecks adds to the script cache and is therefore consensus-critical;
1249
    // CheckInputsFromMempoolAndCache asserts that transactions only spend coins available from the
1250
    // mempool or UTXO set. Submit each transaction to the mempool immediately after calling
1251
    // ConsensusScriptChecks to make the outputs available for subsequent transactions.
1252
7.75k
    for (Workspace& ws : workspaces) {
  Branch (1252:24): [True: 7.75k, False: 2.89k]
1253
7.75k
        if (!ConsensusScriptChecks(ws)) {
  Branch (1253:13): [True: 0, False: 7.75k]
1254
0
            results.emplace(ws.m_ptx->GetWitnessHash(), MempoolAcceptResult::Failure(ws.m_state));
1255
            // Since PolicyScriptChecks() passed, this should never fail.
1256
0
            Assume(false);
1257
0
            all_submitted = false;
1258
0
            package_state.Invalid(PackageValidationResult::PCKG_MEMPOOL_ERROR,
1259
0
                                  strprintf("BUG! PolicyScriptChecks succeeded but ConsensusScriptChecks failed: %s",
1260
0
                                            ws.m_ptx->GetHash().ToString()));
1261
0
        }
1262
        // Remove first failing tx and all subsequent in package
1263
7.75k
        if (!all_submitted) {
  Branch (1263:13): [True: 0, False: 7.75k]
1264
0
            if (!m_subpackage.m_changeset) m_subpackage.m_changeset = m_pool.GetChangeSet();
  Branch (1264:17): [True: 0, False: 0]
1265
0
            m_subpackage.m_changeset->StageRemoval(m_pool.GetIter(ws.m_ptx->GetHash()).value());
1266
0
        }
1267
7.75k
    }
1268
2.89k
    if (!all_submitted) {
  Branch (1268:9): [True: 0, False: 2.89k]
1269
0
        Assume(m_subpackage.m_changeset);
1270
        // This code should be unreachable; it's here as belt-and-suspenders
1271
        // to try to ensure we have no consensus-invalid transactions in the
1272
        // mempool.
1273
0
        m_subpackage.m_changeset->Apply();
1274
0
        m_subpackage.m_changeset.reset();
1275
0
        return false;
1276
0
    }
1277
1278
2.89k
    std::vector<Wtxid> all_package_wtxids;
1279
2.89k
    all_package_wtxids.reserve(workspaces.size());
1280
2.89k
    std::transform(workspaces.cbegin(), workspaces.cend(), std::back_inserter(all_package_wtxids),
1281
7.75k
                   [](const auto& ws) { return ws.m_ptx->GetWitnessHash(); });
1282
1283
2.89k
    if (!m_subpackage.m_replaced_transactions.empty()) {
  Branch (1283:9): [True: 876, False: 2.02k]
1284
876
        LogDebug(BCLog::MEMPOOL, "replaced %u mempool transactions with %u new one(s) for %s additional fees, %d delta bytes\n",
1285
876
                 m_subpackage.m_replaced_transactions.size(), workspaces.size(),
1286
876
                 m_subpackage.m_total_modified_fees - m_subpackage.m_conflicting_fees,
1287
876
                 m_subpackage.m_total_vsize - static_cast<int>(m_subpackage.m_conflicting_size));
1288
876
    }
1289
1290
    // Add successful results. The returned results may change later if LimitMempoolSize() evicts them.
1291
7.75k
    for (Workspace& ws : workspaces) {
  Branch (1291:24): [True: 7.75k, False: 2.89k]
1292
7.75k
        auto iter = m_pool.GetIter(ws.m_ptx->GetHash());
1293
7.75k
        Assume(iter.has_value());
1294
7.75k
        const auto effective_feerate = args.m_package_feerates ? ws.m_package_feerate :
  Branch (1294:40): [True: 7.75k, False: 0]
1295
7.75k
            CFeeRate{ws.m_modified_fees, static_cast<int32_t>(ws.m_vsize)};
1296
7.75k
        const auto effective_feerate_wtxids = args.m_package_feerates ? all_package_wtxids :
  Branch (1296:47): [True: 7.75k, False: 0]
1297
7.75k
            std::vector<Wtxid>{ws.m_ptx->GetWitnessHash()};
1298
7.75k
        results.emplace(ws.m_ptx->GetWitnessHash(),
1299
7.75k
                        MempoolAcceptResult::Success(std::move(m_subpackage.m_replaced_transactions), ws.m_vsize,
1300
7.75k
                                         ws.m_base_fees, effective_feerate, effective_feerate_wtxids));
1301
7.75k
        if (!m_pool.m_opts.signals) continue;
  Branch (1301:13): [True: 0, False: 7.75k]
1302
7.75k
        const CTransaction& tx = *ws.m_ptx;
1303
7.75k
        const auto tx_info = NewMempoolTransactionInfo(ws.m_ptx, ws.m_base_fees,
1304
7.75k
                                                       ws.m_vsize, (*iter)->GetHeight(),
1305
7.75k
                                                       args.m_bypass_limits, args.m_package_submission,
1306
7.75k
                                                       IsCurrentForFeeEstimation(m_active_chainstate),
1307
7.75k
                                                       m_pool.HasNoInputsOf(tx));
1308
7.75k
        m_pool.m_opts.signals->TransactionAddedToMempool(tx_info, m_pool.GetAndIncrementSequence());
1309
7.75k
    }
1310
2.89k
    return all_submitted;
1311
2.89k
}
1312
1313
MempoolAcceptResult MemPoolAccept::AcceptSingleTransactionInternal(const CTransactionRef& ptx, ATMPArgs& args)
1314
2.19M
{
1315
2.19M
    AssertLockHeld(cs_main);
1316
2.19M
    AssertLockHeld(m_pool.cs);
1317
1318
2.19M
    Workspace ws(ptx);
1319
2.19M
    const std::vector<Wtxid> single_wtxid{ws.m_ptx->GetWitnessHash()};
1320
1321
2.19M
    if (!PreChecks(args, ws)) {
  Branch (1321:9): [True: 1.47M, False: 720k]
1322
1.47M
        if (ws.m_state.GetResult() == TxValidationResult::TX_RECONSIDERABLE) {
  Branch (1322:13): [True: 80.0k, False: 1.39M]
1323
            // Failed for fee reasons. Provide the effective feerate and which tx was included.
1324
80.0k
            return MempoolAcceptResult::FeeFailure(ws.m_state, CFeeRate(ws.m_modified_fees, ws.m_vsize), single_wtxid);
1325
80.0k
        }
1326
1.39M
        return MempoolAcceptResult::Failure(ws.m_state);
1327
1.47M
    }
1328
1329
720k
    if (m_subpackage.m_rbf && !ReplacementChecks(ws)) {
  Branch (1329:9): [True: 325k, False: 395k]
  Branch (1329:31): [True: 271k, False: 54.3k]
1330
271k
        if (ws.m_state.GetResult() == TxValidationResult::TX_RECONSIDERABLE) {
  Branch (1330:13): [True: 269k, False: 1.82k]
1331
            // Failed for incentives-based fee reasons. Provide the effective feerate and which tx was included.
1332
269k
            return MempoolAcceptResult::FeeFailure(ws.m_state, CFeeRate(ws.m_modified_fees, ws.m_vsize), single_wtxid);
1333
269k
        }
1334
1.82k
        return MempoolAcceptResult::Failure(ws.m_state);
1335
271k
    }
1336
1337
    // Check if the transaction would exceed the cluster size limit.
1338
449k
    if (!m_subpackage.m_changeset->CheckMemPoolPolicyLimits()) {
  Branch (1338:9): [True: 6.36k, False: 443k]
1339
6.36k
        ws.m_state.Invalid(TxValidationResult::TX_MEMPOOL_POLICY, "too-large-cluster", "");
1340
6.36k
        return MempoolAcceptResult::Failure(ws.m_state);
1341
6.36k
    }
1342
1343
    // Now that we've verified the cluster limit is respected, we can perform
1344
    // calculations involving the full ancestors of the tx.
1345
443k
    if (ws.m_conflicts.size()) {
  Branch (1345:9): [True: 54.3k, False: 388k]
1346
54.3k
        auto ancestors = m_subpackage.m_changeset->CalculateMemPoolAncestors(ws.m_tx_handle);
1347
1348
        // A transaction that spends outputs that would be replaced by it is invalid. Now
1349
        // that we have the set of all ancestors we can detect this
1350
        // pathological case by making sure ws.m_conflicts and this tx's ancestors don't
1351
        // intersect.
1352
54.3k
        if (const auto err_string{EntriesAndTxidsDisjoint(ancestors, ws.m_conflicts, ptx->GetHash())}) {
  Branch (1352:24): [True: 9.04k, False: 45.2k]
1353
            // We classify this as a consensus error because a transaction depending on something it
1354
            // conflicts with would be inconsistent.
1355
9.04k
            ws.m_state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-spends-conflicting-tx", *err_string);
1356
9.04k
            return MempoolAcceptResult::Failure(ws.m_state);
1357
9.04k
        }
1358
54.3k
    }
1359
1360
434k
    m_subpackage.m_total_vsize = ws.m_vsize;
1361
434k
    m_subpackage.m_total_modified_fees = ws.m_modified_fees;
1362
1363
    // Individual modified feerate exceeded caller-defined max; abort
1364
434k
    if (args.m_client_maxfeerate && CFeeRate(ws.m_modified_fees, ws.m_vsize) > args.m_client_maxfeerate.value()) {
  Branch (1364:9): [True: 8.07k, False: 426k]
  Branch (1364:9): [True: 6.42k, False: 427k]
  Branch (1364:37): [True: 6.42k, False: 1.64k]
1365
6.42k
        ws.m_state.Invalid(TxValidationResult::TX_MEMPOOL_POLICY, "max feerate exceeded", "");
1366
6.42k
        return MempoolAcceptResult::Failure(ws.m_state);
1367
6.42k
    }
1368
1369
427k
    if (!args.m_bypass_limits && m_pool.m_opts.require_standard) {
  Branch (1369:9): [True: 292k, False: 135k]
  Branch (1369:34): [True: 196k, False: 95.4k]
1370
196k
        Wtxid dummy_wtxid;
1371
196k
        if (!CheckEphemeralSpends(/*package=*/{ptx}, m_pool.m_opts.dust_relay_feerate, m_pool, ws.m_state, dummy_wtxid)) {
  Branch (1371:13): [True: 4.88k, False: 191k]
1372
4.88k
            return MempoolAcceptResult::Failure(ws.m_state);
1373
4.88k
        }
1374
196k
    }
1375
1376
    // Perform the inexpensive checks first and avoid hashing and signature verification unless
1377
    // those checks pass, to mitigate CPU exhaustion denial-of-service attacks.
1378
422k
    if (!PolicyScriptChecks(ws)) return MempoolAcceptResult::Failure(ws.m_state);
  Branch (1378:9): [True: 61.9k, False: 360k]
1379
1380
360k
    if (!ConsensusScriptChecks(ws)) return MempoolAcceptResult::Failure(ws.m_state);
  Branch (1380:9): [True: 0, False: 360k]
1381
1382
360k
    const CFeeRate effective_feerate{ws.m_modified_fees, static_cast<int32_t>(ws.m_vsize)};
1383
    // Tx was accepted, but not added
1384
360k
    if (args.m_test_accept) {
  Branch (1384:9): [True: 8.51k, False: 352k]
1385
8.51k
        return MempoolAcceptResult::Success(std::move(m_subpackage.m_replaced_transactions), ws.m_vsize,
1386
8.51k
                                            ws.m_base_fees, effective_feerate, single_wtxid);
1387
8.51k
    }
1388
1389
352k
    FinalizeSubpackage(args);
1390
1391
    // Limit the mempool, if appropriate.
1392
352k
    if (!args.m_package_submission && !args.m_bypass_limits) {
  Branch (1392:9): [True: 217k, False: 134k]
  Branch (1392:39): [True: 100k, False: 116k]
1393
100k
        LimitMempoolSize(m_pool, m_active_chainstate.CoinsTip());
1394
        // If mempool contents change, then the m_view cache is dirty. Given this isn't a package
1395
        // submission, we won't be using the cache anymore, but clear it anyway for clarity.
1396
100k
        CleanupTemporaryCoins();
1397
1398
100k
        if (!m_pool.exists(ws.m_hash)) {
  Branch (1398:13): [True: 6.13k, False: 94.6k]
1399
            // The tx no longer meets our (new) mempool minimum feerate but could be reconsidered in a package.
1400
6.13k
            ws.m_state.Invalid(TxValidationResult::TX_RECONSIDERABLE, "mempool full");
1401
6.13k
            return MempoolAcceptResult::FeeFailure(ws.m_state, CFeeRate(ws.m_modified_fees, ws.m_vsize), {ws.m_ptx->GetWitnessHash()});
1402
6.13k
        }
1403
100k
    }
1404
1405
346k
    if (m_pool.m_opts.signals) {
  Branch (1405:9): [True: 346k, False: 0]
1406
346k
        const CTransaction& tx = *ws.m_ptx;
1407
346k
        auto iter = m_pool.GetIter(tx.GetHash());
1408
346k
        Assume(iter.has_value());
1409
346k
        const auto tx_info = NewMempoolTransactionInfo(ws.m_ptx, ws.m_base_fees,
1410
346k
                                                       ws.m_vsize, (*iter)->GetHeight(),
1411
346k
                                                       args.m_bypass_limits, args.m_package_submission,
1412
346k
                                                       IsCurrentForFeeEstimation(m_active_chainstate),
1413
346k
                                                       m_pool.HasNoInputsOf(tx));
1414
346k
        m_pool.m_opts.signals->TransactionAddedToMempool(tx_info, m_pool.GetAndIncrementSequence());
1415
346k
    }
1416
1417
346k
    if (!m_subpackage.m_replaced_transactions.empty()) {
  Branch (1417:9): [True: 31.1k, False: 315k]
1418
31.1k
        LogDebug(BCLog::MEMPOOL, "replaced %u mempool transactions with 1 new transaction for %s additional fees, %d delta bytes\n",
1419
31.1k
                 m_subpackage.m_replaced_transactions.size(),
1420
31.1k
                 ws.m_modified_fees - m_subpackage.m_conflicting_fees,
1421
31.1k
                 ws.m_vsize - static_cast<int>(m_subpackage.m_conflicting_size));
1422
31.1k
    }
1423
1424
346k
    return MempoolAcceptResult::Success(std::move(m_subpackage.m_replaced_transactions), ws.m_vsize, ws.m_base_fees,
1425
346k
                                        effective_feerate, single_wtxid);
1426
352k
}
1427
1428
PackageMempoolAcceptResult MemPoolAccept::AcceptMultipleTransactionsInternal(const std::vector<CTransactionRef>& txns, ATMPArgs& args)
1429
333k
{
1430
333k
    AssertLockHeld(cs_main);
1431
333k
    AssertLockHeld(m_pool.cs);
1432
1433
    // These context-free package limits can be done before taking the mempool lock.
1434
333k
    PackageValidationState package_state;
1435
333k
    if (!IsWellFormedPackage(txns, package_state)) return PackageMempoolAcceptResult(package_state, {});
  Branch (1435:9): [True: 26, False: 333k]
1436
1437
333k
    std::vector<Workspace> workspaces{};
1438
333k
    workspaces.reserve(txns.size());
1439
333k
    std::transform(txns.cbegin(), txns.cend(), std::back_inserter(workspaces),
1440
449k
                   [](const auto& tx) { return Workspace(tx); });
1441
333k
    std::map<Wtxid, MempoolAcceptResult> results;
1442
1443
    // Do all PreChecks first and fail fast to avoid running expensive script checks when unnecessary.
1444
438k
    for (Workspace& ws : workspaces) {
  Branch (1444:24): [True: 438k, False: 124k]
1445
438k
        if (!PreChecks(args, ws)) {
  Branch (1445:13): [True: 208k, False: 230k]
1446
208k
            package_state.Invalid(PackageValidationResult::PCKG_TX, "transaction failed");
1447
            // Exit early to avoid doing pointless work. Update the failed tx result; the rest are unfinished.
1448
208k
            results.emplace(ws.m_ptx->GetWitnessHash(), MempoolAcceptResult::Failure(ws.m_state));
1449
208k
            return PackageMempoolAcceptResult(package_state, std::move(results));
1450
208k
        }
1451
1452
        // Individual modified feerate exceeded caller-defined max; abort
1453
        // N.B. this doesn't take into account CPFPs. Chunk-aware validation may be more robust.
1454
230k
        if (args.m_client_maxfeerate && CFeeRate(ws.m_modified_fees, ws.m_vsize) > args.m_client_maxfeerate.value()) {
  Branch (1454:13): [True: 1.83k, False: 228k]
  Branch (1454:13): [True: 370, False: 230k]
  Branch (1454:41): [True: 370, False: 1.46k]
1455
            // Need to set failure here both individually and at package level
1456
370
            ws.m_state.Invalid(TxValidationResult::TX_MEMPOOL_POLICY, "max feerate exceeded", "");
1457
370
            package_state.Invalid(PackageValidationResult::PCKG_TX, "transaction failed");
1458
            // Exit early to avoid doing pointless work. Update the failed tx result; the rest are unfinished.
1459
370
            results.emplace(ws.m_ptx->GetWitnessHash(), MempoolAcceptResult::Failure(ws.m_state));
1460
370
            return PackageMempoolAcceptResult(package_state, std::move(results));
1461
370
        }
1462
1463
        // Make the coins created by this transaction available for subsequent transactions in the
1464
        // package to spend. If there are no conflicts within the package, no transaction can spend a coin
1465
        // needed by another transaction in the package. We also need to make sure that no package
1466
        // tx replaces (or replaces the ancestor of) the parent of another package tx. As long as we
1467
        // check these two things, we don't need to track the coins spent.
1468
        // If a package tx conflicts with a mempool tx, PackageRBFChecks() ensures later that any package RBF attempt
1469
        // has *no* in-mempool ancestors, so we don't have to worry about subsequent transactions in
1470
        // same package spending the same in-mempool outpoints. This needs to be revisited for general
1471
        // package RBF.
1472
230k
        m_viewmempool.PackageAddTransaction(ws.m_ptx);
1473
230k
    }
1474
1475
    // At this point we have all in-mempool parents, and we know every transaction's vsize.
1476
    // Run the TRUC checks on the package.
1477
193k
    for (Workspace& ws : workspaces) {
  Branch (1477:24): [True: 193k, False: 122k]
1478
193k
        if (auto err{PackageTRUCChecks(m_pool, ws.m_ptx, ws.m_vsize, txns, ws.m_parents)}) {
  Branch (1478:18): [True: 1.40k, False: 192k]
1479
1.40k
            package_state.Invalid(PackageValidationResult::PCKG_POLICY, "TRUC-violation", err.value());
1480
1.40k
            return PackageMempoolAcceptResult(package_state, {});
1481
1.40k
        }
1482
193k
    }
1483
1484
    // Transactions must meet two minimum feerates: the mempool minimum fee and min relay fee.
1485
    // For transactions consisting of exactly one child and its parents, it suffices to use the
1486
    // package feerate (total modified fees / total virtual size) to check this requirement.
1487
    // Note that this is an aggregate feerate; this function has not checked that there are transactions
1488
    // too low feerate to pay for themselves, or that the child transactions are higher feerate than
1489
    // their parents. Using aggregate feerate may allow "parents pay for child" behavior and permit
1490
    // a child that is below mempool minimum feerate. To avoid these behaviors, callers of
1491
    // AcceptMultipleTransactions need to restrict txns topology (e.g. to ancestor sets) and check
1492
    // the feerates of individuals and subsets.
1493
122k
    m_subpackage.m_total_vsize = std::accumulate(workspaces.cbegin(), workspaces.cend(), int64_t{0},
1494
190k
        [](int64_t sum, auto& ws) { return sum + ws.m_vsize; });
1495
122k
    m_subpackage.m_total_modified_fees = std::accumulate(workspaces.cbegin(), workspaces.cend(), CAmount{0},
1496
190k
        [](CAmount sum, auto& ws) { return sum + ws.m_modified_fees; });
1497
122k
    const CFeeRate package_feerate(m_subpackage.m_total_modified_fees, m_subpackage.m_total_vsize);
1498
122k
    std::vector<Wtxid> all_package_wtxids;
1499
122k
    all_package_wtxids.reserve(workspaces.size());
1500
122k
    std::transform(workspaces.cbegin(), workspaces.cend(), std::back_inserter(all_package_wtxids),
1501
190k
                   [](const auto& ws) { return ws.m_ptx->GetWitnessHash(); });
1502
122k
    TxValidationState placeholder_state;
1503
122k
    if (args.m_package_feerates &&
  Branch (1503:9): [True: 51.8k, False: 71.0k]
1504
122k
        !CheckFeeRate(m_subpackage.m_total_vsize, m_subpackage.m_total_modified_fees, placeholder_state)) {
  Branch (1504:9): [True: 3.15k, False: 48.6k]
1505
3.15k
        package_state.Invalid(PackageValidationResult::PCKG_TX, "transaction failed");
1506
3.15k
        return PackageMempoolAcceptResult(package_state, {{workspaces.back().m_ptx->GetWitnessHash(),
1507
3.15k
            MempoolAcceptResult::FeeFailure(placeholder_state, CFeeRate(m_subpackage.m_total_modified_fees, m_subpackage.m_total_vsize), all_package_wtxids)}});
1508
3.15k
    }
1509
1510
    // Apply package mempool RBF checks.
1511
119k
    if (m_subpackage.m_rbf && !PackageRBFChecks(txns, workspaces, package_state)) {
  Branch (1511:9): [True: 46.6k, False: 73.0k]
  Branch (1511:31): [True: 45.3k, False: 1.30k]
1512
45.3k
        return PackageMempoolAcceptResult(package_state, std::move(results));
1513
45.3k
    }
1514
1515
    // Check if the transactions would exceed the cluster size limit.
1516
74.3k
    if (!m_subpackage.m_changeset->CheckMemPoolPolicyLimits()) {
  Branch (1516:9): [True: 2.09k, False: 72.2k]
1517
2.09k
        package_state.Invalid(PackageValidationResult::PCKG_POLICY, "too-large-cluster", "");
1518
2.09k
        return PackageMempoolAcceptResult(package_state, std::move(results));
1519
2.09k
    }
1520
1521
    // Now that we've bounded the resulting possible ancestry count, check package for dust spends
1522
72.2k
    if (m_pool.m_opts.require_standard) {
  Branch (1522:9): [True: 47.0k, False: 25.2k]
1523
47.0k
        TxValidationState child_state;
1524
47.0k
        Wtxid child_wtxid;
1525
47.0k
        if (!CheckEphemeralSpends(txns, m_pool.m_opts.dust_relay_feerate, m_pool, child_state, child_wtxid)) {
  Branch (1525:13): [True: 1.78k, False: 45.2k]
1526
1.78k
            package_state.Invalid(PackageValidationResult::PCKG_TX, "unspent-dust");
1527
1.78k
            results.emplace(child_wtxid, MempoolAcceptResult::Failure(child_state));
1528
1.78k
            return PackageMempoolAcceptResult(package_state, std::move(results));
1529
1.78k
        }
1530
47.0k
    }
1531
1532
75.3k
    for (Workspace& ws : workspaces) {
  Branch (1532:24): [True: 75.3k, False: 67.6k]
1533
75.3k
        ws.m_package_feerate = package_feerate;
1534
75.3k
        if (!PolicyScriptChecks(ws)) {
  Branch (1534:13): [True: 2.87k, False: 72.4k]
1535
            // Exit early to avoid doing pointless work. Update the failed tx result; the rest are unfinished.
1536
2.87k
            package_state.Invalid(PackageValidationResult::PCKG_TX, "transaction failed");
1537
2.87k
            results.emplace(ws.m_ptx->GetWitnessHash(), MempoolAcceptResult::Failure(ws.m_state));
1538
2.87k
            return PackageMempoolAcceptResult(package_state, std::move(results));
1539
2.87k
        }
1540
72.4k
        if (args.m_test_accept) {
  Branch (1540:13): [True: 64.7k, False: 7.75k]
1541
64.7k
            const auto effective_feerate = args.m_package_feerates ? ws.m_package_feerate :
  Branch (1541:44): [True: 0, False: 64.7k]
1542
64.7k
                CFeeRate{ws.m_modified_fees, static_cast<int32_t>(ws.m_vsize)};
1543
64.7k
            const auto effective_feerate_wtxids = args.m_package_feerates ? all_package_wtxids :
  Branch (1543:51): [True: 0, False: 64.7k]
1544
64.7k
                std::vector<Wtxid>{ws.m_ptx->GetWitnessHash()};
1545
64.7k
            results.emplace(ws.m_ptx->GetWitnessHash(),
1546
64.7k
                            MempoolAcceptResult::Success(std::move(m_subpackage.m_replaced_transactions),
1547
64.7k
                                                         ws.m_vsize, ws.m_base_fees, effective_feerate,
1548
64.7k
                                                         effective_feerate_wtxids));
1549
64.7k
        }
1550
72.4k
    }
1551
1552
67.6k
    if (args.m_test_accept) return PackageMempoolAcceptResult(package_state, std::move(results));
  Branch (1552:9): [True: 64.7k, False: 2.89k]
1553
1554
2.89k
    if (!SubmitPackage(args, workspaces, package_state, results)) {
  Branch (1554:9): [True: 0, False: 2.89k]
1555
        // PackageValidationState filled in by SubmitPackage().
1556
0
        return PackageMempoolAcceptResult(package_state, std::move(results));
1557
0
    }
1558
1559
2.89k
    return PackageMempoolAcceptResult(package_state, std::move(results));
1560
2.89k
}
1561
1562
void MemPoolAccept::CleanupTemporaryCoins()
1563
3.00M
{
1564
    // There are 3 kinds of coins in m_view:
1565
    // (1) Temporary coins from the transactions in subpackage, constructed by m_viewmempool.
1566
    // (2) Mempool coins from transactions in the mempool, constructed by m_viewmempool.
1567
    // (3) Confirmed coins fetched from our current UTXO set.
1568
    //
1569
    // (1) Temporary coins need to be removed, regardless of whether the transaction was submitted.
1570
    // If the transaction was submitted to the mempool, m_viewmempool will be able to fetch them from
1571
    // there. If it wasn't submitted to mempool, it is incorrect to keep them - future calls may try
1572
    // to spend those coins that don't actually exist.
1573
    // (2) Mempool coins also need to be removed. If the mempool contents have changed as a result
1574
    // of submitting or replacing transactions, coins previously fetched from mempool may now be
1575
    // spent or nonexistent. Those coins need to be deleted from m_view.
1576
    // (3) Confirmed coins don't need to be removed. The chainstate has not changed (we are
1577
    // holding cs_main and no blocks have been processed) so the confirmed tx cannot disappear like
1578
    // a mempool tx can. The coin may now be spent after we submitted a tx to mempool, but
1579
    // we have already checked that the package does not have 2 transactions spending the same coin
1580
    // and we check whether a mempool transaction spends conflicting coins (CTxMemPool::GetConflictTx).
1581
    // Keeping them in m_view is an optimization to not re-fetch confirmed coins if we later look up
1582
    // inputs for this transaction again.
1583
4.30M
    for (const auto& outpoint : m_viewmempool.GetNonBaseCoins()) {
  Branch (1583:31): [True: 4.30M, False: 3.00M]
1584
        // In addition to resetting m_viewmempool, we also need to manually delete these coins from
1585
        // m_view because it caches copies of the coins it fetched from m_viewmempool previously.
1586
4.30M
        m_view.Uncache(outpoint);
1587
4.30M
    }
1588
    // This deletes the temporary and mempool coins.
1589
3.00M
    m_viewmempool.Reset();
1590
3.00M
}
1591
1592
PackageMempoolAcceptResult MemPoolAccept::AcceptSubPackage(const std::vector<CTransactionRef>& subpackage, ATMPArgs& args)
1593
800k
{
1594
800k
    AssertLockHeld(::cs_main);
1595
800k
    AssertLockHeld(m_pool.cs);
1596
800k
    auto result = [&]() EXCLUSIVE_LOCKS_REQUIRED(::cs_main, m_pool.cs) {
1597
800k
        if (subpackage.size() > 1) {
  Branch (1597:13): [True: 87.7k, False: 712k]
1598
87.7k
            return AcceptMultipleTransactionsInternal(subpackage, args);
1599
87.7k
        }
1600
712k
        const auto& tx = subpackage.front();
1601
712k
        ATMPArgs single_args = ATMPArgs::SingleInPackageAccept(args);
1602
712k
        const auto single_res = AcceptSingleTransactionInternal(tx, single_args);
1603
712k
        PackageValidationState package_state_wrapped;
1604
712k
        if (single_res.m_result_type != MempoolAcceptResult::ResultType::VALID) {
  Branch (1604:13): [True: 577k, False: 134k]
1605
577k
            package_state_wrapped.Invalid(PackageValidationResult::PCKG_TX, "transaction failed");
1606
577k
        }
1607
712k
        return PackageMempoolAcceptResult(package_state_wrapped, {{tx->GetWitnessHash(), single_res}});
1608
800k
    }();
1609
1610
    // Clean up m_view and m_viewmempool so that other subpackage evaluations don't have access to
1611
    // coins they shouldn't. Keep some coins in order to minimize re-fetching coins from the UTXO set.
1612
    // Clean up package feerate and rbf calculations
1613
800k
    ClearSubPackageState();
1614
1615
800k
    return result;
1616
800k
}
1617
1618
PackageMempoolAcceptResult MemPoolAccept::AcceptPackage(const Package& package, ATMPArgs& args)
1619
462k
{
1620
462k
    Assert(!package.empty());
1621
462k
    AssertLockHeld(cs_main);
1622
    // Used if returning a PackageMempoolAcceptResult directly from this function.
1623
462k
    PackageValidationState package_state_quit_early;
1624
1625
    // There are two topologies we are able to handle through this function:
1626
    // (1) A single transaction
1627
    // (2) A child-with-parents package.
1628
    // Check that the package is well-formed. If it isn't, we won't try to validate any of the
1629
    // transactions and thus won't return any MempoolAcceptResults, just a package-wide error.
1630
1631
    // Context-free package checks.
1632
462k
    if (!IsWellFormedPackage(package, package_state_quit_early)) {
  Branch (1632:9): [True: 74.6k, False: 387k]
1633
74.6k
        return PackageMempoolAcceptResult(package_state_quit_early, {});
1634
74.6k
    }
1635
1636
387k
    if (package.size() > 1 && !IsChildWithParents(package)) {
  Branch (1636:9): [True: 229k, False: 158k]
  Branch (1636:31): [True: 12.1k, False: 216k]
1637
        // All transactions in the package must be a parent of the last transaction. This is just an
1638
        // opportunity for us to fail fast on a context-free check without taking the mempool lock.
1639
12.1k
        package_state_quit_early.Invalid(PackageValidationResult::PCKG_POLICY, "package-not-child-with-parents");
1640
12.1k
        return PackageMempoolAcceptResult(package_state_quit_early, {});
1641
12.1k
    }
1642
1643
375k
    LOCK(m_pool.cs);
1644
    // Stores results from which we will create the returned PackageMempoolAcceptResult.
1645
    // A result may be changed if a mempool transaction is evicted later due to LimitMempoolSize().
1646
375k
    std::map<Wtxid, MempoolAcceptResult> results_final;
1647
    // Results from individual validation which will be returned if no other result is available for
1648
    // this transaction. "Nonfinal" because if a transaction fails by itself but succeeds later
1649
    // (i.e. when evaluated with a fee-bumping child), the result in this map may be discarded.
1650
375k
    std::map<Wtxid, MempoolAcceptResult> individual_results_nonfinal;
1651
    // Tracks whether we think package submission could result in successful entry to the mempool
1652
375k
    bool quit_early{false};
1653
375k
    std::vector<CTransactionRef> txns_package_eval;
1654
739k
    for (const auto& tx : package) {
  Branch (1654:25): [True: 739k, False: 375k]
1655
739k
        const auto& wtxid = tx->GetWitnessHash();
1656
739k
        const auto& txid = tx->GetHash();
1657
        // There are 3 possibilities: already in mempool, same-txid-diff-wtxid already in mempool,
1658
        // or not in mempool. An already confirmed tx is treated as one not in mempool, because all
1659
        // we know is that the inputs aren't available.
1660
739k
        if (m_pool.exists(wtxid)) {
  Branch (1660:13): [True: 28.2k, False: 710k]
1661
            // Exact transaction already exists in the mempool.
1662
            // Node operators are free to set their mempool policies however they please, nodes may receive
1663
            // transactions in different orders, and malicious counterparties may try to take advantage of
1664
            // policy differences to pin or delay propagation of transactions. As such, it's possible for
1665
            // some package transaction(s) to already be in the mempool, and we don't want to reject the
1666
            // entire package in that case (as that could be a censorship vector). De-duplicate the
1667
            // transactions that are already in the mempool, and only call AcceptMultipleTransactions() with
1668
            // the new transactions. This ensures we don't double-count transaction counts and sizes when
1669
            // checking ancestor/descendant limits, or double-count transaction fees for fee-related policy.
1670
28.2k
            const auto& entry{*Assert(m_pool.GetEntry(txid))};
1671
28.2k
            results_final.emplace(wtxid, MempoolAcceptResult::MempoolTx(entry.GetTxSize(), entry.GetFee()));
1672
710k
        } else if (m_pool.exists(txid)) {
  Branch (1672:20): [True: 546, False: 710k]
1673
            // Transaction with the same non-witness data but different witness (same txid,
1674
            // different wtxid) already exists in the mempool.
1675
            //
1676
            // We don't allow replacement transactions right now, so just swap the package
1677
            // transaction for the mempool one. Note that we are ignoring the validity of the
1678
            // package transaction passed in.
1679
            // TODO: allow witness replacement in packages.
1680
546
            const auto& entry{*Assert(m_pool.GetEntry(txid))};
1681
            // Provide the wtxid of the mempool tx so that the caller can look it up in the mempool.
1682
546
            results_final.emplace(wtxid, MempoolAcceptResult::MempoolTxDifferentWitness(entry.GetTx().GetWitnessHash()));
1683
710k
        } else {
1684
            // Transaction does not already exist in the mempool.
1685
            // Try submitting the transaction on its own.
1686
710k
            const auto single_package_res = AcceptSubPackage({tx}, args);
1687
710k
            const auto& single_res = single_package_res.m_tx_results.at(wtxid);
1688
710k
            if (single_res.m_result_type == MempoolAcceptResult::ResultType::VALID) {
  Branch (1688:17): [True: 134k, False: 575k]
1689
                // The transaction succeeded on its own and is now in the mempool. Don't include it
1690
                // in package validation, because its fees should only be "used" once.
1691
134k
                assert(m_pool.exists(wtxid));
  Branch (1691:17): [True: 134k, False: 0]
1692
134k
                results_final.emplace(wtxid, single_res);
1693
575k
            } else if (package.size() == 1 || // If there is only one transaction, no need to retry it "as a package"
  Branch (1693:24): [True: 116k, False: 458k]
1694
575k
                       (single_res.m_state.GetResult() != TxValidationResult::TX_RECONSIDERABLE &&
  Branch (1694:25): [True: 313k, False: 145k]
1695
458k
                       single_res.m_state.GetResult() != TxValidationResult::TX_MISSING_INPUTS)) {
  Branch (1695:24): [True: 137k, False: 175k]
1696
                // Package validation policy only differs from individual policy in its evaluation
1697
                // of feerate. For example, if a transaction fails here due to violation of a
1698
                // consensus rule, the result will not change when it is submitted as part of a
1699
                // package. To minimize the amount of repeated work, unless the transaction fails
1700
                // due to feerate or missing inputs (its parent is a previous transaction in the
1701
                // package that failed due to feerate), don't run package validation. Note that this
1702
                // decision might not make sense if different types of packages are allowed in the
1703
                // future.  Continue individually validating the rest of the transactions, because
1704
                // some of them may still be valid.
1705
254k
                quit_early = true;
1706
254k
                package_state_quit_early.Invalid(PackageValidationResult::PCKG_TX, "transaction failed");
1707
254k
                individual_results_nonfinal.emplace(wtxid, single_res);
1708
320k
            } else {
1709
320k
                individual_results_nonfinal.emplace(wtxid, single_res);
1710
320k
                txns_package_eval.push_back(tx);
1711
320k
            }
1712
710k
        }
1713
739k
    }
1714
1715
375k
    auto multi_submission_result = quit_early || txns_package_eval.empty() ? PackageMempoolAcceptResult(package_state_quit_early, {}) :
  Branch (1715:36): [True: 226k, False: 149k]
  Branch (1715:50): [True: 59.2k, False: 89.9k]
1716
375k
        AcceptSubPackage(txns_package_eval, args);
1717
375k
    PackageValidationState& package_state_final = multi_submission_result.m_state;
1718
1719
    // This is invoked by AcceptSubPackage() already, so this is just here for
1720
    // clarity (since it's not permitted to invoke LimitMempoolSize() while a
1721
    // changeset is outstanding).
1722
375k
    ClearSubPackageState();
1723
1724
    // Make sure we haven't exceeded max mempool size.
1725
    // Package transactions that were submitted to mempool or already in mempool may be evicted.
1726
    // If mempool contents change, then the m_view cache is dirty. It has already been cleared above.
1727
375k
    LimitMempoolSize(m_pool, m_active_chainstate.CoinsTip());
1728
1729
739k
    for (const auto& tx : package) {
  Branch (1729:25): [True: 739k, False: 375k]
1730
739k
        const auto& wtxid = tx->GetWitnessHash();
1731
739k
        if (multi_submission_result.m_tx_results.contains(wtxid)) {
  Branch (1731:13): [True: 48.0k, False: 691k]
1732
            // We shouldn't have re-submitted if the tx result was already in results_final.
1733
48.0k
            Assume(!results_final.contains(wtxid));
1734
            // If it was submitted, check to see if the tx is still in the mempool. It could have
1735
            // been evicted due to LimitMempoolSize() above.
1736
48.0k
            const auto& txresult = multi_submission_result.m_tx_results.at(wtxid);
1737
48.0k
            if (txresult.m_result_type == MempoolAcceptResult::ResultType::VALID && !m_pool.exists(wtxid)) {
  Branch (1737:17): [True: 7.75k, False: 40.3k]
  Branch (1737:85): [True: 4.87k, False: 2.88k]
1738
4.87k
                package_state_final.Invalid(PackageValidationResult::PCKG_TX, "transaction failed");
1739
4.87k
                TxValidationState mempool_full_state;
1740
4.87k
                mempool_full_state.Invalid(TxValidationResult::TX_MEMPOOL_POLICY, "mempool full");
1741
4.87k
                results_final.emplace(wtxid, MempoolAcceptResult::Failure(mempool_full_state));
1742
43.1k
            } else {
1743
43.1k
                results_final.emplace(wtxid, txresult);
1744
43.1k
            }
1745
691k
        } else if (const auto it{results_final.find(wtxid)}; it != results_final.end()) {
  Branch (1745:62): [True: 163k, False: 527k]
1746
            // Already-in-mempool transaction. Check to see if it's still there, as it could have
1747
            // been evicted when LimitMempoolSize() was called.
1748
163k
            Assume(it->second.m_result_type != MempoolAcceptResult::ResultType::INVALID);
1749
163k
            Assume(!individual_results_nonfinal.contains(wtxid));
1750
            // Query by txid to include the same-txid-different-witness ones.
1751
163k
            if (!m_pool.exists(tx->GetHash())) {
  Branch (1751:17): [True: 3.26k, False: 160k]
1752
3.26k
                package_state_final.Invalid(PackageValidationResult::PCKG_TX, "transaction failed");
1753
3.26k
                TxValidationState mempool_full_state;
1754
3.26k
                mempool_full_state.Invalid(TxValidationResult::TX_MEMPOOL_POLICY, "mempool full");
1755
                // Replace the previous result.
1756
3.26k
                results_final.erase(wtxid);
1757
3.26k
                results_final.emplace(wtxid, MempoolAcceptResult::Failure(mempool_full_state));
1758
3.26k
            }
1759
527k
        } else if (const auto it{individual_results_nonfinal.find(wtxid)}; it != individual_results_nonfinal.end()) {
  Branch (1759:76): [True: 527k, False: 0]
1760
527k
            Assume(it->second.m_result_type == MempoolAcceptResult::ResultType::INVALID);
1761
            // Interesting result from previous processing.
1762
527k
            results_final.emplace(wtxid, it->second);
1763
527k
        }
1764
739k
    }
1765
375k
    Assume(results_final.size() == package.size());
1766
375k
    return PackageMempoolAcceptResult(package_state_final, std::move(results_final));
1767
375k
}
1768
1769
} // anon namespace
1770
1771
MempoolAcceptResult AcceptToMemoryPool(Chainstate& active_chainstate, const CTransactionRef& tx,
1772
                                       int64_t accept_time, bool bypass_limits, bool test_accept)
1773
1.48M
{
1774
1.48M
    AssertLockHeld(::cs_main);
1775
1.48M
    assert(active_chainstate.GetMempool() != nullptr);
  Branch (1775:5): [True: 1.48M, False: 0]
1776
1.48M
    CTxMemPool& pool{*active_chainstate.GetMempool()};
1777
1778
1.48M
    std::vector<COutPoint> coins_to_uncache;
1779
1780
1.48M
    auto args = MemPoolAccept::ATMPArgs::SingleAccept(accept_time, bypass_limits, coins_to_uncache, test_accept);
1781
1.48M
    MempoolAcceptResult result = MemPoolAccept(pool, active_chainstate).AcceptSingleTransactionAndCleanup(tx, args);
1782
1783
1.48M
    if (result.m_result_type != MempoolAcceptResult::ResultType::VALID) {
  Branch (1783:9): [True: 1.26M, False: 220k]
1784
        // Remove coins that were not present in the coins cache before calling
1785
        // AcceptSingleTransaction(); this is to prevent memory DoS in case we receive a large
1786
        // number of invalid transactions that attempt to overrun the in-memory coins cache
1787
        // (`CCoinsViewCache::cacheCoins`).
1788
1789
1.26M
        for (const COutPoint& hashTx : coins_to_uncache)
  Branch (1789:38): [True: 1.90M, False: 1.26M]
1790
1.90M
            active_chainstate.CoinsTip().Uncache(hashTx);
1791
1.26M
        TRACEPOINT(mempool, rejected,
1792
1.26M
                tx->GetHash().data(),
1793
1.26M
                result.m_state.GetRejectReason().c_str()
1794
1.26M
        );
1795
1.26M
    }
1796
    // After we've (potentially) uncached entries, ensure our coins cache is still within its size limits
1797
1.48M
    BlockValidationState state_dummy;
1798
1.48M
    active_chainstate.FlushStateToDisk(state_dummy, FlushStateMode::PERIODIC);
1799
1.48M
    return result;
1800
1.48M
}
1801
1802
PackageMempoolAcceptResult ProcessNewPackage(Chainstate& active_chainstate, CTxMemPool& pool,
1803
                                                   const Package& package, bool test_accept, const std::optional<CFeeRate>& client_maxfeerate)
1804
707k
{
1805
707k
    AssertLockHeld(cs_main);
1806
707k
    assert(!package.empty());
  Branch (1806:5): [True: 707k, False: 0]
1807
707k
    assert(std::all_of(package.cbegin(), package.cend(), [](const auto& tx){return tx != nullptr;}));
  Branch (1807:5): [True: 707k, False: 0]
1808
1809
707k
    std::vector<COutPoint> coins_to_uncache;
1810
707k
    auto result = [&]() EXCLUSIVE_LOCKS_REQUIRED(cs_main) {
1811
707k
        AssertLockHeld(cs_main);
1812
707k
        if (test_accept) {
  Branch (1812:13): [True: 245k, False: 462k]
1813
245k
            auto args = MemPoolAccept::ATMPArgs::PackageTestAccept(GetTime(), coins_to_uncache);
1814
245k
            return MemPoolAccept(pool, active_chainstate).AcceptMultipleTransactionsAndCleanup(package, args);
1815
462k
        } else {
1816
462k
            auto args = MemPoolAccept::ATMPArgs::PackageChildWithParents(GetTime(), coins_to_uncache, client_maxfeerate);
1817
462k
            return MemPoolAccept(pool, active_chainstate).AcceptPackage(package, args);
1818
462k
        }
1819
707k
    }();
1820
1821
    // Uncache coins pertaining to transactions that were not submitted to the mempool.
1822
707k
    if (test_accept || result.m_state.IsInvalid()) {
  Branch (1822:9): [True: 245k, False: 462k]
  Branch (1822:24): [True: 403k, False: 59.0k]
1823
1.20M
        for (const COutPoint& hashTx : coins_to_uncache) {
  Branch (1823:38): [True: 1.20M, False: 648k]
1824
1.20M
            active_chainstate.CoinsTip().Uncache(hashTx);
1825
1.20M
        }
1826
648k
    }
1827
    // Ensure the coins cache is still within limits.
1828
707k
    BlockValidationState state_dummy;
1829
707k
    active_chainstate.FlushStateToDisk(state_dummy, FlushStateMode::PERIODIC);
1830
707k
    return result;
1831
707k
}
1832
1833
CAmount GetBlockSubsidy(int nHeight, const Consensus::Params& consensusParams)
1834
1.20M
{
1835
1.20M
    int halvings = nHeight / consensusParams.nSubsidyHalvingInterval;
1836
    // Force block reward to zero when right shift is undefined.
1837
1.20M
    if (halvings >= 64)
  Branch (1837:9): [True: 0, False: 1.20M]
1838
0
        return 0;
1839
1840
1.20M
    CAmount nSubsidy = 50 * COIN;
1841
    // Subsidy is cut in half every 210,000 blocks which will occur approximately every 4 years.
1842
1.20M
    nSubsidy >>= halvings;
1843
1.20M
    return nSubsidy;
1844
1.20M
}
1845
1846
CoinsViews::CoinsViews(DBParams db_params, CoinsViewOptions options)
1847
5.27k
    : m_dbview{std::move(db_params), std::move(options)},
1848
5.27k
      m_catcherview(&m_dbview) {}
1849
1850
void CoinsViews::InitCache(int32_t prevoutfetch_threads)
1851
5.27k
{
1852
5.27k
    AssertLockHeld(::cs_main);
1853
5.27k
    m_cacheview = std::make_unique<CCoinsViewCache>(&m_catcherview);
1854
5.27k
    auto thread_pool{std::make_shared<ThreadPool>("prevout")};
1855
5.27k
    if (prevoutfetch_threads > 0) {
  Branch (1855:9): [True: 0, False: 5.27k]
1856
0
        thread_pool->Start(prevoutfetch_threads);
1857
0
        LogInfo("Block input prevout fetching uses %d additional threads", prevoutfetch_threads);
1858
0
    }
1859
5.27k
    m_connect_block_view = std::make_unique<CoinsViewOverlay>(&*m_cacheview, std::move(thread_pool));
1860
5.27k
}
1861
1862
Chainstate::Chainstate(
1863
    CTxMemPool* mempool,
1864
    BlockManager& blockman,
1865
    ChainstateManager& chainman,
1866
    std::optional<uint256> from_snapshot_blockhash)
1867
5.27k
    : m_mempool(mempool),
1868
5.27k
      m_blockman(blockman),
1869
5.27k
      m_chainman(chainman),
1870
5.27k
      m_assumeutxo(from_snapshot_blockhash ? Assumeutxo::UNVALIDATED : Assumeutxo::VALIDATED),
  Branch (1870:20): [True: 2.12k, False: 3.14k]
1871
5.27k
      m_from_snapshot_blockhash(from_snapshot_blockhash) {}
1872
1873
fs::path Chainstate::StoragePath() const
1874
5.27k
{
1875
5.27k
    fs::path path{m_chainman.m_options.datadir / "chainstate"};
1876
5.27k
    if (m_from_snapshot_blockhash) {
  Branch (1876:9): [True: 2.12k, False: 3.14k]
1877
2.12k
        path += node::SNAPSHOT_CHAINSTATE_SUFFIX;
1878
2.12k
    }
1879
5.27k
    return path;
1880
5.27k
}
1881
1882
const CBlockIndex* Chainstate::SnapshotBase() const
1883
1.47M
{
1884
1.47M
    if (!m_from_snapshot_blockhash) return nullptr;
  Branch (1884:9): [True: 1.45M, False: 18.4k]
1885
18.4k
    if (!m_cached_snapshot_base) m_cached_snapshot_base = Assert(m_chainman.m_blockman.LookupBlockIndex(*m_from_snapshot_blockhash));
  Branch (1885:9): [True: 46, False: 18.4k]
1886
18.4k
    return m_cached_snapshot_base;
1887
1.47M
}
1888
1889
const CBlockIndex* Chainstate::TargetBlock() const
1890
2.78M
{
1891
2.78M
    if (!m_target_blockhash) return nullptr;
  Branch (1891:9): [True: 2.78M, False: 0]
1892
0
    if (!m_cached_target_block) m_cached_target_block = Assert(m_chainman.m_blockman.LookupBlockIndex(*m_target_blockhash));
  Branch (1892:9): [True: 0, False: 0]
1893
0
    return m_cached_target_block;
1894
2.78M
}
1895
1896
void Chainstate::SetTargetBlock(CBlockIndex* block)
1897
0
{
1898
0
    if (block) {
  Branch (1898:9): [True: 0, False: 0]
1899
0
        m_target_blockhash = block->GetBlockHash();
1900
0
    } else {
1901
0
        m_target_blockhash.reset();
1902
0
    }
1903
0
    m_cached_target_block = block;
1904
0
}
1905
1906
void Chainstate::SetTargetBlockHash(uint256 block_hash)
1907
0
{
1908
0
    m_target_blockhash = block_hash;
1909
0
    m_cached_target_block = nullptr;
1910
0
}
1911
1912
void Chainstate::InitCoinsDB(
1913
    size_t cache_size_bytes,
1914
    bool in_memory,
1915
    bool should_wipe)
1916
5.27k
{
1917
5.27k
    m_coins_views = std::make_unique<CoinsViews>(
1918
5.27k
        DBParams{
1919
5.27k
            .path = StoragePath(),
1920
5.27k
            .cache_bytes = cache_size_bytes,
1921
5.27k
            .memory_only = in_memory,
1922
5.27k
            .wipe_data = should_wipe,
1923
5.27k
            .obfuscate = true,
1924
5.27k
            .options = m_chainman.m_options.coins_db},
1925
5.27k
        m_chainman.m_options.coins_view);
1926
1927
5.27k
    m_coinsdb_cache_size_bytes = cache_size_bytes;
1928
5.27k
}
1929
1930
void Chainstate::InitCoinsCache(size_t cache_size_bytes)
1931
5.27k
{
1932
5.27k
    AssertLockHeld(::cs_main);
1933
5.27k
    assert(m_coins_views != nullptr);
  Branch (1933:5): [True: 5.27k, False: 0]
1934
5.27k
    m_coinstip_cache_size_bytes = cache_size_bytes;
1935
5.27k
    m_coins_views->InitCache(m_chainman.m_options.prevoutfetch_threads_num);
1936
5.27k
}
1937
1938
// Lock-free: depends on `m_cached_is_ibd`, which is latched by `UpdateIBDStatus()`.
1939
bool ChainstateManager::IsInitialBlockDownload() const noexcept
1940
5.00M
{
1941
5.00M
    return m_cached_is_ibd.load(std::memory_order_relaxed);
1942
5.00M
}
1943
1944
void Chainstate::CheckForkWarningConditions()
1945
377k
{
1946
377k
    AssertLockHeld(cs_main);
1947
1948
377k
    if (this->GetRole().historical) {
  Branch (1948:9): [True: 0, False: 377k]
1949
0
        return;
1950
0
    }
1951
1952
377k
    if (m_chainman.m_best_invalid && m_chainman.m_best_invalid->nChainWork > m_chain.Tip()->nChainWork + (GetBlockProof(*m_chain.Tip()) * 6)) {
  Branch (1952:9): [True: 89.1k, False: 288k]
  Branch (1952:9): [True: 1.39k, False: 376k]
  Branch (1952:38): [True: 1.39k, False: 87.7k]
1953
1.39k
        LogWarning("Found invalid chain more than 6 blocks longer than our best chain. This could be due to database corruption or consensus incompatibility with peers.");
1954
1.39k
        m_chainman.GetNotifications().warningSet(
1955
1.39k
            kernel::Warning::LARGE_WORK_INVALID_CHAIN,
1956
1.39k
            _("Warning: Found invalid chain more than 6 blocks longer than our best chain. This could be due to database corruption or consensus incompatibility with peers."));
1957
376k
    } else {
1958
376k
        m_chainman.GetNotifications().warningUnset(kernel::Warning::LARGE_WORK_INVALID_CHAIN);
1959
376k
    }
1960
377k
}
1961
1962
// Called both upon regular invalid block discovery *and* InvalidateBlock
1963
void Chainstate::InvalidChainFound(CBlockIndex* pindexNew)
1964
23.6k
{
1965
23.6k
    AssertLockHeld(cs_main);
1966
23.6k
    if (!m_chainman.m_best_invalid || pindexNew->nChainWork > m_chainman.m_best_invalid->nChainWork) {
  Branch (1966:9): [True: 1.41k, False: 22.1k]
  Branch (1966:39): [True: 3.74k, False: 18.4k]
1967
5.16k
        m_chainman.m_best_invalid = pindexNew;
1968
5.16k
    }
1969
23.6k
    SetBlockFailureFlags(pindexNew);
1970
23.6k
    if (m_chainman.m_best_header != nullptr && m_chainman.m_best_header->GetAncestor(pindexNew->nHeight) == pindexNew) {
  Branch (1970:9): [True: 23.6k, False: 0]
  Branch (1970:48): [True: 9.75k, False: 13.8k]
1971
9.75k
        m_chainman.RecalculateBestHeader();
1972
9.75k
    }
1973
1974
23.6k
    LogInfo("%s: invalid block=%s height=%d log2_work=%f date=%s", __func__,
1975
23.6k
      pindexNew->GetBlockHash().ToString(), pindexNew->nHeight,
1976
23.6k
      log(pindexNew->nChainWork.getdouble())/log(2.0), FormatISO8601DateTime(pindexNew->GetBlockTime()));
1977
23.6k
    CBlockIndex *tip = m_chain.Tip();
1978
23.6k
    assert (tip);
  Branch (1978:5): [True: 23.6k, False: 0]
1979
23.6k
    LogInfo("%s: current best=%s height=%d log2_work=%f date=%s", __func__,
1980
23.6k
      tip->GetBlockHash().ToString(), m_chain.Height(), log(tip->nChainWork.getdouble())/log(2.0),
1981
23.6k
      FormatISO8601DateTime(tip->GetBlockTime()));
1982
23.6k
    CheckForkWarningConditions();
1983
23.6k
}
1984
1985
// Same as InvalidChainFound, above, except not called directly from InvalidateBlock,
1986
// which does its own setBlockIndexCandidates management.
1987
void Chainstate::InvalidBlockFound(CBlockIndex* pindex, const BlockValidationState& state)
1988
23.5k
{
1989
23.5k
    AssertLockHeld(cs_main);
1990
23.5k
    if (state.GetResult() != BlockValidationResult::BLOCK_MUTATED) {
  Branch (1990:9): [True: 13.2k, False: 10.3k]
1991
13.2k
        pindex->nStatus |= BLOCK_FAILED_VALID;
1992
13.2k
        m_blockman.m_dirty_blockindex.insert(pindex);
1993
13.2k
        setBlockIndexCandidates.erase(pindex);
1994
13.2k
        InvalidChainFound(pindex);
1995
13.2k
    }
1996
23.5k
}
1997
1998
void UpdateCoins(const CTransaction& tx, CCoinsViewCache& inputs, CTxUndo &txundo, int nHeight)
1999
784k
{
2000
    // mark inputs spent
2001
784k
    if (!tx.IsCoinBase()) {
  Branch (2001:9): [True: 52.1k, False: 732k]
2002
52.1k
        txundo.vprevout.reserve(tx.vin.size());
2003
72.4k
        for (const CTxIn &txin : tx.vin) {
  Branch (2003:32): [True: 72.4k, False: 52.1k]
2004
72.4k
            txundo.vprevout.emplace_back();
2005
72.4k
            bool is_spent = inputs.SpendCoin(txin.prevout, &txundo.vprevout.back());
2006
72.4k
            assert(is_spent);
  Branch (2006:13): [True: 72.4k, False: 0]
2007
72.4k
        }
2008
52.1k
    }
2009
    // add outputs
2010
784k
    AddCoins(inputs, tx, nHeight);
2011
784k
}
2012
2013
1.07M
std::optional<std::pair<ScriptError, std::string>> CScriptCheck::operator()() {
2014
1.07M
    const CScript &scriptSig = ptxTo->vin[nIn].scriptSig;
2015
1.07M
    const CScriptWitness *witness = &ptxTo->vin[nIn].scriptWitness;
2016
1.07M
    ScriptError error{SCRIPT_ERR_UNKNOWN_ERROR};
2017
1.07M
    if (VerifyScript(scriptSig, m_tx_out.scriptPubKey, witness, m_flags, CachingTransactionSignatureChecker(ptxTo, nIn, m_tx_out.nValue, cacheStore, *m_signature_cache, *txdata), &error)) {
  Branch (2017:9): [True: 1.01M, False: 64.7k]
2018
1.01M
        return std::nullopt;
2019
1.01M
    } else {
2020
64.7k
        auto debug_str = strprintf("input %i of %s (wtxid %s), spending %s:%i", nIn, ptxTo->GetHash().ToString(), ptxTo->GetWitnessHash().ToString(), ptxTo->vin[nIn].prevout.hash.ToString(), ptxTo->vin[nIn].prevout.n);
2021
64.7k
        return std::make_pair(error, std::move(debug_str));
2022
64.7k
    }
2023
1.07M
}
2024
2025
ValidationCache::ValidationCache(const size_t script_execution_cache_bytes, const size_t signature_cache_bytes)
2026
3.14k
    : m_signature_cache{signature_cache_bytes}
2027
3.14k
{
2028
    // Setup the salted hasher
2029
3.14k
    uint256 nonce = GetRandHash();
2030
    // We want the nonce to be 64 bytes long to force the hasher to process
2031
    // this chunk, which makes later hash computations more efficient. We
2032
    // just write our 32-byte entropy twice to fill the 64 bytes.
2033
3.14k
    m_script_execution_cache_hasher.Write(nonce.begin(), 32);
2034
3.14k
    m_script_execution_cache_hasher.Write(nonce.begin(), 32);
2035
2036
3.14k
    const auto [num_elems, approx_size_bytes] = m_script_execution_cache.setup_bytes(script_execution_cache_bytes);
2037
3.14k
    LogInfo("Using %zu MiB out of %zu MiB requested for script execution cache, able to store %zu elements",
2038
3.14k
              approx_size_bytes >> 20, script_execution_cache_bytes >> 20, num_elems);
2039
3.14k
}
2040
2041
/**
2042
 * Check whether all of this transaction's input scripts succeed.
2043
 *
2044
 * This involves ECDSA signature checks so can be computationally intensive. This function should
2045
 * only be called after the cheap sanity checks in CheckTxInputs passed.
2046
 *
2047
 * If pvChecks is not nullptr, script checks are pushed onto it instead of being performed inline. Any
2048
 * script checks which are not necessary (eg due to script execution cache hits) are, obviously,
2049
 * not pushed onto pvChecks/run.
2050
 *
2051
 * Setting cacheSigStore/cacheFullScriptStore to false will remove elements from the corresponding cache
2052
 * which are matched. This is useful for checking blocks where we will likely never need the cache
2053
 * entry again.
2054
 *
2055
 * Note that we may set state.reason to NOT_STANDARD for extra soft-fork flags in flags, block-checking
2056
 * callers should probably reset it to CONSENSUS in such cases.
2057
 *
2058
 * Non-static (and redeclared) in src/test/txvalidationcache_tests.cpp
2059
 */
2060
bool CheckInputScripts(const CTransaction& tx, TxValidationState& state,
2061
                       const CCoinsViewCache& inputs, script_verify_flags flags, bool cacheSigStore,
2062
                       bool cacheFullScriptStore, PrecomputedTransactionData& txdata,
2063
                       ValidationCache& validation_cache,
2064
                       std::vector<CScriptCheck>* pvChecks)
2065
919k
{
2066
919k
    if (tx.IsCoinBase()) return true;
  Branch (2066:9): [True: 0, False: 919k]
2067
2068
919k
    if (pvChecks) {
  Branch (2068:9): [True: 0, False: 919k]
2069
0
        pvChecks->reserve(tx.vin.size());
2070
0
    }
2071
2072
    // First check if script executions have been cached with the same
2073
    // flags. Note that this assumes that the inputs provided are
2074
    // correct (ie that the transaction hash which is in tx's prevouts
2075
    // properly commits to the scriptPubKey in the inputs view of that
2076
    // transaction).
2077
919k
    uint256 hashCacheEntry;
2078
919k
    CSHA256 hasher = validation_cache.ScriptExecutionCacheHasher();
2079
919k
    hasher.Write(UCharCast(tx.GetWitnessHash().begin()), 32).Write((unsigned char*)&flags, sizeof(flags)).Finalize(hashCacheEntry.begin());
2080
919k
    AssertLockHeld(cs_main); //TODO: Remove this requirement by making CuckooCache not require external locks
2081
919k
    if (validation_cache.m_script_execution_cache.contains(hashCacheEntry, !cacheFullScriptStore)) {
  Branch (2081:9): [True: 323k, False: 595k]
2082
323k
        return true;
2083
323k
    }
2084
2085
595k
    if (!txdata.m_spent_outputs_ready) {
  Branch (2085:9): [True: 498k, False: 96.6k]
2086
498k
        std::vector<CTxOut> spent_outputs;
2087
498k
        spent_outputs.reserve(tx.vin.size());
2088
2089
924k
        for (const auto& txin : tx.vin) {
  Branch (2089:31): [True: 924k, False: 498k]
2090
924k
            const COutPoint& prevout = txin.prevout;
2091
924k
            const Coin& coin = inputs.AccessCoin(prevout);
2092
924k
            assert(!coin.IsSpent());
  Branch (2092:13): [True: 924k, False: 0]
2093
924k
            spent_outputs.emplace_back(coin.out);
2094
924k
        }
2095
498k
        txdata.Init(tx, std::move(spent_outputs));
2096
498k
    }
2097
595k
    assert(txdata.m_spent_outputs.size() == tx.vin.size());
  Branch (2097:5): [True: 595k, False: 0]
2098
2099
1.60M
    for (unsigned int i = 0; i < tx.vin.size(); i++) {
  Branch (2099:30): [True: 1.07M, False: 530k]
2100
2101
        // We very carefully only pass in things to CScriptCheck which
2102
        // are clearly committed to by tx' witness hash. This provides
2103
        // a sanity check that our caching is not introducing consensus
2104
        // failures through additional data in, eg, the coins being
2105
        // spent being checked as a part of CScriptCheck.
2106
2107
        // Verify signature
2108
1.07M
        CScriptCheck check(txdata.m_spent_outputs[i], tx, validation_cache.m_signature_cache, i, flags, cacheSigStore, &txdata);
2109
1.07M
        if (pvChecks) {
  Branch (2109:13): [True: 0, False: 1.07M]
2110
0
            pvChecks->emplace_back(std::move(check));
2111
1.07M
        } else if (auto result = check(); result.has_value()) {
  Branch (2111:43): [True: 64.7k, False: 1.01M]
2112
            // Tx failures never trigger disconnections/bans.
2113
            // This is so that network splits aren't triggered
2114
            // either due to non-consensus relay policies (such as
2115
            // non-standard DER encodings or non-null dummy
2116
            // arguments) or due to new consensus rules introduced in
2117
            // soft forks.
2118
64.7k
            if (flags & STANDARD_NOT_MANDATORY_VERIFY_FLAGS) {
  Branch (2118:17): [True: 64.7k, False: 0]
2119
64.7k
                return state.Invalid(TxValidationResult::TX_NOT_STANDARD, strprintf("mempool-script-verify-flag-failed (%s)", ScriptErrorString(result->first)), result->second);
2120
64.7k
            } else {
2121
0
                return state.Invalid(TxValidationResult::TX_CONSENSUS, strprintf("block-script-verify-flag-failed (%s)", ScriptErrorString(result->first)), result->second);
2122
0
            }
2123
64.7k
        }
2124
1.07M
    }
2125
2126
530k
    if (cacheFullScriptStore && !pvChecks) {
  Branch (2126:9): [True: 96.6k, False: 434k]
  Branch (2126:33): [True: 96.6k, False: 0]
2127
        // We executed all of the provided scripts, and were told to
2128
        // cache the result. Do so now.
2129
96.6k
        validation_cache.m_script_execution_cache.insert(hashCacheEntry);
2130
96.6k
    }
2131
2132
530k
    return true;
2133
595k
}
2134
2135
bool FatalError(Notifications& notifications, BlockValidationState& state, const bilingual_str& message)
2136
0
{
2137
0
    notifications.fatalError(message);
2138
0
    return state.Error(message.original);
2139
0
}
2140
2141
/**
2142
 * Restore the UTXO in a Coin at a given COutPoint
2143
 * @param undo The Coin to be restored.
2144
 * @param view The coins view to which to apply the changes.
2145
 * @param out The out point that corresponds to the tx input.
2146
 * @return A DisconnectResult as an int
2147
 */
2148
int ApplyTxInUndo(Coin&& undo, CCoinsViewCache& view, const COutPoint& out)
2149
0
{
2150
0
    bool fClean = true;
2151
2152
0
    if (view.HaveCoin(out)) fClean = false; // overwriting transaction output
  Branch (2152:9): [True: 0, False: 0]
2153
2154
0
    if (undo.nHeight == 0) {
  Branch (2154:9): [True: 0, False: 0]
2155
        // Missing undo metadata (height and coinbase). Older versions included this
2156
        // information only in undo records for the last spend of a transactions'
2157
        // outputs. This implies that it must be present for some other output of the same tx.
2158
0
        const Coin& alternate = AccessByTxid(view, out.hash);
2159
0
        if (!alternate.IsSpent()) {
  Branch (2159:13): [True: 0, False: 0]
2160
0
            undo.nHeight = alternate.nHeight;
2161
0
            undo.fCoinBase = alternate.fCoinBase;
2162
0
        } else {
2163
0
            return DISCONNECT_FAILED; // adding output for transaction without known metadata
2164
0
        }
2165
0
    }
2166
    // If the coin already exists as an unspent coin in the cache, then the
2167
    // possible_overwrite parameter to AddCoin must be set to true. We have
2168
    // already checked whether an unspent coin exists above using HaveCoin, so
2169
    // we don't need to guess. When fClean is false, an unspent coin already
2170
    // existed and it is an overwrite.
2171
0
    view.AddCoin(out, std::move(undo), !fClean);
2172
2173
0
    return fClean ? DISCONNECT_OK : DISCONNECT_UNCLEAN;
  Branch (2173:12): [True: 0, False: 0]
2174
0
}
2175
2176
/** Undo the effects of this block (with given index) on the UTXO set represented by coins.
2177
 *  When FAILED is returned, view is left in an indeterminate state. */
2178
DisconnectResult Chainstate::DisconnectBlock(const CBlock& block, const CBlockIndex* pindex, CCoinsViewCache& view)
2179
0
{
2180
0
    AssertLockHeld(::cs_main);
2181
0
    bool fClean = true;
2182
2183
0
    CBlockUndo blockUndo;
2184
0
    if (!m_blockman.ReadBlockUndo(blockUndo, *pindex)) {
  Branch (2184:9): [True: 0, False: 0]
2185
0
        LogError("DisconnectBlock(): failure reading undo data\n");
2186
0
        return DISCONNECT_FAILED;
2187
0
    }
2188
2189
0
    if (blockUndo.vtxundo.size() + 1 != block.vtx.size()) {
  Branch (2189:9): [True: 0, False: 0]
2190
0
        LogError("DisconnectBlock(): block and undo data inconsistent\n");
2191
0
        return DISCONNECT_FAILED;
2192
0
    }
2193
2194
    // Ignore blocks that contain transactions which are 'overwritten' by later transactions,
2195
    // unless those are already completely spent.
2196
    // See https://github.com/bitcoin/bitcoin/issues/22596 for additional information.
2197
    // Note: the blocks specified here are different than the ones used in ConnectBlock because DisconnectBlock
2198
    // unwinds the blocks in reverse. As a result, the inconsistency is not discovered until the earlier
2199
    // blocks with the duplicate coinbase transactions are disconnected.
2200
0
    bool fEnforceBIP30 = !((pindex->nHeight==91722 && pindex->GetBlockHash() == uint256{"00000000000271a2dc26e7667f8419f2e15416dc6955e5a6c6cdf3f2574dd08e"}) ||
  Branch (2200:29): [True: 0, False: 0]
  Branch (2200:55): [True: 0, False: 0]
2201
0
                           (pindex->nHeight==91812 && pindex->GetBlockHash() == uint256{"00000000000af0aed4792b1acee3d966af36cf5def14935db8de83d6f9306f2f"}));
  Branch (2201:29): [True: 0, False: 0]
  Branch (2201:55): [True: 0, False: 0]
2202
2203
    // undo transactions in reverse order
2204
0
    for (int i = block.vtx.size() - 1; i >= 0; i--) {
  Branch (2204:40): [True: 0, False: 0]
2205
0
        const CTransaction &tx = *(block.vtx[i]);
2206
0
        Txid hash = tx.GetHash();
2207
0
        bool is_coinbase = tx.IsCoinBase();
2208
0
        bool is_bip30_exception = (is_coinbase && !fEnforceBIP30);
  Branch (2208:36): [True: 0, False: 0]
  Branch (2208:51): [True: 0, False: 0]
2209
2210
        // Check that all outputs are available and match the outputs in the block itself
2211
        // exactly.
2212
0
        for (size_t o = 0; o < tx.vout.size(); o++) {
  Branch (2212:28): [True: 0, False: 0]
2213
0
            if (!tx.vout[o].scriptPubKey.IsUnspendable()) {
  Branch (2213:17): [True: 0, False: 0]
2214
0
                COutPoint out(hash, o);
2215
0
                Coin coin;
2216
0
                bool is_spent = view.SpendCoin(out, &coin);
2217
0
                if (!is_spent || tx.vout[o] != coin.out || pindex->nHeight != coin.nHeight || is_coinbase != coin.IsCoinBase()) {
  Branch (2217:21): [True: 0, False: 0]
  Branch (2217:34): [True: 0, False: 0]
  Branch (2217:60): [True: 0, False: 0]
  Branch (2217:95): [True: 0, False: 0]
2218
0
                    if (!is_bip30_exception) {
  Branch (2218:25): [True: 0, False: 0]
2219
0
                        fClean = false; // transaction output mismatch
2220
0
                    }
2221
0
                }
2222
0
            }
2223
0
        }
2224
2225
        // restore inputs
2226
0
        if (i > 0) { // not coinbases
  Branch (2226:13): [True: 0, False: 0]
2227
0
            CTxUndo &txundo = blockUndo.vtxundo[i-1];
2228
0
            if (txundo.vprevout.size() != tx.vin.size()) {
  Branch (2228:17): [True: 0, False: 0]
2229
0
                LogError("DisconnectBlock(): transaction and undo data inconsistent\n");
2230
0
                return DISCONNECT_FAILED;
2231
0
            }
2232
0
            for (unsigned int j = tx.vin.size(); j > 0;) {
  Branch (2232:50): [True: 0, False: 0]
2233
0
                --j;
2234
0
                const COutPoint& out = tx.vin[j].prevout;
2235
0
                int res = ApplyTxInUndo(std::move(txundo.vprevout[j]), view, out);
2236
0
                if (res == DISCONNECT_FAILED) return DISCONNECT_FAILED;
  Branch (2236:21): [True: 0, False: 0]
2237
0
                fClean = fClean && res != DISCONNECT_UNCLEAN;
  Branch (2237:26): [True: 0, False: 0]
  Branch (2237:36): [True: 0, False: 0]
2238
0
            }
2239
            // At this point, all of txundo.vprevout should have been moved out.
2240
0
        }
2241
0
    }
2242
2243
    // move best block pointer to prevout block
2244
0
    view.SetBestBlock(pindex->pprev->GetBlockHash());
2245
2246
0
    return fClean ? DISCONNECT_OK : DISCONNECT_UNCLEAN;
  Branch (2246:12): [True: 0, False: 0]
2247
0
}
2248
2249
script_verify_flags GetBlockScriptFlags(const CBlockIndex& block_index, const ChainstateManager& chainman)
2250
1.10M
{
2251
1.10M
    const Consensus::Params& consensusparams = chainman.GetConsensus();
2252
2253
    // BIP16 didn't become active until Apr 1 2012 (on mainnet, and
2254
    // retroactively applied to testnet)
2255
    // However, only one historical block violated the P2SH rules (on both
2256
    // mainnet and testnet).
2257
    // Similarly, only one historical block violated the TAPROOT rules on
2258
    // mainnet.
2259
    // For simplicity, always leave P2SH+WITNESS+TAPROOT on except for the two
2260
    // violating blocks.
2261
1.10M
    script_verify_flags flags{SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_TAPROOT};
2262
1.10M
    const auto it{consensusparams.script_flag_exceptions.find(*Assert(block_index.phashBlock))};
2263
1.10M
    if (it != consensusparams.script_flag_exceptions.end()) {
  Branch (2263:9): [True: 0, False: 1.10M]
2264
0
        flags = it->second;
2265
0
    }
2266
2267
    // Enforce the DERSIG (BIP66) rule
2268
1.10M
    if (DeploymentActiveAt(block_index, chainman, Consensus::DEPLOYMENT_DERSIG)) {
  Branch (2268:9): [True: 1.10M, False: 4]
2269
1.10M
        flags |= SCRIPT_VERIFY_DERSIG;
2270
1.10M
    }
2271
2272
    // Enforce CHECKLOCKTIMEVERIFY (BIP65)
2273
1.10M
    if (DeploymentActiveAt(block_index, chainman, Consensus::DEPLOYMENT_CLTV)) {
  Branch (2273:9): [True: 1.10M, False: 4]
2274
1.10M
        flags |= SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY;
2275
1.10M
    }
2276
2277
    // Enforce CHECKSEQUENCEVERIFY (BIP112)
2278
1.10M
    if (DeploymentActiveAt(block_index, chainman, Consensus::DEPLOYMENT_CSV)) {
  Branch (2278:9): [True: 1.10M, False: 4]
2279
1.10M
        flags |= SCRIPT_VERIFY_CHECKSEQUENCEVERIFY;
2280
1.10M
    }
2281
2282
    // Enforce BIP147 NULLDUMMY (activated simultaneously with segwit)
2283
1.10M
    if (DeploymentActiveAt(block_index, chainman, Consensus::DEPLOYMENT_SEGWIT)) {
  Branch (2283:9): [True: 1.10M, False: 0]
2284
1.10M
        flags |= SCRIPT_VERIFY_NULLDUMMY;
2285
1.10M
    }
2286
2287
1.10M
    return flags;
2288
1.10M
}
2289
2290
2291
/** Apply the effects of this block (with given index) on the UTXO set represented by coins.
2292
 *  Validity checks that depend on the UTXO set are also done; ConnectBlock()
2293
 *  can fail if those validity checks fail (among other reasons). */
2294
bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state, CBlockIndex* pindex,
2295
                               CCoinsViewCache& view, bool fJustCheck)
2296
735k
{
2297
735k
    AssertLockHeld(cs_main);
2298
735k
    assert(pindex);
  Branch (2298:5): [True: 735k, False: 0]
2299
2300
735k
    uint256 block_hash{block.GetHash()};
2301
735k
    assert(*pindex->phashBlock == block_hash);
  Branch (2301:5): [True: 735k, False: 0]
2302
2303
735k
    const auto time_start{SteadyClock::now()};
2304
735k
    const CChainParams& params{m_chainman.GetParams()};
2305
2306
    // Check it again in case a previous version let a bad block in
2307
    // NOTE: We don't currently (re-)invoke ContextualCheckBlock() or
2308
    // ContextualCheckBlockHeader() here. This means that if we add a new
2309
    // consensus rule that is enforced in one of those two functions, then we
2310
    // may have let in a block that violates the rule prior to updating the
2311
    // software, and we would NOT be enforcing the rule here. Fully solving
2312
    // upgrade from one software version to the next after a consensus rule
2313
    // change is potentially tricky and issue-specific (see NeedsRedownload()
2314
    // for one approach that was used for BIP 141 deployment).
2315
    // Also, currently the rule against blocks more than 2 hours in the future
2316
    // is enforced in ContextualCheckBlockHeader(); we wouldn't want to
2317
    // re-enforce that rule here (at least until we make it impossible for
2318
    // the clock to go backward).
2319
735k
    if (!CheckBlock(block, state, params.GetConsensus(), !fJustCheck, !fJustCheck)) {
  Branch (2319:9): [True: 0, False: 735k]
2320
0
        if (state.GetResult() == BlockValidationResult::BLOCK_MUTATED) {
  Branch (2320:13): [True: 0, False: 0]
2321
            // We don't write down blocks to disk if they may have been
2322
            // corrupted, so this should be impossible unless we're having hardware
2323
            // problems.
2324
0
            return FatalError(m_chainman.GetNotifications(), state, _("Corrupt block found indicating potential hardware failure."));
2325
0
        }
2326
0
        LogError("%s: Consensus::CheckBlock: %s\n", __func__, state.ToString());
2327
0
        return false;
2328
0
    }
2329
2330
    // verify that the view's current state corresponds to the previous block
2331
735k
    uint256 hashPrevBlock = pindex->pprev == nullptr ? uint256() : pindex->pprev->GetBlockHash();
  Branch (2331:29): [True: 3.14k, False: 732k]
2332
735k
    assert(hashPrevBlock == view.GetBestBlock());
  Branch (2332:5): [True: 735k, False: 0]
2333
2334
735k
    m_chainman.num_blocks_total++;
2335
2336
    // Special case for the genesis block, skipping connection of its transactions
2337
    // (its coinbase is unspendable)
2338
735k
    if (block_hash == params.GetConsensus().hashGenesisBlock) {
  Branch (2338:9): [True: 3.14k, False: 732k]
2339
3.14k
        if (!fJustCheck)
  Branch (2339:13): [True: 3.14k, False: 0]
2340
3.14k
            view.SetBestBlock(pindex->GetBlockHash());
2341
3.14k
        return true;
2342
3.14k
    }
2343
2344
732k
    const char* script_check_reason;
2345
732k
    if (m_chainman.AssumedValidBlock().IsNull()) {
  Branch (2345:9): [True: 732k, False: 0]
2346
732k
        script_check_reason = "assumevalid=0 (always verify)";
2347
732k
    } else {
2348
0
        constexpr int64_t TWO_WEEKS_IN_SECONDS{60 * 60 * 24 * 7 * 2};
2349
        // We've been configured with the hash of a block which has been externally verified to have a valid history.
2350
        // A suitable default value is included with the software and updated from time to time.  Because validity
2351
        //  relative to a piece of software is an objective fact these defaults can be easily reviewed.
2352
        // This setting doesn't force the selection of any particular chain but makes validating some faster by
2353
        //  effectively caching the result of part of the verification.
2354
0
        BlockMap::const_iterator it{m_blockman.m_block_index.find(m_chainman.AssumedValidBlock())};
2355
0
        if (it == m_blockman.m_block_index.end()) {
  Branch (2355:13): [True: 0, False: 0]
2356
0
            script_check_reason = "assumevalid hash not in headers";
2357
0
        } else if (it->second.GetAncestor(pindex->nHeight) != pindex) {
  Branch (2357:20): [True: 0, False: 0]
2358
0
            script_check_reason = (pindex->nHeight > it->second.nHeight) ? "block height above assumevalid height" : "block not in assumevalid chain";
  Branch (2358:35): [True: 0, False: 0]
2359
0
        } else if (m_chainman.m_best_header->GetAncestor(pindex->nHeight) != pindex) {
  Branch (2359:20): [True: 0, False: 0]
2360
0
            script_check_reason = "block not in best header chain";
2361
0
        } else if (m_chainman.m_best_header->nChainWork < m_chainman.MinimumChainWork()) {
  Branch (2361:20): [True: 0, False: 0]
2362
0
            script_check_reason = "best header chainwork below minimumchainwork";
2363
0
        } else if (GetBlockProofEquivalentTime(*m_chainman.m_best_header, *pindex, *m_chainman.m_best_header, params.GetConsensus()) <= TWO_WEEKS_IN_SECONDS) {
  Branch (2363:20): [True: 0, False: 0]
2364
0
            script_check_reason = "block too recent relative to best header";
2365
0
        } else {
2366
            // This block is a member of the assumed verified chain and an ancestor of the best header.
2367
            // Script verification is skipped when connecting blocks under the
2368
            //  assumevalid block. Assuming the assumevalid block is valid this
2369
            //  is safe because block merkle hashes are still computed and checked,
2370
            // Of course, if an assumed valid block is invalid due to false scriptSigs
2371
            //  this optimization would allow an invalid chain to be accepted.
2372
            // The equivalent time check discourages hash power from extorting the network via DOS attack
2373
            //  into accepting an invalid block through telling users they must manually set assumevalid.
2374
            //  Requiring a software change or burying the invalid block, regardless of the setting, makes
2375
            //  it hard to hide the implication of the demand. This also avoids having release candidates
2376
            //  that are hardly doing any signature verification at all in testing without having to
2377
            //  artificially set the default assumed verified block further back.
2378
            // The test against the minimum chain work prevents the skipping when denied access to any chain at
2379
            //  least as good as the expected chain.
2380
0
            script_check_reason = nullptr;
2381
0
        }
2382
0
    }
2383
2384
732k
    const auto time_1{SteadyClock::now()};
2385
732k
    m_chainman.time_check += time_1 - time_start;
2386
732k
    LogDebug(BCLog::BENCH, "    - Sanity checks: %.2fms [%.2fs (%.2fms/blk)]\n",
2387
732k
             Ticks<MillisecondsDouble>(time_1 - time_start),
2388
732k
             Ticks<SecondsDouble>(m_chainman.time_check),
2389
732k
             Ticks<MillisecondsDouble>(m_chainman.time_check) / m_chainman.num_blocks_total);
2390
2391
    // Do not allow blocks that contain transactions which 'overwrite' older transactions,
2392
    // unless those are already completely spent.
2393
    // If such overwrites are allowed, coinbases and transactions depending upon those
2394
    // can be duplicated to remove the ability to spend the first instance -- even after
2395
    // being sent to another address.
2396
    // See BIP30, CVE-2012-1909, and https://r6.ca/blog/20120206T005236Z.html for more information.
2397
    // This rule was originally applied to all blocks with a timestamp after March 15, 2012, 0:00 UTC.
2398
    // Now that the whole chain is irreversibly beyond that time it is applied to all blocks except the
2399
    // two in the chain that violate it. This prevents exploiting the issue against nodes during their
2400
    // initial block download.
2401
732k
    bool fEnforceBIP30 = !IsBIP30Repeat(*pindex);
2402
2403
    // Once BIP34 activated it was not possible to create new duplicate coinbases and thus other than starting
2404
    // with the 2 existing duplicate coinbase pairs, not possible to create overwriting txs.  But by the
2405
    // time BIP34 activated, in each of the existing pairs the duplicate coinbase had overwritten the first
2406
    // before the first had been spent.  Since those coinbases are sufficiently buried it's no longer possible to create further
2407
    // duplicate transactions descending from the known pairs either.
2408
    // If we're on the known chain at height greater than where BIP34 activated, we can save the db accesses needed for the BIP30 check.
2409
2410
    // BIP34 requires that a block at height X (block X) has its coinbase
2411
    // scriptSig start with a CScriptNum of X (indicated height X).  The above
2412
    // logic of no longer requiring BIP30 once BIP34 activates is flawed in the
2413
    // case that there is a block X before the BIP34 height of 227,931 which has
2414
    // an indicated height Y where Y is greater than X.  The coinbase for block
2415
    // X would also be a valid coinbase for block Y, which could be a BIP30
2416
    // violation.  An exhaustive search of all mainnet coinbases before the
2417
    // BIP34 height which have an indicated height greater than the block height
2418
    // reveals many occurrences. The 3 lowest indicated heights found are
2419
    // 209,921, 490,897, and 1,983,702 and thus coinbases for blocks at these 3
2420
    // heights would be the first opportunity for BIP30 to be violated.
2421
2422
    // The search reveals a great many blocks which have an indicated height
2423
    // greater than 1,983,702, so we simply remove the optimization to skip
2424
    // BIP30 checking for blocks at height 1,983,702 or higher.  Before we reach
2425
    // that block in another 25 years or so, we should take advantage of a
2426
    // future consensus change to do a new and improved version of BIP34 that
2427
    // will actually prevent ever creating any duplicate coinbases in the
2428
    // future.
2429
732k
    static constexpr int BIP34_IMPLIES_BIP30_LIMIT = 1983702;
2430
2431
    // There is no potential to create a duplicate coinbase at block 209,921
2432
    // because this is still before the BIP34 height and so explicit BIP30
2433
    // checking is still active.
2434
2435
    // The final case is block 176,684 which has an indicated height of
2436
    // 490,897. Unfortunately, this issue was not discovered until about 2 weeks
2437
    // before block 490,897 so there was not much opportunity to address this
2438
    // case other than to carefully analyze it and determine it would not be a
2439
    // problem. Block 490,897 was, in fact, mined with a different coinbase than
2440
    // block 176,684, but it is important to note that even if it hadn't been or
2441
    // is remined on an alternate fork with a duplicate coinbase, we would still
2442
    // not run into a BIP30 violation.  This is because the coinbase for 176,684
2443
    // is spent in block 185,956 in transaction
2444
    // d4f7fbbf92f4a3014a230b2dc70b8058d02eb36ac06b4a0736d9d60eaa9e8781.  This
2445
    // spending transaction can't be duplicated because it also spends coinbase
2446
    // 0328dd85c331237f18e781d692c92de57649529bd5edf1d01036daea32ffde29.  This
2447
    // coinbase has an indicated height of over 4.2 billion, and wouldn't be
2448
    // duplicatable until that height, and it's currently impossible to create a
2449
    // chain that long. Nevertheless we may wish to consider a future soft fork
2450
    // which retroactively prevents block 490,897 from creating a duplicate
2451
    // coinbase. The two historical BIP30 violations often provide a confusing
2452
    // edge case when manipulating the UTXO and it would be simpler not to have
2453
    // another edge case to deal with.
2454
2455
    // testnet3 has no blocks before the BIP34 height with indicated heights
2456
    // post BIP34 before approximately height 486,000,000. After block
2457
    // 1,983,702 testnet3 starts doing unnecessary BIP30 checking again.
2458
732k
    assert(pindex->pprev);
  Branch (2458:5): [True: 732k, False: 0]
2459
732k
    CBlockIndex* pindexBIP34height = pindex->pprev->GetAncestor(params.GetConsensus().BIP34Height);
2460
    //Only continue to enforce if we're below BIP34 activation height or the block hash at that height doesn't correspond.
2461
732k
    fEnforceBIP30 = fEnforceBIP30 && (!pindexBIP34height || !(pindexBIP34height->GetBlockHash() == params.GetConsensus().BIP34Hash));
  Branch (2461:21): [True: 732k, False: 0]
  Branch (2461:39): [True: 13.0k, False: 719k]
  Branch (2461:61): [True: 719k, False: 0]
2462
2463
    // TODO: Remove BIP30 checking from block height 1,983,702 on, once we have a
2464
    // consensus change that ensures coinbases at those heights cannot
2465
    // duplicate earlier coinbases.
2466
732k
    if (fEnforceBIP30 || pindex->nHeight >= BIP34_IMPLIES_BIP30_LIMIT) {
  Branch (2466:9): [True: 732k, False: 0]
  Branch (2466:26): [True: 0, False: 0]
2467
797k
        for (const auto& tx : block.vtx) {
  Branch (2467:29): [True: 797k, False: 732k]
2468
2.53M
            for (size_t o = 0; o < tx->vout.size(); o++) {
  Branch (2468:32): [True: 1.74M, False: 797k]
2469
1.74M
                if (view.HaveCoin(COutPoint(tx->GetHash(), o))) {
  Branch (2469:21): [True: 258, False: 1.74M]
2470
258
                    state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-txns-BIP30",
2471
258
                                  "tried to overwrite transaction");
2472
258
                }
2473
1.74M
            }
2474
797k
        }
2475
732k
    }
2476
2477
    // Enforce BIP68 (sequence locks)
2478
732k
    int nLockTimeFlags = 0;
2479
732k
    if (DeploymentActiveAt(*pindex, m_chainman, Consensus::DEPLOYMENT_CSV)) {
  Branch (2479:9): [True: 732k, False: 0]
2480
732k
        nLockTimeFlags |= LOCKTIME_VERIFY_SEQUENCE;
2481
732k
    }
2482
2483
    // Get the script flags for this block
2484
732k
    script_verify_flags flags{GetBlockScriptFlags(*pindex, m_chainman)};
2485
2486
732k
    const auto time_2{SteadyClock::now()};
2487
732k
    m_chainman.time_forks += time_2 - time_1;
2488
732k
    LogDebug(BCLog::BENCH, "    - Fork checks: %.2fms [%.2fs (%.2fms/blk)]\n",
2489
732k
             Ticks<MillisecondsDouble>(time_2 - time_1),
2490
732k
             Ticks<SecondsDouble>(m_chainman.time_forks),
2491
732k
             Ticks<MillisecondsDouble>(m_chainman.time_forks) / m_chainman.num_blocks_total);
2492
2493
732k
    const bool fScriptChecks{!!script_check_reason};
2494
732k
    const kernel::ChainstateRole role{GetRole()};
2495
732k
    if (script_check_reason != m_last_script_check_reason_logged && role.validated && !role.historical) {
  Branch (2495:9): [True: 2.60k, False: 730k]
  Branch (2495:69): [True: 2.60k, False: 0]
  Branch (2495:87): [True: 2.60k, False: 0]
2496
2.60k
        if (fScriptChecks) {
  Branch (2496:13): [True: 2.60k, False: 0]
2497
2.60k
            LogInfo("Enabling script verification at block #%d (%s): %s.",
2498
2.60k
                    pindex->nHeight, block_hash.ToString(), script_check_reason);
2499
2.60k
        } else {
2500
0
            LogInfo("Disabling script verification at block #%d (%s).",
2501
0
                    pindex->nHeight, block_hash.ToString());
2502
0
        }
2503
2.60k
        m_last_script_check_reason_logged = script_check_reason;
2504
2.60k
    }
2505
2506
732k
    CBlockUndo blockundo;
2507
2508
    // Precomputed transaction data pointers must not be invalidated
2509
    // until after `control` has run the script checks (potentially
2510
    // in multiple threads). Preallocate the vector size so a new allocation
2511
    // doesn't invalidate pointers into the vector, and keep txsdata in scope
2512
    // for as long as `control`.
2513
732k
    std::vector<PrecomputedTransactionData> txsdata(block.vtx.size());
2514
732k
    std::optional<CCheckQueueControl<CScriptCheck>> control;
2515
732k
    if (auto& queue = m_chainman.GetCheckQueue(); queue.HasThreads() && fScriptChecks) control.emplace(queue);
  Branch (2515:51): [True: 0, False: 732k]
  Branch (2515:73): [True: 0, False: 0]
2516
2517
732k
    std::vector<int> prevheights;
2518
732k
    CAmount nFees = 0;
2519
732k
    int nInputs = 0;
2520
732k
    int64_t nSigOpsCost = 0;
2521
732k
    blockundo.vtxundo.reserve(block.vtx.size() - 1);
2522
1.51M
    for (unsigned int i = 0; i < block.vtx.size(); i++)
  Branch (2522:30): [True: 794k, False: 723k]
2523
794k
    {
2524
794k
        if (!state.IsValid()) break;
  Branch (2524:13): [True: 250, False: 793k]
2525
793k
        const CTransaction &tx = *(block.vtx[i]);
2526
2527
793k
        nInputs += tx.vin.size();
2528
2529
793k
        if (!tx.IsCoinBase())
  Branch (2529:13): [True: 61.3k, False: 732k]
2530
61.3k
        {
2531
61.3k
            CAmount txfee = 0;
2532
61.3k
            TxValidationState tx_state;
2533
61.3k
            if (!Consensus::CheckTxInputs(tx, tx_state, view, pindex->nHeight, txfee)) {
  Branch (2533:17): [True: 9.18k, False: 52.1k]
2534
                // Any transaction validation failure in ConnectBlock is a block consensus failure
2535
9.18k
                state.Invalid(BlockValidationResult::BLOCK_CONSENSUS,
2536
9.18k
                              tx_state.GetRejectReason(),
2537
9.18k
                              tx_state.GetDebugMessage() + " in transaction " + tx.GetHash().ToString());
2538
9.18k
                break;
2539
9.18k
            }
2540
52.1k
            nFees += txfee;
2541
52.1k
            if (!MoneyRange(nFees)) {
  Branch (2541:17): [True: 0, False: 52.1k]
2542
0
                state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-txns-accumulated-fee-outofrange",
2543
0
                              "accumulated fee in the block out of range");
2544
0
                break;
2545
0
            }
2546
2547
            // Check that transaction is BIP68 final
2548
            // BIP68 lock checks (as opposed to nLockTime checks) must
2549
            // be in ConnectBlock because they require the UTXO set
2550
52.1k
            prevheights.resize(tx.vin.size());
2551
124k
            for (size_t j = 0; j < tx.vin.size(); j++) {
  Branch (2551:32): [True: 72.4k, False: 52.1k]
2552
72.4k
                prevheights[j] = view.AccessCoin(tx.vin[j].prevout).nHeight;
2553
72.4k
            }
2554
2555
52.1k
            if (!SequenceLocks(tx, nLockTimeFlags, prevheights, *pindex)) {
  Branch (2555:17): [True: 5, False: 52.1k]
2556
5
                state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-txns-nonfinal",
2557
5
                              "contains a non-BIP68-final transaction " + tx.GetHash().ToString());
2558
5
                break;
2559
5
            }
2560
52.1k
        }
2561
2562
        // GetTransactionSigOpCost counts 3 types of sigops:
2563
        // * legacy (always)
2564
        // * p2sh (when P2SH enabled in flags and excludes coinbase)
2565
        // * witness (when witness enabled in flags and excludes coinbase)
2566
784k
        nSigOpsCost += GetTransactionSigOpCost(tx, view, flags);
2567
784k
        if (nSigOpsCost > MAX_BLOCK_SIGOPS_COST) {
  Branch (2567:13): [True: 0, False: 784k]
2568
0
            state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-blk-sigops", "too many sigops");
2569
0
            break;
2570
0
        }
2571
2572
784k
        if (!tx.IsCoinBase() && fScriptChecks)
  Branch (2572:13): [True: 52.1k, False: 732k]
  Branch (2572:33): [True: 52.1k, False: 0]
2573
52.1k
        {
2574
52.1k
            bool fCacheResults = fJustCheck; /* Don't cache results if we're actually connecting blocks (still consult the cache, though) */
2575
52.1k
            bool tx_ok;
2576
52.1k
            TxValidationState tx_state;
2577
            // If CheckInputScripts is called with a pointer to a checks vector, the resulting checks are appended to it. In that case
2578
            // they need to be added to control which runs them asynchronously. Otherwise, CheckInputScripts runs the checks before returning.
2579
52.1k
            if (control) {
  Branch (2579:17): [True: 0, False: 52.1k]
2580
0
                std::vector<CScriptCheck> vChecks;
2581
0
                tx_ok = CheckInputScripts(tx, tx_state, view, flags, fCacheResults, fCacheResults, txsdata[i], m_chainman.m_validation_cache, &vChecks);
2582
0
                if (tx_ok) control->Add(std::move(vChecks));
  Branch (2582:21): [True: 0, False: 0]
2583
52.1k
            } else {
2584
52.1k
                tx_ok = CheckInputScripts(tx, tx_state, view, flags, fCacheResults, fCacheResults, txsdata[i], m_chainman.m_validation_cache);
2585
52.1k
            }
2586
52.1k
            if (!tx_ok) {
  Branch (2586:17): [True: 0, False: 52.1k]
2587
                // Any transaction validation failure in ConnectBlock is a block consensus failure
2588
0
                state.Invalid(BlockValidationResult::BLOCK_CONSENSUS,
2589
0
                              tx_state.GetRejectReason(), tx_state.GetDebugMessage());
2590
0
                break;
2591
0
            }
2592
52.1k
        }
2593
2594
784k
        CTxUndo undoDummy;
2595
784k
        if (i > 0) {
  Branch (2595:13): [True: 52.1k, False: 732k]
2596
52.1k
            blockundo.vtxundo.emplace_back();
2597
52.1k
        }
2598
784k
        UpdateCoins(tx, view, i == 0 ? undoDummy : blockundo.vtxundo.back(), pindex->nHeight);
  Branch (2598:31): [True: 732k, False: 52.1k]
2599
784k
    }
2600
732k
    const auto time_3{SteadyClock::now()};
2601
732k
    m_chainman.time_connect += time_3 - time_2;
2602
732k
    LogDebug(BCLog::BENCH, "      - Connect %u transactions: %.2fms (%.3fms/tx, %.3fms/txin) [%.2fs (%.2fms/blk)]\n", (unsigned)block.vtx.size(),
2603
732k
             Ticks<MillisecondsDouble>(time_3 - time_2), Ticks<MillisecondsDouble>(time_3 - time_2) / block.vtx.size(),
2604
732k
             nInputs <= 1 ? 0 : Ticks<MillisecondsDouble>(time_3 - time_2) / (nInputs - 1),
2605
732k
             Ticks<SecondsDouble>(m_chainman.time_connect),
2606
732k
             Ticks<MillisecondsDouble>(m_chainman.time_connect) / m_chainman.num_blocks_total);
2607
2608
732k
    CAmount blockReward = nFees + GetBlockSubsidy(pindex->nHeight, params.GetConsensus());
2609
732k
    if (block.vtx[0]->GetValueOut() > blockReward && state.IsValid()) {
  Branch (2609:9): [True: 0, False: 732k]
  Branch (2609:54): [True: 0, False: 0]
2610
0
        state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-cb-amount",
2611
0
                      strprintf("coinbase pays too much (actual=%d vs limit=%d)", block.vtx[0]->GetValueOut(), blockReward));
2612
0
    }
2613
732k
    if (control) {
  Branch (2613:9): [True: 0, False: 732k]
2614
0
        auto parallel_result = control->Complete();
2615
0
        if (parallel_result.has_value() && state.IsValid()) {
  Branch (2615:13): [True: 0, False: 0]
  Branch (2615:44): [True: 0, False: 0]
2616
0
            state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, strprintf("block-script-verify-flag-failed (%s)", ScriptErrorString(parallel_result->first)), parallel_result->second);
2617
0
        }
2618
0
    }
2619
732k
    if (!state.IsValid()) {
  Branch (2619:9): [True: 9.44k, False: 723k]
2620
9.44k
        LogInfo("Block validation error: %s", state.ToString());
2621
9.44k
        return false;
2622
9.44k
    }
2623
723k
    const auto time_4{SteadyClock::now()};
2624
723k
    m_chainman.time_verify += time_4 - time_2;
2625
723k
    LogDebug(BCLog::BENCH, "    - Verify %u txins: %.2fms (%.3fms/txin) [%.2fs (%.2fms/blk)]\n", nInputs - 1,
2626
723k
             Ticks<MillisecondsDouble>(time_4 - time_2),
2627
723k
             nInputs <= 1 ? 0 : Ticks<MillisecondsDouble>(time_4 - time_2) / (nInputs - 1),
2628
723k
             Ticks<SecondsDouble>(m_chainman.time_verify),
2629
723k
             Ticks<MillisecondsDouble>(m_chainman.time_verify) / m_chainman.num_blocks_total);
2630
2631
723k
    if (fJustCheck) {
  Branch (2631:9): [True: 381k, False: 341k]
2632
381k
        return true;
2633
381k
    }
2634
2635
341k
    if (!m_blockman.WriteBlockUndo(blockundo, state, *pindex)) {
  Branch (2635:9): [True: 0, False: 341k]
2636
0
        return false;
2637
0
    }
2638
2639
341k
    const auto time_5{SteadyClock::now()};
2640
341k
    m_chainman.time_undo += time_5 - time_4;
2641
341k
    LogDebug(BCLog::BENCH, "    - Write undo data: %.2fms [%.2fs (%.2fms/blk)]\n",
2642
341k
             Ticks<MillisecondsDouble>(time_5 - time_4),
2643
341k
             Ticks<SecondsDouble>(m_chainman.time_undo),
2644
341k
             Ticks<MillisecondsDouble>(m_chainman.time_undo) / m_chainman.num_blocks_total);
2645
2646
341k
    if (!pindex->IsValid(BLOCK_VALID_SCRIPTS)) {
  Branch (2646:9): [True: 341k, False: 0]
2647
341k
        pindex->RaiseValidity(BLOCK_VALID_SCRIPTS);
2648
341k
        m_blockman.m_dirty_blockindex.insert(pindex);
2649
341k
    }
2650
2651
    // add this block to the view's block chain
2652
341k
    view.SetBestBlock(pindex->GetBlockHash());
2653
2654
341k
    const auto time_6{SteadyClock::now()};
2655
341k
    m_chainman.time_index += time_6 - time_5;
2656
341k
    LogDebug(BCLog::BENCH, "    - Index writing: %.2fms [%.2fs (%.2fms/blk)]\n",
2657
341k
             Ticks<MillisecondsDouble>(time_6 - time_5),
2658
341k
             Ticks<SecondsDouble>(m_chainman.time_index),
2659
341k
             Ticks<MillisecondsDouble>(m_chainman.time_index) / m_chainman.num_blocks_total);
2660
2661
341k
    TRACEPOINT(validation, block_connected,
2662
341k
        block_hash.data(),
2663
341k
        pindex->nHeight,
2664
341k
        block.vtx.size(),
2665
341k
        nInputs,
2666
341k
        nSigOpsCost,
2667
341k
        Ticks<std::chrono::nanoseconds>(time_5 - time_start)
2668
341k
    );
2669
2670
341k
    return true;
2671
341k
}
2672
2673
CoinsCacheSizeState Chainstate::GetCoinsCacheSizeState()
2674
3.37M
{
2675
3.37M
    AssertLockHeld(::cs_main);
2676
3.37M
    return this->GetCoinsCacheSizeState(
2677
3.37M
        m_coinstip_cache_size_bytes,
2678
3.37M
        m_mempool ? m_mempool->m_opts.max_size_bytes : 0);
  Branch (2678:9): [True: 3.37M, False: 52]
2679
3.37M
}
2680
2681
CoinsCacheSizeState Chainstate::GetCoinsCacheSizeState(
2682
    size_t max_coins_cache_size_bytes,
2683
    size_t max_mempool_size_bytes)
2684
3.37M
{
2685
3.37M
    AssertLockHeld(::cs_main);
2686
3.37M
    const int64_t nMempoolUsage = m_mempool ? m_mempool->DynamicMemoryUsage() : 0;
  Branch (2686:35): [True: 3.37M, False: 52]
2687
3.37M
    int64_t cacheSize = CoinsTip().DynamicMemoryUsage();
2688
3.37M
    int64_t nTotalSpace =
2689
3.37M
        max_coins_cache_size_bytes + std::max<int64_t>(int64_t(max_mempool_size_bytes) - nMempoolUsage, 0);
2690
2691
3.37M
    if (cacheSize > nTotalSpace) {
  Branch (2691:9): [True: 0, False: 3.37M]
2692
0
        LogInfo("Cache size (%s) exceeds total space (%s)\n", cacheSize, nTotalSpace);
2693
0
        return CoinsCacheSizeState::CRITICAL;
2694
3.37M
    } else if (cacheSize > LargeCoinsCacheThreshold(nTotalSpace)) {
  Branch (2694:16): [True: 0, False: 3.37M]
2695
0
        return CoinsCacheSizeState::LARGE;
2696
0
    }
2697
3.37M
    return CoinsCacheSizeState::OK;
2698
3.37M
}
2699
2700
bool Chainstate::FlushStateToDisk(
2701
    BlockValidationState &state,
2702
    FlushStateMode mode,
2703
    int nManualPruneHeight)
2704
3.37M
{
2705
3.37M
    LOCK(cs_main);
2706
3.37M
    assert(this->CanFlushToDisk());
  Branch (2706:5): [True: 3.37M, False: 0]
2707
3.37M
    std::set<int> setFilesToPrune;
2708
3.37M
    bool full_flush_completed = false;
2709
2710
3.37M
    [[maybe_unused]] const size_t coins_count{CoinsTip().GetCacheSize()};
2711
3.37M
    [[maybe_unused]] const size_t coins_mem_usage{CoinsTip().DynamicMemoryUsage()};
2712
2713
3.37M
    try {
2714
3.37M
    {
2715
3.37M
        bool fFlushForPrune = false;
2716
2717
3.37M
        CoinsCacheSizeState cache_state = GetCoinsCacheSizeState();
2718
3.37M
        if (m_blockman.IsPruneMode() && (m_blockman.m_check_for_pruning || nManualPruneHeight > 0) && m_chainman.m_blockman.m_blockfiles_indexed) {
  Branch (2718:13): [True: 0, False: 3.37M]
  Branch (2718:42): [True: 0, False: 0]
  Branch (2718:76): [True: 0, False: 0]
  Branch (2718:103): [True: 0, False: 0]
2719
            // make sure we don't prune above any of the prune locks bestblocks
2720
            // pruning is height-based
2721
0
            int last_prune{m_chain.Height()}; // last height we can prune
2722
0
            std::optional<std::string> limiting_lock; // prune lock that actually was the limiting factor, only used for logging
2723
2724
0
            for (const auto& prune_lock : m_blockman.m_prune_locks) {
  Branch (2724:41): [True: 0, False: 0]
2725
0
                if (prune_lock.second.height_first == std::numeric_limits<int>::max()) continue;
  Branch (2725:21): [True: 0, False: 0]
2726
                // Remove the buffer and one additional block here to get actual height that is outside of the buffer
2727
0
                const int lock_height{prune_lock.second.height_first - PRUNE_LOCK_BUFFER - 1};
2728
0
                last_prune = std::max(1, std::min(last_prune, lock_height));
2729
0
                if (last_prune == lock_height) {
  Branch (2729:21): [True: 0, False: 0]
2730
0
                    limiting_lock = prune_lock.first;
2731
0
                }
2732
0
            }
2733
2734
0
            if (limiting_lock) {
  Branch (2734:17): [True: 0, False: 0]
2735
0
                LogDebug(BCLog::PRUNE, "%s limited pruning to height %d\n", limiting_lock.value(), last_prune);
2736
0
            }
2737
2738
0
            if (nManualPruneHeight > 0) {
  Branch (2738:17): [True: 0, False: 0]
2739
0
                LOG_TIME_MILLIS_WITH_CATEGORY("find files to prune (manual)", BCLog::BENCH);
2740
2741
0
                m_blockman.FindFilesToPruneManual(
2742
0
                    setFilesToPrune,
2743
0
                    std::min(last_prune, nManualPruneHeight),
2744
0
                    *this);
2745
0
            } else {
2746
0
                LOG_TIME_MILLIS_WITH_CATEGORY("find files to prune", BCLog::BENCH);
2747
2748
0
                m_blockman.FindFilesToPrune(setFilesToPrune, last_prune, *this, m_chainman);
2749
0
                m_blockman.m_check_for_pruning = false;
2750
0
            }
2751
0
            if (!setFilesToPrune.empty()) {
  Branch (2751:17): [True: 0, False: 0]
2752
0
                fFlushForPrune = true;
2753
0
                if (!m_blockman.m_have_pruned) {
  Branch (2753:21): [True: 0, False: 0]
2754
0
                    m_blockman.m_block_tree_db->WriteFlag("prunedblockfiles", true);
2755
0
                    m_blockman.m_have_pruned = true;
2756
0
                }
2757
0
            }
2758
0
        }
2759
3.37M
        const auto nNow{NodeClock::now()};
2760
        // The cache is large and we're within 10% and 10 MiB of the limit, but we have time now (not in the middle of a block processing).
2761
3.37M
        bool fCacheLarge = mode == FlushStateMode::PERIODIC && cache_state >= CoinsCacheSizeState::LARGE;
  Branch (2761:28): [True: 2.54M, False: 827k]
  Branch (2761:64): [True: 0, False: 2.54M]
2762
        // The cache is over the limit, we have to write now.
2763
3.37M
        bool fCacheCritical = mode == FlushStateMode::IF_NEEDED && cache_state >= CoinsCacheSizeState::CRITICAL;
  Branch (2763:31): [True: 346k, False: 3.02M]
  Branch (2763:68): [True: 0, False: 346k]
2764
        // It's been a while since we wrote the block index and chain state to disk. Do this frequently, so we don't need to redownload or reindex after a crash.
2765
3.37M
        bool fPeriodicWrite = mode == FlushStateMode::PERIODIC && nNow >= m_next_write;
  Branch (2765:31): [True: 2.54M, False: 827k]
  Branch (2765:67): [True: 125, False: 2.54M]
2766
3.37M
        const auto empty_cache{(mode == FlushStateMode::FORCE_FLUSH) || fCacheLarge || fCacheCritical};
  Branch (2766:32): [True: 58.9k, False: 3.31M]
  Branch (2766:73): [True: 0, False: 3.31M]
  Branch (2766:88): [True: 0, False: 3.31M]
2767
        // Combine all conditions that result in a write to disk.
2768
3.37M
        bool should_write = (mode == FlushStateMode::FORCE_SYNC) || empty_cache || fPeriodicWrite || fFlushForPrune;
  Branch (2768:29): [True: 70.9k, False: 3.29M]
  Branch (2768:69): [True: 58.9k, False: 3.24M]
  Branch (2768:84): [True: 125, False: 3.24M]
  Branch (2768:102): [True: 0, False: 3.24M]
2769
        // Write blocks, block index and best chain related state to disk.
2770
3.37M
        if (should_write) {
  Branch (2770:13): [True: 130k, False: 3.24M]
2771
130k
            LogDebug(BCLog::COINDB, "Writing chainstate to disk: flush mode=%s, prune=%d, large=%d, critical=%d, periodic=%d",
2772
130k
                     FlushStateModeNames[size_t(mode)], fFlushForPrune, fCacheLarge, fCacheCritical, fPeriodicWrite);
2773
2774
            // Ensure we can write block index
2775
130k
            if (!CheckDiskSpace(m_blockman.m_opts.blocks_dir)) {
  Branch (2775:17): [True: 0, False: 130k]
2776
0
                return FatalError(m_chainman.GetNotifications(), state, _("Disk space is too low!"));
2777
0
            }
2778
130k
            {
2779
130k
                LOG_TIME_MILLIS_WITH_CATEGORY("write block and undo data to disk", BCLog::BENCH);
2780
2781
                // First make sure all block and undo data is flushed to disk.
2782
                // TODO: Handle return error, or add detailed comment why it is
2783
                // safe to not return an error upon failure.
2784
130k
                if (!m_blockman.FlushChainstateBlockFile(m_chain.Height())) {
  Branch (2784:21): [True: 0, False: 130k]
2785
0
                    LogWarning("%s: Failed to flush block file.\n", __func__);
2786
0
                }
2787
130k
            }
2788
2789
            // Then update all block file information (which may refer to block and undo files).
2790
130k
            {
2791
130k
                LOG_TIME_MILLIS_WITH_CATEGORY("write block index to disk", BCLog::BENCH);
2792
2793
130k
                m_blockman.WriteBlockIndexDB();
2794
130k
            }
2795
            // Finally remove any pruned files
2796
130k
            if (fFlushForPrune) {
  Branch (2796:17): [True: 0, False: 130k]
2797
0
                LOG_TIME_MILLIS_WITH_CATEGORY("unlink pruned files", BCLog::BENCH);
2798
2799
0
                m_blockman.UnlinkPrunedFiles(setFilesToPrune);
2800
0
            }
2801
2802
130k
            if (!CoinsTip().GetBestBlock().IsNull()) {
  Branch (2802:17): [True: 130k, False: 0]
2803
                // Typical Coin structures on disk are around 48 bytes in size.
2804
                // Pushing a new one to the database can cause it to be written
2805
                // twice (once in the log, and once in the tables). This is already
2806
                // an overestimation, as most will delete an existing entry or
2807
                // overwrite one. Still, use a conservative safety factor of 2.
2808
130k
                if (!CheckDiskSpace(m_chainman.m_options.datadir, 48 * 2 * 2 * CoinsTip().GetDirtyCount())) {
  Branch (2808:21): [True: 0, False: 130k]
2809
0
                    return FatalError(m_chainman.GetNotifications(), state, _("Disk space is too low!"));
2810
0
                }
2811
                // Flush the chainstate (which may refer to block index entries).
2812
130k
                empty_cache ? CoinsTip().Flush() : CoinsTip().Sync();
  Branch (2812:17): [True: 58.9k, False: 71.0k]
2813
130k
                m_last_flushed_block = m_blockman.LookupBlockIndex(CoinsTip().GetBestBlock());
2814
130k
                full_flush_completed = true;
2815
130k
                TRACEPOINT(utxocache, flush,
2816
130k
                    int64_t{Ticks<std::chrono::microseconds>(NodeClock::now() - nNow)},
2817
130k
                    (uint32_t)mode,
2818
130k
                    (uint64_t)coins_count,
2819
130k
                    (uint64_t)coins_mem_usage,
2820
130k
                    (bool)fFlushForPrune);
2821
130k
            }
2822
130k
        }
2823
2824
3.37M
        if (should_write || m_next_write == NodeClock::time_point::max()) {
  Branch (2824:13): [True: 130k, False: 3.24M]
  Branch (2824:13): [True: 133k, False: 3.23M]
  Branch (2824:29): [True: 3.14k, False: 3.23M]
2825
133k
            constexpr auto range{DATABASE_WRITE_INTERVAL_MAX - DATABASE_WRITE_INTERVAL_MIN};
2826
133k
            m_next_write = FastRandomContext().rand_uniform_delay(NodeClock::now() + DATABASE_WRITE_INTERVAL_MIN, range);
2827
133k
        }
2828
3.37M
    }
2829
3.37M
    if (full_flush_completed) {
  Branch (2829:9): [True: 130k, False: 3.24M]
2830
130k
        if (m_chainman.m_options.signals) {
  Branch (2830:13): [True: 127k, False: 2.17k]
2831
127k
            m_chainman.m_options.signals->ChainStateFlushed(this->GetRole(), GetLocator(m_last_flushed_block));
2832
127k
        }
2833
2834
130k
        if (!m_chainman.m_interrupt && ShouldCompactChainstate(m_chainman.IsInitialBlockDownload())) {
  Branch (2834:13): [True: 130k, False: 0]
  Branch (2834:40): [True: 587, False: 129k]
2835
587
            try {
2836
587
                CoinsDB().CompactFullAsync();
2837
587
            } catch (const std::exception& e) {
2838
0
                LogWarning("Failed to start chainstate compaction (%s)", e.what());
2839
0
            }
2840
587
        }
2841
130k
    }
2842
3.37M
    } catch (const std::runtime_error& e) {
2843
0
        return FatalError(m_chainman.GetNotifications(), state, strprintf(_("System error while flushing: %s"), e.what()));
2844
0
    }
2845
3.37M
    return true;
2846
3.37M
}
2847
2848
void Chainstate::ForceFlushStateToDisk(bool wipe_cache)
2849
127k
{
2850
127k
    BlockValidationState state;
2851
127k
    if (!this->FlushStateToDisk(state, wipe_cache ? FlushStateMode::FORCE_FLUSH : FlushStateMode::FORCE_SYNC)) {
  Branch (2851:9): [True: 0, False: 127k]
  Branch (2851:40): [True: 56.7k, False: 70.9k]
2852
0
        LogWarning("Failed to force flush state (%s)", state.ToString());
2853
0
    }
2854
127k
}
2855
2856
void Chainstate::PruneAndFlush()
2857
0
{
2858
0
    BlockValidationState state;
2859
0
    m_blockman.m_check_for_pruning = true;
2860
0
    if (!this->FlushStateToDisk(state, FlushStateMode::NONE)) {
  Branch (2860:9): [True: 0, False: 0]
2861
0
        LogWarning("Failed to flush state (%s)", state.ToString());
2862
0
    }
2863
0
}
2864
2865
static void UpdateTipLog(
2866
    const ChainstateManager& chainman,
2867
    const CCoinsViewCache& coins_tip,
2868
    const CBlockIndex* tip,
2869
    const std::string& func_name,
2870
    const std::string& prefix,
2871
    const std::string& warning_messages,
2872
    const bool background_validation) EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
2873
344k
{
2874
2875
344k
    AssertLockHeld(::cs_main);
2876
2877
    // Disable rate limiting as this may log frequently during IBD.
2878
344k
    LogInfo(util::log::NO_RATE_LIMIT, "%s%s: new best=%s height=%d version=0x%08x log2_work=%f tx=%lu date='%s' progress=%f cache=%.1fMiB(%utxo)%s\n",
2879
344k
                   prefix, func_name,
2880
344k
                   tip->GetBlockHash().ToString(), tip->nHeight, tip->nVersion,
2881
344k
                   log(tip->nChainWork.getdouble()) / log(2.0), tip->m_chain_tx_count,
2882
344k
                   FormatISO8601DateTime(tip->GetBlockTime()),
2883
344k
                   background_validation ? chainman.GetBackgroundVerificationProgress(*tip) : chainman.GuessVerificationProgress(tip),
2884
344k
                   coins_tip.DynamicMemoryUsage() / double(1_MiB),
2885
344k
                   coins_tip.GetCacheSize(),
2886
344k
                   !warning_messages.empty() ? strprintf(" warning='%s'", warning_messages) : "");
2887
344k
}
2888
2889
void Chainstate::UpdateTip(const CBlockIndex* pindexNew)
2890
344k
{
2891
344k
    AssertLockHeld(::cs_main);
2892
344k
    const auto& coins_tip = this->CoinsTip();
2893
2894
    // The remainder of the function isn't relevant if we are not acting on
2895
    // the active chainstate, so return if need be.
2896
344k
    if (this != &m_chainman.ActiveChainstate()) {
  Branch (2896:9): [True: 0, False: 344k]
2897
        // Only log every so often so that we don't bury log messages at the tip.
2898
0
        constexpr int BACKGROUND_LOG_INTERVAL = 2000;
2899
0
        if (pindexNew->nHeight % BACKGROUND_LOG_INTERVAL == 0) {
  Branch (2899:13): [True: 0, False: 0]
2900
0
            UpdateTipLog(m_chainman, coins_tip, pindexNew, __func__, "[background validation] ", "", /*background_validation=*/true);
2901
0
        }
2902
0
        return;
2903
0
    }
2904
2905
    // New best block
2906
344k
    if (m_mempool) {
  Branch (2906:9): [True: 344k, False: 0]
2907
344k
        m_mempool->AddTransactionsUpdated(1);
2908
344k
    }
2909
2910
344k
    std::vector<bilingual_str> warning_messages;
2911
344k
    if (!m_chainman.IsInitialBlockDownload()) {
  Branch (2911:9): [True: 296k, False: 48.1k]
2912
296k
        auto bits = m_chainman.m_versionbitscache.CheckUnknownActivations(pindexNew, m_chainman.GetParams());
2913
296k
        for (auto [bit, active] : bits) {
  Branch (2913:33): [True: 0, False: 296k]
2914
0
            const bilingual_str warning = strprintf(_("Unknown new rules activated (versionbit %i)"), bit);
2915
0
            if (active) {
  Branch (2915:17): [True: 0, False: 0]
2916
0
                m_chainman.GetNotifications().warningSet(kernel::Warning::UNKNOWN_NEW_RULES_ACTIVATED, warning);
2917
0
            } else {
2918
0
                warning_messages.push_back(warning);
2919
0
            }
2920
0
        }
2921
296k
    }
2922
344k
    UpdateTipLog(m_chainman, coins_tip, pindexNew, __func__, "",
2923
344k
                 util::Join(warning_messages, Untranslated(", ")).original, /*background_validation=*/false);
2924
344k
}
2925
2926
/** Disconnect m_chain's tip.
2927
  * After calling, the mempool will be in an inconsistent state, with
2928
  * transactions from disconnected blocks being added to disconnectpool.  You
2929
  * should make the mempool consistent again by calling MaybeUpdateMempoolForReorg.
2930
  * with cs_main held.
2931
  *
2932
  * If disconnectpool is nullptr, then no disconnected transactions are added to
2933
  * disconnectpool (note that the caller is responsible for mempool consistency
2934
  * in any case).
2935
  */
2936
bool Chainstate::DisconnectTip(BlockValidationState& state, DisconnectedBlockTransactions* disconnectpool)
2937
0
{
2938
0
    AssertLockHeld(cs_main);
2939
0
    if (m_mempool) AssertLockHeld(m_mempool->cs);
  Branch (2939:9): [True: 0, False: 0]
2940
2941
0
    CBlockIndex *pindexDelete = m_chain.Tip();
2942
0
    assert(pindexDelete);
  Branch (2942:5): [True: 0, False: 0]
2943
0
    assert(pindexDelete->pprev);
  Branch (2943:5): [True: 0, False: 0]
2944
    // Read block from disk.
2945
0
    std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>();
2946
0
    CBlock& block = *pblock;
2947
0
    if (!m_blockman.ReadBlock(block, *pindexDelete)) {
  Branch (2947:9): [True: 0, False: 0]
2948
0
        LogError("DisconnectTip(): Failed to read block\n");
2949
0
        return false;
2950
0
    }
2951
    // Apply the block atomically to the chain state.
2952
0
    const auto time_start{SteadyClock::now()};
2953
0
    {
2954
0
        CCoinsViewCache view(&CoinsTip());
2955
0
        assert(view.GetBestBlock() == pindexDelete->GetBlockHash());
  Branch (2955:9): [True: 0, False: 0]
2956
0
        if (DisconnectBlock(block, pindexDelete, view) != DISCONNECT_OK) {
  Branch (2956:13): [True: 0, False: 0]
2957
0
            LogError("DisconnectTip(): DisconnectBlock %s failed\n", pindexDelete->GetBlockHash().ToString());
2958
0
            return false;
2959
0
        }
2960
0
        view.Flush(/*reallocate_cache=*/false); // local CCoinsViewCache goes out of scope
2961
0
    }
2962
0
    LogDebug(BCLog::BENCH, "- Disconnect block: %.2fms\n",
2963
0
             Ticks<MillisecondsDouble>(SteadyClock::now() - time_start));
2964
2965
0
    {
2966
        // Prune locks that began at or after the tip should be moved backward so they get a chance to reorg
2967
0
        const int max_height_first{pindexDelete->nHeight - 1};
2968
0
        for (auto& prune_lock : m_blockman.m_prune_locks) {
  Branch (2968:31): [True: 0, False: 0]
2969
0
            if (prune_lock.second.height_first <= max_height_first) continue;
  Branch (2969:17): [True: 0, False: 0]
2970
2971
0
            prune_lock.second.height_first = max_height_first;
2972
0
            LogDebug(BCLog::PRUNE, "%s prune lock moved back to %d\n", prune_lock.first, max_height_first);
2973
0
        }
2974
0
    }
2975
2976
    // Write the chain state to disk, if necessary.
2977
0
    if (!FlushStateToDisk(state, FlushStateMode::IF_NEEDED)) {
  Branch (2977:9): [True: 0, False: 0]
2978
0
        return false;
2979
0
    }
2980
2981
0
    if (disconnectpool && m_mempool) {
  Branch (2981:9): [True: 0, False: 0]
  Branch (2981:27): [True: 0, False: 0]
2982
        // Save transactions to re-add to mempool at end of reorg. If any entries are evicted for
2983
        // exceeding memory limits, remove them and their descendants from the mempool.
2984
0
        for (auto&& evicted_tx : disconnectpool->AddTransactionsFromBlock(block.vtx)) {
  Branch (2984:32): [True: 0, False: 0]
2985
0
            m_mempool->removeRecursive(*evicted_tx, MemPoolRemovalReason::REORG);
2986
0
        }
2987
0
    }
2988
2989
0
    m_chain.SetTip(*pindexDelete->pprev);
2990
0
    m_chainman.UpdateIBDStatus();
2991
2992
0
    UpdateTip(pindexDelete->pprev);
2993
    // Let wallets know transactions went from 1-confirmed to
2994
    // 0-confirmed or conflicted:
2995
0
    if (m_chainman.m_options.signals) {
  Branch (2995:9): [True: 0, False: 0]
2996
0
        m_chainman.m_options.signals->BlockDisconnected(std::move(pblock), pindexDelete);
2997
0
    }
2998
0
    return true;
2999
0
}
3000
3001
struct ConnectedBlock {
3002
    const CBlockIndex* pindex;
3003
    std::shared_ptr<const CBlock> pblock;
3004
};
3005
3006
/**
3007
 * Connect a new block to m_chain. block_to_connect is either nullptr or a pointer to a CBlock
3008
 * corresponding to pindexNew, to bypass loading it again from disk.
3009
 *
3010
 * The block is added to connected_blocks if connection succeeds.
3011
 */
3012
bool Chainstate::ConnectTip(
3013
    BlockValidationState& state,
3014
    CBlockIndex* pindexNew,
3015
    std::shared_ptr<const CBlock> block_to_connect,
3016
    std::vector<ConnectedBlock>& connected_blocks,
3017
    DisconnectedBlockTransactions& disconnectpool)
3018
354k
{
3019
354k
    AssertLockHeld(cs_main);
3020
354k
    if (m_mempool) AssertLockHeld(m_mempool->cs);
  Branch (3020:9): [True: 354k, False: 0]
3021
3022
354k
    assert(pindexNew->pprev == m_chain.Tip());
  Branch (3022:5): [True: 354k, False: 0]
3023
    // Read block from disk.
3024
354k
    const auto time_1{SteadyClock::now()};
3025
354k
    if (!block_to_connect) {
  Branch (3025:9): [True: 3.19k, False: 350k]
3026
3.19k
        std::shared_ptr<CBlock> pblockNew = std::make_shared<CBlock>();
3027
3.19k
        if (!m_blockman.ReadBlock(*pblockNew, *pindexNew)) {
  Branch (3027:13): [True: 0, False: 3.19k]
3028
0
            return FatalError(m_chainman.GetNotifications(), state, _("Failed to read block."));
3029
0
        }
3030
3.19k
        block_to_connect = std::move(pblockNew);
3031
350k
    } else {
3032
350k
        LogDebug(BCLog::BENCH, "  - Using cached block\n");
3033
350k
    }
3034
    // Apply the block atomically to the chain state.
3035
354k
    const auto time_2{SteadyClock::now()};
3036
354k
    SteadyClock::time_point time_3;
3037
    // When adding aggregate statistics in the future, keep in mind that
3038
    // num_blocks_total may be zero until the ConnectBlock() call below.
3039
354k
    LogDebug(BCLog::BENCH, "  - Load block from disk: %.2fms\n",
3040
354k
             Ticks<MillisecondsDouble>(time_2 - time_1));
3041
354k
    {
3042
354k
        CoinsViewOverlay& view{*m_coins_views->m_connect_block_view};
3043
354k
        const auto reset_guard{view.StartFetching(*block_to_connect)};
3044
354k
        bool rv = ConnectBlock(*block_to_connect, state, pindexNew, view);
3045
354k
        if (m_chainman.m_options.signals) {
  Branch (3045:13): [True: 353k, False: 536]
3046
353k
            m_chainman.m_options.signals->BlockChecked(block_to_connect, state);
3047
353k
        }
3048
354k
        if (!rv) {
  Branch (3048:13): [True: 9.44k, False: 344k]
3049
9.44k
            if (state.IsInvalid())
  Branch (3049:17): [True: 9.44k, False: 0]
3050
9.44k
                InvalidBlockFound(pindexNew, state);
3051
9.44k
            LogError("%s: ConnectBlock %s failed, %s\n", __func__, pindexNew->GetBlockHash().ToString(), state.ToString());
3052
9.44k
            return false;
3053
9.44k
        }
3054
344k
        time_3 = SteadyClock::now();
3055
344k
        m_chainman.time_connect_total += time_3 - time_2;
3056
344k
        assert(m_chainman.num_blocks_total > 0);
  Branch (3056:9): [True: 344k, False: 0]
3057
344k
        LogDebug(BCLog::BENCH, "  - Connect total: %.2fms [%.2fs (%.2fms/blk)]\n",
3058
344k
                 Ticks<MillisecondsDouble>(time_3 - time_2),
3059
344k
                 Ticks<SecondsDouble>(m_chainman.time_connect_total),
3060
344k
                 Ticks<MillisecondsDouble>(m_chainman.time_connect_total) / m_chainman.num_blocks_total);
3061
344k
        view.Flush(/*reallocate_cache=*/false); // No need to reallocate since it only has capacity for 1 block
3062
344k
    }
3063
0
    const auto time_4{SteadyClock::now()};
3064
344k
    m_chainman.time_flush += time_4 - time_3;
3065
344k
    LogDebug(BCLog::BENCH, "  - Flush: %.2fms [%.2fs (%.2fms/blk)]\n",
3066
344k
             Ticks<MillisecondsDouble>(time_4 - time_3),
3067
344k
             Ticks<SecondsDouble>(m_chainman.time_flush),
3068
344k
             Ticks<MillisecondsDouble>(m_chainman.time_flush) / m_chainman.num_blocks_total);
3069
    // Write the chain state to disk, if necessary.
3070
344k
    if (!FlushStateToDisk(state, FlushStateMode::IF_NEEDED)) {
  Branch (3070:9): [True: 0, False: 344k]
3071
0
        return false;
3072
0
    }
3073
344k
    const auto time_5{SteadyClock::now()};
3074
344k
    m_chainman.time_chainstate += time_5 - time_4;
3075
344k
    LogDebug(BCLog::BENCH, "  - Writing chainstate: %.2fms [%.2fs (%.2fms/blk)]\n",
3076
344k
             Ticks<MillisecondsDouble>(time_5 - time_4),
3077
344k
             Ticks<SecondsDouble>(m_chainman.time_chainstate),
3078
344k
             Ticks<MillisecondsDouble>(m_chainman.time_chainstate) / m_chainman.num_blocks_total);
3079
    // Remove conflicting transactions from the mempool.
3080
344k
    std::vector<RemovedMempoolTransactionInfo> txs_removed_for_block;
3081
344k
    if (m_mempool) {
  Branch (3081:9): [True: 344k, False: 0]
3082
344k
        txs_removed_for_block = m_mempool->removeForBlock(block_to_connect->vtx);
3083
344k
        disconnectpool.removeForBlock(block_to_connect->vtx);
3084
344k
    }
3085
    // Update m_chain & related variables.
3086
344k
    m_chain.SetTip(*pindexNew);
3087
344k
    m_chainman.UpdateIBDStatus();
3088
    // Not fired while IBD is active. removeForBlock() above still runs.
3089
344k
    if (m_mempool && m_chainman.m_options.signals && !m_chainman.IsInitialBlockDownload()) {
  Branch (3089:9): [True: 344k, False: 0]
  Branch (3089:22): [True: 344k, False: 536]
  Branch (3089:54): [True: 296k, False: 47.7k]
3090
296k
        m_chainman.m_options.signals->MempoolTransactionsRemovedForBlock(block_to_connect, std::move(txs_removed_for_block), pindexNew->nHeight);
3091
296k
    }
3092
344k
    UpdateTip(pindexNew);
3093
3094
344k
    const auto time_6{SteadyClock::now()};
3095
344k
    m_chainman.time_post_connect += time_6 - time_5;
3096
344k
    m_chainman.time_total += time_6 - time_1;
3097
344k
    LogDebug(BCLog::BENCH, "  - Connect postprocess: %.2fms [%.2fs (%.2fms/blk)]\n",
3098
344k
             Ticks<MillisecondsDouble>(time_6 - time_5),
3099
344k
             Ticks<SecondsDouble>(m_chainman.time_post_connect),
3100
344k
             Ticks<MillisecondsDouble>(m_chainman.time_post_connect) / m_chainman.num_blocks_total);
3101
344k
    LogDebug(BCLog::BENCH, "- Connect block: %.2fms [%.2fs (%.2fms/blk)]\n",
3102
344k
             Ticks<MillisecondsDouble>(time_6 - time_1),
3103
344k
             Ticks<SecondsDouble>(m_chainman.time_total),
3104
344k
             Ticks<MillisecondsDouble>(m_chainman.time_total) / m_chainman.num_blocks_total);
3105
3106
    // See if this chainstate has reached a target block and can be used to
3107
    // validate an assumeutxo snapshot. If it can, hashing the UTXO database
3108
    // will be slow, and cs_main could remain locked here for several minutes.
3109
    // If the snapshot is validated, the UTXO hash will be saved to
3110
    // this->m_target_utxohash, causing HistoricalChainstate() to return null
3111
    // and this chainstate to no longer be used. ActivateBestChain() will also
3112
    // stop connecting blocks to this chainstate because this->ReachedTarget()
3113
    // will be true and this->setBlockIndexCandidates will not have additional
3114
    // blocks.
3115
344k
    Chainstate& current_cs{m_chainman.CurrentChainstate()};
3116
344k
    m_chainman.MaybeValidateSnapshot(*this, current_cs);
3117
3118
344k
    connected_blocks.emplace_back(pindexNew, std::move(block_to_connect));
3119
344k
    return true;
3120
344k
}
3121
3122
/**
3123
 * Return the tip of the chain with the most work in it, that isn't
3124
 * known to be invalid (it's however far from certain to be valid).
3125
 */
3126
CBlockIndex* Chainstate::FindMostWorkChain()
3127
371k
{
3128
371k
    AssertLockHeld(::cs_main);
3129
372k
    do {
3130
372k
        CBlockIndex *pindexNew = nullptr;
3131
3132
        // Find the best candidate header.
3133
372k
        {
3134
372k
            std::set<CBlockIndex*, CBlockIndexWorkComparator>::reverse_iterator it = setBlockIndexCandidates.rbegin();
3135
372k
            if (it == setBlockIndexCandidates.rend())
  Branch (3135:17): [True: 0, False: 372k]
3136
0
                return nullptr;
3137
372k
            pindexNew = *it;
3138
372k
        }
3139
3140
        // Check whether all blocks on the path between the currently active chain and the candidate are valid.
3141
        // Just going until the active chain is an optimization, as we know all blocks in it are valid already.
3142
0
        bool fInvalidAncestor = false;
3143
730k
        for (CBlockIndex *pindexTest = pindexNew; pindexTest && !m_chain.Contains(*pindexTest); pindexTest = pindexTest->pprev) {
  Branch (3143:51): [True: 727k, False: 3.14k]
  Branch (3143:65): [True: 358k, False: 368k]
3144
358k
            assert(pindexTest->HaveNumChainTxs() || pindexTest->nHeight == 0);
  Branch (3144:13): [True: 358k, False: 0]
  Branch (3144:13): [True: 0, False: 0]
  Branch (3144:13): [True: 358k, False: 0]
3145
3146
            // Pruned nodes may have entries in setBlockIndexCandidates for
3147
            // which block files have been deleted.  Remove those as candidates
3148
            // for the most work chain if we come across them; we can't switch
3149
            // to a chain unless we have all the non-active-chain parent blocks.
3150
358k
            bool fFailedChain = pindexTest->nStatus & BLOCK_FAILED_VALID;
3151
358k
            bool fMissingData = !(pindexTest->nStatus & BLOCK_HAVE_DATA);
3152
358k
            if (fFailedChain || fMissingData) {
  Branch (3152:17): [True: 879, False: 357k]
  Branch (3152:33): [True: 0, False: 357k]
3153
                // Candidate chain is not usable (either invalid or missing data)
3154
879
                if (fFailedChain && (m_chainman.m_best_invalid == nullptr || pindexNew->nChainWork > m_chainman.m_best_invalid->nChainWork)) {
  Branch (3154:21): [True: 879, False: 0]
  Branch (3154:38): [True: 0, False: 879]
  Branch (3154:78): [True: 0, False: 879]
3155
0
                    m_chainman.m_best_invalid = pindexNew;
3156
0
                }
3157
                // Remove the entire chain from the set.
3158
879
                for (CBlockIndex *pindexFailed = pindexNew; pindexFailed != pindexTest; pindexFailed = pindexFailed->pprev) {
  Branch (3158:61): [True: 0, False: 879]
3159
                    // If we're missing data and not a descendant of an invalid block,
3160
                    // then add back to m_blocks_unlinked, so that if the block arrives in the future
3161
                    // we can try adding to setBlockIndexCandidates again.
3162
0
                    if (fMissingData && !fFailedChain) {
  Branch (3162:25): [True: 0, False: 0]
  Branch (3162:41): [True: 0, False: 0]
3163
                        // Avoid duplicate entries in m_blocks_unlinked. If the same entry is
3164
                        // processed twice in ReceivedBlockTransactions(), it may be re-added to
3165
                        // setBlockIndexCandidates with a modified nSequenceId, breaking ordering
3166
                        // guarantees and leading to undefined behavior.
3167
0
                        m_blockman.AddUnlinkedBlock(pindexFailed);
3168
0
                    }
3169
0
                    setBlockIndexCandidates.erase(pindexFailed);
3170
0
                }
3171
879
                setBlockIndexCandidates.erase(pindexTest);
3172
879
                fInvalidAncestor = true;
3173
879
                break;
3174
879
            }
3175
358k
        }
3176
372k
        if (!fInvalidAncestor)
  Branch (3176:13): [True: 371k, False: 879]
3177
371k
            return pindexNew;
3178
372k
    } while(true);
  Branch (3178:13): [Folded - Ignored]
3179
371k
}
3180
3181
/** Delete all entries in setBlockIndexCandidates that are worse than the current tip. */
3182
346k
void Chainstate::PruneBlockIndexCandidates() {
3183
    // Note that we can't delete the current block itself, as we may need to return to it later in case a
3184
    // reorganization to a better block fails.
3185
346k
    std::set<CBlockIndex*, CBlockIndexWorkComparator>::iterator it = setBlockIndexCandidates.begin();
3186
688k
    while (it != setBlockIndexCandidates.end() && setBlockIndexCandidates.value_comp()(*it, m_chain.Tip())) {
  Branch (3186:12): [True: 688k, False: 0]
  Branch (3186:12): [True: 342k, False: 346k]
  Branch (3186:51): [True: 342k, False: 346k]
3187
342k
        setBlockIndexCandidates.erase(it++);
3188
342k
    }
3189
    // Either the current tip or a successor of it we're working towards is left in setBlockIndexCandidates.
3190
346k
    assert(!setBlockIndexCandidates.empty());
  Branch (3190:5): [True: 346k, False: 0]
3191
346k
}
3192
3193
/**
3194
 * Try to make some progress towards making index_most_work the active block.
3195
 * pblock is either nullptr or a pointer to a CBlock corresponding to index_most_work.
3196
 *
3197
 * @returns true unless a system error occurred
3198
 */
3199
bool Chainstate::ActivateBestChainStep(BlockValidationState& state, CBlockIndex& index_most_work, const std::shared_ptr<const CBlock>& pblock, bool& fInvalidFound, std::vector<ConnectedBlock>& connected_blocks)
3200
354k
{
3201
354k
    AssertLockHeld(cs_main);
3202
354k
    if (m_mempool) AssertLockHeld(m_mempool->cs);
  Branch (3202:9): [True: 354k, False: 0]
3203
3204
354k
    const CBlockIndex* pindexOldTip = m_chain.Tip();
3205
354k
    const CBlockIndex* pindexFork = m_chain.FindFork(index_most_work);
3206
3207
    // Disconnect active blocks which are no longer in the best chain.
3208
354k
    bool fBlocksDisconnected = false;
3209
354k
    DisconnectedBlockTransactions disconnectpool{MAX_DISCONNECTED_TX_POOL_BYTES};
3210
354k
    while (m_chain.Tip() && m_chain.Tip() != pindexFork) {
  Branch (3210:12): [True: 350k, False: 3.14k]
  Branch (3210:29): [True: 0, False: 350k]
3211
0
        if (!DisconnectTip(state, &disconnectpool)) {
  Branch (3211:13): [True: 0, False: 0]
3212
            // This is likely a fatal error, but keep the mempool consistent,
3213
            // just in case. Only remove from the mempool in this case.
3214
0
            MaybeUpdateMempoolForReorg(disconnectpool, false);
3215
3216
            // If we're unable to disconnect a block during normal operation,
3217
            // then that is a failure of our local system -- we should abort
3218
            // rather than stay on a less work chain.
3219
0
            FatalError(m_chainman.GetNotifications(), state, _("Failed to disconnect block."));
3220
0
            return false;
3221
0
        }
3222
0
        fBlocksDisconnected = true;
3223
0
    }
3224
3225
    // Build list of new blocks to connect (in descending height order).
3226
354k
    std::vector<CBlockIndex*> vpindexToConnect;
3227
354k
    bool fContinue = true;
3228
354k
    int nHeight = pindexFork ? pindexFork->nHeight : -1;
  Branch (3228:19): [True: 350k, False: 3.14k]
3229
708k
    while (fContinue && nHeight != index_most_work.nHeight) {
  Branch (3229:12): [True: 354k, False: 354k]
  Branch (3229:25): [True: 354k, False: 0]
3230
        // Don't iterate the entire list of potential improvements toward the best tip, as we likely only need
3231
        // a few blocks along the way.
3232
354k
        int nTargetHeight = std::min(nHeight + 32, index_most_work.nHeight);
3233
354k
        vpindexToConnect.clear();
3234
354k
        vpindexToConnect.reserve(nTargetHeight - nHeight);
3235
354k
        CBlockIndex* pindexIter = index_most_work.GetAncestor(nTargetHeight);
3236
708k
        while (pindexIter && pindexIter->nHeight != nHeight) {
  Branch (3236:16): [True: 704k, False: 3.14k]
  Branch (3236:30): [True: 354k, False: 350k]
3237
354k
            vpindexToConnect.push_back(pindexIter);
3238
354k
            pindexIter = pindexIter->pprev;
3239
354k
        }
3240
354k
        nHeight = nTargetHeight;
3241
3242
        // Connect new blocks.
3243
354k
        for (CBlockIndex* pindexConnect : vpindexToConnect | std::views::reverse) {
  Branch (3243:41): [True: 354k, False: 0]
3244
354k
            if (!ConnectTip(state, pindexConnect, pindexConnect == &index_most_work ? pblock : std::shared_ptr<const CBlock>(), connected_blocks, disconnectpool)) {
  Branch (3244:17): [True: 9.44k, False: 344k]
  Branch (3244:51): [True: 353k, False: 28]
3245
9.44k
                if (state.IsInvalid()) {
  Branch (3245:21): [True: 9.44k, False: 0]
3246
                    // The block violates a consensus rule.
3247
9.44k
                    if (state.GetResult() != BlockValidationResult::BLOCK_MUTATED) {
  Branch (3247:25): [True: 9.44k, False: 0]
3248
9.44k
                        InvalidChainFound(vpindexToConnect.front());
3249
9.44k
                    }
3250
9.44k
                    state = BlockValidationState();
3251
9.44k
                    fInvalidFound = true;
3252
9.44k
                    fContinue = false;
3253
9.44k
                    break;
3254
9.44k
                } else {
3255
                    // A system error occurred (disk space, database error, ...).
3256
                    // Make the mempool consistent with the current tip, just in case
3257
                    // any observers try to use it before shutdown.
3258
0
                    MaybeUpdateMempoolForReorg(disconnectpool, false);
3259
0
                    return false;
3260
0
                }
3261
344k
            } else {
3262
344k
                PruneBlockIndexCandidates();
3263
344k
                if (!pindexOldTip || m_chain.Tip()->nChainWork > pindexOldTip->nChainWork) {
  Branch (3263:21): [True: 3.14k, False: 341k]
  Branch (3263:38): [True: 341k, False: 0]
3264
                    // We're in a better position than we were. Return temporarily to release the lock.
3265
344k
                    fContinue = false;
3266
344k
                    break;
3267
344k
                }
3268
344k
            }
3269
354k
        }
3270
354k
    }
3271
3272
354k
    if (fBlocksDisconnected) {
  Branch (3272:9): [True: 0, False: 354k]
3273
        // If any blocks were disconnected, disconnectpool may be non empty.  Add
3274
        // any disconnected transactions back to the mempool.
3275
0
        MaybeUpdateMempoolForReorg(disconnectpool, true);
3276
0
    }
3277
354k
    if (m_mempool) m_mempool->check(this->CoinsTip(), this->m_chain.Height() + 1);
  Branch (3277:9): [True: 354k, False: 0]
3278
3279
354k
    CheckForkWarningConditions();
3280
3281
354k
    return true;
3282
354k
}
3283
3284
static SynchronizationState GetSynchronizationState(bool init, bool blockfiles_indexed)
3285
803k
{
3286
803k
    if (!init) return SynchronizationState::POST_INIT;
  Branch (3286:9): [True: 610k, False: 192k]
3287
192k
    if (!blockfiles_indexed) return SynchronizationState::INIT_REINDEX;
  Branch (3287:9): [True: 0, False: 192k]
3288
192k
    return SynchronizationState::INIT_DOWNLOAD;
3289
192k
}
3290
3291
void ChainstateManager::UpdateIBDStatus()
3292
344k
{
3293
344k
    AssertLockHeld(cs_main);
3294
344k
    if (!m_cached_is_ibd.load(std::memory_order_relaxed)) return;
  Branch (3294:9): [True: 293k, False: 50.7k]
3295
50.7k
    if (m_blockman.LoadingBlocks()) return;
  Branch (3295:9): [True: 0, False: 50.7k]
3296
50.7k
    if (!CurrentChainstate().m_chain.IsTipRecent(MinimumChainWork(), m_options.max_tip_age)) return;
  Branch (3296:9): [True: 48.1k, False: 2.55k]
3297
2.55k
    LogInfo("Leaving InitialBlockDownload (latching to false)");
3298
2.55k
    m_cached_is_ibd.store(false, std::memory_order_relaxed);
3299
2.55k
}
3300
3301
bool ChainstateManager::NotifyHeaderTip()
3302
587k
{
3303
587k
    bool fNotify = false;
3304
587k
    bool fInitialBlockDownload = false;
3305
587k
    CBlockIndex* pindexHeader = nullptr;
3306
587k
    {
3307
587k
        LOCK(GetMutex());
3308
587k
        pindexHeader = m_best_header;
3309
3310
587k
        if (pindexHeader != m_last_notified_header) {
  Branch (3310:13): [True: 458k, False: 128k]
3311
458k
            fNotify = true;
3312
458k
            fInitialBlockDownload = IsInitialBlockDownload();
3313
458k
            m_last_notified_header = pindexHeader;
3314
458k
        }
3315
587k
    }
3316
    // Send block tip changed notifications without the lock held
3317
587k
    if (fNotify) {
  Branch (3317:9): [True: 458k, False: 128k]
3318
458k
        GetNotifications().headerTip(GetSynchronizationState(fInitialBlockDownload, m_blockman.m_blockfiles_indexed), pindexHeader->nHeight, pindexHeader->nTime, false);
3319
458k
    }
3320
587k
    return fNotify;
3321
587k
}
3322
3323
366k
static void LimitValidationInterfaceQueue(ValidationSignals& signals) LOCKS_EXCLUDED(cs_main) {
3324
366k
    AssertLockNotHeld(cs_main);
3325
3326
366k
    if (signals.CallbacksPending() > 10) {
  Branch (3326:9): [True: 0, False: 366k]
3327
0
        signals.SyncWithValidationInterfaceQueue();
3328
0
    }
3329
366k
}
3330
3331
bool Chainstate::ActivateBestChain(BlockValidationState& state, std::shared_ptr<const CBlock> pblock)
3332
357k
{
3333
357k
    AssertLockNotHeld(m_chainstate_mutex);
3334
3335
    // Note that while we're often called here from ProcessNewBlock, this is
3336
    // far from a guarantee. Things in the P2P/RPC will often end up calling
3337
    // us in the middle of ProcessNewBlock - do not assume pblock is set
3338
    // sanely for performance or correctness!
3339
357k
    AssertLockNotHeld(::cs_main);
3340
3341
    // ABC maintains a fair degree of expensive-to-calculate internal state
3342
    // because this function periodically releases cs_main so that it does not lock up other threads for too long
3343
    // during large connects - and to allow for e.g. the callback queue to drain
3344
    // we use m_chainstate_mutex to enforce mutual exclusion so that only one caller may execute this function at a time
3345
357k
    LOCK(m_chainstate_mutex);
3346
3347
    // Belt-and-suspenders check that we aren't attempting to advance the
3348
    // chainstate past the target block.
3349
357k
    if (WITH_LOCK(::cs_main, return m_target_utxohash)) {
3350
0
        LogError("%s", STR_INTERNAL_BUG("m_target_utxohash is set - this chainstate should not be in operation."));
3351
0
        return Assume(false);
3352
0
    }
3353
3354
357k
    CBlockIndex *pindexMostWork = nullptr;
3355
357k
    CBlockIndex *pindexNewTip = nullptr;
3356
357k
    bool exited_ibd{false};
3357
366k
    do {
3358
        // Block until the validation queue drains. This should largely
3359
        // never happen in normal operation, however may happen during
3360
        // reindex, causing memory blowup if we run too far ahead.
3361
        // Note that if a validationinterface callback ends up calling
3362
        // ActivateBestChain this may lead to a deadlock! We should
3363
        // probably have a DEBUG_LOCKORDER test for this in the future.
3364
366k
        if (m_chainman.m_options.signals) LimitValidationInterfaceQueue(*m_chainman.m_options.signals);
  Branch (3364:13): [True: 366k, False: 536]
3365
3366
366k
        {
3367
366k
            LOCK(cs_main);
3368
366k
            {
3369
            // Lock transaction pool for at least as long as it takes for connected_blocks to be consumed
3370
366k
            LOCK(MempoolMutex());
3371
366k
            const bool was_in_ibd = m_chainman.IsInitialBlockDownload();
3372
366k
            CBlockIndex* starting_tip = m_chain.Tip();
3373
366k
            bool blocks_connected = false;
3374
366k
            do {
3375
                // We absolutely may not unlock cs_main until we've made forward progress
3376
                // (with the exception of shutdown due to hardware issues, low disk space, etc).
3377
366k
                std::vector<ConnectedBlock> connected_blocks; // Destructed before cs_main is unlocked
3378
3379
366k
                if (pindexMostWork == nullptr) {
  Branch (3379:21): [True: 366k, False: 28]
3380
366k
                    pindexMostWork = FindMostWorkChain();
3381
366k
                }
3382
3383
                // Whether we have anything to do at all.
3384
366k
                if (pindexMostWork == nullptr || pindexMostWork == m_chain.Tip()) {
  Branch (3384:21): [True: 0, False: 366k]
  Branch (3384:50): [True: 12.7k, False: 354k]
3385
12.7k
                    break;
3386
12.7k
                }
3387
3388
354k
                bool fInvalidFound = false;
3389
354k
                std::shared_ptr<const CBlock> nullBlockPtr;
3390
                // BlockConnected signals must be sent for the original role;
3391
                // in case snapshot validation is completed during ActivateBestChainStep, the
3392
                // result of GetRole() changes from BACKGROUND to NORMAL.
3393
354k
               const ChainstateRole chainstate_role{this->GetRole()};
3394
354k
                if (!ActivateBestChainStep(state, *pindexMostWork, pblock && pblock->GetHash() == pindexMostWork->GetBlockHash() ? pblock : nullBlockPtr, fInvalidFound, connected_blocks)) {
  Branch (3394:21): [True: 0, False: 354k]
  Branch (3394:68): [True: 350k, False: 3.14k]
  Branch (3394:78): [True: 350k, False: 57]
3395
                    // A system error occurred
3396
0
                    return false;
3397
0
                }
3398
354k
                blocks_connected = true;
3399
3400
354k
                if (fInvalidFound) {
  Branch (3400:21): [True: 9.44k, False: 344k]
3401
                    // Wipe cache, we may need another branch now.
3402
9.44k
                    pindexMostWork = nullptr;
3403
9.44k
                }
3404
354k
                pindexNewTip = m_chain.Tip();
3405
3406
354k
                for (auto& [index, block] : std::move(connected_blocks)) {
  Branch (3406:43): [True: 344k, False: 354k]
3407
344k
                    if (m_chainman.m_options.signals) {
  Branch (3407:25): [True: 344k, False: 536]
3408
344k
                        m_chainman.m_options.signals->BlockConnected(chainstate_role, std::move(Assert(block)), Assert(index));
3409
344k
                    }
3410
344k
                }
3411
3412
                // Break this do-while to ensure we don't advance past the target block.
3413
354k
                if (ReachedTarget()) {
  Branch (3413:21): [True: 0, False: 354k]
3414
0
                    break;
3415
0
                }
3416
354k
            } while (!m_chain.Tip() || (starting_tip && CBlockIndexWorkComparator()(m_chain.Tip(), starting_tip)));
  Branch (3416:22): [True: 0, False: 354k]
  Branch (3416:22): [True: 0, False: 354k]
  Branch (3416:41): [True: 350k, False: 3.14k]
  Branch (3416:57): [True: 0, False: 350k]
3417
366k
            if (!blocks_connected) return true;
  Branch (3417:17): [True: 12.7k, False: 354k]
3418
3419
354k
            const CBlockIndex* pindexFork = starting_tip ? m_chain.FindFork(*starting_tip) : nullptr;
  Branch (3419:45): [True: 350k, False: 3.14k]
3420
354k
            bool still_in_ibd = m_chainman.IsInitialBlockDownload();
3421
3422
354k
            if (was_in_ibd && !still_in_ibd) {
  Branch (3422:17): [True: 57.3k, False: 296k]
  Branch (3422:31): [True: 2.55k, False: 54.7k]
3423
                // Active chainstate has exited IBD.
3424
2.55k
                exited_ibd = true;
3425
2.55k
            }
3426
3427
            // Notify external listeners about the new tip.
3428
            // Enqueue while holding cs_main to ensure that UpdatedBlockTip is called in the order in which blocks are connected
3429
354k
            if (this == &m_chainman.ActiveChainstate() && pindexFork != pindexNewTip) {
  Branch (3429:17): [True: 354k, False: 0]
  Branch (3429:59): [True: 344k, False: 9.44k]
3430
                // Notify ValidationInterface subscribers
3431
344k
                if (m_chainman.m_options.signals) {
  Branch (3431:21): [True: 344k, False: 536]
3432
344k
                    m_chainman.m_options.signals->UpdatedBlockTip(pindexNewTip, pindexFork, still_in_ibd);
3433
344k
                }
3434
3435
344k
                if (kernel::IsInterrupted(m_chainman.GetNotifications().blockTip(
  Branch (3435:21): [True: 0, False: 344k]
3436
344k
                        /*state=*/GetSynchronizationState(still_in_ibd, m_chainman.m_blockman.m_blockfiles_indexed),
3437
344k
                        /*index=*/*pindexNewTip,
3438
344k
                        /*verification_progress=*/m_chainman.GuessVerificationProgress(pindexNewTip))))
3439
0
                {
3440
                    // Just breaking and returning success for now. This could
3441
                    // be changed to bubble up the kernel::Interrupted value to
3442
                    // the caller so the caller could distinguish between
3443
                    // completed and interrupted operations.
3444
0
                    break;
3445
0
                }
3446
344k
            }
3447
354k
            } // release MempoolMutex
3448
            // Notify external listeners about the new tip, even if pindexFork == pindexNewTip.
3449
354k
            if (m_chainman.m_options.signals && this == &m_chainman.ActiveChainstate()) {
  Branch (3449:17): [True: 353k, False: 536]
  Branch (3449:49): [True: 353k, False: 0]
3450
353k
                m_chainman.m_options.signals->ActiveTipChange(*Assert(pindexNewTip), m_chainman.IsInitialBlockDownload());
3451
353k
            }
3452
354k
        } // release cs_main
3453
        // When we reach this point, we switched to a new tip (stored in pindexNewTip).
3454
3455
0
        bool reached_target;
3456
354k
        {
3457
354k
            LOCK(m_chainman.GetMutex());
3458
354k
            if (exited_ibd) {
  Branch (3458:17): [True: 2.55k, False: 351k]
3459
                // If a background chainstate is in use, we may need to rebalance our
3460
                // allocation of caches once a chainstate exits initial block download.
3461
2.55k
                m_chainman.MaybeRebalanceCaches();
3462
2.55k
            }
3463
3464
            // Write changes periodically to disk, after relay.
3465
354k
            if (!FlushStateToDisk(state, FlushStateMode::PERIODIC)) {
  Branch (3465:17): [True: 0, False: 354k]
3466
0
                return false;
3467
0
            }
3468
3469
354k
            reached_target = ReachedTarget();
3470
354k
        }
3471
3472
354k
        if (reached_target) {
  Branch (3472:13): [True: 0, False: 354k]
3473
            // Chainstate has reached the target block, so exit.
3474
            //
3475
            // Restart indexes so indexes can resync and index new blocks after
3476
            // the target block.
3477
            //
3478
            // This cannot be done while holding cs_main (within
3479
            // MaybeValidateSnapshot) or a cs_main deadlock will occur.
3480
0
            if (m_chainman.snapshot_download_completed) {
  Branch (3480:17): [True: 0, False: 0]
3481
0
                m_chainman.snapshot_download_completed();
3482
0
            }
3483
0
            break;
3484
0
        }
3485
3486
        // We check interrupt only after giving ActivateBestChainStep a chance to run once so that we
3487
        // never interrupt before connecting the genesis block during LoadChainTip(). Previously this
3488
        // caused an assert() failure during interrupt in such cases as the UTXO DB flushing checks
3489
        // that the best block hash is non-null.
3490
354k
        if (m_chainman.m_interrupt) break;
  Branch (3490:13): [True: 0, False: 354k]
3491
354k
    } while (pindexNewTip != pindexMostWork);
  Branch (3491:14): [True: 9.46k, False: 344k]
3492
3493
344k
    m_chainman.CheckBlockIndex();
3494
3495
344k
    return true;
3496
357k
}
3497
3498
bool Chainstate::PreciousBlock(BlockValidationState& state, CBlockIndex* pindex)
3499
0
{
3500
0
    AssertLockNotHeld(m_chainstate_mutex);
3501
0
    AssertLockNotHeld(::cs_main);
3502
0
    {
3503
0
        LOCK(cs_main);
3504
0
        if (pindex->nChainWork < m_chain.Tip()->nChainWork) {
  Branch (3504:13): [True: 0, False: 0]
3505
            // Nothing to do, this block is not at the tip.
3506
0
            return true;
3507
0
        }
3508
0
        if (m_chain.Tip()->nChainWork > m_chainman.nLastPreciousChainwork) {
  Branch (3508:13): [True: 0, False: 0]
3509
            // The chain has been extended since the last call, reset the counter.
3510
0
            m_chainman.nBlockReverseSequenceId = -1;
3511
0
        }
3512
0
        m_chainman.nLastPreciousChainwork = m_chain.Tip()->nChainWork;
3513
0
        setBlockIndexCandidates.erase(pindex);
3514
0
        pindex->nSequenceId = m_chainman.nBlockReverseSequenceId;
3515
0
        if (m_chainman.nBlockReverseSequenceId > std::numeric_limits<int32_t>::min()) {
  Branch (3515:13): [True: 0, False: 0]
3516
            // We can't keep reducing the counter if somebody really wants to
3517
            // call preciousblock 2**31-1 times on the same set of tips...
3518
0
            m_chainman.nBlockReverseSequenceId--;
3519
0
        }
3520
0
        if (pindex->IsValid(BLOCK_VALID_TRANSACTIONS) && pindex->HaveNumChainTxs()) {
  Branch (3520:13): [True: 0, False: 0]
  Branch (3520:58): [True: 0, False: 0]
3521
0
            setBlockIndexCandidates.insert(pindex);
3522
0
            PruneBlockIndexCandidates();
3523
0
        }
3524
0
    }
3525
3526
0
    return ActivateBestChain(state, std::shared_ptr<const CBlock>());
3527
0
}
3528
3529
bool Chainstate::InvalidateBlock(BlockValidationState& state, CBlockIndex* const pindex)
3530
0
{
3531
0
    AssertLockNotHeld(m_chainstate_mutex);
3532
0
    AssertLockNotHeld(::cs_main);
3533
3534
    // Genesis block can't be invalidated
3535
0
    assert(pindex);
  Branch (3535:5): [True: 0, False: 0]
3536
0
    if (pindex->nHeight == 0) return false;
  Branch (3536:9): [True: 0, False: 0]
3537
3538
    // We do not allow ActivateBestChain() to run while InvalidateBlock() is
3539
    // running, as that could cause the tip to change while we disconnect
3540
    // blocks.
3541
0
    LOCK(m_chainstate_mutex);
3542
3543
    // We'll be acquiring and releasing cs_main below, to allow the validation
3544
    // callbacks to run. However, we should keep the block index in a
3545
    // consistent state as we disconnect blocks -- in particular we need to
3546
    // add equal-work blocks to setBlockIndexCandidates as we disconnect.
3547
    // To avoid walking the block index repeatedly in search of candidates,
3548
    // build a map once so that we can look up candidate blocks by chain
3549
    // work as we go.
3550
0
    std::multimap<const arith_uint256, CBlockIndex*> highpow_outofchain_headers;
3551
3552
0
    {
3553
0
        LOCK(cs_main);
3554
0
        for (auto& entry : m_blockman.m_block_index) {
  Branch (3554:26): [True: 0, False: 0]
3555
0
            CBlockIndex& candidate = entry.second;
3556
            // We don't need to put anything in our active chain into the
3557
            // multimap, because those candidates will be found and considered
3558
            // as we disconnect.
3559
            // Instead, consider only non-active-chain blocks that score
3560
            // at least as good with CBlockIndexWorkComparator as the new tip.
3561
0
            if (!m_chain.Contains(candidate) &&
  Branch (3561:17): [True: 0, False: 0]
  Branch (3561:17): [True: 0, False: 0]
3562
0
                !CBlockIndexWorkComparator()(&candidate, pindex->pprev) &&
  Branch (3562:17): [True: 0, False: 0]
3563
0
                !(candidate.nStatus & BLOCK_FAILED_VALID)) {
  Branch (3563:17): [True: 0, False: 0]
3564
0
                highpow_outofchain_headers.insert({candidate.nChainWork, &candidate});
3565
0
            }
3566
0
        }
3567
0
    }
3568
3569
0
    CBlockIndex* to_mark_failed = pindex;
3570
0
    bool pindex_was_in_chain = false;
3571
0
    int disconnected = 0;
3572
3573
    // Disconnect (descendants of) pindex, and mark them invalid.
3574
0
    while (true) {
  Branch (3574:12): [Folded - Ignored]
3575
0
        if (m_chainman.m_interrupt) break;
  Branch (3575:13): [True: 0, False: 0]
3576
3577
        // Make sure the queue of validation callbacks doesn't grow unboundedly.
3578
0
        if (m_chainman.m_options.signals) LimitValidationInterfaceQueue(*m_chainman.m_options.signals);
  Branch (3578:13): [True: 0, False: 0]
3579
3580
0
        LOCK(cs_main);
3581
        // Lock for as long as disconnectpool is in scope to make sure MaybeUpdateMempoolForReorg is
3582
        // called after DisconnectTip without unlocking in between
3583
0
        LOCK(MempoolMutex());
3584
0
        if (!m_chain.Contains(*pindex)) break;
  Branch (3584:13): [True: 0, False: 0]
3585
0
        pindex_was_in_chain = true;
3586
0
        CBlockIndex* const disconnected_tip{m_chain.Tip()};
3587
3588
        // ActivateBestChain considers blocks already in m_chain
3589
        // unconditionally valid already, so force disconnect away from it.
3590
0
        DisconnectedBlockTransactions disconnectpool{MAX_DISCONNECTED_TX_POOL_BYTES};
3591
0
        bool ret = DisconnectTip(state, &disconnectpool);
3592
        // DisconnectTip will add transactions to disconnectpool.
3593
        // Adjust the mempool to be consistent with the new tip, adding
3594
        // transactions back to the mempool if disconnecting was successful,
3595
        // and we're not doing a very deep invalidation (in which case
3596
        // keeping the mempool up to date is probably futile anyway).
3597
0
        MaybeUpdateMempoolForReorg(disconnectpool, /* fAddToMempool = */ (++disconnected <= 10) && ret);
  Branch (3597:74): [True: 0, False: 0]
  Branch (3597:100): [True: 0, False: 0]
3598
0
        if (!ret) return false;
  Branch (3598:13): [True: 0, False: 0]
3599
0
        CBlockIndex* new_tip{m_chain.Tip()};
3600
0
        assert(disconnected_tip->pprev == new_tip);
  Branch (3600:9): [True: 0, False: 0]
3601
3602
        // We immediately mark the disconnected blocks as invalid.
3603
        // This prevents a case where pruned nodes may fail to invalidateblock
3604
        // and be left unable to start as they have no tip candidates (as there
3605
        // are no blocks that meet the "have data and are not invalid per
3606
        // nStatus" criteria for inclusion in setBlockIndexCandidates).
3607
0
        disconnected_tip->nStatus |= BLOCK_FAILED_VALID;
3608
0
        m_blockman.m_dirty_blockindex.insert(disconnected_tip);
3609
0
        setBlockIndexCandidates.erase(disconnected_tip);
3610
0
        setBlockIndexCandidates.insert(new_tip);
3611
3612
        // Mark out-of-chain descendants of the invalidated block as invalid
3613
        // Add any equal or more work headers that are not invalidated to setBlockIndexCandidates
3614
        // Recalculate m_best_header if it became invalid.
3615
0
        auto candidate_it = highpow_outofchain_headers.lower_bound(new_tip->nChainWork);
3616
3617
0
        const bool best_header_needs_update{m_chainman.m_best_header->GetAncestor(disconnected_tip->nHeight) == disconnected_tip};
3618
0
        if (best_header_needs_update) {
  Branch (3618:13): [True: 0, False: 0]
3619
            // new_tip is definitely still valid at this point, but there may be better ones
3620
0
            m_chainman.m_best_header = new_tip;
3621
0
        }
3622
3623
0
        while (candidate_it != highpow_outofchain_headers.end()) {
  Branch (3623:16): [True: 0, False: 0]
3624
0
            CBlockIndex* candidate{candidate_it->second};
3625
0
            if (candidate->GetAncestor(disconnected_tip->nHeight) == disconnected_tip) {
  Branch (3625:17): [True: 0, False: 0]
3626
                // Children of failed blocks are marked as BLOCK_FAILED_VALID.
3627
0
                candidate->nStatus |= BLOCK_FAILED_VALID;
3628
0
                m_blockman.m_dirty_blockindex.insert(candidate);
3629
                // If invalidated, the block is irrelevant for setBlockIndexCandidates
3630
                // and for m_best_header and can be removed from the cache.
3631
0
                candidate_it = highpow_outofchain_headers.erase(candidate_it);
3632
0
                continue;
3633
0
            }
3634
0
            if (!CBlockIndexWorkComparator()(candidate, new_tip) &&
  Branch (3634:17): [True: 0, False: 0]
  Branch (3634:17): [True: 0, False: 0]
3635
0
                candidate->IsValid(BLOCK_VALID_TRANSACTIONS) &&
  Branch (3635:17): [True: 0, False: 0]
3636
0
                candidate->HaveNumChainTxs()) {
  Branch (3636:17): [True: 0, False: 0]
3637
0
                setBlockIndexCandidates.insert(candidate);
3638
                // Do not remove candidate from the highpow_outofchain_headers cache, because it might be a descendant of the block being invalidated
3639
                // which needs to be marked failed later.
3640
0
            }
3641
0
            if (best_header_needs_update &&
  Branch (3641:17): [True: 0, False: 0]
3642
0
                m_chainman.m_best_header->nChainWork < candidate->nChainWork) {
  Branch (3642:17): [True: 0, False: 0]
3643
0
                m_chainman.m_best_header = candidate;
3644
0
            }
3645
0
            ++candidate_it;
3646
0
        }
3647
3648
        // Track the last disconnected block to call InvalidChainFound on it.
3649
0
        to_mark_failed = disconnected_tip;
3650
0
    }
3651
3652
0
    m_chainman.CheckBlockIndex();
3653
3654
0
    {
3655
0
        LOCK(cs_main);
3656
0
        if (m_chain.Contains(*to_mark_failed)) {
  Branch (3656:13): [True: 0, False: 0]
3657
            // If the to-be-marked invalid block is in the active chain, something is interfering and we can't proceed.
3658
0
            return false;
3659
0
        }
3660
3661
        // Mark pindex as invalid if it never was in the main chain
3662
0
        if (!pindex_was_in_chain && !(pindex->nStatus & BLOCK_FAILED_VALID)) {
  Branch (3662:13): [True: 0, False: 0]
  Branch (3662:37): [True: 0, False: 0]
3663
0
            pindex->nStatus |= BLOCK_FAILED_VALID;
3664
0
            m_blockman.m_dirty_blockindex.insert(pindex);
3665
0
            setBlockIndexCandidates.erase(pindex);
3666
0
        }
3667
3668
        // If any new blocks somehow arrived while we were disconnecting
3669
        // (above), then the pre-calculation of what should go into
3670
        // setBlockIndexCandidates may have missed entries. This would
3671
        // technically be an inconsistency in the block index, but if we clean
3672
        // it up here, this should be an essentially unobservable error.
3673
        // Loop back over all block index entries and add any missing entries
3674
        // to setBlockIndexCandidates.
3675
0
        for (auto& [_, block_index] : m_blockman.m_block_index) {
  Branch (3675:37): [True: 0, False: 0]
3676
0
            if (block_index.IsValid(BLOCK_VALID_TRANSACTIONS) && block_index.HaveNumChainTxs() && !setBlockIndexCandidates.value_comp()(&block_index, m_chain.Tip())) {
  Branch (3676:17): [True: 0, False: 0]
  Branch (3676:17): [True: 0, False: 0]
  Branch (3676:66): [True: 0, False: 0]
  Branch (3676:99): [True: 0, False: 0]
3677
0
                setBlockIndexCandidates.insert(&block_index);
3678
0
            }
3679
0
        }
3680
3681
0
        InvalidChainFound(to_mark_failed);
3682
0
    }
3683
3684
    // Only notify about a new block tip if the active chain was modified.
3685
0
    if (pindex_was_in_chain) {
  Branch (3685:9): [True: 0, False: 0]
3686
        // Ignoring return value for now, this could be changed to bubble up
3687
        // kernel::Interrupted value to the caller so the caller could
3688
        // distinguish between completed and interrupted operations. It might
3689
        // also make sense for the blockTip notification to have an enum
3690
        // parameter indicating the source of the tip change so hooks can
3691
        // distinguish user-initiated invalidateblock changes from other
3692
        // changes.
3693
0
        (void)m_chainman.GetNotifications().blockTip(
3694
0
            /*state=*/GetSynchronizationState(m_chainman.IsInitialBlockDownload(), m_chainman.m_blockman.m_blockfiles_indexed),
3695
0
            /*index=*/*to_mark_failed->pprev,
3696
0
            /*verification_progress=*/WITH_LOCK(m_chainman.GetMutex(), return m_chainman.GuessVerificationProgress(to_mark_failed->pprev)));
3697
3698
        // Fire ActiveTipChange now for the current chain tip to make sure clients are notified.
3699
        // ActivateBestChain may call this as well, but not necessarily.
3700
0
        if (m_chainman.m_options.signals) {
  Branch (3700:13): [True: 0, False: 0]
3701
0
            m_chainman.m_options.signals->ActiveTipChange(*Assert(m_chain.Tip()), m_chainman.IsInitialBlockDownload());
3702
0
        }
3703
0
    }
3704
0
    return true;
3705
0
}
3706
3707
void Chainstate::SetBlockFailureFlags(CBlockIndex* invalid_block)
3708
23.6k
{
3709
23.6k
    AssertLockHeld(cs_main);
3710
3711
2.13M
    for (auto& [_, block_index] : m_blockman.m_block_index) {
  Branch (3711:33): [True: 2.13M, False: 23.6k]
3712
2.13M
        if (invalid_block != &block_index && block_index.GetAncestor(invalid_block->nHeight) == invalid_block) {
  Branch (3712:13): [True: 2.11M, False: 23.6k]
  Branch (3712:46): [True: 7.21k, False: 2.10M]
3713
7.21k
            block_index.nStatus |= BLOCK_FAILED_VALID;
3714
7.21k
            m_blockman.m_dirty_blockindex.insert(&block_index);
3715
7.21k
        }
3716
2.13M
    }
3717
23.6k
}
3718
3719
0
void Chainstate::ResetBlockFailureFlags(CBlockIndex *pindex) {
3720
0
    AssertLockHeld(cs_main);
3721
3722
0
    int nHeight = pindex->nHeight;
3723
3724
    // Remove the invalidity flag from this block and all its descendants and ancestors.
3725
0
    for (auto& [_, block_index] : m_blockman.m_block_index) {
  Branch (3725:33): [True: 0, False: 0]
3726
0
        if ((block_index.nStatus & BLOCK_FAILED_VALID) && (block_index.GetAncestor(nHeight) == pindex || pindex->GetAncestor(block_index.nHeight) == &block_index)) {
  Branch (3726:13): [True: 0, False: 0]
  Branch (3726:60): [True: 0, False: 0]
  Branch (3726:106): [True: 0, False: 0]
3727
0
            block_index.nStatus &= ~BLOCK_FAILED_VALID;
3728
0
            m_blockman.m_dirty_blockindex.insert(&block_index);
3729
0
            if (block_index.IsValid(BLOCK_VALID_TRANSACTIONS) && block_index.HaveNumChainTxs() && setBlockIndexCandidates.value_comp()(m_chain.Tip(), &block_index)) {
  Branch (3729:17): [True: 0, False: 0]
  Branch (3729:17): [True: 0, False: 0]
  Branch (3729:66): [True: 0, False: 0]
  Branch (3729:99): [True: 0, False: 0]
3730
0
                setBlockIndexCandidates.insert(&block_index);
3731
0
            }
3732
0
            if (&block_index == m_chainman.m_best_invalid) {
  Branch (3732:17): [True: 0, False: 0]
3733
                // Reset invalid block marker if it was pointing to one of those.
3734
0
                m_chainman.m_best_invalid = nullptr;
3735
0
            }
3736
0
        }
3737
0
    }
3738
0
}
3739
3740
void Chainstate::TryAddBlockIndexCandidate(CBlockIndex* pindex)
3741
363k
{
3742
363k
    AssertLockHeld(cs_main);
3743
3744
    // Do not continue building a chainstate that is based on an invalid
3745
    // snapshot. This is a belt-and-suspenders type of check because if an
3746
    // invalid snapshot is loaded, the node will shut down to force a manual
3747
    // intervention. But it is good to handle this case correctly regardless.
3748
363k
    if (m_assumeutxo == Assumeutxo::INVALID) {
  Branch (3748:9): [True: 0, False: 363k]
3749
0
        return;
3750
0
    }
3751
3752
    // The block only is a candidate for the most-work-chain if it has the same
3753
    // or more work than our current tip.
3754
363k
    if (m_chain.Tip() != nullptr && setBlockIndexCandidates.value_comp()(pindex, m_chain.Tip())) {
  Branch (3754:9): [True: 357k, False: 6.28k]
  Branch (3754:9): [True: 2.22k, False: 361k]
  Branch (3754:37): [True: 2.22k, False: 355k]
3755
2.22k
        return;
3756
2.22k
    }
3757
3758
361k
    const CBlockIndex* target_block{TargetBlock()};
3759
361k
    if (!target_block) {
  Branch (3759:9): [True: 361k, False: 0]
3760
        // If no specific target block, add all entries that have more
3761
        // work than the tip.
3762
361k
        setBlockIndexCandidates.insert(pindex);
3763
361k
    } else {
3764
        // If there is a target block, only consider connecting blocks
3765
        // towards the target block.
3766
0
        if (target_block->GetAncestor(pindex->nHeight) == pindex) {
  Branch (3766:13): [True: 0, False: 0]
3767
0
            setBlockIndexCandidates.insert(pindex);
3768
0
        }
3769
0
    }
3770
361k
}
3771
3772
/** Mark a block as having its data received and checked (up to BLOCK_VALID_TRANSACTIONS). */
3773
void ChainstateManager::ReceivedBlockTransactions(const CBlock& block, CBlockIndex* pindexNew, const FlatFilePos& pos)
3774
361k
{
3775
361k
    AssertLockHeld(cs_main);
3776
361k
    pindexNew->nTx = block.vtx.size();
3777
    // Typically m_chain_tx_count will be 0 at this point, but it can be nonzero if this
3778
    // is a pruned block which is being downloaded again, or if this is an
3779
    // assumeutxo snapshot block which has a hardcoded m_chain_tx_count value from the
3780
    // snapshot metadata. If the pindex is not the snapshot block and the
3781
    // m_chain_tx_count value is not zero, assert that value is actually correct.
3782
364k
    auto prev_tx_sum = [](CBlockIndex& block) { return block.nTx + (block.pprev ? block.pprev->m_chain_tx_count : 0); };
  Branch (3782:69): [True: 358k, False: 5.58k]
3783
361k
    if (!Assume(pindexNew->m_chain_tx_count == 0 || pindexNew->m_chain_tx_count == prev_tx_sum(*pindexNew) ||
  Branch (3783:9): [True: 0, False: 361k]
3784
361k
                std::ranges::any_of(m_chainstates, [&](const auto& cs) EXCLUSIVE_LOCKS_REQUIRED(cs_main) { return cs->SnapshotBase() == pindexNew; }))) {
3785
0
        LogWarning("Internal bug detected: block %d has unexpected m_chain_tx_count %i that should be %i (%s %s). Please report this issue here: %s\n",
3786
0
            pindexNew->nHeight, pindexNew->m_chain_tx_count, prev_tx_sum(*pindexNew), CLIENT_NAME, FormatFullVersion(), CLIENT_BUGREPORT);
3787
0
        pindexNew->m_chain_tx_count = 0;
3788
0
    }
3789
361k
    pindexNew->nFile = pos.nFile;
3790
361k
    pindexNew->nDataPos = pos.nPos;
3791
361k
    pindexNew->nUndoPos = 0;
3792
361k
    pindexNew->nStatus |= BLOCK_HAVE_DATA;
3793
361k
    if (DeploymentActiveAt(*pindexNew, *this, Consensus::DEPLOYMENT_SEGWIT)) {
  Branch (3793:9): [True: 361k, False: 0]
3794
361k
        pindexNew->nStatus |= BLOCK_OPT_WITNESS;
3795
361k
    }
3796
361k
    pindexNew->RaiseValidity(BLOCK_VALID_TRANSACTIONS);
3797
361k
    m_blockman.m_dirty_blockindex.insert(pindexNew);
3798
3799
361k
    if (pindexNew->pprev == nullptr || pindexNew->pprev->HaveNumChainTxs()) {
  Branch (3799:9): [True: 3.95k, False: 357k]
  Branch (3799:40): [True: 353k, False: 3.28k]
3800
        // If pindexNew is the genesis block or all parents are BLOCK_VALID_TRANSACTIONS.
3801
357k
        std::deque<CBlockIndex*> queue;
3802
357k
        queue.push_back(pindexNew);
3803
3804
        // Recursively process any descendant blocks that now may be eligible to be connected.
3805
717k
        while (!queue.empty()) {
  Branch (3805:16): [True: 360k, False: 357k]
3806
360k
            CBlockIndex *pindex = queue.front();
3807
360k
            queue.pop_front();
3808
            // Before setting m_chain_tx_count, assert that it is 0 or already set to
3809
            // the correct value. This assert will fail after receiving the
3810
            // assumeutxo snapshot block if assumeutxo snapshot metadata has an
3811
            // incorrect hardcoded AssumeutxoData::m_chain_tx_count value.
3812
360k
            if (!Assume(pindex->m_chain_tx_count == 0 || pindex->m_chain_tx_count == prev_tx_sum(*pindex))) {
  Branch (3812:17): [True: 0, False: 360k]
3813
0
                LogWarning("Internal bug detected: block %d has unexpected m_chain_tx_count %i that should be %i (%s %s). Please report this issue here: %s\n",
3814
0
                   pindex->nHeight, pindex->m_chain_tx_count, prev_tx_sum(*pindex), CLIENT_NAME, FormatFullVersion(), CLIENT_BUGREPORT);
3815
0
            }
3816
360k
            pindex->m_chain_tx_count = prev_tx_sum(*pindex);
3817
360k
            pindex->nSequenceId = nBlockSequenceId++;
3818
360k
            for (const auto& c : m_chainstates) {
  Branch (3818:32): [True: 360k, False: 360k]
3819
360k
                c->TryAddBlockIndexCandidate(pindex);
3820
360k
            }
3821
360k
            std::pair<std::multimap<CBlockIndex*, CBlockIndex*>::iterator, std::multimap<CBlockIndex*, CBlockIndex*>::iterator> range = m_blockman.m_blocks_unlinked.equal_range(pindex);
3822
362k
            while (range.first != range.second) {
  Branch (3822:20): [True: 2.40k, False: 360k]
3823
2.40k
                std::multimap<CBlockIndex*, CBlockIndex*>::iterator it = range.first;
3824
2.40k
                queue.push_back(it->second);
3825
2.40k
                range.first++;
3826
2.40k
                m_blockman.m_blocks_unlinked.erase(it);
3827
2.40k
            }
3828
360k
        }
3829
357k
    } else {
3830
3.28k
        if (pindexNew->pprev && pindexNew->pprev->IsValid(BLOCK_VALID_TREE)) {
  Branch (3830:13): [True: 3.28k, False: 0]
  Branch (3830:33): [True: 3.28k, False: 0]
3831
3.28k
            m_blockman.AddUnlinkedBlock(pindexNew);
3832
3.28k
        }
3833
3.28k
    }
3834
361k
}
3835
3836
static bool CheckBlockHeader(const CBlockHeader& block, BlockValidationState& state, const Consensus::Params& consensusParams, bool fCheckPOW = true)
3837
1.73M
{
3838
    // Check proof of work matches claimed amount
3839
1.73M
    if (fCheckPOW && !CheckProofOfWork(block.GetHash(), block.nBits, consensusParams))
  Branch (3839:9): [True: 971k, False: 765k]
  Branch (3839:22): [True: 15.5k, False: 956k]
3840
15.5k
        return state.Invalid(BlockValidationResult::BLOCK_INVALID_HEADER, "high-hash", "proof of work failed");
3841
3842
1.72M
    return true;
3843
1.73M
}
3844
3845
static bool CheckMerkleRoot(const CBlock& block, BlockValidationState& state)
3846
409k
{
3847
409k
    if (block.m_checked_merkle_root) return true;
  Branch (3847:9): [True: 7.90k, False: 401k]
3848
3849
401k
    bool mutated;
3850
401k
    uint256 merkle_root = BlockMerkleRoot(block, &mutated);
3851
401k
    if (block.hashMerkleRoot != merkle_root) {
  Branch (3851:9): [True: 10.4k, False: 390k]
3852
10.4k
        return state.Invalid(
3853
10.4k
            /*result=*/BlockValidationResult::BLOCK_MUTATED,
3854
10.4k
            /*reject_reason=*/"bad-txnmrklroot",
3855
10.4k
            /*debug_message=*/"hashMerkleRoot mismatch");
3856
10.4k
    }
3857
3858
    // Check for merkle tree malleability (CVE-2012-2459): repeating sequences
3859
    // of transactions in a block without affecting the merkle root of a block,
3860
    // while still invalidating it.
3861
390k
    if (mutated) {
  Branch (3861:9): [True: 1.55k, False: 389k]
3862
1.55k
        return state.Invalid(
3863
1.55k
            /*result=*/BlockValidationResult::BLOCK_MUTATED,
3864
1.55k
            /*reject_reason=*/"bad-txns-duplicate",
3865
1.55k
            /*debug_message=*/"duplicate transaction");
3866
1.55k
    }
3867
3868
389k
    block.m_checked_merkle_root = true;
3869
389k
    return true;
3870
390k
}
3871
3872
/** CheckWitnessMalleation performs checks for block malleation with regard to
3873
 * its witnesses.
3874
 *
3875
 * Note: If the witness commitment is expected (i.e. `expect_witness_commitment
3876
 * = true`), then the block is required to have at least one transaction and the
3877
 * first transaction needs to have at least one input. */
3878
static bool CheckWitnessMalleation(const CBlock& block, bool expect_witness_commitment, BlockValidationState& state)
3879
740k
{
3880
740k
    if (expect_witness_commitment) {
  Branch (3880:9): [True: 733k, False: 6.43k]
3881
733k
        if (block.m_checked_witness_commitment) return true;
  Branch (3881:13): [True: 350k, False: 383k]
3882
3883
383k
        int commitpos = GetWitnessCommitmentIndex(block);
3884
383k
        if (commitpos != NO_WITNESS_COMMITMENT) {
  Branch (3884:13): [True: 383k, False: 23]
3885
383k
            assert(!block.vtx.empty() && !block.vtx[0]->vin.empty());
  Branch (3885:13): [True: 383k, False: 0]
  Branch (3885:13): [True: 383k, False: 0]
  Branch (3885:13): [True: 383k, False: 0]
3886
383k
            const auto& witness_stack{block.vtx[0]->vin[0].scriptWitness.stack};
3887
3888
383k
            if (witness_stack.size() != 1 || witness_stack[0].size() != 32) {
  Branch (3888:17): [True: 2, False: 383k]
  Branch (3888:46): [True: 2, False: 383k]
3889
4
                return state.Invalid(
3890
4
                    /*result=*/BlockValidationResult::BLOCK_MUTATED,
3891
4
                    /*reject_reason=*/"bad-witness-nonce-size",
3892
4
                    /*debug_message=*/strprintf("%s : invalid witness reserved value size", __func__));
3893
4
            }
3894
3895
            // The malleation check is ignored; as the transaction tree itself
3896
            // already does not permit it, it is impossible to trigger in the
3897
            // witness tree.
3898
383k
            uint256 hash_witness = BlockWitnessMerkleRoot(block);
3899
3900
383k
            CHash256().Write(hash_witness).Write(witness_stack[0]).Finalize(hash_witness);
3901
383k
            if (memcmp(hash_witness.begin(), &block.vtx[0]->vout[commitpos].scriptPubKey[6], 32)) {
  Branch (3901:17): [True: 4, False: 383k]
3902
4
                return state.Invalid(
3903
4
                    /*result=*/BlockValidationResult::BLOCK_MUTATED,
3904
4
                    /*reject_reason=*/"bad-witness-merkle-match",
3905
4
                    /*debug_message=*/strprintf("%s : witness merkle commitment mismatch", __func__));
3906
4
            }
3907
3908
383k
            block.m_checked_witness_commitment = true;
3909
383k
            return true;
3910
383k
        }
3911
383k
    }
3912
3913
    // No witness data is allowed in blocks that don't commit to witness data, as this would otherwise leave room for spam
3914
6.46k
    for (const auto& tx : block.vtx) {
  Branch (3914:25): [True: 6.46k, False: 6.45k]
3915
6.46k
        if (tx->HasWitness()) {
  Branch (3915:13): [True: 7, False: 6.45k]
3916
7
            return state.Invalid(
3917
7
                /*result=*/BlockValidationResult::BLOCK_MUTATED,
3918
7
                /*reject_reason=*/"unexpected-witness",
3919
7
                /*debug_message=*/strprintf("%s : unexpected witness data found", __func__));
3920
7
        }
3921
6.46k
    }
3922
3923
6.45k
    return true;
3924
6.46k
}
3925
3926
bool CheckBlock(const CBlock& block, BlockValidationState& state, const Consensus::Params& consensusParams, bool fCheckPOW, bool fCheckMerkleRoot)
3927
1.86M
{
3928
    // These are checks that are independent of context.
3929
3930
1.86M
    if (block.fChecked)
  Branch (3930:9): [True: 701k, False: 1.16M]
3931
701k
        return true;
3932
3933
    // Check that the header is valid (particularly PoW).  This is mostly
3934
    // redundant with the call in AcceptBlockHeader.
3935
1.16M
    if (!CheckBlockHeader(block, state, consensusParams, fCheckPOW))
  Branch (3935:9): [True: 1.16k, False: 1.16M]
3936
1.16k
        return false;
3937
3938
    // Signet only: check block solution
3939
1.16M
    if (consensusParams.signet_blocks && fCheckPOW && !CheckSignetBlockSolution(block, consensusParams)) {
  Branch (3939:9): [True: 0, False: 1.16M]
  Branch (3939:42): [True: 0, False: 0]
  Branch (3939:55): [True: 0, False: 0]
3940
0
        return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-signet-blksig", "signet block signature validation failure");
3941
0
    }
3942
3943
    // Check the merkle root.
3944
1.16M
    if (fCheckMerkleRoot && !CheckMerkleRoot(block, state)) {
  Branch (3944:9): [True: 400k, False: 765k]
  Branch (3944:29): [True: 11.8k, False: 388k]
3945
11.8k
        return false;
3946
11.8k
    }
3947
3948
    // All potential-corruption validation must be done before we do any
3949
    // transaction validation, as otherwise we may mark the header as invalid
3950
    // because we receive the wrong transactions for it.
3951
    // Note that witness malleability is checked in ContextualCheckBlock, so no
3952
    // checks that use witness data may be performed here.
3953
3954
    // Size limits
3955
1.15M
    if (block.vtx.empty() || block.vtx.size() * WITNESS_SCALE_FACTOR > MAX_BLOCK_WEIGHT || ::GetSerializeSize(TX_NO_WITNESS(block)) * WITNESS_SCALE_FACTOR > MAX_BLOCK_WEIGHT)
  Branch (3955:9): [True: 322, False: 1.15M]
  Branch (3955:9): [True: 332, False: 1.15M]
  Branch (3955:30): [True: 0, False: 1.15M]
  Branch (3955:92): [True: 10, False: 1.15M]
3956
332
        return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-blk-length", "size limits failed");
3957
3958
    // First transaction must be coinbase, the rest must not be
3959
1.15M
    if (block.vtx.empty() || !block.vtx[0]->IsCoinBase())
  Branch (3959:9): [True: 0, False: 1.15M]
  Branch (3959:30): [True: 5.82k, False: 1.14M]
3960
5.82k
        return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-cb-missing", "first tx is not coinbase");
3961
1.47M
    for (unsigned int i = 1; i < block.vtx.size(); i++)
  Branch (3961:30): [True: 330k, False: 1.14M]
3962
330k
        if (block.vtx[i]->IsCoinBase())
  Branch (3962:13): [True: 17, False: 330k]
3963
17
            return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-cb-multiple", "more than one coinbase");
3964
3965
    // Check transactions
3966
    // Must check for duplicate inputs (see CVE-2018-17144)
3967
1.30M
    for (const auto& tx : block.vtx) {
  Branch (3967:25): [True: 1.30M, False: 1.14M]
3968
1.30M
        TxValidationState tx_state;
3969
1.30M
        if (!CheckTransaction(*tx, tx_state)) {
  Branch (3969:13): [True: 930, False: 1.30M]
3970
            // CheckBlock() does context-free validation checks. The only
3971
            // possible failures are consensus failures.
3972
930
            assert(tx_state.GetResult() == TxValidationResult::TX_CONSENSUS);
  Branch (3972:13): [True: 930, False: 0]
3973
930
            return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, tx_state.GetRejectReason(),
3974
930
                                 strprintf("Transaction check failed (tx hash %s) %s", tx->GetHash().ToString(), tx_state.GetDebugMessage()));
3975
930
        }
3976
1.30M
    }
3977
    // This underestimates the number of sigops, because unlike ConnectBlock it
3978
    // does not count witness and p2sh sigops.
3979
1.14M
    unsigned int nSigOps = 0;
3980
1.14M
    for (const auto& tx : block.vtx)
  Branch (3980:25): [True: 1.29M, False: 1.14M]
3981
1.29M
    {
3982
1.29M
        nSigOps += GetLegacySigOpCount(*tx);
3983
1.29M
    }
3984
1.14M
    if (nSigOps * WITNESS_SCALE_FACTOR > MAX_BLOCK_SIGOPS_COST)
  Branch (3984:9): [True: 11, False: 1.14M]
3985
11
        return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-blk-sigops", "out-of-bounds SigOpCount");
3986
3987
1.14M
    if (fCheckPOW && fCheckMerkleRoot)
  Branch (3987:9): [True: 382k, False: 764k]
  Branch (3987:22): [True: 382k, False: 48]
3988
382k
        block.fChecked = true;
3989
3990
1.14M
    return true;
3991
1.14M
}
3992
3993
void ChainstateManager::UpdateUncommittedBlockStructures(CBlock& block, const CBlockIndex* pindexPrev) const
3994
540k
{
3995
540k
    int commitpos = GetWitnessCommitmentIndex(block);
3996
540k
    static const std::vector<unsigned char> nonce(32, 0x00);
3997
540k
    if (commitpos != NO_WITNESS_COMMITMENT && DeploymentActiveAfter(pindexPrev, *this, Consensus::DEPLOYMENT_SEGWIT) && !block.vtx[0]->HasWitness()) {
  Branch (3997:9): [True: 540k, False: 1]
  Branch (3997:47): [True: 540k, False: 0]
  Branch (3997:121): [True: 415k, False: 124k]
3998
415k
        CMutableTransaction tx(*block.vtx[0]);
3999
415k
        tx.vin[0].scriptWitness.stack.resize(1);
4000
415k
        tx.vin[0].scriptWitness.stack[0] = nonce;
4001
415k
        block.vtx[0] = MakeTransactionRef(std::move(tx));
4002
415k
    }
4003
540k
}
4004
4005
void ChainstateManager::GenerateCoinbaseCommitment(CBlock& block, const CBlockIndex* pindexPrev) const
4006
540k
{
4007
540k
    int commitpos = GetWitnessCommitmentIndex(block);
4008
540k
    std::vector<unsigned char> ret(32, 0x00);
4009
540k
    if (commitpos == NO_WITNESS_COMMITMENT) {
  Branch (4009:9): [True: 536k, False: 3.25k]
4010
536k
        uint256 witnessroot = BlockWitnessMerkleRoot(block);
4011
536k
        CHash256().Write(witnessroot).Write(ret).Finalize(witnessroot);
4012
536k
        CTxOut out;
4013
536k
        out.nValue = 0;
4014
536k
        out.scriptPubKey.resize(MINIMUM_WITNESS_COMMITMENT);
4015
536k
        out.scriptPubKey[0] = OP_RETURN;
4016
536k
        out.scriptPubKey[1] = 0x24;
4017
536k
        out.scriptPubKey[2] = 0xaa;
4018
536k
        out.scriptPubKey[3] = 0x21;
4019
536k
        out.scriptPubKey[4] = 0xa9;
4020
536k
        out.scriptPubKey[5] = 0xed;
4021
536k
        memcpy(&out.scriptPubKey[6], witnessroot.begin(), 32);
4022
536k
        CMutableTransaction tx(*block.vtx[0]);
4023
536k
        tx.vout.push_back(out);
4024
536k
        block.vtx[0] = MakeTransactionRef(std::move(tx));
4025
536k
    }
4026
540k
    UpdateUncommittedBlockStructures(block, pindexPrev);
4027
540k
}
4028
4029
bool HasValidProofOfWork(std::span<const CBlockHeader> headers, const Consensus::Params& consensusParams)
4030
84.1k
{
4031
84.1k
    return std::ranges::all_of(headers,
4032
214k
                               [&](const auto& header) { return CheckProofOfWork(header.GetHash(), header.nBits, consensusParams); });
4033
84.1k
}
4034
4035
bool IsBlockMutated(const CBlock& block, bool check_witness_root)
4036
8.30k
{
4037
8.30k
    BlockValidationState state;
4038
8.30k
    if (!CheckMerkleRoot(block, state)) {
  Branch (4038:9): [True: 83, False: 8.22k]
4039
83
        LogDebug(BCLog::VALIDATION, "Block mutated: %s\n", state.ToString());
4040
83
        return true;
4041
83
    }
4042
4043
8.22k
    if (block.vtx.empty() || !block.vtx[0]->IsCoinBase()) {
  Branch (4043:9): [True: 479, False: 7.74k]
  Branch (4043:30): [True: 188, False: 7.55k]
4044
        // Consider the block mutated if any transaction is 64 bytes in size (see 3.1
4045
        // in "Weaknesses in Bitcoin’s Merkle Root Construction":
4046
        // https://lists.linuxfoundation.org/pipermail/bitcoin-dev/attachments/20190225/a27d8837/attachment-0001.pdf).
4047
        //
4048
        // Note: This is not a consensus change as this only applies to blocks that
4049
        // don't have a coinbase transaction and would therefore already be invalid.
4050
667
        return std::any_of(block.vtx.begin(), block.vtx.end(),
4051
1.08k
                           [](auto& tx) { return GetSerializeSize(TX_NO_WITNESS(tx)) == 64; });
4052
7.55k
    } else {
4053
        // Theoretically it is still possible for a block with a 64 byte
4054
        // coinbase transaction to be mutated but we neglect that possibility
4055
        // here as it requires at least 224 bits of work.
4056
7.55k
    }
4057
4058
7.55k
    if (!CheckWitnessMalleation(block, check_witness_root, state)) {
  Branch (4058:9): [True: 15, False: 7.53k]
4059
15
        LogDebug(BCLog::VALIDATION, "Block mutated: %s\n", state.ToString());
4060
15
        return true;
4061
15
    }
4062
4063
7.53k
    return false;
4064
7.55k
}
4065
4066
arith_uint256 CalculateClaimedHeadersWork(std::span<const CBlockHeader> headers)
4067
40.1k
{
4068
40.1k
    arith_uint256 total_work{0};
4069
207k
    for (const CBlockHeader& header : headers) {
  Branch (4069:37): [True: 207k, False: 40.1k]
4070
207k
        total_work += GetBlockProof(header);
4071
207k
    }
4072
40.1k
    return total_work;
4073
40.1k
}
4074
4075
/** Context-dependent validity checks.
4076
 *  By "context", we mean only the previous block headers, but not the UTXO
4077
 *  set; UTXO-related validity checks are done in ConnectBlock().
4078
 *  NOTE: This function is not currently invoked by ConnectBlock(), so we
4079
 *  should consider upgrade issues if we change which consensus rules are
4080
 *  enforced in this function (eg by adding a new consensus rule). See comment
4081
 *  in ConnectBlock().
4082
 *  Note that -reindex-chainstate skips the validation that happens here!
4083
 *
4084
 *  NOTE: failing to check the header's height against the last checkpoint's opened a DoS vector between
4085
 *  v0.12 and v0.15 (when no additional protection was in place) whereby an attacker could unboundedly
4086
 *  grow our in-memory block index. See https://bitcoincore.org/en/2024/07/03/disclose-header-spam.
4087
 */
4088
static bool ContextualCheckBlockHeader(const CBlockHeader& block, BlockValidationState& state, const ChainstateManager& chainman, const CBlockIndex* pindexPrev) EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
4089
931k
{
4090
931k
    AssertLockHeld(::cs_main);
4091
931k
    assert(pindexPrev != nullptr);
  Branch (4091:5): [True: 931k, False: 0]
4092
931k
    const int nHeight = pindexPrev->nHeight + 1;
4093
4094
    // Check proof of work
4095
931k
    const Consensus::Params& consensusParams = chainman.GetConsensus();
4096
931k
    if (block.nBits != GetNextWorkRequired(pindexPrev, &block, consensusParams))
  Branch (4096:9): [True: 6.09k, False: 924k]
4097
6.09k
        return state.Invalid(BlockValidationResult::BLOCK_INVALID_HEADER, "bad-diffbits", "incorrect proof of work");
4098
4099
    // Check timestamp against prev
4100
924k
    if (block.GetBlockTime() <= pindexPrev->GetMedianTimePast())
  Branch (4100:9): [True: 2.27k, False: 922k]
4101
2.27k
        return state.Invalid(BlockValidationResult::BLOCK_INVALID_HEADER, "time-too-old", "block's timestamp is too early");
4102
4103
    // Testnet4 and regtest only: Check timestamp against prev for difficulty-adjustment
4104
    // blocks to prevent timewarp attacks (see https://github.com/bitcoin/bitcoin/pull/15482).
4105
922k
    if (consensusParams.enforce_BIP94) {
  Branch (4105:9): [True: 0, False: 922k]
4106
        // Check timestamp for the first block of each difficulty adjustment
4107
        // interval, except the genesis block.
4108
0
        if (nHeight % consensusParams.DifficultyAdjustmentInterval() == 0) {
  Branch (4108:13): [True: 0, False: 0]
4109
0
            if (block.GetBlockTime() < pindexPrev->GetBlockTime() - MAX_TIMEWARP) {
  Branch (4109:17): [True: 0, False: 0]
4110
0
                return state.Invalid(BlockValidationResult::BLOCK_INVALID_HEADER, "time-timewarp-attack", "block's timestamp is too early on diff adjustment block");
4111
0
            }
4112
0
        }
4113
0
    }
4114
4115
    // Check timestamp
4116
922k
    if (block.Time() > NodeClock::now() + std::chrono::seconds{MAX_FUTURE_BLOCK_TIME}) {
  Branch (4116:9): [True: 38.4k, False: 884k]
4117
38.4k
        return state.Invalid(BlockValidationResult::BLOCK_TIME_FUTURE, "time-too-new", "block timestamp too far in the future");
4118
38.4k
    }
4119
4120
    // Reject blocks with outdated version
4121
884k
    if ((block.nVersion < 2 && DeploymentActiveAfter(pindexPrev, chainman, Consensus::DEPLOYMENT_HEIGHTINCB)) ||
  Branch (4121:10): [True: 36.2k, False: 847k]
  Branch (4121:32): [True: 35.9k, False: 342]
4122
884k
        (block.nVersion < 3 && DeploymentActiveAfter(pindexPrev, chainman, Consensus::DEPLOYMENT_DERSIG)) ||
  Branch (4122:10): [True: 2.64k, False: 845k]
  Branch (4122:32): [True: 2.08k, False: 562]
4123
884k
        (block.nVersion < 4 && DeploymentActiveAfter(pindexPrev, chainman, Consensus::DEPLOYMENT_CLTV))) {
  Branch (4123:10): [True: 2.11k, False: 844k]
  Branch (4123:32): [True: 1.28k, False: 835]
4124
39.3k
            return state.Invalid(BlockValidationResult::BLOCK_INVALID_HEADER, strprintf("bad-version(0x%08x)", block.nVersion),
4125
39.3k
                                 strprintf("rejected nVersion=0x%08x block", block.nVersion));
4126
39.3k
    }
4127
4128
844k
    return true;
4129
884k
}
4130
4131
/** NOTE: This function is not currently invoked by ConnectBlock(), so we
4132
 *  should consider upgrade issues if we change which consensus rules are
4133
 *  enforced in this function (eg by adding a new consensus rule). See comment
4134
 *  in ConnectBlock().
4135
 *  Note that -reindex-chainstate skips the validation that happens here!
4136
 */
4137
static bool ContextualCheckBlock(const CBlock& block, BlockValidationState& state, const ChainstateManager& chainman, const CBlockIndex* pindexPrev)
4138
732k
{
4139
732k
    const int nHeight = pindexPrev == nullptr ? 0 : pindexPrev->nHeight + 1;
  Branch (4139:25): [True: 0, False: 732k]
4140
4141
    // Enforce BIP113 (Median Time Past).
4142
732k
    bool enforce_locktime_median_time_past{false};
4143
732k
    if (DeploymentActiveAfter(pindexPrev, chainman, Consensus::DEPLOYMENT_CSV)) {
  Branch (4143:9): [True: 732k, False: 0]
4144
732k
        assert(pindexPrev != nullptr);
  Branch (4144:9): [True: 732k, False: 0]
4145
732k
        enforce_locktime_median_time_past = true;
4146
732k
    }
4147
4148
732k
    const int64_t nLockTimeCutoff{enforce_locktime_median_time_past ?
  Branch (4148:35): [True: 732k, False: 0]
4149
732k
                                      pindexPrev->GetMedianTimePast() :
4150
732k
                                      block.GetBlockTime()};
4151
4152
    // Check that all transactions are finalized
4153
798k
    for (const auto& tx : block.vtx) {
  Branch (4153:25): [True: 798k, False: 732k]
4154
798k
        if (!IsFinalTx(*tx, nHeight, nLockTimeCutoff)) {
  Branch (4154:13): [True: 9, False: 798k]
4155
9
            return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-txns-nonfinal", "non-final transaction");
4156
9
        }
4157
798k
    }
4158
4159
    // Enforce rule that the coinbase starts with serialized block height
4160
732k
    if (DeploymentActiveAfter(pindexPrev, chainman, Consensus::DEPLOYMENT_HEIGHTINCB))
  Branch (4160:9): [True: 728k, False: 4.12k]
4161
728k
    {
4162
728k
        CScript expect = CScript() << nHeight;
4163
728k
        if (block.vtx[0]->vin[0].scriptSig.size() < expect.size() ||
  Branch (4163:13): [True: 0, False: 728k]
4164
728k
            !std::equal(expect.begin(), expect.end(), block.vtx[0]->vin[0].scriptSig.begin())) {
  Branch (4164:13): [True: 0, False: 728k]
4165
0
            return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-cb-height", "block height mismatch in coinbase");
4166
0
        }
4167
728k
    }
4168
4169
    // Validation for witness commitments.
4170
    // * We compute the witness hash (which is the hash including witnesses) of all the block's transactions, except the
4171
    //   coinbase (where 0x0000....0000 is used instead).
4172
    // * The coinbase scriptWitness is a stack of a single 32-byte vector, containing a witness reserved value (unconstrained).
4173
    // * We build a merkle tree with all those witness hashes as leaves (similar to the hashMerkleRoot in the block header).
4174
    // * There must be at least one output whose scriptPubKey is a single 36-byte push, the first 4 bytes of which are
4175
    //   {0xaa, 0x21, 0xa9, 0xed}, and the following 32 bytes are SHA256^2(witness root, witness reserved value). In case there are
4176
    //   multiple, the last one is used.
4177
732k
    if (!CheckWitnessMalleation(block, DeploymentActiveAfter(pindexPrev, chainman, Consensus::DEPLOYMENT_SEGWIT), state)) {
  Branch (4177:9): [True: 0, False: 732k]
4178
0
        return false;
4179
0
    }
4180
4181
    // After the coinbase witness reserved value and commitment are verified,
4182
    // we can check if the block weight passes (before we've checked the
4183
    // coinbase witness, it would be possible for the weight to be too
4184
    // large by filling up the coinbase witness, which doesn't change
4185
    // the block hash, so we couldn't mark the block as permanently
4186
    // failed).
4187
732k
    if (GetBlockWeight(block) > MAX_BLOCK_WEIGHT) {
  Branch (4187:9): [True: 0, False: 732k]
4188
0
        return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-blk-weight", strprintf("%s : weight limit failed", __func__));
4189
0
    }
4190
4191
732k
    return true;
4192
732k
}
4193
4194
bool ChainstateManager::AcceptBlockHeader(const CBlockHeader& block, BlockValidationState& state, CBlockIndex** ppindex, bool min_pow_checked)
4195
664k
{
4196
664k
    AssertLockHeld(cs_main);
4197
4198
    // Check for duplicate
4199
664k
    uint256 hash = block.GetHash();
4200
664k
    BlockMap::iterator miSelf{m_blockman.m_block_index.find(hash)};
4201
664k
    if (hash != GetConsensus().hashGenesisBlock) {
  Branch (4201:9): [True: 664k, False: 0]
4202
664k
        if (miSelf != m_blockman.m_block_index.end()) {
  Branch (4202:13): [True: 94.7k, False: 570k]
4203
            // Block header is already known.
4204
94.7k
            CBlockIndex* pindex = &(miSelf->second);
4205
94.7k
            if (ppindex)
  Branch (4205:17): [True: 94.7k, False: 0]
4206
94.7k
                *ppindex = pindex;
4207
94.7k
            if (pindex->nStatus & BLOCK_FAILED_VALID) {
  Branch (4207:17): [True: 21.9k, False: 72.8k]
4208
21.9k
                LogDebug(BCLog::VALIDATION, "%s: block %s is marked invalid\n", __func__, hash.ToString());
4209
21.9k
                return state.Invalid(BlockValidationResult::BLOCK_CACHED_INVALID, "duplicate-invalid",
4210
21.9k
                                     strprintf("block %s was previously marked invalid", hash.ToString()));
4211
21.9k
            }
4212
72.8k
            return true;
4213
94.7k
        }
4214
4215
570k
        if (!CheckBlockHeader(block, state, GetConsensus())) {
  Branch (4215:13): [True: 14.4k, False: 555k]
4216
14.4k
            LogDebug(BCLog::VALIDATION, "%s: Consensus::CheckBlockHeader: %s, %s\n", __func__, hash.ToString(), state.ToString());
4217
14.4k
            return false;
4218
14.4k
        }
4219
4220
        // Get prev block index
4221
555k
        CBlockIndex* pindexPrev = nullptr;
4222
555k
        BlockMap::iterator mi{m_blockman.m_block_index.find(block.hashPrevBlock)};
4223
555k
        if (mi == m_blockman.m_block_index.end()) {
  Branch (4223:13): [True: 5.44k, False: 550k]
4224
5.44k
            LogDebug(BCLog::VALIDATION, "header %s has prev block not found: %s\n", hash.ToString(), block.hashPrevBlock.ToString());
4225
5.44k
            return state.Invalid(BlockValidationResult::BLOCK_MISSING_PREV, "prev-blk-not-found");
4226
5.44k
        }
4227
550k
        pindexPrev = &((*mi).second);
4228
550k
        if (pindexPrev->nStatus & BLOCK_FAILED_VALID) {
  Branch (4228:13): [True: 1.15k, False: 549k]
4229
1.15k
            LogDebug(BCLog::VALIDATION, "header %s has prev block invalid: %s\n", hash.ToString(), block.hashPrevBlock.ToString());
4230
1.15k
            return state.Invalid(BlockValidationResult::BLOCK_INVALID_PREV, "bad-prevblk");
4231
1.15k
        }
4232
549k
        if (!ContextualCheckBlockHeader(block, state, *this, pindexPrev)) {
  Branch (4232:13): [True: 86.1k, False: 462k]
4233
86.1k
            LogDebug(BCLog::VALIDATION, "%s: Consensus::ContextualCheckBlockHeader: %s, %s\n", __func__, hash.ToString(), state.ToString());
4234
86.1k
            return false;
4235
86.1k
        }
4236
549k
    }
4237
462k
    if (!min_pow_checked) {
  Branch (4237:9): [True: 1.06k, False: 461k]
4238
1.06k
        LogDebug(BCLog::VALIDATION, "%s: not adding new block header %s, missing anti-dos proof-of-work validation\n", __func__, hash.ToString());
4239
1.06k
        return state.Invalid(BlockValidationResult::BLOCK_HEADER_LOW_WORK, "too-little-chainwork");
4240
1.06k
    }
4241
461k
    CBlockIndex* pindex{m_blockman.AddToBlockIndex(block, m_best_header)};
4242
4243
461k
    if (ppindex)
  Branch (4243:9): [True: 461k, False: 0]
4244
461k
        *ppindex = pindex;
4245
4246
461k
    return true;
4247
462k
}
4248
4249
// Exposed wrapper for AcceptBlockHeader
4250
bool ChainstateManager::ProcessNewBlockHeaders(std::span<const CBlockHeader> headers, bool min_pow_checked, BlockValidationState& state, const CBlockIndex** ppindex)
4251
237k
{
4252
237k
    AssertLockNotHeld(cs_main);
4253
237k
    {
4254
237k
        LOCK(cs_main);
4255
237k
        for (const CBlockHeader& header : headers) {
  Branch (4255:41): [True: 237k, False: 173k]
4256
237k
            CBlockIndex *pindex = nullptr; // Use a temp pindex instead of ppindex to avoid a const_cast
4257
237k
            bool accepted{AcceptBlockHeader(header, state, &pindex, min_pow_checked)};
4258
237k
            CheckBlockIndex();
4259
4260
237k
            if (!accepted) {
  Branch (4260:17): [True: 63.7k, False: 173k]
4261
63.7k
                return false;
4262
63.7k
            }
4263
173k
            if (ppindex) {
  Branch (4263:17): [True: 66.1k, False: 107k]
4264
66.1k
                *ppindex = pindex;
4265
66.1k
            }
4266
173k
        }
4267
237k
    }
4268
173k
    if (NotifyHeaderTip()) {
  Branch (4268:9): [True: 108k, False: 64.4k]
4269
108k
        if (IsInitialBlockDownload() && ppindex && *ppindex) {
  Branch (4269:13): [True: 90.9k, False: 17.8k]
  Branch (4269:41): [True: 1.15k, False: 89.8k]
  Branch (4269:52): [True: 1.15k, False: 0]
4270
1.15k
            const CBlockIndex& last_accepted{**ppindex};
4271
1.15k
            int64_t blocks_left{(NodeClock::now() - last_accepted.Time()) / GetConsensus().PowTargetSpacing()};
4272
1.15k
            blocks_left = std::max<int64_t>(0, blocks_left);
4273
1.15k
            const double progress{100.0 * last_accepted.nHeight / (last_accepted.nHeight + blocks_left)};
4274
1.15k
            LogInfo("Synchronizing blockheaders, height: %d (~%.2f%%)\n", last_accepted.nHeight, progress);
4275
1.15k
        }
4276
108k
    }
4277
173k
    return true;
4278
237k
}
4279
4280
void ChainstateManager::ReportHeadersPresync(int64_t height, int64_t timestamp)
4281
4.33k
{
4282
4.33k
    AssertLockNotHeld(GetMutex());
4283
4.33k
    {
4284
4.33k
        LOCK(GetMutex());
4285
        // Don't report headers presync progress if we already have a post-minchainwork header chain.
4286
        // This means we lose reporting for potentially legitimate, but unlikely, deep reorgs, but
4287
        // prevent attackers that spam low-work headers from filling our logs.
4288
4.33k
        if (m_best_header->nChainWork >= UintToArith256(GetConsensus().nMinimumChainWork)) return;
  Branch (4288:13): [True: 0, False: 4.33k]
4289
        // Rate limit headers presync updates to 4 per second, as these are not subject to DoS
4290
        // protection.
4291
4.33k
        auto now = MockableSteadyClock::now();
4292
4.33k
        if (now < m_last_presync_update + std::chrono::milliseconds{250}) return;
  Branch (4292:13): [True: 4.33k, False: 0]
4293
0
        m_last_presync_update = now;
4294
0
    }
4295
0
    bool initial_download = IsInitialBlockDownload();
4296
0
    GetNotifications().headerTip(GetSynchronizationState(initial_download, m_blockman.m_blockfiles_indexed), height, timestamp, /*presync=*/true);
4297
0
    if (initial_download) {
  Branch (4297:9): [True: 0, False: 0]
4298
0
        int64_t blocks_left{(NodeClock::now() - NodeSeconds{std::chrono::seconds{timestamp}}) / GetConsensus().PowTargetSpacing()};
4299
0
        blocks_left = std::max<int64_t>(0, blocks_left);
4300
0
        const double progress{100.0 * height / (height + blocks_left)};
4301
0
        LogInfo("Pre-synchronizing blockheaders, height: %d (~%.2f%%)\n", height, progress);
4302
0
    }
4303
0
}
4304
4305
/** Store block on disk. If dbp is non-nullptr, the file is known to already reside on disk */
4306
bool ChainstateManager::AcceptBlock(const std::shared_ptr<const CBlock>& pblock, BlockValidationState& state, CBlockIndex** ppindex, bool fRequested, const FlatFilePos* dbp, bool* fNewBlock, bool min_pow_checked)
4307
427k
{
4308
427k
    const CBlock& block = *pblock;
4309
4310
427k
    if (fNewBlock) *fNewBlock = false;
  Branch (4310:9): [True: 379k, False: 48.3k]
4311
427k
    AssertLockHeld(cs_main);
4312
4313
427k
    CBlockIndex *pindexDummy = nullptr;
4314
427k
    CBlockIndex *&pindex = ppindex ? *ppindex : pindexDummy;
  Branch (4314:28): [True: 379k, False: 48.3k]
4315
4316
427k
    bool accepted_header{AcceptBlockHeader(block, state, &pindex, min_pow_checked)};
4317
427k
    CheckBlockIndex();
4318
4319
427k
    if (!accepted_header)
  Branch (4319:9): [True: 66.4k, False: 361k]
4320
66.4k
        return false;
4321
4322
    // Check all requested blocks that we do not already have for validity and
4323
    // save them to disk. Skip processing of unrequested blocks as an anti-DoS
4324
    // measure, unless the blocks have more work than the active chain tip, and
4325
    // aren't too far ahead of it, so are likely to be attached soon.
4326
361k
    bool fAlreadyHave = pindex->nStatus & BLOCK_HAVE_DATA;
4327
361k
    bool fHasMoreOrSameWork = (ActiveTip() ? pindex->nChainWork >= ActiveTip()->nChainWork : true);
  Branch (4327:32): [True: 361k, False: 0]
4328
    // Blocks that are too out-of-order needlessly limit the effectiveness of
4329
    // pruning, because pruning will not delete block files that contain any
4330
    // blocks which are too close in height to the tip.  Apply this test
4331
    // regardless of whether pruning is enabled; it should generally be safe to
4332
    // not process unrequested blocks.
4333
361k
    bool fTooFarAhead{pindex->nHeight > ActiveHeight() + int(MIN_BLOCKS_TO_KEEP)};
4334
4335
    // TODO: Decouple this function from the block download logic by removing fRequested
4336
    // This requires some new chain data structure to efficiently look up if a
4337
    // block is in a chain leading to a candidate for best tip, despite not
4338
    // being such a candidate itself.
4339
    // Note that this would break the getblockfrompeer RPC
4340
4341
    // TODO: deal better with return value and error conditions for duplicate
4342
    // and unrequested blocks.
4343
361k
    if (fAlreadyHave) return true;
  Branch (4343:9): [True: 0, False: 361k]
4344
361k
    if (!fRequested) {  // If we didn't ask for it:
  Branch (4344:9): [True: 2, False: 361k]
4345
2
        if (pindex->nTx != 0) return true;    // This is a previously-processed block that was pruned
  Branch (4345:13): [True: 0, False: 2]
4346
2
        if (!fHasMoreOrSameWork) return true; // Don't process less-work chains
  Branch (4346:13): [True: 0, False: 2]
4347
2
        if (fTooFarAhead) return true;        // Block height is too high
  Branch (4347:13): [True: 0, False: 2]
4348
4349
        // Protect against DoS attacks from low-work chains.
4350
        // If our tip is behind, a peer could try to send us
4351
        // low-work blocks on a fake chain that we would never
4352
        // request; don't process these.
4353
2
        if (pindex->nChainWork < MinimumChainWork()) return true;
  Branch (4353:13): [True: 0, False: 2]
4354
2
    }
4355
4356
361k
    const CChainParams& params{GetParams()};
4357
4358
361k
    if (!CheckBlock(block, state, params.GetConsensus()) ||
  Branch (4358:9): [True: 10.4k, False: 350k]
4359
361k
        !ContextualCheckBlock(block, state, *this, pindex->pprev)) {
  Branch (4359:9): [True: 9, False: 350k]
4360
10.4k
        if (Assume(state.IsInvalid())) {
4361
10.4k
            ActiveChainstate().InvalidBlockFound(pindex, state);
4362
10.4k
        }
4363
10.4k
        LogError("%s: %s\n", __func__, state.ToString());
4364
10.4k
        return false;
4365
10.4k
    }
4366
4367
    // Header is valid/has work, merkle tree and segwit merkle tree are good...RELAY NOW
4368
    // (but if it does not build on our best tip, let the SendMessages loop relay it)
4369
350k
    if (!IsInitialBlockDownload() && ActiveTip() == pindex->pprev && m_options.signals) {
  Branch (4369:9): [True: 296k, False: 54.2k]
  Branch (4369:38): [True: 296k, False: 19]
  Branch (4369:70): [True: 296k, False: 0]
4370
296k
        m_options.signals->NewPoWValidBlock(pindex, pblock);
4371
296k
    }
4372
4373
    // Write block to history file
4374
350k
    if (fNewBlock) *fNewBlock = true;
  Branch (4374:9): [True: 350k, False: 0]
4375
350k
    try {
4376
350k
        FlatFilePos blockPos{};
4377
350k
        if (dbp) {
  Branch (4377:13): [True: 0, False: 350k]
4378
0
            blockPos = *dbp;
4379
0
            m_blockman.UpdateBlockInfo(block, pindex->nHeight, blockPos);
4380
350k
        } else {
4381
350k
            blockPos = m_blockman.WriteBlock(block, pindex->nHeight);
4382
350k
            if (blockPos.IsNull()) {
  Branch (4382:17): [True: 0, False: 350k]
4383
0
                state.Error(strprintf("%s: Failed to find position to write new block to disk", __func__));
4384
0
                return false;
4385
0
            }
4386
350k
        }
4387
350k
        ReceivedBlockTransactions(block, pindex, blockPos);
4388
350k
    } catch (const std::runtime_error& e) {
4389
0
        return FatalError(GetNotifications(), state, strprintf(_("System error while saving block to disk: %s"), e.what()));
4390
0
    }
4391
4392
    // TODO: FlushStateToDisk() handles flushing of both block and chainstate
4393
    // data, so we should move this to ChainstateManager so that we can be more
4394
    // intelligent about how we flush.
4395
    // For now, since FlushStateMode::NONE is used, all that can happen is that
4396
    // the block files may be pruned, so we can just call this on one
4397
    // chainstate (particularly if we haven't implemented pruning with
4398
    // background validation yet).
4399
    //
4400
    // Flush errors (e.g. low disk space during pruning) are ignored, so that
4401
    // callers can't mistreat a flush failure as a block validation failure.
4402
    // The fatal error notification inside FlushStateToDisk still fires,
4403
    // so the node will shut down on unrecoverable flush errors regardless.
4404
    // For state a dummy value is used, and the return value is ignored.
4405
350k
    BlockValidationState flush_state_ignore;
4406
350k
    (void)ActiveChainstate().FlushStateToDisk(flush_state_ignore, FlushStateMode::NONE);
4407
4408
350k
    CheckBlockIndex();
4409
4410
350k
    return true;
4411
350k
}
4412
4413
bool ChainstateManager::ProcessNewBlock(const std::shared_ptr<const CBlock>& block, bool force_processing, bool min_pow_checked, bool* new_block)
4414
386k
{
4415
386k
    AssertLockNotHeld(cs_main);
4416
4417
386k
    {
4418
386k
        CBlockIndex *pindex = nullptr;
4419
386k
        if (new_block) *new_block = false;
  Branch (4419:13): [True: 386k, False: 0]
4420
386k
        BlockValidationState state;
4421
4422
        // CheckBlock() does not support multi-threaded block validation because CBlock::fChecked can cause data race.
4423
        // Therefore, the following critical section must include the CheckBlock() call as well.
4424
386k
        LOCK(cs_main);
4425
4426
        // Skipping AcceptBlock() for CheckBlock() failures means that we will never mark a block as invalid if
4427
        // CheckBlock() fails.  This is protective against consensus failure if there are any unknown forms of block
4428
        // malleability that cause CheckBlock() to fail; see e.g. CVE-2012-2459 and
4429
        // https://lists.linuxfoundation.org/pipermail/bitcoin-dev/2019-February/016697.html.  Because CheckBlock() is
4430
        // not very expensive, the anti-DoS benefits of caching failure (of a definitely-invalid block) are not substantial.
4431
386k
        bool ret = CheckBlock(*block, state, GetConsensus());
4432
386k
        if (ret) {
  Branch (4432:13): [True: 379k, False: 6.90k]
4433
            // Store to disk
4434
379k
            ret = AcceptBlock(block, state, &pindex, force_processing, nullptr, new_block, min_pow_checked);
4435
379k
        }
4436
386k
        if (!ret) {
  Branch (4436:13): [True: 35.4k, False: 350k]
4437
35.4k
            if (m_options.signals) {
  Branch (4437:17): [True: 23.6k, False: 11.8k]
4438
23.6k
                m_options.signals->BlockChecked(block, state);
4439
23.6k
            }
4440
35.4k
            LogError("%s: AcceptBlock FAILED (%s)\n", __func__, state.ToString());
4441
35.4k
            return false;
4442
35.4k
        }
4443
386k
    }
4444
4445
350k
    NotifyHeaderTip();
4446
4447
350k
    BlockValidationState state; // Only used to report errors, not invalidity - ignore it
4448
350k
    if (!ActiveChainstate().ActivateBestChain(state, block)) {
  Branch (4448:9): [True: 0, False: 350k]
4449
0
        LogError("%s: ActivateBestChain failed (%s)\n", __func__, state.ToString());
4450
0
        return false;
4451
0
    }
4452
4453
350k
    Chainstate* bg_chain{WITH_LOCK(cs_main, return HistoricalChainstate())};
4454
350k
    BlockValidationState bg_state;
4455
350k
    if (bg_chain && !bg_chain->ActivateBestChain(bg_state, block)) {
  Branch (4455:9): [True: 0, False: 350k]
  Branch (4455:9): [True: 0, False: 350k]
  Branch (4455:21): [True: 0, False: 0]
4456
0
        LogError("%s: [background] ActivateBestChain failed (%s)\n", __func__, bg_state.ToString());
4457
0
        return false;
4458
0
     }
4459
4460
350k
    return true;
4461
350k
}
4462
4463
MempoolAcceptResult ChainstateManager::ProcessTransaction(const CTransactionRef& tx, bool test_accept)
4464
19.9k
{
4465
19.9k
    AssertLockHeld(cs_main);
4466
19.9k
    Chainstate& active_chainstate = ActiveChainstate();
4467
19.9k
    if (!active_chainstate.GetMempool()) {
  Branch (4467:9): [True: 0, False: 19.9k]
4468
0
        TxValidationState state;
4469
0
        state.Invalid(TxValidationResult::TX_NO_MEMPOOL, "no-mempool");
4470
0
        return MempoolAcceptResult::Failure(state);
4471
0
    }
4472
19.9k
    auto result = AcceptToMemoryPool(active_chainstate, tx, GetTime(), /*bypass_limits=*/ false, test_accept);
4473
19.9k
    active_chainstate.GetMempool()->check(active_chainstate.CoinsTip(), active_chainstate.m_chain.Height() + 1);
4474
19.9k
    return result;
4475
19.9k
}
4476
4477
4478
BlockValidationState TestBlockValidity(
4479
    Chainstate& chainstate,
4480
    const CBlock& block,
4481
    const bool check_pow,
4482
    const bool check_merkle_root)
4483
381k
{
4484
    // Lock must be held throughout this function for two reasons:
4485
    // 1. We don't want the tip to change during several of the validation steps
4486
    // 2. To prevent a CheckBlock() race condition for fChecked, see ProcessNewBlock()
4487
381k
    AssertLockHeld(chainstate.m_chainman.GetMutex());
4488
4489
381k
    BlockValidationState state;
4490
381k
    CBlockIndex* tip{Assert(chainstate.m_chain.Tip())};
4491
4492
381k
    if (block.hashPrevBlock != *Assert(tip->phashBlock)) {
  Branch (4492:9): [True: 0, False: 381k]
4493
0
        state.Invalid({}, "inconclusive-not-best-prevblk");
4494
0
        return state;
4495
0
    }
4496
4497
    // For signets CheckBlock() verifies the challenge iff fCheckPow is set.
4498
381k
    if (!CheckBlock(block, state, chainstate.m_chainman.GetConsensus(), /*fCheckPow=*/check_pow, /*fCheckMerkleRoot=*/check_merkle_root)) {
  Branch (4498:9): [True: 0, False: 381k]
4499
        // This should never happen, but belt-and-suspenders don't approve the
4500
        // block if it does.
4501
0
        if (state.IsValid()) NONFATAL_UNREACHABLE();
  Branch (4501:13): [True: 0, False: 0]
4502
0
        return state;
4503
0
    }
4504
4505
    /**
4506
     * At this point ProcessNewBlock would call AcceptBlock(), but we
4507
     * don't want to store the block or its header. Run individual checks
4508
     * instead:
4509
     * - skip AcceptBlockHeader() because:
4510
     *   - we don't want to update the block index
4511
     *   - we do not care about duplicates
4512
     *   - we already ran CheckBlockHeader() via CheckBlock()
4513
     *   - we already checked for prev-blk-not-found
4514
     *   - we know the tip is valid, so no need to check bad-prevblk
4515
     * - we already ran CheckBlock()
4516
     * - do run ContextualCheckBlockHeader()
4517
     * - do run ContextualCheckBlock()
4518
     */
4519
4520
381k
    if (!ContextualCheckBlockHeader(block, state, chainstate.m_chainman, tip)) {
  Branch (4520:9): [True: 0, False: 381k]
4521
0
        if (state.IsValid()) NONFATAL_UNREACHABLE();
  Branch (4521:13): [True: 0, False: 0]
4522
0
        return state;
4523
0
    }
4524
4525
381k
    if (!ContextualCheckBlock(block, state, chainstate.m_chainman, tip)) {
  Branch (4525:9): [True: 0, False: 381k]
4526
0
        if (state.IsValid()) NONFATAL_UNREACHABLE();
  Branch (4526:13): [True: 0, False: 0]
4527
0
        return state;
4528
0
    }
4529
4530
    // We don't want ConnectBlock to update the actual chainstate, so create
4531
    // a cache on top of it, along with a dummy block index.
4532
381k
    CBlockIndex index_dummy{block};
4533
381k
    uint256 block_hash(block.GetHash());
4534
381k
    index_dummy.pprev = tip;
4535
381k
    index_dummy.nHeight = tip->nHeight + 1;
4536
381k
    index_dummy.phashBlock = &block_hash;
4537
381k
    CCoinsViewCache view_dummy(&chainstate.CoinsTip());
4538
4539
    // Set fJustCheck to true in order to update, and not clear, validation caches.
4540
381k
    if(!chainstate.ConnectBlock(block, state, &index_dummy, view_dummy, /*fJustCheck=*/true)) {
  Branch (4540:8): [True: 0, False: 381k]
4541
0
        if (state.IsValid()) NONFATAL_UNREACHABLE();
  Branch (4541:13): [True: 0, False: 0]
4542
0
        return state;
4543
0
    }
4544
4545
    // Ensure no check returned successfully while also setting an invalid state.
4546
381k
    if (!state.IsValid()) NONFATAL_UNREACHABLE();
  Branch (4546:9): [True: 0, False: 381k]
4547
4548
381k
    return state;
4549
381k
}
4550
4551
/* This function is called from the RPC code for pruneblockchain */
4552
void PruneBlockFilesManual(Chainstate& active_chainstate, int nManualPruneHeight)
4553
0
{
4554
0
    BlockValidationState state;
4555
0
    if (!active_chainstate.FlushStateToDisk(
  Branch (4555:9): [True: 0, False: 0]
4556
0
            state, FlushStateMode::NONE, nManualPruneHeight)) {
4557
0
        LogWarning("Failed to flush state after manual prune (%s)", state.ToString());
4558
0
    }
4559
0
}
4560
4561
bool Chainstate::LoadChainTip()
4562
0
{
4563
0
    AssertLockHeld(cs_main);
4564
0
    const CCoinsViewCache& coins_cache = CoinsTip();
4565
0
    assert(!coins_cache.GetBestBlock().IsNull()); // Never called when the coins view is empty
  Branch (4565:5): [True: 0, False: 0]
4566
0
    CBlockIndex* tip = m_chain.Tip();
4567
4568
0
    if (tip && tip->GetBlockHash() == coins_cache.GetBestBlock()) {
  Branch (4568:9): [True: 0, False: 0]
  Branch (4568:9): [True: 0, False: 0]
  Branch (4568:16): [True: 0, False: 0]
4569
0
        return true;
4570
0
    }
4571
4572
    // Load pointer to end of best chain
4573
0
    CBlockIndex* pindex = m_blockman.LookupBlockIndex(coins_cache.GetBestBlock());
4574
0
    if (!pindex) {
  Branch (4574:9): [True: 0, False: 0]
4575
0
        return false;
4576
0
    }
4577
0
    m_chain.SetTip(*pindex);
4578
0
    m_chainman.UpdateIBDStatus();
4579
0
    m_last_flushed_block = pindex;
4580
0
    tip = m_chain.Tip();
4581
4582
    // nSequenceId is one of the keys used to sort setBlockIndexCandidates. Ensure all
4583
    // candidate sets are empty to avoid UB, as nSequenceId is about to be modified.
4584
0
    for (const auto& cs : m_chainman.m_chainstates) {
  Branch (4584:25): [True: 0, False: 0]
4585
0
        assert(cs->setBlockIndexCandidates.empty());
  Branch (4585:9): [True: 0, False: 0]
4586
0
    }
4587
4588
    // Make sure our chain tip before shutting down scores better than any other candidate
4589
    // to maintain a consistent best tip over reboots in case of a tie.
4590
0
    auto target = tip;
4591
0
    while (target) {
  Branch (4591:12): [True: 0, False: 0]
4592
0
        target->nSequenceId = SEQ_ID_BEST_CHAIN_FROM_DISK;
4593
0
        target = target->pprev;
4594
0
    }
4595
4596
0
    LogInfo("Loaded best chain: hashBestChain=%s height=%d date=%s progress=%f",
4597
0
              tip->GetBlockHash().ToString(),
4598
0
              m_chain.Height(),
4599
0
              FormatISO8601DateTime(tip->GetBlockTime()),
4600
0
              m_chainman.GuessVerificationProgress(tip));
4601
4602
    // Ensure KernelNotifications m_tip_block is set even if no new block arrives.
4603
0
    if (!this->GetRole().historical) {
  Branch (4603:9): [True: 0, False: 0]
4604
        // Ignoring return value for now.
4605
0
        (void)m_chainman.GetNotifications().blockTip(
4606
0
            /*state=*/GetSynchronizationState(/*init=*/true, m_chainman.m_blockman.m_blockfiles_indexed),
4607
0
            /*index=*/*pindex,
4608
0
            /*verification_progress=*/m_chainman.GuessVerificationProgress(tip));
4609
0
    }
4610
4611
0
    CheckForkWarningConditions();
4612
4613
0
    return true;
4614
0
}
4615
4616
CVerifyDB::CVerifyDB(Notifications& notifications)
4617
3
    : m_notifications{notifications}
4618
3
{
4619
3
    m_notifications.progress(_("Verifying blocks…"), 0, false);
4620
3
}
4621
4622
CVerifyDB::~CVerifyDB()
4623
3
{
4624
3
    m_notifications.progress(bilingual_str{}, 100, false);
4625
3
}
4626
4627
VerifyDBResult CVerifyDB::VerifyDB(
4628
    Chainstate& chainstate,
4629
    const Consensus::Params& consensus_params,
4630
    CCoinsView& coinsview,
4631
    int nCheckLevel, int nCheckDepth)
4632
3
{
4633
3
    AssertLockHeld(cs_main);
4634
4635
3
    if (chainstate.m_chain.Tip() == nullptr || chainstate.m_chain.Tip()->pprev == nullptr) {
  Branch (4635:9): [True: 0, False: 3]
  Branch (4635:48): [True: 3, False: 0]
4636
3
        return VerifyDBResult::SUCCESS;
4637
3
    }
4638
4639
    // Verify blocks in the best chain
4640
0
    if (nCheckDepth <= 0 || nCheckDepth > chainstate.m_chain.Height()) {
  Branch (4640:9): [True: 0, False: 0]
  Branch (4640:29): [True: 0, False: 0]
4641
0
        nCheckDepth = chainstate.m_chain.Height();
4642
0
    }
4643
0
    nCheckLevel = std::max(0, std::min(4, nCheckLevel));
4644
0
    LogInfo("Verifying last %i blocks at level %i", nCheckDepth, nCheckLevel);
4645
0
    CCoinsViewCache coins(&coinsview);
4646
0
    CBlockIndex* pindex;
4647
0
    CBlockIndex* pindexFailure = nullptr;
4648
0
    int nGoodTransactions = 0;
4649
0
    BlockValidationState state;
4650
0
    int reportDone = 0;
4651
0
    bool skipped_no_block_data{false};
4652
0
    bool skipped_l3_checks{false};
4653
0
    LogInfo("Verification progress: 0%%");
4654
4655
0
    const bool is_snapshot_cs{chainstate.m_from_snapshot_blockhash};
4656
4657
0
    for (pindex = chainstate.m_chain.Tip(); pindex && pindex->pprev; pindex = pindex->pprev) {
  Branch (4657:45): [True: 0, False: 0]
  Branch (4657:55): [True: 0, False: 0]
4658
0
        const int percentageDone = std::max(1, std::min(99, (int)(((double)(chainstate.m_chain.Height() - pindex->nHeight)) / (double)nCheckDepth * (nCheckLevel >= 4 ? 50 : 100))));
  Branch (4658:150): [True: 0, False: 0]
4659
0
        if (reportDone < percentageDone / 10) {
  Branch (4659:13): [True: 0, False: 0]
4660
            // report every 10% step
4661
0
            LogInfo("Verification progress: %d%%", percentageDone);
4662
0
            reportDone = percentageDone / 10;
4663
0
        }
4664
0
        m_notifications.progress(_("Verifying blocks…"), percentageDone, false);
4665
0
        if (pindex->nHeight <= chainstate.m_chain.Height() - nCheckDepth) {
  Branch (4665:13): [True: 0, False: 0]
4666
0
            break;
4667
0
        }
4668
0
        if ((chainstate.m_blockman.IsPruneMode() || is_snapshot_cs) && !(pindex->nStatus & BLOCK_HAVE_DATA)) {
  Branch (4668:14): [True: 0, False: 0]
  Branch (4668:53): [True: 0, False: 0]
  Branch (4668:72): [True: 0, False: 0]
4669
            // If pruning or running under an assumeutxo snapshot, only go
4670
            // back as far as we have data.
4671
0
            LogInfo("Block verification stopping at height %d (no data). This could be due to pruning or use of an assumeutxo snapshot.", pindex->nHeight);
4672
0
            skipped_no_block_data = true;
4673
0
            break;
4674
0
        }
4675
0
        CBlock block;
4676
        // check level 0: read from disk
4677
0
        if (!chainstate.m_blockman.ReadBlock(block, *pindex)) {
  Branch (4677:13): [True: 0, False: 0]
4678
0
            LogError("Verification error: ReadBlock failed at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
4679
0
            return VerifyDBResult::CORRUPTED_BLOCK_DB;
4680
0
        }
4681
        // check level 1: verify block validity
4682
0
        if (nCheckLevel >= 1 && !CheckBlock(block, state, consensus_params)) {
  Branch (4682:13): [True: 0, False: 0]
  Branch (4682:33): [True: 0, False: 0]
4683
0
            LogError("Verification error: found bad block at %d, hash=%s (%s)",
4684
0
                      pindex->nHeight, pindex->GetBlockHash().ToString(), state.ToString());
4685
0
            return VerifyDBResult::CORRUPTED_BLOCK_DB;
4686
0
        }
4687
        // check level 2: verify undo validity
4688
0
        if (nCheckLevel >= 2 && pindex) {
  Branch (4688:13): [True: 0, False: 0]
  Branch (4688:33): [True: 0, False: 0]
4689
0
            CBlockUndo undo;
4690
0
            if (!pindex->GetUndoPos().IsNull()) {
  Branch (4690:17): [True: 0, False: 0]
4691
0
                if (!chainstate.m_blockman.ReadBlockUndo(undo, *pindex)) {
  Branch (4691:21): [True: 0, False: 0]
4692
0
                    LogError("Verification error: found bad undo data at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
4693
0
                    return VerifyDBResult::CORRUPTED_BLOCK_DB;
4694
0
                }
4695
0
            }
4696
0
        }
4697
        // check level 3: check for inconsistencies during memory-only disconnect of tip blocks
4698
0
        size_t curr_coins_usage = coins.DynamicMemoryUsage() + chainstate.CoinsTip().DynamicMemoryUsage();
4699
4700
0
        if (nCheckLevel >= 3) {
  Branch (4700:13): [True: 0, False: 0]
4701
0
            if (curr_coins_usage <= chainstate.m_coinstip_cache_size_bytes) {
  Branch (4701:17): [True: 0, False: 0]
4702
0
                assert(coins.GetBestBlock() == pindex->GetBlockHash());
  Branch (4702:17): [True: 0, False: 0]
4703
0
                DisconnectResult res = chainstate.DisconnectBlock(block, pindex, coins);
4704
0
                if (res == DISCONNECT_FAILED) {
  Branch (4704:21): [True: 0, False: 0]
4705
0
                    LogError("Verification error: irrecoverable inconsistency in block data at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
4706
0
                    return VerifyDBResult::CORRUPTED_BLOCK_DB;
4707
0
                }
4708
0
                if (res == DISCONNECT_UNCLEAN) {
  Branch (4708:21): [True: 0, False: 0]
4709
0
                    nGoodTransactions = 0;
4710
0
                    pindexFailure = pindex;
4711
0
                } else {
4712
0
                    nGoodTransactions += block.vtx.size();
4713
0
                }
4714
0
            } else {
4715
0
                skipped_l3_checks = true;
4716
0
            }
4717
0
        }
4718
0
        if (chainstate.m_chainman.m_interrupt) return VerifyDBResult::INTERRUPTED;
  Branch (4718:13): [True: 0, False: 0]
4719
0
    }
4720
0
    if (pindexFailure) {
  Branch (4720:9): [True: 0, False: 0]
4721
0
        LogError("Verification error: coin database inconsistencies found (last %i blocks, %i good transactions before that)", chainstate.m_chain.Height() - pindexFailure->nHeight + 1, nGoodTransactions);
4722
0
        return VerifyDBResult::CORRUPTED_BLOCK_DB;
4723
0
    }
4724
0
    if (skipped_l3_checks) {
  Branch (4724:9): [True: 0, False: 0]
4725
0
        LogWarning("Skipped verification of level >=3 (insufficient database cache size). Consider increasing -dbcache.");
4726
0
    }
4727
4728
    // store block count as we move pindex at check level >= 4
4729
0
    int block_count = chainstate.m_chain.Height() - pindex->nHeight;
4730
4731
    // check level 4: try reconnecting blocks
4732
0
    if (nCheckLevel >= 4 && !skipped_l3_checks) {
  Branch (4732:9): [True: 0, False: 0]
  Branch (4732:29): [True: 0, False: 0]
4733
0
        while (pindex != chainstate.m_chain.Tip()) {
  Branch (4733:16): [True: 0, False: 0]
4734
0
            const int percentageDone = std::max(1, std::min(99, 100 - (int)(((double)(chainstate.m_chain.Height() - pindex->nHeight)) / (double)nCheckDepth * 50)));
4735
0
            if (reportDone < percentageDone / 10) {
  Branch (4735:17): [True: 0, False: 0]
4736
                // report every 10% step
4737
0
                LogInfo("Verification progress: %d%%", percentageDone);
4738
0
                reportDone = percentageDone / 10;
4739
0
            }
4740
0
            m_notifications.progress(_("Verifying blocks…"), percentageDone, false);
4741
0
            pindex = chainstate.m_chain.Next(*pindex);
4742
0
            CBlock block;
4743
0
            if (!chainstate.m_blockman.ReadBlock(block, *pindex)) {
  Branch (4743:17): [True: 0, False: 0]
4744
0
                LogError("Verification error: ReadBlock failed at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
4745
0
                return VerifyDBResult::CORRUPTED_BLOCK_DB;
4746
0
            }
4747
0
            if (!chainstate.ConnectBlock(block, state, pindex, coins)) {
  Branch (4747:17): [True: 0, False: 0]
4748
0
                LogError("Verification error: found unconnectable block at %d, hash=%s (%s)", pindex->nHeight, pindex->GetBlockHash().ToString(), state.ToString());
4749
0
                return VerifyDBResult::CORRUPTED_BLOCK_DB;
4750
0
            }
4751
0
            if (chainstate.m_chainman.m_interrupt) return VerifyDBResult::INTERRUPTED;
  Branch (4751:17): [True: 0, False: 0]
4752
0
        }
4753
0
    }
4754
4755
0
    LogInfo("Verification: checked last %i blocks at level %i", block_count, nCheckLevel);
4756
0
    if (nCheckLevel >= 3 && !skipped_l3_checks) {
  Branch (4756:9): [True: 0, False: 0]
  Branch (4756:29): [True: 0, False: 0]
4757
0
        LogInfo("Verification: no coin database inconsistencies (%i transactions)", nGoodTransactions);
4758
0
    }
4759
4760
0
    if (skipped_l3_checks) {
  Branch (4760:9): [True: 0, False: 0]
4761
0
        return VerifyDBResult::SKIPPED_L3_CHECKS;
4762
0
    }
4763
0
    if (skipped_no_block_data) {
  Branch (4763:9): [True: 0, False: 0]
4764
0
        return VerifyDBResult::SKIPPED_MISSING_BLOCKS;
4765
0
    }
4766
0
    return VerifyDBResult::SUCCESS;
4767
0
}
4768
4769
/** Apply the effects of a block on the utxo cache, ignoring that it may already have been applied. */
4770
bool Chainstate::RollforwardBlock(const CBlockIndex* pindex, CCoinsViewCache& inputs)
4771
0
{
4772
0
    AssertLockHeld(cs_main);
4773
    // TODO: merge with ConnectBlock
4774
0
    CBlock block;
4775
0
    if (!m_blockman.ReadBlock(block, *pindex)) {
  Branch (4775:9): [True: 0, False: 0]
4776
0
        LogError("ReplayBlock(): ReadBlock failed at %d, hash=%s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
4777
0
        return false;
4778
0
    }
4779
4780
0
    for (const CTransactionRef& tx : block.vtx) {
  Branch (4780:36): [True: 0, False: 0]
4781
0
        if (!tx->IsCoinBase()) {
  Branch (4781:13): [True: 0, False: 0]
4782
0
            for (const CTxIn &txin : tx->vin) {
  Branch (4782:36): [True: 0, False: 0]
4783
0
                inputs.SpendCoin(txin.prevout);
4784
0
            }
4785
0
        }
4786
        // Pass check = true as every addition may be an overwrite.
4787
0
        AddCoins(inputs, *tx, pindex->nHeight, true);
4788
0
    }
4789
0
    return true;
4790
0
}
4791
4792
bool Chainstate::ReplayBlocks()
4793
3.14k
{
4794
3.14k
    LOCK(cs_main);
4795
4796
3.14k
    CCoinsView& db = this->CoinsDB();
4797
3.14k
    CCoinsViewCache cache(&db);
4798
4799
3.14k
    std::vector<uint256> hashHeads = db.GetHeadBlocks();
4800
3.14k
    if (hashHeads.empty()) return true; // We're already in a consistent state.
  Branch (4800:9): [True: 3.14k, False: 0]
4801
0
    if (hashHeads.size() != 2) {
  Branch (4801:9): [True: 0, False: 0]
4802
0
        LogError("ReplayBlocks(): unknown inconsistent state\n");
4803
0
        return false;
4804
0
    }
4805
4806
0
    m_chainman.GetNotifications().progress(_("Replaying blocks…"), 0, false);
4807
0
    LogInfo("Replaying blocks");
4808
4809
0
    const CBlockIndex* pindexOld = nullptr;  // Old tip during the interrupted flush.
4810
0
    const CBlockIndex* pindexNew;            // New tip during the interrupted flush.
4811
0
    const CBlockIndex* pindexFork = nullptr; // Latest block common to both the old and the new tip.
4812
4813
0
    if (!m_blockman.m_block_index.contains(hashHeads[0])) {
  Branch (4813:9): [True: 0, False: 0]
4814
0
        LogError("ReplayBlocks(): reorganization to unknown block requested\n");
4815
0
        return false;
4816
0
    }
4817
0
    pindexNew = &(m_blockman.m_block_index[hashHeads[0]]);
4818
4819
0
    if (!hashHeads[1].IsNull()) { // The old tip is allowed to be 0, indicating it's the first flush.
  Branch (4819:9): [True: 0, False: 0]
4820
0
        if (!m_blockman.m_block_index.contains(hashHeads[1])) {
  Branch (4820:13): [True: 0, False: 0]
4821
0
            LogError("ReplayBlocks(): reorganization from unknown block requested\n");
4822
0
            return false;
4823
0
        }
4824
0
        pindexOld = &(m_blockman.m_block_index[hashHeads[1]]);
4825
0
        pindexFork = LastCommonAncestor(pindexOld, pindexNew);
4826
0
        assert(pindexFork != nullptr);
  Branch (4826:9): [True: 0, False: 0]
4827
0
    }
4828
4829
    // Rollback along the old branch.
4830
0
    const int nForkHeight{pindexFork ? pindexFork->nHeight : 0};
  Branch (4830:27): [True: 0, False: 0]
4831
0
    if (pindexOld != pindexFork) {
  Branch (4831:9): [True: 0, False: 0]
4832
0
        LogInfo("Rolling back from %s (%i to %i)", pindexOld->GetBlockHash().ToString(), pindexOld->nHeight, nForkHeight);
4833
0
        while (pindexOld != pindexFork) {
  Branch (4833:16): [True: 0, False: 0]
4834
0
            if (pindexOld->nHeight > 0) { // Never disconnect the genesis block.
  Branch (4834:17): [True: 0, False: 0]
4835
0
                CBlock block;
4836
0
                if (!m_blockman.ReadBlock(block, *pindexOld)) {
  Branch (4836:21): [True: 0, False: 0]
4837
0
                    LogError("RollbackBlock(): ReadBlock() failed at %d, hash=%s\n", pindexOld->nHeight, pindexOld->GetBlockHash().ToString());
4838
0
                    return false;
4839
0
                }
4840
0
                if (pindexOld->nHeight % 10'000 == 0) {
  Branch (4840:21): [True: 0, False: 0]
4841
0
                    LogInfo("Rolling back %s (%i)", pindexOld->GetBlockHash().ToString(), pindexOld->nHeight);
4842
0
                }
4843
0
                DisconnectResult res = DisconnectBlock(block, pindexOld, cache);
4844
0
                if (res == DISCONNECT_FAILED) {
  Branch (4844:21): [True: 0, False: 0]
4845
0
                    LogError("RollbackBlock(): DisconnectBlock failed at %d, hash=%s\n", pindexOld->nHeight, pindexOld->GetBlockHash().ToString());
4846
0
                    return false;
4847
0
                }
4848
                // If DISCONNECT_UNCLEAN is returned, it means a non-existing UTXO was deleted, or an existing UTXO was
4849
                // overwritten. It corresponds to cases where the block-to-be-disconnect never had all its operations
4850
                // applied to the UTXO set. However, as both writing a UTXO and deleting a UTXO are idempotent operations,
4851
                // the result is still a version of the UTXO set with the effects of that block undone.
4852
0
            }
4853
0
            pindexOld = pindexOld->pprev;
4854
0
        }
4855
0
        LogInfo("Rolled back to %s", pindexFork->GetBlockHash().ToString());
4856
0
    }
4857
4858
    // Roll forward from the forking point to the new tip.
4859
0
    if (nForkHeight < pindexNew->nHeight) {
  Branch (4859:9): [True: 0, False: 0]
4860
0
        LogInfo("Rolling forward to %s (%i to %i)", pindexNew->GetBlockHash().ToString(), nForkHeight, pindexNew->nHeight);
4861
0
        for (int nHeight = nForkHeight + 1; nHeight <= pindexNew->nHeight; ++nHeight) {
  Branch (4861:45): [True: 0, False: 0]
4862
0
            const CBlockIndex& pindex{*Assert(pindexNew->GetAncestor(nHeight))};
4863
4864
0
            if (nHeight % 10'000 == 0) {
  Branch (4864:17): [True: 0, False: 0]
4865
0
                LogInfo("Rolling forward %s (%i)", pindex.GetBlockHash().ToString(), nHeight);
4866
0
            }
4867
0
            m_chainman.GetNotifications().progress(_("Replaying blocks…"), (int)((nHeight - nForkHeight) * 100.0 / (pindexNew->nHeight - nForkHeight)), false);
4868
0
            if (!RollforwardBlock(&pindex, cache)) return false;
  Branch (4868:17): [True: 0, False: 0]
4869
0
        }
4870
0
        LogInfo("Rolled forward to %s", pindexNew->GetBlockHash().ToString());
4871
0
    }
4872
4873
0
    cache.SetBestBlock(pindexNew->GetBlockHash());
4874
0
    cache.Flush(/*reallocate_cache=*/false); // local CCoinsViewCache goes out of scope
4875
0
    m_chainman.GetNotifications().progress(bilingual_str{}, 100, false);
4876
0
    return true;
4877
0
}
4878
4879
bool Chainstate::NeedsRedownload() const
4880
3.14k
{
4881
3.14k
    AssertLockHeld(cs_main);
4882
4883
    // At and above m_params.SegwitHeight, segwit consensus rules must be validated
4884
3.14k
    CBlockIndex* block{m_chain.Tip()};
4885
4886
3.14k
    while (block != nullptr && DeploymentActiveAt(*block, m_chainman, Consensus::DEPLOYMENT_SEGWIT)) {
  Branch (4886:12): [True: 0, False: 3.14k]
  Branch (4886:32): [True: 0, False: 0]
4887
0
        if (!(block->nStatus & BLOCK_OPT_WITNESS)) {
  Branch (4887:13): [True: 0, False: 0]
4888
            // block is insufficiently validated for a segwit client
4889
0
            return true;
4890
0
        }
4891
0
        block = block->pprev;
4892
0
    }
4893
4894
3.14k
    return false;
4895
3.14k
}
4896
4897
void Chainstate::ClearBlockIndexCandidates()
4898
0
{
4899
0
    AssertLockHeld(::cs_main);
4900
0
    setBlockIndexCandidates.clear();
4901
0
}
4902
4903
void Chainstate::PopulateBlockIndexCandidates()
4904
3.18k
{
4905
3.18k
    AssertLockHeld(::cs_main);
4906
4907
12.3k
    for (CBlockIndex* pindex : m_blockman.GetAllBlockIndices()) {
  Branch (4907:30): [True: 12.3k, False: 3.18k]
4908
        // With assumeutxo, the snapshot block is a candidate for the tip, but it
4909
        // may not have BLOCK_VALID_TRANSACTIONS (e.g. if we haven't yet downloaded
4910
        // the block), so we special-case it here.
4911
12.3k
        if (pindex == SnapshotBase() ||
  Branch (4911:13): [True: 46, False: 12.3k]
4912
12.3k
                (pindex->IsValid(BLOCK_VALID_TRANSACTIONS) &&
  Branch (4912:18): [True: 3.18k, False: 9.15k]
4913
12.3k
                 (pindex->HaveNumChainTxs() || pindex->pprev == nullptr))) {
  Branch (4913:19): [True: 3.18k, False: 0]
  Branch (4913:48): [True: 0, False: 0]
4914
3.23k
            TryAddBlockIndexCandidate(pindex);
4915
3.23k
        }
4916
12.3k
    }
4917
3.18k
}
4918
4919
bool ChainstateManager::LoadBlockIndex()
4920
3.14k
{
4921
3.14k
    AssertLockHeld(cs_main);
4922
    // Load block index from databases
4923
3.14k
    if (m_blockman.m_blockfiles_indexed) {
  Branch (4923:9): [True: 3.14k, False: 0]
4924
3.14k
        bool ret{m_blockman.LoadBlockIndexDB(CurrentChainstate().m_from_snapshot_blockhash)};
4925
3.14k
        if (!ret) return false;
  Branch (4925:13): [True: 0, False: 3.14k]
4926
4927
3.14k
        m_blockman.ScanAndUnlinkAlreadyPrunedFiles();
4928
4929
3.14k
        std::vector<CBlockIndex*> vSortedByHeight{m_blockman.GetAllBlockIndices()};
4930
3.14k
        std::sort(vSortedByHeight.begin(), vSortedByHeight.end(),
4931
3.14k
                  CBlockIndexHeightOnlyComparator());
4932
4933
3.14k
        for (CBlockIndex* pindex : vSortedByHeight) {
  Branch (4933:34): [True: 0, False: 3.14k]
4934
0
            if (m_interrupt) return false;
  Branch (4934:17): [True: 0, False: 0]
4935
0
            if (pindex->nStatus & BLOCK_FAILED_VALID && (!m_best_invalid || pindex->nChainWork > m_best_invalid->nChainWork)) {
  Branch (4935:17): [True: 0, False: 0]
  Branch (4935:58): [True: 0, False: 0]
  Branch (4935:77): [True: 0, False: 0]
4936
0
                m_best_invalid = pindex;
4937
0
            }
4938
0
            if (pindex->IsValid(BLOCK_VALID_TREE) && (m_best_header == nullptr || CBlockIndexWorkComparator()(m_best_header, pindex)))
  Branch (4938:17): [True: 0, False: 0]
  Branch (4938:17): [True: 0, False: 0]
  Branch (4938:55): [True: 0, False: 0]
  Branch (4938:83): [True: 0, False: 0]
4939
0
                m_best_header = pindex;
4940
0
        }
4941
3.14k
    }
4942
3.14k
    return true;
4943
3.14k
}
4944
4945
bool ChainstateManager::LoadGenesisBlock()
4946
3.14k
{
4947
3.14k
    LOCK(cs_main);
4948
4949
3.14k
    const CBlock& genesis_block{GetParams().GenesisBlock()};
4950
4951
    // Check whether we're already initialized by checking for genesis in
4952
    // m_blockman.m_block_index. Note that we can't use a chainstate's m_chain here, since it is
4953
    // set based on the coins db, not the block index db, which is the only
4954
    // thing loaded at this point.
4955
3.14k
    if (m_blockman.m_block_index.contains(genesis_block.GetHash())) {
  Branch (4955:9): [True: 0, False: 3.14k]
4956
0
        return true;
4957
0
    }
4958
4959
3.14k
    try {
4960
3.14k
        FlatFilePos blockPos{m_blockman.WriteBlock(genesis_block, 0)};
4961
3.14k
        if (blockPos.IsNull()) {
  Branch (4961:13): [True: 0, False: 3.14k]
4962
0
            LogError("Writing genesis block to disk failed");
4963
0
            return false;
4964
0
        }
4965
3.14k
        CBlockIndex* pindex{m_blockman.AddToBlockIndex(genesis_block, m_best_header)};
4966
3.14k
        ReceivedBlockTransactions(genesis_block, pindex, blockPos);
4967
3.14k
    } catch (const std::runtime_error& e) {
4968
0
        LogError("Failed to write genesis block: %s", e.what());
4969
0
        return false;
4970
0
    }
4971
4972
3.14k
    return true;
4973
3.14k
}
4974
4975
void ChainstateManager::LoadExternalBlockFile(
4976
    AutoFile& file_in,
4977
    FlatFilePos* dbp,
4978
    std::multimap<uint256, FlatFilePos>* blocks_with_unknown_parent)
4979
456
{
4980
    // Either both should be specified (-reindex), or neither (-loadblock).
4981
456
    assert(!dbp == !blocks_with_unknown_parent);
  Branch (4981:5): [True: 456, False: 0]
4982
4983
456
    const auto start{SteadyClock::now()};
4984
456
    const CChainParams& params{GetParams()};
4985
4986
456
    int nLoaded = 0;
4987
456
    try {
4988
456
        BufferedFile blkdat{file_in, 2 * MAX_BLOCK_SERIALIZED_SIZE, MAX_BLOCK_SERIALIZED_SIZE + 8};
4989
        // nRewind indicates where to resume scanning in case something goes wrong,
4990
        // such as a block fails to deserialize.
4991
456
        uint64_t nRewind = blkdat.GetPos();
4992
1.22M
        while (!blkdat.eof()) {
  Branch (4992:16): [True: 1.22M, False: 183]
4993
1.22M
            if (m_interrupt) return;
  Branch (4993:17): [True: 0, False: 1.22M]
4994
4995
1.22M
            blkdat.SetPos(nRewind);
4996
1.22M
            nRewind++; // start one byte further next time, in case of failure
4997
1.22M
            blkdat.SetLimit(); // remove former limit
4998
1.22M
            unsigned int nSize = 0;
4999
1.22M
            try {
5000
                // locate a header
5001
1.22M
                MessageStartChars buf;
5002
1.22M
                blkdat.FindByte(std::byte(params.MessageStart()[0]));
5003
1.22M
                nRewind = blkdat.GetPos() + 1;
5004
1.22M
                blkdat >> buf;
5005
1.22M
                if (buf != params.MessageStart()) {
  Branch (5005:21): [True: 993k, False: 227k]
5006
993k
                    continue;
5007
993k
                }
5008
                // read size
5009
227k
                blkdat >> nSize;
5010
227k
                if (nSize < 80 || nSize > MAX_BLOCK_SERIALIZED_SIZE)
  Branch (5010:21): [True: 1.29k, False: 226k]
  Branch (5010:35): [True: 30.3k, False: 196k]
5011
31.3k
                    continue;
5012
227k
            } catch (const std::exception&) {
5013
                // no valid block header found; don't complain
5014
                // (this happens at the end of every blk.dat file)
5015
273
                break;
5016
273
            }
5017
196k
            try {
5018
                // read block header
5019
196k
                const uint64_t nBlockPos{blkdat.GetPos()};
5020
196k
                if (dbp)
  Branch (5020:21): [True: 82.2k, False: 113k]
5021
82.2k
                    dbp->nPos = nBlockPos;
5022
196k
                blkdat.SetLimit(nBlockPos + nSize);
5023
196k
                CBlockHeader header;
5024
196k
                blkdat >> header;
5025
196k
                const uint256 hash{header.GetHash()};
5026
                // Skip the rest of this block (this may read from disk into memory); position to the marker before the
5027
                // next block, but it's still possible to rewind to the start of the current block (without a disk read).
5028
196k
                nRewind = nBlockPos + nSize;
5029
196k
                blkdat.SkipTo(nRewind);
5030
5031
196k
                std::shared_ptr<CBlock> pblock{}; // needs to remain available after the cs_main lock is released to avoid duplicate reads from disk
5032
5033
196k
                {
5034
196k
                    LOCK(cs_main);
5035
                    // detect out of order blocks, and store them for later
5036
196k
                    if (hash != params.GetConsensus().hashGenesisBlock && !m_blockman.LookupBlockIndex(header.hashPrevBlock)) {
  Branch (5036:25): [True: 195k, False: 1.00k]
  Branch (5036:75): [True: 123k, False: 71.7k]
5037
123k
                        LogDebug(BCLog::REINDEX, "%s: Out of order block %s, parent %s not known\n", __func__, hash.ToString(),
5038
123k
                                 header.hashPrevBlock.ToString());
5039
123k
                        if (dbp && blocks_with_unknown_parent) {
  Branch (5039:29): [True: 58.4k, False: 64.9k]
  Branch (5039:36): [True: 58.4k, False: 0]
5040
58.4k
                            blocks_with_unknown_parent->emplace(header.hashPrevBlock, *dbp);
5041
58.4k
                        }
5042
123k
                        continue;
5043
123k
                    }
5044
5045
                    // process in case the block isn't known yet
5046
72.7k
                    const CBlockIndex* pindex = m_blockman.LookupBlockIndex(hash);
5047
72.7k
                    if (!pindex || (pindex->nStatus & BLOCK_HAVE_DATA) == 0) {
  Branch (5047:25): [True: 58.9k, False: 13.8k]
  Branch (5047:36): [True: 13.8k, False: 0]
5048
                        // This block can be processed immediately; rewind to its start, read and deserialize it.
5049
71.7k
                        blkdat.SetPos(nBlockPos);
5050
71.7k
                        pblock = std::make_shared<CBlock>();
5051
71.7k
                        blkdat >> TX_WITH_WITNESS(*pblock);
5052
71.7k
                        nRewind = blkdat.GetPos();
5053
5054
71.7k
                        BlockValidationState state;
5055
71.7k
                        if (AcceptBlock(pblock, state, nullptr, true, dbp, nullptr, true)) {
  Branch (5055:29): [True: 0, False: 71.7k]
5056
0
                            nLoaded++;
5057
0
                        }
5058
71.7k
                        if (state.IsError()) {
  Branch (5058:29): [True: 0, False: 71.7k]
5059
0
                            break;
5060
0
                        }
5061
71.7k
                    } else if (hash != params.GetConsensus().hashGenesisBlock && pindex->nHeight % 1000 == 0) {
  Branch (5061:32): [True: 0, False: 1.00k]
  Branch (5061:82): [True: 0, False: 0]
5062
0
                        LogDebug(BCLog::REINDEX, "Block Import: already had block %s at height %d\n", hash.ToString(), pindex->nHeight);
5063
0
                    }
5064
72.7k
                }
5065
5066
                // Activate the genesis block so normal node progress can continue
5067
                // During first -reindex, this will only connect Genesis since
5068
                // ActivateBestChain only connects blocks which are in the block tree db,
5069
                // which only contains blocks whose parents are in it.
5070
                // But do this only if genesis isn't activated yet, to avoid connecting many blocks
5071
                // without assumevalid in the case of a continuation of a reindex that
5072
                // was interrupted by the user.
5073
72.7k
                if (hash == params.GetConsensus().hashGenesisBlock && WITH_LOCK(::cs_main, return ActiveHeight()) == -1) {
  Branch (5073:21): [True: 0, False: 72.7k]
  Branch (5073:21): [True: 0, False: 72.7k]
  Branch (5073:71): [True: 0, False: 0]
5074
0
                    BlockValidationState state;
5075
0
                    if (!ActiveChainstate().ActivateBestChain(state, nullptr)) {
  Branch (5075:25): [True: 0, False: 0]
5076
0
                        break;
5077
0
                    }
5078
0
                }
5079
5080
72.7k
                if (m_blockman.IsPruneMode() && m_blockman.m_blockfiles_indexed && pblock) {
  Branch (5080:21): [True: 0, False: 72.7k]
  Branch (5080:49): [True: 0, False: 0]
  Branch (5080:84): [True: 0, False: 0]
5081
                    // must update the tip for pruning to work while importing with -loadblock.
5082
                    // this is a tradeoff to conserve disk space at the expense of time
5083
                    // spent updating the tip to be able to prune.
5084
                    // otherwise, ActivateBestChain won't be called by the import process
5085
                    // until after all of the block files are loaded. ActivateBestChain can be
5086
                    // called by concurrent network message processing. but, that is not
5087
                    // reliable for the purpose of pruning while importing.
5088
0
                    if (auto result{ActivateBestChains()}; !result) {
  Branch (5088:60): [True: 0, False: 0]
5089
0
                        LogDebug(BCLog::REINDEX, "%s\n", util::ErrorString(result).original);
5090
0
                        break;
5091
0
                    }
5092
0
                }
5093
5094
72.7k
                NotifyHeaderTip();
5095
5096
72.7k
                if (!blocks_with_unknown_parent) continue;
  Branch (5096:21): [True: 32.3k, False: 40.4k]
5097
5098
                // Recursively process earlier encountered successors of this block
5099
40.4k
                std::deque<uint256> queue;
5100
40.4k
                queue.push_back(hash);
5101
56.4k
                while (!queue.empty()) {
  Branch (5101:24): [True: 16.0k, False: 40.4k]
5102
16.0k
                    uint256 head = queue.front();
5103
16.0k
                    queue.pop_front();
5104
16.0k
                    auto range = blocks_with_unknown_parent->equal_range(head);
5105
30.8k
                    while (range.first != range.second) {
  Branch (5105:28): [True: 14.8k, False: 16.0k]
5106
14.8k
                        std::multimap<uint256, FlatFilePos>::iterator it = range.first;
5107
14.8k
                        std::shared_ptr<CBlock> pblockrecursive = std::make_shared<CBlock>();
5108
14.8k
                        if (m_blockman.ReadBlock(*pblockrecursive, it->second, {})) {
  Branch (5108:29): [True: 0, False: 14.8k]
5109
0
                            const auto& block_hash{pblockrecursive->GetHash()};
5110
0
                            LogDebug(BCLog::REINDEX, "%s: Processing out of order child %s of %s", __func__, block_hash.ToString(), head.ToString());
5111
0
                            LOCK(cs_main);
5112
0
                            BlockValidationState dummy;
5113
0
                            if (AcceptBlock(pblockrecursive, dummy, nullptr, true, &it->second, nullptr, true)) {
  Branch (5113:33): [True: 0, False: 0]
5114
0
                                nLoaded++;
5115
0
                                queue.push_back(block_hash);
5116
0
                            }
5117
0
                        }
5118
14.8k
                        range.first++;
5119
14.8k
                        blocks_with_unknown_parent->erase(it);
5120
14.8k
                        NotifyHeaderTip();
5121
14.8k
                    }
5122
16.0k
                }
5123
40.4k
            } catch (const std::exception& e) {
5124
                // historical bugs added extra data to the block files that does not deserialize cleanly.
5125
                // commonly this data is between readable blocks, but it does not really matter. such data is not fatal to the import process.
5126
                // the code that reads the block files deals with invalid data by simply ignoring it.
5127
                // it continues to search for the next {4 byte magic message start bytes + 4 byte length + block} that does deserialize cleanly
5128
                // and passes all of the other block validation checks dealing with POW and the merkle root, etc...
5129
                // we merely note with this informational log message when unexpected data is encountered.
5130
                // we could also be experiencing a storage system read error, or a read of a previous bad write. these are possible, but
5131
                // less likely scenarios. we don't have enough information to tell a difference here.
5132
                // the reindex process is not the place to attempt to clean and/or compact the block files. if so desired, a studious node operator
5133
                // may use knowledge of the fact that the block files are not entirely pristine in order to prepare a set of pristine, and
5134
                // perhaps ordered, block files for later reindexing.
5135
24.4k
                LogDebug(BCLog::REINDEX, "%s: unexpected data at file offset 0x%x - %s. continuing\n", __func__, (nRewind - 1), e.what());
5136
24.4k
            }
5137
196k
        }
5138
456
    } catch (const std::runtime_error& e) {
5139
0
        GetNotifications().fatalError(strprintf(_("System error while loading external block file: %s"), e.what()));
5140
0
    }
5141
456
    LogInfo("Loaded %i blocks from external file in %dms", nLoaded, Ticks<std::chrono::milliseconds>(SteadyClock::now() - start));
5142
456
}
5143
5144
bool ChainstateManager::ShouldCheckBlockIndex() const
5145
1.36M
{
5146
    // Assert to verify Flatten() has been called.
5147
1.36M
    if (!*Assert(m_options.check_block_index)) return false;
  Branch (5147:9): [True: 0, False: 1.36M]
5148
1.36M
    if (FastRandomContext().randrange(*m_options.check_block_index) >= 1) return false;
  Branch (5148:9): [True: 0, False: 1.36M]
5149
1.36M
    return true;
5150
1.36M
}
5151
5152
void ChainstateManager::CheckBlockIndex() const
5153
1.36M
{
5154
1.36M
    if (!ShouldCheckBlockIndex()) {
  Branch (5154:9): [True: 0, False: 1.36M]
5155
0
        return;
5156
0
    }
5157
5158
1.36M
    LOCK(cs_main);
5159
5160
    // During a reindex, we read the genesis block and call CheckBlockIndex before ActivateBestChain,
5161
    // so we have the genesis block in m_blockman.m_block_index but no active chain. (A few of the
5162
    // tests when iterating the block tree require that m_chain has been initialized.)
5163
1.36M
    if (ActiveChain().Height() < 0) {
  Branch (5163:9): [True: 0, False: 1.36M]
5164
0
        assert(m_blockman.m_block_index.size() <= 1);
  Branch (5164:9): [True: 0, False: 0]
5165
0
        return;
5166
0
    }
5167
5168
    // Build forward-pointing data structure for the entire block tree.
5169
    // For performance reasons, indexes of the best header chain are stored in a vector (within CChain).
5170
    // All remaining blocks are stored in a multimap.
5171
    // The best header chain can differ from the active chain: E.g. its entries may belong to blocks that
5172
    // are not yet validated.
5173
1.36M
    CChain best_hdr_chain;
5174
1.36M
    assert(m_best_header);
  Branch (5174:5): [True: 1.36M, False: 0]
5175
1.36M
    assert(!(m_best_header->nStatus & BLOCK_FAILED_VALID));
  Branch (5175:5): [True: 1.36M, False: 0]
5176
1.36M
    best_hdr_chain.SetTip(*m_best_header);
5177
5178
1.36M
    std::multimap<const CBlockIndex*, const CBlockIndex*> forward;
5179
144M
    for (auto& [_, block_index] : m_blockman.m_block_index) {
  Branch (5179:33): [True: 144M, False: 1.36M]
5180
        // Only save indexes in forward that are not part of the best header chain.
5181
144M
        if (!best_hdr_chain.Contains(block_index)) {
  Branch (5181:13): [True: 13.6M, False: 131M]
5182
            // Only genesis, which must be part of the best header chain, can have a nullptr parent.
5183
13.6M
            assert(block_index.pprev);
  Branch (5183:13): [True: 13.6M, False: 0]
5184
13.6M
            forward.emplace(block_index.pprev, &block_index);
5185
13.6M
        }
5186
144M
    }
5187
1.36M
    assert(forward.size() + best_hdr_chain.Height() + 1 == m_blockman.m_block_index.size());
  Branch (5187:5): [True: 1.36M, False: 0]
5188
5189
1.36M
    const CBlockIndex* pindex = best_hdr_chain[0];
5190
1.36M
    assert(pindex);
  Branch (5190:5): [True: 1.36M, False: 0]
5191
    // Iterate over the entire block tree, using depth-first search.
5192
    // Along the way, remember whether there are blocks on the path from genesis
5193
    // block being explored which are the first to have certain properties.
5194
1.36M
    size_t nNodes = 0;
5195
1.36M
    int nHeight = 0;
5196
1.36M
    const CBlockIndex* pindexFirstInvalid = nullptr;              // Oldest ancestor of pindex which is invalid.
5197
1.36M
    const CBlockIndex* pindexFirstMissing = nullptr;              // Oldest ancestor of pindex which does not have BLOCK_HAVE_DATA, since assumeutxo snapshot if used.
5198
1.36M
    const CBlockIndex* pindexFirstNeverProcessed = nullptr;       // Oldest ancestor of pindex for which nTx == 0, since assumeutxo snapshot if used.
5199
1.36M
    const CBlockIndex* pindexFirstNotTreeValid = nullptr;         // Oldest ancestor of pindex which does not have BLOCK_VALID_TREE (regardless of being valid or not).
5200
1.36M
    const CBlockIndex* pindexFirstNotTransactionsValid = nullptr; // Oldest ancestor of pindex which does not have BLOCK_VALID_TRANSACTIONS (regardless of being valid or not), since assumeutxo snapshot if used.
5201
1.36M
    const CBlockIndex* pindexFirstNotChainValid = nullptr;        // Oldest ancestor of pindex which does not have BLOCK_VALID_CHAIN (regardless of being valid or not), since assumeutxo snapshot if used.
5202
1.36M
    const CBlockIndex* pindexFirstNotScriptsValid = nullptr;      // Oldest ancestor of pindex which does not have BLOCK_VALID_SCRIPTS (regardless of being valid or not), since assumeutxo snapshot if used.
5203
5204
    // After checking an assumeutxo snapshot block, reset pindexFirst pointers
5205
    // to earlier blocks that have not been downloaded or validated yet, so
5206
    // checks for later blocks can assume the earlier blocks were validated and
5207
    // be stricter, testing for more requirements.
5208
1.36M
    const CBlockIndex* snap_base{CurrentChainstate().SnapshotBase()};
5209
1.36M
    const CBlockIndex *snap_first_missing{}, *snap_first_notx{}, *snap_first_notv{}, *snap_first_nocv{}, *snap_first_nosv{};
5210
158M
    auto snap_update_firsts = [&] {
5211
158M
        if (pindex == snap_base) {
  Branch (5211:13): [True: 0, False: 158M]
5212
0
            std::swap(snap_first_missing, pindexFirstMissing);
5213
0
            std::swap(snap_first_notx, pindexFirstNeverProcessed);
5214
0
            std::swap(snap_first_notv, pindexFirstNotTransactionsValid);
5215
0
            std::swap(snap_first_nocv, pindexFirstNotChainValid);
5216
0
            std::swap(snap_first_nosv, pindexFirstNotScriptsValid);
5217
0
        }
5218
158M
    };
5219
5220
144M
    while (pindex != nullptr) {
  Branch (5220:12): [True: 144M, False: 19.1k]
5221
144M
        nNodes++;
5222
144M
        if (pindexFirstInvalid == nullptr && pindex->nStatus & BLOCK_FAILED_VALID) pindexFirstInvalid = pindex;
  Branch (5222:13): [True: 144M, False: 6.56k]
  Branch (5222:46): [True: 4.26M, False: 140M]
5223
144M
        if (pindexFirstMissing == nullptr && !(pindex->nStatus & BLOCK_HAVE_DATA)) {
  Branch (5223:13): [True: 133M, False: 11.2M]
  Branch (5223:46): [True: 12.6M, False: 120M]
5224
12.6M
            pindexFirstMissing = pindex;
5225
12.6M
        }
5226
144M
        if (pindexFirstNeverProcessed == nullptr && pindex->nTx == 0) pindexFirstNeverProcessed = pindex;
  Branch (5226:13): [True: 133M, False: 11.2M]
  Branch (5226:53): [True: 12.6M, False: 120M]
5227
144M
        if (pindex->pprev != nullptr && pindexFirstNotTreeValid == nullptr && (pindex->nStatus & BLOCK_VALID_MASK) < BLOCK_VALID_TREE) pindexFirstNotTreeValid = pindex;
  Branch (5227:13): [True: 143M, False: 1.36M]
  Branch (5227:41): [True: 143M, False: 0]
  Branch (5227:79): [True: 0, False: 143M]
5228
5229
144M
        if (pindex->pprev != nullptr) {
  Branch (5229:13): [True: 143M, False: 1.36M]
5230
143M
            if (pindexFirstNotTransactionsValid == nullptr &&
  Branch (5230:17): [True: 132M, False: 11.2M]
5231
143M
                    (pindex->nStatus & BLOCK_VALID_MASK) < BLOCK_VALID_TRANSACTIONS) {
  Branch (5231:21): [True: 12.6M, False: 119M]
5232
12.6M
                pindexFirstNotTransactionsValid = pindex;
5233
12.6M
            }
5234
5235
143M
            if (pindexFirstNotChainValid == nullptr &&
  Branch (5235:17): [True: 132M, False: 11.2M]
5236
143M
                    (pindex->nStatus & BLOCK_VALID_MASK) < BLOCK_VALID_CHAIN) {
  Branch (5236:21): [True: 14.0M, False: 118M]
5237
14.0M
                pindexFirstNotChainValid = pindex;
5238
14.0M
            }
5239
5240
143M
            if (pindexFirstNotScriptsValid == nullptr &&
  Branch (5240:17): [True: 132M, False: 11.2M]
5241
143M
                    (pindex->nStatus & BLOCK_VALID_MASK) < BLOCK_VALID_SCRIPTS) {
  Branch (5241:21): [True: 14.0M, False: 118M]
5242
14.0M
                pindexFirstNotScriptsValid = pindex;
5243
14.0M
            }
5244
143M
        }
5245
5246
        // Begin: actual consistency checks.
5247
144M
        if (pindex->pprev == nullptr) {
  Branch (5247:13): [True: 1.36M, False: 143M]
5248
            // Genesis block checks.
5249
1.36M
            assert(pindex->GetBlockHash() == GetConsensus().hashGenesisBlock); // Genesis block's hash must match.
  Branch (5249:13): [True: 1.36M, False: 0]
5250
1.36M
            for (const auto& c : m_chainstates) {
  Branch (5250:32): [True: 1.36M, False: 1.36M]
5251
1.36M
                if (c->m_chain.Genesis() != nullptr) {
  Branch (5251:21): [True: 1.36M, False: 0]
5252
1.36M
                    assert(pindex == c->m_chain.Genesis()); // The chain's genesis block must be this block.
  Branch (5252:21): [True: 1.36M, False: 0]
5253
1.36M
                }
5254
1.36M
            }
5255
1.36M
        }
5256
        // nSequenceId can't be set higher than SEQ_ID_INIT_FROM_DISK{1} for blocks that aren't linked
5257
        // (negative is used for preciousblock, SEQ_ID_BEST_CHAIN_FROM_DISK{0} for active chain when loaded from disk)
5258
144M
        if (!pindex->HaveNumChainTxs()) assert(pindex->nSequenceId <= SEQ_ID_INIT_FROM_DISK);
  Branch (5258:13): [True: 23.9M, False: 120M]
  Branch (5258:41): [True: 23.9M, False: 0]
5259
        // VALID_TRANSACTIONS is equivalent to nTx > 0 for all nodes (whether or not pruning has occurred).
5260
        // HAVE_DATA is only equivalent to nTx > 0 (or VALID_TRANSACTIONS) if no pruning has occurred.
5261
144M
        if (!m_blockman.m_have_pruned) {
  Branch (5261:13): [True: 144M, False: 13.4k]
5262
            // If we've never pruned, then HAVE_DATA should be equivalent to nTx > 0
5263
144M
            assert(!(pindex->nStatus & BLOCK_HAVE_DATA) == (pindex->nTx == 0));
  Branch (5263:13): [True: 144M, False: 0]
5264
144M
            assert(pindexFirstMissing == pindexFirstNeverProcessed);
  Branch (5264:13): [True: 144M, False: 0]
5265
144M
        } else {
5266
            // If we have pruned, then we can only say that HAVE_DATA implies nTx > 0
5267
13.4k
            if (pindex->nStatus & BLOCK_HAVE_DATA) assert(pindex->nTx > 0);
  Branch (5267:17): [True: 3.12k, False: 10.3k]
  Branch (5267:52): [True: 3.12k, False: 0]
5268
13.4k
        }
5269
144M
        if (pindex->nStatus & BLOCK_HAVE_UNDO) assert(pindex->nStatus & BLOCK_HAVE_DATA);
  Branch (5269:13): [True: 118M, False: 26.6M]
  Branch (5269:48): [True: 118M, False: 0]
5270
144M
        if (snap_base && snap_base->GetAncestor(pindex->nHeight) == pindex) {
  Branch (5270:13): [True: 0, False: 144M]
  Branch (5270:26): [True: 0, False: 0]
5271
            // Assumed-valid blocks should connect to the main chain.
5272
0
            assert((pindex->nStatus & BLOCK_VALID_MASK) >= BLOCK_VALID_TREE);
  Branch (5272:13): [True: 0, False: 0]
5273
0
        }
5274
        // There should only be an nTx value if we have
5275
        // actually seen a block's transactions.
5276
144M
        assert(((pindex->nStatus & BLOCK_VALID_MASK) >= BLOCK_VALID_TRANSACTIONS) == (pindex->nTx > 0)); // This is pruning-independent.
  Branch (5276:9): [True: 144M, False: 0]
5277
        // All parents having had data (at some point) is equivalent to all parents being VALID_TRANSACTIONS, which is equivalent to HaveNumChainTxs().
5278
        // HaveNumChainTxs will also be set in the assumeutxo snapshot block from snapshot metadata.
5279
144M
        assert((pindexFirstNeverProcessed == nullptr || pindex == snap_base) == pindex->HaveNumChainTxs());
  Branch (5279:9): [True: 120M, False: 23.9M]
  Branch (5279:9): [True: 0, False: 23.9M]
  Branch (5279:9): [True: 144M, False: 0]
5280
144M
        assert((pindexFirstNotTransactionsValid == nullptr || pindex == snap_base) == pindex->HaveNumChainTxs());
  Branch (5280:9): [True: 120M, False: 23.9M]
  Branch (5280:9): [True: 0, False: 23.9M]
  Branch (5280:9): [True: 144M, False: 0]
5281
144M
        assert(pindex->nHeight == nHeight); // nHeight must be consistent.
  Branch (5281:9): [True: 144M, False: 0]
5282
144M
        assert(pindex->pprev == nullptr || pindex->nChainWork >= pindex->pprev->nChainWork); // For every block except the genesis block, the chainwork must be larger than the parent's.
  Branch (5282:9): [True: 1.36M, False: 143M]
  Branch (5282:9): [True: 143M, False: 0]
  Branch (5282:9): [True: 144M, False: 0]
5283
144M
        assert(nHeight < 2 || (pindex->pskip && (pindex->pskip->nHeight < nHeight))); // The pskip pointer must point back for all but the first 2 blocks.
  Branch (5283:9): [True: 130M, False: 0]
  Branch (5283:9): [True: 130M, False: 0]
  Branch (5283:9): [True: 14.3M, False: 130M]
  Branch (5283:9): [True: 144M, False: 0]
5284
144M
        assert(pindexFirstNotTreeValid == nullptr); // All m_blockman.m_block_index entries must at least be TREE valid
  Branch (5284:9): [True: 144M, False: 0]
5285
144M
        if ((pindex->nStatus & BLOCK_VALID_MASK) >= BLOCK_VALID_TREE) assert(pindexFirstNotTreeValid == nullptr); // TREE valid implies all parents are TREE valid
  Branch (5285:13): [True: 144M, False: 0]
  Branch (5285:71): [True: 144M, False: 0]
5286
144M
        if ((pindex->nStatus & BLOCK_VALID_MASK) >= BLOCK_VALID_CHAIN) assert(pindexFirstNotChainValid == nullptr); // CHAIN valid implies all parents are CHAIN valid
  Branch (5286:13): [True: 118M, False: 26.6M]
  Branch (5286:72): [True: 118M, False: 0]
5287
144M
        if ((pindex->nStatus & BLOCK_VALID_MASK) >= BLOCK_VALID_SCRIPTS) assert(pindexFirstNotScriptsValid == nullptr); // SCRIPTS valid implies all parents are SCRIPTS valid
  Branch (5287:13): [True: 118M, False: 26.6M]
  Branch (5287:74): [True: 118M, False: 0]
5288
144M
        if (pindexFirstInvalid == nullptr) {
  Branch (5288:13): [True: 140M, False: 4.27M]
5289
            // Checks for not-invalid blocks.
5290
140M
            assert((pindex->nStatus & BLOCK_FAILED_VALID) == 0); // The failed flag cannot be set for blocks without invalid parents.
  Branch (5290:13): [True: 140M, False: 0]
5291
140M
        } else {
5292
4.27M
            assert(pindex->nStatus & BLOCK_FAILED_VALID); // Invalid blocks and their descendants must be marked as invalid
  Branch (5292:13): [True: 4.27M, False: 0]
5293
4.27M
        }
5294
        // Make sure m_chain_tx_count sum is correctly computed.
5295
144M
        if (!pindex->pprev) {
  Branch (5295:13): [True: 1.36M, False: 143M]
5296
            // If no previous block, nTx and m_chain_tx_count must be the same.
5297
1.36M
            assert(pindex->m_chain_tx_count == pindex->nTx);
  Branch (5297:13): [True: 1.36M, False: 0]
5298
143M
        } else if (pindex->pprev->m_chain_tx_count > 0 && pindex->nTx > 0) {
  Branch (5298:20): [True: 132M, False: 11.2M]
  Branch (5298:59): [True: 119M, False: 12.6M]
5299
            // If previous m_chain_tx_count is set and number of transactions in block is known, sum must be set.
5300
119M
            assert(pindex->m_chain_tx_count == pindex->nTx + pindex->pprev->m_chain_tx_count);
  Branch (5300:13): [True: 119M, False: 0]
5301
119M
        } else {
5302
            // Otherwise m_chain_tx_count should only be set if this is a snapshot
5303
            // block, and must be set if it is.
5304
23.9M
            assert((pindex->m_chain_tx_count != 0) == (pindex == snap_base));
  Branch (5304:13): [True: 23.9M, False: 0]
5305
23.9M
        }
5306
        // There should be no block with more work than m_best_header, unless it's known to be invalid
5307
144M
        assert((pindex->nStatus & BLOCK_FAILED_VALID) || pindex->nChainWork <= m_best_header->nChainWork);
  Branch (5307:9): [True: 4.27M, False: 140M]
  Branch (5307:9): [True: 140M, False: 0]
  Branch (5307:9): [True: 144M, False: 0]
5308
5309
        // Chainstate-specific checks on setBlockIndexCandidates
5310
144M
        for (const auto& c : m_chainstates) {
  Branch (5310:28): [True: 144M, False: 144M]
5311
144M
            if (c->m_chain.Tip() == nullptr) continue;
  Branch (5311:17): [True: 0, False: 144M]
5312
            // Two main factors determine whether pindex is a candidate in
5313
            // setBlockIndexCandidates:
5314
            //
5315
            // - If pindex has less work than the chain tip, it should not be a
5316
            //   candidate, and this will be asserted below. Otherwise it is a
5317
            //   potential candidate.
5318
            //
5319
            // - If pindex or one of its parent blocks back to the genesis block
5320
            //   or an assumeutxo snapshot never downloaded transactions
5321
            //   (pindexFirstNeverProcessed is non-null), it should not be a
5322
            //   candidate, and this will be asserted below. The only exception
5323
            //   is if pindex itself is an assumeutxo snapshot block. Then it is
5324
            //   also a potential candidate.
5325
144M
            if (!CBlockIndexWorkComparator()(pindex, c->m_chain.Tip()) && (pindexFirstNeverProcessed == nullptr || pindex == snap_base)) {
  Branch (5325:17): [True: 25.8M, False: 119M]
  Branch (5325:17): [True: 1.86M, False: 143M]
  Branch (5325:76): [True: 1.86M, False: 23.9M]
  Branch (5325:116): [True: 0, False: 23.9M]
5326
                // If pindex was detected as invalid (pindexFirstInvalid is
5327
                // non-null), it is not required to be in
5328
                // setBlockIndexCandidates.
5329
1.86M
                if (pindexFirstInvalid == nullptr) {
  Branch (5329:21): [True: 1.71M, False: 155k]
5330
                    // If pindex and all its parents back to the genesis block
5331
                    // or an assumeutxo snapshot block downloaded transactions,
5332
                    // and the transactions were not pruned (pindexFirstMissing
5333
                    // is null), it is a potential candidate. The check
5334
                    // excludes pruned blocks, because if any blocks were
5335
                    // pruned between pindex and the current chain tip, pindex will
5336
                    // only temporarily be added to setBlockIndexCandidates,
5337
                    // before being moved to m_blocks_unlinked. This check
5338
                    // could be improved to verify that if all blocks between
5339
                    // the chain tip and pindex have data, pindex must be a
5340
                    // candidate.
5341
                    //
5342
                    // If pindex is the chain tip, it also is a potential
5343
                    // candidate.
5344
                    //
5345
                    // If the chainstate was loaded from a snapshot and pindex
5346
                    // is the base of the snapshot, pindex is also a potential
5347
                    // candidate.
5348
1.71M
                    if (pindexFirstMissing == nullptr || pindex == c->m_chain.Tip() || pindex == c->SnapshotBase()) {
  Branch (5348:25): [True: 1.71M, False: 555]
  Branch (5348:58): [True: 90, False: 465]
  Branch (5348:88): [True: 0, False: 465]
5349
                        // If this chainstate is not a historical chainstate
5350
                        // targeting a specific block, pindex must be in
5351
                        // setBlockIndexCandidates. Otherwise, pindex only
5352
                        // needs to be added if it is an ancestor of the target
5353
                        // block.
5354
1.71M
                        if (!c->TargetBlock() || c->TargetBlock()->GetAncestor(pindex->nHeight) == pindex) {
  Branch (5354:29): [True: 1.71M, False: 0]
  Branch (5354:50): [True: 0, False: 0]
5355
1.71M
                            assert(c->setBlockIndexCandidates.contains(pindex));
  Branch (5355:29): [True: 1.71M, False: 0]
5356
1.71M
                        }
5357
1.71M
                    }
5358
                    // If some parent is missing, then it could be that this block was in
5359
                    // setBlockIndexCandidates but had to be removed because of the missing data.
5360
                    // In this case it must be in m_blocks_unlinked -- see test below.
5361
1.71M
                }
5362
143M
            } else { // If this block sorts worse than the current tip or some ancestor's block has never been seen, it cannot be in setBlockIndexCandidates.
5363
143M
                assert(!c->setBlockIndexCandidates.contains(pindex));
  Branch (5363:17): [True: 143M, False: 0]
5364
143M
            }
5365
144M
        }
5366
        // Check whether this block is in m_blocks_unlinked.
5367
144M
        auto rangeUnlinked{m_blockman.m_blocks_unlinked.equal_range(pindex->pprev)};
5368
144M
        bool foundInUnlinked = false;
5369
144M
        for (auto it = rangeUnlinked.first; it != rangeUnlinked.second; ++it) {
  Branch (5369:45): [True: 16.5k, False: 144M]
5370
16.5k
            assert(it->first == pindex->pprev);
  Branch (5370:13): [True: 16.5k, False: 0]
5371
16.5k
            if (it->second == pindex) {
  Branch (5371:17): [True: 1.77k, False: 14.7k]
5372
1.77k
                assert(!foundInUnlinked); // No duplicates in m_blocks_unlinked
  Branch (5372:17): [True: 1.77k, False: 0]
5373
1.77k
                foundInUnlinked = true;
5374
1.77k
            }
5375
16.5k
        }
5376
144M
        if (pindex->pprev && (pindex->nStatus & BLOCK_HAVE_DATA) && pindexFirstNeverProcessed != nullptr && pindexFirstInvalid == nullptr) {
  Branch (5376:13): [True: 143M, False: 1.36M]
  Branch (5376:30): [True: 119M, False: 23.9M]
  Branch (5376:69): [True: 1.77k, False: 119M]
  Branch (5376:109): [True: 1.49k, False: 283]
5377
            // If this block has block data available, some parent was never received, and has no invalid parents, it must be in m_blocks_unlinked.
5378
1.49k
            assert(foundInUnlinked);
  Branch (5378:13): [True: 1.49k, False: 0]
5379
1.49k
        }
5380
144M
        if (!(pindex->nStatus & BLOCK_HAVE_DATA)) assert(!foundInUnlinked); // Can't be in m_blocks_unlinked if we don't HAVE_DATA
  Branch (5380:13): [True: 23.9M, False: 120M]
  Branch (5380:51): [True: 23.9M, False: 0]
5381
144M
        if (pindexFirstMissing == nullptr) assert(!foundInUnlinked); // We aren't missing data for any parent -- cannot be in m_blocks_unlinked.
  Branch (5381:13): [True: 120M, False: 23.9M]
  Branch (5381:44): [True: 120M, False: 0]
5382
144M
        if (pindex->pprev && (pindex->nStatus & BLOCK_HAVE_DATA) && pindexFirstNeverProcessed == nullptr && pindexFirstMissing != nullptr) {
  Branch (5382:13): [True: 143M, False: 1.36M]
  Branch (5382:30): [True: 119M, False: 23.9M]
  Branch (5382:69): [True: 119M, False: 1.77k]
  Branch (5382:109): [True: 2.13k, False: 119M]
5383
            // We HAVE_DATA for this block, have received data for all parents at some point, but we're currently missing data for some parent.
5384
2.13k
            assert(m_blockman.m_have_pruned);
  Branch (5384:13): [True: 2.13k, False: 0]
5385
            // This block may have entered m_blocks_unlinked if:
5386
            //  - it has a descendant that at some point had more work than the
5387
            //    tip, and
5388
            //  - we tried switching to that descendant but were missing
5389
            //    data for some intermediate block between m_chain and the
5390
            //    tip.
5391
            // So if this block is itself better than any m_chain.Tip() and it wasn't in
5392
            // setBlockIndexCandidates, then it must be in m_blocks_unlinked.
5393
2.13k
            for (const auto& c : m_chainstates) {
  Branch (5393:32): [True: 2.13k, False: 2.13k]
5394
2.13k
                if (!CBlockIndexWorkComparator()(pindex, c->m_chain.Tip()) && !c->setBlockIndexCandidates.contains(pindex)) {
  Branch (5394:21): [True: 1.56k, False: 564]
  Branch (5394:21): [True: 992, False: 1.14k]
  Branch (5394:79): [True: 992, False: 576]
5395
992
                    if (pindexFirstInvalid == nullptr) {
  Branch (5395:25): [True: 0, False: 992]
5396
0
                        if (!c->TargetBlock() || c->TargetBlock()->GetAncestor(pindex->nHeight) == pindex) {
  Branch (5396:29): [True: 0, False: 0]
  Branch (5396:50): [True: 0, False: 0]
5397
0
                            assert(foundInUnlinked);
  Branch (5397:29): [True: 0, False: 0]
5398
0
                        }
5399
0
                    }
5400
992
                }
5401
2.13k
            }
5402
2.13k
        }
5403
        // assert(pindex->GetBlockHash() == pindex->GetBlockHeader().GetHash()); // Perhaps too slow
5404
        // End: actual consistency checks.
5405
5406
5407
        // Try descending into the first subnode. Always process forks first and the best header chain after.
5408
144M
        snap_update_firsts();
5409
144M
        auto range{forward.equal_range(pindex)};
5410
144M
        if (range.first != range.second) {
  Branch (5410:13): [True: 940k, False: 143M]
5411
            // A subnode not part of the best header chain was found.
5412
940k
            pindex = range.first->second;
5413
940k
            nHeight++;
5414
940k
            continue;
5415
143M
        } else if (best_hdr_chain.Contains(*pindex)) {
  Branch (5415:20): [True: 130M, False: 13.4M]
5416
            // Descend further into best header chain.
5417
130M
            nHeight++;
5418
130M
            pindex = best_hdr_chain[nHeight];
5419
130M
            if (!pindex) break; // we are finished, since the best header chain is always processed last
  Branch (5419:17): [True: 1.34M, False: 129M]
5420
129M
            continue;
5421
130M
        }
5422
        // This is a leaf node.
5423
        // Move upwards until we reach a node of which we have not yet visited the last child.
5424
13.6M
        while (pindex) {
  Branch (5424:16): [True: 13.6M, False: 0]
5425
            // We are going to either move to a parent or a sibling of pindex.
5426
13.6M
            snap_update_firsts();
5427
            // If pindex was the first with a certain property, unset the corresponding variable.
5428
13.6M
            if (pindex == pindexFirstInvalid) pindexFirstInvalid = nullptr;
  Branch (5428:17): [True: 4.26M, False: 9.36M]
5429
13.6M
            if (pindex == pindexFirstMissing) pindexFirstMissing = nullptr;
  Branch (5429:17): [True: 12.0M, False: 1.55M]
5430
13.6M
            if (pindex == pindexFirstNeverProcessed) pindexFirstNeverProcessed = nullptr;
  Branch (5430:17): [True: 12.0M, False: 1.55M]
5431
13.6M
            if (pindex == pindexFirstNotTreeValid) pindexFirstNotTreeValid = nullptr;
  Branch (5431:17): [True: 0, False: 13.6M]
5432
13.6M
            if (pindex == pindexFirstNotTransactionsValid) pindexFirstNotTransactionsValid = nullptr;
  Branch (5432:17): [True: 12.0M, False: 1.55M]
5433
13.6M
            if (pindex == pindexFirstNotChainValid) pindexFirstNotChainValid = nullptr;
  Branch (5433:17): [True: 13.0M, False: 562k]
5434
13.6M
            if (pindex == pindexFirstNotScriptsValid) pindexFirstNotScriptsValid = nullptr;
  Branch (5434:17): [True: 13.0M, False: 562k]
5435
            // Find our parent.
5436
13.6M
            CBlockIndex* pindexPar = pindex->pprev;
5437
            // Find which child we just visited.
5438
13.6M
            auto rangePar{forward.equal_range(pindexPar)};
5439
1.60G
            while (rangePar.first->second != pindex) {
  Branch (5439:20): [True: 1.59G, False: 13.6M]
5440
1.59G
                assert(rangePar.first != rangePar.second); // Our parent must have at least the node we're coming from as child.
  Branch (5440:17): [True: 1.59G, False: 0]
5441
1.59G
                rangePar.first++;
5442
1.59G
            }
5443
            // Proceed to the next one.
5444
13.6M
            rangePar.first++;
5445
13.6M
            if (rangePar.first != rangePar.second) {
  Branch (5445:17): [True: 12.6M, False: 940k]
5446
                // Move to a sibling not part of the best header chain.
5447
12.6M
                pindex = rangePar.first->second;
5448
12.6M
                break;
5449
12.6M
            } else if (pindexPar == best_hdr_chain[nHeight - 1]) {
  Branch (5449:24): [True: 792k, False: 147k]
5450
                // Move to pindex's sibling on the best-chain, if it has one.
5451
792k
                pindex = best_hdr_chain[nHeight];
5452
                // There will not be a next block if (and only if) parent block is the best header.
5453
792k
                assert((pindex == nullptr) == (pindexPar == best_hdr_chain.Tip()));
  Branch (5453:17): [True: 792k, False: 0]
5454
792k
                break;
5455
792k
            } else {
5456
                // Move up further.
5457
147k
                pindex = pindexPar;
5458
147k
                nHeight--;
5459
147k
                continue;
5460
147k
            }
5461
13.6M
        }
5462
13.4M
    }
5463
5464
    // Check that we actually traversed the entire block index.
5465
1.36M
    assert(nNodes == forward.size() + best_hdr_chain.Height() + 1);
  Branch (5465:5): [True: 1.36M, False: 0]
5466
1.36M
}
5467
5468
std::string Chainstate::ToString()
5469
11.7k
{
5470
11.7k
    AssertLockHeld(::cs_main);
5471
11.7k
    CBlockIndex* tip = m_chain.Tip();
5472
11.7k
    return strprintf("Chainstate [%s] @ height %d (%s)",
5473
11.7k
                     m_from_snapshot_blockhash ? "snapshot" : "ibd",
  Branch (5473:22): [True: 92, False: 11.6k]
5474
11.7k
                     tip ? tip->nHeight : -1, tip ? tip->GetBlockHash().ToString() : "null");
  Branch (5474:22): [True: 8.60k, False: 3.14k]
  Branch (5474:47): [True: 8.60k, False: 3.14k]
5475
11.7k
}
5476
5477
bool Chainstate::ResizeCoinsCaches(size_t coinstip_size, size_t coinsdb_size)
5478
9.99k
{
5479
9.99k
    AssertLockHeld(::cs_main);
5480
9.99k
    if (coinstip_size == m_coinstip_cache_size_bytes &&
  Branch (5480:9): [True: 5.69k, False: 4.30k]
5481
9.99k
            coinsdb_size == m_coinsdb_cache_size_bytes) {
  Branch (5481:13): [True: 5.69k, False: 0]
5482
        // Cache sizes are unchanged, no need to continue.
5483
5.69k
        return true;
5484
5.69k
    }
5485
4.30k
    size_t old_coinstip_size = m_coinstip_cache_size_bytes;
5486
4.30k
    m_coinstip_cache_size_bytes = coinstip_size;
5487
4.30k
    m_coinsdb_cache_size_bytes = coinsdb_size;
5488
4.30k
    CoinsDB().ResizeCache(coinsdb_size);
5489
5490
4.30k
    LogInfo("[%s] resized coinsdb cache to %.1f MiB",
5491
4.30k
        this->ToString(), coinsdb_size / double(1_MiB));
5492
4.30k
    LogInfo("[%s] resized coinstip cache to %.1f MiB",
5493
4.30k
        this->ToString(), coinstip_size / double(1_MiB));
5494
5495
4.30k
    BlockValidationState state;
5496
4.30k
    bool ret;
5497
5498
4.30k
    if (coinstip_size > old_coinstip_size) {
  Branch (5498:9): [True: 2.12k, False: 2.17k]
5499
        // Likely no need to flush if cache sizes have grown.
5500
2.12k
        ret = FlushStateToDisk(state, FlushStateMode::IF_NEEDED);
5501
2.17k
    } else {
5502
        // Otherwise, flush state to disk and deallocate the in-memory coins map.
5503
2.17k
        ret = FlushStateToDisk(state, FlushStateMode::FORCE_FLUSH);
5504
2.17k
    }
5505
4.30k
    return ret;
5506
9.99k
}
5507
5508
double ChainstateManager::GuessVerificationProgress(const CBlockIndex* pindex) const
5509
689k
{
5510
689k
    AssertLockHeld(GetMutex());
5511
689k
    const ChainTxData& data{GetParams().TxData()};
5512
689k
    if (pindex == nullptr) {
  Branch (5512:9): [True: 0, False: 689k]
5513
0
        return 0.0;
5514
0
    }
5515
5516
689k
    if (pindex->m_chain_tx_count == 0) {
  Branch (5516:9): [True: 0, False: 689k]
5517
0
        LogDebug(BCLog::VALIDATION, "Block %d has unset m_chain_tx_count. Unable to estimate verification progress.\n", pindex->nHeight);
5518
0
        return 0.0;
5519
0
    }
5520
5521
689k
    const int64_t nNow{TicksSinceEpoch<std::chrono::seconds>(NodeClock::now())};
5522
689k
    const auto block_time{
5523
689k
        (Assume(m_best_header) && std::abs(nNow - pindex->GetBlockTime()) <= Ticks<std::chrono::seconds>(2h) &&
  Branch (5523:35): [True: 537k, False: 151k]
5524
689k
         Assume(m_best_header->nHeight >= pindex->nHeight)) ?
5525
            // When the header is known to be recent, switch to a height-based
5526
            // approach. This ensures the returned value is quantized when
5527
            // close to "1.0", because some users expect it to be. This also
5528
            // avoids relying too much on the exact miner-set timestamp, which
5529
            // may be off.
5530
537k
            nNow - (m_best_header->nHeight - pindex->nHeight) * GetConsensus().nPowTargetSpacing :
5531
689k
            pindex->GetBlockTime(),
5532
689k
    };
5533
5534
689k
    double fTxTotal;
5535
5536
689k
    if (pindex->m_chain_tx_count <= data.tx_count) {
  Branch (5536:9): [True: 0, False: 689k]
5537
0
        fTxTotal = data.tx_count + (nNow - data.nTime) * data.dTxRate;
5538
689k
    } else {
5539
689k
        fTxTotal = pindex->m_chain_tx_count + (nNow - block_time) * data.dTxRate;
5540
689k
    }
5541
5542
689k
    return std::min<double>(pindex->m_chain_tx_count / fTxTotal, 1.0);
5543
689k
}
5544
5545
double ChainstateManager::GetBackgroundVerificationProgress(const CBlockIndex& pindex) const
5546
0
{
5547
0
    AssertLockHeld(GetMutex());
5548
0
    Assert(HistoricalChainstate());
5549
0
    auto target_block = HistoricalChainstate()->TargetBlock();
5550
5551
0
    if (pindex.m_chain_tx_count == 0 || target_block->m_chain_tx_count == 0) {
  Branch (5551:9): [True: 0, False: 0]
  Branch (5551:41): [True: 0, False: 0]
5552
0
        LogDebug(BCLog::VALIDATION, "[background validation] Block %d has unset m_chain_tx_count. Unable to estimate verification progress.", pindex.nHeight);
5553
0
        return 0.0;
5554
0
    }
5555
0
    return static_cast<double>(pindex.m_chain_tx_count) / static_cast<double>(target_block->m_chain_tx_count);
5556
0
}
5557
5558
Chainstate& ChainstateManager::InitializeChainstate(CTxMemPool* mempool)
5559
3.14k
{
5560
3.14k
    AssertLockHeld(::cs_main);
5561
3.14k
    assert(m_chainstates.empty());
  Branch (5561:5): [True: 3.14k, False: 0]
5562
3.14k
    m_chainstates.emplace_back(std::make_unique<Chainstate>(mempool, m_blockman, *this));
5563
3.14k
    return *m_chainstates.back();
5564
3.14k
}
5565
5566
[[nodiscard]] static bool DeleteCoinsDBFromDisk(const fs::path db_path, bool is_snapshot)
5567
    EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
5568
0
{
5569
0
    AssertLockHeld(::cs_main);
5570
5571
0
    if (is_snapshot) {
  Branch (5571:9): [True: 0, False: 0]
5572
0
        fs::path base_blockhash_path = db_path / node::SNAPSHOT_BLOCKHASH_FILENAME;
5573
5574
0
        try {
5575
0
            bool existed = fs::remove(base_blockhash_path);
5576
0
            if (!existed) {
  Branch (5576:17): [True: 0, False: 0]
5577
0
                LogWarning("[snapshot] snapshot chainstate dir being removed lacks %s file",
5578
0
                          fs::PathToString(node::SNAPSHOT_BLOCKHASH_FILENAME));
5579
0
            }
5580
0
        } catch (const fs::filesystem_error& e) {
5581
0
            LogWarning("[snapshot] failed to remove file %s: %s\n",
5582
0
                       fs::PathToString(base_blockhash_path), e.code().message());
5583
0
        }
5584
0
    }
5585
5586
0
    std::string path_str = fs::PathToString(db_path);
5587
0
    LogInfo("Removing leveldb dir at %s\n", path_str);
5588
5589
    // We have to destruct before this call leveldb::DB in order to release the db
5590
    // lock, otherwise `DestroyDB` will fail. See `leveldb::~DBImpl()`.
5591
0
    const bool destroyed = DestroyDB(path_str);
5592
5593
0
    if (!destroyed) {
  Branch (5593:9): [True: 0, False: 0]
5594
0
        LogError("leveldb DestroyDB call failed on %s", path_str);
5595
0
    }
5596
5597
    // Datadir should be removed from filesystem; otherwise initialization may detect
5598
    // it on subsequent statups and get confused.
5599
    //
5600
    // If the base_blockhash_path removal above fails in the case of snapshot
5601
    // chainstates, this will return false since leveldb won't remove a non-empty
5602
    // directory.
5603
0
    return destroyed && !fs::exists(db_path);
  Branch (5603:12): [True: 0, False: 0]
  Branch (5603:25): [True: 0, False: 0]
5604
0
}
5605
5606
util::Result<CBlockIndex*> ChainstateManager::ActivateSnapshot(
5607
        AutoFile& coins_file,
5608
        const SnapshotMetadata& metadata,
5609
        bool in_memory)
5610
2.35k
{
5611
2.35k
    uint256 base_blockhash = metadata.m_base_blockhash;
5612
5613
2.35k
    CBlockIndex* snapshot_start_block{};
5614
5615
2.35k
    {
5616
2.35k
        LOCK(::cs_main);
5617
5618
2.35k
        if (this->CurrentChainstate().m_from_snapshot_blockhash) {
  Branch (5618:13): [True: 46, False: 2.30k]
5619
46
            return util::Error{Untranslated("Can't activate a snapshot-based chainstate more than once")};
5620
46
        }
5621
2.30k
        if (!GetParams().AssumeutxoForBlockhash(base_blockhash).has_value()) {
  Branch (5621:13): [True: 158, False: 2.15k]
5622
158
            auto available_heights = GetParams().GetAvailableSnapshotHeights();
5623
474
            std::string heights_formatted = util::Join(available_heights, ", ", [&](const auto& i) { return util::ToString(i); });
5624
158
            return util::Error{Untranslated(strprintf("assumeutxo block hash in snapshot metadata not recognized (hash: %s). The following snapshot heights are available: %s",
5625
158
                base_blockhash.ToString(),
5626
158
                heights_formatted))};
5627
158
        }
5628
5629
2.15k
        snapshot_start_block = m_blockman.LookupBlockIndex(base_blockhash);
5630
2.15k
        if (!snapshot_start_block) {
  Branch (5630:13): [True: 22, False: 2.12k]
5631
22
            return util::Error{Untranslated(strprintf("The base block header (%s) must appear in the headers chain. Make sure all headers are syncing, and call loadtxoutset again",
5632
22
                          base_blockhash.ToString()))};
5633
22
        }
5634
5635
2.12k
        bool start_block_invalid = snapshot_start_block->nStatus & BLOCK_FAILED_VALID;
5636
2.12k
        if (start_block_invalid) {
  Branch (5636:13): [True: 0, False: 2.12k]
5637
0
            return util::Error{Untranslated(strprintf("The base block header (%s) is part of an invalid chain", base_blockhash.ToString()))};
5638
0
        }
5639
5640
2.12k
        if (!m_best_header || m_best_header->GetAncestor(snapshot_start_block->nHeight) != snapshot_start_block) {
  Branch (5640:13): [True: 0, False: 2.12k]
  Branch (5640:31): [True: 0, False: 2.12k]
5641
0
            return util::Error{Untranslated("A forked headers-chain with more work than the chain with the snapshot base block header exists. Please proceed to sync without AssumeUtxo.")};
5642
0
        }
5643
5644
2.12k
        auto mempool{CurrentChainstate().GetMempool()};
5645
2.12k
        if (mempool && mempool->size() > 0) {
  Branch (5645:13): [True: 2.12k, False: 0]
  Branch (5645:24): [True: 0, False: 2.12k]
5646
0
            return util::Error{Untranslated("Can't activate a snapshot when mempool not empty")};
5647
0
        }
5648
2.12k
    }
5649
5650
2.12k
    int64_t current_coinsdb_cache_size{0};
5651
2.12k
    int64_t current_coinstip_cache_size{0};
5652
5653
    // Cache percentages to allocate to each chainstate.
5654
    //
5655
    // These particular percentages don't matter so much since they will only be
5656
    // relevant during snapshot activation; caches are rebalanced at the conclusion of
5657
    // this function. We want to give (essentially) all available cache capacity to the
5658
    // snapshot to aid the bulk load later in this function.
5659
2.12k
    static constexpr double IBD_CACHE_PERC = 0.01;
5660
2.12k
    static constexpr double SNAPSHOT_CACHE_PERC = 0.99;
5661
5662
2.12k
    {
5663
2.12k
        LOCK(::cs_main);
5664
        // Resize the coins caches to ensure we're not exceeding memory limits.
5665
        //
5666
        // Allocate the majority of the cache to the incoming snapshot chainstate, since
5667
        // (optimistically) getting to its tip will be the top priority. We'll need to call
5668
        // `MaybeRebalanceCaches()` once we're done with this function to ensure
5669
        // the right allocation (including the possibility that no snapshot was activated
5670
        // and that we should restore the active chainstate caches to their original size).
5671
        //
5672
2.12k
        current_coinsdb_cache_size = this->ActiveChainstate().m_coinsdb_cache_size_bytes;
5673
2.12k
        current_coinstip_cache_size = this->ActiveChainstate().m_coinstip_cache_size_bytes;
5674
5675
        // Temporarily resize the active coins cache to make room for the newly-created
5676
        // snapshot chain.
5677
2.12k
        this->ActiveChainstate().ResizeCoinsCaches(
5678
2.12k
            static_cast<size_t>(current_coinstip_cache_size * IBD_CACHE_PERC),
5679
2.12k
            static_cast<size_t>(current_coinsdb_cache_size * IBD_CACHE_PERC));
5680
2.12k
    }
5681
5682
2.12k
    auto snapshot_chainstate = WITH_LOCK(::cs_main,
5683
2.12k
        return std::make_unique<Chainstate>(
5684
2.12k
            /*mempool=*/nullptr, m_blockman, *this, base_blockhash));
5685
5686
2.12k
    {
5687
2.12k
        LOCK(::cs_main);
5688
2.12k
        snapshot_chainstate->InitCoinsDB(
5689
2.12k
            static_cast<size_t>(current_coinsdb_cache_size * SNAPSHOT_CACHE_PERC),
5690
2.12k
            in_memory, /*should_wipe=*/false);
5691
2.12k
        snapshot_chainstate->InitCoinsCache(
5692
2.12k
            static_cast<size_t>(current_coinstip_cache_size * SNAPSHOT_CACHE_PERC));
5693
2.12k
    }
5694
5695
2.12k
    auto cleanup_bad_snapshot = [&](bilingual_str reason) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
5696
2.08k
        this->MaybeRebalanceCaches();
5697
5698
        // PopulateAndValidateSnapshot can return (in error) before the leveldb datadir
5699
        // has been created, so only attempt removal if we got that far.
5700
2.08k
        if (auto snapshot_datadir = node::FindAssumeutxoChainstateDir(m_options.datadir)) {
  Branch (5700:18): [True: 0, False: 2.08k]
5701
            // We have to destruct leveldb::DB in order to release the db lock, otherwise
5702
            // DestroyDB() (in DeleteCoinsDBFromDisk()) will fail. See `leveldb::~DBImpl()`.
5703
            // Destructing the chainstate (and so resetting the coinsviews object) does this.
5704
0
            snapshot_chainstate.reset();
5705
0
            bool removed = DeleteCoinsDBFromDisk(*snapshot_datadir, /*is_snapshot=*/true);
5706
0
            if (!removed) {
  Branch (5706:17): [True: 0, False: 0]
5707
0
                GetNotifications().fatalError(strprintf(_("Failed to remove snapshot chainstate dir (%s). "
5708
0
                    "Manually remove it before restarting.\n"), fs::PathToString(*snapshot_datadir)));
5709
0
            }
5710
0
        }
5711
2.08k
        return util::Error{std::move(reason)};
5712
2.08k
    };
5713
5714
2.12k
    if (auto res{this->PopulateAndValidateSnapshot(*snapshot_chainstate, coins_file, metadata)}; !res) {
  Branch (5714:98): [True: 2.08k, False: 46]
5715
2.08k
        LOCK(::cs_main);
5716
2.08k
        return cleanup_bad_snapshot(Untranslated(strprintf("Population failed: %s", util::ErrorString(res).original)));
5717
2.08k
    }
5718
5719
46
    LOCK(::cs_main);  // cs_main required for rest of snapshot activation.
5720
5721
    // Do a final check to ensure that the snapshot chainstate is actually a more
5722
    // work chain than the active chainstate; a user could have loaded a snapshot
5723
    // very late in the IBD process, and we wouldn't want to load a useless chainstate.
5724
46
    if (!CBlockIndexWorkComparator()(ActiveTip(), snapshot_chainstate->m_chain.Tip())) {
  Branch (5724:9): [True: 0, False: 46]
5725
0
        return cleanup_bad_snapshot(Untranslated("work does not exceed active chainstate"));
5726
0
    }
5727
    // If not in-memory, persist the base blockhash for use during subsequent
5728
    // initialization.
5729
46
    if (!in_memory) {
  Branch (5729:9): [True: 0, False: 46]
5730
0
        if (!node::WriteSnapshotBaseBlockhash(*snapshot_chainstate)) {
  Branch (5730:13): [True: 0, False: 0]
5731
0
            return cleanup_bad_snapshot(Untranslated("could not write base blockhash"));
5732
0
        }
5733
0
    }
5734
5735
46
    Chainstate& chainstate{AddChainstate(std::move(snapshot_chainstate))};
5736
46
    m_blockman.m_snapshot_height = Assert(chainstate.SnapshotBase())->nHeight;
5737
5738
46
    chainstate.PopulateBlockIndexCandidates();
5739
5740
46
    LogInfo("[snapshot] successfully activated snapshot %s", base_blockhash.ToString());
5741
46
    LogInfo("[snapshot] (%.2f MB)",
5742
46
              chainstate.CoinsTip().DynamicMemoryUsage() / (1000 * 1000));
5743
5744
46
    this->MaybeRebalanceCaches();
5745
46
    return snapshot_start_block;
5746
46
}
5747
5748
static void FlushSnapshotToDisk(CCoinsViewCache& coins_cache, bool snapshot_loaded)
5749
1.18k
{
5750
1.18k
    LOG_TIME_MILLIS_WITH_CATEGORY_MSG_ONCE(
5751
1.18k
        strprintf("%s (%.2f MB)",
5752
1.18k
                  snapshot_loaded ? "saving snapshot chainstate" : "flushing coins cache",
5753
1.18k
                  coins_cache.DynamicMemoryUsage() / (1000 * 1000)),
5754
1.18k
        BCLog::LogFlags::ALL);
5755
5756
1.18k
    coins_cache.Flush();
5757
1.18k
}
5758
5759
struct StopHashingException : public std::exception
5760
{
5761
    const char* what() const noexcept override
5762
0
    {
5763
0
        return "ComputeUTXOStats interrupted.";
5764
0
    }
5765
};
5766
5767
static void SnapshotUTXOHashBreakpoint(const util::SignalInterrupt& interrupt)
5768
56.9k
{
5769
56.9k
    if (interrupt) throw StopHashingException();
  Branch (5769:9): [True: 0, False: 56.9k]
5770
56.9k
}
5771
5772
util::Result<void> ChainstateManager::PopulateAndValidateSnapshot(
5773
    Chainstate& snapshot_chainstate,
5774
    AutoFile& coins_file,
5775
    const SnapshotMetadata& metadata)
5776
2.12k
{
5777
    // It's okay to release cs_main before we're done using `coins_cache` because we know
5778
    // that nothing else will be referencing the newly created snapshot_chainstate yet.
5779
2.12k
    CCoinsViewCache& coins_cache = *WITH_LOCK(::cs_main, return &snapshot_chainstate.CoinsTip());
5780
5781
2.12k
    uint256 base_blockhash = metadata.m_base_blockhash;
5782
5783
2.12k
    CBlockIndex* snapshot_start_block = WITH_LOCK(::cs_main, return m_blockman.LookupBlockIndex(base_blockhash));
5784
5785
2.12k
    if (!snapshot_start_block) {
  Branch (5785:9): [True: 0, False: 2.12k]
5786
        // Needed for ComputeUTXOStats to determine the
5787
        // height and to avoid a crash when base_blockhash.IsNull()
5788
0
        return util::Error{Untranslated(strprintf("Did not find snapshot start blockheader %s",
5789
0
                  base_blockhash.ToString()))};
5790
0
    }
5791
5792
2.12k
    int base_height = snapshot_start_block->nHeight;
5793
2.12k
    const auto& maybe_au_data = GetParams().AssumeutxoForHeight(base_height);
5794
5795
2.12k
    if (!maybe_au_data) {
  Branch (5795:9): [True: 0, False: 2.12k]
5796
0
        return util::Error{Untranslated(strprintf("Assumeutxo height in snapshot metadata not recognized "
5797
0
                  "(%d) - refusing to load snapshot", base_height))};
5798
0
    }
5799
5800
2.12k
    const AssumeutxoData& au_data = *maybe_au_data;
5801
5802
    // This work comparison is a duplicate check with the one performed later in
5803
    // ActivateSnapshot(), but is done so that we avoid doing the long work of staging
5804
    // a snapshot that isn't actually usable.
5805
2.12k
    if (WITH_LOCK(::cs_main, return !CBlockIndexWorkComparator()(ActiveTip(), snapshot_start_block))) {
5806
0
        return util::Error{Untranslated("Work does not exceed active chainstate")};
5807
0
    }
5808
5809
2.12k
    const uint64_t coins_count = metadata.m_coins_count;
5810
2.12k
    uint64_t coins_left = metadata.m_coins_count;
5811
5812
2.12k
    LogInfo("[snapshot] loading %d coins from snapshot %s", coins_left, base_blockhash.ToString());
5813
2.12k
    int64_t coins_processed{0};
5814
5815
81.7k
    while (coins_left > 0) {
  Branch (5815:12): [True: 80.4k, False: 1.27k]
5816
80.4k
        try {
5817
80.4k
            Txid txid;
5818
80.4k
            coins_file >> txid;
5819
80.4k
            size_t coins_per_txid{0};
5820
80.4k
            coins_per_txid = ReadCompactSize(coins_file);
5821
5822
80.4k
            if (coins_per_txid > coins_left) {
  Branch (5822:17): [True: 76, False: 80.3k]
5823
76
                return util::Error{Untranslated("Mismatch in coins count in snapshot metadata and actual snapshot data")};
5824
76
            }
5825
5826
1.62M
            for (size_t i = 0; i < coins_per_txid; i++) {
  Branch (5826:32): [True: 1.54M, False: 80.1k]
5827
1.54M
                COutPoint outpoint;
5828
1.54M
                Coin coin;
5829
1.54M
                outpoint.n = static_cast<uint32_t>(ReadCompactSize(coins_file));
5830
1.54M
                outpoint.hash = txid;
5831
1.54M
                coins_file >> coin;
5832
1.54M
                if (coin.nHeight > base_height ||
  Branch (5832:21): [True: 642, False: 1.54M]
5833
1.54M
                    outpoint.n >= std::numeric_limits<decltype(outpoint.n)>::max() // Avoid integer wrap-around in coinstats.cpp:ApplyHash
  Branch (5833:21): [True: 0, False: 1.54M]
5834
1.54M
                ) {
5835
158
                    return util::Error{Untranslated(strprintf("Bad snapshot data after deserializing %d coins",
5836
158
                              coins_count - coins_left))};
5837
158
                }
5838
1.54M
                if (!MoneyRange(coin.out.nValue)) {
  Branch (5838:21): [True: 44, False: 1.54M]
5839
44
                    return util::Error{Untranslated(strprintf("Bad snapshot data after deserializing %d coins - bad tx out value",
5840
44
                              coins_count - coins_left))};
5841
44
                }
5842
1.54M
                coins_cache.EmplaceCoinInternalDANGER(outpoint, std::move(coin));
5843
5844
1.54M
                --coins_left;
5845
1.54M
                ++coins_processed;
5846
5847
1.54M
                if (coins_processed % 1000000 == 0) {
  Branch (5847:21): [True: 0, False: 1.54M]
5848
0
                    LogInfo("[snapshot] %d coins loaded (%.2f%%, %.2f MB)",
5849
0
                        coins_processed,
5850
0
                        static_cast<float>(coins_processed) * 100 / static_cast<float>(coins_count),
5851
0
                        coins_cache.DynamicMemoryUsage() / (1000 * 1000));
5852
0
                }
5853
5854
                // Batch write and flush (if we need to) every so often.
5855
                //
5856
                // If our average Coin size is roughly 41 bytes, checking every 120,000 coins
5857
                // means <5MB of memory imprecision.
5858
1.54M
                if (coins_processed % 120000 == 0) {
  Branch (5858:21): [True: 6, False: 1.54M]
5859
6
                    if (m_interrupt) {
  Branch (5859:25): [True: 0, False: 6]
5860
0
                        return util::Error{Untranslated("Aborting after an interrupt was requested")};
5861
0
                    }
5862
5863
6
                    const auto snapshot_cache_state = WITH_LOCK(::cs_main,
5864
6
                        return snapshot_chainstate.GetCoinsCacheSizeState());
5865
5866
6
                    if (snapshot_cache_state >= CoinsCacheSizeState::CRITICAL) {
  Branch (5866:25): [True: 0, False: 6]
5867
                        // This is a hack - we don't know what the actual best block is, but that
5868
                        // doesn't matter for the purposes of flushing the cache here. We'll set this
5869
                        // to its correct value (`base_blockhash`) below after the coins are loaded.
5870
0
                        coins_cache.SetBestBlock(GetRandHash());
5871
5872
                        // No need to acquire cs_main since this chainstate isn't being used yet.
5873
0
                        FlushSnapshotToDisk(coins_cache, /*snapshot_loaded=*/false);
5874
0
                    }
5875
6
                }
5876
1.54M
            }
5877
80.3k
        } catch (const std::ios_base::failure&) {
5878
572
            return util::Error{Untranslated(strprintf("Bad snapshot format or truncated snapshot after deserializing %d coins",
5879
572
                      coins_processed))};
5880
572
        }
5881
80.4k
    }
5882
5883
    // Important that we set this. This and the coins_cache accesses above are
5884
    // sort of a layer violation, but either we reach into the innards of
5885
    // CCoinsViewCache here or we have to invert some of the Chainstate to
5886
    // embed them in a snapshot-activation-specific CCoinsViewCache bulk load
5887
    // method.
5888
1.27k
    coins_cache.SetBestBlock(base_blockhash);
5889
5890
1.27k
    bool out_of_coins{false};
5891
1.27k
    try {
5892
1.27k
        std::byte left_over_byte;
5893
1.27k
        coins_file >> left_over_byte;
5894
1.27k
    } catch (const std::ios_base::failure&) {
5895
        // We expect an exception since we should be out of coins.
5896
1.18k
        out_of_coins = true;
5897
1.18k
    }
5898
1.27k
    if (!out_of_coins) {
  Branch (5898:9): [True: 96, False: 1.18k]
5899
96
        return util::Error{Untranslated(strprintf("Bad snapshot - coins left over after deserializing %d coins",
5900
96
            coins_count))};
5901
96
    }
5902
5903
1.18k
    LogInfo("[snapshot] loaded %d (%.2f MB) coins from snapshot %s",
5904
1.18k
        coins_count,
5905
1.18k
        coins_cache.DynamicMemoryUsage() / (1000 * 1000),
5906
1.18k
        base_blockhash.ToString());
5907
5908
    // No need to acquire cs_main since this chainstate isn't being used yet.
5909
1.18k
    FlushSnapshotToDisk(coins_cache, /*snapshot_loaded=*/true);
5910
5911
1.18k
    assert(coins_cache.GetBestBlock() == base_blockhash);
  Branch (5911:5): [True: 1.18k, False: 0]
5912
5913
    // As above, okay to immediately release cs_main here since no other context knows
5914
    // about the snapshot_chainstate.
5915
1.18k
    const CCoinsViewDB& snapshot_coinsdb = WITH_LOCK(::cs_main, return snapshot_chainstate.CoinsDB());
5916
5917
1.18k
    std::optional<CCoinsStats> maybe_stats;
5918
5919
1.18k
    try {
5920
1.18k
        maybe_stats = ComputeUTXOStats(
5921
56.9k
            CoinStatsHashType::HASH_SERIALIZED, snapshot_coinsdb, m_blockman, [&interrupt = m_interrupt] { SnapshotUTXOHashBreakpoint(interrupt); });
5922
1.18k
    } catch (StopHashingException const&) {
5923
0
        return util::Error{Untranslated("Aborting after an interrupt was requested")};
5924
0
    }
5925
1.18k
    if (!maybe_stats.has_value()) {
  Branch (5925:9): [True: 0, False: 1.18k]
5926
0
        return util::Error{Untranslated("Failed to generate coins stats")};
5927
0
    }
5928
5929
    // Assert that the deserialized chainstate contents match the expected assumeutxo value.
5930
1.18k
    if (AssumeutxoHash{maybe_stats->hashSerialized} != au_data.hash_serialized) {
  Branch (5930:9): [True: 1.13k, False: 46]
5931
1.13k
        return util::Error{Untranslated(strprintf("Bad snapshot content hash: expected %s, got %s",
5932
1.13k
            au_data.hash_serialized.ToString(), maybe_stats->hashSerialized.ToString()))};
5933
1.13k
    }
5934
5935
46
    snapshot_chainstate.m_chain.SetTip(*snapshot_start_block);
5936
5937
    // The remainder of this function requires modifying data protected by cs_main.
5938
46
    LOCK(::cs_main);
5939
5940
    // Fake various pieces of CBlockIndex state:
5941
46
    CBlockIndex* index = nullptr;
5942
5943
    // Don't make any modifications to the genesis block since it shouldn't be
5944
    // necessary, and since the genesis block doesn't have normal flags like
5945
    // BLOCK_VALID_SCRIPTS set.
5946
46
    constexpr int AFTER_GENESIS_START{1};
5947
5948
9.24k
    for (int i = AFTER_GENESIS_START; i <= snapshot_chainstate.m_chain.Height(); ++i) {
  Branch (5948:39): [True: 9.20k, False: 46]
5949
9.20k
        index = snapshot_chainstate.m_chain[i];
5950
5951
        // Fake BLOCK_OPT_WITNESS so that Chainstate::NeedsRedownload()
5952
        // won't ask for -reindex on startup.
5953
9.20k
        if (DeploymentActiveAt(*index, *this, Consensus::DEPLOYMENT_SEGWIT)) {
  Branch (5953:13): [True: 9.20k, False: 0]
5954
9.20k
            index->nStatus |= BLOCK_OPT_WITNESS;
5955
9.20k
        }
5956
5957
9.20k
        m_blockman.m_dirty_blockindex.insert(index);
5958
        // Changes to the block index will be flushed to disk after this call
5959
        // returns in `ActivateSnapshot()`, when `MaybeRebalanceCaches()` is
5960
        // called, since we've added a snapshot chainstate and therefore will
5961
        // have to downsize the IBD chainstate, which will result in a call to
5962
        // `FlushStateToDisk(FORCE_FLUSH)`.
5963
9.20k
    }
5964
5965
46
    assert(index);
  Branch (5965:5): [True: 46, False: 0]
5966
46
    assert(index == snapshot_start_block);
  Branch (5966:5): [True: 46, False: 0]
5967
46
    index->m_chain_tx_count = au_data.m_chain_tx_count;
5968
5969
46
    LogInfo("[snapshot] validated snapshot (%.2f MB)",
5970
46
        coins_cache.DynamicMemoryUsage() / (1000 * 1000));
5971
46
    return {};
5972
46
}
5973
5974
// Currently, this function holds cs_main for its duration, which could be for
5975
// multiple minutes due to the ComputeUTXOStats call. Holding cs_main used to be
5976
// necessary (before d43a1f1a2fa3) to avoid advancing validated_cs farther than
5977
// its target block. Now it should be possible to avoid this, but simply
5978
// releasing cs_main here would not be possible because this function is invoked
5979
// by ConnectTip within ActivateBestChain.
5980
//
5981
// Eventually (TODO) it would be better to call this function outside of
5982
// ActivateBestChain, on a separate thread that should not require cs_main to
5983
// hash, because the UTXO set is only hashed after the historical chainstate
5984
// reaches its target block and is no longer changing.
5985
SnapshotCompletionResult ChainstateManager::MaybeValidateSnapshot(Chainstate& validated_cs, Chainstate& unvalidated_cs)
5986
344k
{
5987
344k
    AssertLockHeld(cs_main);
5988
5989
    // If the snapshot does not need to be validated...
5990
344k
    if (unvalidated_cs.m_assumeutxo != Assumeutxo::UNVALIDATED ||
  Branch (5990:9): [True: 344k, False: 0]
5991
            // Or if either chainstate is unusable...
5992
344k
            !unvalidated_cs.m_from_snapshot_blockhash ||
  Branch (5992:13): [True: 0, False: 0]
5993
344k
            validated_cs.m_assumeutxo != Assumeutxo::VALIDATED ||
  Branch (5993:13): [True: 0, False: 0]
5994
344k
            !validated_cs.m_chain.Tip() ||
  Branch (5994:13): [True: 0, False: 0]
5995
            // Or the validated chainstate is not targeting the snapshot block...
5996
344k
            !validated_cs.m_target_blockhash ||
  Branch (5996:13): [True: 0, False: 0]
5997
344k
            *validated_cs.m_target_blockhash != *unvalidated_cs.m_from_snapshot_blockhash ||
  Branch (5997:13): [True: 0, False: 0]
5998
            // Or the validated chainstate has not reached the snapshot block yet...
5999
344k
            !validated_cs.ReachedTarget()) {
  Branch (5999:13): [True: 0, False: 0]
6000
       // Then the snapshot cannot be validated and there is nothing to do.
6001
344k
       return SnapshotCompletionResult::SKIPPED;
6002
344k
    }
6003
344k
    assert(validated_cs.TargetBlock() == validated_cs.m_chain.Tip());
  Branch (6003:5): [True: 0, False: 0]
6004
6005
0
    auto handle_invalid_snapshot = [&]() EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
6006
0
        bilingual_str user_error = strprintf(_(
6007
0
            "%s failed to validate the -assumeutxo snapshot state. "
6008
0
            "This indicates a hardware problem, or a bug in the software, or a "
6009
0
            "bad software modification that allowed an invalid snapshot to be "
6010
0
            "loaded. As a result of this, the node will shut down and stop using any "
6011
0
            "state that was built on the snapshot, resetting the chain height "
6012
0
            "from %d to %d. On the next "
6013
0
            "restart, the node will resume syncing from %d "
6014
0
            "without using any snapshot data. "
6015
0
            "Please report this incident to %s, including how you obtained the snapshot. "
6016
0
            "The invalid snapshot chainstate will be left on disk in case it is "
6017
0
            "helpful in diagnosing the issue that caused this error."),
6018
0
            CLIENT_NAME, unvalidated_cs.m_chain.Height(),
6019
0
            validated_cs.m_chain.Height(),
6020
0
            validated_cs.m_chain.Height(), CLIENT_BUGREPORT);
6021
6022
0
        LogError("[snapshot] !!! %s\n", user_error.original);
6023
0
        LogError("[snapshot] deleting snapshot, reverting to validated chain, and stopping node\n");
6024
6025
        // Reset chainstate target to network tip instead of snapshot block.
6026
0
        validated_cs.SetTargetBlock(nullptr);
6027
6028
0
        unvalidated_cs.m_assumeutxo = Assumeutxo::INVALID;
6029
6030
0
        auto rename_result = unvalidated_cs.InvalidateCoinsDBOnDisk();
6031
0
        if (!rename_result) {
  Branch (6031:13): [True: 0, False: 0]
6032
0
            user_error += Untranslated("\n") + util::ErrorString(rename_result);
6033
0
        }
6034
6035
0
        GetNotifications().fatalError(user_error);
6036
0
    };
6037
6038
0
    CCoinsViewDB& validated_coins_db = validated_cs.CoinsDB();
6039
0
    validated_cs.ForceFlushStateToDisk();
6040
6041
0
    const auto& maybe_au_data = m_options.chainparams.AssumeutxoForHeight(validated_cs.m_chain.Height());
6042
0
    if (!maybe_au_data) {
  Branch (6042:9): [True: 0, False: 0]
6043
0
        LogWarning("[snapshot] assumeutxo data not found for height "
6044
0
            "(%d) - refusing to validate snapshot", validated_cs.m_chain.Height());
6045
0
        handle_invalid_snapshot();
6046
0
        return SnapshotCompletionResult::MISSING_CHAINPARAMS;
6047
0
    }
6048
6049
0
    const AssumeutxoData& au_data = *maybe_au_data;
6050
0
    std::optional<CCoinsStats> validated_cs_stats;
6051
0
    LogInfo("[snapshot] computing UTXO stats for background chainstate to validate "
6052
0
        "snapshot - this could take a few minutes");
6053
0
    try {
6054
0
        validated_cs_stats = ComputeUTXOStats(
6055
0
            CoinStatsHashType::HASH_SERIALIZED,
6056
0
            validated_coins_db,
6057
0
            m_blockman,
6058
0
            [&interrupt = m_interrupt] { SnapshotUTXOHashBreakpoint(interrupt); });
6059
0
    } catch (StopHashingException const&) {
6060
0
        return SnapshotCompletionResult::STATS_FAILED;
6061
0
    }
6062
6063
    // XXX note that this function is slow and will hold cs_main for potentially minutes.
6064
0
    if (!validated_cs_stats) {
  Branch (6064:9): [True: 0, False: 0]
6065
0
        LogWarning("[snapshot] failed to generate stats for validation coins db");
6066
        // While this isn't a problem with the snapshot per se, this condition
6067
        // prevents us from validating the snapshot, so we should shut down and let the
6068
        // user handle the issue manually.
6069
0
        handle_invalid_snapshot();
6070
0
        return SnapshotCompletionResult::STATS_FAILED;
6071
0
    }
6072
6073
    // Compare the validated chainstate's UTXO set hash against the hard-coded
6074
    // assumeutxo hash we expect.
6075
    //
6076
    // TODO: For belt-and-suspenders, we could cache the UTXO set
6077
    // hash for the snapshot when it's loaded in its chainstate's leveldb. We could then
6078
    // reference that here for an additional check.
6079
0
    if (AssumeutxoHash{validated_cs_stats->hashSerialized} != au_data.hash_serialized) {
  Branch (6079:9): [True: 0, False: 0]
6080
0
        LogWarning("[snapshot] hash mismatch: actual=%s, expected=%s",
6081
0
            validated_cs_stats->hashSerialized.ToString(),
6082
0
            au_data.hash_serialized.ToString());
6083
0
        handle_invalid_snapshot();
6084
0
        return SnapshotCompletionResult::HASH_MISMATCH;
6085
0
    }
6086
6087
0
    LogInfo("[snapshot] snapshot beginning at %s has been fully validated",
6088
0
        unvalidated_cs.m_from_snapshot_blockhash->ToString());
6089
6090
0
    unvalidated_cs.m_assumeutxo = Assumeutxo::VALIDATED;
6091
0
    validated_cs.m_target_utxohash = AssumeutxoHash{validated_cs_stats->hashSerialized};
6092
0
    this->MaybeRebalanceCaches();
6093
6094
0
    return SnapshotCompletionResult::SUCCESS;
6095
0
}
6096
6097
Chainstate& ChainstateManager::ActiveChainstate() const
6098
7.92M
{
6099
7.92M
    LOCK(::cs_main);
6100
7.92M
    return CurrentChainstate();
6101
7.92M
}
6102
6103
void ChainstateManager::MaybeRebalanceCaches()
6104
7.82k
{
6105
7.82k
    AssertLockHeld(::cs_main);
6106
7.82k
    Chainstate& current_cs{CurrentChainstate()};
6107
7.82k
    Chainstate* historical_cs{HistoricalChainstate()};
6108
7.82k
    if (!historical_cs && !current_cs.m_from_snapshot_blockhash) {
  Branch (6108:9): [True: 7.77k, False: 46]
  Branch (6108:27): [True: 7.77k, False: 0]
6109
        // Allocate everything to the IBD chainstate. This will always happen
6110
        // when we are not using a snapshot.
6111
7.77k
        current_cs.ResizeCoinsCaches(m_total_coinstip_cache, m_total_coinsdb_cache);
6112
7.77k
    } else if (!historical_cs) {
  Branch (6112:16): [True: 0, False: 46]
6113
        // If background validation has completed and snapshot is our active chain...
6114
0
        LogInfo("[snapshot] allocating all cache to the snapshot chainstate");
6115
        // Allocate everything to the snapshot chainstate.
6116
0
        current_cs.ResizeCoinsCaches(m_total_coinstip_cache, m_total_coinsdb_cache);
6117
46
    } else {
6118
        // If both chainstates exist, determine who needs more cache based on IBD status.
6119
        //
6120
        // Note: shrink caches first so that we don't inadvertently overwhelm available memory.
6121
46
        if (IsInitialBlockDownload()) {
  Branch (6121:13): [True: 37, False: 9]
6122
37
            historical_cs->ResizeCoinsCaches(
6123
37
                m_total_coinstip_cache * 0.05, m_total_coinsdb_cache * 0.05);
6124
37
            current_cs.ResizeCoinsCaches(
6125
37
                m_total_coinstip_cache * 0.95, m_total_coinsdb_cache * 0.95);
6126
37
        } else {
6127
9
            current_cs.ResizeCoinsCaches(
6128
9
                m_total_coinstip_cache * 0.05, m_total_coinsdb_cache * 0.05);
6129
9
            historical_cs->ResizeCoinsCaches(
6130
9
                m_total_coinstip_cache * 0.95, m_total_coinsdb_cache * 0.95);
6131
9
        }
6132
46
    }
6133
7.82k
}
6134
6135
void ChainstateManager::ResetChainstates()
6136
0
{
6137
0
    m_chainstates.clear();
6138
0
}
6139
6140
/**
6141
 * Apply default chain params to nullopt members.
6142
 * This helps to avoid coding errors around the accidental use of the compare
6143
 * operators that accept nullopt, thus ignoring the intended default value.
6144
 */
6145
static ChainstateManager::Options&& Flatten(ChainstateManager::Options&& opts)
6146
3.14k
{
6147
3.14k
    if (!opts.check_block_index.has_value()) opts.check_block_index = opts.chainparams.DefaultConsistencyChecks();
  Branch (6147:9): [True: 0, False: 3.14k]
6148
3.14k
    if (!opts.minimum_chain_work.has_value()) opts.minimum_chain_work = UintToArith256(opts.chainparams.GetConsensus().nMinimumChainWork);
  Branch (6148:9): [True: 3.14k, False: 0]
6149
3.14k
    if (!opts.assumed_valid_block.has_value()) opts.assumed_valid_block = opts.chainparams.GetConsensus().defaultAssumeValid;
  Branch (6149:9): [True: 3.14k, False: 0]
6150
3.14k
    return std::move(opts);
6151
3.14k
}
6152
6153
ChainstateManager::ChainstateManager(const util::SignalInterrupt& interrupt, Options options, node::BlockManager::Options blockman_options)
6154
3.14k
    : m_script_check_queue{/*batch_size=*/128, std::clamp(options.worker_threads_num, 0, MAX_SCRIPTCHECK_THREADS)},
6155
3.14k
      m_interrupt{interrupt},
6156
3.14k
      m_options{Flatten(std::move(options))},
6157
3.14k
      m_blockman{interrupt, std::move(blockman_options)},
6158
3.14k
      m_validation_cache{m_options.script_execution_cache_bytes, m_options.signature_cache_bytes}
6159
3.14k
{
6160
3.14k
}
6161
6162
ChainstateManager::~ChainstateManager()
6163
3.17k
{
6164
3.17k
    LOCK(::cs_main);
6165
6166
3.17k
    m_versionbitscache.Clear();
6167
3.17k
}
6168
6169
Chainstate* ChainstateManager::LoadAssumeutxoChainstate()
6170
3.14k
{
6171
3.14k
    assert(!CurrentChainstate().m_from_snapshot_blockhash);
  Branch (6171:5): [True: 3.14k, False: 0]
6172
3.14k
    std::optional<fs::path> path = node::FindAssumeutxoChainstateDir(m_options.datadir);
6173
3.14k
    if (!path) {
  Branch (6173:9): [True: 3.14k, False: 0]
6174
3.14k
        return nullptr;
6175
3.14k
    }
6176
0
    std::optional<uint256> base_blockhash = node::ReadSnapshotBaseBlockhash(*path);
6177
0
    if (!base_blockhash) {
  Branch (6177:9): [True: 0, False: 0]
6178
0
        return nullptr;
6179
0
    }
6180
0
    LogInfo("[snapshot] detected active snapshot chainstate (%s) - loading",
6181
0
        fs::PathToString(*path));
6182
6183
0
    auto snapshot_chainstate{std::make_unique<Chainstate>(nullptr, m_blockman, *this, base_blockhash)};
6184
0
    LogInfo("[snapshot] switching active chainstate to %s", snapshot_chainstate->ToString());
6185
0
    return &this->AddChainstate(std::move(snapshot_chainstate));
6186
0
}
6187
6188
Chainstate& ChainstateManager::AddChainstate(std::unique_ptr<Chainstate> chainstate)
6189
46
{
6190
46
    Chainstate& prev_chainstate{CurrentChainstate()};
6191
46
    assert(prev_chainstate.m_assumeutxo == Assumeutxo::VALIDATED);
  Branch (6191:5): [True: 46, False: 0]
6192
    // Set target block for historical chainstate to snapshot block.
6193
46
    assert(!prev_chainstate.m_target_blockhash);
  Branch (6193:5): [True: 46, False: 0]
6194
46
    prev_chainstate.m_target_blockhash = chainstate->m_from_snapshot_blockhash;
6195
46
    m_chainstates.push_back(std::move(chainstate));
6196
46
    Chainstate& curr_chainstate{CurrentChainstate()};
6197
46
    assert(&curr_chainstate == m_chainstates.back().get());
  Branch (6197:5): [True: 46, False: 0]
6198
6199
    // Transfer possession of the mempool to the chainstate.
6200
    // Mempool is empty at this point because we're still in IBD.
6201
46
    assert(!prev_chainstate.m_mempool || prev_chainstate.m_mempool->size() == 0);
  Branch (6201:5): [True: 0, False: 46]
  Branch (6201:5): [True: 46, False: 0]
  Branch (6201:5): [True: 46, False: 0]
6202
46
    assert(!curr_chainstate.m_mempool);
  Branch (6202:5): [True: 46, False: 0]
6203
46
    std::swap(curr_chainstate.m_mempool, prev_chainstate.m_mempool);
6204
46
    return curr_chainstate;
6205
46
}
6206
6207
bool IsBIP30Repeat(const CBlockIndex& block_index)
6208
732k
{
6209
732k
    return (block_index.nHeight==91842 && block_index.GetBlockHash() == uint256{"00000000000a4d0a398161ffc163c503763b1f4360639393e0e4c8e300e0caec"}) ||
  Branch (6209:13): [True: 0, False: 732k]
  Branch (6209:43): [True: 0, False: 0]
6210
732k
           (block_index.nHeight==91880 && block_index.GetBlockHash() == uint256{"00000000000743f190a18c5577a3c2d2a1f610ae9601ac046a38084ccb7cd721"});
  Branch (6210:13): [True: 0, False: 732k]
  Branch (6210:43): [True: 0, False: 0]
6211
732k
}
6212
6213
bool IsBIP30Unspendable(const uint256& block_hash, int block_height)
6214
0
{
6215
0
    return (block_height==91722 && block_hash == uint256{"00000000000271a2dc26e7667f8419f2e15416dc6955e5a6c6cdf3f2574dd08e"}) ||
  Branch (6215:13): [True: 0, False: 0]
  Branch (6215:36): [True: 0, False: 0]
6216
0
           (block_height==91812 && block_hash == uint256{"00000000000af0aed4792b1acee3d966af36cf5def14935db8de83d6f9306f2f"});
  Branch (6216:13): [True: 0, False: 0]
  Branch (6216:36): [True: 0, False: 0]
6217
0
}
6218
6219
util::Result<void> Chainstate::InvalidateCoinsDBOnDisk()
6220
0
{
6221
    // Should never be called on a non-snapshot chainstate.
6222
0
    assert(m_from_snapshot_blockhash);
  Branch (6222:5): [True: 0, False: 0]
6223
6224
    // Coins views no longer usable.
6225
0
    m_coins_views.reset();
6226
6227
0
    const fs::path db_path{StoragePath()};
6228
0
    const fs::path invalid_path{db_path + "_INVALID"};
6229
0
    const std::string db_path_str{fs::PathToString(db_path)};
6230
0
    const std::string invalid_path_str{fs::PathToString(invalid_path)};
6231
0
    LogInfo("[snapshot] renaming snapshot datadir %s to %s", db_path_str, invalid_path_str);
6232
6233
    // The invalid storage directory is simply moved and not deleted because we may
6234
    // want to do forensics later during issue investigation. The user is instructed
6235
    // accordingly in MaybeValidateSnapshot().
6236
0
    try {
6237
0
        fs::rename(db_path, invalid_path);
6238
0
    } catch (const fs::filesystem_error& e) {
6239
0
        LogError("While invalidating the coins db: Error renaming file '%s' -> '%s': %s",
6240
0
                 db_path_str, invalid_path_str, e.what());
6241
0
        return util::Error{strprintf(_(
6242
0
            "Rename of '%s' -> '%s' failed. "
6243
0
            "You should resolve this by manually moving or deleting the invalid "
6244
0
            "snapshot directory %s, otherwise you will encounter the same error again "
6245
0
            "on the next startup."),
6246
0
            db_path_str, invalid_path_str, db_path_str)};
6247
0
    }
6248
0
    return {};
6249
0
}
6250
6251
bool ChainstateManager::DeleteChainstate(Chainstate& chainstate)
6252
0
{
6253
0
    AssertLockHeld(::cs_main);
6254
0
    assert(!chainstate.m_coins_views);
  Branch (6254:5): [True: 0, False: 0]
6255
0
    const fs::path db_path{chainstate.StoragePath()};
6256
0
    if (!DeleteCoinsDBFromDisk(db_path, /*is_snapshot=*/bool{chainstate.m_from_snapshot_blockhash})) {
  Branch (6256:9): [True: 0, False: 0]
6257
0
        LogError("Deletion of %s failed. Please remove it manually to continue reindexing.",
6258
0
                  fs::PathToString(db_path));
6259
0
        return false;
6260
0
    }
6261
0
    std::unique_ptr<Chainstate> prev_chainstate{Assert(RemoveChainstate(chainstate))};
6262
0
    Chainstate& curr_chainstate{CurrentChainstate()};
6263
0
    assert(!prev_chainstate->m_mempool || prev_chainstate->m_mempool->size() == 0);
  Branch (6263:5): [True: 0, False: 0]
  Branch (6263:5): [True: 0, False: 0]
  Branch (6263:5): [True: 0, False: 0]
6264
0
    assert(!curr_chainstate.m_mempool);
  Branch (6264:5): [True: 0, False: 0]
6265
0
    std::swap(curr_chainstate.m_mempool, prev_chainstate->m_mempool);
6266
0
    return true;
6267
0
}
6268
6269
ChainstateRole Chainstate::GetRole() const
6270
1.59M
{
6271
1.59M
    return ChainstateRole{.validated = m_assumeutxo == Assumeutxo::VALIDATED, .historical = bool{m_target_blockhash}};
6272
1.59M
}
6273
6274
void ChainstateManager::RecalculateBestHeader()
6275
9.75k
{
6276
9.75k
    AssertLockHeld(cs_main);
6277
9.75k
    m_best_header = ActiveChain().Tip();
6278
486k
    for (auto& entry : m_blockman.m_block_index) {
  Branch (6278:22): [True: 486k, False: 9.75k]
6279
486k
        if (!(entry.second.nStatus & BLOCK_FAILED_VALID) && m_best_header->nChainWork < entry.second.nChainWork) {
  Branch (6279:13): [True: 347k, False: 138k]
  Branch (6279:61): [True: 656, False: 347k]
6280
656
            m_best_header = &entry.second;
6281
656
        }
6282
486k
    }
6283
9.75k
}
6284
6285
std::optional<int> ChainstateManager::BlocksAheadOfTip() const
6286
0
{
6287
0
    LOCK(::cs_main);
6288
0
    const CBlockIndex* best_header{m_best_header};
6289
0
    const CBlockIndex* tip{ActiveChain().Tip()};
6290
    // Only consider headers that extend the active tip; ignore competing branches.
6291
0
    if (best_header && tip && best_header->nChainWork > tip->nChainWork &&
  Branch (6291:9): [True: 0, False: 0]
  Branch (6291:24): [True: 0, False: 0]
  Branch (6291:31): [True: 0, False: 0]
6292
0
        best_header->GetAncestor(tip->nHeight) == tip) {
  Branch (6292:9): [True: 0, False: 0]
6293
0
        return best_header->nHeight - tip->nHeight;
6294
0
    }
6295
0
    return std::nullopt;
6296
0
}
6297
6298
bool ChainstateManager::ValidatedSnapshotCleanup(Chainstate& validated_cs, Chainstate& unvalidated_cs)
6299
0
{
6300
0
    AssertLockHeld(::cs_main);
6301
0
    if (unvalidated_cs.m_assumeutxo != Assumeutxo::VALIDATED) {
  Branch (6301:9): [True: 0, False: 0]
6302
        // No need to clean up.
6303
0
        return false;
6304
0
    }
6305
6306
0
    const fs::path validated_path{validated_cs.StoragePath()};
6307
0
    const fs::path assumed_valid_path{unvalidated_cs.StoragePath()};
6308
0
    const fs::path delete_path{validated_path + "_todelete"};
6309
6310
    // Since we're going to be moving around the underlying leveldb filesystem content
6311
    // for each chainstate, make sure that the chainstates (and their constituent
6312
    // CoinsViews members) have been destructed first.
6313
    //
6314
    // The caller of this method will be responsible for reinitializing chainstates
6315
    // if they want to continue operation.
6316
0
    this->ResetChainstates();
6317
0
    assert(this->m_chainstates.size() == 0);
  Branch (6317:5): [True: 0, False: 0]
6318
6319
0
    LogInfo("[snapshot] deleting background chainstate directory (now unnecessary) (%s)",
6320
0
              fs::PathToString(validated_path));
6321
6322
0
    auto rename_failed_abort = [this](
6323
0
                                   fs::path p_old,
6324
0
                                   fs::path p_new,
6325
0
                                   const fs::filesystem_error& err) {
6326
0
        LogError("[snapshot] Error renaming path (%s) -> (%s): %s\n",
6327
0
                  fs::PathToString(p_old), fs::PathToString(p_new), err.what());
6328
0
        GetNotifications().fatalError(strprintf(_(
6329
0
            "Rename of '%s' -> '%s' failed. "
6330
0
            "Cannot clean up the background chainstate leveldb directory."),
6331
0
            fs::PathToString(p_old), fs::PathToString(p_new)));
6332
0
    };
6333
6334
0
    try {
6335
0
        fs::rename(validated_path, delete_path);
6336
0
    } catch (const fs::filesystem_error& e) {
6337
0
        rename_failed_abort(validated_path, delete_path, e);
6338
0
        throw;
6339
0
    }
6340
6341
0
    LogInfo("[snapshot] moving snapshot chainstate (%s) to "
6342
0
              "default chainstate directory (%s)",
6343
0
              fs::PathToString(assumed_valid_path), fs::PathToString(validated_path));
6344
6345
0
    try {
6346
0
        fs::rename(assumed_valid_path, validated_path);
6347
0
    } catch (const fs::filesystem_error& e) {
6348
0
        rename_failed_abort(assumed_valid_path, validated_path, e);
6349
0
        throw;
6350
0
    }
6351
6352
0
    if (!DeleteCoinsDBFromDisk(delete_path, /*is_snapshot=*/false)) {
  Branch (6352:9): [True: 0, False: 0]
6353
        // No need to FatalError because once the unneeded bg chainstate data is
6354
        // moved, it will not interfere with subsequent initialization.
6355
0
        LogWarning("Deletion of %s failed. Please remove it manually, as the "
6356
0
                   "directory is now unnecessary.",
6357
0
                   fs::PathToString(delete_path));
6358
0
    } else {
6359
0
        LogInfo("[snapshot] deleted background chainstate directory (%s)",
6360
0
                fs::PathToString(validated_path));
6361
0
    }
6362
0
    return true;
6363
0
}
6364
6365
std::pair<int, int> Chainstate::GetPruneRange(int last_height_can_prune) const
6366
0
{
6367
0
    if (m_chain.Height() <= 0) {
  Branch (6367:9): [True: 0, False: 0]
6368
0
        return {0, 0};
6369
0
    }
6370
0
    int prune_start{0};
6371
6372
0
    if (m_from_snapshot_blockhash && m_assumeutxo != Assumeutxo::VALIDATED) {
  Branch (6372:9): [True: 0, False: 0]
  Branch (6372:38): [True: 0, False: 0]
6373
        // Only prune blocks _after_ the snapshot if this is a snapshot chain
6374
        // that has not been fully validated yet. The earlier blocks need to be
6375
        // kept to validate the snapshot
6376
0
        prune_start = Assert(SnapshotBase())->nHeight + 1;
6377
0
    }
6378
6379
0
    int max_prune = std::max<int>(
6380
0
        0, m_chain.Height() - static_cast<int>(MIN_BLOCKS_TO_KEEP));
6381
6382
    // last block to prune is the lesser of (caller-specified height, MIN_BLOCKS_TO_KEEP from the tip)
6383
    //
6384
    // While you might be tempted to prune the background chainstate more
6385
    // aggressively (i.e. fewer MIN_BLOCKS_TO_KEEP), this won't work with index
6386
    // building - specifically blockfilterindex requires undo data, and if
6387
    // we don't maintain this trailing window, we hit indexing failures.
6388
0
    int prune_end = std::min(last_height_can_prune, max_prune);
6389
6390
0
    return {prune_start, prune_end};
6391
0
}
6392
6393
std::optional<std::pair<const CBlockIndex*, const CBlockIndex*>> ChainstateManager::GetHistoricalBlockRange() const
6394
651k
{
6395
651k
    const Chainstate* chainstate{HistoricalChainstate()};
6396
651k
    if (!chainstate) return {};
  Branch (6396:9): [True: 651k, False: 0]
6397
0
    return std::make_pair(chainstate->m_chain.Tip(), chainstate->TargetBlock());
6398
651k
}
6399
6400
util::Result<void> ChainstateManager::ActivateBestChains()
6401
0
{
6402
    // We can't hold cs_main during ActivateBestChain even though we're accessing
6403
    // the chainman unique_ptrs since ABC requires us not to be holding cs_main, so retrieve
6404
    // the relevant pointers before the ABC call.
6405
0
    AssertLockNotHeld(cs_main);
6406
0
    std::vector<Chainstate*> chainstates;
6407
0
    {
6408
0
        LOCK(GetMutex());
6409
0
        chainstates.reserve(m_chainstates.size());
6410
0
        for (const auto& chainstate : m_chainstates) {
  Branch (6410:37): [True: 0, False: 0]
6411
0
            if (chainstate && chainstate->m_assumeutxo != Assumeutxo::INVALID && !chainstate->m_target_utxohash) {
  Branch (6411:17): [True: 0, False: 0]
  Branch (6411:31): [True: 0, False: 0]
  Branch (6411:82): [True: 0, False: 0]
6412
0
                chainstates.push_back(chainstate.get());
6413
0
            }
6414
0
        }
6415
0
    }
6416
0
    for (Chainstate* chainstate : chainstates) {
  Branch (6416:33): [True: 0, False: 0]
6417
0
        BlockValidationState state;
6418
0
        if (!chainstate->ActivateBestChain(state, nullptr)) {
  Branch (6418:13): [True: 0, False: 0]
6419
0
            LOCK(GetMutex());
6420
0
            return util::Error{Untranslated(strprintf("%s Failed to connect best block (%s)", chainstate->ToString(), state.ToString()))};
6421
0
        }
6422
0
    }
6423
0
    return {};
6424
0
}