Skip to content

Commit 21527cb

Browse files
committed
merge bitcoin#19604: Pass mempool pointer to UnloadBlockIndex/GetCoinsCacheSizeState
1 parent cb82f14 commit 21527cb

File tree

8 files changed

+27
-36
lines changed

8 files changed

+27
-36
lines changed

src/init.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1986,7 +1986,7 @@ bool AppInitMain(const util::Ref& context, NodeContext& node, interfaces::BlockA
19861986
chainman.m_total_coinstip_cache = nCoinCacheUsage;
19871987
chainman.m_total_coinsdb_cache = nCoinDBCache;
19881988

1989-
UnloadBlockIndex();
1989+
UnloadBlockIndex(node.mempool);
19901990

19911991
// new CBlockTreeDB tries to delete the existing file, which
19921992
// fails if it's still open from the previous loop. Close it first:

src/qt/test/apptests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ void AppTests::appTests()
8787
// Reset global state to avoid interfering with later tests.
8888
LogInstance().DisconnectTestLogger();
8989
AbortShutdown();
90-
UnloadBlockIndex();
90+
UnloadBlockIndex(/* mempool */ nullptr);
9191
WITH_LOCK(::cs_main, g_chainman.Reset());
9292
}
9393

src/test/util/setup_common.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,9 @@ TestingSetup::~TestingSetup()
177177
GetMainSignals().UnregisterBackgroundSignalScheduler();
178178
m_node.connman.reset();
179179
m_node.banman.reset();
180+
UnloadBlockIndex(m_node.mempool);
180181
m_node.mempool = nullptr;
181182
m_node.scheduler.reset();
182-
UnloadBlockIndex();
183183
llmq::DestroyLLMQSystem();
184184
m_node.chainman->Reset();
185185
m_node.chainman = nullptr;

src/test/validation_chainstate_tests.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@ BOOST_AUTO_TEST_CASE(validation_chainstate_resize_caches)
3434
return outp;
3535
};
3636

37-
ENTER_CRITICAL_SECTION(cs_main);
38-
CChainState& c1 = manager.InitializeChainstate();
39-
LEAVE_CRITICAL_SECTION(cs_main);
37+
CChainState& c1 = *WITH_LOCK(cs_main, return &manager.InitializeChainstate());
4038
c1.InitCoinsDB(
4139
/* cache_size_bytes */ 1 << 23, /* in_memory */ true, /* should_wipe */ false);
4240
WITH_LOCK(::cs_main, c1.InitCoinsCache(1 << 23));

src/test/validation_chainstatemanager_tests.cpp

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,7 @@ BOOST_AUTO_TEST_CASE(chainstatemanager)
5959

6060
// Create a snapshot-based chainstate.
6161
//
62-
ENTER_CRITICAL_SECTION(cs_main);
63-
CChainState& c2 = *WITH_LOCK(::cs_main,
64-
return &manager.InitializeChainstate(GetRandHash()));
65-
LEAVE_CRITICAL_SECTION(cs_main);
62+
CChainState& c2 = *WITH_LOCK(::cs_main, return &manager.InitializeChainstate(GetRandHash()));
6663
chainstates.push_back(&c2);
6764
c2.InitCoinsDB(
6865
/* cache_size_bytes */ 1 << 23, /* in_memory */ true, /* should_wipe */ false);
@@ -120,9 +117,7 @@ BOOST_AUTO_TEST_CASE(chainstatemanager_rebalance_caches)
120117

121118
// Create a legacy (IBD) chainstate.
122119
//
123-
ENTER_CRITICAL_SECTION(cs_main);
124-
CChainState& c1 = manager.InitializeChainstate();
125-
LEAVE_CRITICAL_SECTION(cs_main);
120+
CChainState& c1 = *WITH_LOCK(cs_main, return &manager.InitializeChainstate());
126121
chainstates.push_back(&c1);
127122
c1.InitCoinsDB(
128123
/* cache_size_bytes */ 1 << 23, /* in_memory */ true, /* should_wipe */ false);
@@ -139,9 +134,7 @@ BOOST_AUTO_TEST_CASE(chainstatemanager_rebalance_caches)
139134

