Bitcoin Core  24.99.0
P2P Digital Currency
validation_flush_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2019-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 <sync.h>
6 #include <test/util/coins.h>
7 #include <test/util/random.h>
9 #include <validation.h>
10 
11 #include <boost/test/unit_test.hpp>
12 
13 BOOST_FIXTURE_TEST_SUITE(validation_flush_tests, TestingSetup)
14 
15 BOOST_AUTO_TEST_CASE(getcoinscachesizestate)
21 {
22  Chainstate& chainstate{m_node.chainman->ActiveChainstate()};
23 
24  constexpr bool is_64_bit = sizeof(void*) == 8;
25 
26  LOCK(::cs_main);
27  auto& view = chainstate.CoinsTip();
28 
29  // The number of bytes consumed by coin's heap data, i.e. CScript
30  // (prevector<28, unsigned char>) when assigned 56 bytes of data per above.
31  //
32  // See also: Coin::DynamicMemoryUsage().
33  constexpr unsigned int COIN_SIZE = is_64_bit ? 80 : 64;
34 
35  auto print_view_mem_usage = [](CCoinsViewCache& view) {
36  BOOST_TEST_MESSAGE("CCoinsViewCache memory usage: " << view.DynamicMemoryUsage());
37  };
38 
39  constexpr size_t MAX_COINS_CACHE_BYTES = 1024;
40 
41  // Without any coins in the cache, we shouldn't need to flush.
43  chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes=*/0),
45 
46  // If the initial memory allocations of cacheCoins don't match these common
47  // cases, we can't really continue to make assertions about memory usage.
48  // End the test early.
49  if (view.DynamicMemoryUsage() != 32 && view.DynamicMemoryUsage() != 16) {
50  // Add a bunch of coins to see that we at least flip over to CRITICAL.
51 
52  for (int i{0}; i < 1000; ++i) {
53  const COutPoint res = AddTestCoin(view);
54  BOOST_CHECK_EQUAL(view.AccessCoin(res).DynamicMemoryUsage(), COIN_SIZE);
55  }
56 
58  chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes=*/0),
60 
61  BOOST_TEST_MESSAGE("Exiting cache flush tests early due to unsupported arch");
62  return;
63  }
64 
65  print_view_mem_usage(view);
66  BOOST_CHECK_EQUAL(view.DynamicMemoryUsage(), is_64_bit ? 32U : 16U);
67 
68  // We should be able to add COINS_UNTIL_CRITICAL coins to the cache before going CRITICAL.
69  // This is contingent not only on the dynamic memory usage of the Coins
70  // that we're adding (COIN_SIZE bytes per), but also on how much memory the
71  // cacheCoins (unordered_map) preallocates.
72  constexpr int COINS_UNTIL_CRITICAL{3};
73 
74  for (int i{0}; i < COINS_UNTIL_CRITICAL; ++i) {
75  const COutPoint res = AddTestCoin(view);
76  print_view_mem_usage(view);
77  BOOST_CHECK_EQUAL(view.AccessCoin(res).DynamicMemoryUsage(), COIN_SIZE);
79  chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes=*/0),
81  }
82 
83  // Adding some additional coins will push us over the edge to CRITICAL.
84  for (int i{0}; i < 4; ++i) {
85  AddTestCoin(view);
86  print_view_mem_usage(view);
87  if (chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes=*/0) ==
89  break;
90  }
91  }
92 
94  chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes=*/0),
96 
97  // Passing non-zero max mempool usage should allow us more headroom.
99  chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes=*/1 << 10),
101 
102  for (int i{0}; i < 3; ++i) {
103  AddTestCoin(view);
104  print_view_mem_usage(view);
106  chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes=*/1 << 10),
108  }
109 
110  // Adding another coin with the additional mempool room will put us >90%
111  // but not yet critical.
112  AddTestCoin(view);
113  print_view_mem_usage(view);
114 
115  // Only perform these checks on 64 bit hosts; I haven't done the math for 32.
116  if (is_64_bit) {
117  float usage_percentage = (float)view.DynamicMemoryUsage() / (MAX_COINS_CACHE_BYTES + (1 << 10));
118  BOOST_TEST_MESSAGE("CoinsTip usage percentage: " << usage_percentage);
119  BOOST_CHECK(usage_percentage >= 0.9);
120  BOOST_CHECK(usage_percentage < 1);
122  chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, 1 << 10),
124  }
125 
126  // Using the default max_* values permits way more coins to be added.
127  for (int i{0}; i < 1000; ++i) {
128  AddTestCoin(view);
130  chainstate.GetCoinsCacheSizeState(),
132  }
133 
134  // Flushing the view doesn't take us back to OK because cacheCoins has
135  // preallocated memory that doesn't get reclaimed even after flush.
136 
138  chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, 0),
140 
141  view.SetBestBlock(InsecureRand256());
142  BOOST_CHECK(view.Flush());
143  print_view_mem_usage(view);
144 
146  chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, 0),
148 }
149 
node::NodeContext m_node
Definition: bitcoin-gui.cpp:37
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:213
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:36
Chainstate stores and provides an API to update our local knowledge of the current best chain.
Definition: validation.h:455
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: cs_main.cpp:8
BOOST_AUTO_TEST_SUITE_END()
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
#define BOOST_CHECK(expr)
Definition: object.cpp:17
Testing setup that configures a complete environment.
Definition: setup_common.h:102
std::unique_ptr< ChainstateManager > chainman
Definition: context.h:54
#define LOCK(cs)
Definition: sync.h:258
COutPoint AddTestCoin(CCoinsViewCache &coins_view)
Create a Coin with DynamicMemoryUsage of 80 bytes and add it to the given view.
Definition: coins.cpp:16
static uint256 InsecureRand256()
Definition: random.h:20
@ LARGE
The cache is at >= 90% capacity.
@ CRITICAL
The coins cache is in immediate need of a flush.
BOOST_AUTO_TEST_CASE(getcoinscachesizestate)
Test utilities for detecting when we need to flush the coins cache based on estimated memory usage.