42 CCoinsMap::iterator it =
cacheCoins.find(outpoint);
48 CCoinsMap::iterator
ret =
cacheCoins.emplace(std::piecewise_construct, std::forward_as_tuple(outpoint), std::forward_as_tuple(std::move(tmp))).first;
49 if (
ret->second.coin.IsSpent()) {
59 CCoinsMap::const_iterator it =
FetchCoin(outpoint);
61 coin = it->second.coin;
69 if (coin.out.scriptPubKey.IsUnspendable())
return;
70 CCoinsMap::iterator it;
72 std::tie(it, inserted) =
cacheCoins.emplace(std::piecewise_construct, std::forward_as_tuple(outpoint), std::tuple<>());
77 if (!possible_overwrite) {
78 if (!it->second.coin.IsSpent()) {
79 throw std::logic_error(
"Attempted to overwrite an unspent coin (when possible_overwrite is false)");
96 it->second.coin = std::move(coin);
101 (uint32_t)outpoint.
n,
102 (uint32_t)it->second.coin.nHeight,
103 (int64_t)it->second.coin.out.nValue,
104 (
bool)it->second.coin.IsCoinBase());
110 std::piecewise_construct,
111 std::forward_as_tuple(std::move(outpoint)),
118 for (
size_t i = 0; i < tx.
vout.size(); ++i) {
119 bool overwrite = check_for_overwrite ? cache.
HaveCoin(
COutPoint(txid, i)) : fCoinbase;
127 CCoinsMap::iterator it =
FetchCoin(outpoint);
132 (uint32_t)outpoint.
n,
133 (uint32_t)it->second.coin.nHeight,
134 (int64_t)it->second.coin.out.nValue,
135 (
bool)it->second.coin.IsCoinBase());
137 *moveout = std::move(it->second.coin);
143 it->second.coin.Clear();
151 CCoinsMap::const_iterator it =
FetchCoin(outpoint);
155 return it->second.coin;
160 CCoinsMap::const_iterator it =
FetchCoin(outpoint);
161 return (it !=
cacheCoins.end() && !it->second.coin.IsSpent());
165 CCoinsMap::const_iterator it =
cacheCoins.find(outpoint);
166 return (it !=
cacheCoins.end() && !it->second.coin.IsSpent());
180 for (CCoinsMap::iterator it = mapCoins.begin();
181 it != mapCoins.end();
182 it = erase ? mapCoins.erase(it) : std::next(it)) {
187 CCoinsMap::iterator itUs =
cacheCoins.find(it->first);
199 entry.
coin = std::move(it->second.coin);
201 entry.
coin = it->second.coin;
219 throw std::logic_error(
"FRESH flag misapplied to coin that exists in parent cache");
234 itUs->second.coin = std::move(it->second.coin);
236 itUs->second.coin = it->second.coin;
255 throw std::logic_error(
"Not all cached coins were erased");
267 if (it->second.coin.IsSpent()) {
271 it->second.flags = 0;
280 CCoinsMap::iterator it =
cacheCoins.find(hash);
281 if (it !=
cacheCoins.end() && it->second.flags == 0) {
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++) {
325 if (!alternate.
IsSpent())
return alternate;
334 }
catch(
const std::runtime_error& e) {
338 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.
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)
uint256 GetBestBlock() const override
Retrieve the block hash whose state this CCoinsView currently represents.
void SetBackend(CCoinsView &viewIn)
std::unique_ptr< CCoinsViewCursor > Cursor() const override
Get a cursor to iterate over the whole state.
bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock, bool erase=true) override
Do a bulk modification (multiple Coin changes + BestBlock change).
std::vector< uint256 > 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.
uint256 hashBlock
Make mutable so that we can "fill the cache" even from Get-methods declared as "const".
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.
CCoinsViewCache(CCoinsView *baseIn)
bool HaveInputs(const CTransaction &tx) const
Check whether all prevouts of the transaction are present in the UTXO set represented by this view.
void AddCoin(const COutPoint &outpoint, Coin &&coin, bool possible_overwrite)
Add a coin.
bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock, bool erase=true) override
Do a bulk modification (multiple Coin changes + BestBlock change).
unsigned int GetCacheSize() const
Calculate the size of the cache (in number of transaction outputs)
uint256 GetBestBlock() const override
Retrieve the block hash whose state this CCoinsView currently represents.
void SetBestBlock(const uint256 &hashBlock)
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.
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.
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 std::vector< uint256 > GetHeadBlocks() const
Retrieve the range of blocks that may have been only partially written.
virtual bool HaveCoin(const COutPoint &outpoint) const
Just check whether a given outpoint is unspent.
virtual size_t EstimateSize() const
Estimate database size (0 if not implemented)
virtual std::unique_ptr< CCoinsViewCursor > Cursor() const
Get a cursor to iterate over the whole state.
virtual bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock, bool erase=true)
Do a bulk modification (multiple Coin changes + BestBlock change).
virtual uint256 GetBestBlock() const
Retrieve the block hash whose state this CCoinsView currently represents.
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
const uint256 & GetHash() const
const std::vector< CTxIn > vin
An output of a transaction.
bool IsSpent() const
Either this coin never existed (see e.g.
size_t DynamicMemoryUsage() const
constexpr bool IsNull() const
constexpr const unsigned char * data() const
static const size_t MIN_TRANSACTION_OUTPUT_WEIGHT
static const Coin coinEmpty
const Coin & AccessByTxid(const CCoinsViewCache &view, const uint256 &txid)
Utility function to find any unspent output with a given txid.
static const size_t MAX_OUTPUTS_PER_BLOCK
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 > CCoinsMap
static const unsigned int MAX_BLOCK_WEIGHT
The maximum allowed weight for a block, see BIP 141 (network rule)
static const int WITNESS_SCALE_FACTOR
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)
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.
#define TRACE5(context, event, a, b, c, d, e)
static const int PROTOCOL_VERSION
network protocol versioning