Tuesday, December 5, 2023

supply code – What’s CCoinsCacheEntry within the Bitcoin Core?


CCoinsCacheEntry is applied right here.

/**
 * A Coin in a single degree of the cash database caching hierarchy.
 *
 * A coin can both be:
 * - unspent or spent (by which case the Coin object can be nulled out - see Coin.Clear())
 * - DIRTY or not DIRTY
 * - FRESH or not FRESH
 *
 * Out of those 2^3 = 8 states, just some combos are legitimate:
 * - unspent, FRESH, DIRTY (e.g. a brand new coin created within the cache)
 * - unspent, not FRESH, DIRTY (e.g. a coin modified within the cache throughout a reorg)
 * - unspent, not FRESH, not DIRTY (e.g. an unspent coin fetched from the dad or mum cache)
 * - spent, FRESH, not DIRTY (e.g. a spent coin fetched from the dad or mum cache)
 * - spent, not FRESH, DIRTY (e.g. a coin is spent and spentness must be flushed to the dad or mum)
 */
struct CCoinsCacheEntry
{
    Coin coin; // The precise cached information.
    unsigned char flags;

    enum Flags {
        /**
         * DIRTY means the CCoinsCacheEntry is probably completely different from the
         * model within the dad or mum cache. Failure to mark a coin as DIRTY when
         * it's probably completely different from the dad or mum cache will trigger a
         * consensus failure, for the reason that coin's state will not get written to the
         * dad or mum when the cache is flushed.
         */
        DIRTY = (1 << 0),
        /**
         * FRESH means the dad or mum cache doesn't have this coin or that it's a
         * spent coin within the dad or mum cache. If a FRESH coin within the cache is
         * later spent, it may be deleted totally and would not ever have to be
         * flushed to the dad or mum. This can be a efficiency optimization. Marking a
         * coin as FRESH when it exists unspent within the dad or mum cache will trigger a
         * consensus failure, because it won't be deleted from the dad or mum
         * when this cache is flushed.
         */
        FRESH = (1 << 1),
    };

    CCoinsCacheEntry() : flags(0) {}
    specific CCoinsCacheEntry(Coin&& coin_) : coin(std::transfer(coin_)), flags(0) {}
    CCoinsCacheEntry(Coin&& coin_, unsigned char flag) : coin(std::transfer(coin_)), flags(flag) {}
};

And is utilized in a map right here:

typedef std::unordered_map<COutPoint, CCoinsCacheEntry, SaltedOutpointHasher> CCoinsMap;

I can not perceive what’s CCoinsCacheEntry by studying the code and feedback. What does it imply by dad or mum?

DIRTY means the CCoinsCacheEntry is probably completely different from the
model within the dad or mum cache.

Can somebody clarify what’s it and the way it’s used? And in addition what does DIRTY and FRESH imply?

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles