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