140135
// Create a snapshot-based chainstate.
141136
//
142-
ENTER_CRITICAL_SECTION(cs_main);
143-
CChainState& c2 = manager.InitializeChainstate(GetRandHash());
144-
LEAVE_CRITICAL_SECTION(cs_main);
137+
CChainState& c2 = *WITH_LOCK(cs_main, return &manager.InitializeChainstate(GetRandHash()));
145138
chainstates.push_back(&c2);
146139
c2.InitCoinsDB(
147140
/* cache_size_bytes */ 1 << 23, /* in_memory */ true, /* should_wipe */ false);

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: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2402,7 +2402,7 @@ bool CChainState::ConnectBlock(const CBlock& block, CValidationState& state, CBl
24022402
return true;
24032403
}
24042404

2405-
CoinsCacheSizeState CChainState::GetCoinsCacheSizeState(const CTxMemPool& tx_pool)
2405+
CoinsCacheSizeState CChainState::GetCoinsCacheSizeState(const CTxMemPool* tx_pool)
24062406
{
24072407
return this->GetCoinsCacheSizeState(
24082408
tx_pool,
@@ -2411,11 +2411,11 @@ CoinsCacheSizeState CChainState::GetCoinsCacheSizeState(const CTxMemPool& tx_poo
24112411
}
24122412

24132413
CoinsCacheSizeState CChainState::GetCoinsCacheSizeState(
2414-
const CTxMemPool& tx_pool,
2414+
const CTxMemPool* tx_pool,
24152415
size_t max_coins_cache_size_bytes,
24162416
size_t max_mempool_size_bytes)
24172417
{
2418-
int64_t nMempoolUsage = tx_pool.DynamicMemoryUsage();
2418+
const int64_t nMempoolUsage = tx_pool ? tx_pool->DynamicMemoryUsage() : 0;
24192419
int64_t cacheSize = CoinsTip().DynamicMemoryUsage();
24202420
int64_t nTotalSpace =
24212421
max_coins_cache_size_bytes + std::max<int64_t>(max_mempool_size_bytes - nMempoolUsage, 0);
@@ -2456,7 +2456,7 @@ bool CChainState::FlushStateToDisk(
24562456
{
24572457
bool fFlushForPrune = false;
24582458
bool fDoFullFlush = false;
2459-
CoinsCacheSizeState cache_state = GetCoinsCacheSizeState(::mempool);
2459+
CoinsCacheSizeState cache_state = GetCoinsCacheSizeState(&::mempool);
24602460
LOCK(cs_LastBlockFile);
24612461
if (fPruneMode && (fCheckForPruning || nManualPruneHeight > 0) && !fReindex) {
24622462
if (nManualPruneHeight > 0) {
@@ -4832,13 +4832,13 @@ void CChainState::UnloadBlockIndex() {
48324832
// May NOT be used after any connections are up as much
48334833
// of the peer-processing logic assumes a consistent
48344834
// block index state
4835-
void UnloadBlockIndex()
4835+
void UnloadBlockIndex(CTxMemPool* mempool)
48364836
{
48374837
LOCK(cs_main);
48384838
g_chainman.Unload();
48394839
pindexBestInvalid = nullptr;
48404840
pindexBestHeader = nullptr;
4841-
mempool.clear();
4841+
if (mempool) mempool->clear();
48424842
vinfoBlockFile.clear();
48434843
nLastBlockFile = 0;
48444844
setDirtyBlockIndex.clear();

src/validation.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ void LoadExternalBlockFile(const CChainParams& chainparams, FILE* fileIn, FlatFi
178178
/** Ensures we have a genesis block in the block tree, possibly writing one to disk. */
179179
bool LoadGenesisBlock(const CChainParams& chainparams);
180180
/** Unload database information */
181-
void UnloadBlockIndex();
181+
void UnloadBlockIndex(CTxMemPool* mempool);
182182
/** Run instances of script checking worker threads */
183183
void StartScriptCheckWorkerThreads(int threads_num);
184184
/** Stop all of the script checking worker threads */
@@ -691,11 +691,11 @@ class CChainState
691691
//! Dictates whether we need to flush the cache to disk or not.
692692
//!
693693
//! @return the state of the size of the coins cache.
694-
CoinsCacheSizeState GetCoinsCacheSizeState(const CTxMemPool& tx_pool)
694+
CoinsCacheSizeState GetCoinsCacheSizeState(const CTxMemPool* tx_pool)
695695
EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
696696

697697
CoinsCacheSizeState GetCoinsCacheSizeState(
698-
const CTxMemPool& tx_pool,
698+
const CTxMemPool* tx_pool,
699699
size_t max_coins_cache_size_bytes,
700700
size_t max_mempool_size_bytes) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
701701

0 commit comments

Comments
 (0)