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/index/coinstatsindex.cpp
Line
Count
Source
1
// Copyright (c) 2020-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 <index/coinstatsindex.h>
6
7
#include <arith_uint256.h>
8
#include <chain.h>
9
#include <chainparams.h>
10
#include <coins.h>
11
#include <common/args.h>
12
#include <consensus/amount.h>
13
#include <crypto/muhash.h>
14
#include <dbwrapper.h>
15
#include <index/base.h>
16
#include <index/db_key.h>
17
#include <interfaces/chain.h>
18
#include <interfaces/types.h>
19
#include <kernel/coinstats.h>
20
#include <primitives/block.h>
21
#include <primitives/transaction.h>
22
#include <script/script.h>
23
#include <serialize.h>
24
#include <uint256.h>
25
#include <undo.h>
26
#include <util/check.h>
27
#include <util/fs.h>
28
#include <util/log.h>
29
#include <validation.h>
30
31
#include <compare>
32
#include <limits>
33
#include <string>
34
#include <utility>
35
#include <vector>
36
37
using kernel::ApplyCoinHash;
38
using kernel::CCoinsStats;
39
using kernel::GetBogoSize;
40
using kernel::RemoveCoinHash;
41
42
static constexpr uint8_t DB_MUHASH{'M'};
43
44
namespace {
45
46
struct DBVal {
47
    uint256 muhash{uint256::ZERO};
48
    uint64_t transaction_output_count{0};
49
    uint64_t bogo_size{0};
50
    CAmount total_amount{0};
51
    CAmount total_subsidy{0};
52
    arith_uint256 total_prevout_spent_amount{0};
53
    arith_uint256 total_new_outputs_ex_coinbase_amount{0};
54
    arith_uint256 total_coinbase_amount{0};
55
    CAmount total_unspendables_genesis_block{0};
56
    CAmount total_unspendables_bip30{0};
57
    CAmount total_unspendables_scripts{0};
58
    CAmount total_unspendables_unclaimed_rewards{0};
59
60
    SERIALIZE_METHODS(DBVal, obj)
61
0
    {
62
0
        uint256 prevout_spent, new_outputs, coinbase;
63
0
        SER_WRITE(obj, prevout_spent = ArithToUint256(obj.total_prevout_spent_amount));
Line
Count
Source
149
0
#define SER_WRITE(obj, code) ser_action.SerWrite(s, obj, [&](Stream& s, const Type& obj) { code; })
        SER_WRITE(obj, prevout_spent = ArithToUint256(obj.total_prevout_spent_amount));
Line
Count
Source
149
0
#define SER_WRITE(obj, code) ser_action.SerWrite(s, obj, [&](Stream& s, const Type& obj) { code; })
        SER_WRITE(obj, prevout_spent = ArithToUint256(obj.total_prevout_spent_amount));
Line
Count
Source
149
0
#define SER_WRITE(obj, code) ser_action.SerWrite(s, obj, [&](Stream& s, const Type& obj) { code; })
64
0
        SER_WRITE(obj, new_outputs = ArithToUint256(obj.total_new_outputs_ex_coinbase_amount));
Line
Count
Source
149
0
#define SER_WRITE(obj, code) ser_action.SerWrite(s, obj, [&](Stream& s, const Type& obj) { code; })
        SER_WRITE(obj, new_outputs = ArithToUint256(obj.total_new_outputs_ex_coinbase_amount));
Line
Count
Source
149
0
#define SER_WRITE(obj, code) ser_action.SerWrite(s, obj, [&](Stream& s, const Type& obj) { code; })
        SER_WRITE(obj, new_outputs = ArithToUint256(obj.total_new_outputs_ex_coinbase_amount));
Line
Count
Source
149
0
#define SER_WRITE(obj, code) ser_action.SerWrite(s, obj, [&](Stream& s, const Type& obj) { code; })
65
0
        SER_WRITE(obj, coinbase = ArithToUint256(obj.total_coinbase_amount));
Line
Count
Source
149
0
#define SER_WRITE(obj, code) ser_action.SerWrite(s, obj, [&](Stream& s, const Type& obj) { code; })
        SER_WRITE(obj, coinbase = ArithToUint256(obj.total_coinbase_amount));
Line
Count
Source
149
0
#define SER_WRITE(obj, code) ser_action.SerWrite(s, obj, [&](Stream& s, const Type& obj) { code; })
        SER_WRITE(obj, coinbase = ArithToUint256(obj.total_coinbase_amount));
Line
Count
Source
149
0
#define SER_WRITE(obj, code) ser_action.SerWrite(s, obj, [&](Stream& s, const Type& obj) { code; })
66
67
0
        READWRITE(obj.muhash);
Line
Count
Source
147
0
#define READWRITE(...) (ser_action.SerReadWriteMany(s, __VA_ARGS__))
        READWRITE(obj.muhash);
Line
Count
Source
147
0
#define READWRITE(...) (ser_action.SerReadWriteMany(s, __VA_ARGS__))
        READWRITE(obj.muhash);
Line
Count
Source
147
0
#define READWRITE(...) (ser_action.SerReadWriteMany(s, __VA_ARGS__))
68
0
        READWRITE(obj.transaction_output_count);
Line
Count
Source
147
0
#define READWRITE(...) (ser_action.SerReadWriteMany(s, __VA_ARGS__))
        READWRITE(obj.transaction_output_count);
Line
Count
Source
147
0
#define READWRITE(...) (ser_action.SerReadWriteMany(s, __VA_ARGS__))
        READWRITE(obj.transaction_output_count);
Line
Count
Source
147
0
#define READWRITE(...) (ser_action.SerReadWriteMany(s, __VA_ARGS__))
69
0
        READWRITE(obj.bogo_size);
Line
Count
Source
147
0
#define READWRITE(...) (ser_action.SerReadWriteMany(s, __VA_ARGS__))
        READWRITE(obj.bogo_size);
Line
Count
Source
147
0
#define READWRITE(...) (ser_action.SerReadWriteMany(s, __VA_ARGS__))
        READWRITE(obj.bogo_size);
Line
Count
Source
147
0
#define READWRITE(...) (ser_action.SerReadWriteMany(s, __VA_ARGS__))
70
0
        READWRITE(obj.total_amount);
Line
Count
Source
147
0
#define READWRITE(...) (ser_action.SerReadWriteMany(s, __VA_ARGS__))
        READWRITE(obj.total_amount);
Line
Count
Source
147
0
#define READWRITE(...) (ser_action.SerReadWriteMany(s, __VA_ARGS__))
        READWRITE(obj.total_amount);
Line
Count
Source
147
0
#define READWRITE(...) (ser_action.SerReadWriteMany(s, __VA_ARGS__))
71
0
        READWRITE(obj.total_subsidy);
Line
Count
Source
147
0
#define READWRITE(...) (ser_action.SerReadWriteMany(s, __VA_ARGS__))
        READWRITE(obj.total_subsidy);
Line
Count
Source
147
0
#define READWRITE(...) (ser_action.SerReadWriteMany(s, __VA_ARGS__))
        READWRITE(obj.total_subsidy);
Line
Count
Source
147
0
#define READWRITE(...) (ser_action.SerReadWriteMany(s, __VA_ARGS__))
72
0
        READWRITE(prevout_spent);
Line
Count
Source
147
0
#define READWRITE(...) (ser_action.SerReadWriteMany(s, __VA_ARGS__))
        READWRITE(prevout_spent);
Line
Count
Source
147
0
#define READWRITE(...) (ser_action.SerReadWriteMany(s, __VA_ARGS__))
        READWRITE(prevout_spent);
Line
Count
Source
147
0
#define READWRITE(...) (ser_action.SerReadWriteMany(s, __VA_ARGS__))
73
0
        READWRITE(new_outputs);
Line
Count
Source
147
0
#define READWRITE(...) (ser_action.SerReadWriteMany(s, __VA_ARGS__))
        READWRITE(new_outputs);
Line
Count
Source
147
0
#define READWRITE(...) (ser_action.SerReadWriteMany(s, __VA_ARGS__))
        READWRITE(new_outputs);
Line
Count
Source
147
0
#define READWRITE(...) (ser_action.SerReadWriteMany(s, __VA_ARGS__))
74
0
        READWRITE(coinbase);
Line
Count
Source
147
0
#define READWRITE(...) (ser_action.SerReadWriteMany(s, __VA_ARGS__))
        READWRITE(coinbase);
Line
Count
Source
147
0
#define READWRITE(...) (ser_action.SerReadWriteMany(s, __VA_ARGS__))
        READWRITE(coinbase);
Line
Count
Source
147
0
#define READWRITE(...) (ser_action.SerReadWriteMany(s, __VA_ARGS__))
75
0
        READWRITE(obj.total_unspendables_genesis_block);
Line
Count
Source
147
0
#define READWRITE(...) (ser_action.SerReadWriteMany(s, __VA_ARGS__))
        READWRITE(obj.total_unspendables_genesis_block);
Line
Count
Source
147
0
#define READWRITE(...) (ser_action.SerReadWriteMany(s, __VA_ARGS__))
        READWRITE(obj.total_unspendables_genesis_block);
Line
Count
Source
147
0
#define READWRITE(...) (ser_action.SerReadWriteMany(s, __VA_ARGS__))
76
0
        READWRITE(obj.total_unspendables_bip30);
Line
Count
Source
147
0
#define READWRITE(...) (ser_action.SerReadWriteMany(s, __VA_ARGS__))
        READWRITE(obj.total_unspendables_bip30);
Line
Count
Source
147
0
#define READWRITE(...) (ser_action.SerReadWriteMany(s, __VA_ARGS__))
        READWRITE(obj.total_unspendables_bip30);
Line
Count
Source
147
0
#define READWRITE(...) (ser_action.SerReadWriteMany(s, __VA_ARGS__))
77
0
        READWRITE(obj.total_unspendables_scripts);
Line
Count
Source
147
0
#define READWRITE(...) (ser_action.SerReadWriteMany(s, __VA_ARGS__))
        READWRITE(obj.total_unspendables_scripts);
Line
Count
Source
147
0
#define READWRITE(...) (ser_action.SerReadWriteMany(s, __VA_ARGS__))
        READWRITE(obj.total_unspendables_scripts);
Line
Count
Source
147
0
#define READWRITE(...) (ser_action.SerReadWriteMany(s, __VA_ARGS__))
78
0
        READWRITE(obj.total_unspendables_unclaimed_rewards);
Line
Count
Source
147
0
#define READWRITE(...) (ser_action.SerReadWriteMany(s, __VA_ARGS__))
        READWRITE(obj.total_unspendables_unclaimed_rewards);
Line
Count
Source
147
0
#define READWRITE(...) (ser_action.SerReadWriteMany(s, __VA_ARGS__))
        READWRITE(obj.total_unspendables_unclaimed_rewards);
Line
Count
Source
147
0
#define READWRITE(...) (ser_action.SerReadWriteMany(s, __VA_ARGS__))
79
80
0
        SER_READ(obj, obj.total_prevout_spent_amount = UintToArith256(prevout_spent));
Line
Count
Source
148
0
#define SER_READ(obj, code) ser_action.SerRead(s, obj, [&](Stream& s, std::remove_const_t<Type>& obj) { code; })
        SER_READ(obj, obj.total_prevout_spent_amount = UintToArith256(prevout_spent));
Line
Count
Source
148
0
#define SER_READ(obj, code) ser_action.SerRead(s, obj, [&](Stream& s, std::remove_const_t<Type>& obj) { code; })
        SER_READ(obj, obj.total_prevout_spent_amount = UintToArith256(prevout_spent));
Line
Count
Source
148
0
#define SER_READ(obj, code) ser_action.SerRead(s, obj, [&](Stream& s, std::remove_const_t<Type>& obj) { code; })
81
0
        SER_READ(obj, obj.total_new_outputs_ex_coinbase_amount = UintToArith256(new_outputs));
Line
Count
Source
148
0
#define SER_READ(obj, code) ser_action.SerRead(s, obj, [&](Stream& s, std::remove_const_t<Type>& obj) { code; })
        SER_READ(obj, obj.total_new_outputs_ex_coinbase_amount = UintToArith256(new_outputs));
Line
Count
Source
148
0
#define SER_READ(obj, code) ser_action.SerRead(s, obj, [&](Stream& s, std::remove_const_t<Type>& obj) { code; })
        SER_READ(obj, obj.total_new_outputs_ex_coinbase_amount = UintToArith256(new_outputs));
Line
Count
Source
148
0
#define SER_READ(obj, code) ser_action.SerRead(s, obj, [&](Stream& s, std::remove_const_t<Type>& obj) { code; })
82
0
        SER_READ(obj, obj.total_coinbase_amount = UintToArith256(coinbase));
Line
Count
Source
148
0
#define SER_READ(obj, code) ser_action.SerRead(s, obj, [&](Stream& s, std::remove_const_t<Type>& obj) { code; })
        SER_READ(obj, obj.total_coinbase_amount = UintToArith256(coinbase));
Line
Count
Source
148
0
#define SER_READ(obj, code) ser_action.SerRead(s, obj, [&](Stream& s, std::remove_const_t<Type>& obj) { code; })
        SER_READ(obj, obj.total_coinbase_amount = UintToArith256(coinbase));
Line
Count
Source
148
0
#define SER_READ(obj, code) ser_action.SerRead(s, obj, [&](Stream& s, std::remove_const_t<Type>& obj) { code; })
83
0
    }
Unexecuted instantiation: coinstatsindex.cpp:void (anonymous namespace)::DBVal::SerializationOps<DataStream, (anonymous namespace)::DBVal const, ActionSerialize>((anonymous namespace)::DBVal const&, DataStream&, ActionSerialize)
Unexecuted instantiation: coinstatsindex.cpp:void (anonymous namespace)::DBVal::SerializationOps<DataStream, (anonymous namespace)::DBVal, ActionUnserialize>((anonymous namespace)::DBVal&, DataStream&, ActionUnserialize)
Unexecuted instantiation: coinstatsindex.cpp:void (anonymous namespace)::DBVal::SerializationOps<SpanReader, (anonymous namespace)::DBVal, ActionUnserialize>((anonymous namespace)::DBVal&, SpanReader&, ActionUnserialize)
84
};
85
}; // namespace
86
87
std::unique_ptr<CoinStatsIndex> g_coin_stats_index;
88
89
CoinStatsIndex::CoinStatsIndex(std::unique_ptr<interfaces::Chain> chain, size_t n_cache_size, bool f_memory, bool f_wipe)
90
0
    : BaseIndex(std::move(chain), "coinstatsindex")
