Bitcoin Core 31.99.0
P2P Digital Currency
coins_tests.cpp
Go to the documentation of this file.
1// Copyright (c) 2014-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 <addresstype.h>
6#include <clientversion.h>
7#include <coins.h>
8#include <streams.h>
9#include <test/util/coins.h>
10#include <test/util/common.h>
12#include <test/util/random.h>
14#include <txdb.h>
15#include <uint256.h>
16#include <undo.h>
17#include <util/byte_units.h>
18#include <util/check.h>
19#include <util/strencodings.h>
20
21#include <map>
22#include <string>
23#include <variant>
24#include <vector>
25
26#include <boost/test/unit_test.hpp>
27
28using namespace util::hex_literals;
29
30int ApplyTxInUndo(Coin&& undo, CCoinsViewCache& view, const COutPoint& out);
31void UpdateCoins(const CTransaction& tx, CCoinsViewCache& inputs, CTxUndo &txundo, int nHeight);
32
33namespace
34{
35
36class CCoinsViewTest : public CoinsViewEmpty
37{
38 FastRandomContext& m_rng;
39 uint256 hashBestBlock_;
40 std::map<COutPoint, Coin> map_;
41
42public:
43 explicit CCoinsViewTest(FastRandomContext& rng) : m_rng{rng} {}
44
45 std::optional<Coin> GetCoin(const COutPoint& outpoint) const override
46 {
47 if (auto it{map_.find(outpoint)}; it != map_.end() && !it->second.IsSpent()) return it->second;
48 return std::nullopt;
49 }
50
51 uint256 GetBestBlock() const override { return hashBestBlock_; }
52
53 void BatchWrite(CoinsViewCacheCursor& cursor, const uint256& block_hash) override
54 {
55 for (auto it{cursor.Begin()}; it != cursor.End(); it = cursor.NextAndMaybeErase(*it)){
56 if (it->second.IsDirty()) {
57 // Same optimization used in CCoinsViewDB is to only write dirty entries.
58 map_[it->first] = it->second.coin;
59 if (it->second.coin.IsSpent() && m_rng.randrange(3) == 0) {
60 // Randomly delete empty entries on write.
61 map_.erase(it->first);
62 }
63 }
64 }
65 if (!block_hash.IsNull())
66 hashBestBlock_ = block_hash;
67 }
68};
69
70class CCoinsViewCacheTest : public CCoinsViewCache
71{
72public:
73 explicit CCoinsViewCacheTest(CCoinsView* _base) : CCoinsViewCache(_base) {}
74
75 void SelfTest(bool sanity_check = true) const
76 {
77 // Manually recompute the dynamic usage of the whole data, and compare it.
78 size_t ret = memusage::DynamicUsage(cacheCoins);
79 size_t count = 0;
80 for (const auto& entry : cacheCoins) {
81 ret += entry.second.coin.DynamicMemoryUsage();
82 ++count;
83 }
86 if (sanity_check) {
88 }
89 }
90
91 CCoinsMap& map() const { return cacheCoins; }
92 CoinsCachePair& sentinel() const { return m_sentinel; }
93 size_t& usage() const { return cachedCoinsUsage; }
94 size_t& dirty() const { return m_dirty_count; }
95};
96
97} // namespace
98
99static const unsigned int NUM_SIMULATION_ITERATIONS = 40000;
100
102// This is a large randomized insert/remove simulation test on a variable-size
103// stack of caches on top of CCoinsViewTest.
104//
105// It will randomly create/update/delete Coin entries to a tip of caches, with
106// txids picked from a limited list of random 256-bit hashes. Occasionally, a
107// new tip is added to the stack of caches, or the tip is flushed and removed.
108//
109// During the process, booleans are kept to make sure that the randomized
110// operation hits all branches.
111//
112// If fake_best_block is true, assign a random uint256 to mock the recording
113// of best block on flush. This is necessary when using CCoinsViewDB as the base,
114// otherwise we'll hit an assertion in BatchWrite.
115//
116void SimulationTest(CCoinsView* base, bool fake_best_block)
117{
118 // Various coverage trackers.
119 bool removed_all_caches = false;
120 bool reached_4_caches = false;
121 bool added_an_entry = false;
122 bool added_an_unspendable_entry = false;
123 bool removed_an_entry = false;
124 bool updated_an_entry = false;
125 bool found_an_entry = false;
126 bool missed_an_entry = false;
127 bool uncached_an_entry = false;
128 bool flushed_without_erase = false;
129
130 // A simple map to track what we expect the cache stack to represent.
131 std::map<COutPoint, Coin> result;
132
133 // The cache stack.
134 std::vector<std::unique_ptr<CCoinsViewCacheTest>> stack; // A stack of CCoinsViewCaches on top.
135 stack.push_back(std::make_unique<CCoinsViewCacheTest>(base)); // Start with one cache.
136
137 // Use a limited set of random transaction ids, so we do test overwriting entries.
138 std::vector<Txid> txids;
139 txids.resize(NUM_SIMULATION_ITERATIONS / 8);
140 for (unsigned int i = 0; i < txids.size(); i++) {
141 txids[i] = Txid::FromUint256(m_rng.rand256());
142 }
143
144 for (unsigned int i = 0; i < NUM_SIMULATION_ITERATIONS; i++) {
145 // Do a random modification.
146 {
147 auto txid = txids[m_rng.randrange(txids.size())]; // txid we're going to modify in this iteration.
148 Coin& coin = result[COutPoint(txid, 0)];
149
150 // Determine whether to test HaveCoin before or after Access* (or both). As these functions
151 // can influence each other's behaviour by pulling things into the cache, all combinations
152 // are tested.
153 bool test_havecoin_before = m_rng.randbits(2) == 0;
154 bool test_havecoin_after = m_rng.randbits(2) == 0;
155
156 bool result_havecoin = test_havecoin_before ? stack.back()->HaveCoin(COutPoint(txid, 0)) : false;
157
158 // Infrequently, test usage of AccessByTxid instead of AccessCoin - the
159 // former just delegates to the latter and returns the first unspent in a txn.
160 const Coin& entry = (m_rng.randrange(500) == 0) ?
161 AccessByTxid(*stack.back(), txid) : stack.back()->AccessCoin(COutPoint(txid, 0));
162 BOOST_CHECK_EQUAL(coin, entry);
163
164 if (test_havecoin_before) {
165 BOOST_CHECK(result_havecoin == !entry.IsSpent());
166 }
167
168 if (test_havecoin_after) {
169 bool ret = stack.back()->HaveCoin(COutPoint(txid, 0));
170 BOOST_CHECK(ret == !entry.IsSpent());
171 }
172
173 if (m_rng.randrange(5) == 0 || coin.IsSpent()) {
174 Coin newcoin;
175 newcoin.out.nValue = RandMoney(m_rng);
176 newcoin.nHeight = 1;
177
178 // Infrequently test adding unspendable coins.
179 if (m_rng.randrange(16) == 0 && coin.IsSpent()) {
182 added_an_unspendable_entry = true;
183 } else {
184 // Random sizes so we can test memory usage accounting
185 newcoin.out.scriptPubKey.assign(m_rng.randbits(6), 0);
186 (coin.IsSpent() ? added_an_entry : updated_an_entry) = true;
187 coin = newcoin;
188 }
189 if (COutPoint op(txid, 0); !stack.back()->map().contains(op) && !newcoin.out.scriptPubKey.IsUnspendable() && m_rng.randbool()) {
190 stack.back()->EmplaceCoinInternalDANGER(op, std::move(newcoin));
191 } else {
192 stack.back()->AddCoin(op, std::move(newcoin), /*possible_overwrite=*/!coin.IsSpent() || m_rng.randbool());
193 }
194 } else {
195 // Spend the coin.
196 removed_an_entry = true;
197 coin.Clear();
198 BOOST_CHECK(stack.back()->SpendCoin(COutPoint(txid, 0)));
199 }
200 }
201
202 // Once every 10 iterations, remove a random entry from the cache
203 if (m_rng.randrange(10) == 0) {
204 COutPoint out(txids[m_rng.rand32() % txids.size()], 0);
205 int cacheid = m_rng.rand32() % stack.size();
206 stack[cacheid]->Uncache(out);
207 uncached_an_entry |= !stack[cacheid]->HaveCoinInCache(out);
208 }
209
210 // Once every 1000 iterations and at the end, verify the full cache.
211 if (m_rng.randrange(1000) == 1 || i == NUM_SIMULATION_ITERATIONS - 1) {
212 for (const auto& entry : result) {
213 bool have = stack.back()->HaveCoin(entry.first);
214 const Coin& coin = stack.back()->AccessCoin(entry.first);
215 BOOST_CHECK(have == !coin.IsSpent());
216 BOOST_CHECK_EQUAL(coin, entry.second);
217 if (coin.IsSpent()) {
218 missed_an_entry = true;
219 } else {
220 BOOST_CHECK(stack.back()->HaveCoinInCache(entry.first));
221 found_an_entry = true;
222 }
223 }
224 for (const auto& test : stack) {
225 test->SelfTest();
226 }
227 }
228
229 if (m_rng.randrange(100) == 0) {
230 // Every 100 iterations, flush an intermediate cache
231 if (stack.size() > 1 && m_rng.randbool() == 0) {
232 unsigned int flushIndex = m_rng.randrange(stack.size() - 1);
233 if (fake_best_block) stack[flushIndex]->SetBestBlock(m_rng.rand256());
234 bool should_erase = m_rng.randrange(4) < 3;
235 should_erase ? stack[flushIndex]->Flush() : stack[flushIndex]->Sync();
236 flushed_without_erase |= !should_erase;
237 }
238 }
239 if (m_rng.randrange(100) == 0) {
240 // Every 100 iterations, change the cache stack.
241 if (stack.size() > 0 && m_rng.randbool() == 0) {
242 //Remove the top cache
243 if (fake_best_block) stack.back()->SetBestBlock(m_rng.rand256());
244 bool should_erase = m_rng.randrange(4) < 3;
245 should_erase ? stack.back()->Flush() : stack.back()->Sync();
246 flushed_without_erase |= !should_erase;
247 stack.pop_back();
248 }
249 if (stack.size() == 0 || (stack.size() < 4 && m_rng.randbool())) {
250 //Add a new cache
251 CCoinsView* tip = base;
252 if (stack.size() > 0) {
253 tip = stack.back().get();
254 } else {
255 removed_all_caches = true;
256 }
257 stack.push_back(std::make_unique<CCoinsViewCacheTest>(tip));
258 if (stack.size() == 4) {
259 reached_4_caches = true;
260 }
261 }
262 }
263 }
264
265 // Verify coverage.
266 BOOST_CHECK(removed_all_caches);
267 BOOST_CHECK(reached_4_caches);
268 BOOST_CHECK(added_an_entry);
269 BOOST_CHECK(added_an_unspendable_entry);
270 BOOST_CHECK(removed_an_entry);
271 BOOST_CHECK(updated_an_entry);
272 BOOST_CHECK(found_an_entry);
273 BOOST_CHECK(missed_an_entry);
274 BOOST_CHECK(uncached_an_entry);
275 BOOST_CHECK(flushed_without_erase);
276}
277}; // struct CacheTest
278
280
281// Run the above simulation for multiple base types.
282BOOST_FIXTURE_TEST_CASE(coins_cache_base_simulation_test, CacheTest)
283{
284 CCoinsViewTest base{m_rng};
285 SimulationTest(&base, false);
286}
287
289
291
292BOOST_FIXTURE_TEST_CASE(coins_cache_dbbase_simulation_test, CacheTest)
293{
294 CCoinsViewDB db_base{{.path = "test", .cache_bytes = 8_MiB, .memory_only = true}, {}};
295 SimulationTest(&db_base, true);
296}
297
299
301
303// Store of all necessary tx and undo data for next test
304typedef std::map<COutPoint, std::tuple<CTransaction,CTxUndo,Coin>> UtxoData;
306
307UtxoData::iterator FindRandomFrom(const std::set<COutPoint> &utxoSet) {
308 assert(utxoSet.size());
309 auto utxoSetIt = utxoSet.lower_bound(COutPoint(Txid::FromUint256(m_rng.rand256()), 0));
310 if (utxoSetIt == utxoSet.end()) {
311 utxoSetIt = utxoSet.begin();
312 }
313 auto utxoDataIt = utxoData.find(*utxoSetIt);
314 assert(utxoDataIt != utxoData.end());
315 return utxoDataIt;
316}
317}; // struct UpdateTest
318
319
320// This test is similar to the previous test
321// except the emphasis is on testing the functionality of UpdateCoins
322// random txs are created and UpdateCoins is used to update the cache stack
323// In particular it is tested that spending a duplicate coinbase tx
324// has the expected effect (the other duplicate is overwritten at all cache levels)
325BOOST_FIXTURE_TEST_CASE(updatecoins_simulation_test, UpdateTest)
326{
327 SeedRandomForTest(SeedRand::ZEROS);
328
329 bool spent_a_duplicate_coinbase = false;
330 // A simple map to track what we expect the cache stack to represent.
331 std::map<COutPoint, Coin> result;
332
333 // The cache stack.
334 CCoinsViewTest base{m_rng}; // A CCoinsViewTest at the bottom.
335 std::vector<std::unique_ptr<CCoinsViewCacheTest>> stack; // A stack of CCoinsViewCaches on top.
336 stack.push_back(std::make_unique<CCoinsViewCacheTest>(&base)); // Start with one cache.
337
338 // Track the txids we've used in various sets
339 std::set<COutPoint> coinbase_coins;
340 std::set<COutPoint> disconnected_coins;
341 std::set<COutPoint> duplicate_coins;
342 std::set<COutPoint> utxoset;
343
344 for (unsigned int i = 0; i < NUM_SIMULATION_ITERATIONS; i++) {
345 uint32_t randiter = m_rng.rand32();
346
347 // 19/20 txs add a new transaction
348 if (randiter % 20 < 19) {
350 tx.vin.resize(1);
351 tx.vout.resize(1);
352 tx.vout[0].nValue = i; //Keep txs unique unless intended to duplicate
353 tx.vout[0].scriptPubKey.assign(m_rng.rand32() & 0x3F, 0); // Random sizes so we can test memory usage accounting
354 const int height{int(m_rng.rand32() >> 1)};
355 Coin old_coin;
356
357 // 2/20 times create a new coinbase
358 if (randiter % 20 < 2 || coinbase_coins.size() < 10) {
359 // 1/10 of those times create a duplicate coinbase
360 if (m_rng.randrange(10) == 0 && coinbase_coins.size()) {
361 auto utxod = FindRandomFrom(coinbase_coins);
362 // Reuse the exact same coinbase
363 tx = CMutableTransaction{std::get<0>(utxod->second)};
364 // shouldn't be available for reconnection if it's been duplicated
365 disconnected_coins.erase(utxod->first);
366
367 duplicate_coins.insert(utxod->first);
368 }
369 else {
370 coinbase_coins.insert(COutPoint(tx.GetHash(), 0));
371 }
372 assert(CTransaction(tx).IsCoinBase());
373 }
374
375 // 17/20 times reconnect previous or add a regular tx
376 else {
377
378 COutPoint prevout;
379 // 1/20 times reconnect a previously disconnected tx
380 if (randiter % 20 == 2 && disconnected_coins.size()) {
381 auto utxod = FindRandomFrom(disconnected_coins);
382 tx = CMutableTransaction{std::get<0>(utxod->second)};
383 prevout = tx.vin[0].prevout;
384 if (!CTransaction(tx).IsCoinBase() && !utxoset.contains(prevout)) {
385 disconnected_coins.erase(utxod->first);
386 continue;
387 }
388
389 // If this tx is already IN the UTXO, then it must be a coinbase, and it must be a duplicate
390 if (utxoset.contains(utxod->first)) {
391 assert(CTransaction(tx).IsCoinBase());
392 assert(duplicate_coins.contains(utxod->first));
393 }
394 disconnected_coins.erase(utxod->first);
395 }
396
397 // 16/20 times create a regular tx
398 else {
399 auto utxod = FindRandomFrom(utxoset);
400 prevout = utxod->first;
401
402 // Construct the tx to spend the coins of prevouthash
403 tx.vin[0].prevout = prevout;
404 assert(!CTransaction(tx).IsCoinBase());
405 }
406 // In this simple test coins only have two states, spent or unspent, save the unspent state to restore
407 old_coin = result[prevout];
408 // Update the expected result of prevouthash to know these coins are spent
409 result[prevout].Clear();
410
411 utxoset.erase(prevout);
412
413 // The test is designed to ensure spending a duplicate coinbase will work properly
414 // if that ever happens and not resurrect the previously overwritten coinbase
415 if (duplicate_coins.contains(prevout)) {
416 spent_a_duplicate_coinbase = true;
417 }
418
419 }
420 // Update the expected result to know about the new output coins
421 assert(tx.vout.size() == 1);
422 const COutPoint outpoint(tx.GetHash(), 0);
423 result[outpoint] = Coin{tx.vout[0], height, CTransaction{tx}.IsCoinBase()};
424
425 // Call UpdateCoins on the top cache
426 CTxUndo undo;
427 UpdateCoins(CTransaction{tx}, *(stack.back()), undo, height);
428
429 // Update the utxo set for future spends
430 utxoset.insert(outpoint);
431
432 // Track this tx and undo info to use later
433 utxoData.emplace(outpoint, std::make_tuple(tx,undo,old_coin));
434 } else if (utxoset.size()) {
435 //1/20 times undo a previous transaction
436 auto utxod = FindRandomFrom(utxoset);
437
438 CTransaction &tx = std::get<0>(utxod->second);
439 CTxUndo &undo = std::get<1>(utxod->second);
440 Coin &orig_coin = std::get<2>(utxod->second);
441
442 // Update the expected result
443 // Remove new outputs
444 result[utxod->first].Clear();
445 // If not coinbase restore prevout
446 if (!tx.IsCoinBase()) {
447 result[tx.vin[0].prevout] = orig_coin;
448 }
449
450 // Disconnect the tx from the current UTXO
451 // See code in DisconnectBlock
452 // remove outputs
453 BOOST_CHECK(stack.back()->SpendCoin(utxod->first));
454 // restore inputs
455 if (!tx.IsCoinBase()) {
456 const COutPoint &out = tx.vin[0].prevout;
457 Coin coin = undo.vprevout[0];
458 ApplyTxInUndo(std::move(coin), *(stack.back()), out);
459 }
460 // Store as a candidate for reconnection
461 disconnected_coins.insert(utxod->first);
462
463 // Update the utxoset
464 utxoset.erase(utxod->first);
465 if (!tx.IsCoinBase())
466 utxoset.insert(tx.vin[0].prevout);
467 }
468
469 // Once every 1000 iterations and at the end, verify the full cache.
470 if (m_rng.randrange(1000) == 1 || i == NUM_SIMULATION_ITERATIONS - 1) {
471 for (const auto& entry : result) {
472 bool have = stack.back()->HaveCoin(entry.first);
473 const Coin& coin = stack.back()->AccessCoin(entry.first);
474 BOOST_CHECK(have == !coin.IsSpent());
475 BOOST_CHECK_EQUAL(coin, entry.second);
476 }
477 }
478
479 // One every 10 iterations, remove a random entry from the cache
480 if (utxoset.size() > 1 && m_rng.randrange(30) == 0) {
481 stack[m_rng.rand32() % stack.size()]->Uncache(FindRandomFrom(utxoset)->first);
482 }
483 if (disconnected_coins.size() > 1 && m_rng.randrange(30) == 0) {
484 stack[m_rng.rand32() % stack.size()]->Uncache(FindRandomFrom(disconnected_coins)->first);
485 }
486 if (duplicate_coins.size() > 1 && m_rng.randrange(30) == 0) {
487 stack[m_rng.rand32() % stack.size()]->Uncache(FindRandomFrom(duplicate_coins)->first);
488 }
489
490 if (m_rng.randrange(100) == 0) {
491 // Every 100 iterations, flush an intermediate cache
492 if (stack.size() > 1 && m_rng.randbool() == 0) {
493 unsigned int flushIndex = m_rng.randrange(stack.size() - 1);
494 stack[flushIndex]->Flush();
495 }
496 }
497 if (m_rng.randrange(100) == 0) {
498 // Every 100 iterations, change the cache stack.
499 if (stack.size() > 0 && m_rng.randbool() == 0) {
500 stack.back()->Flush();
501 stack.pop_back();
502 }
503 if (stack.size() == 0 || (stack.size() < 4 && m_rng.randbool())) {
504 CCoinsView* tip = &base;
505 if (stack.size() > 0) {
506 tip = stack.back().get();
507 }
508 stack.push_back(std::make_unique<CCoinsViewCacheTest>(tip));
509 }
510 }
511 }
512
513 // Verify coverage.
514 BOOST_CHECK(spent_a_duplicate_coinbase);
515}
516
517BOOST_AUTO_TEST_CASE(ccoins_serialization)
518{
519 // Good example
520 Coin cc1;
521 SpanReader{"97f23c835800816115944e077fe7c803cfa57f29b36bf87c1d35"_hex} >> cc1;
522 BOOST_CHECK_EQUAL(cc1.IsCoinBase(), false);
523 BOOST_CHECK_EQUAL(cc1.nHeight, 203998U);
524 BOOST_CHECK_EQUAL(cc1.out.nValue, CAmount{60000000000});
525 BOOST_CHECK_EQUAL(HexStr(cc1.out.scriptPubKey), HexStr(GetScriptForDestination(PKHash(uint160("816115944e077fe7c803cfa57f29b36bf87c1d35"_hex_u8)))));
526
527 // Good example
528 Coin cc2;
529 SpanReader{"8ddf77bbd123008c988f1a4a4de2161e0f50aac7f17e7f9555caa4"_hex} >> cc2;
530 BOOST_CHECK_EQUAL(cc2.IsCoinBase(), true);
531 BOOST_CHECK_EQUAL(cc2.nHeight, 120891U);
532 BOOST_CHECK_EQUAL(cc2.out.nValue, 110397);
533 BOOST_CHECK_EQUAL(HexStr(cc2.out.scriptPubKey), HexStr(GetScriptForDestination(PKHash(uint160("8c988f1a4a4de2161e0f50aac7f17e7f9555caa4"_hex_u8)))));
534
535 // Smallest possible example
536 Coin cc3;
537 SpanReader{"000006"_hex} >> cc3;
538 BOOST_CHECK_EQUAL(cc3.IsCoinBase(), false);
539 BOOST_CHECK_EQUAL(cc3.nHeight, 0U);
542
543 // scriptPubKey that ends beyond the end of the stream
544 BOOST_CHECK_EXCEPTION(SpanReader{"000007"_hex} >> Coin{}, std::ios_base::failure, HasReason{"end of data"});
545
546 // Very large scriptPubKey (3*10^9 bytes) past the end of the stream
547 DataStream tmp{};
548 uint64_t x = 3000000000ULL;
549 tmp << VARINT(x);
550 BOOST_CHECK_EQUAL(HexStr(tmp), "8a95c0bb00");
551 BOOST_CHECK_EXCEPTION(SpanReader{"00008a95c0bb00"_hex} >> Coin{}, std::ios_base::failure, HasReason{"end of data"});
552}
553
554const static COutPoint OUTPOINT;
555constexpr CAmount SPENT {-1};
556constexpr CAmount ABSENT{-2};
557constexpr CAmount VALUE1{100};
558constexpr CAmount VALUE2{200};
559constexpr CAmount VALUE3{300};
560
561struct CoinEntry {
562 enum class State { CLEAN, DIRTY, FRESH, DIRTY_FRESH };
563
566
567 constexpr CoinEntry(const CAmount v, const State s) : value{v}, state{s} {}
568
569 bool operator==(const CoinEntry& o) const = default;
570 friend std::ostream& operator<<(std::ostream& os, const CoinEntry& e) { return os << e.value << ", " << e.state; }
571
572 constexpr bool IsDirtyFresh() const { return state == State::DIRTY_FRESH; }
573 constexpr bool IsDirty() const { return state == State::DIRTY || IsDirtyFresh(); }
574 constexpr bool IsFresh() const { return state == State::FRESH || IsDirtyFresh(); }
575
576 static constexpr State ToState(const bool is_dirty, const bool is_fresh) {
577 if (is_dirty && is_fresh) return State::DIRTY_FRESH;
578 if (is_dirty) return State::DIRTY;
579 if (is_fresh) return State::FRESH;
580 return State::CLEAN;
581 }
582};
583
584using MaybeCoin = std::optional<CoinEntry>;
585using CoinOrError = std::variant<MaybeCoin, std::string>;
586
587constexpr MaybeCoin MISSING {std::nullopt};
602
603constexpr auto EX_OVERWRITE_UNSPENT{"Attempted to overwrite an unspent coin (when possible_overwrite is false)"};
604constexpr auto EX_FRESH_MISAPPLIED {"FRESH flag misapplied to coin that exists in parent cache"};
605
606static void SetCoinsValue(const CAmount value, Coin& coin)
607{
608 assert(value != ABSENT);
609 coin.Clear();
610 assert(coin.IsSpent());
611 if (value != SPENT) {
612 coin.out.nValue = value;
613 coin.nHeight = 1;
614 assert(!coin.IsSpent());
615 }
616}
617
618static size_t InsertCoinsMapEntry(CCoinsMap& map, CoinsCachePair& sentinel, const CoinEntry& cache_coin)
619{
620 CCoinsCacheEntry entry;
621 SetCoinsValue(cache_coin.value, entry.coin);
622 auto [iter, inserted] = map.emplace(OUTPOINT, std::move(entry));
623 assert(inserted);
624 if (cache_coin.IsDirty()) CCoinsCacheEntry::SetDirty(*iter, sentinel);
625 if (cache_coin.IsFresh()) CCoinsCacheEntry::SetFresh(*iter, sentinel);
626 return iter->second.coin.DynamicMemoryUsage();
627}
628
629static MaybeCoin GetCoinsMapEntry(const CCoinsMap& map, const COutPoint& outp = OUTPOINT)
630{
631 if (auto it{map.find(outp)}; it != map.end()) {
632 return CoinEntry{
633 it->second.coin.IsSpent() ? SPENT : it->second.coin.out.nValue,
634 CoinEntry::ToState(it->second.IsDirty(), it->second.IsFresh())};
635 }
636 return MISSING;
637}
638
639static void WriteCoinsViewEntry(CCoinsView& view, const MaybeCoin& cache_coin)
640{
641 CoinsCachePair sentinel{};
642 sentinel.second.SelfRef(sentinel);
644 CCoinsMap map{0, CCoinsMap::hasher{}, CCoinsMap::key_equal{}, &resource};
645 if (cache_coin) InsertCoinsMapEntry(map, sentinel, *cache_coin);
646 size_t dirty_count{cache_coin && cache_coin->IsDirty()};
647 auto cursor{CoinsViewCacheCursor(dirty_count, sentinel, map, /*will_erase=*/true)};
648 view.BatchWrite(cursor, {});
649 BOOST_CHECK_EQUAL(dirty_count, 0U);
650}
651
653{
654public:
655 SingleEntryCacheTest(const CAmount base_value, const MaybeCoin& cache_coin)
656 {
657 auto base_cache_coin{base_value == ABSENT ? MISSING : CoinEntry{base_value, CoinEntry::State::DIRTY}};
658 WriteCoinsViewEntry(base, base_cache_coin);
659 if (cache_coin) {
660 cache.usage() += InsertCoinsMapEntry(cache.map(), cache.sentinel(), *cache_coin);
661 cache.dirty() += cache_coin->IsDirty();
662 }
663 }
664
665 CCoinsViewCacheTest base{&CoinsViewEmpty::Get()};
666 CCoinsViewCacheTest cache{&base};
667};
668
669static void CheckAccessCoin(const CAmount base_value, const MaybeCoin& cache_coin, const MaybeCoin& expected)
670{
671 SingleEntryCacheTest test{base_value, cache_coin};
672 auto& coin = test.cache.AccessCoin(OUTPOINT);
673 BOOST_CHECK_EQUAL(coin.IsSpent(), !test.cache.GetCoin(OUTPOINT));
674 test.cache.SelfTest(/*sanity_check=*/false);
675 BOOST_CHECK_EQUAL(GetCoinsMapEntry(test.cache.map()), expected);
676}
677
679{
680 /* Check AccessCoin behavior, requesting a coin from a cache view layered on
681 * top of a base view, and checking the resulting entry in the cache after
682 * the access.
683 * Base Cache Expected
684 */
685 for (auto base_value : {ABSENT, SPENT, VALUE1}) {
686 CheckAccessCoin(base_value, MISSING, base_value == VALUE1 ? VALUE1_CLEAN : MISSING);
687
692
697 }
698}
699
700static void CheckSpendCoins(const CAmount base_value, const MaybeCoin& cache_coin, const MaybeCoin& expected)
701{
702 SingleEntryCacheTest test{base_value, cache_coin};
703 test.cache.SpendCoin(OUTPOINT);
704 test.cache.SelfTest();
705 BOOST_CHECK_EQUAL(GetCoinsMapEntry(test.cache.map()), expected);
706}
707
709{
710 /* Check SpendCoin behavior, requesting a coin from a cache view layered on
711 * top of a base view, spending, and then checking
712 * the resulting entry in the cache after the modification.
713 * Base Cache Expected
714 */
715 for (auto base_value : {ABSENT, SPENT, VALUE1}) {
716 CheckSpendCoins(base_value, MISSING, base_value == VALUE1 ? SPENT_DIRTY : MISSING);
717
719 CheckSpendCoins(base_value, SPENT_FRESH, MISSING );
722
724 CheckSpendCoins(base_value, VALUE2_FRESH, MISSING );
727 }
728}
729
730static void CheckAddCoin(const CAmount base_value, const MaybeCoin& cache_coin, const CAmount modify_value, const CoinOrError& expected, const bool coinbase)
731{
732 SingleEntryCacheTest test{base_value, cache_coin};
733 bool possible_overwrite{coinbase};
734 auto add_coin{[&] { test.cache.AddCoin(OUTPOINT, Coin{CTxOut{modify_value, CScript{}}, 1, coinbase}, possible_overwrite); }};
735 if (auto* expected_coin{std::get_if<MaybeCoin>(&expected)}) {
736 add_coin();
737 test.cache.SelfTest();
738 BOOST_CHECK_EQUAL(GetCoinsMapEntry(test.cache.map()), *expected_coin);
739 } else {
740 BOOST_CHECK_EXCEPTION(add_coin(), std::logic_error, HasReason(std::get<std::string>(expected)));
741 }
742}
743
745{
746 /* Check AddCoin behavior, requesting a new coin from a cache view,
747 * writing a modification to the coin, and then checking the resulting
748 * entry in the cache after the modification. Verify behavior with the
749 * AddCoin coinbase argument set to false, and to true.
750 * Base Cache Write Expected Coinbase
751 */
752 for (auto base_value : {ABSENT, SPENT, VALUE1}) {
753 CheckAddCoin(base_value, MISSING, VALUE3, VALUE3_DIRTY_FRESH, false);
754 CheckAddCoin(base_value, MISSING, VALUE3, VALUE3_DIRTY, true );
755
757 CheckAddCoin(base_value, SPENT_CLEAN, VALUE3, VALUE3_DIRTY, true );
760 CheckAddCoin(base_value, SPENT_DIRTY, VALUE3, VALUE3_DIRTY, false);
761 CheckAddCoin(base_value, SPENT_DIRTY, VALUE3, VALUE3_DIRTY, true );
764
766 CheckAddCoin(base_value, VALUE2_CLEAN, VALUE3, VALUE3_DIRTY, true );
770 CheckAddCoin(base_value, VALUE2_DIRTY, VALUE3, VALUE3_DIRTY, true );
773 }
774}
775
776static void CheckWriteCoins(const MaybeCoin& parent, const MaybeCoin& child, const CoinOrError& expected)
777{
778 SingleEntryCacheTest test{ABSENT, parent};
779 auto write_coins{[&] { WriteCoinsViewEntry(test.cache, child); }};
780 if (auto* expected_coin{std::get_if<MaybeCoin>(&expected)}) {
781 write_coins();
782 test.cache.SelfTest(/*sanity_check=*/false);
783 BOOST_CHECK_EQUAL(GetCoinsMapEntry(test.cache.map()), *expected_coin);
784 } else {
785 BOOST_CHECK_EXCEPTION(write_coins(), std::logic_error, HasReason(std::get<std::string>(expected)));
786 }
787}
788
790{
791 /* Check BatchWrite behavior, flushing one entry from a child cache to a
792 * parent cache, and checking the resulting entry in the parent cache
793 * after the write.
794 * Parent Child Expected
795 */
805
814
823
836
845
846 // The checks above omit cases where the child state is not DIRTY, since
847 // they would be too repetitive (the parent cache is never updated in these
848 // cases). The loop below covers these cases and makes sure the parent cache
849 // is always left unchanged.
850 for (const MaybeCoin& parent : {MISSING,
853 for (const MaybeCoin& child : {MISSING,
856 auto expected{CoinOrError{parent}}; // TODO test failure cases as well
857 CheckWriteCoins(parent, child, expected);
858 }
859 }
860}
861
864{
865 Coin coin;
866 coin.out.nValue = m_rng.rand32();
867 coin.nHeight = m_rng.randrange(4096);
868 coin.fCoinBase = false;
869 return coin;
870}
871
872
884 CCoinsViewCacheTest* view,
885 CCoinsViewDB& base,
886 std::vector<std::unique_ptr<CCoinsViewCacheTest>>& all_caches,
887 bool do_erasing_flush)
888{
889 size_t cache_usage;
890 size_t cache_size;
891
892 auto flush_all = [this, &all_caches](bool erase) {
893 // Flush in reverse order to ensure that flushes happen from children up.
894 for (auto i = all_caches.rbegin(); i != all_caches.rend(); ++i) {
895 auto& cache = *i;
896 cache->SanityCheck();
897 // block_hash must be filled before flushing to disk; value is
898 // unimportant here. This is normally done during connect/disconnect block.
899 cache->SetBestBlock(m_rng.rand256());
900 erase ? cache->Flush() : cache->Sync();
901 }
902 };
903
905 COutPoint outp = COutPoint(txid, 0);
906 Coin coin = MakeCoin();
907 // Ensure the coins views haven't seen this coin before.
908 BOOST_CHECK(!base.HaveCoin(outp));
909 BOOST_CHECK(!view->HaveCoin(outp));
910
911 // --- 1. Adding a random coin to the child cache
912 //
913 view->AddCoin(outp, Coin(coin), false);
914
915 cache_usage = view->DynamicMemoryUsage();
916 cache_size = view->map().size();
917
918 // `base` shouldn't have coin (no flush yet) but `view` should have cached it.
919 BOOST_CHECK(!base.HaveCoin(outp));
920 BOOST_CHECK(view->HaveCoin(outp));
921
922 BOOST_CHECK_EQUAL(GetCoinsMapEntry(view->map(), outp), CoinEntry(coin.out.nValue, CoinEntry::State::DIRTY_FRESH));
923
924 // --- 2. Flushing all caches (without erasing)
925 //
926 flush_all(/*erase=*/ false);
927
928 // CoinsMap usage should be unchanged since we didn't erase anything.
929 BOOST_CHECK_EQUAL(cache_usage, view->DynamicMemoryUsage());
930 BOOST_CHECK_EQUAL(cache_size, view->map().size());
931
932 // --- 3. Ensuring the entry still exists in the cache and has been written to parent
933 //
934 BOOST_CHECK_EQUAL(GetCoinsMapEntry(view->map(), outp), CoinEntry(coin.out.nValue, CoinEntry::State::CLEAN)); // State should have been wiped.
935
936 // Both views should now have the coin.
937 BOOST_CHECK(base.HaveCoin(outp));
938 BOOST_CHECK(view->HaveCoin(outp));
939
940 if (do_erasing_flush) {
941 // --- 4. Flushing the caches again (with erasing)
942 //
943 flush_all(/*erase=*/ true);
944
945 // Memory does not necessarily go down due to the map using a memory pool
946 BOOST_TEST(view->DynamicMemoryUsage() <= cache_usage);
947 // Size of the cache must go down though
948 BOOST_TEST(view->map().size() < cache_size);
949
950 // --- 5. Ensuring the entry is no longer in the cache
951 //
952 BOOST_CHECK(!GetCoinsMapEntry(view->map(), outp));
953 view->AccessCoin(outp);
954 BOOST_CHECK_EQUAL(GetCoinsMapEntry(view->map(), outp), CoinEntry(coin.out.nValue, CoinEntry::State::CLEAN));
955 }
956
957 // Can't overwrite an entry without specifying that an overwrite is
958 // expected.
960 view->AddCoin(outp, Coin(coin), /*possible_overwrite=*/ false),
961 std::logic_error);
962
963 // --- 6. Spend the coin.
964 //
965 BOOST_CHECK(view->SpendCoin(outp));
966
967 // The coin should be in the cache, but spent and marked dirty.
969 BOOST_CHECK(!view->HaveCoin(outp)); // Coin should be considered spent in `view`.
970 BOOST_CHECK(base.HaveCoin(outp)); // But coin should still be unspent in `base`.
971
972 flush_all(/*erase=*/ false);
973
974 // Coin should be considered spent in both views.
975 BOOST_CHECK(!view->HaveCoin(outp));
976 BOOST_CHECK(!base.HaveCoin(outp));
977
978 // Spent coin should not be spendable.
979 BOOST_CHECK(!view->SpendCoin(outp));
980
981 // --- Bonus check: ensure that a coin added to the base view via one cache
982 // can be spent by another cache which has never seen it.
983 //
985 outp = COutPoint(txid, 0);
986 coin = MakeCoin();
987 BOOST_CHECK(!base.HaveCoin(outp));
988 BOOST_CHECK(!all_caches[0]->HaveCoin(outp));
989 BOOST_CHECK(!all_caches[1]->HaveCoin(outp));
990
991 all_caches[0]->AddCoin(outp, std::move(coin), false);
992 all_caches[0]->Sync();
993 BOOST_CHECK(base.HaveCoin(outp));
994 BOOST_CHECK(all_caches[0]->HaveCoin(outp));
995 BOOST_CHECK(!all_caches[1]->HaveCoinInCache(outp));
996
997 BOOST_CHECK(all_caches[1]->SpendCoin(outp));
998 flush_all(/*erase=*/ false);
999 BOOST_CHECK(!base.HaveCoin(outp));
1000 BOOST_CHECK(!all_caches[0]->HaveCoin(outp));
1001 BOOST_CHECK(!all_caches[1]->HaveCoin(outp));
1002
1003 flush_all(/*erase=*/ true); // Erase all cache content.
1004
1005 // --- Bonus check 2: ensure that a FRESH, spent coin is deleted by Sync()
1006 //
1008 outp = COutPoint(txid, 0);
1009 coin = MakeCoin();
1010 CAmount coin_val = coin.out.nValue;
1011 BOOST_CHECK(!base.HaveCoin(outp));
1012 BOOST_CHECK(!all_caches[0]->HaveCoin(outp));
1013 BOOST_CHECK(!all_caches[1]->HaveCoin(outp));
1014
1015 // Add and spend from same cache without flushing.
1016 all_caches[0]->AddCoin(outp, std::move(coin), false);
1017
1018 // Coin should be FRESH in the cache.
1019 BOOST_CHECK_EQUAL(GetCoinsMapEntry(all_caches[0]->map(), outp), CoinEntry(coin_val, CoinEntry::State::DIRTY_FRESH));
1020 // Base shouldn't have seen coin.
1021 BOOST_CHECK(!base.HaveCoin(outp));
1022
1023 BOOST_CHECK(all_caches[0]->SpendCoin(outp));
1024 all_caches[0]->Sync();
1025
1026 // Ensure there is no sign of the coin after spend/flush.
1027 BOOST_CHECK(!GetCoinsMapEntry(all_caches[0]->map(), outp));
1028 BOOST_CHECK(!all_caches[0]->HaveCoinInCache(outp));
1029 BOOST_CHECK(!base.HaveCoin(outp));
1030}
1031}; // struct FlushTest
1032
1033BOOST_FIXTURE_TEST_CASE(ccoins_flush_behavior, FlushTest)
1034{
1035 // Create two in-memory caches atop a leveldb view.
1036 CCoinsViewDB base{{.path = "test", .cache_bytes = 8_MiB, .memory_only = true}, {}};
1037 std::vector<std::unique_ptr<CCoinsViewCacheTest>> caches;
1038 caches.push_back(std::make_unique<CCoinsViewCacheTest>(&base));
1039 caches.push_back(std::make_unique<CCoinsViewCacheTest>(caches.back().get()));
1040
1041 for (const auto& view : caches) {
1042 TestFlushBehavior(view.get(), base, caches, /*do_erasing_flush=*/false);
1043 TestFlushBehavior(view.get(), base, caches, /*do_erasing_flush=*/true);
1044 }
1045}
1046
1047BOOST_FIXTURE_TEST_CASE(coins_db_leveldb_layout, FlushTest)
1048{
1049 auto level2_files{[](CCoinsViewDB& base) {
1050 return *Assert(ToIntegral<int>(*Assert(base.GetDBProperty("leveldb.num-files-at-level2"))));
1051 }};
1052 const COutPoint outpoint{Txid::FromUint256(m_rng.rand256()), 0};
1053 const Coin coin{MakeCoin()};
1054 const uint256 block_hash{m_rng.rand256()};
1055
1056 CCoinsViewDB base{{.path = m_args.GetDataDirBase() / "coins_db_leveldb_layout", .cache_bytes = 1_MiB, .wipe_data = true}, {}};
1057 CCoinsViewCache cache{&base};
1058
1059 cache.EmplaceCoinInternalDANGER(COutPoint{outpoint}, Coin{coin});
1060 cache.SetBestBlock(block_hash);
1061 cache.Sync();
1062
1063 BOOST_CHECK_EQUAL(level2_files(base), 0);
1064 WITH_LOCK(::cs_main, return base.CompactFullAsync()).wait();
1065 BOOST_CHECK_EQUAL(level2_files(base), 1);
1066
1067 BOOST_CHECK_EQUAL(*Assert(base.GetCoin(outpoint)), coin);
1068 BOOST_CHECK_EQUAL(base.GetBestBlock(), block_hash);
1069}
1070
1071BOOST_AUTO_TEST_CASE(coins_resource_is_used)
1072{
1073 CCoinsMapMemoryResource resource;
1075
1076 {
1077 CCoinsMap map{0, CCoinsMap::hasher{}, CCoinsMap::key_equal{}, &resource};
1078 BOOST_TEST(memusage::DynamicUsage(map) >= resource.ChunkSizeBytes());
1079
1080 map.reserve(1000);
1081
1082 // The resource has preallocated a chunk, so we should have space for at several nodes without the need to allocate anything else.
1083 const auto usage_before = memusage::DynamicUsage(map);
1084
1085 COutPoint out_point{};
1086 for (size_t i = 0; i < 1000; ++i) {
1087 out_point.n = i;
1088 map[out_point];
1089 }
1090 BOOST_TEST(usage_before == memusage::DynamicUsage(map));
1091 }
1092
1094}
1095
1096BOOST_AUTO_TEST_CASE(ccoins_addcoin_exception_keeps_usage_balanced)
1097{
1098 CCoinsViewCacheTest cache{&CoinsViewEmpty::Get()};
1099
1100 const COutPoint outpoint{Txid::FromUint256(m_rng.rand256()), m_rng.rand32()};
1101
1102 const Coin coin1{CTxOut{m_rng.randrange(10), CScript{} << m_rng.randbytes(CScriptBase::STATIC_SIZE + 1)}, 1, false};
1103 cache.AddCoin(outpoint, Coin{coin1}, /*possible_overwrite=*/false);
1104 cache.SelfTest();
1105
1106 const Coin coin2{CTxOut{m_rng.randrange(20), CScript{} << m_rng.randbytes(CScriptBase::STATIC_SIZE + 2)}, 2, false};
1107 BOOST_CHECK_THROW(cache.AddCoin(outpoint, Coin{coin2}, /*possible_overwrite=*/false), std::logic_error);
1108 cache.SelfTest();
1109
1110 BOOST_CHECK_EQUAL(cache.AccessCoin(outpoint), coin1);
1111}
1112
1113BOOST_AUTO_TEST_CASE(ccoins_emplace_duplicate_keeps_usage_balanced)
1114{
1115 CCoinsViewCacheTest cache{&CoinsViewEmpty::Get()};
1116
1117 const COutPoint outpoint{Txid::FromUint256(m_rng.rand256()), m_rng.rand32()};
1118
1119 const Coin coin1{CTxOut{m_rng.randrange(10), CScript{} << m_rng.randbytes(CScriptBase::STATIC_SIZE + 1)}, 1, false};
1120 cache.EmplaceCoinInternalDANGER(outpoint, Coin{coin1});
1121 cache.SelfTest();
1122
1123 const Coin coin2{CTxOut{m_rng.randrange(20), CScript{} << m_rng.randbytes(CScriptBase::STATIC_SIZE + 2)}, 2, false};
1124 cache.EmplaceCoinInternalDANGER(outpoint, Coin{coin2});
1125 cache.SelfTest();
1126
1127 BOOST_CHECK_EQUAL(cache.AccessCoin(outpoint), coin1);
1128}
1129
1130BOOST_AUTO_TEST_CASE(ccoins_reset_guard)
1131{
1132 CCoinsViewTest root{m_rng};
1133 CCoinsViewCache root_cache{&root};
1134 uint256 base_best_block{m_rng.rand256()};
1135 root_cache.SetBestBlock(base_best_block);
1136 root_cache.Flush();
1137
1138 CCoinsViewCache cache{&root};
1139
1140 const COutPoint outpoint{Txid::FromUint256(m_rng.rand256()), m_rng.rand32()};
1141
1142 const Coin coin{CTxOut{m_rng.randrange(10), CScript{} << m_rng.randbytes(CScriptBase::STATIC_SIZE + 1)}, 1, false};
1143 cache.EmplaceCoinInternalDANGER(outpoint, Coin{coin});
1144 BOOST_CHECK_EQUAL(cache.GetDirtyCount(), 1U);
1145
1146 uint256 cache_best_block{m_rng.rand256()};
1147 cache.SetBestBlock(cache_best_block);
1148
1149 {
1150 const auto reset_guard{cache.CreateResetGuard()};
1151 BOOST_CHECK_EQUAL(cache.AccessCoin(outpoint), coin);
1152 BOOST_CHECK(!cache.AccessCoin(outpoint).IsSpent());
1153 BOOST_CHECK_EQUAL(cache.GetCacheSize(), 1);
1154 BOOST_CHECK_EQUAL(cache.GetDirtyCount(), 1);
1155 BOOST_CHECK_EQUAL(cache.GetBestBlock(), cache_best_block);
1156 BOOST_CHECK(!root_cache.HaveCoinInCache(outpoint));
1157 }
1158
1159 BOOST_CHECK(cache.AccessCoin(outpoint).IsSpent());
1160 BOOST_CHECK_EQUAL(cache.GetCacheSize(), 0);
1161 BOOST_CHECK_EQUAL(cache.GetDirtyCount(), 0);
1162 BOOST_CHECK_EQUAL(cache.GetBestBlock(), base_best_block);
1163 BOOST_CHECK(!root_cache.HaveCoinInCache(outpoint));
1164
1165 // Using a reset guard again is idempotent
1166 {
1167 const auto reset_guard{cache.CreateResetGuard()};
1168 }
1169
1170 BOOST_CHECK(cache.AccessCoin(outpoint).IsSpent());
1171 BOOST_CHECK_EQUAL(cache.GetCacheSize(), 0);
1172 BOOST_CHECK_EQUAL(cache.GetDirtyCount(), 0U);
1173 BOOST_CHECK_EQUAL(cache.GetBestBlock(), base_best_block);
1174 BOOST_CHECK(!root_cache.HaveCoinInCache(outpoint));
1175
1176 // Flush should be a no-op after reset.
1177 cache.Flush();
1178 BOOST_CHECK_EQUAL(cache.GetDirtyCount(), 0U);
1179}
1180
1181BOOST_AUTO_TEST_CASE(ccoins_peekcoin)
1182{
1183 CCoinsViewTest base{m_rng};
1184
1185 // Populate the base view with a coin.
1186 const COutPoint outpoint{Txid::FromUint256(m_rng.rand256()), m_rng.rand32()};
1187 const Coin coin{CTxOut{m_rng.randrange(10), CScript{}}, 1, false};
1188 {
1189 CCoinsViewCache cache{&base};
1190 cache.AddCoin(outpoint, Coin{coin}, /*possible_overwrite=*/false);
1191 cache.Flush();
1192 }
1193
1194 // Verify PeekCoin can read through the cache stack without mutating the intermediate cache.
1195 CCoinsViewCacheTest main_cache{&base};
1196 const auto fetched{main_cache.PeekCoin(outpoint)};
1197 BOOST_CHECK(fetched.has_value());
1198 BOOST_CHECK_EQUAL(*fetched, coin);
1199 BOOST_CHECK(!main_cache.HaveCoinInCache(outpoint));
1200}
1201
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
int ret
#define Assert(val)
Identity function.
Definition: check.h:116
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:437
void EmplaceCoinInternalDANGER(const COutPoint &outpoint, Coin &&coin)
Emplace a coin into cacheCoins without performing any checks, marking the emplaced coin as dirty.
Definition: coins.cpp:123
void AddCoin(const COutPoint &outpoint, Coin &&coin, bool possible_overwrite)
Add a coin.
Definition: coins.cpp:80
size_t m_dirty_count
Definition: coins.h:468
unsigned int GetCacheSize() const
Size of the cache (in number of transaction outputs)
Definition: coins.cpp:318
size_t cachedCoinsUsage
Definition: coins.h:466
CoinsCachePair m_sentinel
Definition: coins.h:462
size_t DynamicMemoryUsage() const
Calculate the size of the cache (in bytes)
Definition: coins.cpp:50
void SanityCheck() const
Run an internal sanity check on the cache data structure. *‍/.
Definition: coins.cpp:344
CCoinsMap cacheCoins
Definition: coins.h:463
CCoinsView backed by the coin database (chainstate/)
Definition: txdb.h:37
bool HaveCoin(const COutPoint &outpoint) const override
Just check whether a given outpoint is unspent.
Definition: txdb.cpp:102
Pure abstract view on the open txout dataset.
Definition: coins.h:356
virtual void BatchWrite(CoinsViewCacheCursor &cursor, const uint256 &block_hash)=0
Do a bulk modification (multiple Coin changes + BestBlock change).
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:29
uint32_t n
Definition: transaction.h:32
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:406
bool IsUnspendable() const
Returns whether the script is guaranteed to fail at execution, regardless of the initial stack.
Definition: script.h:564
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:281
bool IsCoinBase() const
Definition: transaction.h:341
const std::vector< CTxIn > vin
Definition: transaction.h:291
An output of a transaction.
Definition: transaction.h:140
CScript scriptPubKey
Definition: transaction.h:143
CAmount nValue
Definition: transaction.h:142
Undo information for a CTransaction.
Definition: undo.h:54
std::vector< Coin > vprevout
Definition: undo.h:57
A UTXO entry.
Definition: coins.h:46
bool IsCoinBase() const
Definition: coins.h:70
void Clear()
Definition: coins.h:61
CTxOut out
unspent transaction output
Definition: coins.h:49
bool IsSpent() const
Either this coin never existed (see e.g.
Definition: coins.h:94
bool fCoinBase
whether containing transaction was a coinbase
Definition: coins.h:52
uint32_t nHeight
at which height this containing transaction was included in the active block chain
Definition: coins.h:55
Noop coins view.
Definition: coins.h:392
void BatchWrite(CoinsViewCacheCursor &cursor, const uint256 &) override
Do a bulk modification (multiple Coin changes + BestBlock change).
Definition: coins.h:407
uint256 GetBestBlock() const override
Retrieve the block hash whose state this CCoinsView currently represents.
Definition: coins.h:405
std::optional< Coin > GetCoin(const COutPoint &) const override
Retrieve the Coin (unspent transaction output) for a given outpoint.
Definition: coins.h:402
static CoinsViewEmpty & Get()
Definition: coins.cpp:29
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:165
Fast randomness source.
Definition: random.h:386
BOOST_CHECK_EXCEPTION predicates to check the specific validation error.
Definition: common.h:19
static void CheckAllDataAccountedFor(const PoolResource< MAX_BLOCK_SIZE_BYTES, ALIGN_BYTES > &resource)
Once all blocks are given back to the resource, tests that the freelists are consistent:
I randrange(I range) noexcept
Generate a random integer in the range [0..range), with range > 0.
Definition: random.h:254
uint256 rand256() noexcept
generate a random uint256.
Definition: random.h:317
bool randbool() noexcept
Generate a random boolean.
Definition: random.h:325
std::vector< B > randbytes(size_t len) noexcept
Generate random bytes.
Definition: random.h:297
uint32_t rand32() noexcept
Generate a random 32-bit integer.
Definition: random.h:314
uint64_t randbits(int bits) noexcept
Generate a random (bits)-bit integer.
Definition: random.h:204
CCoinsViewCacheTest cache
CCoinsViewCacheTest base
SingleEntryCacheTest(const CAmount base_value, const MaybeCoin &cache_coin)
Minimal stream for reading from an existing byte array by std::span.
Definition: streams.h:83
constexpr bool IsNull() const
Definition: uint256.h:50
size_type size() const
Definition: prevector.h:247
static constexpr unsigned int STATIC_SIZE
Definition: prevector.h:41
void assign(size_type n, const T &val)
Definition: prevector.h:176
static transaction_identifier FromUint256(const uint256 &id)
160-bit opaque blob.
Definition: uint256.h:184
256-bit opaque blob.
Definition: uint256.h:196
const Coin & AccessByTxid(const CCoinsViewCache &view, const Txid &txid)
Utility function to find any unspent output with a given txid.
Definition: coins.cpp:417
std::pair< const COutPoint, CCoinsCacheEntry > CoinsCachePair
Definition: coins.h:104
std::unordered_map< COutPoint, CCoinsCacheEntry, SaltedCoinsCacheHasher, 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:272
CCoinsMap::allocator_type::ResourceType CCoinsMapMemoryResource
Definition: coins.h:274
constexpr CAmount VALUE2
BOOST_AUTO_TEST_CASE(ccoins_serialization)
constexpr MaybeCoin VALUE2_DIRTY
constexpr CAmount ABSENT
std::optional< CoinEntry > MaybeCoin
constexpr MaybeCoin VALUE2_CLEAN
constexpr MaybeCoin MISSING
static MaybeCoin GetCoinsMapEntry(const CCoinsMap &map, const COutPoint &outp=OUTPOINT)
static const COutPoint OUTPOINT
constexpr MaybeCoin VALUE2_DIRTY_FRESH
static void WriteCoinsViewEntry(CCoinsView &view, const MaybeCoin &cache_coin)
static void CheckWriteCoins(const MaybeCoin &parent, const MaybeCoin &child, const CoinOrError &expected)
int ApplyTxInUndo(Coin &&undo, CCoinsViewCache &view, const COutPoint &out)
Restore the UTXO in a Coin at a given COutPoint.
static const unsigned int NUM_SIMULATION_ITERATIONS
Definition: coins_tests.cpp:99
constexpr MaybeCoin VALUE1_CLEAN
constexpr CAmount VALUE1
constexpr MaybeCoin SPENT_DIRTY_FRESH
constexpr MaybeCoin SPENT_CLEAN
constexpr auto EX_OVERWRITE_UNSPENT
constexpr MaybeCoin VALUE1_DIRTY
constexpr MaybeCoin VALUE1_FRESH
static size_t InsertCoinsMapEntry(CCoinsMap &map, CoinsCachePair &sentinel, const CoinEntry &cache_coin)
static void CheckSpendCoins(const CAmount base_value, const MaybeCoin &cache_coin, const MaybeCoin &expected)
void UpdateCoins(const CTransaction &tx, CCoinsViewCache &inputs, CTxUndo &txundo, int nHeight)
constexpr CAmount VALUE3
constexpr MaybeCoin VALUE2_FRESH
constexpr MaybeCoin VALUE3_DIRTY_FRESH
static void CheckAccessCoin(const CAmount base_value, const MaybeCoin &cache_coin, const MaybeCoin &expected)
constexpr MaybeCoin VALUE3_DIRTY
constexpr MaybeCoin VALUE1_DIRTY_FRESH
constexpr MaybeCoin SPENT_DIRTY
constexpr auto EX_FRESH_MISAPPLIED
std::variant< MaybeCoin, std::string > CoinOrError
static void SetCoinsValue(const CAmount value, Coin &coin)
BOOST_FIXTURE_TEST_CASE(coins_cache_base_simulation_test, CacheTest)
constexpr MaybeCoin SPENT_FRESH
constexpr CAmount SPENT
static void CheckAddCoin(const CAmount base_value, const MaybeCoin &cache_coin, const CAmount modify_value, const CoinOrError &expected, const bool coinbase)
std::vector< std::unique_ptr< CCoinsViewCache > > caches
Real CCoinsViewCache objects.
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: cs_main.cpp:8
BOOST_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_SUITE_END()
std::string HexStr(const std::span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
Definition: hex_base.cpp:30
BOOST_CHECK_EQUAL(headers.FindFirst("key"), "value")
BOOST_CHECK_EXCEPTION(HTTPHeaders{}.Read(reader), std::runtime_error, HasReason{"Empty HTTP header name"})
unsigned int nHeight
static bool sanity_check(const std::vector< CTransactionRef > &transactions, const std::map< COutPoint, CAmount > &bumpfees)
static size_t DynamicUsage(const int8_t &v)
Dynamic memory usage for built-in types is zero.
Definition: memusage.h:31
""_hex is a compile-time user-defined literal returning a std::array<std::byte>, equivalent to ParseH...
Definition: strencodings.h:386
static void add_coin(const CAmount &nValue, uint32_t nInput, std::vector< OutputGroup > &set)
static OutputGroup MakeCoin(const CAmount &amount, bool is_eff_value=true, CoinSelectionParams cs_params=default_cs_params, int custom_spending_vsize=P2WPKH_INPUT_VSIZE)
Make one OutputGroup with a single UTXO that either has a given effective value (default) or a given ...
#define BOOST_CHECK_THROW(stmt, excMatch)
Definition: object.cpp:18
#define BOOST_CHECK(expr)
Definition: object.cpp:16
@ OP_RETURN
Definition: script.h:112
#define VARINT(obj)
Definition: serialize.h:494
Basic testing setup.
Definition: setup_common.h:58
FastRandomContext m_rng
Definition: setup_common.h:62
A Coin in one level of the coins database caching hierarchy.
Definition: coins.h:121
Coin coin
Definition: coins.h:153
static void SetFresh(CoinsCachePair &pair, CoinsCachePair &sentinel) noexcept
Definition: coins.h:184
static void SetDirty(CoinsCachePair &pair, CoinsCachePair &sentinel) noexcept
Definition: coins.h:183
A mutable version of CTransaction.
Definition: transaction.h:358
std::vector< CTxOut > vout
Definition: transaction.h:360
Txid GetHash() const
Compute the hash of this CMutableTransaction.
Definition: transaction.cpp:69
std::vector< CTxIn > vin
Definition: transaction.h:359
void SimulationTest(CCoinsView *base, bool fake_best_block)
const CAmount value
constexpr bool IsDirty() const
friend std::ostream & operator<<(std::ostream &os, const CoinEntry &e)
bool operator==(const CoinEntry &o) const =default
constexpr bool IsDirtyFresh() const
State
@ DIRTY
@ FRESH
@ CLEAN
@ DIRTY_FRESH
static constexpr State ToState(const bool is_dirty, const bool is_fresh)
constexpr bool IsFresh() const
const State state
constexpr CoinEntry(const CAmount v, const State s)
Cursor for iterating over the linked list of flagged entries in CCoinsViewCache.
Definition: coins.h:309
CoinsCachePair * NextAndMaybeErase(CoinsCachePair &current) noexcept
Return the next entry after current, possibly erasing current.
Definition: coins.h:327
CoinsCachePair * Begin() const noexcept
Definition: coins.h:323
CoinsCachePair * End() const noexcept
Definition: coins.h:324
void TestFlushBehavior(CCoinsViewCacheTest *view, CCoinsViewDB &base, std::vector< std::unique_ptr< CCoinsViewCacheTest > > &all_caches, bool do_erasing_flush)
For CCoinsViewCache instances backed by either another cache instance or leveldb, test cache behavior...
Coin MakeCoin()
std::map< COutPoint, std::tuple< CTransaction, CTxUndo, Coin > > UtxoData
UtxoData::iterator FindRandomFrom(const std::set< COutPoint > &utxoSet)
UtxoData utxoData
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
Definition: sync.h:299
FastRandomContext rng
Definition: dbwrapper.cpp:413
@ ZEROS
Seed with a compile time constant of zeros.
CAmount RandMoney(Rng &&rng)
Definition: random.h:35
static int count
assert(!tx.IsCoinBase())