24#include <unordered_map>
62 template<
typename Stream>
70 template<
typename Stream>
129 if (!pair.second.m_flags) {
130 Assume(!pair.second.m_prev && !pair.second.m_next);
131 pair.second.m_prev = sentinel.second.m_prev;
132 pair.second.m_next = &sentinel;
133 sentinel.second.m_prev = &pair;
134 pair.second.m_prev->second.m_next = &pair;
136 Assume(pair.second.m_prev && pair.second.m_next);
137 pair.second.m_flags |=
flags;
202 Assume(&pair.second ==
this);
221 std::equal_to<COutPoint>,
270 bool will_erase) noexcept
279 const auto next_entry{current.second.Next()};
283 if (current.second.coin.IsSpent()) {
284 assert(current.second.coin.DynamicMemoryUsage() == 0);
285 m_map.erase(current.first);
287 current.second.SetClean();
324 virtual std::unique_ptr<CCoinsViewCursor>
Cursor()
const;
348 std::unique_ptr<CCoinsViewCursor>
Cursor()
const override;
377 void Reset() noexcept;
394 throw std::logic_error(
"CCoinsViewCache cursor iteration not supported.");
445 void Flush(
bool reallocate_cache =
true);
#define Assume(val)
Assume is the identity function.
CCoinsView backed by another CCoinsView.
void BatchWrite(CoinsViewCacheCursor &cursor, const uint256 &hashBlock) override
Do a bulk modification (multiple Coin changes + BestBlock change).
bool HaveCoin(const COutPoint &outpoint) const override
Just check whether a given outpoint is unspent.
size_t EstimateSize() const override
Estimate database size (0 if not implemented)
uint256 GetBestBlock() const override
Retrieve the block hash whose state this CCoinsView currently represents.
std::optional< Coin > GetCoin(const COutPoint &outpoint) const override
Retrieve the Coin (unspent transaction output) for a given outpoint.
void SetBackend(CCoinsView &viewIn)
std::unique_ptr< CCoinsViewCursor > Cursor() const override
Get a cursor to iterate over the whole state.
std::vector< uint256 > GetHeadBlocks() const override
Retrieve the range of blocks that may have been only partially written.
CCoinsViewBacked(CCoinsView *viewIn)
CCoinsViewCache & m_cache
ResetGuard(const ResetGuard &)=delete
ResetGuard & operator=(ResetGuard &&)=delete
ResetGuard(ResetGuard &&)=delete
ResetGuard & operator=(const ResetGuard &)=delete
ResetGuard(CCoinsViewCache &cache LIFETIMEBOUND) noexcept
CCoinsView that adds a memory cache for transactions to another CCoinsView.
void Sync()
Push the modifications applied to this cache to its base while retaining the contents of this cache (...
const bool m_deterministic
uint256 hashBlock
Make mutable so that we can "fill the cache" even from Get-methods declared as "const".
std::unique_ptr< CCoinsViewCursor > Cursor() const override
Get a cursor to iterate over the whole state.
CCoinsMapMemoryResource m_cache_coins_memory_resource
bool SpendCoin(const COutPoint &outpoint, Coin *moveto=nullptr)
Spend a coin.
ResetGuard CreateResetGuard() noexcept
Create a scoped guard that will call Reset() on this cache when it goes out of scope.
void Uncache(const COutPoint &outpoint)
Removes the UTXO with the given outpoint from the cache, if it is not modified.
bool HaveInputs(const CTransaction &tx) const
Check whether all prevouts of the transaction are present in the UTXO set represented by this view.
void AddCoin(const COutPoint &outpoint, Coin &&coin, bool possible_overwrite)
Add a coin.
void Flush(bool reallocate_cache=true)
Push the modifications applied to this cache to its base and wipe local state.
void Reset() noexcept
Discard all modifications made to this cache without flushing to the base view.
unsigned int GetCacheSize() const
Calculate the size of the cache (in number of transaction outputs)
uint256 GetBestBlock() const override
Retrieve the block hash whose state this CCoinsView currently represents.
void SetBestBlock(const uint256 &hashBlock)
CCoinsMap::iterator FetchCoin(const COutPoint &outpoint) const
bool HaveCoinInCache(const COutPoint &outpoint) const
Check if we have the given utxo already loaded in this cache.
CoinsCachePair m_sentinel
size_t DynamicMemoryUsage() const
Calculate the size of the cache (in bytes)
void BatchWrite(CoinsViewCacheCursor &cursor, const uint256 &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.
void SanityCheck() const
Run an internal sanity check on the cache data structure. */.
std::optional< Coin > GetCoin(const COutPoint &outpoint) const override
Retrieve the Coin (unspent transaction output) for a given outpoint.
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.
const uint256 & GetBestBlock() const
Get best block at the time this cursor was created.
virtual bool Valid() const =0
CCoinsViewCursor(const uint256 &hashBlockIn)
virtual ~CCoinsViewCursor()=default
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::optional< Coin > GetCoin(const COutPoint &outpoint) const override
Retrieve the Coin (unspent transaction output) for a given outpoint.
std::vector< std::function< void()> > m_err_callbacks
A list of callbacks to execute upon leveldb read error.
bool HaveCoin(const COutPoint &outpoint) const override
Just check whether a given outpoint is unspent.
CCoinsViewErrorCatcher(CCoinsView *view)
Abstract view on the open txout dataset.
virtual std::optional< Coin > GetCoin(const COutPoint &outpoint) const
Retrieve the Coin (unspent transaction output) for a given outpoint.
virtual void BatchWrite(CoinsViewCacheCursor &cursor, const uint256 &hashBlock)
Do a bulk modification (multiple Coin changes + BestBlock change).
virtual std::vector< uint256 > GetHeadBlocks() const
Retrieve the range of blocks that may have been only partially written.
virtual ~CCoinsView()=default
As we use CCoinsViews polymorphically, have a virtual destructor.
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)
virtual std::unique_ptr< CCoinsViewCursor > Cursor() const
Get a cursor to iterate over the whole state.
virtual uint256 GetBestBlock() const
Retrieve the block hash whose state this CCoinsView currently represents.
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.
void Serialize(Stream &s) const
Coin(CTxOut &&outIn, int nHeightIn, bool fCoinBaseIn)
construct a Coin from a CTxOut and height/coinbase information.
CTxOut out
unspent transaction output
bool IsSpent() const
Either this coin never existed (see e.g.
Coin(const CTxOut &outIn, int nHeightIn, bool fCoinBaseIn)
void Unserialize(Stream &s)
uint32_t nHeight
at which height this containing transaction was included in the active block chain
size_t DynamicMemoryUsage() const
unsigned int fCoinBase
whether containing transaction was a coinbase
Forwards all allocations/deallocations to the PoolResource.
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::pair< const COutPoint, CCoinsCacheEntry > CoinsCachePair
std::unordered_map< COutPoint, CCoinsCacheEntry, SaltedOutpointHasher, std::equal_to< COutPoint >, PoolAllocator< CoinsCachePair, sizeof(CoinsCachePair)+sizeof(void *) *4 > > CCoinsMap
PoolAllocator's MAX_BLOCK_SIZE_BYTES parameter here uses sizeof the data, and adds the size of 4 poin...
const Coin & AccessByTxid(const CCoinsViewCache &cache, const Txid &txid)
Utility function to find any unspent output with a given txid.
CCoinsMap::allocator_type::ResourceType CCoinsMapMemoryResource
static size_t DynamicUsage(const int8_t &v)
Dynamic memory usage for built-in types is zero.
A Coin in one level of the coins database caching hierarchy.
CCoinsCacheEntry() noexcept=default
@ 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.
static void SetFresh(CoinsCachePair &pair, CoinsCachePair &sentinel) noexcept
static void AddFlags(uint8_t flags, CoinsCachePair &pair, CoinsCachePair &sentinel) noexcept
Adding a flag requires a reference to the sentinel of the flagged pair linked list.
bool IsFresh() const noexcept
static void SetDirty(CoinsCachePair &pair, CoinsCachePair &sentinel) noexcept
void SelfRef(CoinsCachePair &pair) noexcept
Only use this for initializing the linked list sentinel.
bool IsDirty() const noexcept
CoinsCachePair * m_prev
These are used to create a doubly linked list of flagged entries.
CoinsCachePair * Next() const noexcept
Only call Next when this entry is DIRTY, FRESH, or both.
CoinsCachePair * Prev() const noexcept
Only call Prev when this entry is DIRTY, FRESH, or both.
Cursor for iterating over the linked list of flagged entries in CCoinsViewCache.
CoinsCachePair & m_sentinel
CoinsCachePair * NextAndMaybeErase(CoinsCachePair ¤t) noexcept
Return the next entry after current, possibly erasing current.
CoinsViewCacheCursor(CoinsCachePair &sentinel LIFETIMEBOUND, CCoinsMap &map LIFETIMEBOUND, bool will_erase) noexcept
If will_erase is not set, iterating through the cursor will erase spent coins from the map,...
bool WillErase(CoinsCachePair ¤t) const noexcept
CoinsCachePair * Begin() const noexcept
CoinsCachePair * End() const noexcept