91
0
{
92
    // An earlier version of the index used "indexes/coinstats" but it contained
93
    // a bug and is superseded by a fixed version at "indexes/coinstatsindex".
94
    // The original index is kept around until the next release in case users
95
    // decide to downgrade their node.
96
0
    auto old_path = gArgs.GetDataDirNet() / "indexes" / "coinstats";
97
0
    if (fs::exists(old_path)) {
98
        // TODO: Change this to deleting the old index with v31.
99
0
        LogWarning("Old version of coinstatsindex found at %s. This folder can be safely deleted unless you " \
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__)
100
0
            "plan to downgrade your node to version 29 or lower.", fs::PathToString(old_path));
101
0
    }
102
0
    fs::path path{gArgs.GetDataDirNet() / "indexes" / "coinstatsindex"};
103
0
    fs::create_directories(path);
104
105
0
    m_db = std::make_unique<CoinStatsIndex::DB>(path / "db", n_cache_size, f_memory, f_wipe);
106
0
}
107
108
bool CoinStatsIndex::CustomAppend(const interfaces::BlockInfo& block)
109
0
{
110
0
    const CAmount block_subsidy{GetBlockSubsidy(block.height, Params().GetConsensus())};
111
0
    m_total_subsidy += block_subsidy;
112
113
    // Ignore genesis block
114
0
    if (block.height > 0) {
115
0
        uint256 expected_block_hash{*Assert(block.prev_hash)};
Line
Count
Source
113
0
#define Assert(val) inline_assertion_check<true>(val, std::source_location::current(), #val)
116
0
        if (m_current_block_hash != expected_block_hash) {
117
0
            LogError("previous block header belongs to unexpected block %s; expected %s",
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__)
118
0
                      m_current_block_hash.ToString(), expected_block_hash.ToString());
119
0
            return false;
120
0
        }
121
122
        // Add the new utxos created from the block
123
0
        assert(block.data);
124
0
        for (size_t i = 0; i < block.data->vtx.size(); ++i) {
125
0
            const auto& tx{block.data->vtx.at(i)};
126
0
            const bool is_coinbase{tx->IsCoinBase()};
127
128
            // Skip duplicate txid coinbase transactions (BIP30).
129
0
            if (is_coinbase && IsBIP30Unspendable(block.hash, block.height)) {
130
0
                m_total_unspendables_bip30 += block_subsidy;
131
0
                continue;
132
0
            }
133
134
0
            for (uint32_t j = 0; j < tx->vout.size(); ++j) {
135
0
                const CTxOut& out{tx->vout[j]};
136
0
                const Coin coin{out, block.height, is_coinbase};
137
0
                const COutPoint outpoint{tx->GetHash(), j};
138
139
                // Skip unspendable coins
140
0
                if (coin.out.scriptPubKey.IsUnspendable()) {
141
0
                    m_total_unspendables_scripts += coin.out.nValue;
142
0
                    continue;
143
0
                }
144
145
0
                ApplyCoinHash(m_muhash, outpoint, coin);
146
147
0
                if (is_coinbase) {
148
0
                    m_total_coinbase_amount += coin.out.nValue;
149
0
                } else {
150
0
                    m_total_new_outputs_ex_coinbase_amount += coin.out.nValue;
151
0
                }
152
153
0
                ++m_transaction_output_count;
154
0
                m_total_amount += coin.out.nValue;
155
0
                m_bogo_size += GetBogoSize(coin.out.scriptPubKey);
156
0
            }
157
158
            // The coinbase tx has no undo data since no former output is spent
159
0
            if (!is_coinbase) {
160
0
                const auto& tx_undo{Assert(block.undo_data)->vtxundo.at(i - 1)};
Line
Count
Source
113
0
#define Assert(val) inline_assertion_check<true>(val, std::source_location::current(), #val)
161
162
0
                for (size_t j = 0; j < tx_undo.vprevout.size(); ++j) {
163
0
                    const Coin& coin{tx_undo.vprevout[j]};
164
0
                    const COutPoint outpoint{tx->vin[j].prevout.hash, tx->vin[j].prevout.n};
165
166
0
                    RemoveCoinHash(m_muhash, outpoint, coin);
167
168
0
                    m_total_prevout_spent_amount += coin.out.nValue;
169
170
0
                    --m_transaction_output_count;
171
0
                    m_total_amount -= coin.out.nValue;
172
0
                    m_bogo_size -= GetBogoSize(coin.out.scriptPubKey);
173
0
                }
174
0
            }
175
0
        }
176
0
    } else {
177
        // genesis block
178
0
        m_total_unspendables_genesis_block += block_subsidy;
179
0
    }
