Skip to content

Commit bf5b8cb

Browse files
jamesobjnewbery
authored andcommitted
refactor: no mempool arg to GetCoinsCacheSizeState
Unnecessary argument since we can make use of this->m_mempool Co-authored-by: John Newbery <[email protected]>
1 parent 78bdd7b commit bf5b8cb

File tree

3 files changed

+16
-21
lines changed

3 files changed

+16
-21
lines changed

src/test/validation_flush_tests.cpp

+11-12
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ BOOST_AUTO_TEST_CASE(getcoinscachesizestate)
2323
CChainState chainstate{&mempool, blockman};
2424
chainstate.InitCoinsDB(/*cache_size_bytes*/ 1 << 10, /*in_memory*/ true, /*should_wipe*/ false);
2525
WITH_LOCK(::cs_main, chainstate.InitCoinsCache(1 << 10));
26-
CTxMemPool tx_pool{};
2726

2827
constexpr bool is_64_bit = sizeof(void*) == 8;
2928

@@ -57,7 +56,7 @@ BOOST_AUTO_TEST_CASE(getcoinscachesizestate)
5756

5857
// Without any coins in the cache, we shouldn't need to flush.
5958
BOOST_CHECK_EQUAL(
60-
chainstate.GetCoinsCacheSizeState(&tx_pool, MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes*/ 0),
59+
chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes*/ 0),
6160
CoinsCacheSizeState::OK);
6261

6362
// If the initial memory allocations of cacheCoins don't match these common
@@ -72,7 +71,7 @@ BOOST_AUTO_TEST_CASE(getcoinscachesizestate)
7271
}
7372

7473
BOOST_CHECK_EQUAL(
75-
chainstate.GetCoinsCacheSizeState(&tx_pool, MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes*/ 0),
74+
chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes*/ 0),
7675
CoinsCacheSizeState::CRITICAL);
7776

7877
BOOST_TEST_MESSAGE("Exiting cache flush tests early due to unsupported arch");
@@ -93,34 +92,34 @@ BOOST_AUTO_TEST_CASE(getcoinscachesizestate)
9392
print_view_mem_usage(view);
9493
BOOST_CHECK_EQUAL(view.AccessCoin(res).DynamicMemoryUsage(), COIN_SIZE);
9594
BOOST_CHECK_EQUAL(
96-
chainstate.GetCoinsCacheSizeState(&tx_pool, MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes*/ 0),
95+
chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes*/ 0),
9796
CoinsCacheSizeState::OK);
9897
}
9998

10099
// Adding some additional coins will push us over the edge to CRITICAL.
101100
for (int i{0}; i < 4; ++i) {
102101
add_coin(view);
103102
print_view_mem_usage(view);
104-
if (chainstate.GetCoinsCacheSizeState(&tx_pool, MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes*/ 0) ==
103+
if (chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes*/ 0) ==
105104
CoinsCacheSizeState::CRITICAL) {
106105
break;
107106
}
108107
}
109108

110109
BOOST_CHECK_EQUAL(
111-
chainstate.GetCoinsCacheSizeState(&tx_pool, MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes*/ 0),
110+
chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes*/ 0),
112111
CoinsCacheSizeState::CRITICAL);
113112

114113
// Passing non-zero max mempool usage should allow us more headroom.
115114
BOOST_CHECK_EQUAL(
116-
chainstate.GetCoinsCacheSizeState(&tx_pool, MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes*/ 1 << 10),
115+
chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes*/ 1 << 10),
117116
CoinsCacheSizeState::OK);
118117

119118
for (int i{0}; i < 3; ++i) {
120119
add_coin(view);
121120
print_view_mem_usage(view);
122121
BOOST_CHECK_EQUAL(
123-
chainstate.GetCoinsCacheSizeState(&tx_pool, MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes*/ 1 << 10),
122+
chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes*/ 1 << 10),
124123
CoinsCacheSizeState::OK);
125124
}
126125

@@ -136,31 +135,31 @@ BOOST_AUTO_TEST_CASE(getcoinscachesizestate)
136135
BOOST_CHECK(usage_percentage >= 0.9);
137136
BOOST_CHECK(usage_percentage < 1);
138137
BOOST_CHECK_EQUAL(
139-
chainstate.GetCoinsCacheSizeState(&tx_pool, MAX_COINS_CACHE_BYTES, 1 << 10),
138+
chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, 1 << 10),
140139
CoinsCacheSizeState::LARGE);
141140
}
142141

