fuzz coverage

Coverage Report

Created: 2026-04-24 13:48

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/bitcoin/src/node/chainstate.cpp
Line
Count
Source
1
// Copyright (c) 2021-present The Bitcoin Core developers
2
// Distributed under the MIT software license, see the accompanying
3
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5
#include <node/chainstate.h>
6
7
#include <arith_uint256.h>
8
#include <chain.h>
9
#include <coins.h>
10
#include <consensus/params.h>
11
#include <kernel/caches.h>
12
#include <node/blockstorage.h>
13
#include <sync.h>
14
#include <tinyformat.h>
15
#include <txdb.h>
16
#include <uint256.h>
17
#include <util/fs.h>
18
#include <util/log.h>
19
#include <util/signalinterrupt.h>
20
#include <util/time.h>
21
#include <util/translation.h>
22
#include <validation.h>
23
24
#include <algorithm>
25
#include <cassert>
26
#include <vector>
27
28
using kernel::CacheSizes;
29
30
namespace node {
31
// Complete initialization of chainstates after the initial call has been made
32
// to ChainstateManager::InitializeChainstate().
33
static ChainstateLoadResult CompleteChainstateInitialization(
34
    ChainstateManager& chainman,
35
    const ChainstateLoadOptions& options) EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
36
147k
{
37
147k
    if (chainman.m_interrupt) 
return {ChainstateLoadStatus::INTERRUPTED, {}}0
;
38
39
    // LoadBlockIndex will load m_have_pruned if we've ever removed a
40
    // block file from disk.
41
    // Note that it also sets m_blockfiles_indexed based on the disk flag!
42
147k
    if (!chainman.LoadBlockIndex()) {
43
0
        if (chainman.m_interrupt) return {ChainstateLoadStatus::INTERRUPTED, {}};
44
0
        return {ChainstateLoadStatus::FAILURE, _("Error loading block database")};
45
0
    }
46
47
147k
    if (!chainman.BlockIndex().empty() &&
48
147k
            
!chainman.m_blockman.LookupBlockIndex(chainman.GetConsensus().hashGenesisBlock)0
) {
49
        // If the loaded chain has a wrong genesis, bail out immediately
50
        // (we're likely using a testnet datadir, or the other way around).
51
0
        return {ChainstateLoadStatus::FAILURE_INCOMPATIBLE_DB, _("Incorrect or no genesis block found. Wrong datadir for network?")};
52
0
    }
53
54
    // Check for changed -prune state.  What we are concerned about is a user who has pruned blocks
55
    // in the past, but is now trying to run unpruned.
56
147k
    if (chainman.m_blockman.m_have_pruned && 
!options.prune0
) {
57
0
        return {ChainstateLoadStatus::FAILURE, _("You need to rebuild the database using -reindex to go back to unpruned mode.  This will redownload the entire blockchain")};
58
0
    }
59
60
    // At this point blocktree args are consistent with what's on disk.
61
    // If we're not mid-reindex (based on disk + args), add a genesis block on disk
62
    // (otherwise we use the one already on disk).
63
    // This is called again in ImportBlocks after the reindex completes.
64
147k
    if (chainman.m_blockman.m_blockfiles_indexed && !chainman.ActiveChainstate().LoadGenesisBlock()) {
65
0
        return {ChainstateLoadStatus::FAILURE, _("Error initializing block database")};
66
0
    }
67
68
147k
    auto is_coinsview_empty = [&](Chainstate& chainstate) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
69
147k
        return options.wipe_chainstate_db || chainstate.CoinsTip().GetBestBlock().IsNull();
70
147k
    };
71
72
147k
    assert(chainman.m_total_coinstip_cache > 0);
73
147k
    assert(chainman.m_total_coinsdb_cache > 0);
74
75
    // If running with multiple chainstates, limit the cache sizes with a
76
    // discount factor. If discounted the actual cache size will be
77
    // recalculated by `chainman.MaybeRebalanceCaches()`. The discount factor
78
    // is conservatively chosen such that the sum of the caches does not exceed
79
    // the allowable amount during this temporary initialization state.
80
147k
    double init_cache_fraction = chainman.HistoricalChainstate() ? 
0.20
: 1.0;
81
82
    // At this point we're either in reindex or we've loaded a useful
83
    // block tree into BlockIndex()!
84
85
147k
    for (const auto& chainstate : chainman.m_chainstates) {
86
147k
        LogInfo("Initializing chainstate %s", chainstate->ToString());
Line
Count
Source
97
147k
#define LogInfo(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Info, /*should_ratelimit=*/true, __VA_ARGS__)
Line
Count
Source
91
147k
#define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__)
87
88
147k
        try {
89
147k
            chainstate->InitCoinsDB(
90
147k
                /*cache_size_bytes=*/chainman.m_total_coinsdb_cache * init_cache_fraction,
91
147k
                /*in_memory=*/options.coins_db_in_memory,
92
147k
                /*should_wipe=*/options.wipe_chainstate_db);
93
147k
        } catch (dbwrapper_error& err) {
94
0
            LogError("%s\n", err.what());
Line
Count
Source
99
0
#define LogError(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Error, /*should_ratelimit=*/true, __VA_ARGS__)
Line
Count
Source
91
0
#define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__)
95
0
            return {ChainstateLoadStatus::FAILURE, _("Error opening coins database")};
96
0
        }
97
98
147k
        if (options.coins_error_cb) {
99
0
            chainstate->CoinsErrorCatcher().AddReadErrCallback(options.coins_error_cb);
100
0
        }
101
102
        // Refuse to load unsupported database format.
103
        // This is a no-op if we cleared the coinsviewdb with -reindex or -reindex-chainstate
104
147k
        if (chainstate->CoinsDB().NeedsUpgrade()) {
105
0
            return {ChainstateLoadStatus::FAILURE_INCOMPATIBLE_DB, _("Unsupported chainstate database format found. "
106
0
                                                                     "Please restart with -reindex-chainstate. This will "
107
0
                                                                     "rebuild the chainstate database.")};
108
0
        }
109
110
        // ReplayBlocks is a no-op if we cleared the coinsviewdb with -reindex or -reindex-chainstate
111
147k
        if (!chainstate->ReplayBlocks()) {
112
0
            return {ChainstateLoadStatus::FAILURE, _("Unable to replay blocks. You will need to rebuild the database using -reindex-chainstate.")};
113
0
        }
114
115
        // The on-disk coinsdb is now in a good state, create the cache
116
147k
        chainstate->InitCoinsCache(chainman.m_total_coinstip_cache * init_cache_fraction);
117
147k
        assert(chainstate->CanFlushToDisk());
118
119
147k
        if (!is_coinsview_empty(*chainstate)) {
120
            // LoadChainTip initializes the chain based on CoinsTip()'s best block
121
0
            if (!chainstate->LoadChainTip()) {
122
0
                return {ChainstateLoadStatus::FAILURE, _("Error initializing block database")};
123
0
            }
124
0
            assert(chainstate->m_chain.Tip() != nullptr);
125
0
        }
126
147k
    }
127
128
    // Populate setBlockIndexCandidates in a separate loop, after all LoadChainTip()
129
    // calls have finished modifying nSequenceId. Because nSequenceId is used in the
130
    // set's comparator, changing it while blocks are in the set would be UB.
131
147k
    for (const auto& chainstate : chainman.m_chainstates) {
132
147k
        chainstate->PopulateBlockIndexCandidates();
133
147k
    }
134
135
147k
    const auto& chainstates{chainman.m_chainstates};
136
147k
    if (std::any_of(chainstates.begin(), chainstates.end(),
137
147k
                    [](const auto& cs) EXCLUSIVE_LOCKS_REQUIRED(cs_main) { return cs->NeedsRedownload(); })) {
138
0
        return {ChainstateLoadStatus::FAILURE, strprintf(_("Witness data for blocks after height %d requires validation. Please restart with -reindex."),
Line
Count
Source
1172
0
#define strprintf tfm::format
139
0
                                                         chainman.GetConsensus().SegwitHeight)};
140
147k
    };
141
142
    // Now that chainstates are loaded and we're able to flush to
143
    // disk, rebalance the coins caches to desired levels based
144
    // on the condition of each chainstate.
145
147k
    chainman.MaybeRebalanceCaches();
146
147
147k
    return {ChainstateLoadStatus::SUCCESS, {}};
148
147k
}
149
150
ChainstateLoadResult LoadChainstate(ChainstateManager& chainman, const CacheSizes& cache_sizes,
151
                                    const ChainstateLoadOptions& options)
