6 #ifndef BITCOIN_COINS_H
7 #define BITCOIN_COINS_H
18 #include <unordered_map>
56 template <
typename Stream>
void Serialize(Stream &s)
const {
123 typedef std::unordered_map<COutPoint, CCoinsCacheEntry, SaltedOutpointHasher>
231 throw std::logic_error(
232 "CCoinsViewCache cursor iteration not supported.");
CCoinsView backed by another CCoinsView.
bool HaveCoin(const COutPoint &outpoint) const override
Just check whether a given outpoint is unspent.
BlockHash GetBestBlock() const override
Retrieve the block hash whose state this CCoinsView currently represents.
CCoinsViewCursor * Cursor() const override
Get a cursor to iterate over the whole state.
bool GetCoin(const COutPoint &outpoint, Coin &coin) const override
Retrieve the Coin (unspent transaction output) for a given outpoint.
size_t EstimateSize() const override
Estimate database size (0 if not implemented)
bool BatchWrite(CCoinsMap &mapCoins, const BlockHash &hashBlock) override
Do a bulk modification (multiple Coin changes + BestBlock change).
void SetBackend(CCoinsView &viewIn)
std::vector< BlockHash > GetHeadBlocks() const override
Retrieve the range of blocks that may have been only partially written.
CCoinsViewBacked(CCoinsView *viewIn)
CCoinsView that adds a memory cache for transactions to another CCoinsView.
void AddCoin(const COutPoint &outpoint, Coin coin, bool possible_overwrite)
Add a coin.
BlockHash GetBestBlock() const override
Retrieve the block hash whose state this CCoinsView currently represents.
bool SpendCoin(const COutPoint &outpoint, Coin *moveto=nullptr)
Spend a coin.
void Uncache(const COutPoint &outpoint)
Removes the UTXO with the given outpoint from the cache, if it is not modified.
CCoinsViewCursor * Cursor() const override
Get a cursor to iterate over the whole state.
CCoinsViewCache(CCoinsView *baseIn)
CCoinsViewCache(const CCoinsViewCache &)=delete
By deleting the copy constructor, we prevent accidentally using it when one intends to create a cache...
bool HaveInputs(const CTransaction &tx) const
Check whether all prevouts of the transaction are present in the UTXO set represented by this view.
void SetBestBlock(const BlockHash &hashBlock)
BlockHash hashBlock
Make mutable so that we can "fill the cache" even from Get-methods declared as "const".
unsigned int GetCacheSize() const
Calculate the size of the cache (in number of transaction outputs)
CCoinsMap::iterator FetchCoin(const COutPoint &outpoint) const
bool GetCoin(const COutPoint &outpoint, Coin &coin) const override
Retrieve the Coin (unspent transaction output) for a given outpoint.
bool HaveCoinInCache(const COutPoint &outpoint) const
Check if we have the given utxo already loaded in this cache.
bool Flush()
Push the modifications applied to this cache to its base.
size_t DynamicMemoryUsage() const
Calculate the size of the cache (in bytes)
bool BatchWrite(CCoinsMap &mapCoins, const BlockHash &hashBlock) override
Do a bulk modification (multiple Coin changes + BestBlock change).
void EmplaceCoinInternalDANGER(COutPoint &&outpoint, Coin &&coin)
Emplace a coin into cacheCoins without performing any checks, marking the emplaced coin as dirty.
bool HaveCoin(const COutPoint &outpoint) const override
Just check whether a given outpoint is unspent.
const Coin & AccessCoin(const COutPoint &output) const
Return a reference to Coin in the cache, or coinEmpty if not found.
void ReallocateCache()
Force a reallocation of the cache map.
Cursor for iterating over CoinsView state.
virtual bool Valid() const =0
CCoinsViewCursor(const BlockHash &hashBlockIn)
virtual unsigned int GetValueSize() const =0
virtual ~CCoinsViewCursor()
const BlockHash & GetBestBlock() const
Get best block at the time this cursor was created.
virtual bool GetKey(COutPoint &key) const =0
virtual bool GetValue(Coin &coin) const =0
This is a minimally invasive approach to shutdown on LevelDB read errors from the chainstate,...
void AddReadErrCallback(std::function< void()> f)
std::vector< std::function< void()> > m_err_callbacks
A list of callbacks to execute upon leveldb read error.
bool GetCoin(const COutPoint &outpoint, Coin &coin) const override
Retrieve the Coin (unspent transaction output) for a given outpoint.
CCoinsViewErrorCatcher(CCoinsView *view)
Abstract view on the open txout dataset.
virtual bool GetCoin(const COutPoint &outpoint, Coin &coin) const
Retrieve the Coin (unspent transaction output) for a given outpoint.
virtual CCoinsViewCursor * Cursor() const
Get a cursor to iterate over the whole state.
virtual std::vector< BlockHash > GetHeadBlocks() const
Retrieve the range of blocks that may have been only partially written.
virtual bool BatchWrite(CCoinsMap &mapCoins, const BlockHash &hashBlock)
Do a bulk modification (multiple Coin changes + BestBlock change).
virtual ~CCoinsView()
As we use CCoinsViews polymorphically, have a virtual destructor.
virtual BlockHash GetBestBlock() const
Retrieve the block hash whose state this CCoinsView currently represents.
virtual bool HaveCoin(const COutPoint &outpoint) const
Just check whether a given outpoint is unspent.
virtual size_t EstimateSize() const
Estimate database size (0 if not implemented)
An outpoint - a combination of a transaction hash and an index n into its vout.
The basic transaction that is broadcasted on the network and contained in blocks.
An output of a transaction.
const CTxOut & GetTxOut() const
uint32_t GetHeight() const
Coin(CTxOut outIn, uint32_t nHeightIn, bool IsCoinbase)
Constructor from a CTxOut and height/coinbase information.
void Serialize(Stream &s) const
uint32_t nHeightAndIsCoinBase
Whether containing transaction was a coinbase and height at which the transaction was included into a...
CTxOut out
Unspent transaction output.
void Unserialize(Stream &s)
size_t DynamicMemoryUsage() const
void AddCoins(CCoinsViewCache &cache, const CTransaction &tx, int nHeight, bool check=false)
Utility function to add all of a transaction's outputs to a cache.
std::unordered_map< COutPoint, CCoinsCacheEntry, SaltedOutpointHasher > CCoinsMap
const Coin & AccessByTxid(const CCoinsViewCache &cache, const TxId &txid)
Utility function to find any unspent output with a given txid.
static size_t DynamicUsage(const int8_t &v)
Dynamic memory usage for built-in types is zero.
Implement std::hash so RCUPtr can be used as a key for maps or sets.
A BlockHash is a unqiue identifier for a block.
A Coin in one level of the coins database caching hierarchy.
@ FRESH
FRESH means the parent cache does not have this coin or that it is a spent coin in the parent cache.
@ DIRTY
DIRTY means the CCoinsCacheEntry is potentially different from the version in the parent cache.
CCoinsCacheEntry(Coin &&coin_, uint8_t flag)
CCoinsCacheEntry(Coin coinIn)
A TxId is the identifier of a transaction.