31 const Coin EMPTY_COIN{};
42 static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();
43 g_setup = testing_setup.get();
56 LIMITED_WHILE(good_data && fuzzed_data_provider.ConsumeBool(), 10
'000) 61 if (random_coin.IsSpent()) { 64 Coin coin = random_coin; 65 bool expected_code_path = false; 66 const bool possible_overwrite = fuzzed_data_provider.ConsumeBool(); 68 coins_view_cache.AddCoin(random_out_point, std::move(coin), possible_overwrite); 69 expected_code_path = true; 70 } catch (const std::logic_error& e) { 71 if (e.what() == std::string{"Attempted to overwrite an unspent coin (when possible_overwrite is false)"}) { 72 assert(!possible_overwrite); 73 expected_code_path = true; 76 assert(expected_code_path); 79 (void)coins_view_cache.Flush(); 82 (void)coins_view_cache.Sync(); 85 coins_view_cache.SetBestBlock(ConsumeUInt256(fuzzed_data_provider)); 89 (void)coins_view_cache.SpendCoin(random_out_point, fuzzed_data_provider.ConsumeBool() ? &move_to : nullptr); 92 coins_view_cache.Uncache(random_out_point); 95 if (fuzzed_data_provider.ConsumeBool()) { 96 backend_coins_view = CCoinsView{}; 98 coins_view_cache.SetBackend(backend_coins_view); 101 const std::optional<COutPoint> opt_out_point = ConsumeDeserializable<COutPoint>(fuzzed_data_provider); 102 if (!opt_out_point) { 106 random_out_point = *opt_out_point; 109 const std::optional<Coin> opt_coin = ConsumeDeserializable<Coin>(fuzzed_data_provider); 114 random_coin = *opt_coin; 117 const std::optional<CMutableTransaction> opt_mutable_transaction = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider, TX_WITH_WITNESS); 118 if (!opt_mutable_transaction) { 122 random_mutable_transaction = *opt_mutable_transaction; 125 CCoinsMapMemoryResource resource; 126 CCoinsMap coins_map{0, SaltedOutpointHasher{/*deterministic=*/true}, CCoinsMap::key_equal{}, &resource}; 127 LIMITED_WHILE(good_data && fuzzed_data_provider.ConsumeBool(), 10'000)
130 coins_cache_entry.
flags = fuzzed_data_provider.ConsumeIntegral<
unsigned char>();
131 if (fuzzed_data_provider.ConsumeBool()) {
132 coins_cache_entry.
coin = random_coin;
134 const std::optional<Coin> opt_coin = ConsumeDeserializable<Coin>(fuzzed_data_provider);
139 coins_cache_entry.
coin = *opt_coin;
141 coins_map.emplace(random_out_point, std::move(coins_cache_entry));
143 bool expected_code_path =
false;
145 coins_view_cache.BatchWrite(coins_map, fuzzed_data_provider.ConsumeBool() ?
ConsumeUInt256(fuzzed_data_provider) : coins_view_cache.GetBestBlock());
146 expected_code_path =
true;
147 }
catch (
const std::logic_error& e) {
148 if (e.what() == std::string{
"FRESH flag misapplied to coin that exists in parent cache"}) {
149 expected_code_path =
true;
152 assert(expected_code_path);
157 const Coin& coin_using_access_coin = coins_view_cache.AccessCoin(random_out_point);
158 const bool exists_using_access_coin = !(coin_using_access_coin == EMPTY_COIN);
159 const bool exists_using_have_coin = coins_view_cache.HaveCoin(random_out_point);
160 const bool exists_using_have_coin_in_cache = coins_view_cache.HaveCoinInCache(random_out_point);
161 Coin coin_using_get_coin;
162 const bool exists_using_get_coin = coins_view_cache.GetCoin(random_out_point, coin_using_get_coin);
163 if (exists_using_get_coin) {
164 assert(coin_using_get_coin == coin_using_access_coin);
166 assert((exists_using_access_coin && exists_using_have_coin_in_cache && exists_using_have_coin && exists_using_get_coin) ||
167 (!exists_using_access_coin && !exists_using_have_coin_in_cache && !exists_using_have_coin && !exists_using_get_coin));
169 const bool exists_using_have_coin_in_backend = backend_coins_view.HaveCoin(random_out_point);
170 if (!coin_using_access_coin.
IsSpent() && exists_using_have_coin_in_backend) {
171 assert(exists_using_have_coin);
173 Coin coin_using_backend_get_coin;
174 if (backend_coins_view.GetCoin(random_out_point, coin_using_backend_get_coin)) {
175 assert(exists_using_have_coin_in_backend);
179 assert(!exists_using_have_coin_in_backend);
184 bool expected_code_path =
false;
186 (void)coins_view_cache.Cursor();
187 }
catch (
const std::logic_error&) {
188 expected_code_path =
true;
190 assert(expected_code_path);
191 (void)coins_view_cache.DynamicMemoryUsage();
192 (void)coins_view_cache.EstimateSize();
193 (void)coins_view_cache.GetBestBlock();
194 (void)coins_view_cache.GetCacheSize();
195 (void)coins_view_cache.GetHeadBlocks();
196 (void)coins_view_cache.HaveInputs(
CTransaction{random_mutable_transaction});
200 std::unique_ptr<CCoinsViewCursor> coins_view_cursor = backend_coins_view.Cursor();
201 assert(!coins_view_cursor);
202 (void)backend_coins_view.EstimateSize();
203 (void)backend_coins_view.GetBestBlock();
204 (void)backend_coins_view.GetHeadBlocks();
207 if (fuzzed_data_provider.ConsumeBool()) {
209 fuzzed_data_provider,
211 const CTransaction transaction{random_mutable_transaction};
212 bool is_spent =
false;
213 for (
const CTxOut& tx_out : transaction.vout) {
214 if (Coin{tx_out, 0, transaction.IsCoinBase()}.IsSpent()) {
223 bool expected_code_path =
false;
224 const int height{int(fuzzed_data_provider.ConsumeIntegral<uint32_t>() >> 1)};
225 const bool possible_overwrite = fuzzed_data_provider.ConsumeBool();
227 AddCoins(coins_view_cache, transaction, height, possible_overwrite);
228 expected_code_path =
true;
229 }
catch (
const std::logic_error& e) {
230 if (e.what() == std::string{
"Attempted to overwrite an unspent coin (when possible_overwrite is false)"}) {
231 assert(!possible_overwrite);
232 expected_code_path =
true;
235 assert(expected_code_path);
243 const CTransaction transaction{random_mutable_transaction};
254 if (
Consensus::CheckTxInputs(transaction, state, coins_view_cache, fuzzed_data_provider.ConsumeIntegralInRange<
int>(0, std::numeric_limits<int>::max()), tx_fee_out)) {
259 const CTransaction transaction{random_mutable_transaction};
268 const CTransaction transaction{random_mutable_transaction};
274 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.
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.
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.
uint256 ConsumeUInt256(FuzzedDataProvider &fuzzed_data_provider) noexcept
bool CheckTransaction(const CTransaction &tx, TxValidationState &state)
Testing setup that configures a complete environment.