fuzz coverage

Coverage Report

Created: 2026-05-08 05:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/bitcoin/src/txdb.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 <txdb.h>
7
8
#include <coins.h>
9
#include <dbwrapper.h>
10
#include <logging/timer.h>
11
#include <primitives/transaction.h>
12
#include <random.h>
13
#include <serialize.h>
14
#include <uint256.h>
15
#include <util/byte_units.h>
16
#include <util/log.h>
17
#include <util/vector.h>
18
19
#include <cassert>
20
#include <cstdlib>
21
#include <iterator>
22
#include <utility>
23
24
static constexpr uint8_t DB_COIN{'C'};
25
static constexpr uint8_t DB_BEST_BLOCK{'B'};
26
static constexpr uint8_t DB_HEAD_BLOCKS{'H'};
27
// Keys used in previous version that might still be found in the DB:
28
static constexpr uint8_t DB_COINS{'c'};
29
30
// Threshold for warning when writing this many dirty cache entries to disk.
31
static constexpr size_t WARN_FLUSH_COINS_COUNT{10'000'000};
32
33
bool CCoinsViewDB::NeedsUpgrade()
34
925
{
35
925
    std::unique_ptr<CDBIterator> cursor{m_db->NewIterator()};
36
    // DB_COINS was deprecated in v0.15.0, commit
37
    // 1088b02f0ccd7358d2b7076bb9e122d59d502d02
38
925
    cursor->Seek(std::make_pair(DB_COINS, uint256{}));
39
925
    return cursor->Valid();
40
925
}
41
42
namespace {
43
44
struct CoinEntry {
45
    COutPoint* outpoint;
46
    uint8_t key{DB_COIN};
47
752k
    explicit CoinEntry(const COutPoint* ptr) : outpoint(const_cast<COutPoint*>(ptr)) {}
48
49
752k
    SERIALIZE_METHODS(CoinEntry, obj) { READWRITE(obj.key, obj.outpoint->hash, VARINT(obj.outpoint->n)); }
Line
Count
Source
147
752k
#define READWRITE(...) (ser_action.SerReadWriteMany(s, __VA_ARGS__))
    SERIALIZE_METHODS(CoinEntry, obj) { READWRITE(obj.key, obj.outpoint->hash, VARINT(obj.outpoint->n)); }
Line
Count
Source
147
0
#define READWRITE(...) (ser_action.SerReadWriteMany(s, __VA_ARGS__))
txdb.cpp:void (anonymous namespace)::CoinEntry::SerializationOps<DataStream, (anonymous namespace)::CoinEntry const, ActionSerialize>((anonymous namespace)::CoinEntry const&, DataStream&, ActionSerialize)
Line
Count
Source
49
752k
    SERIALIZE_METHODS(CoinEntry, obj) { READWRITE(obj.key, obj.outpoint->hash, VARINT(obj.outpoint->n)); }
Line
Count
Source
147
752k
#define READWRITE(...) (ser_action.SerReadWriteMany(s, __VA_ARGS__))
Unexecuted instantiation: txdb.cpp:void (anonymous namespace)::CoinEntry::SerializationOps<SpanReader, (anonymous namespace)::CoinEntry, ActionUnserialize>((anonymous namespace)::CoinEntry&, SpanReader&, ActionUnserialize)
50
};
51
52
} // namespace
53
54
CCoinsViewDB::CCoinsViewDB(DBParams db_params, CoinsViewOptions options) :
55
925
    m_db_params{std::move(db_params)},
56
925
    m_options{std::move(options)},
57
925
    m_db{std::make_unique<CDBWrapper>(m_db_params)} { }