180
181
    // If spent prevouts + block subsidy are still a higher amount than
182
    // new outputs + coinbase + current unspendable amount this means
183
    // the miner did not claim the full block reward. Unclaimed block
184
    // rewards are also unspendable.
185
0
    const CAmount temp_total_unspendable_amount{m_total_unspendables_genesis_block + m_total_unspendables_bip30 + m_total_unspendables_scripts + m_total_unspendables_unclaimed_rewards};
186
0
    const arith_uint256 unclaimed_rewards{(m_total_prevout_spent_amount + m_total_subsidy) - (m_total_new_outputs_ex_coinbase_amount + m_total_coinbase_amount + temp_total_unspendable_amount)};
187
0
    assert(unclaimed_rewards <= arith_uint256(std::numeric_limits<CAmount>::max()));
188
0
    m_total_unspendables_unclaimed_rewards += static_cast<CAmount>(unclaimed_rewards.GetLow64());
189
190
0
    std::pair<uint256, DBVal> value;
191
0
    value.first = block.hash;
192
0
    value.second.transaction_output_count = m_transaction_output_count;
193
0
    value.second.bogo_size = m_bogo_size;
194
0
    value.second.total_amount = m_total_amount;
195
0
    value.second.total_subsidy = m_total_subsidy;
196
0
    value.second.total_prevout_spent_amount = m_total_prevout_spent_amount;