143142
// Using the default max_* values permits way more coins to be added.
144143
for (int i{0}; i < 1000; ++i) {
145144
add_coin(view);
146145
BOOST_CHECK_EQUAL(
147-
chainstate.GetCoinsCacheSizeState(&tx_pool),
146+
chainstate.GetCoinsCacheSizeState(),
148147
CoinsCacheSizeState::OK);
149148
}
150149

151150
// Flushing the view doesn't take us back to OK because cacheCoins has
152151
// preallocated memory that doesn't get reclaimed even after flush.
153152

154153
BOOST_CHECK_EQUAL(
155-
chainstate.GetCoinsCacheSizeState(&tx_pool, MAX_COINS_CACHE_BYTES, 0),
154+
chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, 0),
156155
CoinsCacheSizeState::CRITICAL);
157156

158157
view.SetBestBlock(InsecureRand256());
159158
BOOST_CHECK(view.Flush());
160159
print_view_mem_usage(view);
161160

162161
BOOST_CHECK_EQUAL(
163-
chainstate.GetCoinsCacheSizeState(&tx_pool, MAX_COINS_CACHE_BYTES, 0),
162+
chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, 0),
164163
CoinsCacheSizeState::CRITICAL);
165164
}
166165

src/validation.cpp

+4-6
Original file line numberDiff line numberDiff line change
@@ -2002,20 +2002,18 @@ bool CChainState::ConnectBlock(const CBlock& block, BlockValidationState& state,
20022002
return true;
20032003
}
20042004

2005-
CoinsCacheSizeState CChainState::GetCoinsCacheSizeState(const CTxMemPool* tx_pool)
2005+
CoinsCacheSizeState CChainState::GetCoinsCacheSizeState()
20062006
{
20072007
return this->GetCoinsCacheSizeState(
2008-
tx_pool,
20092008
m_coinstip_cache_size_bytes,
20102009
gArgs.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000);
20112010
}
20122011

20132012
CoinsCacheSizeState CChainState::GetCoinsCacheSizeState(
2014-
const CTxMemPool* tx_pool,
20152013
size_t max_coins_cache_size_bytes,
20162014
size_t max_mempool_size_bytes)
20172015
{
2018-
const int64_t nMempoolUsage = tx_pool ? tx_pool->DynamicMemoryUsage() : 0;
2016+
const int64_t nMempoolUsage = m_mempool ? m_mempool->DynamicMemoryUsage() : 0;
20192017
int64_t cacheSize = CoinsTip().DynamicMemoryUsage();
20202018
int64_t nTotalSpace =
20212019
max_coins_cache_size_bytes + std::max<int64_t>(max_mempool_size_bytes - nMempoolUsage, 0);
@@ -2054,7 +2052,7 @@ bool CChainState::FlushStateToDisk(
20542052
bool fFlushForPrune = false;
20552053
bool fDoFullFlush = false;
20562054

2057-
CoinsCacheSizeState cache_state = GetCoinsCacheSizeState(m_mempool);
2055+
CoinsCacheSizeState cache_state = GetCoinsCacheSizeState();
20582056
LOCK(cs_LastBlockFile);
20592057
if (fPruneMode && (fCheckForPruning || nManualPruneHeight > 0) && !fReindex) {
20602058
// make sure we don't prune above the blockfilterindexes bestblocks
@@ -4889,7 +4887,7 @@ bool ChainstateManager::PopulateAndValidateSnapshot(
48894887
}
48904888

48914889
const auto snapshot_cache_state = WITH_LOCK(::cs_main,
4892-
return snapshot_chainstate.GetCoinsCacheSizeState(snapshot_chainstate.m_mempool));
4890+
return snapshot_chainstate.GetCoinsCacheSizeState());
48934891

48944892
if (snapshot_cache_state >=
48954893
CoinsCacheSizeState::CRITICAL) {

src/validation.h

+1-3
Original file line numberDiff line numberDiff line change
@@ -777,11 +777,9 @@ class CChainState
777777
//! Dictates whether we need to flush the cache to disk or not.
778778
//!
779779
//! @return the state of the size of the coins cache.
780-
CoinsCacheSizeState GetCoinsCacheSizeState(const CTxMemPool* tx_pool)
781-
EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
780+
CoinsCacheSizeState GetCoinsCacheSizeState() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
782781

783782
CoinsCacheSizeState GetCoinsCacheSizeState(
784-
const CTxMemPool* tx_pool,
785783
size_t max_coins_cache_size_bytes,
786784
size_t max_mempool_size_bytes) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
787785

0 commit comments

Comments
 (0)