30 return GetCoin(outpoint).has_value();
47 return it->second.coin.IsSpent() ? std::nullopt : std::optional{it->second.coin};
56 m_sentinel.second.SelfRef(m_sentinel);
69 const auto [
ret, inserted] =
cacheCoins.try_emplace(outpoint);
72 ret->second.coin = std::move(*coin);
85 if (
auto it{
FetchCoin(outpoint)}; it !=
cacheCoins.end() && !it->second.coin.IsSpent())
return it->second.coin;
91 if (coin.out.scriptPubKey.IsUnspendable())
return;
92 CCoinsMap::iterator it;
94 std::tie(it, inserted) =
cacheCoins.emplace(std::piecewise_construct, std::forward_as_tuple(outpoint), std::tuple<>());
96 if (!possible_overwrite) {
97 if (!it->second.coin.IsSpent()) {
98 throw std::logic_error(
"Attempted to overwrite an unspent coin (when possible_overwrite is false)");
113 fresh = !it->second.IsDirty();
119 it->second.coin = std::move(coin);
126 (uint32_t)outpoint.
n,
127 (uint32_t)it->second.coin.nHeight,
128 (int64_t)it->second.coin.out.nValue,
129 (
bool)it->second.coin.IsCoinBase());
133 const auto mem_usage{coin.DynamicMemoryUsage()};
134 auto [it, inserted] =
cacheCoins.try_emplace(std::move(outpoint), std::move(coin));
145 for (
size_t i = 0; i < tx.
vout.size(); ++i) {
146 bool overwrite = check_for_overwrite ? cache.
HaveCoin(
COutPoint(txid, i)) : fCoinbase;
154 CCoinsMap::iterator it =
FetchCoin(outpoint);
160 (uint32_t)outpoint.
n,
161 (uint32_t)it->second.coin.nHeight,
162 (int64_t)it->second.coin.out.nValue,
163 (
bool)it->second.coin.IsCoinBase());
165 *moveout = std::move(it->second.coin);
167 if (it->second.IsFresh()) {
172 it->second.coin.Clear();
180 CCoinsMap::const_iterator it =
FetchCoin(outpoint);
184 return it->second.coin;
189 CCoinsMap::const_iterator it =
FetchCoin(outpoint);
190 return (it !=
cacheCoins.end() && !it->second.coin.IsSpent());
194 CCoinsMap::const_iterator it =
cacheCoins.find(outpoint);
195 return (it !=
cacheCoins.end() && !it->second.coin.IsSpent());
211 if (!it->second.IsDirty()) {
214 auto [itUs, inserted]{
cacheCoins.try_emplace(it->first)};
216 if (it->second.IsFresh() && it->second.coin.IsSpent()) {
222 assert(entry.coin.DynamicMemoryUsage() == 0);
226 entry.coin = std::move(it->second.coin);
228 entry.coin = it->second.coin;
240 if (it->second.IsFresh() && !itUs->second.coin.IsSpent()) {
245 throw std::logic_error(
"FRESH flag misapplied to coin that exists in parent cache");
248 if (itUs->second.IsFresh() && it->second.coin.IsSpent()) {
260 itUs->second.coin = std::move(it->second.coin);
262 itUs->second.coin = it->second.coin;
265 if (!itUs->second.IsDirty()) {
285 if (reallocate_cache) {
298 throw std::logic_error(
"Not all unspent flagged entries were cleared");
312 CCoinsMap::iterator it =
cacheCoins.find(hash);
313 if (it !=
cacheCoins.end() && !it->second.IsDirty()) {
318 (uint32_t)it->second.coin.nHeight,
319 (int64_t)it->second.coin.out.nValue,
320 (
bool)it->second.coin.IsCoinBase());
332 for (
unsigned int i = 0; i < tx.
vin.size(); i++) {
353 size_t recomputed_usage = 0;
354 size_t count_dirty = 0;
356 if (entry.coin.IsSpent()) {
357 assert(entry.IsDirty() && !entry.IsFresh());
359 assert(entry.IsDirty() || !entry.IsFresh());
363 recomputed_usage += entry.coin.DynamicMemoryUsage();
366 if (entry.IsDirty()) ++count_dirty;
369 size_t count_linked = 0;
372 assert(it->second.Next()->second.Prev() == it);
373 assert(it->second.Prev()->second.Next() == it);
375 assert(it->second.IsDirty());
391 if (!alternate.
IsSpent())
return alternate;
397 template <
typename ReturnType,
typename Func>
402 }
catch(
const std::runtime_error& e) {
403 for (
const auto& f : err_callbacks) {
406 LogError(
"Error reading from database: %s\n", e.what());
CoinsCachePair * NextAndMaybeErase(CoinsCachePair ¤t) noexcept
Return the next entry after current, possibly erasing current.
uint256 GetBestBlock() const override
Retrieve the block hash whose state this CCoinsView currently represents.
CCoinsViewCache(CCoinsView *baseIn, bool deterministic=false)
bool IsSpent() const
Either this coin never existed (see e.g.
static ReturnType ExecuteBackedWrapper(Func func, const std::vector< std::function< void()>> &err_callbacks)
constexpr bool TrySub(T &i, const U j) noexcept
static const int WITNESS_SCALE_FACTOR
CoinsCachePair m_sentinel
A Coin in one level of the coins database caching hierarchy.
const Coin & AccessCoin(const COutPoint &output) const
Return a reference to Coin in the cache, or coinEmpty if not found.
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.
std::vector< std::function< void()> > m_err_callbacks
A list of callbacks to execute upon leveldb read error.
CCoinsMapMemoryResource m_cache_coins_memory_resource
size_t DynamicMemoryUsage() const
Calculate the size of the cache (in bytes)
std::optional< Coin > PeekCoin(const COutPoint &outpoint) const override
Retrieve the Coin (unspent transaction output) for a given outpoint, without caching results...
bool WillErase(CoinsCachePair ¤t) const noexcept
std::vector< uint256 > GetHeadBlocks() const override
Retrieve the range of blocks that may have been only partially written.
bool HaveCoinInCache(const COutPoint &outpoint) const
Check if we have the given utxo already loaded in this cache.
void ReallocateCache()
Force a reallocation of the cache map.
virtual bool HaveCoin(const COutPoint &outpoint) const
Just check whether a given outpoint is unspent.
std::optional< Coin > PeekCoin(const COutPoint &outpoint) const override
Retrieve the Coin (unspent transaction output) for a given outpoint, without caching results...
static const Coin coinEmpty
consteval auto _(util::TranslatedLiteral str)
bool HaveInputs(const CTransaction &tx) const
Check whether all prevouts of the transaction are present in the UTXO set represented by this view...
std::optional< Coin > GetCoin(const COutPoint &outpoint) const override
Retrieve the Coin (unspent transaction output) for a given outpoint.
CCoinsMap::allocator_type::ResourceType CCoinsMapMemoryResource
const bool m_deterministic
bool SpendCoin(const COutPoint &outpoint, Coin *moveto=nullptr)
Spend a coin.
const std::vector< CTxIn > vin
const Coin & AccessByTxid(const CCoinsViewCache &view, const Txid &txid)
Utility function to find any unspent output with a given txid.
bool Func(const std::string &str, std::span< const char > &sp)
Parse a function call.
static const unsigned int MAX_BLOCK_WEIGHT
The maximum allowed weight for a block, see BIP 141 (network rule)
void SetBestBlock(const uint256 &hashBlock)
void AddCoins(CCoinsViewCache &cache, const CTransaction &tx, int nHeight, bool check_for_overwrite)
Utility function to add all of a transaction's outputs to a cache.
unsigned int GetCacheSize() const
Size of the cache (in number of transaction outputs)
void EmplaceCoinInternalDANGER(COutPoint &&outpoint, Coin &&coin)
Emplace a coin into cacheCoins without performing any checks, marking the emplaced coin as dirty...
Abstract view on the open txout dataset.
void Flush(bool reallocate_cache=true)
Push the modifications applied to this cache to its base and wipe local state.
Cursor for iterating over the linked list of flagged entries in CCoinsViewCache.
static const uint64_t MIN_TRANSACTION_OUTPUT_WEIGHT
std::unique_ptr< CCoinsViewCursor > Cursor() const override
Get a cursor to iterate over the whole state.
static const uint256 ZERO
std::optional< Coin > GetCoin(const COutPoint &outpoint) const override
Retrieve the Coin (unspent transaction output) for a given outpoint.
const std::vector< CTxOut > vout
uint64_t GetSerializeSize(const T &t)
virtual std::vector< uint256 > GetHeadBlocks() const
Retrieve the range of blocks that may have been only partially written.
virtual std::optional< Coin > PeekCoin(const COutPoint &outpoint) const
Retrieve the Coin (unspent transaction output) for a given outpoint, without caching results...
An output of a transaction.
virtual void BatchWrite(CoinsViewCacheCursor &cursor, const uint256 &hashBlock)
Do a bulk modification (multiple Coin changes + BestBlock change).
constexpr const std::byte * data() const
void BatchWrite(CoinsViewCacheCursor &cursor, const uint256 &hashBlock) override
Do a bulk modification (multiple Coin changes + BestBlock change).
An outpoint - a combination of a transaction hash and an index n into its vout.
constexpr bool IsNull() const
TRACEPOINT_SEMAPHORE(utxocache, add)
CCoinsViewBacked(CCoinsView *viewIn)
#define Assume(val)
Assume is the identity function.
void AddCoin(const COutPoint &outpoint, Coin &&coin, bool possible_overwrite)
Add a coin.
CoinsCachePair * Begin() const noexcept
uint256 GetBestBlock() const override
Retrieve the block hash whose state this CCoinsView currently represents.
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".
static const uint64_t MAX_OUTPUTS_PER_BLOCK
void Sync()
Push the modifications applied to this cache to its base while retaining the contents of this cache (...
static void SetDirty(CoinsCachePair &pair, CoinsCachePair &sentinel) noexcept
virtual size_t EstimateSize() const
Estimate database size (0 if not implemented)
#define TRACEPOINT(context,...)
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.
virtual std::optional< Coin > GetCoin(const COutPoint &outpoint) const
Retrieve the Coin (unspent transaction output) for a given outpoint.
std::optional< Coin > GetCoin(const COutPoint &outpoint) const override
Retrieve the Coin (unspent transaction output) for a given outpoint.
CCoinsMap::iterator FetchCoin(const COutPoint &outpoint) const
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.
static void SetFresh(CoinsCachePair &pair, CoinsCachePair &sentinel) noexcept
bool HaveCoin(const COutPoint &outpoint) const override
Just check whether a given outpoint is unspent.
void Reset() noexcept
Discard all modifications made to this cache without flushing to the base view.
#define Assert(val)
Identity function.
void BatchWrite(CoinsViewCacheCursor &cursor, const uint256 &hashBlock) override
Do a bulk modification (multiple Coin changes + BestBlock change).
const Txid & GetHash() const LIFETIMEBOUND
virtual std::optional< Coin > FetchCoinFromBase(const COutPoint &outpoint) const
std::optional< Coin > PeekCoin(const COutPoint &outpoint) const override
Retrieve the Coin (unspent transaction output) for a given outpoint, without caching results...
bool HaveCoin(const COutPoint &outpoint) const override
Just check whether a given outpoint is unspent.