152
147k
{
153
147k
    if (!chainman.AssumedValidBlock().IsNull()) {
154
0
        LogInfo("Assuming ancestors of block %s have valid signatures.", chainman.AssumedValidBlock().GetHex());
Line
Count
Source
97
0
#define LogInfo(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Info, /*should_ratelimit=*/true, __VA_ARGS__)
Line
Count
Source
91
0
#define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__)
155
147k
    } else {
156
147k
        LogInfo("Validating signatures for all blocks.");
Line
Count
Source
97
147k
#define LogInfo(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Info, /*should_ratelimit=*/true, __VA_ARGS__)
Line
Count
Source
91
147k
#define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__)
157
147k
    }
158
147k
    LogInfo("Setting nMinimumChainWork=%s", chainman.MinimumChainWork().GetHex());
Line
Count
Source
97
147k
#define LogInfo(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Info, /*should_ratelimit=*/true, __VA_ARGS__)
Line
Count
Source
91
147k
#define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__)
159
147k
    if (chainman.MinimumChainWork() < UintToArith256(chainman.GetConsensus().nMinimumChainWork)) {
160
0
        LogWarning("nMinimumChainWork set below default value of %s", chainman.GetConsensus().nMinimumChainWork.GetHex());
Line
Count
Source
98
0
#define LogWarning(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Warning, /*should_ratelimit=*/true, __VA_ARGS__)
Line
Count
Source
91
0
#define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__)
161
0
    }
162
147k
    if (chainman.m_blockman.GetPruneTarget() == BlockManager::PRUNE_TARGET_MANUAL) {
163
0
        LogInfo("Block pruning enabled. Use RPC call pruneblockchain(height) to manually prune block and undo files.");
Line
Count
Source
97
0
#define LogInfo(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Info, /*should_ratelimit=*/true, __VA_ARGS__)
Line
Count
Source
91
0
#define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__)
164
147k
    } else if (chainman.m_blockman.GetPruneTarget()) {
165
0
        LogInfo("Prune configured to target %u MiB on disk for block and undo files.",
Line
Count
Source
97
0
#define LogInfo(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Info, /*should_ratelimit=*/true, __VA_ARGS__)
Line
Count
Source
91
0
#define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__)
166
0
                chainman.m_blockman.GetPruneTarget() / 1024 / 1024);
167
0
    }
168
169
147k
    LOCK(cs_main);
Line
Count
Source
268
147k
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
Line
Count
Source
11
147k
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
147k
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
147k
#define PASTE(x, y) x ## y
170
171
147k
    chainman.m_total_coinstip_cache = cache_sizes.coins;
172
147k
    chainman.m_total_coinsdb_cache = cache_sizes.coins_db;
173
174
    // Load the fully validated chainstate.
175
147k
    Chainstate& validated_cs{chainman.InitializeChainstate(options.mempool)};
176
177
    // Load a chain created from a UTXO snapshot, if any exist.
178
147k
    Chainstate* assumeutxo_cs{chainman.LoadAssumeutxoChainstate()};
179
180
147k
    if (assumeutxo_cs && 
options.wipe_chainstate_db0
) {
181
        // Reset chainstate target to network tip instead of snapshot block.
182
0
        validated_cs.SetTargetBlock(nullptr);
183
0
        LogInfo("[snapshot] deleting snapshot chainstate due to reindexing");
Line
Count
Source
97
0
#define LogInfo(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Info, /*should_ratelimit=*/true, __VA_ARGS__)
Line
Count
Source
91
0
#define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__)
184
0
        if (!chainman.DeleteChainstate(*assumeutxo_cs)) {
185
0
            return {ChainstateLoadStatus::FAILURE_FATAL, Untranslated("Couldn't remove snapshot chainstate.")};
186
0
        }
187
0
        assumeutxo_cs = nullptr;
188
0
    }
189
190
147k
    auto [init_status, init_error] = CompleteChainstateInitialization(chainman, options);
191
147k
    if (init_status != ChainstateLoadStatus::SUCCESS) {
192
0
        return {init_status, init_error};
193
0
    }
194
195
    // If a snapshot chainstate was fully validated by a background chainstate during
196
    // the last run, detect it here and clean up the now-unneeded background
197
    // chainstate.
198
    //
199
    // Why is this cleanup done here (on subsequent restart) and not just when the
200
    // snapshot is actually validated? Because this entails unusual
201
    // filesystem operations to move leveldb data directories around, and that seems
202
    // too risky to do in the middle of normal runtime.
203
147k
    auto snapshot_completion{assumeutxo_cs
204
147k
                             ? 
chainman.MaybeValidateSnapshot(validated_cs, *assumeutxo_cs)0
205
147k
                             : SnapshotCompletionResult::SKIPPED};
206
207
147k
    if (snapshot_completion == SnapshotCompletionResult::SKIPPED) {
208
        // do nothing; expected case
209
147k
    } else 
if (0
snapshot_completion == SnapshotCompletionResult::SUCCESS0
) {
210
0
        LogInfo("[snapshot] cleaning up unneeded background chainstate, then reinitializing");
Line
Count
Source
97
0
#define LogInfo(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Info, /*should_ratelimit=*/true, __VA_ARGS__)
Line
Count
Source
91
0
#define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__)
211
0
        if (!chainman.ValidatedSnapshotCleanup(validated_cs, *assumeutxo_cs)) {
212
0
            return {ChainstateLoadStatus::FAILURE_FATAL, Untranslated("Background chainstate cleanup failed unexpectedly.")};
213
0
        }
214
215
        // Because ValidatedSnapshotCleanup() has torn down chainstates with
216
        // ChainstateManager::ResetChainstates(), reinitialize them here without
217
        // duplicating the blockindex work above.
218
0
        assert(chainman.m_chainstates.empty());
219
220
0
        chainman.InitializeChainstate(options.mempool);
221
222
        // A reload of the block index is required to recompute setBlockIndexCandidates
223
        // for the fully validated chainstate.
224
0
        chainman.ActiveChainstate().ClearBlockIndexCandidates();
225
226
0
        auto [init_status, init_error] = CompleteChainstateInitialization(chainman, options);
227
0
        if (init_status != ChainstateLoadStatus::SUCCESS) {
228
0
            return {init_status, init_error};
229
0
        }
230
0
    } else {
231
0
        return {ChainstateLoadStatus::FAILURE_FATAL, _(
232
0
           "UTXO snapshot failed to validate. "
233
0
           "Restart to resume normal initial block download, or try loading a different snapshot.")};
234
0
    }
235
236
147k
    return {ChainstateLoadStatus::SUCCESS, {}};
237
147k
}
238
239
ChainstateLoadResult VerifyLoadedChainstate(ChainstateManager& chainman, const ChainstateLoadOptions& options)
240
147k
{
241
147k
    auto is_coinsview_empty = [&](Chainstate& chainstate) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
242
147k
        return options.wipe_chainstate_db || chainstate.CoinsTip().GetBestBlock().IsNull();
243
147k
    };
