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>
6 #include <test/util/setup_common.h>
7 #include <txmempool.h>
8 #include <validation.h>
9 
10 #include <boost/test/unit_test.hpp>
11 
BOOST_FIXTURE_TEST_SUITE(validation_flush_tests,BasicTestingSetup)12 BOOST_FIXTURE_TEST_SUITE(validation_flush_tests, BasicTestingSetup)
13 
14 //! Test utilities for detecting when we need to flush the coins cache based
15 //! on estimated memory usage.
16 //!
17 //! @sa CChainState::GetCoinsCacheSizeState()
18 //!
19 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 
32     //! Create and add a Coin with DynamicMemoryUsage of 80 bytes to the given view.
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.
58     BOOST_CHECK_EQUAL(
59         chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes*/ 0),
60         CoinsCacheSizeState::OK);
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 
73         BOOST_CHECK_EQUAL(
74             chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes*/ 0),
75             CoinsCacheSizeState::CRITICAL);
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);
94         BOOST_CHECK_EQUAL(
95             chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes*/ 0),
96             CoinsCacheSizeState::OK);
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) ==
104             CoinsCacheSizeState::CRITICAL) {
105             break;
106         }
107     }
108 
109     BOOST_CHECK_EQUAL(
110         chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes*/ 0),
111         CoinsCacheSizeState::CRITICAL);
112 
113     // Passing non-zero max mempool usage should allow us more headroom.
114     BOOST_CHECK_EQUAL(
115         chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes*/ 1 << 10),
116         CoinsCacheSizeState::OK);
117 
118     for (int i{0}; i < 3; ++i) {
119         add_coin(view);
120         print_view_mem_usage(view);
121         BOOST_CHECK_EQUAL(
122             chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes*/ 1 << 10),
123             CoinsCacheSizeState::OK);
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);
137         BOOST_CHECK_EQUAL(
138             chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, 1 << 10),
139             CoinsCacheSizeState::LARGE);
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);
145         BOOST_CHECK_EQUAL(
146             chainstate.GetCoinsCacheSizeState(),
147             CoinsCacheSizeState::OK);
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 
153     BOOST_CHECK_EQUAL(
154         chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, 0),
155         CoinsCacheSizeState::CRITICAL);
156 
157     view.SetBestBlock(InsecureRand256());
158     BOOST_CHECK(view.Flush());
159     print_view_mem_usage(view);
160 
161     BOOST_CHECK_EQUAL(
162         chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, 0),
163         CoinsCacheSizeState::CRITICAL);
164 }
165 
166 BOOST_AUTO_TEST_SUITE_END()
167