Skip to content

Commit

Permalink
test/setup: Use LoadChainstate
Browse files Browse the repository at this point in the history
This commit coalesces the chainstate loading sequence between our unit
test and non-unit test init codepaths.
  • Loading branch information
dongcarl committed Dec 7, 2021
1 parent c541da0 commit 9a5a5a3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
28 changes: 18 additions & 10 deletions src/test/util/setup_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@
#include <net_processing.h>
#include <node/miner.h>
#include <noui.h>
#include <node/blockstorage.h> // for fReindex, fPruneMode
#include <node/chainstate.h> // for LoadChainstate
#include <policy/fees.h>
#include <pow.h>
#include <rpc/blockchain.h>
#include <rpc/register.h>
#include <rpc/server.h>
#include <scheduler.h>
#include <script/sigcache.h>
#include <shutdown.h> // for ShutdownRequested
#include <streams.h>
#include <txdb.h>
#include <util/strencodings.h>
Expand Down Expand Up @@ -143,8 +146,10 @@ ChainTestingSetup::ChainTestingSetup(const std::string& chainName, const std::ve
m_node.fee_estimator = std::make_unique<CBlockPolicyEstimator>();
m_node.mempool = std::make_unique<CTxMemPool>(m_node.fee_estimator.get(), 1);

m_cache_sizes = CalculateCacheSizes(m_args);

m_node.chainman = std::make_unique<ChainstateManager>();
m_node.chainman->m_blockman.m_block_tree_db = std::make_unique<CBlockTreeDB>(1 << 20, true);
m_node.chainman->m_blockman.m_block_tree_db = std::make_unique<CBlockTreeDB>(m_cache_sizes.block_tree_db, true);

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

m_node.chainman->InitializeChainstate(m_node.mempool.get());
m_node.chainman->ActiveChainstate().InitCoinsDB(
/*cache_size_bytes=*/1 << 23, /*in_memory=*/true, /*should_wipe=*/false);
assert(!m_node.chainman->ActiveChainstate().CanFlushToDisk());
m_node.chainman->ActiveChainstate().InitCoinsCache(1 << 23);
assert(m_node.chainman->ActiveChainstate().CanFlushToDisk());
if (!m_node.chainman->ActiveChainstate().LoadGenesisBlock()) {
throw std::runtime_error("LoadGenesisBlock failed.");
}
auto rv = LoadChainstate(fReindex.load(),
*Assert(m_node.chainman.get()),
Assert(m_node.mempool.get()),
fPruneMode,
chainparams.GetConsensus(),
m_args.GetBoolArg("-reindex-chainstate", false),
m_cache_sizes.block_tree_db,
m_cache_sizes.coins_db,
m_cache_sizes.coins,
true,
true);
assert(!rv.has_value());

BlockValidationState state;
if (!m_node.chainman->ActiveChainstate().ActivateBestChain(state)) {
Expand Down
2 changes: 2 additions & 0 deletions src/test/util/setup_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <fs.h>
#include <key.h>
#include <util/system.h>
#include <node/caches.h>
#include <node/context.h>
#include <pubkey.h>
#include <random.h>
Expand Down Expand Up @@ -89,6 +90,7 @@ struct BasicTestingSetup {
* initialization behaviour.
*/
struct ChainTestingSetup : public BasicTestingSetup {
CacheSizes m_cache_sizes{};

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

0 comments on commit 9a5a5a3

Please sign in to comment.