31const Coin EMPTY_COIN{};
42 static const auto testing_setup = MakeNoLogFileContext<>();
62 Coin coin = random_coin;
63 bool expected_code_path =
false;
64 const bool possible_overwrite = fuzzed_data_provider.
ConsumeBool();
66 coins_view_cache.AddCoin(random_out_point, std::move(coin), possible_overwrite);
67 expected_code_path = true;
68 }
catch (
const std::logic_error& e) {
69 if (e.what() == std::string{
"Attempted to overwrite an unspent coin (when possible_overwrite is false)"}) {
70 assert(!possible_overwrite);
71 expected_code_path = true;
77 (void)coins_view_cache.Flush();
80 assert(expected_code_path);
83 (void)coins_view_cache.Flush();
86 (void)coins_view_cache.Sync();
91 if (is_db && best_block.IsNull()) best_block =
uint256::ONE;
92 coins_view_cache.SetBestBlock(best_block);
96 (void)coins_view_cache.SpendCoin(random_out_point, fuzzed_data_provider.
ConsumeBool() ? &move_to :
nullptr);
99 coins_view_cache.Uncache(random_out_point);
105 coins_view_cache.SetBackend(backend_coins_view);
108 const std::optional<COutPoint> opt_out_point = ConsumeDeserializable<COutPoint>(fuzzed_data_provider);
109 if (!opt_out_point) {
113 random_out_point = *opt_out_point;
116 const std::optional<Coin> opt_coin = ConsumeDeserializable<Coin>(fuzzed_data_provider);
121 random_coin = *opt_coin;
124 const std::optional<CMutableTransaction> opt_mutable_transaction = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider,
TX_WITH_WITNESS);
125 if (!opt_mutable_transaction) {
129 random_mutable_transaction = *opt_mutable_transaction;
133 sentinel.second.SelfRef(sentinel);
140 const auto dirty{fuzzed_data_provider.
ConsumeBool()};
141 const auto fresh{fuzzed_data_provider.
ConsumeBool()};
143 coins_cache_entry.
coin = random_coin;
145 const std::optional<Coin> opt_coin = ConsumeDeserializable<Coin>(fuzzed_data_provider);
150 coins_cache_entry.
coin = *opt_coin;
152 auto it{coins_map.emplace(random_out_point, std::move(coins_cache_entry)).first};
155 usage += it->second.coin.DynamicMemoryUsage();
157 bool expected_code_path =
false;
160 uint256 best_block{coins_view_cache.GetBestBlock()};
163 if (is_db && best_block.IsNull()) best_block =
uint256::ONE;
164 coins_view_cache.BatchWrite(cursor, best_block);
165 expected_code_path =
true;
166 }
catch (
const std::logic_error& e) {
167 if (e.what() == std::string{
"FRESH flag misapplied to coin that exists in parent cache"}) {
168 expected_code_path =
true;
171 assert(expected_code_path);
176 const Coin& coin_using_access_coin = coins_view_cache.AccessCoin(random_out_point);
177 const bool exists_using_access_coin = !(coin_using_access_coin == EMPTY_COIN);
178 const bool exists_using_have_coin = coins_view_cache.HaveCoin(random_out_point);
179 const bool exists_using_have_coin_in_cache = coins_view_cache.HaveCoinInCache(random_out_point);
180 if (
auto coin{coins_view_cache.GetCoin(random_out_point)}) {
181 assert(*coin == coin_using_access_coin);
182 assert(exists_using_access_coin && exists_using_have_coin_in_cache && exists_using_have_coin);
184 assert(!exists_using_access_coin && !exists_using_have_coin_in_cache && !exists_using_have_coin);
187 const bool exists_using_have_coin_in_backend = backend_coins_view.
HaveCoin(random_out_point);
188 if (!coin_using_access_coin.
IsSpent() && exists_using_have_coin_in_backend) {
189 assert(exists_using_have_coin);
191 if (
auto coin{backend_coins_view.
GetCoin(random_out_point)}) {
192 assert(exists_using_have_coin_in_backend);
196 assert(!exists_using_have_coin_in_backend);
201 bool expected_code_path =
false;
203 (void)coins_view_cache.Cursor();
204 }
catch (
const std::logic_error&) {
205 expected_code_path =
true;
207 assert(expected_code_path);
208 (void)coins_view_cache.DynamicMemoryUsage();
209 (void)coins_view_cache.EstimateSize();
210 (void)coins_view_cache.GetBestBlock();
211 (void)coins_view_cache.GetCacheSize();
212 (void)coins_view_cache.GetHeadBlocks();
213 (void)coins_view_cache.HaveInputs(
CTransaction{random_mutable_transaction});
217 std::unique_ptr<CCoinsViewCursor> coins_view_cursor = backend_coins_view.Cursor();
218 assert(is_db == !!coins_view_cursor);
219 (void)backend_coins_view.EstimateSize();
220 (void)backend_coins_view.GetBestBlock();
221 (void)backend_coins_view.GetHeadBlocks();
224 if (fuzzed_data_provider.ConsumeBool()) {
226 fuzzed_data_provider,
228 const CTransaction transaction{random_mutable_transaction};
229 bool is_spent =
false;
230 for (
const CTxOut& tx_out : transaction.vout) {
231 if (Coin{tx_out, 0, transaction.IsCoinBase()}.IsSpent()) {
240 bool expected_code_path =
false;
241 const int height{int(fuzzed_data_provider.ConsumeIntegral<uint32_t>() >> 1)};
242 const bool possible_overwrite = fuzzed_data_provider.ConsumeBool();
244 AddCoins(coins_view_cache, transaction, height, possible_overwrite);
245 expected_code_path =
true;
246 }
catch (
const std::logic_error& e) {
247 if (e.what() == std::string{
"Attempted to overwrite an unspent coin (when possible_overwrite is false)"}) {
248 assert(!possible_overwrite);
249 expected_code_path =
true;
252 assert(expected_code_path);
260 const CTransaction transaction{random_mutable_transaction};
271 if (
Consensus::CheckTxInputs(transaction, state, coins_view_cache, fuzzed_data_provider.ConsumeIntegralInRange<
int>(0, std::numeric_limits<int>::max()), tx_fee_out)) {
276 const CTransaction transaction{random_mutable_transaction};
285 const CTransaction transaction{random_mutable_transaction};
291 const auto flags{fuzzed_data_provider.ConsumeIntegral<uint32_t>()};
309 TestCoinsView(fuzzed_data_provider, backend_coins_view,
false);
317 .cache_bytes = 1_MiB,
bool MoneyRange(const CAmount &nValue)
int64_t CAmount
Amount in satoshis (Can be negative)
catch(const std::exception &e)
CCoinsView that adds a memory cache for transactions to another CCoinsView.
CCoinsView backed by the coin database (chainstate/)
Abstract view on the open txout dataset.
virtual std::optional< Coin > GetCoin(const COutPoint &outpoint) const
Retrieve the Coin (unspent transaction output) for a given outpoint.
virtual bool HaveCoin(const COutPoint &outpoint) const
Just check whether a given outpoint is unspent.
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.
uint32_t nHeight
at which height this containing transaction was included in the active block chain
unsigned int fCoinBase
whether containing transaction was a coinbase
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, CCoinsView &backend_coins_view, bool is_db)
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)
bool AreInputsStandard(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, uint32_t 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.