20 return std::vector<BlockHash>();
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;
110 CCoinsMap::iterator it;
114 std::forward_as_tuple(outpoint), std::tuple<>());
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);
152 std::piecewise_construct, std::forward_as_tuple(std::move(outpoint)),
160 for (
size_t i = 0; i < tx.
vout.size(); ++i) {
173 CCoinsMap::iterator it =
FetchCoin(outpoint);
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());
228 it = erase ?
mapCoins.erase(it) : std::next(it)) {
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) {
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++) {
381 if (entry.coin.IsSpent()) {
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 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.
const std::vector< CTxOut > vout
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.
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
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