Bitcoin Core  22.99.0
P2P Digital Currency
validation_flush_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2019-2020 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 <sync.h>
7 #include <txmempool.h>
8 #include <validation.h>
9 
10 #include <boost/test/unit_test.hpp>
11 
12 BOOST_FIXTURE_TEST_SUITE(validation_flush_tests, BasicTestingSetup)
13 
14 BOOST_AUTO_TEST_CASE(getcoinscachesizestate)
20 {
21  CTxMemPool mempool;
22  BlockManager blockman{};
23  CChainState chainstate{&mempool, blockman};
24  chainstate.InitCoinsDB(/*cache_size_bytes*/ 1 << 10, /*in_memory*/ true, /*should_wipe*/ false);
25  WITH_LOCK(::cs_main, chainstate.InitCoinsCache(1 << 10));
26 
27  constexpr bool is_64_bit = sizeof(void*) == 8;
28 
29  LOCK(::cs_main);
30  auto& view = chainstate.CoinsTip();
31 
33  auto add_coin = [](CCoinsViewCache& coins_view) -> COutPoint {
34  Coin newcoin;
35  uint256 txid = InsecureRand256();
36  COutPoint outp{txid, 0};
37  newcoin.nHeight = 1;
38  newcoin.out.nValue = InsecureRand32();
39  newcoin.out.scriptPubKey.assign((uint32_t)56, 1);
40  coins_view.AddCoin(outp, std::move(newcoin), false);
41 
42  return outp;
43  };
44 
45  // The number of bytes consumed by coin's heap data, i.e. CScript
46  // (prevector<28, unsigned char>) when assigned 56 bytes of data per above.
47  //
48  // See also: Coin::DynamicMemoryUsage().
49  constexpr unsigned int COIN_SIZE = is_64_bit ? 80 : 64;
50 
51  auto print_view_mem_usage = [](CCoinsViewCache& view) {
52  BOOST_TEST_MESSAGE("CCoinsViewCache memory usage: " << view.DynamicMemoryUsage());
53  };
54 
55  constexpr size_t MAX_COINS_CACHE_BYTES = 1024;
56 
57  // Without any coins in the cache, we shouldn't need to flush.
59  chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes*/ 0),
61 
62  // If the initial memory allocations of cacheCoins don't match these common
63  // cases, we can't really continue to make assertions about memory usage.
64  // End the test early.
65  if (view.DynamicMemoryUsage() != 32 && view.DynamicMemoryUsage() != 16) {
66  // Add a bunch of coins to see that we at least flip over to CRITICAL.
67 
68  for (int i{0}; i < 1000; ++i) {
69  COutPoint res = add_coin(view);
70  BOOST_CHECK_EQUAL(view.AccessCoin(res).DynamicMemoryUsage(), COIN_SIZE);
71  }
72 
74  chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes*/ 0),
76 
77  BOOST_TEST_MESSAGE("Exiting cache flush tests early due to unsupported arch");
78  return;
79  }
80 
81  print_view_mem_usage(view);
82  BOOST_CHECK_EQUAL(view.DynamicMemoryUsage(), is_64_bit ? 32U : 16U);
83 
84  // We should be able to add COINS_UNTIL_CRITICAL coins to the cache before going CRITICAL.
85  // This is contingent not only on the dynamic memory usage of the Coins
86  // that we're adding (COIN_SIZE bytes per), but also on how much memory the
87  // cacheCoins (unordered_map) preallocates.
88  constexpr int COINS_UNTIL_CRITICAL{3};
89 
90  for (int i{0}; i < COINS_UNTIL_CRITICAL; ++i) {
91  COutPoint res = add_coin(view);
92  print_view_mem_usage(view);
93  BOOST_CHECK_EQUAL(view.AccessCoin(res).DynamicMemoryUsage(), COIN_SIZE);
95  chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes*/ 0),
97  }
98 
99  // Adding some additional coins will push us over the edge to CRITICAL.
100  for (int i{0}; i < 4; ++i) {
101  add_coin(view);
102  print_view_mem_usage(view);
103  if (chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes*/ 0) ==
105  break;
106  }
107  }
108 
110  chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes*/ 0),
112 
113  // Passing non-zero max mempool usage should allow us more headroom.
115  chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes*/ 1 << 10),
117 
118  for (int i{0}; i < 3; ++i) {
119  add_coin(view);
120  print_view_mem_usage(view);
122  chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes*/ 1 << 10),
124  }
125 
126  // Adding another coin with the additional mempool room will put us >90%
127  // but not yet critical.
128  add_coin(view);
129  print_view_mem_usage(view);
130 
131  // Only perform these checks on 64 bit hosts; I haven't done the math for 32.
132  if (is_64_bit) {
133  float usage_percentage = (float)view.DynamicMemoryUsage() / (MAX_COINS_CACHE_BYTES + (1 << 10));
134  BOOST_TEST_MESSAGE("CoinsTip usage percentage: " << usage_percentage);
135  BOOST_CHECK(usage_percentage >= 0.9);
136  BOOST_CHECK(usage_percentage < 1);
138  chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, 1 << 10),
140  }
141 
142  // Using the default max_* values permits way more coins to be added.
143  for (int i{0}; i < 1000; ++i) {
144  add_coin(view);
146  chainstate.GetCoinsCacheSizeState(),
148  }
149 
150  // Flushing the view doesn't take us back to OK because cacheCoins has
151  // preallocated memory that doesn't get reclaimed even after flush.
152 
154  chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, 0),
156 
157  view.SetBestBlock(InsecureRand256());
158  BOOST_CHECK(view.Flush());
159  print_view_mem_usage(view);
160 
162  chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, 0),
164 }
165 
CoinsCacheSizeState::OK
@ OK
setup_common.h
CoinsCacheSizeState::CRITICAL
@ CRITICAL
The coins cache is in immediate need of a flush.
sync.h
CTxMemPool
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:478
InsecureRand256
static uint256 InsecureRand256()
Definition: setup_common.h:66
CoinsCacheSizeState::LARGE
@ LARGE
The cache is at >= 90% capacity.
add_coin
static void add_coin(const CAmount &nValue, int nInput, std::vector< OutputGroup > &set)
Definition: coin_selection.cpp:74
WITH_LOCK
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
Definition: sync.h:270
InsecureRand32
static uint32_t InsecureRand32()
Definition: setup_common.h:65
BlockManager
Maintains a tree of blocks (stored in m_block_index) which is consulted to determine where the most-w...
Definition: validation.h:387
BOOST_FIXTURE_TEST_SUITE
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:14
BOOST_AUTO_TEST_SUITE_END
BOOST_AUTO_TEST_SUITE_END()
txmempool.h
Coin::out
CTxOut out
unspent transaction output
Definition: coins.h:34
CTxOut::nValue
CAmount nValue
Definition: transaction.h:131
Coin
A UTXO entry.
Definition: coins.h:30
Coin::nHeight
uint32_t nHeight
at which height this containing transaction was included in the active block chain
Definition: coins.h:40
CTxOut::scriptPubKey
CScript scriptPubKey
Definition: transaction.h:132
BasicTestingSetup
Basic testing setup.
Definition: setup_common.h:76
uint256
256-bit opaque blob.
Definition: uint256.h:124
CChainState
CChainState stores and provides an API to update our local knowledge of the current best chain.
Definition: validation.h:556
prevector::assign
void assign(size_type n, const T &val)
Definition: prevector.h:218
cs_main
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: validation.cpp:116
CCoinsViewCache
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:213
LOCK
#define LOCK(cs)
Definition: sync.h:226
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(getcoinscachesizestate)
Test utilities for detecting when we need to flush the coins cache based on estimated memory usage.
Definition: validation_flush_tests.cpp:19
COutPoint
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:26
BOOST_CHECK
#define BOOST_CHECK(expr)
Definition: object.cpp:17
BOOST_CHECK_EQUAL
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18