30 const Coin EMPTY_COIN{};
41 static const auto testing_setup = MakeNoLogFileContext<>();
54 LIMITED_WHILE(good_data && fuzzed_data_provider.ConsumeBool(), 10
'000) 59 if (random_coin.IsSpent()) { 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; 74 assert(expected_code_path); 77 (void)coins_view_cache.Flush(); 80 (void)coins_view_cache.Sync(); 83 coins_view_cache.SetBestBlock(ConsumeUInt256(fuzzed_data_provider)); 87 (void)coins_view_cache.SpendCoin(random_out_point, fuzzed_data_provider.ConsumeBool() ? &move_to : nullptr); 90 coins_view_cache.Uncache(random_out_point); 93 if (fuzzed_data_provider.ConsumeBool()) { 94 backend_coins_view = CCoinsView{}; 96 coins_view_cache.SetBackend(backend_coins_view); 99 const std::optional<COutPoint> opt_out_point = ConsumeDeserializable<COutPoint>(fuzzed_data_provider); 100 if (!opt_out_point) { 104 random_out_point = *opt_out_point; 107 const std::optional<Coin> opt_coin = ConsumeDeserializable<Coin>(fuzzed_data_provider); 112 random_coin = *opt_coin; 115 const std::optional<CMutableTransaction> opt_mutable_transaction = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider, TX_WITH_WITNESS); 116 if (!opt_mutable_transaction) { 120 random_mutable_transaction = *opt_mutable_transaction; 123 CoinsCachePair sentinel{}; 124 sentinel.second.SelfRef(sentinel); 126 CCoinsMapMemoryResource resource; 127 CCoinsMap coins_map{0, SaltedOutpointHasher{/*deterministic=*/true}, CCoinsMap::key_equal{}, &resource}; 128 LIMITED_WHILE(good_data && fuzzed_data_provider.ConsumeBool(), 10'000)
131 const auto dirty{fuzzed_data_provider.ConsumeBool()};
132 const auto fresh{fuzzed_data_provider.ConsumeBool()};
133 if (fuzzed_data_provider.ConsumeBool()) {
134 coins_cache_entry.
coin = random_coin;
136 const std::optional<Coin> opt_coin = ConsumeDeserializable<Coin>(fuzzed_data_provider);
141 coins_cache_entry.
coin = *opt_coin;
143 auto it{coins_map.emplace(random_out_point, std::move(coins_cache_entry)).first};
146 usage += it->second.coin.DynamicMemoryUsage();
148 bool expected_code_path =
false;
151 coins_view_cache.BatchWrite(cursor, fuzzed_data_provider.ConsumeBool() ?
ConsumeUInt256(fuzzed_data_provider) : coins_view_cache.GetBestBlock());
152 expected_code_path =
true;
153 }
catch (
const std::logic_error& e) {
154 if (e.what() == std::string{
"FRESH flag misapplied to coin that exists in parent cache"}) {
155 expected_code_path =
true;
158 assert(expected_code_path);
163 const Coin& coin_using_access_coin = coins_view_cache.AccessCoin(random_out_point);
164 const bool exists_using_access_coin = !(coin_using_access_coin == EMPTY_COIN);
165 const bool exists_using_have_coin = coins_view_cache.HaveCoin(random_out_point);
166 const bool exists_using_have_coin_in_cache = coins_view_cache.HaveCoinInCache(random_out_point);
167 if (
auto coin{coins_view_cache.GetCoin(random_out_point)}) {
168 assert(*coin == coin_using_access_coin);
169 assert(exists_using_access_coin && exists_using_have_coin_in_cache && exists_using_have_coin);
171 assert(!exists_using_access_coin && !exists_using_have_coin_in_cache && !exists_using_have_coin);
174 const bool exists_using_have_coin_in_backend = backend_coins_view.HaveCoin(random_out_point);
175 if (!coin_using_access_coin.
IsSpent() && exists_using_have_coin_in_backend) {
176 assert(exists_using_have_coin);
178 if (
auto coin{backend_coins_view.GetCoin(random_out_point)}) {
179 assert(exists_using_have_coin_in_backend);
183 assert(!exists_using_have_coin_in_backend);
188 bool expected_code_path =
false;
190 (void)coins_view_cache.Cursor();
191 }
catch (
const std::logic_error&) {
192 expected_code_path =
true;
194 assert(expected_code_path);
195 (void)coins_view_cache.DynamicMemoryUsage();
196 (void)coins_view_cache.EstimateSize();
197 (void)coins_view_cache.GetBestBlock();
198 (void)coins_view_cache.GetCacheSize();
199 (void)coins_view_cache.GetHeadBlocks();
200 (void)coins_view_cache.HaveInputs(
CTransaction{random_mutable_transaction});
204 std::unique_ptr<CCoinsViewCursor> coins_view_cursor = backend_coins_view.Cursor();
205 assert(!coins_view_cursor);
206 (void)backend_coins_view.EstimateSize();
207 (void)backend_coins_view.GetBestBlock();
208 (void)backend_coins_view.GetHeadBlocks();
211 if (fuzzed_data_provider.ConsumeBool()) {
213 fuzzed_data_provider,
215 const CTransaction transaction{random_mutable_transaction};
216 bool is_spent =
false;
217 for (
const CTxOut& tx_out : transaction.vout) {
218 if (Coin{tx_out, 0, transaction.IsCoinBase()}.IsSpent()) {
227 bool expected_code_path =
false;
228 const int height{int(fuzzed_data_provider.ConsumeIntegral<uint32_t>() >> 1)};
229 const bool possible_overwrite = fuzzed_data_provider.ConsumeBool();
231 AddCoins(coins_view_cache, transaction, height, possible_overwrite);
232 expected_code_path =
true;
233 }
catch (
const std::logic_error& e) {
234 if (e.what() == std::string{
"Attempted to overwrite an unspent coin (when possible_overwrite is false)"}) {
235 assert(!possible_overwrite);
236 expected_code_path =
true;
239 assert(expected_code_path);
247 const CTransaction transaction{random_mutable_transaction};
258 if (
Consensus::CheckTxInputs(transaction, state, coins_view_cache, fuzzed_data_provider.ConsumeIntegralInRange<
int>(0, std::numeric_limits<int>::max()), tx_fee_out)) {
263 const CTransaction transaction{random_mutable_transaction};
272 const CTransaction transaction{random_mutable_transaction};
278 const auto flags{fuzzed_data_provider.ConsumeIntegral<uint32_t>()};
FUZZ_TARGET(coins_view,.init=initialize_coins_view)
bool IsSpent() const
Either this coin never existed (see e.g.
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...
A Coin in one level of the coins database caching hierarchy.
bool operator==(const CNetAddr &a, const CNetAddr &b)
bool MoneyRange(const CAmount &nValue)
CTxOut out
unspent transaction output
unsigned int fCoinBase
whether containing transaction was a coinbase
#define LIMITED_WHILE(condition, limit)
Can be used to limit a theoretically unbounded loop.
void initialize_coins_view()
bool IsWitnessStandard(const CTransaction &tx, const CCoinsViewCache &mapInputs)
Check if the transaction is over standard P2WSH resources limit: 3600bytes witnessScript size...
int64_t CAmount
Amount in satoshis (Can be negative)
bool AreInputsStandard(const CTransaction &tx, const CCoinsViewCache &mapInputs)
Check transaction inputs to mitigate two potential denial-of-service attacks:
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.
uint32_t nHeight
at which height this containing transaction was included in the active block chain ...
int64_t GetTransactionSigOpCost(const CTransaction &tx, const CCoinsViewCache &inputs, uint32_t flags)
Compute total signature operation cost of a transaction.
Abstract view on the open txout dataset.
Cursor for iterating over the linked list of flagged entries in CCoinsViewCache.
bool ContainsSpentInput(const CTransaction &tx, const CCoinsViewCache &inputs) noexcept
An output of a transaction.
An outpoint - a combination of a transaction hash and an index n into its vout.
static void SetDirty(CoinsCachePair &pair, CoinsCachePair &sentinel) noexcept
unsigned int GetP2SHSigOpCount(const CTransaction &tx, const CCoinsViewCache &inputs)
Count ECDSA signature operations in pay-to-script-hash inputs.
A mutable version of CTransaction.
size_t CallOneOf(FuzzedDataProvider &fuzzed_data_provider, Callables... callables)
The basic transaction that is broadcasted on the network and contained in blocks. ...
CCoinsView that adds a memory cache for transactions to another CCoinsView.
static void SetFresh(CoinsCachePair &pair, CoinsCachePair &sentinel) noexcept
uint256 ConsumeUInt256(FuzzedDataProvider &fuzzed_data_provider) noexcept
bool CheckTransaction(const CTransaction &tx, TxValidationState &state)