Skip to content

Commit ad2952d

Browse files
author
MarcoFalke
committed
Merge #19604: Pass mempool pointer to UnloadBlockIndex/GetCoinsCacheSizeState
fae8c28 Pass mempool pointer to GetCoinsCacheSizeState (MarcoFalke) fac674d Pass mempool pointer to UnloadBlockIndex (MarcoFalke) faec851 test: Simplify cs_main locks (MarcoFalke) Pull request description: Split out from #19556 Instead of relying on the implicit mempool global, pass a mempool pointer (which can be `0`). This helps with testing, code clarity and unlocks the features described in #19556. ACKs for top commit: jnewbery: code review ACK fae8c28 fjahr: Code review ACK fae8c28 darosior: Tested ACK fae8c28 jamesob: ACK fae8c28 ([`jamesob/ackr/19604.1.MarcoFalke.pass_mempool_pointer_to`](https://github.com/jamesob/bitcoin/tree/ackr/19604.1.MarcoFalke.pass_mempool_pointer_to)) Tree-SHA512: fa687518c8cda4a095bdbdfe56e01fae2fb16c13d51efbb1312cd6dc007611fc47f53f475602e4a843e3973c9410e6af5a81d6847bd2399f8262ca7205975728
2 parents 62d137a + fae8c28 commit ad2952d

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
@@ -1567,7 +1567,7 @@ bool AppInitMain(const util::Ref& context, NodeContext& node)
15671567
chainman.m_total_coinstip_cache = nCoinCacheUsage;
15681568
chainman.m_total_coinsdb_cache = nCoinDBCache;
15691569

1570-
UnloadBlockIndex();
1570+
UnloadBlockIndex(node.mempool);
15711571

15721572
// new CBlockTreeDB tries to delete the existing file, which
15731573
// 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
@@ -83,7 +83,7 @@ void AppTests::appTests()
8383
// Reset global state to avoid interfering with later tests.
8484
LogInstance().DisconnectTestLogger();
8585
AbortShutdown();
86-
UnloadBlockIndex();
86+
UnloadBlockIndex(/* mempool */ nullptr);
8787
WITH_LOCK(::cs_main, g_chainman.Reset());
8888
}
8989

src/test/util/setup_common.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,9 @@ TestingSetup::~TestingSetup()
182182
m_node.connman.reset();
183183
m_node.banman.reset();
184184
m_node.args = nullptr;
185+
UnloadBlockIndex(m_node.mempool);
185186
m_node.mempool = nullptr;
186187
m_node.scheduler.reset();
187-
UnloadBlockIndex();
188188
m_node.chainman->Reset();
189189
m_node.chainman = nullptr;
190190
pblocktree.reset();

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
@@ -54,10 +54,7 @@ BOOST_AUTO_TEST_CASE(chainstatemanager)
5454

5555
// Create a snapshot-based chainstate.
5656
//
57-
ENTER_CRITICAL_SECTION(cs_main);
58-
CChainState& c2 = *WITH_LOCK(::cs_main,
59-
return &manager.InitializeChainstate(GetRandHash()));
60-
LEAVE_CRITICAL_SECTION(cs_main);
57+
CChainState& c2 = *WITH_LOCK(::cs_main, return &manager.InitializeChainstate(GetRandHash()));
6158
chainstates.push_back(&c2);
6259
c2.InitCoinsDB(
6360
/* cache_size_bytes */ 1 << 23, /* in_memory */ true, /* should_wipe */ false);
@@ -115,9 +112,7 @@ BOOST_AUTO_TEST_CASE(chainstatemanager_rebalance_caches)
115112

116113
// Create a legacy (IBD) chainstate.
117114
//
118-
ENTER_CRITICAL_SECTION(cs_main);
119-
CChainState& c1 = manager.InitializeChainstate();
120-
LEAVE_CRITICAL_SECTION(cs_main);
115+
CChainState& c1 = *WITH_LOCK(cs_main, return &manager.InitializeChainstate());
121116
chainstates.push_back(&c1);
122117
c1.InitCoinsDB(
123118
/* cache_size_bytes */ 1 << 23, /* in_memory */ true, /* should_wipe */ false);
@@ -134,9 +129,7 @@ BOOST_AUTO_TEST_CASE(chainstatemanager_rebalance_caches)
134129

135130
// Create a snapshot-based chainstate.
136131
//
137-
ENTER_CRITICAL_SECTION(cs_main);
138-
CChainState& c2 = manager.InitializeChainstate(GetRandHash());
139-
LEAVE_CRITICAL_SECTION(cs_main);
132+
CChainState& c2 = *WITH_LOCK(cs_main, return &manager.InitializeChainstate(GetRandHash()));
140133
chainstates.push_back(&c2);
141134
c2.InitCoinsDB(
142135
/* 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
@@ -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) {
@@ -4587,13 +4587,13 @@ void CChainState::UnloadBlockIndex() {
45874587
// May NOT be used after any connections are up as much
45884588
// of the peer-processing logic assumes a consistent
45894589
// block index state
4590-
void UnloadBlockIndex()
4590+
void UnloadBlockIndex(CTxMemPool* mempool)
45914591
{
45924592
LOCK(cs_main);
45934593
g_chainman.Unload();
45944594
pindexBestInvalid = nullptr;
45954595
pindexBestHeader = nullptr;
4596-
mempool.clear();
4596+
if (mempool) mempool->clear();
45974597
vinfoBlockFile.clear();
45984598
nLastBlockFile = 0;
45994599
setDirtyBlockIndex.clear();

src/validation.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ void LoadExternalBlockFile(const CChainParams& chainparams, FILE* fileIn, FlatFi
160160
/** Ensures we have a genesis block in the block tree, possibly writing one to disk. */
161161
bool LoadGenesisBlock(const CChainParams& chainparams);
162162
/** Unload database information */
163-
void UnloadBlockIndex();
163+
void UnloadBlockIndex(CTxMemPool* mempool);
164164
/** Run an instance of the script checking thread */
165165
void ThreadScriptCheck(int worker_num);
166166
/**
@@ -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)