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