Coverage Report

Created: 2026-04-29 19:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/tmp/bitcoin/src/coins.h
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
#ifndef BITCOIN_COINS_H
7
#define BITCOIN_COINS_H
8
9
#include <attributes.h>
10
#include <compressor.h>
11
#include <core_memusage.h>
12
#include <memusage.h>
13
#include <primitives/transaction.h>
14
#include <serialize.h>
15
#include <support/allocators/pool.h>
16
#include <uint256.h>
17
#include <util/check.h>
18
#include <util/overflow.h>
19
#include <util/hasher.h>
20
21
#include <cassert>
22
#include <cstdint>
23
24
#include <functional>
25
#include <unordered_map>
26
27
/**
28
 * A UTXO entry.
29
 *
30
 * Serialized format:
31
 * - VARINT((height << 1) | (coinbase ? 1 : 0))
32
 * - the non-spent CTxOut (via TxOutCompression)
33
 */
34
class Coin
35
{
36
public:
37
    //! unspent transaction output
38
    CTxOut out;
39
40
    //! whether containing transaction was a coinbase
41
    bool fCoinBase : 1;
42
43
    //! at which height this containing transaction was included in the active block chain
44
    uint32_t nHeight : 31;
45
46
    //! construct a Coin from a CTxOut and height/coinbase information.
47
66
    Coin(CTxOut&& outIn, int nHeightIn, bool fCoinBaseIn) : out(std::move(outIn)), fCoinBase(fCoinBaseIn), nHeight(nHeightIn) {}
48
21.7M
    Coin(const CTxOut& outIn, int nHeightIn, bool fCoinBaseIn) : out(outIn), fCoinBase(fCoinBaseIn),nHeight(nHeightIn) {}
49
50
13.7M
    void Clear() {
51
13.7M
        out.SetNull();
52
13.7M
        fCoinBase = false;
53
13.7M
        nHeight = 0;
54
13.7M
    }
55
56
    //! empty constructor
57
74.5M
    Coin() : fCoinBase(false), nHeight(0) { }
58
59
14.1M
    bool IsCoinBase() const {
60
14.1M
        return fCoinBase;
61
14.1M
    }
62
63
    template<typename Stream>
64
279k
    void Serialize(Stream &s) const {
65
279k
        assert(!IsSpent());
66
279k
        uint32_t code{(uint32_t{nHeight} << 1) | uint32_t{fCoinBase}};
67
279k
        ::Serialize(s, VARINT(code));
68
279k
        ::Serialize(s, Using<TxOutCompression>(out));
69
279k
    }
void Coin::Serialize<AutoFile>(AutoFile&) const
Line
Count
Source
64
6.58k
    void Serialize(Stream &s) const {
65
6.58k
        assert(!IsSpent());
66
6.58k
        uint32_t code{(uint32_t{nHeight} << 1) | uint32_t{fCoinBase}};
67
6.58k
        ::Serialize(s, VARINT(code));
68
6.58k
        ::Serialize(s, Using<TxOutCompression>(out));
69
6.58k
    }
void Coin::Serialize<DataStream>(DataStream&) const
Line
Count
Source
64
272k
    void Serialize(Stream &s) const {
65
272k
        assert(!IsSpent());
66
272k
        uint32_t code{(uint32_t{nHeight} << 1) | uint32_t{fCoinBase}};
67
272k
        ::Serialize(s, VARINT(code));
68
272k
        ::Serialize(s, Using<TxOutCompression>(out));
69
272k
    }
70
71
    template<typename Stream>
72
311k
    void Unserialize(Stream &s) {
73
311k
        uint32_t code = 0;
74
311k
        ::Unserialize(s, VARINT(code));
75
311k
        nHeight = code >> 1;
76
311k
        fCoinBase = code & 1;
77
311k
        ::Unserialize(s, Using<TxOutCompression>(out));
78
311k
    }
void Coin::Unserialize<SpanReader>(SpanReader&)
Line
Count
Source
72
83.5k
    void Unserialize(Stream &s) {
73
83.5k
        uint32_t code = 0;
74
83.5k
        ::Unserialize(s, VARINT(code));
75
83.5k
        nHeight = code >> 1;
76
83.5k
        fCoinBase = code & 1;
77
83.5k
        ::Unserialize(s, Using<TxOutCompression>(out));
78
83.5k
    }
void Coin::Unserialize<AutoFile>(AutoFile&)
Line
Count
Source
72
6.35k
    void Unserialize(Stream &s) {
73
6.35k
        uint32_t code = 0;
74
6.35k
        ::Unserialize(s, VARINT(code));
75
6.35k
        nHeight = code >> 1;
76
6.35k
        fCoinBase = code & 1;
77
6.35k
        ::Unserialize(s, Using<TxOutCompression>(out));
78
6.35k
    }
void Coin::Unserialize<DataStream>(DataStream&)
Line
Count
Source
72
221k
    void Unserialize(Stream &s) {
73
221k
        uint32_t code = 0;
74
221k
        ::Unserialize(s, VARINT(code));
75
221k
        nHeight = code >> 1;
76
221k
        fCoinBase = code & 1;
77
221k
        ::Unserialize(s, Using<TxOutCompression>(out));
78
221k
    }
79
80
    /** Either this coin never existed (see e.g. coinEmpty in coins.cpp), or it
81
      * did exist and has been spent.
82
      */
83
129M
    bool IsSpent() const {
84
129M
        return out.IsNull();
85
129M
    }
86
87
51.5M
    size_t DynamicMemoryUsage() const {
88
51.5M
        return memusage::DynamicUsage(out.scriptPubKey);
89
51.5M
    }
90
};
91
92
struct CCoinsCacheEntry;
93
using CoinsCachePair = std::pair<const COutPoint, CCoinsCacheEntry>;
94
95
/**
96
 * A Coin in one level of the coins database caching hierarchy.
97
 *
98
 * A coin can either be:
99
 * - unspent or spent (in which case the Coin object will be nulled out - see Coin.Clear())
100
 * - DIRTY or not DIRTY
101
 * - FRESH or not FRESH
102
 *
103
 * Out of these 2^3 = 8 states, only some combinations are valid:
104
 * - unspent, FRESH, DIRTY (e.g. a new coin created in the cache)
105
 * - unspent, not FRESH, DIRTY (e.g. a coin changed in the cache during a reorg)
106
 * - unspent, not FRESH, not DIRTY (e.g. an unspent coin fetched from the parent cache)
107
 * - spent, not FRESH, DIRTY (e.g. a coin is spent and spentness needs to be flushed to the parent)
108
 */
