Bitcoin Core 31.99.0
P2P Digital Currency
Classes | Public Member Functions | Protected Member Functions | Protected Attributes | Private Member Functions | Private Attributes | List of all members
CCoinsViewCache Class Reference

CCoinsView that adds a memory cache for transactions to another CCoinsView. More...

#include <coins.h>

Inheritance diagram for CCoinsViewCache:
[legend]
Collaboration diagram for CCoinsViewCache:
[legend]

Classes

class  ResetGuard
 

Public Member Functions

 CCoinsViewCache (CCoinsView *in_base, bool deterministic=false)
 
 CCoinsViewCache (const CCoinsViewCache &)=delete
 By deleting the copy constructor, we prevent accidentally using it when one intends to create a cache on top of a base cache. More...
 
std::optional< CoinGetCoin (const COutPoint &outpoint) const override
 Retrieve the Coin (unspent transaction output) for a given outpoint. More...
 
std::optional< CoinPeekCoin (const COutPoint &outpoint) const override
 Retrieve the Coin (unspent transaction output) for a given outpoint, without caching results. More...
 
bool HaveCoin (const COutPoint &outpoint) const override
 Just check whether a given outpoint is unspent. More...
 
uint256 GetBestBlock () const override
 Retrieve the block hash whose state this CCoinsView currently represents. More...
 
void SetBestBlock (const uint256 &block_hash)
 
void BatchWrite (CoinsViewCacheCursor &cursor, const uint256 &block_hash) override
 Do a bulk modification (multiple Coin changes + BestBlock change). More...
 
std::unique_ptr< CCoinsViewCursorCursor () const override
 Get a cursor to iterate over the whole state. Implementations may return nullptr. More...
 
bool HaveCoinInCache (const COutPoint &outpoint) const
 Check if we have the given utxo already loaded in this cache. More...
 
const CoinAccessCoin (const COutPoint &output) const
 Return a reference to Coin in the cache, or coinEmpty if not found. More...
 
void AddCoin (const COutPoint &outpoint, Coin &&coin, bool possible_overwrite)
 Add a coin. More...
 
void EmplaceCoinInternalDANGER (COutPoint &&outpoint, Coin &&coin)
 Emplace a coin into cacheCoins without performing any checks, marking the emplaced coin as dirty. More...
 
bool SpendCoin (const COutPoint &outpoint, Coin *moveto=nullptr)
 Spend a coin. More...
 
void Flush (bool reallocate_cache=true)
 Push the modifications applied to this cache to its base and wipe local state. More...
 
void 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). More...
 
void Uncache (const COutPoint &outpoint)
 Removes the UTXO with the given outpoint from the cache, if it is not modified. More...
 
unsigned int GetCacheSize () const
 Size of the cache (in number of transaction outputs) More...
 
size_t GetDirtyCount () const noexcept
 Number of dirty cache entries (transaction outputs) More...
 
size_t DynamicMemoryUsage () const
 Calculate the size of the cache (in bytes) More...
 
bool HaveInputs (const CTransaction &tx) const
 Check whether all prevouts of the transaction are present in the UTXO set represented by this view. More...
 
void ReallocateCache ()
 Force a reallocation of the cache map. More...
 
void SanityCheck () const
 Run an internal sanity check on the cache data structure. *‍/. More...
 
ResetGuard CreateResetGuard () noexcept
 Create a scoped guard that will call Reset() on this cache when it goes out of scope. More...
 
- Public Member Functions inherited from CCoinsViewBacked
 CCoinsViewBacked (CCoinsView *in_view)
 
void SetBackend (CCoinsView &in_view)
 
std::optional< CoinGetCoin (const COutPoint &outpoint) const override
 Retrieve the Coin (unspent transaction output) for a given outpoint. More...
 
std::optional< CoinPeekCoin (const COutPoint &outpoint) const override
 Retrieve the Coin (unspent transaction output) for a given outpoint, without caching results. More...
 
bool HaveCoin (const COutPoint &outpoint) const override
 Just check whether a given outpoint is unspent. More...
 
uint256 GetBestBlock () const override
 Retrieve the block hash whose state this CCoinsView currently represents. More...
 
std::vector< uint256GetHeadBlocks () const override
 Retrieve the range of blocks that may have been only partially written. More...
 
void BatchWrite (CoinsViewCacheCursor &cursor, const uint256 &block_hash) override
 Do a bulk modification (multiple Coin changes + BestBlock change). More...
 
std::unique_ptr< CCoinsViewCursorCursor () const override
 Get a cursor to iterate over the whole state. Implementations may return nullptr. More...
 
size_t EstimateSize () const override
 Estimate database size. More...
 
- Public Member Functions inherited from CCoinsView
virtual ~CCoinsView ()=default
 As we use CCoinsViews polymorphically, have a virtual destructor. More...
 
virtual std::optional< CoinGetCoin (const COutPoint &outpoint) const =0
 Retrieve the Coin (unspent transaction output) for a given outpoint. More...
 
