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