109
struct CCoinsCacheEntry
110
{
111
private:
112
    /**
113
     * These are used to create a doubly linked list of flagged entries.
114
     * They are set in SetDirty, SetFresh, and unset in SetClean.
115
     * A flagged entry is any entry that is either DIRTY, FRESH, or both.
116
     *
117
     * DIRTY entries are tracked so that only modified entries can be passed to
118
     * the parent cache for batch writing. This is a performance optimization
119
     * compared to giving all entries in the cache to the parent and having the
120
     * parent scan for only modified entries.
121
     */
122
    CoinsCachePair* m_prev{nullptr};
123
    CoinsCachePair* m_next{nullptr};
124
    uint8_t m_flags{0};
125
126
    //! Adding a flag requires a reference to the sentinel of the flagged pair linked list.
127
    static void AddFlags(uint8_t flags, CoinsCachePair& pair, CoinsCachePair& sentinel) noexcept
128
57.1M
    {
129
57.1M
        Assume(flags & (DIRTY | FRESH));
130
57.1M
        if (!pair.second.m_flags) {
131
35.6M
            Assume(!pair.second.m_prev && !pair.second.m_next);
132
35.6M
            pair.second.m_prev = sentinel.second.m_prev;
133
35.6M
            pair.second.m_next = &sentinel;
134
35.6M
            sentinel.second.m_prev = &pair;
135
35.6M
            pair.second.m_prev->second.m_next = &pair;
136
35.6M
        }
137
57.1M
        Assume(pair.second.m_prev && pair.second.m_next);
138
57.1M
        pair.second.m_flags |= flags;
139
57.1M
    }
140
141
public:
142
    Coin coin; // The actual cached data.
143
144
    enum Flags {
145
        /**
146
         * DIRTY means the CCoinsCacheEntry is potentially different from the
147
         * version in the parent cache. Failure to mark a coin as DIRTY when
148
         * it is potentially different from the parent cache will cause a
149
         * consensus failure, since the coin's state won't get written to the
150
         * parent when the cache is flushed.
151
         */
152
        DIRTY = (1 << 0),
153
        /**
154
         * FRESH means the parent cache does not have this coin or that it is a
155
         * spent coin in the parent cache. If a FRESH coin in the cache is
156
         * later spent, it can be deleted entirely and doesn't ever need to be
157
         * flushed to the parent. This is a performance optimization. Marking a
158
         * coin as FRESH when it exists unspent in the parent cache will cause a
159
         * consensus failure, since it might not be deleted from the parent
160
         * when this cache is flushed.
161
         */
162
        FRESH = (1 << 1),
163
    };
164
165
67.1M
    CCoinsCacheEntry() noexcept = default;
166
24.5k
    explicit CCoinsCacheEntry(Coin&& coin_) noexcept : coin(std::move(coin_)) {}
167
    ~CCoinsCacheEntry()
168
67.2M
    {
169
67.2M
        SetClean();
170
67.2M
    }
171
172
35.6M
    static void SetDirty(CoinsCachePair& pair, CoinsCachePair& sentinel) noexcept { AddFlags(DIRTY, pair, sentinel); }
173
21.5M
    static void SetFresh(CoinsCachePair& pair, CoinsCachePair& sentinel) noexcept { AddFlags(FRESH, pair, sentinel); }
174
175
    void SetClean() noexcept
176
67.2M
    {
177
67.2M
        if (!m_flags) return;
178
36.0M
        m_next->second.m_prev = m_prev;
179
36.0M
        m_prev->second.m_next = m_next;
180
36.0M
        m_flags = 0;
181
36.0M
        m_prev = m_next = nullptr;
182
36.0M
    }
183
38.0M
    bool IsDirty() const noexcept { return m_flags & DIRTY; }
184
15.3M
    bool IsFresh() const noexcept { return m_flags & FRESH; }
185
186
    //! Only call Next when this entry is DIRTY, FRESH, or both
187
    CoinsCachePair* Next() const noexcept
188
1.13M
    {
189
1.13M
        Assume(m_flags);
190
1.13M
        return m_next;
191
1.13M
    }
192
193
    //! Only call Prev when this entry is DIRTY, FRESH, or both
194
    CoinsCachePair* Prev() const noexcept
195
83.1k
    {
196
83.1k
        Assume(m_flags);
197
83.1k
        return m_prev;
198
83.1k
    }
199
200
    //! Only use this for initializing the linked list sentinel
201
    void SelfRef(CoinsCachePair& pair) noexcept
202
373k
    {
203
373k
        Assume(&pair.second == this);
204
373k
        m_prev = &pair;
205
373k
        m_next = &pair;
206
        // Set sentinel to DIRTY so we can call Next on it
207
373k
        m_flags = DIRTY;
208
373k
    }
209
};
210
211
/**
212
 * PoolAllocator's MAX_BLOCK_SIZE_BYTES parameter here uses sizeof the data, and adds the size
213
 * of 4 pointers. We do not know the exact node size used in the std::unordered_node implementation
214
 * because it is implementation defined. Most implementations have an overhead of 1 or 2 pointers,
215
 * so nodes can be connected in a linked list, and in some cases the hash value is stored as well.
216
 * Using an additional sizeof(void*)*4 for MAX_BLOCK_SIZE_BYTES should thus be sufficient so that
217
 * all implementations can allocate the nodes from the PoolAllocator.
218
 */
