Skip to content

Commit 9a5a5a3

Browse files
committed
test/setup: Use LoadChainstate
This commit coalesces the chainstate loading sequence between our unit test and non-unit test init codepaths.
1 parent c541da0 commit 9a5a5a3

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

src/test/util/setup_common.cpp

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,16 @@
1717
#include <net_processing.h>
1818
#include <node/miner.h>
1919
#include <noui.h>
20+
#include <node/blockstorage.h> // for fReindex, fPruneMode
21+
#include <node/chainstate.h> // for LoadChainstate
2022
#include <policy/fees.h>
2123
#include <pow.h>
2224
#include <rpc/blockchain.h>
2325
#include <rpc/register.h>
2426
#include <rpc/server.h>
2527
#include <scheduler.h>
2628
#include <script/sigcache.h>
29+
#include <shutdown.h> // for ShutdownRequested
2730
#include <streams.h>
2831
#include <txdb.h>
2932
#include <util/strencodings.h>
@@ -143,8 +146,10 @@ ChainTestingSetup::ChainTestingSetup(const std::string& chainName, const std::ve
143146
m_node.fee_estimator = std::make_unique<CBlockPolicyEstimator>();
144147
m_node.mempool = std::make_unique<CTxMemPool>(m_node.fee_estimator.get(), 1);
145148

149+
m_cache_sizes = CalculateCacheSizes(m_args);
150+
146151
m_node.chainman = std::make_unique<ChainstateManager>();
147-
m_node.chainman->m_blockman.m_block_tree_db = std::make_unique<CBlockTreeDB>(1 << 20, true);
152+
m_node.chainman->m_blockman.m_block_tree_db = std::make_unique<CBlockTreeDB>(m_cache_sizes.block_tree_db, true);
148153

149154
// Start script-checking threads. Set g_parallel_script_checks to true so they are used.
150155
constexpr int script_check_threads = 2;
@@ -177,15 +182,18 @@ TestingSetup::TestingSetup(const std::string& chainName, const std::vector<const
177182
// instead of unit tests, but for now we need these here.
178183
RegisterAllCoreRPCCommands(tableRPC);
179184

180-
m_node.chainman->InitializeChainstate(m_node.mempool.get());
181-
m_node.chainman->ActiveChainstate().InitCoinsDB(
182-
/*cache_size_bytes=*/1 << 23, /*in_memory=*/true, /*should_wipe=*/false);
183-
assert(!m_node.chainman->ActiveChainstate().CanFlushToDisk());
184-
m_node.chainman->ActiveChainstate().InitCoinsCache(1 << 23);
185-
assert(m_node.chainman->ActiveChainstate().CanFlushToDisk());
186-
if (!m_node.chainman->ActiveChainstate().LoadGenesisBlock()) {
187-
throw std::runtime_error("LoadGenesisBlock failed.");
188-
}
185+
auto rv = LoadChainstate(fReindex.load(),
186+
*Assert(m_node.chainman.get()),
187+
Assert(m_node.mempool.get()),
188+
fPruneMode,
189+
chainparams.GetConsensus(),
190+
m_args.GetBoolArg("-reindex-chainstate", false),
191+
m_cache_sizes.block_tree_db,
192+
m_cache_sizes.coins_db,
193+
m_cache_sizes.coins,
194+
true,
195+
true);
196+
assert(!rv.has_value());
189197

190198
BlockValidationState state;
191199
if (!m_node.chainman->ActiveChainstate().ActivateBestChain(state)) {

src/test/util/setup_common.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <fs.h>
1010
#include <key.h>
1111
#include <util/system.h>
12+
#include <node/caches.h>
1213
#include <node/context.h>
1314
#include <pubkey.h>
1415
#include <random.h>
@@ -89,6 +90,7 @@ struct BasicTestingSetup {
8990
* initialization behaviour.
9091
*/
9192
struct ChainTestingSetup : public BasicTestingSetup {
93+
CacheSizes m_cache_sizes{};
9294

9395
explicit ChainTestingSetup(const std::string& chainName = CBaseChainParams::MAIN, const std::vector<const char*>& extra_args = {});
9496
~ChainTestingSetup();

0 commit comments

Comments
 (0)