CCoinsViewCache

CCoinsView that adds a memory cache for transactions to another CCoinsView

Synopsis

Declared in <coins.h>

class CCoinsViewCache
    : public CCoinsViewBacked

Base Classes

NameDescription
CCoinsViewBackedCCoinsView backed by another CCoinsView

Types

NameDescription
ResetGuard Scope guard that calls Reset() on its cache when it goes out of scope.

Member Functions

NameDescription
CCoinsViewCache [constructor]Constructors
AccessCoin Return a reference to Coin in the cache, or coinEmpty if not found. This is more efficient than GetCoin.
AddCoin Add a coin. Set possible_overwrite to true if an unspent version may already exist in the cache.
BatchWrite [virtual]Write flagged coins from a cursor into this cache and update the best block.
CreateResetGuard Create a scoped guard that will call Reset() on this cache when it goes out of scope.
DynamicMemoryUsage Calculate the size of the cache (in bytes)
EmplaceCoinInternalDANGER Emplace a coin into cacheCoins without performing any checks, marking the emplaced coin as dirty.
EstimateSize [virtual]Forward the size estimate to the backing view.
Flush [virtual]Push the modifications applied to this cache to its base and wipe local state. Failure to call this method or Sync() before destruction will cause the changes to be forgotten. If reallocate_cache is false, the cache will retain the same memory footprint after flushing and should be destroyed to deallocate.
GetBestBlock [virtual]Retrieve the best block hash this cache represents.
GetCacheSize Size of the cache (in number of transaction outputs)
GetCoin [virtual]Retrieve a coin, populating this cache on a hit.
GetDirtyCount Number of dirty cache entries (transaction outputs)
GetHeadBlocks [virtual]Forward the head-blocks query to the backing view.
HaveCoin [virtual]Check whether an outpoint is unspent, possibly populating this cache.
HaveCoinInCache Check if we have the given utxo already loaded in this cache. The semantics are the same as HaveCoin(), but no calls to the backing CCoinsView are made.
HaveInputs Check whether all prevouts of the transaction are present in the UTXO set represented by this view
PeekCoin [virtual]Retrieve a coin without populating this cache.
SanityCheck Run an internal sanity check on the cache data structure. */
SetBackend Replace the backing view.
SetBestBlock Set the best block hash this cache represents.
SpendCoin Spend a coin. Pass moveto in order to get the deleted data. If no unspent output exists for the passed outpoint, this call has no effect.
Sync Push the modifications applied to this cache to its base while retaining the contents of this cache (except for spent coins, which we erase). Failure to call this method or Flush() before destruction will cause the changes to be forgotten.
Uncache Removes the UTXO with the given outpoint from the cache, if it is not modified.

Protected Member Functions

NameDescription
FetchCoinFromBase [virtual]Fetch the coin from base. Used for cache misses in FetchCoin.
Reset [virtual]Discard all modifications made to this cache without flushing to the base view. This can be used to efficiently reuse a cache instance across multiple operations.

Protected Data Members

NameDescription
base The underlying view that all calls are forwarded to.
cacheCoins The map of cached coins, keyed by outpoint.
cachedCoinsUsage Cached dynamic memory usage for the inner Coin objects.
m_block_hash Make mutable so that we can "fill the cache" even from Get-methods declared as "const".
m_cache_coins_memory_resource Memory resource that backs the pool allocator used by cacheCoins.
m_dirty_count Running count of dirty Coin cache entries.
m_sentinel The starting sentinel of the flagged entry circular doubly linked list.

Non-Member Functions

NameDescription
AccessByTxidUtility function to find any unspent output with a given txid. This function can be quite expensive because in the event of a transaction which is not found in the cache, it can cause up to MAX_OUTPUTS_PER_BLOCK lookups to database, so it should be used with care.
AddCoinsUtility function to add all of a transaction's outputs to a cache. When check is false, this assumes that overwrites are only possible for coinbase transactions. When check is true, the underlying view may be queried to determine whether an addition is an overwrite.

Derived Classes

NameDescription
CoinsViewOverlay CCoinsViewCache subclass that asynchronously fetches most block input prevouts in parallel during ConnectBlock without mutating the base cache.