219
using CCoinsMap = std::unordered_map<COutPoint,
220
                                     CCoinsCacheEntry,
221
                                     SaltedOutpointHasher,
222
                                     std::equal_to<COutPoint>,
223
                                     PoolAllocator<CoinsCachePair,
224
                                                   sizeof(CoinsCachePair) + sizeof(void*) * 4>>;
225
226
using CCoinsMapMemoryResource = CCoinsMap::allocator_type::ResourceType;
227
228
/** Cursor for iterating over CoinsView state */
229
class CCoinsViewCursor
230
{
231
public:
232
1.22k
    CCoinsViewCursor(const uint256& in_block_hash) : block_hash(in_block_hash) {}
233
1.22k
    virtual ~CCoinsViewCursor() = default;
234
235
    virtual bool GetKey(COutPoint &key) const = 0;
236
    virtual bool GetValue(Coin &coin) const = 0;
237
238
    virtual bool Valid() const = 0;
239
    virtual void Next() = 0;
240
241
    //! Get best block at the time this cursor was created
242
102
    const uint256& GetBestBlock() const { return block_hash; }
243
private:
244
    uint256 block_hash;
245
};
246
247
/**
248
 * Cursor for iterating over the linked list of flagged entries in CCoinsViewCache.
249
 *
250
 * This is a helper struct to encapsulate the diverging logic between a non-erasing
251
 * CCoinsViewCache::Sync and an erasing CCoinsViewCache::Flush. This allows the receiver
252
 * of CCoinsView::BatchWrite to iterate through the flagged entries without knowing
253
 * the caller's intent.
254
 *
255
 * However, the receiver can still call CoinsViewCacheCursor::WillErase to see if the
256
 * caller will erase the entry after BatchWrite returns. If so, the receiver can
257
 * perform optimizations such as moving the coin out of the CCoinsCachEntry instead
258
 * of copying it.
259
 */
