kernel: Add chainstate load options for in-memory dbs in C header

This allows a user to run the kernel without creating on-disk files for
the block tree and chainstate indexes. This is potentially useful in
scenarios where the user needs to do some ephemeral validation
operations.

One specific use case is when linearizing the blocks on disk. The block
files store blocks out of order, so a program may utilize the library
and its header to read the blocks with one chainstate manager, and then
write them back in order, and without orphans, with another chainstate
maanger. To save disk resources and if the indexes are not required once
done, it may be beneficial to keep the indexes in memory for the
chainstate manager that writes the blocks back again.
This commit is contained in:
TheCharlatan
2024-06-22 21:08:10 +02:00
parent 070e77732c
commit a747ca1f51
4 changed files with 81 additions and 5 deletions

View File

@@ -524,6 +524,8 @@ BOOST_AUTO_TEST_CASE(btck_chainman_tests)
std::unique_ptr<ChainMan> create_chainman(TestDirectory& test_directory,
bool reindex,
bool wipe_chainstate,
bool block_tree_db_in_memory,
bool chainstate_db_in_memory,
Context& context)
{
ChainstateManagerOptions chainman_opts{context, test_directory.m_directory.string(), (test_directory.m_directory / "blocks").string()};
@@ -534,6 +536,12 @@ std::unique_ptr<ChainMan> create_chainman(TestDirectory& test_directory,
if (wipe_chainstate) {
chainman_opts.SetWipeDbs(/*wipe_block_tree=*/false, /*wipe_chainstate=*/wipe_chainstate);
}
if (block_tree_db_in_memory) {
chainman_opts.UpdateBlockTreeDbInMemory(block_tree_db_in_memory);
}
if (chainstate_db_in_memory) {
chainman_opts.UpdateChainstateDbInMemory(chainstate_db_in_memory);
}
auto chainman{std::make_unique<ChainMan>(context, chainman_opts)};
return chainman;
@@ -543,21 +551,21 @@ void chainman_reindex_test(TestDirectory& test_directory)
{
auto notifications{std::make_shared<TestKernelNotifications>()};
auto context{create_context(notifications, ChainType::MAINNET)};
auto chainman{create_chainman(test_directory, true, false, context)};
auto chainman{create_chainman(test_directory, true, false, false, false, context)};
}
void chainman_reindex_chainstate_test(TestDirectory& test_directory)
{
auto notifications{std::make_shared<TestKernelNotifications>()};
auto context{create_context(notifications, ChainType::MAINNET)};
auto chainman{create_chainman(test_directory, false, true, context)};
auto chainman{create_chainman(test_directory, false, true, false, false, context)};
}
void chainman_mainnet_validation_test(TestDirectory& test_directory)
{
auto notifications{std::make_shared<TestKernelNotifications>()};
auto context{create_context(notifications, ChainType::MAINNET)};
auto chainman{create_chainman(test_directory, false, false, context)};
auto chainman{create_chainman(test_directory, false, false, false, false, context)};
{
// Process an invalid block
@@ -601,6 +609,26 @@ BOOST_AUTO_TEST_CASE(btck_chainman_mainnet_tests)
chainman_reindex_chainstate_test(test_directory);
}
BOOST_AUTO_TEST_CASE(btck_chainman_in_memory_tests)
{
auto in_memory_test_directory{TestDirectory{"in-memory_test_bitcoin_kernel"}};
auto notifications{std::make_shared<TestKernelNotifications>()};
auto context{create_context(notifications, ChainType::REGTEST)};
auto chainman{create_chainman(in_memory_test_directory, false, false, true, true, context)};
for (auto& raw_block : REGTEST_BLOCK_DATA) {
Block block{hex_string_to_byte_vec(raw_block)};
bool new_block{false};
chainman->ProcessBlock(block, &new_block);
BOOST_CHECK(new_block);
}
BOOST_CHECK(std::filesystem::exists(in_memory_test_directory.m_directory / "blocks"));
BOOST_CHECK(!std::filesystem::exists(in_memory_test_directory.m_directory / "blocks" / "index"));
BOOST_CHECK(!std::filesystem::exists(in_memory_test_directory.m_directory / "chainstate"));
}
BOOST_AUTO_TEST_CASE(btck_chainman_regtest_tests)
{
auto test_directory{TestDirectory{"regtest_test_bitcoin_kernel"}};
@@ -614,7 +642,7 @@ BOOST_AUTO_TEST_CASE(btck_chainman_regtest_tests)
const size_t mid{REGTEST_BLOCK_DATA.size() / 2};
{
auto chainman{create_chainman(test_directory, false, false, context)};
auto chainman{create_chainman(test_directory, false, false, false, false, context)};
for (size_t i{0}; i < mid; i++) {
Block block{hex_string_to_byte_vec(REGTEST_BLOCK_DATA[i])};
bool new_block{false};
@@ -623,7 +651,7 @@ BOOST_AUTO_TEST_CASE(btck_chainman_regtest_tests)
}
}
auto chainman{create_chainman(test_directory, false, false, context)};
auto chainman{create_chainman(test_directory, false, false, false, false, context)};
for (size_t i{mid}; i < REGTEST_BLOCK_DATA.size(); i++) {
Block block{hex_string_to_byte_vec(REGTEST_BLOCK_DATA[i])};