197
0
    value.second.total_new_outputs_ex_coinbase_amount = m_total_new_outputs_ex_coinbase_amount;
198
0
    value.second.total_coinbase_amount = m_total_coinbase_amount;
199
0
    value.second.total_unspendables_genesis_block = m_total_unspendables_genesis_block;
200
0
    value.second.total_unspendables_bip30 = m_total_unspendables_bip30;
201
0
    value.second.total_unspendables_scripts = m_total_unspendables_scripts;
202
0
    value.second.total_unspendables_unclaimed_rewards = m_total_unspendables_unclaimed_rewards;
203
204
0
    uint256 out;
205
0
    m_muhash.Finalize(out);
206
0
    value.second.muhash = out;
207
208
0
    m_current_block_hash = block.hash;
209
210
    // Intentionally do not update DB_MUHASH here so it stays in sync with
211
    // DB_BEST_BLOCK, and the index is not corrupted if there is an unclean shutdown.
212
0
    m_db->Write(index_util::DBHeightKey(block.height), value);
213
0
    return true;
214
0
}
215
216
bool CoinStatsIndex::CustomRemove(const interfaces::BlockInfo& block)
217
0
{
218
0
    CDBBatch batch(*m_db);
219
0
    std::unique_ptr<CDBIterator> db_it(m_db->NewIterator());
220
221
    // During a reorg, copy the block's hash digest from the height index to the hash index,
222
    // ensuring it's still accessible after the height index entry is overwritten.
223
0
    if (!index_util::CopyHeightIndexToHashIndex<DBVal>(*db_it, batch, m_name, block.height)) {
224
0
        return false;
225
0
    }
226
227
0
    m_db->WriteBatch(batch);
228
229
0
    if (!RevertBlock(block)) {
230
0
        return false; // failure cause logged internally
231
0
    }
232
233
0
    return true;
234
0
}
235
236
std::optional<CCoinsStats> CoinStatsIndex::LookUpStats(const CBlockIndex& block_index) const
237
0
{
238
0
    CCoinsStats stats{block_index.nHeight, block_index.GetBlockHash()};
239
0
    stats.index_used = true;
240
241
0
    DBVal entry;
242
0
    if (!index_util::LookUpOne(*m_db, {block_index.GetBlockHash(), block_index.nHeight}, entry)) {
243
0
        return std::nullopt;
244
0
    }
245
246
0
    stats.hashSerialized = entry.muhash;
247
0
    stats.nTransactionOutputs = entry.transaction_output_count;
248
0
    stats.nBogoSize = entry.bogo_size;
249
0
    stats.total_amount = entry.total_amount;
250
0
    stats.total_subsidy = entry.total_subsidy;
251
0
    stats.total_prevout_spent_amount = entry.total_prevout_spent_amount;
252
0
    stats.total_new_outputs_ex_coinbase_amount = entry.total_new_outputs_ex_coinbase_amount;
253
0
    stats.total_coinbase_amount = entry.total_coinbase_amount;
254
0
    stats.total_unspendables_genesis_block = entry.total_unspendables_genesis_block;
255
0
    stats.total_unspendables_bip30 = entry.total_unspendables_bip30;
256
0
    stats.total_unspendables_scripts = entry.total_unspendables_scripts;
257
0
    stats.total_unspendables_unclaimed_rewards = entry.total_unspendables_unclaimed_rewards;
258
259
0
    return stats;
260
0
}
261
262
bool CoinStatsIndex::CustomInit(const std::optional<interfaces::BlockRef>& block)
263
0
{
264
0
    if (!m_db->Read(DB_MUHASH, m_muhash)) {
265
        // Check that the cause of the read failure is that the key does not
266
        // exist. Any other errors indicate database corruption or a disk
267
        // failure, and starting the index would cause further corruption.
268
0
        if (m_db->Exists(DB_MUHASH)) {
269
0
            LogError("Cannot read current %s state; index may be corrupted",
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__)
270
0
                      GetName());
271
0
            return false;
272
0
        }
273
0
    }