260
struct CoinsViewCacheCursor
261
{
262
    //! If will_erase is not set, iterating through the cursor will erase spent coins from the map,
263
    //! and other coins will be unflagged (removing them from the linked list).
264
    //! If will_erase is set, the underlying map and linked list will not be modified,
265
    //! as the caller is expected to wipe the entire map anyway.
266
    //! This is an optimization compared to erasing all entries as the cursor iterates them when will_erase is set.
267
    //! Calling CCoinsMap::clear() afterwards is faster because a CoinsCachePair cannot be coerced back into a
268
    //! CCoinsMap::iterator to be erased, and must therefore be looked up again by key in the CCoinsMap before being erased.
269
    CoinsViewCacheCursor(size_t& dirty_count LIFETIMEBOUND,
270
                         CoinsCachePair& sentinel LIFETIMEBOUND,
271
                         CCoinsMap& map LIFETIMEBOUND,
272
                         bool will_erase) noexcept
273
125k
        : m_dirty_count(dirty_count), m_sentinel(sentinel), m_map(map), m_will_erase(will_erase) {}
274
275
125k
    inline CoinsCachePair* Begin() const noexcept { return m_sentinel.second.Next(); }
276
1.00M
    inline CoinsCachePair* End() const noexcept { return &m_sentinel; }
277
278
    //! Return the next entry after current, possibly erasing current
279
    inline CoinsCachePair* NextAndMaybeErase(CoinsCachePair& current) noexcept
280
883k
    {
281
883k
        const auto next_entry{current.second.Next()};
282
883k
        Assume(TrySub(m_dirty_count, current.second.IsDirty()));
283
        // If we are not going to erase the cache, we must still erase spent entries.
284
        // Otherwise, clear the state of the entry.
285
883k
        if (!m_will_erase) {
286
64.5k
            if (current.second.coin.IsSpent()) {
287
17.2k
                assert(current.second.coin.DynamicMemoryUsage() == 0); // scriptPubKey was already cleared in SpendCoin
288
17.2k
                m_map.erase(current.first);
289
47.2k
            } else {
290
47.2k
                current.second.SetClean();
291
47.2k
            }
292
64.5k
        }
293
883k
        return next_entry;
294
883k
    }
295
296
455k
    inline bool WillErase(CoinsCachePair& current) const noexcept { return m_will_erase || current.second.coin.IsSpent(); }
297
3.67k
    size_t GetDirtyCount() const noexcept { return m_dirty_count; }
298
3.67k
    size_t GetTotalCount() const noexcept { return m_map.size(); }
299
private:
300
    size_t& m_dirty_count;
301
    CoinsCachePair& m_sentinel;
302
    CCoinsMap& m_map;
303
    bool m_will_erase;
304
};
305
306
/** Pure abstract view on the open txout dataset. */
307
class CCoinsView
308
{
309
public:
310
    //! As we use CCoinsViews polymorphically, have a virtual destructor
311
416k
    virtual ~CCoinsView() = default;
312
313
    //! Retrieve the Coin (unspent transaction output) for a given outpoint.
314
    //! May populate the cache. Use PeekCoin() to perform a non-caching lookup.
315
    virtual std::optional<Coin> GetCoin(const COutPoint& outpoint) const = 0;
316
317
    //! Retrieve the Coin (unspent transaction output) for a given outpoint, without caching results.
318
    //! Does not populate the cache. Use GetCoin() to cache the result.
319
    virtual std::optional<Coin> PeekCoin(const COutPoint& outpoint) const = 0;
320
321
    //! Just check whether a given outpoint is unspent.
322
    //! May populate the cache. Use PeekCoin() to perform a non-caching lookup.
323
    virtual bool HaveCoin(const COutPoint& outpoint) const = 0;
324
325
    //! Retrieve the block hash whose state this CCoinsView currently represents
326
    virtual uint256 GetBestBlock() const = 0;
327
328
    //! Retrieve the range of blocks that may have been only partially written.
329
    //! If the database is in a consistent state, the result is the empty vector.
330
    //! Otherwise, a two-element vector is returned consisting of the new and
331
    //! the old block hash, in that order.
332
    virtual std::vector<uint256> GetHeadBlocks() const = 0;
333
334
    //! Do a bulk modification (multiple Coin changes + BestBlock change).
335
    //! The passed cursor is used to iterate through the coins.
336
    virtual void BatchWrite(CoinsViewCacheCursor& cursor, const uint256& block_hash) = 0;
337
338
    //! Get a cursor to iterate over the whole state. Implementations may return nullptr.
339
    virtual std::unique_ptr<CCoinsViewCursor> Cursor() const = 0;
340
341
    //! Estimate database size
342
    virtual size_t EstimateSize() const = 0;
343
};
344
345
/** Noop coins view. */
346
class CoinsViewEmpty : public CCoinsView
347
{
348
protected:
349
4
    CoinsViewEmpty() = default;
350
351
public:
352
    static CoinsViewEmpty& Get();
353
354
    CoinsViewEmpty(const CoinsViewEmpty&) = delete;
355
    CoinsViewEmpty& operator=(const CoinsViewEmpty&) = delete;
356
357
28
    std::optional<Coin> GetCoin(const COutPoint&) const override { return {}; }
358
1
    std::optional<Coin> PeekCoin(const COutPoint& outpoint) const override { return GetCoin(outpoint); }
359
0
    bool HaveCoin(const COutPoint& outpoint) const override { return !!GetCoin(outpoint); }
360
0
    uint256 GetBestBlock() const override { return {}; }
361
0
    std::vector<uint256> GetHeadBlocks() const override { return {}; }
362
    void BatchWrite(CoinsViewCacheCursor& cursor, const uint256&) override
363
0
    {
364
0
        for (auto it{cursor.Begin()}; it != cursor.End(); it = cursor.NextAndMaybeErase(*it)) { }
365
0
    }
366
0
    std::unique_ptr<CCoinsViewCursor> Cursor() const override { return {}; }
367
0
    size_t EstimateSize() const override { return 0; }
368
};
369
370
/** CCoinsView backed by another CCoinsView */
371
class CCoinsViewBacked : public CCoinsView
372
{
373
protected:
374
    CCoinsView* base;
375
376
public:
377
415k
    explicit CCoinsViewBacked(CCoinsView* in_view) : base{Assert(in_view)} {}
378
379
68.8k
    void SetBackend(CCoinsView& in_view) { base = &in_view; }
380
381
889k
    std::optional<Coin> GetCoin(const COutPoint& outpoint) const override { return base->GetCoin(outpoint); }
382
381k
    std::optional<Coin> PeekCoin(const COutPoint& outpoint) const override { return base->PeekCoin(outpoint); }
383
0
    bool HaveCoin(const COutPoint& outpoint) const override { return base->HaveCoin(outpoint); }
384
35.0k
    uint256 GetBestBlock() const override { return base->GetBestBlock(); }
385
0
    std::vector<uint256> GetHeadBlocks() const override { return base->GetHeadBlocks(); }
386
3.38k
    void BatchWrite(CoinsViewCacheCursor& cursor, const uint256& block_hash) override { base->BatchWrite(cursor, block_hash); }
387
0
    std::unique_ptr<CCoinsViewCursor> Cursor() const override { return base->Cursor(); }
388
0
    size_t EstimateSize() const override { return base->EstimateSize(); }
389
};
390
391
392
/** CCoinsView that adds a memory cache for transactions to another CCoinsView */
393
class CCoinsViewCache : public CCoinsViewBacked
394
{
395
private:
396
    const bool m_deterministic;
397
398
protected:
399
    /**
400
     * Make mutable so that we can "fill the cache" even from Get-methods
401
     * declared as "const".
402
     */
403
    mutable uint256 m_block_hash;
404
    mutable CCoinsMapMemoryResource m_cache_coins_memory_resource{};
405
    /* The starting sentinel of the flagged entry circular doubly linked list. */
406
    mutable CoinsCachePair m_sentinel;
407
    mutable CCoinsMap cacheCoins;
408
409
    /* Cached dynamic memory usage for the inner Coin objects. */
410
    mutable size_t cachedCoinsUsage{0};
411
    /* Running count of dirty Coin cache entries. */
412
    mutable size_t m_dirty_count{0};
413
414
    /**
415
     * Discard all modifications made to this cache without flushing to the base view.
416
     * This can be used to efficiently reuse a cache instance across multiple operations.
417
     */
418
    void Reset() noexcept;
419
420
    /* Fetch the coin from base. Used for cache misses in FetchCoin. */
421
    virtual std::optional<Coin> FetchCoinFromBase(const COutPoint& outpoint) const;
422
423
public:
424
    CCoinsViewCache(CCoinsView* in_base, bool deterministic = false);
425
426
    /**
427
     * By deleting the copy constructor, we prevent accidentally using it when one intends to create a cache on top of a base cache.
428
     */
429
    CCoinsViewCache(const CCoinsViewCache &) = delete;
430
431
    // Standard CCoinsView methods
432
    std::optional<Coin> GetCoin(const COutPoint& outpoint) const override;
433
    std::optional<Coin> PeekCoin(const COutPoint& outpoint) const override;
434
    bool HaveCoin(const COutPoint& outpoint) const override;
435
    uint256 GetBestBlock() const override;
436
    void SetBestBlock(const uint256& block_hash);
437
    void BatchWrite(CoinsViewCacheCursor& cursor, const uint256& block_hash) override;
438
0
    std::unique_ptr<CCoinsViewCursor> Cursor() const override {
439
0
        throw std::logic_error("CCoinsViewCache cursor iteration not supported.");
440
0
    }
441
442
    /**
443
     * Check if we have the given utxo already loaded in this cache.
444
     * The semantics are the same as HaveCoin(), but no calls to
445
     * the backing CCoinsView are made.
446
     */
447
    bool HaveCoinInCache(const COutPoint &outpoint) const;
448
449
    /**
450
     * Return a reference to Coin in the cache, or coinEmpty if not found. This is
451
     * more efficient than GetCoin.
452
     *
453
     * Generally, do not hold the reference returned for more than a short scope.
454
     * While the current implementation allows for modifications to the contents
455
     * of the cache while holding the reference, this behavior should not be relied
456
     * on! To be safe, best to not hold the returned reference through any other
457
     * calls to this cache.
458
     */
459
    const Coin& AccessCoin(const COutPoint &output) const;
460
461
    /**
462
     * Add a coin. Set possible_overwrite to true if an unspent version may
463
     * already exist in the cache.
464
     */
465
    void AddCoin(const COutPoint& outpoint, Coin&& coin, bool possible_overwrite);
466
467
    /**
468
     * Emplace a coin into cacheCoins without performing any checks, marking
469
     * the emplaced coin as dirty.
470
     *
471
     * NOT FOR GENERAL USE. Used only when loading coins from a UTXO snapshot.
472
     * @sa ChainstateManager::PopulateAndValidateSnapshot()
473
     */
474
    void EmplaceCoinInternalDANGER(COutPoint&& outpoint, Coin&& coin);
475
476
    /**
477
     * Spend a coin. Pass moveto in order to get the deleted data.
478
     * If no unspent output exists for the passed outpoint, this call
479
     * has no effect.
480
     */
481
    bool SpendCoin(const COutPoint &outpoint, Coin* moveto = nullptr);
482
483
    /**
484
     * Push the modifications applied to this cache to its base and wipe local state.
485
     * Failure to call this method or Sync() before destruction will cause the changes
486
     * to be forgotten.
487
     * If reallocate_cache is false, the cache will retain the same memory footprint
488
     * after flushing and should be destroyed to deallocate.
489
     */
490
    void Flush(bool reallocate_cache = true);
491
492
    /**
493
     * Push the modifications applied to this cache to its base while retaining
494
     * the contents of this cache (except for spent coins, which we erase).
495
     * Failure to call this method or Flush() before destruction will cause the changes
496
     * to be forgotten.
497
     */
498
    void Sync();
499
500
    /**
501
     * Removes the UTXO with the given outpoint from the cache, if it is
502
     * not modified.
503
     */
504
    void Uncache(const COutPoint &outpoint);
505
506
    //! Size of the cache (in number of transaction outputs)
507
    unsigned int GetCacheSize() const;
508
509
    //! Number of dirty cache entries (transaction outputs)
510
3.36k
    size_t GetDirtyCount() const noexcept { return m_dirty_count; }
511
512
    //! Calculate the size of the cache (in bytes)
513
    size_t DynamicMemoryUsage() const;
514
515
    //! Check whether all prevouts of the transaction are present in the UTXO set represented by this view
516
    bool HaveInputs(const CTransaction& tx) const;
517
518
    //! Force a reallocation of the cache map. This is required when downsizing
519
    //! the cache because the map's allocator may be hanging onto a lot of
520
    //! memory despite having called .clear().
521
    //!
522
    //! See: https://stackoverflow.com/questions/42114044/how-to-release-unordered-map-memory
523
    void ReallocateCache();
524
525
    //! Run an internal sanity check on the cache data structure. */
526
    void SanityCheck() const;
527
528
    class ResetGuard
529
    {
530
    private:
531
        friend CCoinsViewCache;
532
        CCoinsViewCache& m_cache;
533
109k
        explicit ResetGuard(CCoinsViewCache& cache LIFETIMEBOUND) noexcept : m_cache{cache} {}
534
535
    public:
536
        ResetGuard(const ResetGuard&) = delete;
537
        ResetGuard& operator=(const ResetGuard&) = delete;
538
        ResetGuard(ResetGuard&&) = delete;
539
        ResetGuard& operator=(ResetGuard&&) = delete;
540
541
109k
        ~ResetGuard() { m_cache.Reset(); }
542
    };
543
544
    //! Create a scoped guard that will call `Reset()` on this cache when it goes out of scope.
545
109k
    [[nodiscard]] ResetGuard CreateResetGuard() noexcept { return ResetGuard{*this}; }
546
547
private:
548
    /**
549
     * @note this is marked const, but may actually append to `cacheCoins`, increasing
550
     * memory usage.
551
     */
552
    CCoinsMap::iterator FetchCoin(const COutPoint &outpoint) const;
553
};
554
555
/**
556
 * CCoinsViewCache overlay that avoids populating/mutating parent cache layers on cache misses.
557
 *
558
 * This is achieved by fetching coins from the base view using PeekCoin() instead of GetCoin(),
559
 * so intermediate CCoinsViewCache layers are not filled.
560
 *
561
 * Used during ConnectBlock() as an ephemeral, resettable top-level view that is flushed only
562
 * on success, so invalid blocks don't pollute the underlying cache.
563
 */
