34const Coin EMPTY_COIN{};
52 struct CacheCoinSnapshot {
57 bool operator==(
const CacheCoinSnapshot&)
const =
default;
60 std::vector<CacheCoinSnapshot> ComputeCacheCoinsSnapshot()
const
62 std::vector<CacheCoinSnapshot> snapshot;
63 snapshot.reserve(cacheCoins.size());
65 for (
const auto& [outpoint, entry] : cacheCoins) {
66 snapshot.emplace_back(outpoint, entry.IsDirty(), entry.IsFresh(), entry.coin);
69 std::ranges::sort(snapshot, std::less<>{}, &CacheCoinSnapshot::outpoint);
73 mutable std::vector<CacheCoinSnapshot> m_expected_snapshot{ComputeCacheCoinsSnapshot()};
79 assert(ComputeCacheCoinsSnapshot() == m_expected_snapshot);
81 m_expected_snapshot = ComputeCacheCoinsSnapshot();
90 static const auto testing_setup = MakeNoLogFileContext<>();
95 const bool is_db{
dynamic_cast<CCoinsViewDB*
>(backend_coins_view) !=
nullptr};
97 auto* original_backend{backend_coins_view};
112 Coin coin{random_coin};
115 const bool possible_overwrite{coins_view_cache.PeekCoin(outpoint) || fuzzed_data_provider.ConsumeBool()};
116 coins_view_cache.
AddCoin(outpoint, std::move(coin), possible_overwrite);
125 coins_view_cache.
Sync();
130 if (is_db && best_block.IsNull()) best_block =
uint256::ONE;
138 if (best_block.IsNull()) {
150 coins_view_cache.
Uncache(random_out_point);
154 if (use_original_backend && backend_coins_view != original_backend) {
162 coins_view_cache.
SetBackend(*backend_coins_view);
165 const std::optional<COutPoint> opt_out_point = ConsumeDeserializable<COutPoint>(
fuzzed_data_provider);
166 if (!opt_out_point) {
170 random_out_point = *opt_out_point;
178 random_coin = *opt_coin;
182 if (!opt_mutable_transaction) {
186 random_mutable_transaction = *opt_mutable_transaction;
190 sentinel.second.SelfRef(sentinel);
191 size_t dirty_count{0};
198 coins_cache_entry.
coin = random_coin;
205 coins_cache_entry.
coin = *opt_coin;
210 auto it{coins_map.emplace(random_out_point, std::move(coins_cache_entry)).first};
213 dirty_count += dirty;
219 if (is_db && best_block.IsNull()) best_block =
uint256::ONE;
220 coins_view_cache.
BatchWrite(cursor, best_block);
225 bool expected_code_path =
false;
227 (void)coins_view_cache.Cursor();
228 }
catch (
const std::logic_error&) {
229 expected_code_path =
true;
231 assert(expected_code_path);
232 (void)coins_view_cache.DynamicMemoryUsage();
233 (void)coins_view_cache.EstimateSize();
234 (void)coins_view_cache.GetBestBlock();
235 (void)coins_view_cache.GetCacheSize();
236 (void)coins_view_cache.GetHeadBlocks();
237 (void)coins_view_cache.HaveInputs(
CTransaction{random_mutable_transaction});
241 if (is_db && backend_coins_view == original_backend) {
242 assert(backend_coins_view->Cursor());
244 (void)backend_coins_view->EstimateSize();
245 (void)backend_coins_view->GetBestBlock();
246 (void)backend_coins_view->GetHeadBlocks();
253 const CTransaction transaction{random_mutable_transaction};
254 bool is_spent =
false;
255 for (
const CTxOut& tx_out : transaction.vout) {
256 if (Coin{tx_out, 0, transaction.IsCoinBase()}.IsSpent()) {
266 const bool check_for_overwrite{transaction.IsCoinBase() || [&] {
267 for (uint32_t i{0}; i < transaction.vout.size(); ++i) {
268 if (coins_view_cache.PeekCoin(
COutPoint{transaction.GetHash(), i}))
return true;
272 AddCoins(coins_view_cache, transaction, height, check_for_overwrite);
280 const CTransaction transaction{random_mutable_transaction};
296 const CTransaction transaction{random_mutable_transaction};
305 const CTransaction transaction{random_mutable_transaction};
312 if (!transaction.vin.empty() && (
flags & SCRIPT_VERIFY_WITNESS) != 0 && (
flags & SCRIPT_VERIFY_P2SH) == 0) {
325 const Coin& coin_using_access_coin = coins_view_cache.AccessCoin(random_out_point);
326 const bool exists_using_access_coin = !(coin_using_access_coin == EMPTY_COIN);
327 const bool exists_using_have_coin = coins_view_cache.HaveCoin(random_out_point);
328 const bool exists_using_have_coin_in_cache = coins_view_cache.HaveCoinInCache(random_out_point);
329 if (
auto coin{coins_view_cache.GetCoin(random_out_point)}) {
330 assert(*coin == coin_using_access_coin);
331 assert(exists_using_access_coin && exists_using_have_coin_in_cache && exists_using_have_coin);
333 assert(!exists_using_access_coin && !exists_using_have_coin_in_cache && !exists_using_have_coin);
336 const bool exists_using_have_coin_in_backend = backend_coins_view->HaveCoin(random_out_point);
337 if (!coin_using_access_coin.
IsSpent() && exists_using_have_coin_in_backend) {
338 assert(exists_using_have_coin);
340 if (
auto coin{backend_coins_view->GetCoin(random_out_point)}) {
341 assert(exists_using_have_coin_in_backend);
345 assert(!exists_using_have_coin_in_backend);
362 .cache_bytes = 1_MiB,
bool MoneyRange(const CAmount &nValue)
int64_t CAmount
Amount in satoshis (Can be negative)
void SetBackend(CCoinsView &in_view)
CCoinsView that adds a memory cache for transactions to another CCoinsView.
void Sync()
Push the modifications applied to this cache to its base while retaining the contents of this cache (...
bool SpendCoin(const COutPoint &outpoint, Coin *moveto=nullptr)
Spend a coin.
ResetGuard CreateResetGuard() noexcept
Create a scoped guard that will call Reset() on this cache when it goes out of scope.
void Uncache(const COutPoint &outpoint)
Removes the UTXO with the given outpoint from the cache, if it is not modified.
void AddCoin(const COutPoint &outpoint, Coin &&coin, bool possible_overwrite)
Add a coin.
CCoinsViewCache(CCoinsView *in_base, bool deterministic=false)
void Flush(bool reallocate_cache=true)
Push the modifications applied to this cache to its base and wipe local state.
void SetBestBlock(const uint256 &block_hash)
uint256 GetBestBlock() const override
Retrieve the block hash whose state this CCoinsView currently represents.
void BatchWrite(CoinsViewCacheCursor &cursor, const uint256 &block_hash) override
Do a bulk modification (multiple Coin changes + BestBlock change).
std::optional< Coin > PeekCoin(const COutPoint &outpoint) const override
Retrieve the Coin (unspent transaction output) for a given outpoint, without caching results.
void EmplaceCoinInternalDANGER(COutPoint &&outpoint, Coin &&coin)
Emplace a coin into cacheCoins without performing any checks, marking the emplaced coin as dirty.
CCoinsView backed by the coin database (chainstate/)
Pure abstract view on the open txout dataset.
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.
An output of a transaction.
CTxOut out
unspent transaction output
bool IsSpent() const
Either this coin never existed (see e.g.
bool fCoinBase
whether containing transaction was a coinbase
uint32_t nHeight
at which height this containing transaction was included in the active block chain
static CoinsViewEmpty & Get()
CCoinsViewCache overlay that avoids populating/mutating parent cache layers on cache misses.
T ConsumeIntegralInRange(T min, T max)
static constexpr script_verify_flags from_int(value_type f)
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::pair< const COutPoint, CCoinsCacheEntry > CoinsCachePair
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...
CCoinsMap::allocator_type::ResourceType CCoinsMapMemoryResource
void TestCoinsView(FuzzedDataProvider &fuzzed_data_provider, CCoinsViewCache &coins_view_cache, CCoinsView *backend_coins_view)
FUZZ_TARGET(coins_view,.init=initialize_coins_view)
void initialize_coins_view()
#define LIMITED_WHILE(condition, limit)
Can be used to limit a theoretically unbounded loop.
bool CheckTxInputs(const CTransaction &tx, TxValidationState &state, const CCoinsViewCache &inputs, int nSpendHeight, CAmount &txfee)
Check whether all inputs of this transaction are valid (no double spends and amounts) This does not m...
bool operator==(const CNetAddr &a, const CNetAddr &b)
TxValidationState ValidateInputsStandardness(const CTransaction &tx, const CCoinsViewCache &mapInputs)
Check transaction inputs.
bool IsWitnessStandard(const CTransaction &tx, const CCoinsViewCache &mapInputs)
Check if the transaction is over standard P2WSH resources limit: 3600bytes witnessScript size,...
static constexpr TransactionSerParams TX_WITH_WITNESS
A Coin in one level of the coins database caching hierarchy.
static void SetFresh(CoinsCachePair &pair, CoinsCachePair &sentinel) noexcept
static void SetDirty(CoinsCachePair &pair, CoinsCachePair &sentinel) noexcept
A mutable version of CTransaction.
Cursor for iterating over the linked list of flagged entries in CCoinsViewCache.
User-controlled performance and debug options.
Application-specific storage settings.
fs::path path
Location in the filesystem where leveldb data will be stored.
bool ContainsSpentInput(const CTransaction &tx, const CCoinsViewCache &inputs) noexcept
uint256 ConsumeUInt256(FuzzedDataProvider &fuzzed_data_provider) noexcept
size_t CallOneOf(FuzzedDataProvider &fuzzed_data_provider, Callables... callables)
bool CheckTransaction(const CTransaction &tx, TxValidationState &state)
int64_t GetTransactionSigOpCost(const CTransaction &tx, const CCoinsViewCache &inputs, script_verify_flags flags)
Compute total signature operation cost of a transaction.
unsigned int GetP2SHSigOpCount(const CTransaction &tx, const CCoinsViewCache &inputs)
Count ECDSA signature operations in pay-to-script-hash inputs.
FuzzedDataProvider & fuzzed_data_provider