6 #ifndef BITCOIN_COINS_H 7 #define BITCOIN_COINS_H 23 #include <unordered_map> 61 template<
typename Stream>
69 template<
typename Stream>
167 Assume(&
self.second ==
this);
169 m_prev = sentinel.second.m_prev;
171 sentinel.second.m_prev = &
self;
172 m_prev->second.m_next = &
self;
202 Assume(&
self.second ==
this);
221 std::equal_to<COutPoint>,
237 virtual bool Valid()
const = 0;
238 virtual void Next() = 0;
271 bool will_erase) noexcept
280 const auto next_entry{current.second.Next()};
284 if (current.second.coin.IsSpent()) {
285 m_usage -= current.second.coin.DynamicMemoryUsage();
286 m_map.erase(current.first);
288 current.second.ClearFlags();
329 virtual std::unique_ptr<CCoinsViewCursor>
Cursor()
const;
353 std::unique_ptr<CCoinsViewCursor>
Cursor()
const override;
392 std::unique_ptr<CCoinsViewCursor>
Cursor()
const override {
393 throw std::logic_error(
"CCoinsViewCache cursor iteration not supported.");
526 #endif // BITCOIN_COINS_H CoinsCachePair * NextAndMaybeErase(CoinsCachePair ¤t) noexcept
Return the next entry after current, possibly erasing current.
bool GetCoin(const COutPoint &outpoint, Coin &coin) const override
Retrieve the Coin (unspent transaction output) for a given outpoint.
uint256 GetBestBlock() const override
Retrieve the block hash whose state this CCoinsView currently represents.
void SelfRef(CoinsCachePair &self) noexcept
Only use this for initializing the linked list sentinel.
CCoinsViewCache(CCoinsView *baseIn, bool deterministic=false)
bool IsSpent() const
Either this coin never existed (see e.g.
CoinsCachePair m_sentinel
A Coin in one level of the coins database caching hierarchy.
virtual bool GetCoin(const COutPoint &outpoint, Coin &coin) const
Retrieve the Coin (unspent transaction output) for a given outpoint.
const Coin & AccessByTxid(const CCoinsViewCache &cache, const Txid &txid)
Utility function to find any unspent output with a given txid.
const Coin & AccessCoin(const COutPoint &output) const
Return a reference to Coin in the cache, or coinEmpty if not found.
bool Flush()
Push the modifications applied to this cache to its base and wipe local state.
void SetBackend(CCoinsView &viewIn)
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...
CoinsCachePair * End() const noexcept
static size_t DynamicUsage(const int8_t &v)
Dynamic memory usage for built-in types is zero.
virtual bool BatchWrite(CoinsViewCacheCursor &cursor, const uint256 &hashBlock)
Do a bulk modification (multiple Coin changes + BestBlock change).
std::vector< std::function< void()> > m_err_callbacks
A list of callbacks to execute upon leveldb read error.
CCoinsMapMemoryResource m_cache_coins_memory_resource
bool BatchWrite(CoinsViewCacheCursor &cursor, const uint256 &hashBlock) override
Do a bulk modification (multiple Coin changes + BestBlock change).
CoinsCachePair * m_prev
These are used to create a doubly linked list of flagged entries.
void AddReadErrCallback(std::function< void()> f)
size_t DynamicMemoryUsage() const
Calculate the size of the cache (in bytes)
CTxOut out
unspent transaction output
bool WillErase(CoinsCachePair ¤t) const noexcept
std::vector< uint256 > GetHeadBlocks() const override
Retrieve the range of blocks that may have been only partially written.
virtual ~CCoinsViewCursor()=default
unsigned int fCoinBase
whether containing transaction was a coinbase
bool HaveCoinInCache(const COutPoint &outpoint) const
Check if we have the given utxo already loaded in this cache.
bool IsFresh() const noexcept
void ReallocateCache()
Force a reallocation of the cache map.
virtual bool HaveCoin(const COutPoint &outpoint) const
Just check whether a given outpoint is unspent.
bool HaveInputs(const CTransaction &tx) const
Check whether all prevouts of the transaction are present in the UTXO set represented by this view...
void Serialize(Stream &s) const
CCoinsMap::allocator_type::ResourceType CCoinsMapMemoryResource
const bool m_deterministic
bool SpendCoin(const COutPoint &outpoint, Coin *moveto=nullptr)
Spend a coin.
DIRTY means the CCoinsCacheEntry is potentially different from the version in the parent cache...
CCoinsViewErrorCatcher(CCoinsView *view)
void ClearFlags() noexcept
void SetBestBlock(const uint256 &hashBlock)
virtual bool GetValue(Coin &coin) const =0
CoinsCachePair & m_sentinel
uint32_t nHeight
at which height this containing transaction was included in the active block chain ...
unsigned int GetCacheSize() const
Calculate the size of the cache (in number of transaction outputs)
CCoinsCacheEntry() noexcept=default
std::pair< const COutPoint, CCoinsCacheEntry > CoinsCachePair
void EmplaceCoinInternalDANGER(COutPoint &&outpoint, Coin &&coin)
Emplace a coin into cacheCoins without performing any checks, marking the emplaced coin as dirty...
virtual bool Valid() const =0
Abstract view on the open txout dataset.
CoinsCachePair * Next() const noexcept
Only call Next when this entry is DIRTY, FRESH, or both.
Cursor for iterating over the linked list of flagged entries in CCoinsViewCache.
std::unique_ptr< CCoinsViewCursor > Cursor() const override
Get a cursor to iterate over the whole state.
void AddFlags(uint8_t flags, CoinsCachePair &self, CoinsCachePair &sentinel) noexcept
Adding a flag also requires a self reference to the pair that contains this entry in the CCoinsCache ...
virtual std::vector< uint256 > GetHeadBlocks() const
Retrieve the range of blocks that may have been only partially written.
An output of a transaction.
CoinsCachePair * Prev() const noexcept
Only call Prev when this entry is DIRTY, FRESH, or both.
An outpoint - a combination of a transaction hash and an index n into its vout.
virtual ~CCoinsView()=default
As we use CCoinsViews polymorphically, have a virtual destructor.
CCoinsViewBacked(CCoinsView *viewIn)
#define Assume(val)
Assume is the identity function.
void AddCoin(const COutPoint &outpoint, Coin &&coin, bool possible_overwrite)
Add a coin.
virtual bool GetKey(COutPoint &key) const =0
std::unique_ptr< CCoinsViewCursor > Cursor() const override
Get a cursor to iterate over the whole state.
CoinsCachePair * Begin() const noexcept
uint256 GetBestBlock() const override
Retrieve the block hash whose state this CCoinsView currently represents.
Forwards all allocations/deallocations to the PoolResource.
bool HaveCoin(const COutPoint &outpoint) const override
Just check whether a given outpoint is unspent.
uint256 hashBlock
Make mutable so that we can "fill the cache" even from Get-methods declared as "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.
bool GetCoin(const COutPoint &outpoint, Coin &coin) const override
Retrieve the Coin (unspent transaction output) for a given outpoint.
void Unserialize(Stream &s)
FRESH means the parent cache does not have this coin or that it is a spent coin in the parent cache...
virtual size_t EstimateSize() const
Estimate database size (0 if not implemented)
CoinsViewCacheCursor(size_t &usage LIFETIMEBOUND, 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...
void Uncache(const COutPoint &outpoint)
Removes the UTXO with the given outpoint from the cache, if it is not modified.
virtual uint256 GetBestBlock() const
Retrieve the block hash whose state this CCoinsView currently represents.
size_t DynamicMemoryUsage() const
bool GetCoin(const COutPoint &outpoint, Coin &coin) const override
Retrieve the Coin (unspent transaction output) for a given outpoint.
bool BatchWrite(CoinsViewCacheCursor &cursor, const uint256 &hashBlock) override
Do a bulk modification (multiple Coin changes + BestBlock change).
CCoinsMap::iterator FetchCoin(const COutPoint &outpoint) const
CCoinsViewCursor(const uint256 &hashBlockIn)
virtual std::unique_ptr< CCoinsViewCursor > Cursor() const
Get a cursor to iterate over the whole state.
The basic transaction that is broadcasted on the network and contained in blocks. ...
void SanityCheck() const
Run an internal sanity check on the cache data structure. */.
CCoinsView backed by another CCoinsView.
size_t EstimateSize() const override
Estimate database size (0 if not implemented)
CCoinsView that adds a memory cache for transactions to another CCoinsView.
bool HaveCoin(const COutPoint &outpoint) const override
Just check whether a given outpoint is unspent.
const uint256 & GetBestBlock() const
Get best block at the time this cursor was created.
uint8_t GetFlags() const noexcept
bool IsDirty() const noexcept
This is a minimally invasive approach to shutdown on LevelDB read errors from the chainstate...
Coin(const CTxOut &outIn, int nHeightIn, bool fCoinBaseIn)
bool Sync()
Push the modifications applied to this cache to its base while retaining the contents of this cache (...
transaction_identifier represents the two canonical transaction identifier types (txid, wtxid).
bool HaveCoin(const COutPoint &outpoint) const override
Just check whether a given outpoint is unspent.
Coin(CTxOut &&outIn, int nHeightIn, bool fCoinBaseIn)
construct a Coin from a CTxOut and height/coinbase information.
Cursor for iterating over CoinsView state.