virtual std::optional< CoinPeekCoin (const COutPoint &outpoint) const =0
 Retrieve the Coin (unspent transaction output) for a given outpoint, without caching results. More...
 
virtual bool HaveCoin (const COutPoint &outpoint) const =0
 Just check whether a given outpoint is unspent. More...
 
virtual uint256 GetBestBlock () const =0
 Retrieve the block hash whose state this CCoinsView currently represents. More...
 
virtual std::vector< uint256GetHeadBlocks () const =0
 Retrieve the range of blocks that may have been only partially written. More...
 
virtual void BatchWrite (CoinsViewCacheCursor &cursor, const uint256 &block_hash)=0
 Do a bulk modification (multiple Coin changes + BestBlock change). More...
 
virtual std::unique_ptr< CCoinsViewCursorCursor () const =0
 Get a cursor to iterate over the whole state. Implementations may return nullptr. More...
 
virtual size_t EstimateSize () const =0
 Estimate database size. More...
 

Protected Member Functions

void Reset () noexcept
 Discard all modifications made to this cache without flushing to the base view. More...
 
virtual std::optional< CoinFetchCoinFromBase (const COutPoint &outpoint) const
 

Protected Attributes

uint256 m_block_hash
 Make mutable so that we can "fill the cache" even from Get-methods declared as "const". More...
 
CCoinsMapMemoryResource m_cache_coins_memory_resource {}
 
CoinsCachePair m_sentinel
 
CCoinsMap cacheCoins
 
size_t cachedCoinsUsage {0}
 
size_t m_dirty_count {0}
 
- Protected Attributes inherited from CCoinsViewBacked
CCoinsViewbase
 

Private Member Functions

CCoinsMap::iterator FetchCoin (const COutPoint &outpoint) const
 

Private Attributes

const bool m_deterministic
 

Detailed Description

CCoinsView that adds a memory cache for transactions to another CCoinsView.

Definition at line 393 of file coins.h.

Constructor & Destructor Documentation

◆ CCoinsViewCache() [1/2]

CCoinsViewCache::CCoinsViewCache ( CCoinsView in_base,
bool  deterministic = false 
)

Definition at line 31 of file coins.cpp.

◆ CCoinsViewCache() [2/2]

CCoinsViewCache::CCoinsViewCache ( const CCoinsViewCache )
delete

By deleting the copy constructor, we prevent accidentally using it when one intends to create a cache on top of a base cache.

Member Function Documentation

◆ AccessCoin()

const Coin & CCoinsViewCache::AccessCoin ( const COutPoint output) const

Return a reference to Coin in the cache, or coinEmpty if not found.

This is more efficient than GetCoin.

Generally, do not hold the reference returned for more than a short scope. While the current implementation allows for modifications to the contents of the cache while holding the reference, this behavior should not be relied on! To be safe, best to not hold the returned reference through any other calls to this cache.

Definition at line 158 of file coins.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ AddCoin()

void CCoinsViewCache::AddCoin ( const COutPoint outpoint,
Coin &&  coin,
bool  possible_overwrite 
)

Add a coin.

Set possible_overwrite to true if an unspent version may already exist in the cache.

Definition at line 68 of file coins.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ BatchWrite()

void CCoinsViewCache::BatchWrite ( CoinsViewCacheCursor cursor,
const uint256 block_hash 
)
overridevirtual

Do a bulk modification (multiple Coin changes + BestBlock change).

The passed cursor is used to iterate through the coins.

Reimplemented from CCoinsViewBacked.

Definition at line 189 of file coins.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ CreateResetGuard()

ResetGuard CCoinsViewCache::CreateResetGuard ( )
inlinenoexcept

Create a scoped guard that will call Reset() on this cache when it goes out of scope.

Definition at line 545 of file coins.h.

Here is the caller graph for this function:

◆ Cursor()

std::unique_ptr< CCoinsViewCursor > CCoinsViewCache::Cursor ( ) const
inlineoverridevirtual

Get a cursor to iterate over the whole state. Implementations may return nullptr.

Reimplemented from CCoinsViewBacked.

Definition at line 438 of file coins.h.

◆ DynamicMemoryUsage()

size_t CCoinsViewCache::DynamicMemoryUsage ( ) const

Calculate the size of the cache (in bytes)

Definition at line 38 of file coins.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ EmplaceCoinInternalDANGER()

void CCoinsViewCache::EmplaceCoinInternalDANGER ( COutPoint &&  outpoint,
Coin &&  coin 
)

Emplace a coin into cacheCoins without performing any checks, marking the emplaced coin as dirty.

NOT FOR GENERAL USE. Used only when loading coins from a UTXO snapshot.

See also
ChainstateManager::PopulateAndValidateSnapshot()

Definition at line 111 of file coins.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ FetchCoin()

CCoinsMap::iterator CCoinsViewCache::FetchCoin ( const COutPoint outpoint) const
private
Note
this is marked const, but may actually append to cacheCoins, increasing memory usage.

Definition at line 47 of file coins.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ FetchCoinFromBase()

