Bitcoin Core  24.99.0
P2P Digital Currency
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 <clientversion.h>
6 #include <coins.h>
7 #include <script/standard.h>
8 #include <streams.h>
10 #include <txdb.h>
11 #include <uint256.h>
12 #include <undo.h>
13 #include <util/strencodings.h>
14 
15 #include <map>
16 #include <vector>
17 
18 #include <boost/test/unit_test.hpp>
19 
20 int ApplyTxInUndo(Coin&& undo, CCoinsViewCache& view, const COutPoint& out);
21 void UpdateCoins(const CTransaction& tx, CCoinsViewCache& inputs, CTxUndo &txundo, int nHeight);
22 
23 namespace
24 {
26 bool operator==(const Coin &a, const Coin &b) {
27  // Empty Coin objects are always equal.
28  if (a.IsSpent() && b.IsSpent()) return true;
29  return a.fCoinBase == b.fCoinBase &&
30  a.nHeight == b.nHeight &&
31  a.out == b.out;
32 }
33 
34 class CCoinsViewTest : public CCoinsView
35 {
36  uint256 hashBestBlock_;
37  std::map<COutPoint, Coin> map_;
38 
39 public:
40  [[nodiscard]] bool GetCoin(const COutPoint& outpoint, Coin& coin) const override
41  {
42  std::map<COutPoint, Coin>::const_iterator it = map_.find(outpoint);
43  if (it == map_.end()) {
44  return false;
45  }
46  coin = it->second;
47  if (coin.IsSpent() && InsecureRandBool() == 0) {
48  // Randomly return false in case of an empty entry.
49  return false;
50  }
51  return true;
52  }
53 
54  uint256 GetBestBlock() const override { return hashBestBlock_; }
55 
56  bool BatchWrite(CCoinsMap& mapCoins, const uint256& hashBlock, bool erase = true) override
57  {
58  for (CCoinsMap::iterator it = mapCoins.begin(); it != mapCoins.end(); it = erase ? mapCoins.erase(it) : std::next(it)) {
59  if (it->second.flags & CCoinsCacheEntry::DIRTY) {
60  // Same optimization used in CCoinsViewDB is to only write dirty entries.
61  map_[it->first] = it->second.coin;
62  if (it->second.coin.IsSpent() && InsecureRandRange(3) == 0) {
63  // Randomly delete empty entries on write.
64  map_.erase(it->first);
65  }
66  }
67  }
68  if (!hashBlock.IsNull())
69  hashBestBlock_ = hashBlock;
70  return true;
71  }
72 };
73 
74 class CCoinsViewCacheTest : public CCoinsViewCache
75 {
76 public:
77  explicit CCoinsViewCacheTest(CCoinsView* _base) : CCoinsViewCache(_base) {}
78 
79  void SelfTest() const
80  {
81  // Manually recompute the dynamic usage of the whole data, and compare it.
82  size_t ret = memusage::DynamicUsage(cacheCoins);
83  size_t count = 0;
84  for (const auto& entry : cacheCoins) {
85  ret += entry.second.coin.DynamicMemoryUsage();
86  ++count;
87  }
90  }
91 
92  CCoinsMap& map() const { return cacheCoins; }
93  size_t& usage() const { return cachedCoinsUsage; }
94 };
95 
96 } // namespace
97 
98 BOOST_FIXTURE_TEST_SUITE(coins_tests, BasicTestingSetup)
99 
100 static const unsigned int NUM_SIMULATION_ITERATIONS = 40000;
101 
102 // This is a large randomized insert/remove simulation test on a variable-size
103 // stack of caches on top of CCoinsViewTest.
104 //
105 // It will randomly create/update/delete Coin entries to a tip of caches, with
106 // txids picked from a limited list of random 256-bit hashes. Occasionally, a
107 // new tip is added to the stack of caches, or the tip is flushed and removed.
108 //
109 // During the process, booleans are kept to make sure that the randomized
110 // operation hits all branches.
111 //
112 // If fake_best_block is true, assign a random uint256 to mock the recording
113 // of best block on flush. This is necessary when using CCoinsViewDB as the base,
114 // otherwise we'll hit an assertion in BatchWrite.
115 //
116 void SimulationTest(CCoinsView* base, bool fake_best_block)
117 {
118  // Various coverage trackers.
119  bool removed_all_caches = false;
120  bool reached_4_caches = false;
121  bool added_an_entry = false;
122  bool added_an_unspendable_entry = false;
123  bool removed_an_entry = false;
124  bool updated_an_entry = false;
125  bool found_an_entry = false;
126  bool missed_an_entry = false;
127  bool uncached_an_entry = false;
128  bool flushed_without_erase = false;
129 
130  // A simple map to track what we expect the cache stack to represent.
131  std::map<COutPoint, Coin> result;
132 
133  // The cache stack.
134  std::vector<std::unique_ptr<CCoinsViewCacheTest>> stack; // A stack of CCoinsViewCaches on top.
135  stack.push_back(std::make_unique<CCoinsViewCacheTest>(base)); // Start with one cache.
136 
137  // Use a limited set of random transaction ids, so we do test overwriting entries.
138  std::vector<uint256> txids;
139  txids.resize(NUM_SIMULATION_ITERATIONS / 8);
140  for (unsigned int i = 0; i < txids.size(); i++) {
141  txids[i] = InsecureRand256();
142  }
143 
144  for (unsigned int i = 0; i < NUM_SIMULATION_ITERATIONS; i++) {
145  // Do a random modification.
146  {
147  uint256 txid = txids[InsecureRandRange(txids.size())]; // txid we're going to modify in this iteration.
148  Coin& coin = result[COutPoint(txid, 0)];
149 
150  // Determine whether to test HaveCoin before or after Access* (or both). As these functions
151  // can influence each other's behaviour by pulling things into the cache, all combinations
152  // are tested.
153  bool test_havecoin_before = InsecureRandBits(2) == 0;
154  bool test_havecoin_after = InsecureRandBits(2) == 0;
155 
156  bool result_havecoin = test_havecoin_before ? stack.back()->HaveCoin(COutPoint(txid, 0)) : false;
157 
158  // Infrequently, test usage of AccessByTxid instead of AccessCoin - the
159  // former just delegates to the latter and returns the first unspent in a txn.
160  const Coin& entry = (InsecureRandRange(500) == 0) ?
161  AccessByTxid(*stack.back(), txid) : stack.back()->AccessCoin(COutPoint(txid, 0));
162  BOOST_CHECK(coin == entry);
163 
164  if (test_havecoin_before) {
165  BOOST_CHECK(result_havecoin == !entry.IsSpent());
166  }
167 
168  if (test_havecoin_after) {
169  bool ret = stack.back()->HaveCoin(COutPoint(txid, 0));
170  BOOST_CHECK(ret == !entry.IsSpent());
171  }
172 
173  if (InsecureRandRange(5) == 0 || coin.IsSpent()) {
174  Coin newcoin;
175  newcoin.out.nValue = InsecureRand32();
176  newcoin.nHeight = 1;
177 
178  // Infrequently test adding unspendable coins.
179  if (InsecureRandRange(16) == 0 && coin.IsSpent()) {
182  added_an_unspendable_entry = true;
183  } else {
184  // Random sizes so we can test memory usage accounting
185  newcoin.out.scriptPubKey.assign(InsecureRandBits(6), 0);
186  (coin.IsSpent() ? added_an_entry : updated_an_entry) = true;
187  coin = newcoin;
188  }
189  bool is_overwrite = !coin.IsSpent() || InsecureRand32() & 1;
190  stack.back()->AddCoin(COutPoint(txid, 0), std::move(newcoin), is_overwrite);
191  } else {
192  // Spend the coin.
193  removed_an_entry = true;
194  coin.Clear();
195  BOOST_CHECK(stack.back()->SpendCoin(COutPoint(txid, 0)));
196  }
197  }
198 
199  // Once every 10 iterations, remove a random entry from the cache
200  if (InsecureRandRange(10) == 0) {
201  COutPoint out(txids[InsecureRand32() % txids.size()], 0);
202  int cacheid = InsecureRand32() % stack.size();
203  stack[cacheid]->Uncache(out);
204  uncached_an_entry |= !stack[cacheid]->HaveCoinInCache(out);
205  }
206 
207  // Once every 1000 iterations and at the end, verify the full cache.
208  if (InsecureRandRange(1000) == 1 || i == NUM_SIMULATION_ITERATIONS - 1) {
209  for (const auto& entry : result) {
210  bool have = stack.back()->HaveCoin(entry.first);
211  const Coin& coin = stack.back()->AccessCoin(entry.first);
212  BOOST_CHECK(have == !coin.IsSpent());
213  BOOST_CHECK(coin == entry.second);
214  if (coin.IsSpent()) {
215  missed_an_entry = true;
216  } else {
217  BOOST_CHECK(stack.back()->HaveCoinInCache(entry.first));
218  found_an_entry = true;
219  }
220  }
221  for (const auto& test : stack) {
222  test->SelfTest();
223  }
224  }
225 
226  if (InsecureRandRange(100) == 0) {
227  // Every 100 iterations, flush an intermediate cache
228  if (stack.size() > 1 && InsecureRandBool() == 0) {
229  unsigned int flushIndex = InsecureRandRange(stack.size() - 1);
230  if (fake_best_block) stack[flushIndex]->SetBestBlock(InsecureRand256());
231  bool should_erase = InsecureRandRange(4) < 3;
232  BOOST_CHECK(should_erase ? stack[flushIndex]->Flush() : stack[flushIndex]->Sync());
233  flushed_without_erase |= !should_erase;
234  }
235  }
236  if (InsecureRandRange(100) == 0) {
237  // Every 100 iterations, change the cache stack.
238  if (stack.size() > 0 && InsecureRandBool() == 0) {
239  //Remove the top cache
240  if (fake_best_block) stack.back()->SetBestBlock(InsecureRand256());
241  bool should_erase = InsecureRandRange(4) < 3;
242  BOOST_CHECK(should_erase ? stack.back()->Flush() : stack.back()->Sync());
243  flushed_without_erase |= !should_erase;
244  stack.pop_back();
245  }
246  if (stack.size() == 0 || (stack.size() < 4 && InsecureRandBool())) {
247  //Add a new cache
248  CCoinsView* tip = base;
249  if (stack.size() > 0) {
250  tip = stack.back().get();
251  } else {
252  removed_all_caches = true;
253  }
254  stack.push_back(std::make_unique<CCoinsViewCacheTest>(tip));
255  if (stack.size() == 4) {
256  reached_4_caches = true;
257  }
258  }
259  }
260  }
261 
262  // Verify coverage.
263  BOOST_CHECK(removed_all_caches);
264  BOOST_CHECK(reached_4_caches);
265  BOOST_CHECK(added_an_entry);
266  BOOST_CHECK(added_an_unspendable_entry);
267  BOOST_CHECK(removed_an_entry);
268  BOOST_CHECK(updated_an_entry);
269  BOOST_CHECK(found_an_entry);
270  BOOST_CHECK(missed_an_entry);
271  BOOST_CHECK(uncached_an_entry);
272  BOOST_CHECK(flushed_without_erase);
273 }
274 
275 // Run the above simulation for multiple base types.
276 BOOST_AUTO_TEST_CASE(coins_cache_simulation_test)
277 {
278  CCoinsViewTest base;
279  SimulationTest(&base, false);
280 
281  CCoinsViewDB db_base{"test", /*nCacheSize=*/1 << 23, /*fMemory=*/true, /*fWipe=*/false};
282  SimulationTest(&db_base, true);
283 }
284 
285 // Store of all necessary tx and undo data for next test
286 typedef std::map<COutPoint, std::tuple<CTransaction,CTxUndo,Coin>> UtxoData;
288 
289 UtxoData::iterator FindRandomFrom(const std::set<COutPoint> &utxoSet) {
290  assert(utxoSet.size());
291  auto utxoSetIt = utxoSet.lower_bound(COutPoint(InsecureRand256(), 0));
292  if (utxoSetIt == utxoSet.end()) {
293  utxoSetIt = utxoSet.begin();
294  }
295  auto utxoDataIt = utxoData.find(*utxoSetIt);
296  assert(utxoDataIt != utxoData.end());
297  return utxoDataIt;
298 }
299 
300 
301 // This test is similar to the previous test
302 // except the emphasis is on testing the functionality of UpdateCoins
303 // random txs are created and UpdateCoins is used to update the cache stack
304 // In particular it is tested that spending a duplicate coinbase tx
305 // has the expected effect (the other duplicate is overwritten at all cache levels)
306 BOOST_AUTO_TEST_CASE(updatecoins_simulation_test)
307 {
310 
311  bool spent_a_duplicate_coinbase = false;
312  // A simple map to track what we expect the cache stack to represent.
313  std::map<COutPoint, Coin> result;
314 
315  // The cache stack.
316  CCoinsViewTest base; // A CCoinsViewTest at the bottom.
317  std::vector<std::unique_ptr<CCoinsViewCacheTest>> stack; // A stack of CCoinsViewCaches on top.
318  stack.push_back(std::make_unique<CCoinsViewCacheTest>(&base)); // Start with one cache.
319 
320  // Track the txids we've used in various sets
321  std::set<COutPoint> coinbase_coins;
322  std::set<COutPoint> disconnected_coins;
323  std::set<COutPoint> duplicate_coins;
324  std::set<COutPoint> utxoset;
325 
326  for (unsigned int i = 0; i < NUM_SIMULATION_ITERATIONS; i++) {
327  uint32_t randiter = InsecureRand32();
328 
329  // 19/20 txs add a new transaction
330  if (randiter % 20 < 19) {
332  tx.vin.resize(1);
333  tx.vout.resize(1);
334  tx.vout[0].nValue = i; //Keep txs unique unless intended to duplicate
335  tx.vout[0].scriptPubKey.assign(InsecureRand32() & 0x3F, 0); // Random sizes so we can test memory usage accounting
336  const int height{int(InsecureRand32() >> 1)};
337  Coin old_coin;
338 
339  // 2/20 times create a new coinbase
340  if (randiter % 20 < 2 || coinbase_coins.size() < 10) {
341  // 1/10 of those times create a duplicate coinbase
342  if (InsecureRandRange(10) == 0 && coinbase_coins.size()) {
343  auto utxod = FindRandomFrom(coinbase_coins);
344  // Reuse the exact same coinbase
345  tx = CMutableTransaction{std::get<0>(utxod->second)};
346  // shouldn't be available for reconnection if it's been duplicated
347  disconnected_coins.erase(utxod->first);
348 
349  duplicate_coins.insert(utxod->first);
350  }
351  else {
352  coinbase_coins.insert(COutPoint(tx.GetHash(), 0));
353  }
354  assert(CTransaction(tx).IsCoinBase());
355  }
356 
357  // 17/20 times reconnect previous or add a regular tx
358  else {
359 
360  COutPoint prevout;
361  // 1/20 times reconnect a previously disconnected tx
362  if (randiter % 20 == 2 && disconnected_coins.size()) {
363  auto utxod = FindRandomFrom(disconnected_coins);
364  tx = CMutableTransaction{std::get<0>(utxod->second)};
365  prevout = tx.vin[0].prevout;
366  if (!CTransaction(tx).IsCoinBase() && !utxoset.count(prevout)) {
367  disconnected_coins.erase(utxod->first);
368  continue;
369  }
370 
371  // If this tx is already IN the UTXO, then it must be a coinbase, and it must be a duplicate
372  if (utxoset.count(utxod->first)) {
373  assert(CTransaction(tx).IsCoinBase());
374  assert(duplicate_coins.count(utxod->first));
375  }
376  disconnected_coins.erase(utxod->first);
377  }
378 
379  // 16/20 times create a regular tx
380  else {
381  auto utxod = FindRandomFrom(utxoset);
382  prevout = utxod->first;
383 
384  // Construct the tx to spend the coins of prevouthash
385  tx.vin[0].prevout = prevout;
386  assert(!CTransaction(tx).IsCoinBase());
387  }
388  // In this simple test coins only have two states, spent or unspent, save the unspent state to restore
389  old_coin = result[prevout];
390  // Update the expected result of prevouthash to know these coins are spent
391  result[prevout].Clear();
392 
393  utxoset.erase(prevout);
394 
395  // The test is designed to ensure spending a duplicate coinbase will work properly
396  // if that ever happens and not resurrect the previously overwritten coinbase
397  if (duplicate_coins.count(prevout)) {
398  spent_a_duplicate_coinbase = true;
399  }
400 
401  }
402  // Update the expected result to know about the new output coins
403  assert(tx.vout.size() == 1);
404  const COutPoint outpoint(tx.GetHash(), 0);
405  result[outpoint] = Coin{tx.vout[0], height, CTransaction{tx}.IsCoinBase()};
406 
407  // Call UpdateCoins on the top cache
408  CTxUndo undo;
409  UpdateCoins(CTransaction{tx}, *(stack.back()), undo, height);
410 
411  // Update the utxo set for future spends
412  utxoset.insert(outpoint);
413 
414  // Track this tx and undo info to use later
415  utxoData.emplace(outpoint, std::make_tuple(tx,undo,old_coin));
416  } else if (utxoset.size()) {
417  //1/20 times undo a previous transaction
418  auto utxod = FindRandomFrom(utxoset);
419 
420  CTransaction &tx = std::get<0>(utxod->second);
421  CTxUndo &undo = std::get<1>(utxod->second);
422  Coin &orig_coin = std::get<2>(utxod->second);
423 
424  // Update the expected result
425  // Remove new outputs
426  result[utxod->first].Clear();
427  // If not coinbase restore prevout
428  if (!tx.IsCoinBase()) {
429  result[tx.vin[0].prevout] = orig_coin;
430  }
431 
432  // Disconnect the tx from the current UTXO
433  // See code in DisconnectBlock
434  // remove outputs
435  BOOST_CHECK(stack.back()->SpendCoin(utxod->first));
436  // restore inputs
437  if (!tx.IsCoinBase()) {
438  const COutPoint &out = tx.vin[0].prevout;
439  Coin coin = undo.vprevout[0];
440  ApplyTxInUndo(std::move(coin), *(stack.back()), out);
441  }
442  // Store as a candidate for reconnection
443  disconnected_coins.insert(utxod->first);
444 
445  // Update the utxoset
446  utxoset.erase(utxod->first);
447  if (!tx.IsCoinBase())
448  utxoset.insert(tx.vin[0].prevout);
449  }
450 
451  // Once every 1000 iterations and at the end, verify the full cache.
452  if (InsecureRandRange(1000) == 1 || i == NUM_SIMULATION_ITERATIONS - 1) {
453  for (const auto& entry : result) {
454  bool have = stack.back()->HaveCoin(entry.first);
455  const Coin& coin = stack.back()->AccessCoin(entry.first);
456  BOOST_CHECK(have == !coin.IsSpent());
457  BOOST_CHECK(coin == entry.second);
458  }
459  }
460 
461  // One every 10 iterations, remove a random entry from the cache
462  if (utxoset.size() > 1 && InsecureRandRange(30) == 0) {
463  stack[InsecureRand32() % stack.size()]->Uncache(FindRandomFrom(utxoset)->first);
464  }
465  if (disconnected_coins.size() > 1 && InsecureRandRange(30) == 0) {
466  stack[InsecureRand32() % stack.size()]->Uncache(FindRandomFrom(disconnected_coins)->first);
467  }
468  if (duplicate_coins.size() > 1 && InsecureRandRange(30) == 0) {
469  stack[InsecureRand32() % stack.size()]->Uncache(FindRandomFrom(duplicate_coins)->first);
470  }
471 
472  if (InsecureRandRange(100) == 0) {
473  // Every 100 iterations, flush an intermediate cache
474  if (stack.size() > 1 && InsecureRandBool() == 0) {
475  unsigned int flushIndex = InsecureRandRange(stack.size() - 1);
476  BOOST_CHECK(stack[flushIndex]->Flush());
477  }
478  }
479  if (InsecureRandRange(100) == 0) {
480  // Every 100 iterations, change the cache stack.
481  if (stack.size() > 0 && InsecureRandBool() == 0) {
482  BOOST_CHECK(stack.back()->Flush());
483  stack.pop_back();
484  }
485  if (stack.size() == 0 || (stack.size() < 4 && InsecureRandBool())) {
486  CCoinsView* tip = &base;
487  if (stack.size() > 0) {
488  tip = stack.back().get();
489  }
490  stack.push_back(std::make_unique<CCoinsViewCacheTest>(tip));
491  }
492  }
493  }
494 
495  // Verify coverage.
496  BOOST_CHECK(spent_a_duplicate_coinbase);
497 
499 }
500 
501 BOOST_AUTO_TEST_CASE(ccoins_serialization)
502 {
503  // Good example
504  DataStream ss1{ParseHex("97f23c835800816115944e077fe7c803cfa57f29b36bf87c1d35")};
505  Coin cc1;
506  ss1 >> cc1;
507  BOOST_CHECK_EQUAL(cc1.fCoinBase, false);
508  BOOST_CHECK_EQUAL(cc1.nHeight, 203998U);
509  BOOST_CHECK_EQUAL(cc1.out.nValue, CAmount{60000000000});
510  BOOST_CHECK_EQUAL(HexStr(cc1.out.scriptPubKey), HexStr(GetScriptForDestination(PKHash(uint160(ParseHex("816115944e077fe7c803cfa57f29b36bf87c1d35"))))));
511 
512  // Good example
513  DataStream ss2{ParseHex("8ddf77bbd123008c988f1a4a4de2161e0f50aac7f17e7f9555caa4")};
514  Coin cc2;
515  ss2 >> cc2;
516  BOOST_CHECK_EQUAL(cc2.fCoinBase, true);
517  BOOST_CHECK_EQUAL(cc2.nHeight, 120891U);
518  BOOST_CHECK_EQUAL(cc2.out.nValue, 110397);
519  BOOST_CHECK_EQUAL(HexStr(cc2.out.scriptPubKey), HexStr(GetScriptForDestination(PKHash(uint160(ParseHex("8c988f1a4a4de2161e0f50aac7f17e7f9555caa4"))))));
520 
521  // Smallest possible example
522  DataStream ss3{ParseHex("000006")};
523  Coin cc3;
524  ss3 >> cc3;
525  BOOST_CHECK_EQUAL(cc3.fCoinBase, false);
526  BOOST_CHECK_EQUAL(cc3.nHeight, 0U);
527  BOOST_CHECK_EQUAL(cc3.out.nValue, 0);
529 
530  // scriptPubKey that ends beyond the end of the stream
531  DataStream ss4{ParseHex("000007")};
532  try {
533  Coin cc4;
534  ss4 >> cc4;
535  BOOST_CHECK_MESSAGE(false, "We should have thrown");
536  } catch (const std::ios_base::failure&) {
537  }
538 
539  // Very large scriptPubKey (3*10^9 bytes) past the end of the stream
540  DataStream tmp{};
541  uint64_t x = 3000000000ULL;
542  tmp << VARINT(x);
543  BOOST_CHECK_EQUAL(HexStr(tmp), "8a95c0bb00");
544  DataStream ss5{ParseHex("00008a95c0bb00")};
545  try {
546  Coin cc5;
547  ss5 >> cc5;
548  BOOST_CHECK_MESSAGE(false, "We should have thrown");
549  } catch (const std::ios_base::failure&) {
550  }
551 }
552 
553 const static COutPoint OUTPOINT;
554 const static CAmount SPENT = -1;
555 const static CAmount ABSENT = -2;
556 const static CAmount FAIL = -3;
557 const static CAmount VALUE1 = 100;
558 const static CAmount VALUE2 = 200;
559 const static CAmount VALUE3 = 300;
560 const static char DIRTY = CCoinsCacheEntry::DIRTY;
561 const static char FRESH = CCoinsCacheEntry::FRESH;
562 const static char NO_ENTRY = -1;
563 
564 const static auto FLAGS = {char(0), FRESH, DIRTY, char(DIRTY | FRESH)};
565 const static auto CLEAN_FLAGS = {char(0), FRESH};
566 const static auto ABSENT_FLAGS = {NO_ENTRY};
567 
568 static void SetCoinsValue(CAmount value, Coin& coin)
569 {
570  assert(value != ABSENT);
571  coin.Clear();
572  assert(coin.IsSpent());
573  if (value != SPENT) {
574  coin.out.nValue = value;
575  coin.nHeight = 1;
576  assert(!coin.IsSpent());
577  }
578 }
579 
580 static size_t InsertCoinsMapEntry(CCoinsMap& map, CAmount value, char flags)
581 {
582  if (value == ABSENT) {
583  assert(flags == NO_ENTRY);
584  return 0;
585  }
586  assert(flags != NO_ENTRY);
587  CCoinsCacheEntry entry;
588  entry.flags = flags;
589  SetCoinsValue(value, entry.coin);
590  auto inserted = map.emplace(OUTPOINT, std::move(entry));
591  assert(inserted.second);
592  return inserted.first->second.coin.DynamicMemoryUsage();
593 }
594 
595 void GetCoinsMapEntry(const CCoinsMap& map, CAmount& value, char& flags, const COutPoint& outp = OUTPOINT)
596 {
597  auto it = map.find(outp);
598  if (it == map.end()) {
599  value = ABSENT;
600  flags = NO_ENTRY;
601  } else {
602  if (it->second.coin.IsSpent()) {
603  value = SPENT;
604  } else {
605  value = it->second.coin.out.nValue;
606  }
607  flags = it->second.flags;
608  assert(flags != NO_ENTRY);
609  }
610 }
611 
612 void WriteCoinsViewEntry(CCoinsView& view, CAmount value, char flags)
613 {
614  CCoinsMap map;
615  InsertCoinsMapEntry(map, value, flags);
616  BOOST_CHECK(view.BatchWrite(map, {}));
617 }
618 
620 {
621 public:
622  SingleEntryCacheTest(CAmount base_value, CAmount cache_value, char cache_flags)
623  {
624  WriteCoinsViewEntry(base, base_value, base_value == ABSENT ? NO_ENTRY : DIRTY);
625  cache.usage() += InsertCoinsMapEntry(cache.map(), cache_value, cache_flags);
626  }
627 
629  CCoinsViewCacheTest base{&root};
630  CCoinsViewCacheTest cache{&base};
631 };
632 
633 static void CheckAccessCoin(CAmount base_value, CAmount cache_value, CAmount expected_value, char cache_flags, char expected_flags)
634 {
635  SingleEntryCacheTest test(base_value, cache_value, cache_flags);
636  test.cache.AccessCoin(OUTPOINT);
637  test.cache.SelfTest();
638 
639  CAmount result_value;
640  char result_flags;
641  GetCoinsMapEntry(test.cache.map(), result_value, result_flags);
642  BOOST_CHECK_EQUAL(result_value, expected_value);
643  BOOST_CHECK_EQUAL(result_flags, expected_flags);
644 }
645 
646 BOOST_AUTO_TEST_CASE(ccoins_access)
647 {
648  /* Check AccessCoin behavior, requesting a coin from a cache view layered on
649  * top of a base view, and checking the resulting entry in the cache after
650  * the access.
651  *
652  * Base Cache Result Cache Result
653  * Value Value Value Flags Flags
654  */
656  CheckAccessCoin(ABSENT, SPENT , SPENT , 0 , 0 );
660  CheckAccessCoin(ABSENT, VALUE2, VALUE2, 0 , 0 );
665  CheckAccessCoin(SPENT , SPENT , SPENT , 0 , 0 );
669  CheckAccessCoin(SPENT , VALUE2, VALUE2, 0 , 0 );
674  CheckAccessCoin(VALUE1, SPENT , SPENT , 0 , 0 );
678  CheckAccessCoin(VALUE1, VALUE2, VALUE2, 0 , 0 );
682 }
683 
684 static void CheckSpendCoins(CAmount base_value, CAmount cache_value, CAmount expected_value, char cache_flags, char expected_flags)
685 {
686  SingleEntryCacheTest test(base_value, cache_value, cache_flags);
687  test.cache.SpendCoin(OUTPOINT);
688  test.cache.SelfTest();
689 
690  CAmount result_value;
691  char result_flags;
692  GetCoinsMapEntry(test.cache.map(), result_value, result_flags);
693  BOOST_CHECK_EQUAL(result_value, expected_value);
694  BOOST_CHECK_EQUAL(result_flags, expected_flags);
695 };
696 
697 BOOST_AUTO_TEST_CASE(ccoins_spend)
698 {
699  /* Check SpendCoin behavior, requesting a coin from a cache view layered on
700  * top of a base view, spending, and then checking
701  * the resulting entry in the cache after the modification.
702  *
703  * Base Cache Result Cache Result
704  * Value Value Value Flags Flags
705  */
716  CheckSpendCoins(SPENT , SPENT , SPENT , 0 , DIRTY );
733 }
734 
735 static void CheckAddCoinBase(CAmount base_value, CAmount cache_value, CAmount modify_value, CAmount expected_value, char cache_flags, char expected_flags, bool coinbase)
736 {
737  SingleEntryCacheTest test(base_value, cache_value, cache_flags);
738 
739  CAmount result_value;
740  char result_flags;
741  try {
742  CTxOut output;
743  output.nValue = modify_value;
744  test.cache.AddCoin(OUTPOINT, Coin(std::move(output), 1, coinbase), coinbase);
745  test.cache.SelfTest();
746  GetCoinsMapEntry(test.cache.map(), result_value, result_flags);
747  } catch (std::logic_error&) {
748  result_value = FAIL;
749  result_flags = NO_ENTRY;
750  }
751 
752  BOOST_CHECK_EQUAL(result_value, expected_value);
753  BOOST_CHECK_EQUAL(result_flags, expected_flags);
754 }
755 
756 // Simple wrapper for CheckAddCoinBase function above that loops through
757 // different possible base_values, making sure each one gives the same results.
758 // This wrapper lets the coins_add test below be shorter and less repetitive,
759 // while still verifying that the CoinsViewCache::AddCoin implementation
760 // ignores base values.
761 template <typename... Args>
762 static void CheckAddCoin(Args&&... args)
763 {
764  for (const CAmount base_value : {ABSENT, SPENT, VALUE1})
765  CheckAddCoinBase(base_value, std::forward<Args>(args)...);
766 }
767 
769 {
770  /* Check AddCoin behavior, requesting a new coin from a cache view,
771  * writing a modification to the coin, and then checking the resulting
772  * entry in the cache after the modification. Verify behavior with the
773  * AddCoin possible_overwrite argument set to false, and to true.
774  *
775  * Cache Write Result Cache Result possible_overwrite
776  * Value Value Value Flags Flags
777  */
780  CheckAddCoin(SPENT , VALUE3, VALUE3, 0 , DIRTY|FRESH, false);
781  CheckAddCoin(SPENT , VALUE3, VALUE3, 0 , DIRTY , true );
784  CheckAddCoin(SPENT , VALUE3, VALUE3, DIRTY , DIRTY , false);
785  CheckAddCoin(SPENT , VALUE3, VALUE3, DIRTY , DIRTY , true );
788  CheckAddCoin(VALUE2, VALUE3, FAIL , 0 , NO_ENTRY , false);
789  CheckAddCoin(VALUE2, VALUE3, VALUE3, 0 , DIRTY , true );
790  CheckAddCoin(VALUE2, VALUE3, FAIL , FRESH , NO_ENTRY , false);
792  CheckAddCoin(VALUE2, VALUE3, FAIL , DIRTY , NO_ENTRY , false);
793  CheckAddCoin(VALUE2, VALUE3, VALUE3, DIRTY , DIRTY , true );
796 }
797 
798 void CheckWriteCoins(CAmount parent_value, CAmount child_value, CAmount expected_value, char parent_flags, char child_flags, char expected_flags)
799 {
800  SingleEntryCacheTest test(ABSENT, parent_value, parent_flags);
801 
802  CAmount result_value;
803  char result_flags;
804  try {
805  WriteCoinsViewEntry(test.cache, child_value, child_flags);
806  test.cache.SelfTest();
807  GetCoinsMapEntry(test.cache.map(), result_value, result_flags);
808  } catch (std::logic_error&) {
809  result_value = FAIL;
810  result_flags = NO_ENTRY;
811  }
812 
813  BOOST_CHECK_EQUAL(result_value, expected_value);
814  BOOST_CHECK_EQUAL(result_flags, expected_flags);
815 }
816 
817 BOOST_AUTO_TEST_CASE(ccoins_write)
818 {
819  /* Check BatchWrite behavior, flushing one entry from a child cache to a
820  * parent cache, and checking the resulting entry in the parent cache
821  * after the write.
822  *
823  * Parent Child Result Parent Child Result
824  * Value Value Value Flags Flags Flags
825  */
831  CheckWriteCoins(SPENT , ABSENT, SPENT , 0 , NO_ENTRY , 0 );
835  CheckWriteCoins(SPENT , SPENT , SPENT , 0 , DIRTY , DIRTY );
871 
872  // The checks above omit cases where the child flags are not DIRTY, since
873  // they would be too repetitive (the parent cache is never updated in these
874  // cases). The loop below covers these cases and makes sure the parent cache
875  // is always left unchanged.
876  for (const CAmount parent_value : {ABSENT, SPENT, VALUE1})
877  for (const CAmount child_value : {ABSENT, SPENT, VALUE2})
878  for (const char parent_flags : parent_value == ABSENT ? ABSENT_FLAGS : FLAGS)
879  for (const char child_flags : child_value == ABSENT ? ABSENT_FLAGS : CLEAN_FLAGS)
880  CheckWriteCoins(parent_value, child_value, parent_value, parent_flags, child_flags, parent_flags);
881 }
882 
883 
885 {
886  Coin coin;
887  coin.out.nValue = InsecureRand32();
888  coin.nHeight = InsecureRandRange(4096);
889  coin.fCoinBase = 0;
890  return coin;
891 }
892 
893 
905  CCoinsViewCacheTest* view,
906  CCoinsViewDB& base,
907  std::vector<std::unique_ptr<CCoinsViewCacheTest>>& all_caches,
908  bool do_erasing_flush)
909 {
910  CAmount value;
911  char flags;
912  size_t cache_usage;
913 
914  auto flush_all = [&all_caches](bool erase) {
915  // Flush in reverse order to ensure that flushes happen from children up.
916  for (auto i = all_caches.rbegin(); i != all_caches.rend(); ++i) {
917  auto& cache = *i;
918  // hashBlock must be filled before flushing to disk; value is
919  // unimportant here. This is normally done during connect/disconnect block.
920  cache->SetBestBlock(InsecureRand256());
921  erase ? cache->Flush() : cache->Sync();
922  }
923  };
924 
925  uint256 txid = InsecureRand256();
926  COutPoint outp = COutPoint(txid, 0);
927  Coin coin = MakeCoin();
928  // Ensure the coins views haven't seen this coin before.
929  BOOST_CHECK(!base.HaveCoin(outp));
930  BOOST_CHECK(!view->HaveCoin(outp));
931 
932  // --- 1. Adding a random coin to the child cache
933  //
934  view->AddCoin(outp, Coin(coin), false);
935 
936  cache_usage = view->DynamicMemoryUsage();
937  // `base` shouldn't have coin (no flush yet) but `view` should have cached it.
938  BOOST_CHECK(!base.HaveCoin(outp));
939  BOOST_CHECK(view->HaveCoin(outp));
940 
941  GetCoinsMapEntry(view->map(), value, flags, outp);
942  BOOST_CHECK_EQUAL(value, coin.out.nValue);
944 
945  // --- 2. Flushing all caches (without erasing)
946  //
947  flush_all(/*erase=*/ false);
948 
949  // CoinsMap usage should be unchanged since we didn't erase anything.
950  BOOST_CHECK_EQUAL(cache_usage, view->DynamicMemoryUsage());
951 
952  // --- 3. Ensuring the entry still exists in the cache and has been written to parent
953  //
954  GetCoinsMapEntry(view->map(), value, flags, outp);
955  BOOST_CHECK_EQUAL(value, coin.out.nValue);
956  BOOST_CHECK_EQUAL(flags, 0); // Flags should have been wiped.
957 
958  // Both views should now have the coin.
959  BOOST_CHECK(base.HaveCoin(outp));
960  BOOST_CHECK(view->HaveCoin(outp));
961 
962  if (do_erasing_flush) {
963  // --- 4. Flushing the caches again (with erasing)
964  //
965  flush_all(/*erase=*/ true);
966 
967  // Memory usage should have gone down.
968  BOOST_CHECK(view->DynamicMemoryUsage() < cache_usage);
969 
970  // --- 5. Ensuring the entry is no longer in the cache
971  //
972  GetCoinsMapEntry(view->map(), value, flags, outp);
973  BOOST_CHECK_EQUAL(value, ABSENT);
975 
976  view->AccessCoin(outp);
977  GetCoinsMapEntry(view->map(), value, flags, outp);
978  BOOST_CHECK_EQUAL(value, coin.out.nValue);
980  }
981 
982  // Can't overwrite an entry without specifying that an overwrite is
983  // expected.
985  view->AddCoin(outp, Coin(coin), /*possible_overwrite=*/ false),
986  std::logic_error);
987 
988  // --- 6. Spend the coin.
989  //
990  BOOST_CHECK(view->SpendCoin(outp));
991 
992  // The coin should be in the cache, but spent and marked dirty.
993  GetCoinsMapEntry(view->map(), value, flags, outp);
994  BOOST_CHECK_EQUAL(value, SPENT);
996  BOOST_CHECK(!view->HaveCoin(outp)); // Coin should be considered spent in `view`.
997  BOOST_CHECK(base.HaveCoin(outp)); // But coin should still be unspent in `base`.
998 
999  flush_all(/*erase=*/ false);
1000 
1001  // Coin should be considered spent in both views.
1002  BOOST_CHECK(!view->HaveCoin(outp));
1003  BOOST_CHECK(!base.HaveCoin(outp));
1004 
1005  // Spent coin should not be spendable.
1006  BOOST_CHECK(!view->SpendCoin(outp));
1007 
1008  // --- Bonus check: ensure that a coin added to the base view via one cache
1009  // can be spent by another cache which has never seen it.
1010  //
1011  txid = InsecureRand256();
1012  outp = COutPoint(txid, 0);
1013  coin = MakeCoin();
1014  BOOST_CHECK(!base.HaveCoin(outp));
1015  BOOST_CHECK(!all_caches[0]->HaveCoin(outp));
1016  BOOST_CHECK(!all_caches[1]->HaveCoin(outp));
1017 
1018  all_caches[0]->AddCoin(outp, std::move(coin), false);
1019  all_caches[0]->Sync();
1020  BOOST_CHECK(base.HaveCoin(outp));
1021  BOOST_CHECK(all_caches[0]->HaveCoin(outp));
1022  BOOST_CHECK(!all_caches[1]->HaveCoinInCache(outp));
1023 
1024  BOOST_CHECK(all_caches[1]->SpendCoin(outp));
1025  flush_all(/*erase=*/ false);
1026  BOOST_CHECK(!base.HaveCoin(outp));
1027  BOOST_CHECK(!all_caches[0]->HaveCoin(outp));
1028  BOOST_CHECK(!all_caches[1]->HaveCoin(outp));
1029 
1030  flush_all(/*erase=*/ true); // Erase all cache content.
1031 
1032  // --- Bonus check 2: ensure that a FRESH, spent coin is deleted by Sync()
1033  //
1034  txid = InsecureRand256();
1035  outp = COutPoint(txid, 0);
1036  coin = MakeCoin();
1037  CAmount coin_val = coin.out.nValue;
1038  BOOST_CHECK(!base.HaveCoin(outp));
1039  BOOST_CHECK(!all_caches[0]->HaveCoin(outp));
1040  BOOST_CHECK(!all_caches[1]->HaveCoin(outp));
1041 
1042  // Add and spend from same cache without flushing.
1043  all_caches[0]->AddCoin(outp, std::move(coin), false);
1044 
1045  // Coin should be FRESH in the cache.
1046  GetCoinsMapEntry(all_caches[0]->map(), value, flags, outp);
1047  BOOST_CHECK_EQUAL(value, coin_val);
1049 
1050  // Base shouldn't have seen coin.
1051  BOOST_CHECK(!base.HaveCoin(outp));
1052 
1053  BOOST_CHECK(all_caches[0]->SpendCoin(outp));
1054  all_caches[0]->Sync();
1055 
1056  // Ensure there is no sign of the coin after spend/flush.
1057  GetCoinsMapEntry(all_caches[0]->map(), value, flags, outp);
1058  BOOST_CHECK_EQUAL(value, ABSENT);
1060  BOOST_CHECK(!all_caches[0]->HaveCoinInCache(outp));
1061  BOOST_CHECK(!base.HaveCoin(outp));
1062 }
1063 
1064 BOOST_AUTO_TEST_CASE(ccoins_flush_behavior)
1065 {
1066  // Create two in-memory caches atop a leveldb view.
1067  CCoinsViewDB base{"test", /*nCacheSize=*/ 1 << 23, /*fMemory=*/ true, /*fWipe=*/ false};
1068  std::vector<std::unique_ptr<CCoinsViewCacheTest>> caches;
1069  caches.push_back(std::make_unique<CCoinsViewCacheTest>(&base));
1070  caches.push_back(std::make_unique<CCoinsViewCacheTest>(caches.back().get()));
1071 
1072  for (const auto& view : caches) {
1073  TestFlushBehavior(view.get(), base, caches, /*do_erasing_flush=*/false);
1074  TestFlushBehavior(view.get(), base, caches, /*do_erasing_flush=*/true);
1075  }
1076 }
1077 
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
int ret
int flags
Definition: bitcoin-tx.cpp:525
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:213
unsigned int GetCacheSize() const
Calculate the size of the cache (in number of transaction outputs)
Definition: coins.cpp:293
size_t cachedCoinsUsage
Definition: coins.h:223
size_t DynamicMemoryUsage() const
Calculate the size of the cache (in bytes)
Definition: coins.cpp:37
CCoinsMap cacheCoins
Definition: coins.h:220
CCoinsView backed by the coin database (chainstate/)
Definition: txdb.h:50
bool HaveCoin(const COutPoint &outpoint) const override
Just check whether a given outpoint is unspent.
Definition: txdb.cpp:95
Abstract view on the open txout dataset.
Definition: coins.h:157
virtual bool GetCoin(const COutPoint &outpoint, Coin &coin) const
Retrieve the Coin (unspent transaction output) for a given outpoint.
Definition: coins.cpp:13
virtual bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock, bool erase=true)
Do a bulk modification (multiple Coin changes + BestBlock change).
Definition: coins.cpp:16
virtual uint256 GetBestBlock() const
Retrieve the block hash whose state this CCoinsView currently represents.
Definition: coins.cpp:14
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:36
bool IsUnspendable() const
Returns whether the script is guaranteed to fail at execution, regardless of the initial stack.
Definition: script.h:549
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:295
bool IsCoinBase() const
Definition: transaction.h:350
const std::vector< CTxIn > vin
Definition: transaction.h:305
An output of a transaction.
Definition: transaction.h:158
CScript scriptPubKey
Definition: transaction.h:161
CAmount nValue
Definition: transaction.h:160
Undo information for a CTransaction.
Definition: undo.h:54
std::vector< Coin > vprevout
Definition: undo.h:57
A UTXO entry.
Definition: coins.h:31
void Clear()
Definition: coins.h:46
CTxOut out
unspent transaction output
Definition: coins.h:34
bool IsSpent() const
Either this coin never existed (see e.g.
Definition: coins.h:79
uint32_t nHeight
at which height this containing transaction was included in the active block chain
Definition: coins.h:40
unsigned int fCoinBase
whether containing transaction was a coinbase
Definition: coins.h:37
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:186
CCoinsViewCacheTest cache
CCoinsViewCacheTest base
SingleEntryCacheTest(CAmount base_value, CAmount cache_value, char cache_flags)
CCoinsView root
constexpr bool IsNull() const
Definition: uint256.h:41
size_type size() const
Definition: prevector.h:284
void assign(size_type n, const T &val)
Definition: prevector.h:220
160-bit opaque blob.
Definition: uint256.h:94
256-bit opaque blob.
Definition: uint256.h:105
const Coin & AccessByTxid(const CCoinsViewCache &view, const uint256 &txid)
Utility function to find any unspent output with a given txid.
Definition: coins.cpp:320
std::unordered_map< COutPoint, CCoinsCacheEntry, SaltedOutpointHasher > CCoinsMap
Definition: coins.h:134
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 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 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...
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
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)
static size_t InsertCoinsMapEntry(CCoinsMap &map, CAmount value, char 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()
unsigned int nHeight
static size_t DynamicUsage(const int8_t &v)
Dynamic memory usage for built-in types is zero.
Definition: memusage.h:28
bool operator==(const CNetAddr &a, const CNetAddr &b)
Definition: netaddress.cpp:630
ArgsManager args
#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
bool g_mock_deterministic_tests
Flag to make GetRand in random.h return the same number.
Definition: random.cpp:585
@ OP_RETURN
Definition: script.h:107
#define VARINT(obj)
Definition: serialize.h:436
@ ZEROS
Seed with a compile time constant of zeros.
static uint64_t InsecureRandRange(uint64_t range)
Definition: setup_common.h:77
static uint256 InsecureRand256()
Definition: setup_common.h:75
static void SeedInsecureRand(SeedRand seed=SeedRand::SEED)
Definition: setup_common.h:65
static uint64_t InsecureRandBits(int bits)
Definition: setup_common.h:76
static uint32_t InsecureRand32()
Definition: setup_common.h:74
static bool InsecureRandBool()
Definition: setup_common.h:78
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
Definition: standard.cpp:334
Basic testing setup.
Definition: setup_common.h:85
A Coin in one level of the coins database caching hierarchy.
Definition: coins.h:104
unsigned char flags
Definition: coins.h:106
Coin coin
Definition: coins.h:105
@ 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:126
@ DIRTY
DIRTY means the CCoinsCacheEntry is potentially different from the version in the parent cache.
Definition: coins.h:116
A mutable version of CTransaction.
Definition: transaction.h:380
uint256 GetHash() const
Compute the hash of this CMutableTransaction.
Definition: transaction.cpp:68
std::vector< CTxOut > vout
Definition: transaction.h:382
std::vector< CTxIn > vin
Definition: transaction.h:381
static int count
Definition: tests.c:34
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
std::vector< Byte > ParseHex(std::string_view str)
Parse the hex string into bytes (uint8_t or std::byte).
assert(!tx.IsCoinBase())