0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-02-13 11:25:02 -05:00

refactor: no mempool arg to GetCoinsCacheSizeState

Unnecessary argument since we can make use of this->m_mempool

Co-authored-by: John Newbery <john@johnnewbery.com>
This commit is contained in:
James O'Beirne 2021-07-09 09:24:27 -04:00
parent 46e3efd1e4
commit 4abf0779d6
No known key found for this signature in database
GPG key ID: 7A935DADB2C44F05
3 changed files with 16 additions and 21 deletions

View file

@ -23,7 +23,6 @@ BOOST_AUTO_TEST_CASE(getcoinscachesizestate)
CChainState chainstate{&mempool, blockman}; CChainState chainstate{&mempool, blockman};
chainstate.InitCoinsDB(/*cache_size_bytes*/ 1 << 10, /*in_memory*/ true, /*should_wipe*/ false); chainstate.InitCoinsDB(/*cache_size_bytes*/ 1 << 10, /*in_memory*/ true, /*should_wipe*/ false);
WITH_LOCK(::cs_main, chainstate.InitCoinsCache(1 << 10)); WITH_LOCK(::cs_main, chainstate.InitCoinsCache(1 << 10));
CTxMemPool tx_pool{};
constexpr bool is_64_bit = sizeof(void*) == 8; constexpr bool is_64_bit = sizeof(void*) == 8;
@ -57,7 +56,7 @@ BOOST_AUTO_TEST_CASE(getcoinscachesizestate)
// Without any coins in the cache, we shouldn't need to flush. // Without any coins in the cache, we shouldn't need to flush.
BOOST_CHECK_EQUAL( BOOST_CHECK_EQUAL(
chainstate.GetCoinsCacheSizeState(&tx_pool, MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes*/ 0), chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes*/ 0),
CoinsCacheSizeState::OK); CoinsCacheSizeState::OK);
// If the initial memory allocations of cacheCoins don't match these common // If the initial memory allocations of cacheCoins don't match these common
@ -72,7 +71,7 @@ BOOST_AUTO_TEST_CASE(getcoinscachesizestate)
} }
BOOST_CHECK_EQUAL( BOOST_CHECK_EQUAL(
chainstate.GetCoinsCacheSizeState(&tx_pool, MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes*/ 0), chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes*/ 0),
CoinsCacheSizeState::CRITICAL); CoinsCacheSizeState::CRITICAL);
BOOST_TEST_MESSAGE("Exiting cache flush tests early due to unsupported arch"); BOOST_TEST_MESSAGE("Exiting cache flush tests early due to unsupported arch");
@ -93,7 +92,7 @@ BOOST_AUTO_TEST_CASE(getcoinscachesizestate)
print_view_mem_usage(view); print_view_mem_usage(view);
BOOST_CHECK_EQUAL(view.AccessCoin(res).DynamicMemoryUsage(), COIN_SIZE); BOOST_CHECK_EQUAL(view.AccessCoin(res).DynamicMemoryUsage(), COIN_SIZE);
BOOST_CHECK_EQUAL( BOOST_CHECK_EQUAL(
chainstate.GetCoinsCacheSizeState(&tx_pool, MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes*/ 0), chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes*/ 0),
CoinsCacheSizeState::OK); CoinsCacheSizeState::OK);
} }
@ -101,26 +100,26 @@ BOOST_AUTO_TEST_CASE(getcoinscachesizestate)
for (int i{0}; i < 4; ++i) { for (int i{0}; i < 4; ++i) {
add_coin(view); add_coin(view);
print_view_mem_usage(view); print_view_mem_usage(view);
if (chainstate.GetCoinsCacheSizeState(&tx_pool, MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes*/ 0) == if (chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes*/ 0) ==
CoinsCacheSizeState::CRITICAL) { CoinsCacheSizeState::CRITICAL) {
break; break;
} }
} }
BOOST_CHECK_EQUAL( BOOST_CHECK_EQUAL(
chainstate.GetCoinsCacheSizeState(&tx_pool, MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes*/ 0), chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes*/ 0),
CoinsCacheSizeState::CRITICAL); CoinsCacheSizeState::CRITICAL);
// Passing non-zero max mempool usage should allow us more headroom. // Passing non-zero max mempool usage should allow us more headroom.
BOOST_CHECK_EQUAL( BOOST_CHECK_EQUAL(
chainstate.GetCoinsCacheSizeState(&tx_pool, MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes*/ 1 << 10), chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes*/ 1 << 10),
CoinsCacheSizeState::OK); CoinsCacheSizeState::OK);
for (int i{0}; i < 3; ++i) { for (int i{0}; i < 3; ++i) {
add_coin(view); add_coin(view);
print_view_mem_usage(view); print_view_mem_usage(view);
BOOST_CHECK_EQUAL( BOOST_CHECK_EQUAL(
chainstate.GetCoinsCacheSizeState(&tx_pool, MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes*/ 1 << 10), chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes*/ 1 << 10),
CoinsCacheSizeState::OK); CoinsCacheSizeState::OK);
} }
@ -136,7 +135,7 @@ BOOST_AUTO_TEST_CASE(getcoinscachesizestate)
BOOST_CHECK(usage_percentage >= 0.9); BOOST_CHECK(usage_percentage >= 0.9);
BOOST_CHECK(usage_percentage < 1); BOOST_CHECK(usage_percentage < 1);
BOOST_CHECK_EQUAL( BOOST_CHECK_EQUAL(
chainstate.GetCoinsCacheSizeState(&tx_pool, MAX_COINS_CACHE_BYTES, 1 << 10), chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, 1 << 10),
CoinsCacheSizeState::LARGE); CoinsCacheSizeState::LARGE);
} }
@ -144,7 +143,7 @@ BOOST_AUTO_TEST_CASE(getcoinscachesizestate)
for (int i{0}; i < 1000; ++i) { for (int i{0}; i < 1000; ++i) {
add_coin(view); add_coin(view);
BOOST_CHECK_EQUAL( BOOST_CHECK_EQUAL(
chainstate.GetCoinsCacheSizeState(&tx_pool), chainstate.GetCoinsCacheSizeState(),
CoinsCacheSizeState::OK); CoinsCacheSizeState::OK);
} }
@ -152,7 +151,7 @@ BOOST_AUTO_TEST_CASE(getcoinscachesizestate)
// preallocated memory that doesn't get reclaimed even after flush. // preallocated memory that doesn't get reclaimed even after flush.
BOOST_CHECK_EQUAL( BOOST_CHECK_EQUAL(
chainstate.GetCoinsCacheSizeState(&tx_pool, MAX_COINS_CACHE_BYTES, 0), chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, 0),
CoinsCacheSizeState::CRITICAL); CoinsCacheSizeState::CRITICAL);
view.SetBestBlock(InsecureRand256()); view.SetBestBlock(InsecureRand256());
@ -160,7 +159,7 @@ BOOST_AUTO_TEST_CASE(getcoinscachesizestate)
print_view_mem_usage(view); print_view_mem_usage(view);
BOOST_CHECK_EQUAL( BOOST_CHECK_EQUAL(
chainstate.GetCoinsCacheSizeState(&tx_pool, MAX_COINS_CACHE_BYTES, 0), chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, 0),
CoinsCacheSizeState::CRITICAL); CoinsCacheSizeState::CRITICAL);
} }