274
275
0
    if (block) {
276
0
        DBVal entry;
277
0
        if (!index_util::LookUpOne(*m_db, *block, entry)) {
278
0
            LogError("Cannot read current %s state; index may be corrupted",
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__)
279
0
                      GetName());
280
0
            return false;
281
0
        }
282
283
0
        uint256 out;
284
0
        m_muhash.Finalize(out);
285
0
        if (entry.muhash != out) {
286
0
            LogError("Cannot read current %s state; index may be corrupted",
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__)
287
0
                      GetName());
288
0
            return false;
289
0
        }
290
291
0
        m_transaction_output_count = entry.transaction_output_count;
292
0
        m_bogo_size = entry.bogo_size;
293
0
        m_total_amount = entry.total_amount;
294
0
        m_total_subsidy = entry.total_subsidy;
295
0
        m_total_prevout_spent_amount = entry.total_prevout_spent_amount;
296
0
        m_total_new_outputs_ex_coinbase_amount = entry.total_new_outputs_ex_coinbase_amount;
297
0
        m_total_coinbase_amount = entry.total_coinbase_amount;
298
0
        m_total_unspendables_genesis_block = entry.total_unspendables_genesis_block;
299
0
        m_total_unspendables_bip30 = entry.total_unspendables_bip30;
