Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

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

#include <coins.h>

+ Inheritance diagram for CCoinsViewCache:
+ Collaboration diagram for CCoinsViewCache:

Public Member Functions

 CCoinsViewCache (CCoinsView *baseIn)
 
 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...
 
bool GetCoin (const COutPoint &outpoint, Coin &coin) const override
 Retrieve the Coin (unspent transaction output) for a given outpoint. 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 &hashBlock)
 
bool BatchWrite (CCoinsMap &mapCoins, const uint256 &hashBlock) override
 Do a bulk modification (multiple Coin changes + BestBlock change). More...
 
CCoinsViewCursorCursor () const override
 Get a cursor to iterate over the whole state. 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 a pruned one if not found. More...
 
void AddCoin (const COutPoint &outpoint, Coin &&coin, bool potential_overwrite)
 Add a coin. More...
 
bool SpendCoin (const COutPoint &outpoint, Coin *moveto=nullptr)
 Spend a coin. More...
 
bool Flush ()
 Push the modifications applied to this cache to its base. 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
 Calculate the size of the cache (in number of transaction outputs) More...
 
size_t DynamicMemoryUsage () const
 Calculate the size of the cache (in bytes) More...
 
CAmount GetValueIn (const CTransaction &tx) const
 Amount of dash coming in to a transaction Note that lightweight clients may not know anything besides the hash of previous transactions, so may not be able to calculate this. 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...
 
- Public Member Functions inherited from CCoinsViewBacked
 CCoinsViewBacked (CCoinsView *viewIn)
 
std::vector< uint256GetHeadBlocks () const override
 Retrieve the range of blocks that may have been only partially written. More...
 
void SetBackend (CCoinsView &viewIn)
 
size_t EstimateSize () const override
 Estimate database size (0 if not implemented) More...
 
- Public Member Functions inherited from CCoinsView
virtual ~CCoinsView ()
 As we use CCoinsViews polymorphically, have a virtual destructor. More...
 

Protected Attributes

uint256 hashBlock
 Make mutable so that we can "fill the cache" even from Get-methods declared as "const". More...
 
CCoinsMap cacheCoins
 
size_t cachedCoinsUsage
 
- Protected Attributes inherited from CCoinsViewBacked
CCoinsViewbase
 

Private Member Functions

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

Detailed Description

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

Definition at line 201 of file coins.h.

Constructor & Destructor Documentation

◆ CCoinsViewCache() [1/2]

CCoinsViewCache::CCoinsViewCache ( CCoinsView baseIn)

Definition at line 34 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 a pruned one 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 116 of file coins.cpp.

References cacheCoins, coinEmpty, and FetchCoin().

Referenced by AcceptToMemoryPoolWorker(), AccessByTxid(), CTxMemPool::addAddressIndex(), CTxMemPool::addSpentIndex(), AreInputsStandard(), CheckInputs(), CheckInputsFromMempoolAndCache(), Consensus::CheckTxInputs(), combinerawtransaction(), CChainState::ConnectBlock(), CChainState::DisconnectBlock(), GetP2SHSigOpCount(), GetValueIn(), MutateTxSign(), CTxMemPool::removeForReorg(), sendrawtransaction(), and signrawtransaction().

◆ AddCoin()

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

Add a coin.

Set potential_overwrite to true if a non-pruned version may already exist.

Definition at line 66 of file coins.cpp.

References cacheCoins, cachedCoinsUsage, CCoinsCacheEntry::DIRTY, and CCoinsCacheEntry::FRESH.

Referenced by AddCoins(), ApplyTxInUndo(), MutateTxSign(), and signrawtransaction().

◆ BatchWrite()

bool CCoinsViewCache::BatchWrite ( CCoinsMap mapCoins,
const uint256 hashBlock 
)
overridevirtual

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

The passed mapCoins can be modified.

Reimplemented from CCoinsViewBacked.

Definition at line 145 of file coins.cpp.

References cacheCoins, cachedCoinsUsage, CCoinsCacheEntry::coin, CCoinsCacheEntry::DIRTY, Coin::DynamicMemoryUsage(), CCoinsCacheEntry::flags, CCoinsCacheEntry::FRESH, and hashBlock.

◆ Cursor()

CCoinsViewCursor* CCoinsViewCache::Cursor ( ) const
inlineoverridevirtual

Get a cursor to iterate over the whole state.

Reimplemented from CCoinsViewBacked.

Definition at line 228 of file coins.h.

◆ DynamicMemoryUsage()

size_t CCoinsViewCache::DynamicMemoryUsage ( ) const