std::optional< Coin > CCoinsViewCache::FetchCoinFromBase ( const COutPoint outpoint) const
protectedvirtual

Reimplemented in CoinsViewOverlay.

Definition at line 42 of file coins.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Flush()

void CCoinsViewCache::Flush ( bool  reallocate_cache = true)

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.

Definition at line 260 of file coins.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetBestBlock()

uint256 CCoinsViewCache::GetBestBlock ( ) const
overridevirtual

Retrieve the block hash whose state this CCoinsView currently represents.

Reimplemented from CCoinsViewBacked.

Definition at line 178 of file coins.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetCacheSize()

unsigned int CCoinsViewCache::GetCacheSize ( ) const

Size of the cache (in number of transaction outputs)

Definition at line 306 of file coins.cpp.

Here is the caller graph for this function:

◆ GetCoin()

std::optional< Coin > CCoinsViewCache::GetCoin ( const COutPoint outpoint) const
overridevirtual

Retrieve the Coin (unspent transaction output) for a given outpoint.

May populate the cache. Use PeekCoin() to perform a non-caching lookup.

Reimplemented from CCoinsViewBacked.

Definition at line 62 of file coins.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetDirtyCount()

size_t CCoinsViewCache::GetDirtyCount ( ) const
inlinenoexcept

Number of dirty cache entries (transaction outputs)

Definition at line 510 of file coins.h.

◆ HaveCoin()

bool CCoinsViewCache::HaveCoin ( const COutPoint outpoint) const
overridevirtual

Just check whether a given outpoint is unspent.

May populate the cache. Use PeekCoin() to perform a non-caching lookup.

Reimplemented from CCoinsViewBacked.

Definition at line 167 of file coins.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ HaveCoinInCache()

bool CCoinsViewCache::HaveCoinInCache ( const COutPoint outpoint) const

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.

Definition at line 173 of file coins.cpp.

Here is the caller graph for this function:

◆ HaveInputs()

bool CCoinsViewCache::HaveInputs ( const CTransaction tx) const

Check whether all prevouts of the transaction are present in the UTXO set represented by this view.

Definition at line 310 of file coins.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ PeekCoin()

std::optional< Coin > CCoinsViewCache::PeekCoin ( const COutPoint outpoint) const
overridevirtual

Retrieve the Coin (unspent transaction output) for a given outpoint, without caching results.

Does not populate the cache. Use GetCoin() to cache the result.

Reimplemented from CCoinsViewBacked.

Definition at line 23 of file coins.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ReallocateCache()

void CCoinsViewCache::ReallocateCache ( )

Force a reallocation of the cache map.

This is required when downsizing the cache because the map's allocator may be hanging onto a lot of memory despite having called .clear().

See: https://stackoverflow.com/questions/42114044/how-to-release-unordered-map-memory

Definition at line 322 of file coins.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Reset()

void CCoinsViewCache::Reset ( )
protectednoexcept

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.

Definition at line 283 of file coins.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SanityCheck()

void CCoinsViewCache::SanityCheck ( ) const

Run an internal sanity check on the cache data structure. *‍/.

Definition at line 332 of file coins.cpp.

Here is the call graph for this function:

◆ SetBestBlock()

void CCoinsViewCache::SetBestBlock ( const uint256 block_hash)

Definition at line 184 of file coins.cpp.

Here is the caller graph for this function:

◆ SpendCoin()

bool CCoinsViewCache::SpendCoin ( const COutPoint outpoint,
Coin moveto = nullptr 
)

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.

Definition at line 132 of file coins.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Sync()

void CCoinsViewCache::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.

Definition at line 272 of file coins.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Uncache()

void CCoinsViewCache::Uncache ( const COutPoint outpoint)

Removes the UTXO with the given outpoint from the cache, if it is not modified.

Definition at line 291 of file coins.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ cacheCoins

CCoinsMap CCoinsViewCache::cacheCoins
mutableprotected

Definition at line 407 of file coins.h.

◆ cachedCoinsUsage

size_t CCoinsViewCache::cachedCoinsUsage {0}
mutableprotected

Definition at line 410 of file coins.h.

◆ m_block_hash

uint256 CCoinsViewCache::m_block_hash
mutableprotected

Make mutable so that we can "fill the cache" even from Get-methods declared as "const".

Definition at line 403 of file coins.h.

◆ m_cache_coins_memory_resource

CCoinsMapMemoryResource CCoinsViewCache::m_cache_coins_memory_resource {}
mutableprotected

Definition at line 404 of file coins.h.

◆ m_deterministic

const bool CCoinsViewCache::m_deterministic
private

Definition at line 396 of file coins.h.

◆ m_dirty_count

size_t CCoinsViewCache::m_dirty_count {0}
mutableprotected

Definition at line 412 of file coins.h.

◆ m_sentinel

CoinsCachePair CCoinsViewCache::m_sentinel
mutableprotected

Definition at line 406 of file coins.h.


The documentation for this class was generated from the following files: