Skip to content

Commit fae8c28

Browse files
author
MarcoFalke
committed
Pass mempool pointer to GetCoinsCacheSizeState
1 parent fac674d commit fae8c28

File tree

3 files changed

+17
-17
lines changed

3 files changed

+17
-17
lines changed

src/test/validation_flush_tests.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ BOOST_AUTO_TEST_CASE(getcoinscachesizestate)
5656

5757
// Without any coins in the cache, we shouldn't need to flush.
5858
BOOST_CHECK_EQUAL(
59-
chainstate.GetCoinsCacheSizeState(tx_pool, MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes*/ 0),
59+
chainstate.GetCoinsCacheSizeState(&tx_pool, MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes*/ 0),
6060
CoinsCacheSizeState::OK);
6161

6262
// If the initial memory allocations of cacheCoins don't match these common
@@ -71,7 +71,7 @@ BOOST_AUTO_TEST_CASE(getcoinscachesizestate)
7171
}
7272

7373
BOOST_CHECK_EQUAL(
74-
chainstate.GetCoinsCacheSizeState(tx_pool, MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes*/ 0),
74+
chainstate.GetCoinsCacheSizeState(&tx_pool, MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes*/ 0),
7575
CoinsCacheSizeState::CRITICAL);
7676

7777
BOOST_TEST_MESSAGE("Exiting cache flush tests early due to unsupported arch");
@@ -92,34 +92,34 @@ BOOST_AUTO_TEST_CASE(getcoinscachesizestate)
9292
print_view_mem_usage(view);
9393
BOOST_CHECK_EQUAL(view.AccessCoin(res).DynamicMemoryUsage(), COIN_SIZE);
9494
BOOST_CHECK_EQUAL(
95-
chainstate.GetCoinsCacheSizeState(tx_pool, MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes*/ 0),
95+
chainstate.GetCoinsCacheSizeState(&tx_pool, MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes*/ 0),
9696
CoinsCacheSizeState::OK);
9797
}
9898

9999
// Adding some additional coins will push us over the edge to CRITICAL.
100100
for (int i{0}; i < 4; ++i) {
101101
add_coin(view);
102102
print_view_mem_usage(view);
103-
if (chainstate.GetCoinsCacheSizeState(tx_pool, MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes*/ 0) ==
103+
if (chainstate.GetCoinsCacheSizeState(&tx_pool, MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes*/ 0) ==
104104
CoinsCacheSizeState::CRITICAL) {
105105
break;
106106
}
107107
}
108108

109109
BOOST_CHECK_EQUAL(
110-
chainstate.GetCoinsCacheSizeState(tx_pool, MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes*/ 0),
110+
chainstate.GetCoinsCacheSizeState(&tx_pool, MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes*/ 0),
111111
CoinsCacheSizeState::CRITICAL);
112112

113113
// Passing non-zero max mempool usage should allow us more headroom.
114114
BOOST_CHECK_EQUAL(
115-
chainstate.GetCoinsCacheSizeState(tx_pool, MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes*/ 1 << 10),
115+
chainstate.GetCoinsCacheSizeState(&tx_pool, MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes*/ 1 << 10),
116116
CoinsCacheSizeState::OK);
117117

118118
for (int i{0}; i < 3; ++i) {
119119
add_coin(view);
120120
print_view_mem_usage(view);
121121
BOOST_CHECK_EQUAL(
122-
chainstate.GetCoinsCacheSizeState(tx_pool, MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes*/ 1 << 10),
122+
chainstate.GetCoinsCacheSizeState(&tx_pool, MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes*/ 1 << 10),
123123
CoinsCacheSizeState::OK);
124124
}
125125

@@ -135,31 +135,31 @@ BOOST_AUTO_TEST_CASE(getcoinscachesizestate)
135135
BOOST_CHECK(usage_percentage >= 0.9);
136136
BOOST_CHECK(usage_percentage < 1);
137137
BOOST_CHECK_EQUAL(
138-
chainstate.GetCoinsCacheSizeState(tx_pool, MAX_COINS_CACHE_BYTES, 1 << 10),
138+
chainstate.GetCoinsCacheSizeState(&tx_pool, MAX_COINS_CACHE_BYTES, 1 << 10),
139139
CoinsCacheSizeState::LARGE);
140140
}
141141

142142
// Using the default max_* values permits way more coins to be added.
143143
for (int i{0}; i < 1000; ++i) {
144144
add_coin(view);
145145
BOOST_CHECK_EQUAL(
146-
chainstate.GetCoinsCacheSizeState(tx_pool),
146+
chainstate.GetCoinsCacheSizeState(&tx_pool),
147147
CoinsCacheSizeState::OK);
148148
}
149149

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

153153
BOOST_CHECK_EQUAL(
154-
chainstate.GetCoinsCacheSizeState(tx_pool, MAX_COINS_CACHE_BYTES, 0),
154+
chainstate.GetCoinsCacheSizeState(&tx_pool, MAX_COINS_CACHE_BYTES, 0),
155155
CoinsCacheSizeState::CRITICAL);
156156

157157
view.SetBestBlock(InsecureRand256());
158158
BOOST_CHECK(view.Flush());
159159
print_view_mem_usage(view);
160160

161161
BOOST_CHECK_EQUAL(
162-
chainstate.GetCoinsCacheSizeState(tx_pool, MAX_COINS_CACHE_BYTES, 0),
162+
chainstate.GetCoinsCacheSizeState(&tx_pool, MAX_COINS_CACHE_BYTES, 0),
163163
CoinsCacheSizeState::CRITICAL);
164164
}
165165

src/validation.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2227,7 +2227,7 @@ bool CChainState::ConnectBlock(const CBlock& block, BlockValidationState& state,
22272227
return true;
22282228
}
22292229

2230-
CoinsCacheSizeState CChainState::GetCoinsCacheSizeState(const CTxMemPool& tx_pool)
2230+
CoinsCacheSizeState CChainState::GetCoinsCacheSizeState(const CTxMemPool* tx_pool)
22312231
{
22322232
return this->GetCoinsCacheSizeState(
22332233
tx_pool,
@@ -2236,11 +2236,11 @@ CoinsCacheSizeState CChainState::GetCoinsCacheSizeState(const CTxMemPool& tx_poo
22362236
}
22372237

22382238
CoinsCacheSizeState CChainState::GetCoinsCacheSizeState(
2239-
const CTxMemPool& tx_pool,
2239+
const CTxMemPool* tx_pool,
22402240
size_t max_coins_cache_size_bytes,
22412241
size_t max_mempool_size_bytes)
22422242
{
2243-
int64_t nMempoolUsage = tx_pool.DynamicMemoryUsage();
2243+
const int64_t nMempoolUsage = tx_pool ? tx_pool->DynamicMemoryUsage() : 0;
22442244
int64_t cacheSize = CoinsTip().DynamicMemoryUsage();
22452245
int64_t nTotalSpace =
22462246
max_coins_cache_size_bytes + std::max<int64_t>(max_mempool_size_bytes - nMempoolUsage, 0);
@@ -2279,7 +2279,7 @@ bool CChainState::FlushStateToDisk(
22792279
{
22802280
bool fFlushForPrune = false;
22812281
bool fDoFullFlush = false;
2282-
CoinsCacheSizeState cache_state = GetCoinsCacheSizeState(::mempool);
2282+
CoinsCacheSizeState cache_state = GetCoinsCacheSizeState(&::mempool);
22832283
LOCK(cs_LastBlockFile);
22842284
if (fPruneMode && (fCheckForPruning || nManualPruneHeight > 0) && !fReindex) {
22852285
if (nManualPruneHeight > 0) {

src/validation.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -674,11 +674,11 @@ class CChainState {
674674
//! Dictates whether we need to flush the cache to disk or not.
675675
//!
676676
//! @return the state of the size of the coins cache.
677-
CoinsCacheSizeState GetCoinsCacheSizeState(const CTxMemPool& tx_pool)
677+
CoinsCacheSizeState GetCoinsCacheSizeState(const CTxMemPool* tx_pool)
678678
EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
679679

680680
CoinsCacheSizeState GetCoinsCacheSizeState(
681-
const CTxMemPool& tx_pool,
681+
const CTxMemPool* tx_pool,
682682
size_t max_coins_cache_size_bytes,
683683
size_t max_mempool_size_bytes) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
684684

0 commit comments

Comments
 (0)