20 return std::vector<BlockHash>();
64 CCoinsMap::key_equal{}, &m_cache_coins_memory_resource),
65 cachedCoinsUsage(0) {}
73 CCoinsMap::iterator it =
cacheCoins.find(outpoint);
81 CCoinsMap::iterator ret =
83 .emplace(std::piecewise_construct, std::forward_as_tuple(outpoint),
84 std::forward_as_tuple(std::move(tmp)))
86 if (ret->second.coin.IsSpent()) {
96 CCoinsMap::const_iterator it =
FetchCoin(outpoint);
100 coin = it->second.coin;
105 bool possible_overwrite) {
110 CCoinsMap::iterator it;
112 std::tie(it, inserted) =
114 std::forward_as_tuple(outpoint), std::tuple<>());
119 if (!possible_overwrite) {
120 if (!it->second.coin.IsSpent()) {
121 throw std::logic_error(
"Attempted to overwrite an unspent coin "
122 "(when possible_overwrite is false)");
139 it->second.coin = std::move(coin);
143 TRACE5(utxocache, add, outpoint.GetTxId().data(), outpoint.GetN(),
152 std::piecewise_construct, std::forward_as_tuple(std::move(outpoint)),
157 bool check_for_overwrite) {
158 bool fCoinbase = tx.IsCoinBase();
159 const TxId txid = tx.GetId();
160 for (
size_t i = 0; i < tx.vout.size(); ++i) {
161 const COutPoint outpoint(txid, i);
163 check_for_overwrite ? cache.
HaveCoin(outpoint) : fCoinbase;
173 CCoinsMap::iterator it =
FetchCoin(outpoint);
178 TRACE5(utxocache, spent, outpoint.GetTxId().data(), outpoint.GetN(),
179 it->second.coin.GetHeight(),
180 it->second.coin.GetTxOut().nValue.ToString().c_str(),
181 it->second.coin.IsCoinBase());
183 *moveout = std::move(it->second.coin);
189 it->second.coin.Clear();
197 CCoinsMap::const_iterator it =
FetchCoin(outpoint);
201 return it->second.coin;
205 CCoinsMap::const_iterator it =
FetchCoin(outpoint);
206 return it !=
cacheCoins.end() && !it->second.coin.IsSpent();
210 CCoinsMap::const_iterator it =
cacheCoins.find(outpoint);
211 return (it !=
cacheCoins.end() && !it->second.coin.IsSpent());
226 const BlockHash &hashBlockIn,
bool erase) {
227 for (CCoinsMap::iterator it = mapCoins.begin(); it != mapCoins.end();
228 it = erase ? mapCoins.erase(it) : std::next(it)) {
233 CCoinsMap::iterator itUs =
cacheCoins.find(it->first);
238 it->second.coin.IsSpent())) {
246 entry.
coin = std::move(it->second.coin);
248 entry.
coin = it->second.coin;
262 !itUs->second.coin.IsSpent()) {
267 throw std::logic_error(
"FRESH flag misapplied to coin that "
268 "exists in parent cache");
272 it->second.coin.IsSpent()) {
284 itUs->second.coin = std::move(it->second.coin);
286 itUs->second.coin = it->second.coin;
306 throw std::logic_error(
"Not all cached coins were erased");
319 if (it->second.coin.IsSpent()) {
323 it->second.flags = 0;
331 CCoinsMap::iterator it =
cacheCoins.find(outpoint);
332 if (it !=
cacheCoins.end() && it->second.flags == 0) {
334 TRACE5(utxocache, uncache, outpoint.GetTxId().data(), outpoint.GetN(),
335 it->second.coin.GetHeight(),
336 it->second.coin.GetTxOut().nValue.ToString().c_str(),
337 it->second.coin.IsCoinBase());
347 if (tx.IsCoinBase()) {
351 for (
size_t i = 0; i < tx.vin.size(); i++) {
372 size_t recomputed_usage = 0;
381 if (entry.coin.IsSpent()) {
385 assert(attr != 2 && attr != 4 && attr != 7);
388 recomputed_usage += entry.coin.DynamicMemoryUsage();
412 }
catch (
const std::runtime_error &e) {
416 LogPrintf(
"Error reading from database: %s\n", e.what());
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, bool erase=true) 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.
CCoinsViewCache(CCoinsView *baseIn, bool deterministic=false)
void AddCoin(const COutPoint &outpoint, Coin coin, bool possible_overwrite)
Add a coin.
const bool m_deterministic
BlockHash GetBestBlock() const override
Retrieve the block hash whose state this CCoinsView currently represents.
CCoinsMapMemoryResource m_cache_coins_memory_resource
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.
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)
bool BatchWrite(CCoinsMap &mapCoins, const BlockHash &hashBlock, bool erase=true) override
Do a bulk modification (multiple Coin changes + BestBlock change).
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 and wipe local state.
size_t DynamicMemoryUsage() const
Calculate the size of the cache (in bytes)
bool Sync()
Push the modifications applied to this cache to its base while retaining the contents of this cache (...
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.
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.
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.
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 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 bool BatchWrite(CCoinsMap &mapCoins, const BlockHash &hashBlock, bool erase=true)
Do a bulk modification (multiple Coin changes + BestBlock change).
virtual size_t EstimateSize() const
Estimate database size (0 if not implemented)
An output of a transaction.
uint32_t GetHeight() const
size_t DynamicMemoryUsage() const
static const size_t MAX_OUTPUTS_PER_TX
const Coin & AccessByTxid(const CCoinsViewCache &view, const TxId &txid)
Utility function to find any unspent output with a given txid.
static const Coin coinEmpty
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.
std::unordered_map< COutPoint, CCoinsCacheEntry, SaltedOutpointHasher, std::equal_to< COutPoint >, PoolAllocator< std::pair< const COutPoint, CCoinsCacheEntry >, sizeof(std::pair< const COutPoint, CCoinsCacheEntry >)+sizeof(void *) *4 > > CCoinsMap
PoolAllocator's MAX_BLOCK_SIZE_BYTES parameter here uses sizeof the data, and adds the size of 4 poin...
CCoinsMap::allocator_type::ResourceType CCoinsMapMemoryResource
static const uint64_t MAX_TX_SIZE
The maximum allowed size for a transaction, in bytes.
static size_t DynamicUsage(const int8_t &v)
Dynamic memory usage for built-in types is zero.
size_t GetSerializeSize(const T &t, int nVersion=0)
std::string ToString() const
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.
A TxId is the identifier of a transaction.
#define TRACE5(context, event, a, b, c, d, e)
bilingual_str _(const char *psz)
Translation function.
static const int PROTOCOL_VERSION
network protocol versioning