49 CCoinsMap::iterator ret =
cacheCoins.emplace(std::piecewise_construct, std::forward_as_tuple(outpoint), std::forward_as_tuple(std::move(tmp))).first;
50 if (ret->second.coin.IsSpent()) {
62 coin = it->second.coin;
69 assert(!coin.IsSpent());
70 if (coin.out.scriptPubKey.IsUnspendable())
return;
71 CCoinsMap::iterator
it;
73 std::tie(it, inserted) =
cacheCoins.emplace(std::piecewise_construct, std::forward_as_tuple(outpoint), std::tuple<>());
78 if (!possible_overwrite) {
79 if (!it->second.coin.IsSpent()) {
80 throw std::logic_error(
"Attempted to overwrite an unspent coin (when possible_overwrite is false)");
97 it->second.coin = std::move(coin);
105 for (
size_t i = 0; i < tx.
vout.size(); ++i) {
106 bool overwrite = check_for_overwrite ? cache.
HaveCoin(
COutPoint(txid, i)) : fCoinbase;
118 *moveout = std::move(it->second.coin);
124 it->second.coin.Clear();
132 CCoinsMap::const_iterator
it =
FetchCoin(outpoint);
136 return it->second.coin;
141 CCoinsMap::const_iterator
it =
FetchCoin(outpoint);
142 return (it !=
cacheCoins.end() && !it->second.coin.IsSpent());
146 CCoinsMap::const_iterator
it =
cacheCoins.find(outpoint);
147 return (it !=
cacheCoins.end() && !it->second.coin.IsSpent());
161 for (CCoinsMap::iterator
it = mapCoins.begin();
it != mapCoins.end();
it = mapCoins.erase(
it)) {
174 entry.
coin = std::move(
it->second.coin);
191 throw std::logic_error(
"FRESH flag misapplied to coin that exists in parent cache");
202 itUs->second.coin = std::move(
it->second.coin);
226 if (it !=
cacheCoins.end() && it->second.flags == 0) {
239 for (
unsigned int i = 0; i < tx.
vin.size(); i++) {
262 while (iter.
n < MAX_OUTPUTS_PER_BLOCK) {
264 if (!alternate.
IsSpent())
return alternate;
273 }
catch(
const std::runtime_error& e) {
274 for (
auto f : m_err_callbacks) {
277 LogPrintf(
"Error reading from database: %s\n", e.what());
const Coin & AccessByTxid(const CCoinsViewCache &view, const uint256 &txid)
Utility function to find any unspent output with a given txid.
uint64_t GetRand(uint64_t nMax) noexcept
Generate a uniform random integer in the range [0..range).
std::vector< uint256 > GetHeadBlocks() const override
Retrieve the range of blocks that may have been only partially written.
std::deque< CInv >::iterator it
CCoinsViewCache(CCoinsView *baseIn)
bool HaveCoinInCache(const COutPoint &outpoint) const
Check if we have the given utxo already loaded in this cache.
static const int WITNESS_SCALE_FACTOR
A Coin in one level of the coins database caching hierarchy.
uint256 GetBestBlock() const override
Retrieve the block hash whose state this CCoinsView currently represents.
bool Flush()
Push the modifications applied to this cache to its base.
void SetBackend(CCoinsView &viewIn)
bool HaveCoin(const COutPoint &outpoint) const override
Just check whether a given outpoint is unspent.
static size_t DynamicUsage(const int8_t &v)
Dynamic memory usage for built-in types is zero.
static void LogPrintf(const char *fmt, const Args &...args)
bool HaveInputs(const CTransaction &tx) const
Check whether all prevouts of the transaction are present in the UTXO set represented by this view...
virtual bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock)
Do a bulk modification (multiple Coin changes + BestBlock change).
unsigned int GetCacheSize() const
Calculate the size of the cache (in number of transaction outputs)
void ReallocateCache()
Force a reallocation of the cache map.
CCoinsViewCursor * Cursor() const override
Get a cursor to iterate over the whole state.
static const Coin coinEmpty
bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) override
Do a bulk modification (multiple Coin changes + BestBlock change).
virtual CCoinsViewCursor * Cursor() const
Get a cursor to iterate over the whole state.
bool SpendCoin(const COutPoint &outpoint, Coin *moveto=nullptr)
Spend a coin.
const std::vector< CTxIn > vin
size_t GetSerializeSize(const T &t, int nVersion=0)
DIRTY means the CCoinsCacheEntry is potentially different from the version in the parent cache...
virtual bool GetCoin(const COutPoint &outpoint, Coin &coin) const
Retrieve the Coin (unspent transaction output) for a given outpoint.
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.
virtual std::vector< uint256 > GetHeadBlocks() const
Retrieve the range of blocks that may have been only partially written.
bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) override
Do a bulk modification (multiple Coin changes + BestBlock change).
virtual uint256 GetBestBlock() const
Retrieve the block hash whose state this CCoinsView currently represents.
Abstract view on the open txout dataset.
std::unordered_map< COutPoint, CCoinsCacheEntry, SaltedOutpointHasher > CCoinsMap
const std::vector< CTxOut > vout
const Coin & AccessCoin(const COutPoint &output) const
Return a reference to Coin in the cache, or coinEmpty if not found.
static const unsigned char k1[32]
An output of a transaction.
An outpoint - a combination of a transaction hash and an index n into its vout.
CCoinsViewBacked(CCoinsView *viewIn)
void AddCoin(const COutPoint &outpoint, Coin &&coin, bool possible_overwrite)
Add a coin.
CCoinsMap::iterator FetchCoin(const COutPoint &outpoint) const
size_t EstimateSize() const override
Estimate database size (0 if not implemented)
size_t DynamicMemoryUsage() const
uint256 hashBlock
Make mutable so that we can "fill the cache" even from Get-methods declared as "const".
virtual bool HaveCoin(const COutPoint &outpoint) const
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.
uint256 GetBestBlock() const override
Retrieve the block hash whose state this CCoinsView currently represents.
virtual size_t EstimateSize() const
Estimate database size (0 if not implemented)
FRESH means the parent cache does not have this coin or that it is a spent coin in the parent cache...
static const int PROTOCOL_VERSION
network protocol versioning
bool HaveCoin(const COutPoint &outpoint) const override
Just check whether a given outpoint is unspent.
void Uncache(const COutPoint &outpoint)
Removes the UTXO with the given outpoint from the cache, if it is not modified.
bool GetCoin(const COutPoint &outpoint, Coin &coin) const override
Retrieve the Coin (unspent transaction output) for a given outpoint.
bool GetCoin(const COutPoint &outpoint, Coin &coin) const override
Retrieve the Coin (unspent transaction output) for a given outpoint.
static const size_t MAX_OUTPUTS_PER_BLOCK
const uint256 & GetHash() const
The basic transaction that is broadcasted on the network and contained in blocks. ...
CCoinsView backed by another CCoinsView.
CCoinsView that adds a memory cache for transactions to another CCoinsView.
static const size_t MIN_TRANSACTION_OUTPUT_WEIGHT
size_t DynamicMemoryUsage() const
Calculate the size of the cache (in bytes)
Cursor for iterating over CoinsView state.