38 m_sentinel.second.SelfRef(m_sentinel);
46 const auto [
ret, inserted] =
cacheCoins.try_emplace(outpoint);
52 if (
ret->second.coin.IsSpent()) {
62 CCoinsMap::const_iterator it =
FetchCoin(outpoint);
64 coin = it->second.coin;
72 if (coin.out.scriptPubKey.IsUnspendable())
return;
73 CCoinsMap::iterator it;
75 std::tie(it, inserted) =
cacheCoins.emplace(std::piecewise_construct, std::forward_as_tuple(outpoint), std::tuple<>());
80 if (!possible_overwrite) {
81 if (!it->second.coin.IsSpent()) {
82 throw std::logic_error(
"Attempted to overwrite an unspent coin (when possible_overwrite is false)");
97 fresh = !it->second.IsDirty();
99 it->second.coin = std::move(coin);
104 (uint32_t)outpoint.
n,
105 (uint32_t)it->second.coin.nHeight,
106 (int64_t)it->second.coin.out.nValue,
107 (
bool)it->second.coin.IsCoinBase());
113 std::piecewise_construct,
114 std::forward_as_tuple(std::move(outpoint)),
115 std::forward_as_tuple(std::move(coin)));
124 for (
size_t i = 0; i < tx.
vout.size(); ++i) {
125 bool overwrite = check_for_overwrite ? cache.
HaveCoin(
COutPoint(txid, i)) : fCoinbase;
133 CCoinsMap::iterator it =
FetchCoin(outpoint);
138 (uint32_t)outpoint.
n,
139 (uint32_t)it->second.coin.nHeight,
140 (int64_t)it->second.coin.out.nValue,
141 (
bool)it->second.coin.IsCoinBase());
143 *moveout = std::move(it->second.coin);
145 if (it->second.IsFresh()) {
149 it->second.coin.Clear();
157 CCoinsMap::const_iterator it =
FetchCoin(outpoint);
161 return it->second.coin;
166 CCoinsMap::const_iterator it =
FetchCoin(outpoint);
167 return (it !=
cacheCoins.end() && !it->second.coin.IsSpent());
171 CCoinsMap::const_iterator it =
cacheCoins.find(outpoint);
172 return (it !=
cacheCoins.end() && !it->second.coin.IsSpent());
188 if (!it->second.IsDirty()) {
191 CCoinsMap::iterator itUs =
cacheCoins.find(it->first);
195 if (!(it->second.IsFresh() && it->second.coin.IsSpent())) {
198 itUs =
cacheCoins.try_emplace(it->first).first;
203 entry.
coin = std::move(it->second.coin);
205 entry.coin = it->second.coin;
212 if (it->second.IsFresh()) {
218 if (it->second.IsFresh() && !itUs->second.coin.IsSpent()) {
223 throw std::logic_error(
"FRESH flag misapplied to coin that exists in parent cache");
226 if (itUs->second.IsFresh() && it->second.coin.IsSpent()) {
237 itUs->second.coin = std::move(it->second.coin);
239 itUs->second.coin = it->second.coin;
272 throw std::logic_error(
"Not all unspent flagged entries were cleared");
280 CCoinsMap::iterator it =
cacheCoins.find(hash);
281 if (it !=
cacheCoins.end() && !it->second.IsDirty() && !it->second.IsFresh()) {
283 TRACE5(utxocache, uncache,
286 (uint32_t)it->second.coin.nHeight,
287 (int64_t)it->second.coin.out.nValue,
288 (
bool)it->second.coin.IsCoinBase());
300 for (
unsigned int i = 0; i < tx.
vin.size(); i++) {
321 size_t recomputed_usage = 0;
322 size_t count_flagged = 0;
325 if (entry.IsDirty()) attr |= 1;
326 if (entry.IsFresh()) attr |= 2;
327 if (entry.coin.IsSpent()) attr |= 4;
329 assert(attr != 2 && attr != 4 && attr != 7);
332 recomputed_usage += entry.coin.DynamicMemoryUsage();
335 if (entry.IsDirty() || entry.IsFresh()) ++count_flagged;
338 size_t count_linked = 0;
341 assert(it->second.Next()->second.Prev() == it);
342 assert(it->second.Prev()->second.Next() == it);
344 assert(it->second.IsDirty() || it->second.IsFresh());
348 assert(count_linked == count_flagged);
360 if (!alternate.
IsSpent())
return alternate;
366 template <
typename Func>
371 }
catch(
const std::runtime_error& e) {
372 for (
const auto& f : err_callbacks) {
375 LogError(
"Error reading from database: %s\n", e.what());
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.
CCoinsViewCache(CCoinsView *baseIn, bool deterministic=false)
bool IsSpent() const
Either this coin never existed (see e.g.
static const int WITNESS_SCALE_FACTOR
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 & 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).
size_t GetSerializeSize(const T &t)
size_t DynamicMemoryUsage() const
Calculate the size of the cache (in bytes)
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.
static const Coin coinEmpty
bool HaveInputs(const CTransaction &tx) const
Check whether all prevouts of the transaction are present in the UTXO set represented by this view...
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
bool Func(const std::string &str, Span< const char > &sp)
Parse a function call.
DIRTY means the CCoinsCacheEntry is potentially different from the version in the parent cache...
const Coin & AccessByTxid(const CCoinsViewCache &view, const Txid &txid)
Utility function to find any unspent output with a given txid.
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
Calculate the 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.
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.
const std::vector< CTxOut > vout
virtual std::vector< uint256 > GetHeadBlocks() const
Retrieve the range of blocks that may have been only partially written.
An output of a transaction.
static bool ExecuteBackedWrapper(Func func, const std::vector< std::function< void()>> &err_callbacks)
constexpr const std::byte * data() const
An outpoint - a combination of a transaction hash and an index n into its vout.
constexpr bool IsNull() const
CCoinsViewBacked(CCoinsView *viewIn)
void AddCoin(const COutPoint &outpoint, Coin &&coin, bool possible_overwrite)
Add a coin.
#define TRACE5(context, event, a, b, c, d, e)
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".
bool GetCoin(const COutPoint &outpoint, Coin &coin) const override
Retrieve the Coin (unspent transaction output) for a given outpoint.
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)
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.
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).
static const size_t MAX_OUTPUTS_PER_BLOCK
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.
bool HaveCoin(const COutPoint &outpoint) const override
Just check whether a given outpoint is unspent.
static const size_t MIN_TRANSACTION_OUTPUT_WEIGHT
bilingual_str _(ConstevalStringLiteral str)
Translation function.
bool Sync()
Push the modifications applied to this cache to its base while retaining the contents of this cache (...
const Txid & GetHash() const LIFETIMEBOUND
bool HaveCoin(const COutPoint &outpoint) const override
Just check whether a given outpoint is unspent.