244
245
147k
    LOCK(cs_main);
Line
Count
Source
268
147k
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
Line
Count
Source
11
147k
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
147k
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
147k
#define PASTE(x, y) x ## y
246
247
147k
    for (auto& chainstate : chainman.m_chainstates) {
248
147k
        if (!is_coinsview_empty(*chainstate)) {
249
0
            const CBlockIndex* tip = chainstate->m_chain.Tip();
250
0
            if (tip && tip->nTime > GetTime() + MAX_FUTURE_BLOCK_TIME) {
251
0
                return {ChainstateLoadStatus::FAILURE, _("The block database contains a block which appears to be from the future. "
252
0
                                                         "This may be due to your computer's date and time being set incorrectly. "
253
0
                                                         "Only rebuild the block database if you are sure that your computer's date and time are correct")};
254
0
            }
255
256
0
            VerifyDBResult result = CVerifyDB(chainman.GetNotifications()).VerifyDB(
257
0
                *chainstate, chainman.GetConsensus(), chainstate->CoinsDB(),
258
0
                options.check_level,
259
0
                options.check_blocks);
260
0
            switch (result) {
261
0
            case VerifyDBResult::SUCCESS:
262
0
            case VerifyDBResult::SKIPPED_MISSING_BLOCKS:
263
0
                break;
264
0
            case VerifyDBResult::INTERRUPTED:
265
0
                return {ChainstateLoadStatus::INTERRUPTED, _("Block verification was interrupted")};
266
0
            case VerifyDBResult::CORRUPTED_BLOCK_DB:
267
0
                return {ChainstateLoadStatus::FAILURE, _("Corrupted block database detected")};
268
0
            case VerifyDBResult::SKIPPED_L3_CHECKS:
269
0
                if (options.require_full_verification) {
270
0
                    return {ChainstateLoadStatus::FAILURE_INSUFFICIENT_DBCACHE, _("Insufficient dbcache for block verification")};
271
0
                }
272
0
                break;
273
0
            } // no default case, so the compiler can warn about missing cases
274
0
        }
275
147k
    }
276
277
147k
    return {ChainstateLoadStatus::SUCCESS, {}};
278
147k
}
279
} // namespace node