Bitcoin Core 28.0.0
P2P Digital Currency
Loading...
Searching...
No Matches
coins_tests.cpp
Go to the documentation of this file.
1// Copyright (c) 2014-2022 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>
10#include <test/util/random.h>
12#include <txdb.h>
13#include <uint256.h>
14#include <undo.h>
15#include <util/strencodings.h>
16
17#include <map>
18#include <vector>
19
20#include <boost/test/unit_test.hpp>
21
22int ApplyTxInUndo(Coin&& undo, CCoinsViewCache& view, const COutPoint& out);
23void UpdateCoins(const CTransaction& tx, CCoinsViewCache& inputs, CTxUndo &txundo, int nHeight);
24
25namespace
26{
28bool operator==(const Coin &a, const Coin &b) {
29 // Empty Coin objects are always equal.
30 if (a.IsSpent() && b.IsSpent()) return true;
31 return a.fCoinBase == b.fCoinBase &&
32 a.nHeight == b.nHeight &&
33 a.out == b.out;
34}
35
36class CCoinsViewTest : public CCoinsView
37{
38 uint256 hashBestBlock_;
39 std::map<COutPoint, Coin> map_;
40
41public:
42 [[nodiscard]] bool GetCoin(const COutPoint& outpoint, Coin& coin) const override
43 {
44 std::map<COutPoint, Coin>::const_iterator it = map_.find(outpoint);
45 if (it == map_.end()) {
46 return false;
47 }
48 coin = it->second;
49 if (coin.IsSpent() && InsecureRandBool() == 0) {
50 // Randomly return false in case of an empty entry.
51 return false;
52 }
53 return true;
54 }
55
56 uint256 GetBestBlock() const override { return hashBestBlock_; }
57
58 bool BatchWrite(CoinsViewCacheCursor& cursor, const uint256& hashBlock) override
59 {
60 for (auto it{cursor.Begin()}; it != cursor.End(); it = cursor.NextAndMaybeErase(*it)){
61 if (it->second.IsDirty()) {
62 // Same optimization used in CCoinsViewDB is to only write dirty entries.
63 map_[it->first] = it->second.coin;
64 if (it->second.coin.IsSpent() && InsecureRandRange(3) == 0) {
65 // Randomly delete empty entries on write.
66 map_.erase(it->first);
67 }
68 }
69 }
70 if (!hashBlock.IsNull())
71 hashBestBlock_ = hashBlock;
72 return true;
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};
101
102} // namespace
103
104BOOST_FIXTURE_TEST_SUITE(coins_tests, BasicTestingSetup)
105
106static const unsigned int NUM_SIMULATION_ITERATIONS = 40000;
107
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++) {
148 }
149
150 for (unsigned int i = 0; i < NUM_SIMULATION_ITERATIONS; i++) {
151 // Do a random modification.
152 {
153 auto txid = txids[InsecureRandRange(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 = InsecureRandBits(2) == 0;
160 bool test_havecoin_after = InsecureRandBits(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 = (InsecureRandRange(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 (InsecureRandRange(5) == 0 || coin.IsSpent()) {
180 Coin newcoin;
182 newcoin.nHeight = 1;
183
184 // Infrequently test adding unspendable coins.
185 if (InsecureRandRange(16) == 0 && coin.IsSpent()) {
188 added_an_unspendable_entry = true;
189 } else {
190 // Random sizes so we can test memory usage accounting
192 (coin.IsSpent() ? added_an_entry : updated_an_entry) = true;
193 coin = newcoin;
194 }
195 bool is_overwrite = !coin.IsSpent() || InsecureRand32() & 1;
196 stack.back()->AddCoin(COutPoint(txid, 0), std::move(newcoin), is_overwrite);
197 } else {
198 // Spend the coin.
199 removed_an_entry = true;
200 coin.Clear();
201 BOOST_CHECK(stack.back()->SpendCoin(COutPoint(txid, 0)));
202 }
203 }
204
205 // Once every 10 iterations, remove a random entry from the cache
206 if (InsecureRandRange(10) == 0) {
207 COutPoint out(txids[InsecureRand32() % txids.size()], 0);
208 int cacheid = InsecureRand32() % stack.size();
209 stack[cacheid]->Uncache(out);
210 uncached_an_entry |= !stack[cacheid]->HaveCoinInCache(out);
211 }
212
213 // Once every 1000 iterations and at the end, verify the full cache.
214 if (InsecureRandRange(1000) == 1 || i == NUM_SIMULATION_ITERATIONS - 1) {
215 for (const auto& entry : result) {
216 bool have = stack.back()->HaveCoin(entry.first);
217 const Coin& coin = stack.back()->AccessCoin(entry.first);
218 BOOST_CHECK(have == !coin.IsSpent());
219 BOOST_CHECK(coin == entry.second);
220 if (coin.IsSpent()) {
221 missed_an_entry = true;
222 } else {
223 BOOST_CHECK(stack.back()->HaveCoinInCache(entry.first));
224 found_an_entry = true;
225 }
226 }
227 for (const auto& test : stack) {
228 test->SelfTest();
229 }
230 }
231
232 if (InsecureRandRange(100) == 0) {
233 // Every 100 iterations, flush an intermediate cache
234 if (stack.size() > 1 && InsecureRandBool() == 0) {
235 unsigned int flushIndex = InsecureRandRange(stack.size() - 1);
236 if (fake_best_block) stack[flushIndex]->SetBestBlock(InsecureRand256());
237 bool should_erase = InsecureRandRange(4) < 3;
238 BOOST_CHECK(should_erase ? stack[flushIndex]->Flush() : stack[flushIndex]->Sync());
239 flushed_without_erase |= !should_erase;
240 }
241 }
242 if (InsecureRandRange(100) == 0) {
243 // Every 100 iterations, change the cache stack.
244 if (stack.size() > 0 && InsecureRandBool() == 0) {
245 //Remove the top cache
246 if (fake_best_block) stack.back()->SetBestBlock(InsecureRand256());
247 bool should_erase = InsecureRandRange(4) < 3;
248 BOOST_CHECK(should_erase ? stack.back()->Flush() : stack.back()->Sync());
249 flushed_without_erase |= !should_erase;
250 stack.pop_back();
251 }
252 if (stack.size() == 0 || (stack.size() < 4 && InsecureRandBool())) {
253 //Add a new cache
254 CCoinsView* tip = base;
255 if (stack.size() > 0) {
256 tip = stack.back().get();
257 } else {
258 removed_all_caches = true;
259 }
260 stack.push_back(std::make_unique<CCoinsViewCacheTest>(tip));
261 if (stack.size() == 4) {
262 reached_4_caches = true;
263 }
264 }
265 }
266 }
267
268 // Verify coverage.
269 BOOST_CHECK(removed_all_caches);
270 BOOST_CHECK(reached_4_caches);
271 BOOST_CHECK(added_an_entry);
272 BOOST_CHECK(added_an_unspendable_entry);
273 BOOST_CHECK(removed_an_entry);
274 BOOST_CHECK(updated_an_entry);
275 BOOST_CHECK(found_an_entry);
276 BOOST_CHECK(missed_an_entry);
277 BOOST_CHECK(uncached_an_entry);
278 BOOST_CHECK(flushed_without_erase);
279}
280
281// Run the above simulation for multiple base types.
282BOOST_AUTO_TEST_CASE(coins_cache_simulation_test)
283{
284 CCoinsViewTest base;
285 SimulationTest(&base, false);
286
287 CCoinsViewDB db_base{{.path = "test", .cache_bytes = 1 << 23, .memory_only = true}, {}};
288 SimulationTest(&db_base, true);
289}
290
291// Store of all necessary tx and undo data for next test
292typedef std::map<COutPoint, std::tuple<CTransaction,CTxUndo,Coin>> UtxoData;
294
295UtxoData::iterator FindRandomFrom(const std::set<COutPoint> &utxoSet) {
296 assert(utxoSet.size());
297 auto utxoSetIt = utxoSet.lower_bound(COutPoint(Txid::FromUint256(InsecureRand256()), 0));
298 if (utxoSetIt == utxoSet.end()) {
299 utxoSetIt = utxoSet.begin();
300 }
301 auto utxoDataIt = utxoData.find(*utxoSetIt);
302 assert(utxoDataIt != utxoData.end());
303 return utxoDataIt;
304}
305
306
307// This test is similar to the previous test
308// except the emphasis is on testing the functionality of UpdateCoins
309// random txs are created and UpdateCoins is used to update the cache stack
310// In particular it is tested that spending a duplicate coinbase tx
311// has the expected effect (the other duplicate is overwritten at all cache levels)
312BOOST_AUTO_TEST_CASE(updatecoins_simulation_test)
313{
315
316 bool spent_a_duplicate_coinbase = false;
317 // A simple map to track what we expect the cache stack to represent.
318 std::map<COutPoint, Coin> result;
319
320 // The cache stack.
321 CCoinsViewTest base; // A CCoinsViewTest at the bottom.
322 std::vector<std::unique_ptr<CCoinsViewCacheTest>> stack; // A stack of CCoinsViewCaches on top.
323 stack.push_back(std::make_unique<CCoinsViewCacheTest>(&base)); // Start with one cache.
324
325 // Track the txids we've used in various sets
326 std::set<COutPoint> coinbase_coins;
327 std::set<COutPoint> disconnected_coins;
328 std::set<COutPoint> duplicate_coins;
329 std::set<COutPoint> utxoset;
330
331 for (unsigned int i = 0; i < NUM_SIMULATION_ITERATIONS; i++) {
332 uint32_t randiter = InsecureRand32();
333
334 // 19/20 txs add a new transaction
335 if (randiter % 20 < 19) {
337 tx.vin.resize(1);
338 tx.vout.resize(1);
339 tx.vout[0].nValue = i; //Keep txs unique unless intended to duplicate
340 tx.vout[0].scriptPubKey.assign(InsecureRand32() & 0x3F, 0); // Random sizes so we can test memory usage accounting
341 const int height{int(InsecureRand32() >> 1)};
342 Coin old_coin;
343
344 // 2/20 times create a new coinbase
345 if (randiter % 20 < 2 || coinbase_coins.size() < 10) {
346 // 1/10 of those times create a duplicate coinbase
347 if (InsecureRandRange(10) == 0 && coinbase_coins.size()) {
348 auto utxod = FindRandomFrom(coinbase_coins);
349 // Reuse the exact same coinbase
350 tx = CMutableTransaction{std::get<0>(utxod->second)};
351 // shouldn't be available for reconnection if it's been duplicated
352 disconnected_coins.erase(utxod->first);
353
354 duplicate_coins.insert(utxod->first);
355 }
356 else {
357 coinbase_coins.insert(COutPoint(tx.GetHash(), 0));
358 }
359 assert(CTransaction(tx).IsCoinBase());
360 }
361
362 // 17/20 times reconnect previous or add a regular tx
363 else {
364
365 COutPoint prevout;
366 // 1/20 times reconnect a previously disconnected tx
367 if (randiter % 20 == 2 && disconnected_coins.size()) {
368 auto utxod = FindRandomFrom(disconnected_coins);
369 tx = CMutableTransaction{std::get<0>(utxod->second)};
370 prevout = tx.vin[0].prevout;
371 if (!CTransaction(tx).IsCoinBase() && !utxoset.count(prevout)) {
372 disconnected_coins.erase(utxod->first);
373 continue;
374 }
375
376 // If this tx is already IN the UTXO, then it must be a coinbase, and it must be a duplicate
377 if (utxoset.count(utxod->first)) {
378 assert(CTransaction(tx).IsCoinBase());
379 assert(duplicate_coins.count(utxod->first));
380 }
381 disconnected_coins.erase(utxod->first);
382 }
383
384 // 16/20 times create a regular tx
385 else {
386 auto utxod = FindRandomFrom(utxoset);
387 prevout = utxod->first;
388
389 // Construct the tx to spend the coins of prevouthash
390 tx.vin[0].prevout = prevout;
391 assert(!CTransaction(tx).IsCoinBase());
392 }
393 // In this simple test coins only have two states, spent or unspent, save the unspent state to restore
394 old_coin = result[prevout];
395 // Update the expected result of prevouthash to know these coins are spent
396 result[prevout].Clear();
397
398 utxoset.erase(prevout);
399
400 // The test is designed to ensure spending a duplicate coinbase will work properly
401 // if that ever happens and not resurrect the previously overwritten coinbase
402 if (duplicate_coins.count(prevout)) {
403 spent_a_duplicate_coinbase = true;
404 }
405
406 }
407 // Update the expected result to know about the new output coins
408 assert(tx.vout.size() == 1);
409 const COutPoint outpoint(tx.GetHash(), 0);
410 result[outpoint] = Coin{tx.vout[0], height, CTransaction{tx}.IsCoinBase()};
411
412 // Call UpdateCoins on the top cache
413 CTxUndo undo;
414 UpdateCoins(CTransaction{tx}, *(stack.back()), undo, height);
415
416 // Update the utxo set for future spends
417 utxoset.insert(outpoint);
418
419 // Track this tx and undo info to use later
420 utxoData.emplace(outpoint, std::make_tuple(tx,undo,old_coin));
421 } else if (utxoset.size()) {
422 //1/20 times undo a previous transaction
423 auto utxod = FindRandomFrom(utxoset);
424
425 CTransaction &tx = std::get<0>(utxod->second);
426 CTxUndo &undo = std::get<1>(utxod->second);
427 Coin &orig_coin = std::get<2>(utxod->second);
428
429 // Update the expected result
430 // Remove new outputs
431 result[utxod->first].Clear();
432 // If not coinbase restore prevout
433 if (!tx.IsCoinBase()) {
434 result[tx.vin[0].prevout] = orig_coin;
435 }
436
437 // Disconnect the tx from the current UTXO
438 // See code in DisconnectBlock
439 // remove outputs
440 BOOST_CHECK(stack.back()->SpendCoin(utxod->first));
441 // restore inputs
442 if (!tx.IsCoinBase()) {
443 const COutPoint &out = tx.vin[0].prevout;
444 Coin coin = undo.vprevout[0];
445 ApplyTxInUndo(std::move(coin), *(stack.back()), out);
446 }
447 // Store as a candidate for reconnection
448 disconnected_coins.insert(utxod->first);
449
450 // Update the utxoset
451 utxoset.erase(utxod->first);
452 if (!tx.IsCoinBase())
453 utxoset.insert(tx.vin[0].prevout);
454 }
455
456 // Once every 1000 iterations and at the end, verify the full cache.
457 if (InsecureRandRange(1000) == 1 || i == NUM_SIMULATION_ITERATIONS - 1) {
458 for (const auto& entry : result) {
459 bool have = stack.back()->HaveCoin(entry.first);
460 const Coin& coin = stack.back()->AccessCoin(entry.first);
461 BOOST_CHECK(have == !coin.IsSpent());
462 BOOST_CHECK(coin == entry.second);
463 }
464 }
465
466 // One every 10 iterations, remove a random entry from the cache
467 if (utxoset.size() > 1 && InsecureRandRange(30) == 0) {
468 stack[InsecureRand32() % stack.size()]->Uncache(FindRandomFrom(utxoset)->first);
469 }
470 if (disconnected_coins.size() > 1 && InsecureRandRange(30) == 0) {
471 stack[InsecureRand32() % stack.size()]->Uncache(FindRandomFrom(disconnected_coins)->first);
472 }
473 if (duplicate_coins.size() > 1 && InsecureRandRange(30) == 0) {
474 stack[InsecureRand32() % stack.size()]->Uncache(FindRandomFrom(duplicate_coins)->first);
475 }
476
477 if (InsecureRandRange(100) == 0) {
478 // Every 100 iterations, flush an intermediate cache
479 if (stack.size() > 1 && InsecureRandBool() == 0) {
480 unsigned int flushIndex = InsecureRandRange(stack.size() - 1);
481 BOOST_CHECK(stack[flushIndex]->Flush());
482 }
483 }
484 if (InsecureRandRange(100) == 0) {
485 // Every 100 iterations, change the cache stack.
486 if (stack.size() > 0 && InsecureRandBool() == 0) {
487 BOOST_CHECK(stack.back()->Flush());
488 stack.pop_back();
489 }
490 if (stack.size() == 0 || (stack.size() < 4 && InsecureRandBool())) {
491 CCoinsView* tip = &base;
492 if (stack.size() > 0) {
493 tip = stack.back().get();
494 }
495 stack.push_back(std::make_unique<CCoinsViewCacheTest>(tip));
496 }
497 }
498 }
499
500 // Verify coverage.
501 BOOST_CHECK(spent_a_duplicate_coinbase);
502}
503
504BOOST_AUTO_TEST_CASE(ccoins_serialization)
505{
506 // Good example
507 DataStream ss1{ParseHex("97f23c835800816115944e077fe7c803cfa57f29b36bf87c1d35")};
508 Coin cc1;
509 ss1 >> cc1;
510 BOOST_CHECK_EQUAL(cc1.fCoinBase, false);
511 BOOST_CHECK_EQUAL(cc1.nHeight, 203998U);
512 BOOST_CHECK_EQUAL(cc1.out.nValue, CAmount{60000000000});
513 BOOST_CHECK_EQUAL(HexStr(cc1.out.scriptPubKey), HexStr(GetScriptForDestination(PKHash(uint160(ParseHex("816115944e077fe7c803cfa57f29b36bf87c1d35"))))));
514
515 // Good example
516 DataStream ss2{ParseHex("8ddf77bbd123008c988f1a4a4de2161e0f50aac7f17e7f9555caa4")};
517 Coin cc2;
518 ss2 >> cc2;
519 BOOST_CHECK_EQUAL(cc2.fCoinBase, true);
520 BOOST_CHECK_EQUAL(cc2.nHeight, 120891U);
521 BOOST_CHECK_EQUAL(cc2.out.nValue, 110397);
522 BOOST_CHECK_EQUAL(HexStr(cc2.out.scriptPubKey), HexStr(GetScriptForDestination(PKHash(uint160(ParseHex("8c988f1a4a4de2161e0f50aac7f17e7f9555caa4"))))));
523
524 // Smallest possible example
525 DataStream ss3{ParseHex("000006")};
526 Coin cc3;
527 ss3 >> cc3;
528 BOOST_CHECK_EQUAL(cc3.fCoinBase, false);
529 BOOST_CHECK_EQUAL(cc3.nHeight, 0U);
532
533 // scriptPubKey that ends beyond the end of the stream
534 DataStream ss4{ParseHex("000007")};
535 try {
536 Coin cc4;
537 ss4 >> cc4;
538 BOOST_CHECK_MESSAGE(false, "We should have thrown");
539 } catch (const std::ios_base::failure&) {
540 }
541
542 // Very large scriptPubKey (3*10^9 bytes) past the end of the stream
543 DataStream tmp{};
544 uint64_t x = 3000000000ULL;
545 tmp << VARINT(x);
546 BOOST_CHECK_EQUAL(HexStr(tmp), "8a95c0bb00");
547 DataStream ss5{ParseHex("00008a95c0bb00")};
548 try {
549 Coin cc5;
550 ss5 >> cc5;
551 BOOST_CHECK_MESSAGE(false, "We should have thrown");
552 } catch (const std::ios_base::failure&) {
553 }
554}
555
556const static COutPoint OUTPOINT;
557const static CAmount SPENT = -1;
558const static CAmount ABSENT = -2;
559const static CAmount FAIL = -3;
560const static CAmount VALUE1 = 100;
561const static CAmount VALUE2 = 200;
562const static CAmount VALUE3 = 300;
563const static char DIRTY = CCoinsCacheEntry::DIRTY;
564const static char FRESH = CCoinsCacheEntry::FRESH;
565const static char NO_ENTRY = -1;
566
567const static auto FLAGS = {char(0), FRESH, DIRTY, char(DIRTY | FRESH)};
568const static auto CLEAN_FLAGS = {char(0), FRESH};
569const static auto ABSENT_FLAGS = {NO_ENTRY};
570
571static void SetCoinsValue(CAmount value, Coin& coin)
572{
573 assert(value != ABSENT);
574 coin.Clear();
575 assert(coin.IsSpent());
576 if (value != SPENT) {
577 coin.out.nValue = value;
578 coin.nHeight = 1;
579 assert(!coin.IsSpent());
580 }
581}
582
583static size_t InsertCoinsMapEntry(CCoinsMap& map, CoinsCachePair& sentinel, CAmount value, char flags)
584{
585 if (value == ABSENT) {
587 return 0;
588 }
590 CCoinsCacheEntry entry;
591 SetCoinsValue(value, entry.coin);
592 auto inserted = map.emplace(OUTPOINT, std::move(entry));
593 assert(inserted.second);
594 inserted.first->second.AddFlags(flags, *inserted.first, sentinel);
595 return inserted.first->second.coin.DynamicMemoryUsage();
596}
597
598void GetCoinsMapEntry(const CCoinsMap& map, CAmount& value, char& flags, const COutPoint& outp = OUTPOINT)
599{
600 auto it = map.find(outp);
601 if (it == map.end()) {
602 value = ABSENT;
603 flags = NO_ENTRY;
604 } else {
605 if (it->second.coin.IsSpent()) {
606 value = SPENT;
607 } else {
608 value = it->second.coin.out.nValue;
609 }
610 flags = it->second.GetFlags();
612 }
613}
614
616{
617 CoinsCachePair sentinel{};
618 sentinel.second.SelfRef(sentinel);
620 CCoinsMap map{0, CCoinsMap::hasher{}, CCoinsMap::key_equal{}, &resource};
621 auto usage{InsertCoinsMapEntry(map, sentinel, value, flags)};
622 auto cursor{CoinsViewCacheCursor(usage, sentinel, map, /*will_erase=*/true)};
623 BOOST_CHECK(view.BatchWrite(cursor, {}));
624}
625
627{
628public:
629 SingleEntryCacheTest(CAmount base_value, CAmount cache_value, char cache_flags)
630 {
631 WriteCoinsViewEntry(base, base_value, base_value == ABSENT ? NO_ENTRY : DIRTY);
632 cache.usage() += InsertCoinsMapEntry(cache.map(), cache.sentinel(), cache_value, cache_flags);
633 }
634
636 CCoinsViewCacheTest base{&root};
637 CCoinsViewCacheTest cache{&base};
638};
639
640static void CheckAccessCoin(CAmount base_value, CAmount cache_value, CAmount expected_value, char cache_flags, char expected_flags)
641{
642 SingleEntryCacheTest test(base_value, cache_value, cache_flags);
643 test.cache.AccessCoin(OUTPOINT);
644 test.cache.SelfTest(/*sanity_check=*/false);
645
646 CAmount result_value;
647 char result_flags;
648 GetCoinsMapEntry(test.cache.map(), result_value, result_flags);
649 BOOST_CHECK_EQUAL(result_value, expected_value);
650 BOOST_CHECK_EQUAL(result_flags, expected_flags);
651}
652
654{
655 /* Check AccessCoin behavior, requesting a coin from a cache view layered on
656 * top of a base view, and checking the resulting entry in the cache after
657 * the access.
658 *
659 * Base Cache Result Cache Result
660 * Value Value Value Flags Flags
661 */
663 CheckAccessCoin(ABSENT, SPENT , SPENT , 0 , 0 );
672 CheckAccessCoin(SPENT , SPENT , SPENT , 0 , 0 );
681 CheckAccessCoin(VALUE1, SPENT , SPENT , 0 , 0 );
689}
690
691static void CheckSpendCoins(CAmount base_value, CAmount cache_value, CAmount expected_value, char cache_flags, char expected_flags)
692{
693 SingleEntryCacheTest test(base_value, cache_value, cache_flags);
694 test.cache.SpendCoin(OUTPOINT);
695 test.cache.SelfTest();
696
697 CAmount result_value;
698 char result_flags;
699 GetCoinsMapEntry(test.cache.map(), result_value, result_flags);
700 BOOST_CHECK_EQUAL(result_value, expected_value);
701 BOOST_CHECK_EQUAL(result_flags, expected_flags);
702};
703
705{
706 /* Check SpendCoin behavior, requesting a coin from a cache view layered on
707 * top of a base view, spending, and then checking
708 * the resulting entry in the cache after the modification.
709 *
710 * Base Cache Result Cache Result
711 * Value Value Value Flags Flags
712 */
740}
741
742static void CheckAddCoinBase(CAmount base_value, CAmount cache_value, CAmount modify_value, CAmount expected_value, char cache_flags, char expected_flags, bool coinbase)
743{
744 SingleEntryCacheTest test(base_value, cache_value, cache_flags);
745
746 CAmount result_value;
747 char result_flags;
748 try {
749 CTxOut output;
750 output.nValue = modify_value;
751 test.cache.AddCoin(OUTPOINT, Coin(std::move(output), 1, coinbase), coinbase);
752 test.cache.SelfTest();
753 GetCoinsMapEntry(test.cache.map(), result_value, result_flags);
754 } catch (std::logic_error&) {
755 result_value = FAIL;
756 result_flags = NO_ENTRY;
757 }
758
759 BOOST_CHECK_EQUAL(result_value, expected_value);
760 BOOST_CHECK_EQUAL(result_flags, expected_flags);
761}
762
763// Simple wrapper for CheckAddCoinBase function above that loops through
764// different possible base_values, making sure each one gives the same results.
765// This wrapper lets the coins_add test below be shorter and less repetitive,
766// while still verifying that the CoinsViewCache::AddCoin implementation
767// ignores base values.
768template <typename... Args>
769static void CheckAddCoin(Args&&... args)
770{
771 for (const CAmount base_value : {ABSENT, SPENT, VALUE1})
772 CheckAddCoinBase(base_value, std::forward<Args>(args)...);
773}
774
776{
777 /* Check AddCoin behavior, requesting a new coin from a cache view,
778 * writing a modification to the coin, and then checking the resulting
779 * entry in the cache after the modification. Verify behavior with the
780 * AddCoin possible_overwrite argument set to false, and to true.
781 *
782 * Cache Write Result Cache Result possible_overwrite
783 * Value Value Value Flags Flags
784 */
787 CheckAddCoin(SPENT , VALUE3, VALUE3, 0 , DIRTY|FRESH, false);
788 CheckAddCoin(SPENT , VALUE3, VALUE3, 0 , DIRTY , true );
791 CheckAddCoin(SPENT , VALUE3, VALUE3, DIRTY , DIRTY , false);
792 CheckAddCoin(SPENT , VALUE3, VALUE3, DIRTY , DIRTY , true );
795 CheckAddCoin(VALUE2, VALUE3, FAIL , 0 , NO_ENTRY , false);
796 CheckAddCoin(VALUE2, VALUE3, VALUE3, 0 , DIRTY , true );
803}
804
805void CheckWriteCoins(CAmount parent_value, CAmount child_value, CAmount expected_value, char parent_flags, char child_flags, char expected_flags)
806{
807 SingleEntryCacheTest test(ABSENT, parent_value, parent_flags);
808
809 CAmount result_value;
810 char result_flags;
811 try {
812 WriteCoinsViewEntry(test.cache, child_value, child_flags);
813 test.cache.SelfTest(/*sanity_check=*/false);
814 GetCoinsMapEntry(test.cache.map(), result_value, result_flags);
815 } catch (std::logic_error&) {
816 result_value = FAIL;
817 result_flags = NO_ENTRY;
818 }
819
820 BOOST_CHECK_EQUAL(result_value, expected_value);
821 BOOST_CHECK_EQUAL(result_flags, expected_flags);
822}
823
825{
826 /* Check BatchWrite behavior, flushing one entry from a child cache to a
827 * parent cache, and checking the resulting entry in the parent cache
828 * after the write.
829 *
830 * Parent Child Result Parent Child Result
831 * Value Value Value Flags Flags Flags
832 */
878
879 // The checks above omit cases where the child flags are not DIRTY, since
880 // they would be too repetitive (the parent cache is never updated in these
881 // cases). The loop below covers these cases and makes sure the parent cache
882 // is always left unchanged.
883 for (const CAmount parent_value : {ABSENT, SPENT, VALUE1})
884 for (const CAmount child_value : {ABSENT, SPENT, VALUE2})
885 for (const char parent_flags : parent_value == ABSENT ? ABSENT_FLAGS : FLAGS)
886 for (const char child_flags : child_value == ABSENT ? ABSENT_FLAGS : CLEAN_FLAGS)
887 CheckWriteCoins(parent_value, child_value, parent_value, parent_flags, child_flags, parent_flags);
888}
889
890
892{
893 Coin coin;
894 coin.out.nValue = InsecureRand32();
895 coin.nHeight = InsecureRandRange(4096);
896 coin.fCoinBase = 0;
897 return coin;
898}
899
900
912 CCoinsViewCacheTest* view,
913 CCoinsViewDB& base,
914 std::vector<std::unique_ptr<CCoinsViewCacheTest>>& all_caches,
915 bool do_erasing_flush)
916{
917 CAmount value;
918 char flags;
919 size_t cache_usage;
920 size_t cache_size;
921
922 auto flush_all = [&all_caches](bool erase) {
923 // Flush in reverse order to ensure that flushes happen from children up.
924 for (auto i = all_caches.rbegin(); i != all_caches.rend(); ++i) {
925 auto& cache = *i;
926 cache->SanityCheck();
927 // hashBlock must be filled before flushing to disk; value is
928 // unimportant here. This is normally done during connect/disconnect block.
929 cache->SetBestBlock(InsecureRand256());
930 erase ? cache->Flush() : cache->Sync();
931 }
932 };
933
935 COutPoint outp = COutPoint(txid, 0);
936 Coin coin = MakeCoin();
937 // Ensure the coins views haven't seen this coin before.
938 BOOST_CHECK(!base.HaveCoin(outp));
939 BOOST_CHECK(!view->HaveCoin(outp));
940
941 // --- 1. Adding a random coin to the child cache
942 //
943 view->AddCoin(outp, Coin(coin), false);
944
945 cache_usage = view->DynamicMemoryUsage();
946 cache_size = view->map().size();
947
948 // `base` shouldn't have coin (no flush yet) but `view` should have cached it.
949 BOOST_CHECK(!base.HaveCoin(outp));
950 BOOST_CHECK(view->HaveCoin(outp));
951
952 GetCoinsMapEntry(view->map(), value, flags, outp);
953 BOOST_CHECK_EQUAL(value, coin.out.nValue);
955
956 // --- 2. Flushing all caches (without erasing)
957 //
958 flush_all(/*erase=*/ false);
959
960 // CoinsMap usage should be unchanged since we didn't erase anything.
961 BOOST_CHECK_EQUAL(cache_usage, view->DynamicMemoryUsage());
962 BOOST_CHECK_EQUAL(cache_size, view->map().size());
963
964 // --- 3. Ensuring the entry still exists in the cache and has been written to parent
965 //
966 GetCoinsMapEntry(view->map(), value, flags, outp);
967 BOOST_CHECK_EQUAL(value, coin.out.nValue);
968 BOOST_CHECK_EQUAL(flags, 0); // Flags should have been wiped.
969
970 // Both views should now have the coin.
971 BOOST_CHECK(base.HaveCoin(outp));
972 BOOST_CHECK(view->HaveCoin(outp));
973
974 if (do_erasing_flush) {
975 // --- 4. Flushing the caches again (with erasing)
976 //
977 flush_all(/*erase=*/ true);
978
979 // Memory does not necessarily go down due to the map using a memory pool
980 BOOST_TEST(view->DynamicMemoryUsage() <= cache_usage);
981 // Size of the cache must go down though
982 BOOST_TEST(view->map().size() < cache_size);
983
984 // --- 5. Ensuring the entry is no longer in the cache
985 //
986 GetCoinsMapEntry(view->map(), value, flags, outp);
989
990 view->AccessCoin(outp);
991 GetCoinsMapEntry(view->map(), value, flags, outp);
992 BOOST_CHECK_EQUAL(value, coin.out.nValue);
994 }
995
996 // Can't overwrite an entry without specifying that an overwrite is
997 // expected.
999 view->AddCoin(outp, Coin(coin), /*possible_overwrite=*/ false),
1000 std::logic_error);
1001
1002 // --- 6. Spend the coin.
1003 //
1004 BOOST_CHECK(view->SpendCoin(outp));
1005
1006 // The coin should be in the cache, but spent and marked dirty.
1007 GetCoinsMapEntry(view->map(), value, flags, outp);
1008 BOOST_CHECK_EQUAL(value, SPENT);
1010 BOOST_CHECK(!view->HaveCoin(outp)); // Coin should be considered spent in `view`.
1011 BOOST_CHECK(base.HaveCoin(outp)); // But coin should still be unspent in `base`.
1012
1013 flush_all(/*erase=*/ false);
1014
1015 // Coin should be considered spent in both views.
1016 BOOST_CHECK(!view->HaveCoin(outp));
1017 BOOST_CHECK(!base.HaveCoin(outp));
1018
1019 // Spent coin should not be spendable.
1020 BOOST_CHECK(!view->SpendCoin(outp));
1021
1022 // --- Bonus check: ensure that a coin added to the base view via one cache
1023 // can be spent by another cache which has never seen it.
1024 //
1026 outp = COutPoint(txid, 0);
1027 coin = MakeCoin();
1028 BOOST_CHECK(!base.HaveCoin(outp));
1029 BOOST_CHECK(!all_caches[0]->HaveCoin(outp));
1030 BOOST_CHECK(!all_caches[1]->HaveCoin(outp));
1031
1032 all_caches[0]->AddCoin(outp, std::move(coin), false);
1033 all_caches[0]->Sync();
1034 BOOST_CHECK(base.HaveCoin(outp));
1035 BOOST_CHECK(all_caches[0]->HaveCoin(outp));
1036 BOOST_CHECK(!all_caches[1]->HaveCoinInCache(outp));
1037
1038 BOOST_CHECK(all_caches[1]->SpendCoin(outp));
1039 flush_all(/*erase=*/ false);
1040 BOOST_CHECK(!base.HaveCoin(outp));
1041 BOOST_CHECK(!all_caches[0]->HaveCoin(outp));
1042 BOOST_CHECK(!all_caches[1]->HaveCoin(outp));
1043
1044 flush_all(/*erase=*/ true); // Erase all cache content.
1045
1046 // --- Bonus check 2: ensure that a FRESH, spent coin is deleted by Sync()
1047 //
1049 outp = COutPoint(txid, 0);
1050 coin = MakeCoin();
1051 CAmount coin_val = coin.out.nValue;
1052 BOOST_CHECK(!base.HaveCoin(outp));
1053 BOOST_CHECK(!all_caches[0]->HaveCoin(outp));
1054 BOOST_CHECK(!all_caches[1]->HaveCoin(outp));
1055
1056 // Add and spend from same cache without flushing.
1057 all_caches[0]->AddCoin(outp, std::move(coin), false);
1058
1059 // Coin should be FRESH in the cache.
1060 GetCoinsMapEntry(all_caches[0]->map(), value, flags, outp);
1061 BOOST_CHECK_EQUAL(value, coin_val);
1063
1064 // Base shouldn't have seen coin.
1065 BOOST_CHECK(!base.HaveCoin(outp));
1066
1067 BOOST_CHECK(all_caches[0]->SpendCoin(outp));
1068 all_caches[0]->Sync();
1069
1070 // Ensure there is no sign of the coin after spend/flush.
1071 GetCoinsMapEntry(all_caches[0]->map(), value, flags, outp);
1072 BOOST_CHECK_EQUAL(value, ABSENT);
1074 BOOST_CHECK(!all_caches[0]->HaveCoinInCache(outp));
1075 BOOST_CHECK(!base.HaveCoin(outp));
1076}
1077
1078BOOST_AUTO_TEST_CASE(ccoins_flush_behavior)
1079{
1080 // Create two in-memory caches atop a leveldb view.
1081 CCoinsViewDB base{{.path = "test", .cache_bytes = 1 << 23, .memory_only = true}, {}};
1082 std::vector<std::unique_ptr<CCoinsViewCacheTest>> caches;
1083 caches.push_back(std::make_unique<CCoinsViewCacheTest>(&base));
1084 caches.push_back(std::make_unique<CCoinsViewCacheTest>(caches.back().get()));
1085
1086 for (const auto& view : caches) {
1087 TestFlushBehavior(view.get(), base, caches, /*do_erasing_flush=*/false);
1088 TestFlushBehavior(view.get(), base, caches, /*do_erasing_flush=*/true);
1089 }
1090}
1091
1092BOOST_AUTO_TEST_CASE(coins_resource_is_used)
1093{
1094 CCoinsMapMemoryResource resource;
1096
1097 {
1098 CCoinsMap map{0, CCoinsMap::hasher{}, CCoinsMap::key_equal{}, &resource};
1099 BOOST_TEST(memusage::DynamicUsage(map) >= resource.ChunkSizeBytes());
1100
1101 map.reserve(1000);
1102
1103 // The resource has preallocated a chunk, so we should have space for at several nodes without the need to allocate anything else.
1104 const auto usage_before = memusage::DynamicUsage(map);
1105
1106 COutPoint out_point{};
1107 for (size_t i = 0; i < 1000; ++i) {
1108 out_point.n = i;
1109 map[out_point];
1110 }
1111 BOOST_TEST(usage_before == memusage::DynamicUsage(map));
1112 }
1113
1115}
1116
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
int flags
ArgsManager & args
Definition bitcoind.cpp:270
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition coins.h:360
CCoinsView backed by the coin database (chainstate/)
Definition txdb.h:54
bool HaveCoin(const COutPoint &outpoint) const override
Just check whether a given outpoint is unspent.
Definition txdb.cpp:72
Abstract view on the open txout dataset.
Definition coins.h:304
virtual bool BatchWrite(CoinsViewCacheCursor &cursor, const uint256 &hashBlock)
Do a bulk modification (multiple Coin changes + BestBlock change).
Definition coins.cpp:15
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition transaction.h:29
bool IsUnspendable() const
Returns whether the script is guaranteed to fail at execution, regardless of the initial stack.
Definition script.h:560
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:33
void Clear()
Definition coins.h:48
CTxOut out
unspent transaction output
Definition coins.h:36
bool IsSpent() const
Either this coin never existed (see e.g.
Definition coins.h:81
uint32_t nHeight
at which height this containing transaction was included in the active block chain
Definition coins.h:42
unsigned int fCoinBase
whether containing transaction was a coinbase
Definition coins.h:39
Double ended buffer combining vector and stream-like interfaces.
Definition streams.h:147
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:
CCoinsViewCacheTest cache
CCoinsViewCacheTest base
SingleEntryCacheTest(CAmount base_value, CAmount cache_value, char cache_flags)
CCoinsView root
constexpr bool IsNull() const
Definition uint256.h:46
size_type size() const
Definition prevector.h:296
void assign(size_type n, const T &val)
Definition prevector.h:225
static transaction_identifier FromUint256(const uint256 &id)
160-bit opaque blob.
Definition uint256.h:166
256-bit opaque blob.
Definition uint256.h:178
const Coin & AccessByTxid(const CCoinsViewCache &view, const Txid &txid)
Utility function to find any unspent output with a given txid.
Definition coins.cpp:355
std::pair< const COutPoint, CCoinsCacheEntry > CoinsCachePair
Definition coins.h:91
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:218
CCoinsMap::allocator_type::ResourceType CCoinsMapMemoryResource
Definition coins.h:225
static void CheckAddCoin(Args &&... args)
static const char DIRTY
Coin MakeCoin()
static const COutPoint OUTPOINT
static const CAmount ABSENT
void WriteCoinsViewEntry(CCoinsView &view, CAmount value, char flags)
static size_t InsertCoinsMapEntry(CCoinsMap &map, CoinsCachePair &sentinel, CAmount value, char flags)
static const CAmount VALUE2
static const CAmount SPENT
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
std::map< COutPoint, std::tuple< CTransaction, CTxUndo, Coin > > UtxoData
static void CheckAddCoinBase(CAmount base_value, CAmount cache_value, CAmount modify_value, CAmount expected_value, char cache_flags, char expected_flags, bool coinbase)
static const char FRESH
UtxoData utxoData
void UpdateCoins(const CTransaction &tx, CCoinsViewCache &inputs, CTxUndo &txundo, int nHeight)
static void CheckAccessCoin(CAmount base_value, CAmount cache_value, CAmount expected_value, char cache_flags, char expected_flags)
static void SetCoinsValue(CAmount value, Coin &coin)
void CheckWriteCoins(CAmount parent_value, CAmount child_value, CAmount expected_value, char parent_flags, char child_flags, char expected_flags)
static const CAmount VALUE1
static const char NO_ENTRY
static const auto FLAGS
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...
BOOST_AUTO_TEST_CASE(coins_cache_simulation_test)
static const auto ABSENT_FLAGS
void GetCoinsMapEntry(const CCoinsMap &map, CAmount &value, char &flags, const COutPoint &outp=OUTPOINT)
static const CAmount VALUE3
static void CheckSpendCoins(CAmount base_value, CAmount cache_value, CAmount expected_value, char cache_flags, char expected_flags)
void SimulationTest(CCoinsView *base, bool fake_best_block)
UtxoData::iterator FindRandomFrom(const std::set< COutPoint > &utxoSet)
static const auto CLEAN_FLAGS
static const CAmount FAIL
BOOST_AUTO_TEST_SUITE_END()
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
Definition hex_base.cpp:29
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:30
bool operator==(const CNetAddr &a, const CNetAddr &b)
#define BOOST_CHECK_THROW(stmt, excMatch)
Definition object.cpp:19
#define BOOST_CHECK_EQUAL(v1, v2)
Definition object.cpp:18
#define BOOST_CHECK(expr)
Definition object.cpp:17
@ OP_RETURN
Definition script.h:110
#define VARINT(obj)
Definition serialize.h:498
std::vector< Byte > ParseHex(std::string_view hex_str)
Like TryParseHex, but returns an empty vector on invalid input.
Basic testing setup.
A Coin in one level of the coins database caching hierarchy.
Definition coins.h:109
Coin coin
Definition coins.h:132
@ FRESH
FRESH means the parent cache does not have this coin or that it is a spent coin in the parent cache.
Definition coins.h:152
@ DIRTY
DIRTY means the CCoinsCacheEntry is potentially different from the version in the parent cache.
Definition coins.h:142
A mutable version of CTransaction.
std::vector< CTxOut > vout
Txid GetHash() const
Compute the hash of this CMutableTransaction.
std::vector< CTxIn > vin
Cursor for iterating over the linked list of flagged entries in CCoinsViewCache.
Definition coins.h:260
CoinsCachePair * NextAndMaybeErase(CoinsCachePair &current) noexcept
Return the next entry after current, possibly erasing current.
Definition coins.h:278
CoinsCachePair * Begin() const noexcept
Definition coins.h:274
CoinsCachePair * End() const noexcept
Definition coins.h:275
void SeedRandomForTest(SeedRand seedtype)
Seed the RNG for testing.
Definition random.cpp:18
@ ZEROS
Seed with a compile time constant of zeros.
static CAmount InsecureRandMoneyAmount()
Definition random.h:55
static uint64_t InsecureRandRange(uint64_t range)
Definition random.h:45
static uint256 InsecureRand256()
Definition random.h:35
static uint64_t InsecureRandBits(int bits)
Definition random.h:40
static uint32_t InsecureRand32()
Definition random.h:30
static bool InsecureRandBool()
Definition random.h:50
static int count
assert(!tx.IsCoinBase())