View file

@ -1998,20 +1998,18 @@ bool CChainState::ConnectBlock(const CBlock& block, BlockValidationState& state,
return true; return true;
} }
CoinsCacheSizeState CChainState::GetCoinsCacheSizeState(const CTxMemPool* tx_pool) CoinsCacheSizeState CChainState::GetCoinsCacheSizeState()
{ {
return this->GetCoinsCacheSizeState( return this->GetCoinsCacheSizeState(
tx_pool,
m_coinstip_cache_size_bytes, m_coinstip_cache_size_bytes,
gArgs.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000); gArgs.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000);
} }
CoinsCacheSizeState CChainState::GetCoinsCacheSizeState( CoinsCacheSizeState CChainState::GetCoinsCacheSizeState(
const CTxMemPool* tx_pool,
size_t max_coins_cache_size_bytes, size_t max_coins_cache_size_bytes,
size_t max_mempool_size_bytes) size_t max_mempool_size_bytes)
{ {
const int64_t nMempoolUsage = tx_pool ? tx_pool->DynamicMemoryUsage() : 0; const int64_t nMempoolUsage = m_mempool ? m_mempool->DynamicMemoryUsage() : 0;
int64_t cacheSize = CoinsTip().DynamicMemoryUsage(); int64_t cacheSize = CoinsTip().DynamicMemoryUsage();
int64_t nTotalSpace = int64_t nTotalSpace =
max_coins_cache_size_bytes + std::max<int64_t>(max_mempool_size_bytes - nMempoolUsage, 0); max_coins_cache_size_bytes + std::max<int64_t>(max_mempool_size_bytes - nMempoolUsage, 0);
@ -2050,7 +2048,7 @@ bool CChainState::FlushStateToDisk(
bool fFlushForPrune = false; bool fFlushForPrune = false;
bool fDoFullFlush = false; bool fDoFullFlush = false;
CoinsCacheSizeState cache_state = GetCoinsCacheSizeState(m_mempool); CoinsCacheSizeState cache_state = GetCoinsCacheSizeState();
LOCK(cs_LastBlockFile); LOCK(cs_LastBlockFile);
if (fPruneMode && (fCheckForPruning || nManualPruneHeight > 0) && !fReindex) { if (fPruneMode && (fCheckForPruning || nManualPruneHeight > 0) && !fReindex) {
// make sure we don't prune above the blockfilterindexes bestblocks // make sure we don't prune above the blockfilterindexes bestblocks
@ -4885,7 +4883,7 @@ bool ChainstateManager::PopulateAndValidateSnapshot(
} }
const auto snapshot_cache_state = WITH_LOCK(::cs_main, const auto snapshot_cache_state = WITH_LOCK(::cs_main,
return snapshot_chainstate.GetCoinsCacheSizeState(snapshot_chainstate.m_mempool)); return snapshot_chainstate.GetCoinsCacheSizeState());
if (snapshot_cache_state >= if (snapshot_cache_state >=
CoinsCacheSizeState::CRITICAL) { CoinsCacheSizeState::CRITICAL) {

View file

@ -777,11 +777,9 @@ public:
//! Dictates whether we need to flush the cache to disk or not. //! Dictates whether we need to flush the cache to disk or not.
//! //!
//! @return the state of the size of the coins cache. //! @return the state of the size of the coins cache.
CoinsCacheSizeState GetCoinsCacheSizeState(const CTxMemPool* tx_pool) CoinsCacheSizeState GetCoinsCacheSizeState() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
CoinsCacheSizeState GetCoinsCacheSizeState( CoinsCacheSizeState GetCoinsCacheSizeState(
const CTxMemPool* tx_pool,
size_t max_coins_cache_size_bytes, size_t max_coins_cache_size_bytes,
size_t max_mempool_size_bytes) EXCLUSIVE_LOCKS_REQUIRED(::cs_main); size_t max_mempool_size_bytes) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);