564
class CoinsViewOverlay : public CCoinsViewCache
565
{
566
private:
567
    std::optional<Coin> FetchCoinFromBase(const COutPoint& outpoint) const override
568
438k
    {
569
438k
        return base->PeekCoin(outpoint);
570
438k
    }
571
572
public:
573
    using CCoinsViewCache::CCoinsViewCache;
574
};
575
576
//! Utility function to add all of a transaction's outputs to a cache.
577
//! When check is false, this assumes that overwrites are only possible for coinbase transactions.
578
//! When check is true, the underlying view may be queried to determine whether an addition is
579
//! an overwrite.
580
// TODO: pass in a boolean to limit these possible overwrites to known
581
// (pre-BIP34) cases.
582
void AddCoins(CCoinsViewCache& cache, const CTransaction& tx, int nHeight, bool check = false);
583
584
//! Utility function to find any unspent output with a given txid.
585
//! This function can be quite expensive because in the event of a transaction
586
//! which is not found in the cache, it can cause up to MAX_OUTPUTS_PER_BLOCK
587
//! lookups to database, so it should be used with care.
588
const Coin& AccessByTxid(const CCoinsViewCache& cache, const Txid& txid);
589
590
/**
591
 * This is a minimally invasive approach to shutdown on LevelDB read errors from the
592
 * chainstate, while keeping user interface out of the common library, which is shared
593
 * between bitcoind, and bitcoin-qt and non-server tools.
594
 *
595
 * Writes do not need similar protection, as failure to write is handled by the caller.
596
*/
597
class CCoinsViewErrorCatcher final : public CCoinsViewBacked
598
{
599
public:
600
1.22k
    explicit CCoinsViewErrorCatcher(CCoinsView* view) : CCoinsViewBacked(view) {}
601
602
1.02k
    void AddReadErrCallback(std::function<void()> f) {
603
1.02k
        m_err_callbacks.emplace_back(std::move(f));
604
1.02k
    }
605
606
    std::optional<Coin> GetCoin(const COutPoint& outpoint) const override;
607
    bool HaveCoin(const COutPoint& outpoint) const override;
608
    std::optional<Coin> PeekCoin(const COutPoint& outpoint) const override;
609
610
private:
611
    /** A list of callbacks to execute upon leveldb read error. */
612
    std::vector<std::function<void()>> m_err_callbacks;
613
614
};
615
616
#endif // BITCOIN_COINS_H