mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-01-19 23:03:45 +01:00
CCoinsViewCache::BatchWrite always returns true if called from a backed cache, so just return void instead. Also return void from ::Sync and ::Flush. This allows for dropping a FatalError condition and simplifying some dead error handling code a bit. Since we now no longer exercise the "error path" when returning from `CCoinsView::BatchWrite`, make the method clear the cache instead. This should only be exercised by tests and not change production behaviour. This might slightly improve the coins_view fuzz test's ability to generate better coverage. Co-authored-by: l0rinc <pap.lorinc@gmail.com>
68 lines
2.8 KiB
C++
68 lines
2.8 KiB
C++
// Copyright (c) 2019-present The Bitcoin Core developers
|
|
// Distributed under the MIT software license, see the accompanying
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
#include <sync.h>
|
|
#include <test/util/coins.h>
|
|
#include <test/util/random.h>
|
|
#include <test/util/setup_common.h>
|
|
#include <validation.h>
|
|
|
|
#include <boost/test/unit_test.hpp>
|
|
|
|
BOOST_FIXTURE_TEST_SUITE(validation_flush_tests, TestingSetup)
|
|
|
|
//! Verify that Chainstate::GetCoinsCacheSizeState() switches from OK→LARGE→CRITICAL
|
|
//! at the expected utilization thresholds, first with *no* mempool head-room,
|
|
//! then with additional mempool head-room.
|
|
BOOST_AUTO_TEST_CASE(getcoinscachesizestate)
|
|
{
|
|
Chainstate& chainstate{m_node.chainman->ActiveChainstate()};
|
|
|
|
LOCK(::cs_main);
|
|
CCoinsViewCache& view{chainstate.CoinsTip()};
|
|
|
|
// Sanity: an empty cache should be ≲ 1 chunk (~ 256 KiB).
|
|
BOOST_CHECK_LT(view.DynamicMemoryUsage() / (256 * 1024.0), 1.1);
|
|
|
|
constexpr size_t MAX_COINS_BYTES{8_MiB};
|
|
constexpr size_t MAX_MEMPOOL_BYTES{4_MiB};
|
|
constexpr size_t MAX_ATTEMPTS{50'000};
|
|
|
|
// Run the same growth-path twice: first with 0 head-room, then with extra head-room
|
|
for (size_t max_mempool_size_bytes : {size_t{0}, MAX_MEMPOOL_BYTES}) {
|
|
const int64_t full_cap{int64_t(MAX_COINS_BYTES + max_mempool_size_bytes)};
|
|
const int64_t large_cap{LargeCoinsCacheThreshold(full_cap)};
|
|
|
|
// OK → LARGE
|
|
auto state{chainstate.GetCoinsCacheSizeState(MAX_COINS_BYTES, max_mempool_size_bytes)};
|
|
for (size_t i{0}; i < MAX_ATTEMPTS && int64_t(view.DynamicMemoryUsage()) <= large_cap; ++i) {
|
|
BOOST_CHECK_EQUAL(state, CoinsCacheSizeState::OK);
|
|
AddTestCoin(m_rng, view);
|
|
state = chainstate.GetCoinsCacheSizeState(MAX_COINS_BYTES, max_mempool_size_bytes);
|
|
}
|
|
|
|
// LARGE → CRITICAL
|
|
for (size_t i{0}; i < MAX_ATTEMPTS && int64_t(view.DynamicMemoryUsage()) <= full_cap; ++i) {
|
|
BOOST_CHECK_EQUAL(state, CoinsCacheSizeState::LARGE);
|
|
AddTestCoin(m_rng, view);
|
|
state = chainstate.GetCoinsCacheSizeState(MAX_COINS_BYTES, max_mempool_size_bytes);
|
|
}
|
|
BOOST_CHECK_EQUAL(state, CoinsCacheSizeState::CRITICAL);
|
|
}
|
|
|
|
// Default thresholds (no explicit limits) permit many more coins.
|
|
for (int i{0}; i < 1'000; ++i) {
|
|
AddTestCoin(m_rng, view);
|
|
BOOST_CHECK_EQUAL(chainstate.GetCoinsCacheSizeState(), CoinsCacheSizeState::OK);
|
|
}
|
|
|
|
// CRITICAL → OK via Flush
|
|
BOOST_CHECK_EQUAL(chainstate.GetCoinsCacheSizeState(MAX_COINS_BYTES, /*max_mempool_size_bytes=*/0), CoinsCacheSizeState::CRITICAL);
|
|
view.SetBestBlock(m_rng.rand256());
|
|
view.Flush();
|
|
BOOST_CHECK_EQUAL(chainstate.GetCoinsCacheSizeState(MAX_COINS_BYTES, /*max_mempool_size_bytes=*/0), CoinsCacheSizeState::OK);
|
|
}
|
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|