Skip to content

Commit 4abf077

Browse files
jamesobjnewbery
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 46e3efd commit 4abf077

File tree

3 files changed

+16
-21
lines changed

3 files changed

+16
-21
lines changed

src/test/validation_flush_tests.cpp

Lines changed: 11 additions & 12 deletions
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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1998,20 +1998,18 @@ bool CChainState::ConnectBlock(const CBlock& block, BlockValidationState& state,
19981998
return true;
19991999
}
20002000

2001-
CoinsCacheSizeState CChainState::GetCoinsCacheSizeState(const CTxMemPool* tx_pool)
2001+
CoinsCacheSizeState CChainState::GetCoinsCacheSizeState()
20022002
{
20032003
return this->GetCoinsCacheSizeState(
2004-
tx_pool,
20052004
m_coinstip_cache_size_bytes,
20062005
gArgs.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000);
20072006
}
20082007

20092008
CoinsCacheSizeState CChainState::GetCoinsCacheSizeState(
2010-
const CTxMemPool* tx_pool,
20112009
size_t max_coins_cache_size_bytes,
20122010
size_t max_mempool_size_bytes)
20132011
{
2014-
const int64_t nMempoolUsage = tx_pool ? tx_pool->DynamicMemoryUsage() : 0;
2012+
const int64_t nMempoolUsage = m_mempool ? m_mempool->DynamicMemoryUsage() : 0;
20152013
int64_t cacheSize = CoinsTip().DynamicMemoryUsage();
20162014
int64_t nTotalSpace =
20172015
max_coins_cache_size_bytes + std::max<int64_t>(max_mempool_size_bytes - nMempoolUsage, 0);
@@ -2050,7 +2048,7 @@ bool CChainState::FlushStateToDisk(
20502048
bool fFlushForPrune = false;
20512049
bool fDoFullFlush = false;
20522050

2053-
CoinsCacheSizeState cache_state = GetCoinsCacheSizeState(m_mempool);
2051+
CoinsCacheSizeState cache_state = GetCoinsCacheSizeState();
20542052
LOCK(cs_LastBlockFile);
20552053
if (fPruneMode && (fCheckForPruning || nManualPruneHeight > 0) && !fReindex) {
20562054
// make sure we don't prune above the blockfilterindexes bestblocks
@@ -4885,7 +4883,7 @@ bool ChainstateManager::PopulateAndValidateSnapshot(
48854883
}
48864884

48874885
const auto snapshot_cache_state = WITH_LOCK(::cs_main,
4888-
return snapshot_chainstate.GetCoinsCacheSizeState(snapshot_chainstate.m_mempool));
4886+
return snapshot_chainstate.GetCoinsCacheSizeState());
48894887

48904888
if (snapshot_cache_state >=
48914889
CoinsCacheSizeState::CRITICAL) {

src/validation.h

Lines changed: 1 addition & 3 deletions
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)