Skip to content

Commit 51fc924

Browse files
committed
test: allow on-disk coins and block tree dbs in tests
Used when testing cleanup of on-disk chainstate data for snapshot testcases. Also necessary for simulating node restart in .cpp tests.
1 parent 3c36139 commit 51fc924

File tree

3 files changed

+35
-11
lines changed

3 files changed

+35
-11
lines changed

src/test/util/chainstate.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ const auto NoMalleation = [](AutoFile& file, node::SnapshotMetadata& meta){};
3030
*/
3131
template<typename F = decltype(NoMalleation)>
3232
static bool
33-
CreateAndActivateUTXOSnapshot(TestingSetup* fixture, F malleation = NoMalleation, bool reset_chainstate = false)
33+
CreateAndActivateUTXOSnapshot(
34+
TestingSetup* fixture,
35+
F malleation = NoMalleation,
36+
bool reset_chainstate = false,
37+
bool in_memory_chainstate = false)
3438
{
3539
node::NodeContext& node = fixture->m_node;
3640
fs::path root = fixture->m_path_root;
@@ -88,7 +92,7 @@ CreateAndActivateUTXOSnapshot(TestingSetup* fixture, F malleation = NoMalleation
8892
0 == WITH_LOCK(node.chainman->GetMutex(), return node.chainman->ActiveHeight()));
8993
}
9094

91-
return node.chainman->ActivateSnapshot(auto_infile, metadata, /*in_memory=*/true);
95+
return node.chainman->ActivateSnapshot(auto_infile, metadata, in_memory_chainstate);
9296
}
9397

9498

src/test/util/setup_common.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -220,17 +220,23 @@ ChainTestingSetup::~ChainTestingSetup()
220220
m_node.chainman.reset();
221221
}
222222

223-
TestingSetup::TestingSetup(const std::string& chainName, const std::vector<const char*>& extra_args)
224-
: ChainTestingSetup(chainName, extra_args)
223+
TestingSetup::TestingSetup(
224+
const std::string& chainName,
225+
const std::vector<const char*>& extra_args,
226+
const bool coins_db_in_memory,
227+
const bool block_tree_db_in_memory)
228+
: ChainTestingSetup(chainName, extra_args),
229+
m_coins_db_in_memory(coins_db_in_memory),
230+
m_block_tree_db_in_memory(block_tree_db_in_memory)
225231
{
226232
// Ideally we'd move all the RPC tests to the functional testing framework
227233
// instead of unit tests, but for now we need these here.
228234
RegisterAllCoreRPCCommands(tableRPC);
229235

230236
node::ChainstateLoadOptions options;
231237
options.mempool = Assert(m_node.mempool.get());
232-
options.block_tree_db_in_memory = true;
233-
options.coins_db_in_memory = true;
238+
options.block_tree_db_in_memory = m_block_tree_db_in_memory;
239+
options.coins_db_in_memory = m_coins_db_in_memory;
234240
options.reindex = node::fReindex;
235241
options.reindex_chainstate = m_args.GetBoolArg("-reindex-chainstate", false);
236242
options.prune = node::fPruneMode;
@@ -263,8 +269,12 @@ TestingSetup::TestingSetup(const std::string& chainName, const std::vector<const
263269
}
264270
}
265271

266-
TestChain100Setup::TestChain100Setup(const std::string& chain_name, const std::vector<const char*>& extra_args)
267-
: TestingSetup{chain_name, extra_args}
272+
TestChain100Setup::TestChain100Setup(
273+
const std::string& chain_name,
274+
const std::vector<const char*>& extra_args,
275+
const bool coins_db_in_memory,
276+
const bool block_tree_db_in_memory)
277+
: TestingSetup{CBaseChainParams::REGTEST, extra_args, coins_db_in_memory, block_tree_db_in_memory}
268278
{
269279
SetMockTime(1598887952);
270280
constexpr std::array<unsigned char, 32> vchKey = {

src/test/util/setup_common.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,14 @@ struct ChainTestingSetup : public BasicTestingSetup {
107107
/** Testing setup that configures a complete environment.
108108
*/
109109
struct TestingSetup : public ChainTestingSetup {
110-
explicit TestingSetup(const std::string& chainName = CBaseChainParams::MAIN, const std::vector<const char*>& extra_args = {});
110+
bool m_coins_db_in_memory{true};
111+
bool m_block_tree_db_in_memory{true};
112+
113+
explicit TestingSetup(
114+
const std::string& chainName = CBaseChainParams::MAIN,
115+
const std::vector<const char*>& extra_args = {},
116+
const bool coins_db_in_memory = true,
117+
const bool block_tree_db_in_memory = true);
111118
};
112119

113120
/** Identical to TestingSetup, but chain set to regtest */
@@ -124,8 +131,11 @@ class CScript;
124131
* Testing fixture that pre-creates a 100-block REGTEST-mode block chain
125132
*/
126133
struct TestChain100Setup : public TestingSetup {
127-
TestChain100Setup(const std::string& chain_name = CBaseChainParams::REGTEST,
128-
const std::vector<const char*>& extra_args = {});
134+
TestChain100Setup(
135+
const std::string& chain_name = CBaseChainParams::REGTEST,
136+
const std::vector<const char*>& extra_args = {},
137+
const bool coins_db_in_memory = true,
138+
const bool block_tree_db_in_memory = true);
129139

130140
/**
131141
* Create a new block with just given transactions, coinbase paying to

0 commit comments

Comments
 (0)