58
59
void CCoinsViewDB::ResizeCache(size_t new_cache_size)
60
0
{
61
    // We can't do this operation with an in-memory DB since we'll lose all the coins upon
62
    // reset.
63
0
    if (!m_db_params.memory_only) {
64
        // Have to do a reset first to get the original `m_db` state to release its
65
        // filesystem lock.
66
0
        m_db.reset();
67
0
        m_db_params.cache_bytes = new_cache_size;
68
0
        m_db_params.wipe_data = false;
69
0
        m_db = std::make_unique<CDBWrapper>(m_db_params);
70
0
    }
71
0
}
72
73
std::optional<Coin> CCoinsViewDB::GetCoin(const COutPoint& outpoint) const
74
752k
{
75
752k
    if (Coin coin; m_db->Read(CoinEntry(&outpoint), coin)) {
76
0
        Assert(!coin.IsSpent()); // The UTXO database should never contain spent coins
Line
Count
Source
116
0
#define Assert(val) inline_assertion_check<true>(val, std::source_location::current(), #val)
77
0
        return coin;
78
0
    }
79
752k
    return std::nullopt;
80
752k
}
81
82
std::optional<Coin> CCoinsViewDB::PeekCoin(const COutPoint& outpoint) const
83
372k
{
84
372k
    return GetCoin(outpoint);
85
372k
}
86
87
bool CCoinsViewDB::HaveCoin(const COutPoint& outpoint) const
88
0
{
89
0
    return m_db->Exists(CoinEntry(&outpoint));
90
0
}
91
92
2.77k
uint256 CCoinsViewDB::GetBestBlock() const {
93
2.77k
    uint256 hashBestChain;
94
2.77k
    if (!m_db->Read(DB_BEST_BLOCK, hashBestChain))
95
2.77k
        return uint256();
96
0
    return hashBestChain;
97
2.77k
}
98
99
925
std::vector<uint256> CCoinsViewDB::GetHeadBlocks() const {
100
925
    std::vector<uint256> vhashHeadBlocks;
101
925
    if (!m_db->Read(DB_HEAD_BLOCKS, vhashHeadBlocks)) {
102
925
        return std::vector<uint256>();
103
925
    }
104
0
    return vhashHeadBlocks;
105
925
}
106
107
void CCoinsViewDB::BatchWrite(CoinsViewCacheCursor& cursor, const uint256& block_hash)
108
0
{
109
0
    CDBBatch batch(*m_db);
110
0
    size_t count = 0;
111
0
    const size_t dirty_count{cursor.GetDirtyCount()};
112
0
    assert(!block_hash.IsNull());
113
114
0
    uint256 old_tip = GetBestBlock();
115
0
    if (old_tip.IsNull()) {
116
        // We may be in the middle of replaying.
117
0
        std::vector<uint256> old_heads = GetHeadBlocks();
118
0
        if (old_heads.size() == 2) {
119
0
            if (old_heads[0] != block_hash) {
120
0
                LogError("The coins database detected an inconsistent state, likely due to a previous crash or shutdown. You will need to restart bitcoind with the -reindex-chainstate or -reindex configuration option.\n");
Line
Count
Source
105
0
#define LogError(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Error, /*should_ratelimit=*/true, __VA_ARGS__)
Line
Count
Source
97
0
#define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__)
121
0
            }
122
0
            assert(old_heads[0] == block_hash);
123
0
            old_tip = old_heads[1];
124
0
        }
125
0
    }
126
127
0
    if (dirty_count > WARN_FLUSH_COINS_COUNT) LogWarning("Flushing large (%d entries) UTXO set to disk, it may take several minutes", dirty_count);
Line
Count
Source
104
0
#define LogWarning(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Warning, /*should_ratelimit=*/true, __VA_ARGS__)
Line
Count
Source
97
0
#define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__)
128
0
    LOG_TIME_MILLIS_WITH_CATEGORY(strprintf("write coins cache to disk (%d out of %d cached coins)",
Line
Count
Source
104
0
    BCLog::Timer<std::chrono::milliseconds> UNIQUE_NAME(logging_timer)(__func__, end_msg, log_category)
Line
Count
Source
11
0
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
0
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
0
#define PASTE(x, y) x ## y
129
0
        dirty_count, cursor.GetTotalCount()), BCLog::BENCH);
130
131
    // In the first batch, mark the database as being in the middle of a
132
    // transition from old_tip to block_hash.
133
    // A vector is used for future extensibility, as we may want to support
134
    // interrupting after partial writes from multiple independent reorgs.
135
0
    batch.Erase(DB_BEST_BLOCK);
136
0
    batch.Write(DB_HEAD_BLOCKS, Vector(block_hash, old_tip));
137
138
0
    for (auto it{cursor.Begin()}; it != cursor.End();) {
139
0
        if (it->second.IsDirty()) {
140
0
            CoinEntry entry(&it->first);
141
0
            if (it->second.coin.IsSpent()) {
142
0
                batch.Erase(entry);
143
0
            } else {
144
0
                batch.Write(entry, it->second.coin);
145
0
            }
146
0
        }
147
0
        count++;
148
0
        it = cursor.NextAndMaybeErase(*it);
149
0
        if (batch.ApproximateSize() > m_options.batch_write_bytes) {
150
0
            LogDebug(BCLog::COINDB, "Writing partial batch of %.2f MiB\n", batch.ApproximateSize() / double(1_MiB));
Line
Count
Source
123
0
#define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, BCLog::Level::Debug, __VA_ARGS__)
Line
Count
Source
114
0
    do {                                                               \
115
0
        if (util::log::ShouldLog((category), (level))) {               \
116
0
            bool rate_limit{level >= BCLog::Level::Info};              \
117
0
            Assume(!rate_limit); /*Only called with the levels below*/ \
Line
Count
Source
128
0
#define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val)
118
0
            LogPrintLevel_(category, level, rate_limit, __VA_ARGS__);  \
Line
Count
Source
97
0
#define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__)
119
0
        }                                                              \
120
0
    } while (0)
151
152
0
            m_db->WriteBatch(batch);
153
0
            batch.Clear();
154
0
            if (m_options.simulate_crash_ratio) {
155
0
                static FastRandomContext rng;
156
0
                if (rng.randrange(m_options.simulate_crash_ratio) == 0) {
157
0
                    LogError("Simulating a crash. Goodbye.");
Line
Count
Source
105
0
#define LogError(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Error, /*should_ratelimit=*/true, __VA_ARGS__)
Line
Count
Source
97
0
#define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__)
158
0
                    _Exit(0);
159
0
                }
160
0
            }
161
0
        }
162
0
    }
163
164
    // In the last batch, mark the database as consistent with block_hash again.
165
0
    batch.Erase(DB_HEAD_BLOCKS);
166
0
    batch.Write(DB_BEST_BLOCK, block_hash);
167
168
0
    LogDebug(BCLog::COINDB, "Writing final batch of %.2f MiB\n", batch.ApproximateSize() / double(1_MiB));
Line
Count
Source
123
0
#define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, BCLog::Level::Debug, __VA_ARGS__)
Line
Count
Source
114
0
    do {                                                               \
115
0
        if (util::log::ShouldLog((category), (level))) {               \
116
0
            bool rate_limit{level >= BCLog::Level::Info};              \
117
0
            Assume(!rate_limit); /*Only called with the levels below*/ \
Line
Count
Source
128
0
#define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val)
118
0
            LogPrintLevel_(category, level, rate_limit, __VA_ARGS__);  \
Line
Count
Source
97
0
#define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__)
119
0
        }                                                              \
120
0
    } while (0)
169
0
    m_db->WriteBatch(batch);
170
0
    LogDebug(BCLog::COINDB, "Committed %u changed transaction outputs (out of %u) to coin database...", (unsigned int)dirty_count, (unsigned int)count);
Line
Count
Source
123
0
#define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, BCLog::Level::Debug, __VA_ARGS__)
Line
Count
Source
114
0
    do {                                                               \
115
0
        if (util::log::ShouldLog((category), (level))) {               \
116
0
            bool rate_limit{level >= BCLog::Level::Info};              \
117
0
            Assume(!rate_limit); /*Only called with the levels below*/ \
Line
Count
Source
128
0
#define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val)
118
0
            LogPrintLevel_(category, level, rate_limit, __VA_ARGS__);  \
Line
Count
Source
97
0
#define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__)
119
0
        }                                                              \
120
0
    } while (0)
171
0
}
172
173
size_t CCoinsViewDB::EstimateSize() const
174
0
{
175
0
    return m_db->EstimateSize(DB_COIN, uint8_t(DB_COIN + 1));
176
0
}
177
178
/** Specialization of CCoinsViewCursor to iterate over a CCoinsViewDB */
179
class CCoinsViewDBCursor: public CCoinsViewCursor
180
{
181
public:
182
    // Prefer using CCoinsViewDB::Cursor() since we want to perform some
183
    // cache warmup on instantiation.
184
    CCoinsViewDBCursor(CDBIterator* pcursorIn, const uint256& in_block_hash):
185
0
        CCoinsViewCursor(in_block_hash), pcursor(pcursorIn) {}
186
0
    ~CCoinsViewDBCursor() = default;
187
188
    bool GetKey(COutPoint &key) const override;
189
    bool GetValue(Coin &coin) const override;
190
191
    bool Valid() const override;
192
    void Next() override;
193
194
private:
195
    std::unique_ptr<CDBIterator> pcursor;
196
    std::pair<char, COutPoint> keyTmp;
197
198
    friend class CCoinsViewDB;
199
};
200
201
std::unique_ptr<CCoinsViewCursor> CCoinsViewDB::Cursor() const
202
0
{
203
0
    auto i = std::make_unique<CCoinsViewDBCursor>(
204
0
        const_cast<CDBWrapper&>(*m_db).NewIterator(), GetBestBlock());
205
    /* It seems that there are no "const iterators" for LevelDB.  Since we
206
       only need read operations on it, use a const-cast to get around
207
       that restriction.  */
208
0
    i->pcursor->Seek(DB_COIN);
209
    // Cache key of first record
210
0
    if (i->pcursor->Valid()) {
211
0
        CoinEntry entry(&i->keyTmp.second);
212
0
        i->pcursor->GetKey(entry);
213
0
        i->keyTmp.first = entry.key;
214
0
    } else {
215
0
        i->keyTmp.first = 0; // Make sure Valid() and GetKey() return false
216
0
    }
217
0
    return i;
218
0
}
219
220
bool CCoinsViewDBCursor::GetKey(COutPoint &key) const
221
0
{
222
    // Return cached key
223
0
    if (keyTmp.first == DB_COIN) {
224
0
        key = keyTmp.second;
225
0
        return true;
226
0
    }
227
0
    return false;
228
0
}
229
230
bool CCoinsViewDBCursor::GetValue(Coin &coin) const
231
0
{
232
0
    return pcursor->GetValue(coin);
233
0
}
234
235
bool CCoinsViewDBCursor::Valid() const
236
0
{
237
0
    return keyTmp.first == DB_COIN;
238
0
}
239
240
void CCoinsViewDBCursor::Next()
241
0
{
242
0
    pcursor->Next();
243
0
    CoinEntry entry(&keyTmp.second);
244
0
    if (!pcursor->Valid() || !pcursor->GetKey(entry)) {
245
0
        keyTmp.first = 0; // Invalidate cached key after last record so that Valid() and GetKey() return false
246
0
    } else {
247
0
        keyTmp.first = entry.key;
248
0
    }
249
0
}