Bitcoin Core 30.99.0
P2P Digital Currency
coins_view.cpp
Go to the documentation of this file.
1// Copyright (c) 2020-present The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#include <coins.h>
6#include <consensus/amount.h>
10#include <policy/policy.h>
12#include <script/interpreter.h>
14#include <test/fuzz/fuzz.h>
15#include <test/fuzz/util.h>
17#include <txdb.h>
18#include <util/hasher.h>
19
20#include <cassert>
21#include <algorithm>
22#include <cstdint>
23#include <functional>
24#include <limits>
25#include <memory>
26#include <optional>
27#include <ranges>
28#include <stdexcept>
29#include <string>
30#include <utility>
31#include <vector>
32
33namespace {
34const Coin EMPTY_COIN{};
35
36bool operator==(const Coin& a, const Coin& b)
37{
38 if (a.IsSpent() && b.IsSpent()) return true;
39 return a.fCoinBase == b.fCoinBase && a.nHeight == b.nHeight && a.out == b.out;
40}
41
49class MutationGuardCoinsViewCache final : public CCoinsViewCache
50{
51private:
52 struct CacheCoinSnapshot {
53 COutPoint outpoint;
54 bool dirty{false};
55 bool fresh{false};
56 Coin coin;
57 bool operator==(const CacheCoinSnapshot&) const = default;
58 };
59
60 std::vector<CacheCoinSnapshot> ComputeCacheCoinsSnapshot() const
61 {
62 std::vector<CacheCoinSnapshot> snapshot;
63 snapshot.reserve(cacheCoins.size());
64
65 for (const auto& [outpoint, entry] : cacheCoins) {
66 snapshot.emplace_back(outpoint, entry.IsDirty(), entry.IsFresh(), entry.coin);
67 }
68
69 std::ranges::sort(snapshot, std::less<>{}, &CacheCoinSnapshot::outpoint);
70 return snapshot;
71 }
72
73 mutable std::vector<CacheCoinSnapshot> m_expected_snapshot{ComputeCacheCoinsSnapshot()};
74
75public:
76 void BatchWrite(CoinsViewCacheCursor& cursor, const uint256& block_hash) override
77 {
78 // Nothing must modify cacheCoins other than BatchWrite.
79 assert(ComputeCacheCoinsSnapshot() == m_expected_snapshot);
80 try {
81 CCoinsViewCache::BatchWrite(cursor, block_hash);
82 } catch (const std::logic_error& e) {
83 // This error is thrown if the cursor contains a fresh entry for an outpoint that we already have a fresh
84 // entry for. This can happen if the fuzzer calls AddCoin -> Flush -> AddCoin -> Flush on the child cache.
85 // There's not an easy way to prevent the fuzzer from reaching this, so we handle it here.
86 // Since it is thrown in the middle of the write, we reset our own state and iterate through
87 // the cursor so the caller's state is also reset.
88 assert(e.what() == std::string{"FRESH flag misapplied to coin that exists in parent cache"});
89 Reset();
90 for (auto it{cursor.Begin()}; it != cursor.End(); it = cursor.NextAndMaybeErase(*it)) {}
91 }
92 m_expected_snapshot = ComputeCacheCoinsSnapshot();
93 }
94
96};
97} // namespace
98
100{
101 static const auto testing_setup = MakeNoLogFileContext<>();
102}
103
104void TestCoinsView(FuzzedDataProvider& fuzzed_data_provider, CCoinsViewCache& coins_view_cache, CCoinsView& backend_coins_view, bool is_db)
105{
106 bool good_data{true};
107
108 if (is_db) coins_view_cache.SetBestBlock(uint256::ONE);
109 COutPoint random_out_point;
110 Coin random_coin;
111 CMutableTransaction random_mutable_transaction;
112 LIMITED_WHILE(good_data && fuzzed_data_provider.ConsumeBool(), 10'000)
113 {
114 CallOneOf(
116 [&] {
117 if (random_coin.IsSpent()) {
118 return;
119 }
120 COutPoint outpoint{random_out_point};
121 Coin coin{random_coin};
123 const bool possible_overwrite{fuzzed_data_provider.ConsumeBool()};
124 try {
125 coins_view_cache.AddCoin(outpoint, std::move(coin), possible_overwrite);
126 } catch (const std::logic_error& e) {
127 assert(e.what() == std::string{"Attempted to overwrite an unspent coin (when possible_overwrite is false)"});
128 assert(!possible_overwrite);
129 }
130 } else {
131 coins_view_cache.EmplaceCoinInternalDANGER(std::move(outpoint), std::move(coin));
132 }
133 },
134 [&] {
135 coins_view_cache.Flush(/*reallocate_cache=*/fuzzed_data_provider.ConsumeBool());
136 },
137 [&] {
138 coins_view_cache.Sync();
139 },
140 [&] {
142 // Set best block hash to non-null to satisfy the assertion in CCoinsViewDB::BatchWrite().
143 if (is_db && best_block.IsNull()) best_block = uint256::ONE;
144 coins_view_cache.SetBestBlock(best_block);
145 },
146 [&] {
147 {
148 const auto reset_guard{coins_view_cache.CreateResetGuard()};
149 }
150 // Set best block hash to non-null to satisfy the assertion in CCoinsViewDB::BatchWrite().
151 if (is_db) {
153 if (best_block.IsNull()) {
154 good_data = false;
155 return;
156 }
157 coins_view_cache.SetBestBlock(best_block);
158 }
159 },
160 [&] {
161 Coin move_to;
162 (void)coins_view_cache.SpendCoin(random_out_point, fuzzed_data_provider.ConsumeBool() ? &move_to : nullptr);
163 },
164 [&] {
165 coins_view_cache.Uncache(random_out_point);
166 },
167 [&] {
169 backend_coins_view = CCoinsView{};
170 }
171 coins_view_cache.SetBackend(backend_coins_view);
172 },
173 [&] {
174 const std::optional<COutPoint> opt_out_point = ConsumeDeserializable<COutPoint>(fuzzed_data_provider);
175 if (!opt_out_point) {
176 good_data = false;
177 return;
178 }
179 random_out_point = *opt_out_point;
180 },
181 [&] {
182 const std::optional<Coin> opt_coin = ConsumeDeserializable<Coin>(fuzzed_data_provider);
183 if (!opt_coin) {
184 good_data = false;
185 return;
186 }
187 random_coin = *opt_coin;
188 },
189 [&] {
190 const std::optional<CMutableTransaction> opt_mutable_transaction = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider, TX_WITH_WITNESS);
191 if (!opt_mutable_transaction) {
192 good_data = false;
193 return;
194 }
195 random_mutable_transaction = *opt_mutable_transaction;
196 },
197 [&] {
198 CoinsCachePair sentinel{};
199 sentinel.second.SelfRef(sentinel);
200 size_t dirty_count{0};
202 CCoinsMap coins_map{0, SaltedOutpointHasher{/*deterministic=*/true}, CCoinsMap::key_equal{}, &resource};
203 LIMITED_WHILE(good_data && fuzzed_data_provider.ConsumeBool(), 10'000)
204 {
205 CCoinsCacheEntry coins_cache_entry;
206 const auto dirty{fuzzed_data_provider.ConsumeBool()};
207 const auto fresh{fuzzed_data_provider.ConsumeBool()};
209 coins_cache_entry.coin = random_coin;
210 } else {
211 const std::optional<Coin> opt_coin = ConsumeDeserializable<Coin>(fuzzed_data_provider);
212 if (!opt_coin) {
213 good_data = false;
214 return;
215 }
216 coins_cache_entry.coin = *opt_coin;
217 }
218 auto it{coins_map.emplace(random_out_point, std::move(coins_cache_entry)).first};
219 if (dirty) CCoinsCacheEntry::SetDirty(*it, sentinel);
220 if (fresh) CCoinsCacheEntry::SetFresh(*it, sentinel);
221 dirty_count += dirty;
222 }
223 bool expected_code_path = false;
224 try {
225 auto cursor{CoinsViewCacheCursor(dirty_count, sentinel, coins_map, /*will_erase=*/true)};
226 uint256 best_block{coins_view_cache.GetBestBlock()};
228 // Set best block hash to non-null to satisfy the assertion in CCoinsViewDB::BatchWrite().
229 if (is_db && best_block.IsNull()) best_block = uint256::ONE;
230 coins_view_cache.BatchWrite(cursor, best_block);
231 expected_code_path = true;
232 } catch (const std::logic_error& e) {
233 if (e.what() == std::string{"FRESH flag misapplied to coin that exists in parent cache"}) {
234 expected_code_path = true;
235 }
236 }
237 assert(expected_code_path);
238 });
239 }
240
241 {
242 bool expected_code_path = false;
243 try {
244 (void)coins_view_cache.Cursor();
245 } catch (const std::logic_error&) {
246 expected_code_path = true;
247 }
248 assert(expected_code_path);
249 (void)coins_view_cache.DynamicMemoryUsage();
250 (void)coins_view_cache.EstimateSize();
251 (void)coins_view_cache.GetBestBlock();
252 (void)coins_view_cache.GetCacheSize();
253 (void)coins_view_cache.GetHeadBlocks();
254 (void)coins_view_cache.HaveInputs(CTransaction{random_mutable_transaction});
255 }
256
257 {
258 if (is_db) {
259 std::unique_ptr<CCoinsViewCursor> coins_view_cursor = backend_coins_view.Cursor();
260 assert(!!coins_view_cursor);
261 }
262 (void)backend_coins_view.EstimateSize();
263 (void)backend_coins_view.GetBestBlock();
264 (void)backend_coins_view.GetHeadBlocks();
265 }
266
268 CallOneOf(
270 [&] {
271 const CTransaction transaction{random_mutable_transaction};
272 bool is_spent = false;
273 for (const CTxOut& tx_out : transaction.vout) {
274 if (Coin{tx_out, 0, transaction.IsCoinBase()}.IsSpent()) {
275 is_spent = true;
276 }
277 }
278 if (is_spent) {
279 // Avoid:
280 // coins.cpp:69: void CCoinsViewCache::AddCoin(const COutPoint &, Coin &&, bool): Assertion `!coin.IsSpent()' failed.
281 return;
282 }
283 bool expected_code_path = false;
284 const int height{int(fuzzed_data_provider.ConsumeIntegral<uint32_t>() >> 1)};
285 const bool possible_overwrite = fuzzed_data_provider.ConsumeBool();
286 try {
287 AddCoins(coins_view_cache, transaction, height, possible_overwrite);
288 expected_code_path = true;
289 } catch (const std::logic_error& e) {
290 if (e.what() == std::string{"Attempted to overwrite an unspent coin (when possible_overwrite is false)"}) {
291 assert(!possible_overwrite);
292 expected_code_path = true;
293 }
294 }
295 assert(expected_code_path);
296 },
297 [&] {
298 (void)AreInputsStandard(CTransaction{random_mutable_transaction}, coins_view_cache);
299 },
300 [&] {
301 TxValidationState state;
302 CAmount tx_fee_out;
303 const CTransaction transaction{random_mutable_transaction};
304 if (ContainsSpentInput(transaction, coins_view_cache)) {
305 // Avoid:
306 // consensus/tx_verify.cpp:171: bool Consensus::CheckTxInputs(const CTransaction &, TxValidationState &, const CCoinsViewCache &, int, CAmount &): Assertion `!coin.IsSpent()' failed.
307 return;
308 }
309 TxValidationState dummy;
310 if (!CheckTransaction(transaction, dummy)) {
311 // It is not allowed to call CheckTxInputs if CheckTransaction failed
312 return;
313 }
314 if (Consensus::CheckTxInputs(transaction, state, coins_view_cache, fuzzed_data_provider.ConsumeIntegralInRange<int>(0, std::numeric_limits<int>::max()), tx_fee_out)) {
315 assert(MoneyRange(tx_fee_out));
316 }
317 },
318 [&] {
319 const CTransaction transaction{random_mutable_transaction};
320 if (ContainsSpentInput(transaction, coins_view_cache)) {
321 // Avoid:
322 // consensus/tx_verify.cpp:130: unsigned int GetP2SHSigOpCount(const CTransaction &, const CCoinsViewCache &): Assertion `!coin.IsSpent()' failed.
323 return;
324 }
325 (void)GetP2SHSigOpCount(transaction, coins_view_cache);
326 },
327 [&] {
328 const CTransaction transaction{random_mutable_transaction};
329 if (ContainsSpentInput(transaction, coins_view_cache)) {
330 // Avoid:
331 // consensus/tx_verify.cpp:130: unsigned int GetP2SHSigOpCount(const CTransaction &, const CCoinsViewCache &): Assertion `!coin.IsSpent()' failed.
332 return;
333 }
335 if (!transaction.vin.empty() && (flags & SCRIPT_VERIFY_WITNESS) != 0 && (flags & SCRIPT_VERIFY_P2SH) == 0) {
336 // Avoid:
337 // script/interpreter.cpp:1705: size_t CountWitnessSigOps(const CScript &, const CScript &, const CScriptWitness &, unsigned int): Assertion `(flags & SCRIPT_VERIFY_P2SH) != 0' failed.
338 return;
339 }
340 (void)GetTransactionSigOpCost(transaction, coins_view_cache, flags);
341 },
342 [&] {
343 (void)IsWitnessStandard(CTransaction{random_mutable_transaction}, coins_view_cache);
344 });
345 }
346
347 {
348 const Coin& coin_using_access_coin = coins_view_cache.AccessCoin(random_out_point);
349 const bool exists_using_access_coin = !(coin_using_access_coin == EMPTY_COIN);
350 const bool exists_using_have_coin = coins_view_cache.HaveCoin(random_out_point);
351 const bool exists_using_have_coin_in_cache = coins_view_cache.HaveCoinInCache(random_out_point);
352 if (auto coin{coins_view_cache.GetCoin(random_out_point)}) {
353 assert(*coin == coin_using_access_coin);
354 assert(exists_using_access_coin && exists_using_have_coin_in_cache && exists_using_have_coin);
355 } else {
356 assert(!exists_using_access_coin && !exists_using_have_coin_in_cache && !exists_using_have_coin);
357 }
358 // If HaveCoin on the backend is true, it must also be on the cache if the coin wasn't spent.
359 const bool exists_using_have_coin_in_backend = backend_coins_view.HaveCoin(random_out_point);
360 if (!coin_using_access_coin.IsSpent() && exists_using_have_coin_in_backend) {
361 assert(exists_using_have_coin);
362 }
363 if (auto coin{backend_coins_view.GetCoin(random_out_point)}) {
364 assert(exists_using_have_coin_in_backend);
365 // Note we can't assert that `coin_using_get_coin == *coin` because the coin in
366 // the cache may have been modified but not yet flushed.
367 } else {
368 assert(!exists_using_have_coin_in_backend);
369 }
370 }
371}
372
374{
375 FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
376 CCoinsView backend_coins_view;
377 CCoinsViewCache coins_view_cache{&backend_coins_view, /*deterministic=*/true};
378 TestCoinsView(fuzzed_data_provider, coins_view_cache, backend_coins_view, /*is_db=*/false);
379}
380
382{
383 FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
384 auto db_params = DBParams{
385 .path = "",
386 .cache_bytes = 1_MiB,
387 .memory_only = true,
388 };
389 CCoinsViewDB backend_coins_view{std::move(db_params), CoinsViewOptions{}};
390 CCoinsViewCache coins_view_cache{&backend_coins_view, /*deterministic=*/true};
391 TestCoinsView(fuzzed_data_provider, coins_view_cache, backend_coins_view, /*is_db=*/true);
392}
393
394// Creates a CoinsViewOverlay and a MutationGuardCoinsViewCache as the base.
395// This allows us to exercise all methods on a CoinsViewOverlay, while also
396// ensuring that nothing can mutate the underlying cache until Flush or Sync is
397// called.
399{
400 FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
401 CCoinsView backend_base_coins_view;
402 MutationGuardCoinsViewCache backend_cache{&backend_base_coins_view, /*deterministic=*/true};
403 CoinsViewOverlay coins_view_cache{&backend_cache, /*deterministic=*/true};
404 TestCoinsView(fuzzed_data_provider, coins_view_cache, backend_cache, /*is_db=*/false);
405}
bool MoneyRange(const CAmount &nValue)
Definition: amount.h:27
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
if(!SetupNetworking())
catch(const std::exception &e)
int flags
Definition: bitcoin-tx.cpp:529
void SetBackend(CCoinsView &viewIn)
Definition: coins.cpp:39
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:367
void Sync()
Push the modifications applied to this cache to its base while retaining the contents of this cache (...
Definition: coins.cpp:291
CCoinsViewCache(CCoinsView *baseIn, bool deterministic=false)
Definition: coins.cpp:52
bool SpendCoin(const COutPoint &outpoint, Coin *moveto=nullptr)
Spend a coin.
Definition: coins.cpp:153
ResetGuard CreateResetGuard() noexcept
Create a scoped guard that will call Reset() on this cache when it goes out of scope.
Definition: coins.h:518
void Uncache(const COutPoint &outpoint)
Removes the UTXO with the given outpoint from the cache, if it is not modified.
Definition: coins.cpp:310
void Flush(bool reallocate_cache=true)
Push the modifications applied to this cache to its base and wipe local state.
Definition: coins.cpp:279
void Reset() noexcept
Discard all modifications made to this cache without flushing to the base view.
Definition: coins.cpp:302
uint256 GetBestBlock() const override
Retrieve the block hash whose state this CCoinsView currently represents.
Definition: coins.cpp:198
void SetBestBlock(const uint256 &hashBlock)
Definition: coins.cpp:204
void BatchWrite(CoinsViewCacheCursor &cursor, const uint256 &hashBlock) override
Do a bulk modification (multiple Coin changes + BestBlock change).
Definition: coins.cpp:208
void EmplaceCoinInternalDANGER(COutPoint &&outpoint, Coin &&coin)
Emplace a coin into cacheCoins without performing any checks, marking the emplaced coin as dirty.
Definition: coins.cpp:132
CCoinsView backed by the coin database (chainstate/)
Definition: txdb.h:35
Abstract view on the open txout dataset.
Definition: coins.h:307
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:29
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:281
An output of a transaction.
Definition: transaction.h:140
A UTXO entry.
Definition: coins.h:34
CTxOut out
unspent transaction output
Definition: coins.h:37
bool IsSpent() const
Either this coin never existed (see e.g.
Definition: coins.h:82
uint32_t nHeight
at which height this containing transaction was included in the active block chain
Definition: coins.h:43
unsigned int fCoinBase
whether containing transaction was a coinbase
Definition: coins.h:40
CCoinsViewCache overlay that avoids populating/mutating parent cache layers on cache misses.
Definition: coins.h:538
T ConsumeIntegralInRange(T min, T max)
static constexpr script_verify_flags from_int(value_type f)
Definition: verify_flags.h:35
256-bit opaque blob.
Definition: uint256.h:195
static const uint256 ONE
Definition: uint256.h:204
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.
Definition: coins.cpp:142
std::pair< const COutPoint, CCoinsCacheEntry > CoinsCachePair
Definition: coins.h:92
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...
Definition: coins.h:223
CCoinsMap::allocator_type::ResourceType CCoinsMapMemoryResource
Definition: coins.h:225
FUZZ_TARGET(coins_view,.init=initialize_coins_view)
Definition: coins_view.cpp:373
void initialize_coins_view()
Definition: coins_view.cpp:99
void TestCoinsView(FuzzedDataProvider &fuzzed_data_provider, CCoinsViewCache &coins_view_cache, CCoinsView &backend_coins_view, bool is_db)
Definition: coins_view.cpp:104
#define LIMITED_WHILE(condition, limit)
Can be used to limit a theoretically unbounded loop.
Definition: fuzz.h:22
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...
Definition: tx_verify.cpp:164
bool operator==(const CNetAddr &a, const CNetAddr &b)
Definition: netaddress.cpp:603
bool AreInputsStandard(const CTransaction &tx, const CCoinsViewCache &mapInputs)
Check transaction inputs.
Definition: policy.cpp:213
bool IsWitnessStandard(const CTransaction &tx, const CCoinsViewCache &mapInputs)
Check if the transaction is over standard P2WSH resources limit: 3600bytes witnessScript size,...
Definition: policy.cpp:251
static constexpr TransactionSerParams TX_WITH_WITNESS
Definition: transaction.h:180
A Coin in one level of the coins database caching hierarchy.
Definition: coins.h:109
Coin coin
Definition: coins.h:141
static void SetFresh(CoinsCachePair &pair, CoinsCachePair &sentinel) noexcept
Definition: coins.h:172
static void SetDirty(CoinsCachePair &pair, CoinsCachePair &sentinel) noexcept
Definition: coins.h:171
A mutable version of CTransaction.
Definition: transaction.h:358
Cursor for iterating over the linked list of flagged entries in CCoinsViewCache.
Definition: coins.h:260
CoinsCachePair * NextAndMaybeErase(CoinsCachePair &current) noexcept
Return the next entry after current, possibly erasing current.
Definition: coins.h:278
CoinsCachePair * Begin() const noexcept
Definition: coins.h:274
CoinsCachePair * End() const noexcept
Definition: coins.h:275
User-controlled performance and debug options.
Definition: txdb.h:26
Application-specific storage settings.
Definition: dbwrapper.h:33
fs::path path
Location in the filesystem where leveldb data will be stored.
Definition: dbwrapper.h:35
bool ContainsSpentInput(const CTransaction &tx, const CCoinsViewCache &inputs) noexcept
Definition: util.cpp:245
uint256 ConsumeUInt256(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: util.h:167
size_t CallOneOf(FuzzedDataProvider &fuzzed_data_provider, Callables... callables)
Definition: util.h:35
bool CheckTransaction(const CTransaction &tx, TxValidationState &state)
Definition: tx_check.cpp:11
int64_t GetTransactionSigOpCost(const CTransaction &tx, const CCoinsViewCache &inputs, script_verify_flags flags)
Compute total signature operation cost of a transaction.
Definition: tx_verify.cpp:143
unsigned int GetP2SHSigOpCount(const CTransaction &tx, const CCoinsViewCache &inputs)
Count ECDSA signature operations in pay-to-script-hash inputs.
Definition: tx_verify.cpp:126
assert(!tx.IsCoinBase())
FuzzedDataProvider & fuzzed_data_provider
Definition: fees.cpp:38