Coverage Report

Created: 2024-08-21 05:08

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