Skip to content

Commit c0224bc

Browse files
author
MarcoFalke
committed
Merge bitcoin/bitcoin#22415: Make m_mempool optional in CChainState
ceb7b35 refactor: move UpdateTip into CChainState (James O'Beirne) 4abf077 refactor: no mempool arg to GetCoinsCacheSizeState (James O'Beirne) 46e3efd refactor: move UpdateMempoolForReorg into CChainState (James O'Beirne) 6176617 validation: make CChainState::m_mempool optional (James O'Beirne) Pull request description: Make `CChainState::m_mempool` optional by making it a pointer instead of a reference. This will allow a simplification to assumeutxo semantics (see bitcoin/bitcoin#15606 (review)) and help facilitate the `-nomempool` option. ACKs for top commit: jnewbery: ACK ceb7b35 naumenkogs: ACK ceb7b35 ryanofsky: Code review ACK ceb7b35 (just minor style and test tweaks since last review) lsilva01: Code review ACK and tested on Signet ACK bitcoin/bitcoin@ceb7b35 MarcoFalke: review ACK ceb7b35 😌 Tree-SHA512: cc445ad33439d5918cacf80a6354eea8f3d33bb7719573ed5b970fad1a0dab410bcd70be44c862b8aba1b71263b82d79876688c553e339362653dfb3d8ec81e6
2 parents 97153a7 + ceb7b35 commit c0224bc

File tree

7 files changed

+118
-86
lines changed

7 files changed

+118
-86
lines changed

src/init.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1349,7 +1349,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
13491349
const int64_t load_block_index_start_time = GetTimeMillis();
13501350
try {
13511351
LOCK(cs_main);
1352-
chainman.InitializeChainstate(*Assert(node.mempool));
1352+
chainman.InitializeChainstate(Assert(node.mempool.get()));
13531353
chainman.m_total_coinstip_cache = nCoinCacheUsage;
13541354
chainman.m_total_coinsdb_cache = nCoinDBCache;
13551355

src/test/util/setup_common.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ TestingSetup::TestingSetup(const std::string& chainName, const std::vector<const
180180
// instead of unit tests, but for now we need these here.
181181
RegisterAllCoreRPCCommands(tableRPC);
182182

183-
m_node.chainman->InitializeChainstate(*m_node.mempool);
183+
m_node.chainman->InitializeChainstate(m_node.mempool.get());
184184
m_node.chainman->ActiveChainstate().InitCoinsDB(
185185
/* cache_size_bytes */ 1 << 23, /* in_memory */ true, /* should_wipe */ false);
186186
assert(!m_node.chainman->ActiveChainstate().CanFlushToDisk());

src/test/validation_chainstate_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ BOOST_AUTO_TEST_CASE(validation_chainstate_resize_caches)
3535
return outp;
3636
};
3737

38-
CChainState& c1 = WITH_LOCK(cs_main, return manager.InitializeChainstate(mempool));
38+
CChainState& c1 = WITH_LOCK(cs_main, return manager.InitializeChainstate(&mempool));
3939
c1.InitCoinsDB(
4040
/* cache_size_bytes */ 1 << 23, /* in_memory */ true, /* should_wipe */ false);
4141
WITH_LOCK(::cs_main, c1.InitCoinsCache(1 << 23));

src/test/validation_chainstatemanager_tests.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ BOOST_AUTO_TEST_CASE(chainstatemanager)
3636

3737
// Create a legacy (IBD) chainstate.
3838
//
39-
CChainState& c1 = WITH_LOCK(::cs_main, return manager.InitializeChainstate(mempool));
39+
CChainState& c1 = WITH_LOCK(::cs_main, return manager.InitializeChainstate(&mempool));
4040
chainstates.push_back(&c1);
4141
c1.InitCoinsDB(
4242
/* cache_size_bytes */ 1 << 23, /* in_memory */ true, /* should_wipe */ false);
@@ -66,7 +66,7 @@ BOOST_AUTO_TEST_CASE(chainstatemanager)
6666
//
6767
const uint256 snapshot_blockhash = GetRandHash();
6868
CChainState& c2 = WITH_LOCK(::cs_main, return manager.InitializeChainstate(
69-
mempool, snapshot_blockhash));
69+
&mempool, snapshot_blockhash));
7070
chainstates.push_back(&c2);
7171

7272
BOOST_CHECK_EQUAL(manager.SnapshotBlockhash().value(), snapshot_blockhash);
@@ -129,7 +129,7 @@ BOOST_AUTO_TEST_CASE(chainstatemanager_rebalance_caches)
129129

130130
// Create a legacy (IBD) chainstate.
131131
//
132-
CChainState& c1 = WITH_LOCK(cs_main, return manager.InitializeChainstate(mempool));
132+
CChainState& c1 = WITH_LOCK(cs_main, return manager.InitializeChainstate(&mempool));
133133
chainstates.push_back(&c1);
134134
c1.InitCoinsDB(
135135
/* cache_size_bytes */ 1 << 23, /* in_memory */ true, /* should_wipe */ false);
@@ -147,7 +147,7 @@ BOOST_AUTO_TEST_CASE(chainstatemanager_rebalance_caches)
147147

148148
// Create a snapshot-based chainstate.
149149
//
150-
CChainState& c2 = WITH_LOCK(cs_main, return manager.InitializeChainstate(mempool, GetRandHash()));
150+
CChainState& c2 = WITH_LOCK(cs_main, return manager.InitializeChainstate(&mempool, GetRandHash()));
151151
chainstates.push_back(&c2);
152152
c2.InitCoinsDB(
153153
/* cache_size_bytes */ 1 << 23, /* in_memory */ true, /* should_wipe */ false);

src/test/validation_flush_tests.cpp

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@ BOOST_AUTO_TEST_CASE(getcoinscachesizestate)
2020
{
2121
CTxMemPool mempool;
2222
BlockManager blockman{};
23-
CChainState chainstate{mempool, blockman};
23+
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

0 commit comments

Comments
 (0)