300
0
        m_total_unspendables_scripts = entry.total_unspendables_scripts;
301
0
        m_total_unspendables_unclaimed_rewards = entry.total_unspendables_unclaimed_rewards;
302
0
        m_current_block_hash = block->hash;
303
0
    }
304
305
0
    return true;
306
0
}
307
308
bool CoinStatsIndex::CustomCommit(CDBBatch& batch)
309
0
{
310
    // DB_MUHASH should always be committed in a batch together with DB_BEST_BLOCK
311
    // to prevent an inconsistent state of the DB.
312
0
    batch.Write(DB_MUHASH, m_muhash);
313
0
    return true;
314
0
}
315
316
interfaces::Chain::NotifyOptions CoinStatsIndex::CustomOptions()
317
0
{
318
0
    interfaces::Chain::NotifyOptions options;
319
0
    options.connect_undo_data = true;
320
0
    options.disconnect_data = true;
321
0
    options.disconnect_undo_data = true;
322
0
    return options;
323
0
}
324
325
// Revert a single block as part of a reorg
326
bool CoinStatsIndex::RevertBlock(const interfaces::BlockInfo& block)
327
0
{
328
0
    std::pair<uint256, DBVal> read_out;
329
330
    // Ignore genesis block
331
0
    if (block.height > 0) {
332
0
        if (!m_db->Read(index_util::DBHeightKey(block.height - 1), read_out)) {
333
0
            return false;
334
0
        }
335
336
0
        uint256 expected_block_hash{*block.prev_hash};
337
0
        if (read_out.first != expected_block_hash) {
338
0
            LogWarning("previous block header belongs to unexpected block %s; expected %s",
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__)
339
0
                      read_out.first.ToString(), expected_block_hash.ToString());
340
341
0
            if (!m_db->Read(index_util::DBHashKey(expected_block_hash), read_out)) {
342
0
                LogError("previous block header not found; expected %s",
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__)
343
0
                          expected_block_hash.ToString());
344
0
                return false;
345
0
            }
346
0
        }
347
0
    }
348
349
    // Roll back muhash by removing the new UTXOs that were created by the
350
    // block and reapplying the old UTXOs that were spent by the block
351
0
    assert(block.data);
352
0
    assert(block.undo_data);
353
0
    for (size_t i = 0; i < block.data->vtx.size(); ++i) {
354
0
        const auto& tx{block.data->vtx.at(i)};
355
0
        const bool is_coinbase{tx->IsCoinBase()};
356
357
0
        if (is_coinbase && IsBIP30Unspendable(block.hash, block.height)) {
358
0
            continue;
359
0
        }
360
361
0
        for (uint32_t j = 0; j < tx->vout.size(); ++j) {
362
0
            const CTxOut& out{tx->vout[j]};
363
0
            const COutPoint outpoint{tx->GetHash(), j};
364
0
            const Coin coin{out, block.height, is_coinbase};
365
366
0
            if (!coin.out.scriptPubKey.IsUnspendable()) {
367
0
                RemoveCoinHash(m_muhash, outpoint, coin);
368
0
            }
369
0
        }
370
371
        // The coinbase tx has no undo data since no former output is spent
372
0
        if (!is_coinbase) {
373
0
            const auto& tx_undo{block.undo_data->vtxundo.at(i - 1)};
374
375
0
            for (size_t j = 0; j < tx_undo.vprevout.size(); ++j) {
376
0
                const Coin& coin{tx_undo.vprevout[j]};
377
0
                const COutPoint outpoint{tx->vin[j].prevout.hash, tx->vin[j].prevout.n};
378
0
                ApplyCoinHash(m_muhash, outpoint, coin);
379
0
            }
380
0
        }
381
0
    }
