Bitcoin Core
31.1.0
P2P Digital Currency
Toggle main menu visibility
Loading...
Searching...
No Matches
src
test
validation_flush_tests.cpp
Go to the documentation of this file.
1
// Copyright (c) 2019-present 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
>
8
#include <
test/util/common.h
>
9
#include <
test/util/setup_common.h
>
10
#include <validation.h>
11
12
#include <boost/test/unit_test.hpp>
13
14
BOOST_FIXTURE_TEST_SUITE
(validation_flush_tests,
TestingSetup
)
15
16
19
BOOST_AUTO_TEST_CASE
(getcoinscachesizestate)
20
{
21
Chainstate
& chainstate{m_node.chainman->ActiveChainstate()};
22
23
LOCK
(
::cs_main
);
24
CCoinsViewCache
& view{chainstate.
CoinsTip
()};
25
26
// Sanity: an empty cache should be ≲ 1 chunk (~ 256 KiB).
27
BOOST_CHECK_LT
(view.
DynamicMemoryUsage
() / (256 * 1024.0), 1.1);
28
29
constexpr
size_t
MAX_COINS_BYTES{8_MiB};
30
constexpr
size_t
MAX_MEMPOOL_BYTES{4_MiB};
31
constexpr
size_t
MAX_ATTEMPTS{50'000};
32
33
// Run the same growth-path twice: first with 0 head-room, then with extra head-room
34
for
(
size_t
max_mempool_size_bytes : {
size_t
{0}, MAX_MEMPOOL_BYTES}) {
35
const
int64_t full_cap{int64_t(MAX_COINS_BYTES + max_mempool_size_bytes)};
36
const
int64_t large_cap{
LargeCoinsCacheThreshold
(full_cap)};
37
38
// OK → LARGE
39
auto
state{chainstate.GetCoinsCacheSizeState(MAX_COINS_BYTES, max_mempool_size_bytes)};
40
for
(
size_t
i{0}; i < MAX_ATTEMPTS && int64_t(view.
DynamicMemoryUsage
()) <= large_cap; ++i) {
41
BOOST_CHECK_EQUAL
(state,
CoinsCacheSizeState::OK
);
42
AddTestCoin
(m_rng, view);
43
state = chainstate.GetCoinsCacheSizeState(MAX_COINS_BYTES, max_mempool_size_bytes);
44
}
45
46
// LARGE → CRITICAL
47
for
(
size_t
i{0}; i < MAX_ATTEMPTS && int64_t(view.
DynamicMemoryUsage
()) <= full_cap; ++i) {
48
BOOST_CHECK_EQUAL
(state,
CoinsCacheSizeState::LARGE
);
49
AddTestCoin
(m_rng, view);
50
state = chainstate.GetCoinsCacheSizeState(MAX_COINS_BYTES, max_mempool_size_bytes);
51
}
52
BOOST_CHECK_EQUAL
(state,
CoinsCacheSizeState::CRITICAL
);
53
}
54
55
// Default thresholds (no explicit limits) permit many more coins.
56
for
(
int
i{0}; i < 1'000; ++i) {
57
AddTestCoin
(m_rng, view);
58
BOOST_CHECK_EQUAL
(chainstate.GetCoinsCacheSizeState(),
CoinsCacheSizeState::OK
);
59
}
60
61
// CRITICAL → OK via Flush
62
BOOST_CHECK_EQUAL
(chainstate.GetCoinsCacheSizeState(MAX_COINS_BYTES,
/*max_mempool_size_bytes=*/
0),
CoinsCacheSizeState::CRITICAL
);
63
view.
SetBestBlock
(m_rng.rand256());
64
view.
Flush
();
65
BOOST_CHECK_EQUAL
(chainstate.GetCoinsCacheSizeState(MAX_COINS_BYTES,
/*max_mempool_size_bytes=*/
0),
CoinsCacheSizeState::OK
);
66
}
67
68
BOOST_AUTO_TEST_SUITE_END
()
BOOST_CHECK_LT
BOOST_CHECK_LT(ZeroL, OneL)
CCoinsViewCache
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition
coins.h:368
CCoinsViewCache::Flush
void Flush(bool reallocate_cache=true)
Push the modifications applied to this cache to its base and wipe local state.
Definition
coins.cpp:279
CCoinsViewCache::SetBestBlock
void SetBestBlock(const uint256 &hashBlock)
Definition
coins.cpp:204
CCoinsViewCache::DynamicMemoryUsage
size_t DynamicMemoryUsage() const
Calculate the size of the cache (in bytes).
Definition
coins.cpp:59
Chainstate
Chainstate stores and provides an API to update our local knowledge of the current best chain.
Definition
validation.h:551
Chainstate::CoinsTip
CCoinsViewCache & CoinsTip() EXCLUSIVE_LOCKS_REQUIRED(
Definition
validation.h:686
cs_main
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition
cs_main.cpp:8
BOOST_FIXTURE_TEST_SUITE
BOOST_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_SUITE_END
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(ipc_tests)
Definition
ipc_tests.cpp:13
BOOST_CHECK_EQUAL
#define BOOST_CHECK_EQUAL(v1, v2)
Definition
object.cpp:17
setup_common.h
TestingSetup
Testing setup that configures a complete environment.
Definition
setup_common.h:121
sync.h
LOCK
#define LOCK(cs)
Definition
sync.h:258
AddTestCoin
COutPoint AddTestCoin(FastRandomContext &rng, CCoinsViewCache &coins_view)
Create a Coin with DynamicMemoryUsage of 80 bytes and add it to the given view.
Definition
coins.cpp:16
coins.h
common.h
random.h
LargeCoinsCacheThreshold
constexpr int64_t LargeCoinsCacheThreshold(int64_t total_space) noexcept
Definition
validation.h:518
CoinsCacheSizeState::LARGE
@ LARGE
The cache is at >= 90% capacity.
Definition
validation.h:514
CoinsCacheSizeState::CRITICAL
@ CRITICAL
The coins cache is in immediate need of a flush.
Definition
validation.h:512
CoinsCacheSizeState::OK
@ OK
Definition
validation.h:515
Generated on
for Bitcoin Core by
1.17.0