Calculate the size of the cache (in bytes)

Definition at line 36 of file coins.cpp.

References cacheCoins, cachedCoinsUsage, and memusage::DynamicUsage().

Referenced by CVerifyDB::VerifyDB().

◆ FetchCoin()

CCoinsMap::iterator CCoinsViewCache::FetchCoin ( const COutPoint outpoint) const
private

◆ Flush()

bool CCoinsViewCache::Flush ( )

Push the modifications applied to this cache to its base.

Failure to call this method before destruction will cause the changes to be forgotten. If false is returned, the state of this cache (and its backing view) will be undefined.

Definition at line 203 of file coins.cpp.

References CCoinsViewBacked::base, CCoinsView::BatchWrite(), cacheCoins, cachedCoinsUsage, and hashBlock.

Referenced by CChainState::ReplayBlocks().

◆ GetBestBlock()

uint256 CCoinsViewCache::GetBestBlock ( ) const
overridevirtual

Retrieve the block hash whose state this CCoinsView currently represents.

Reimplemented from CCoinsViewBacked.

Definition at line 135 of file coins.cpp.

References CCoinsViewBacked::base, CCoinsView::GetBestBlock(), hashBlock, and base_blob< BITS >::IsNull().

Referenced by AcceptToMemoryPoolWorker(), CChainState::ConnectBlock(), GetSpendHeight(), and CVerifyDB::VerifyDB().

◆ GetCacheSize()

unsigned int CCoinsViewCache::GetCacheSize ( ) const

Calculate the size of the cache (in number of transaction outputs)

Definition at line 219 of file coins.cpp.

References cacheCoins.

◆ GetCoin()

bool CCoinsViewCache::GetCoin ( const COutPoint outpoint,
Coin coin 
) const
overridevirtual

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

Returns true only when an unspent coin was found, which is returned in coin. When false is returned, coin's value is unspecified.

Reimplemented from CCoinsViewBacked.

Definition at line 57 of file coins.cpp.

References cacheCoins, FetchCoin(), and Coin::IsSpent().

Referenced by rest_getutxos().

◆ GetValueIn()

CAmount CCoinsViewCache::GetValueIn ( const CTransaction tx) const

Amount of dash coming in to a transaction Note that lightweight clients may not know anything besides the hash of previous transactions, so may not be able to calculate this.

Parameters
[in]txtransaction for which we are checking input total
Returns
Sum of value of all inputs (scriptSigs)

Definition at line 223 of file coins.cpp.

References AccessCoin(), CTransaction::IsCoinBase(), CTxOut::nValue, Coin::out, and CTransaction::vin.

Referenced by CCoinsCaching().

◆ HaveCoin()

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

Just check whether a given outpoint is unspent.

Reimplemented from CCoinsViewBacked.

Definition at line 125 of file coins.cpp.

References cacheCoins, and FetchCoin().

Referenced by AcceptToMemoryPoolWorker(), AddCoins(), ApplyTxInUndo(), CTxMemPool::check(), CChainState::ConnectBlock(), and HaveInputs().

◆ 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 130 of file coins.cpp.

References cacheCoins.

◆ 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 235 of file coins.cpp.

References HaveCoin(), CTransaction::IsCoinBase(), and CTransaction::vin.

Referenced by CTxMemPool::check(), and Consensus::CheckTxInputs().

◆ SetBestBlock()

void CCoinsViewCache::SetBestBlock ( const uint256 hashBlock)

◆ 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 98 of file coins.cpp.

References cacheCoins, cachedCoinsUsage, CCoinsCacheEntry::DIRTY, FetchCoin(), and CCoinsCacheEntry::FRESH.

Referenced by CChainState::DisconnectBlock(), CChainState::RollforwardBlock(), and UpdateCoins().

◆ 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 210 of file coins.cpp.

References cacheCoins, and cachedCoinsUsage.

Member Data Documentation

◆ cacheCoins

CCoinsMap CCoinsViewCache::cacheCoins
mutableprotected

◆ cachedCoinsUsage

size_t CCoinsViewCache::cachedCoinsUsage
mutableprotected

Definition at line 212 of file coins.h.

Referenced by AddCoin(), BatchWrite(), DynamicMemoryUsage(), FetchCoin(), Flush(), SpendCoin(), and Uncache().

◆ hashBlock

uint256 CCoinsViewCache::hashBlock
mutableprotected

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

Definition at line 208 of file coins.h.

Referenced by BatchWrite(), Flush(), GetBestBlock(), and SetBestBlock().


The documentation for this class was generated from the following files:
Released under the MIT license