382
383
    // Check that the rolled back muhash is consistent with the DB read out
384
0
    uint256 out;
385
0
    m_muhash.Finalize(out);
386
0
    Assert(read_out.second.muhash == out);
Line
Count
Source
113
0
#define Assert(val) inline_assertion_check<true>(val, std::source_location::current(), #val)
387
388
    // Apply the other values from the DB to the member variables
389
0
    m_transaction_output_count = read_out.second.transaction_output_count;
390
0
    m_total_amount = read_out.second.total_amount;
391
0
    m_bogo_size = read_out.second.bogo_size;
392
0
    m_total_subsidy = read_out.second.total_subsidy;
393
0
    m_total_prevout_spent_amount = read_out.second.total_prevout_spent_amount;
394
0
    m_total_new_outputs_ex_coinbase_amount = read_out.second.total_new_outputs_ex_coinbase_amount;
395
0
    m_total_coinbase_amount = read_out.second.total_coinbase_amount;
396
0
    m_total_unspendables_genesis_block = read_out.second.total_unspendables_genesis_block;
397
0
    m_total_unspendables_bip30 = read_out.second.total_unspendables_bip30;
398
0
    m_total_unspendables_scripts = read_out.second.total_unspendables_scripts;
399
0
    m_total_unspendables_unclaimed_rewards = read_out.second.total_unspendables_unclaimed_rewards;
400
0
    m_current_block_hash = *block.prev_hash;
401
402
0
    return true;
403
0
}