Skip to content

Commit

Permalink
kernel: disallow invalid wiping option combination
Browse files Browse the repository at this point in the history
Instead of failing the chainstate_manager_create call, disallow
setting the set_wipe_block_tree_db and set_wipe_chainstate_db
options to invalid combinations. This improves user error feedback,
removing the need to inspect the logs when creating a
ChainstateManager fails.
  • Loading branch information
stickies-v committed Feb 6, 2025
1 parent e130acf commit eb783d8
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 11 deletions.
16 changes: 14 additions & 2 deletions src/kernel/bitcoinkernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -769,12 +769,18 @@ kernel_ChainstateManagerOptions* kernel_chainstate_manager_options_create(
}
}

void kernel_chainstate_manager_options_set_wipe_chainstate_db(
bool kernel_chainstate_manager_options_set_wipe_chainstate_db(
kernel_ChainstateManagerOptions* chainstate_manager_opts_,
bool wipe_chainstate_db)
{
auto chainstate_load_opts{cast_chainstate_load_options(chainstate_manager_opts_)};
auto block_manager_opts{cast_block_manager_options(chainstate_manager_opts_)};
if (block_manager_opts->block_tree_db_params.wipe_data && !wipe_chainstate_db) {
LogWarning("Wiping the block tree db without also wiping the chainstate db is currently unsupported.");
return false;
}
chainstate_load_opts->wipe_chainstate_db = wipe_chainstate_db;
return true;
}

void kernel_chainstate_manager_options_set_chainstate_db_in_memory(
Expand All @@ -798,12 +804,18 @@ void kernel_chainstate_manager_options_destroy(kernel_ChainstateManagerOptions*
}
}

void kernel_chainstate_manager_options_set_wipe_block_tree_db(
bool kernel_chainstate_manager_options_set_wipe_block_tree_db(
kernel_ChainstateManagerOptions* chainstate_manager_opts_,
bool wipe_block_tree_db)
{
auto block_manager_options{cast_block_manager_options(chainstate_manager_opts_)};
auto chainstate_load_opts{cast_chainstate_load_options(chainstate_manager_opts_)};
if (wipe_block_tree_db && !chainstate_load_opts->wipe_chainstate_db) {
LogWarning("Wiping the block tree db without also wiping the chainstate db is currently unsupported.");
return false;
}
block_manager_options->block_tree_db_params.wipe_data = wipe_block_tree_db;
return true;
}

void kernel_chainstate_manager_options_set_block_tree_db_in_memory(
Expand Down
13 changes: 11 additions & 2 deletions src/kernel/bitcoinkernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -767,11 +767,16 @@ BITCOINKERNEL_API kernel_ChainstateManagerOptions* BITCOINKERNEL_WARN_UNUSED_RES

/**
* @brief Sets wipe block tree db in the chainstate manager options.
* If wipe_chainstate_db is set to False, this option may not be
* set to True.
*
* @param[in] chainstate_manager_options Non-null, options to be set.
* @param[in] wipe_block_tree_db Set wipe block tree db.
*
* @return True if the set was successful, False if the set failed
* due to wipe_chainstate_db incompatibility.
*/
BITCOINKERNEL_API void kernel_chainstate_manager_options_set_wipe_block_tree_db(
BITCOINKERNEL_API bool BITCOINKERNEL_WARN_UNUSED_RESULT kernel_chainstate_manager_options_set_wipe_block_tree_db(
kernel_ChainstateManagerOptions* chainstate_manager_options,
bool wipe_block_tree_db) BITCOINKERNEL_ARG_NONNULL(1);

Expand All @@ -788,11 +793,15 @@ BITCOINKERNEL_API void kernel_chainstate_manager_options_set_block_tree_db_in_me

/**
* @brief Sets wipe chainstate db in the chainstate manager options.
* If wipe_block_tree_db is set to True, this must be set to True.
*
* @param[in] chainstate_manager_options Non-null, options to be set.
* @param[in] wipe_chainstate_db Set wipe chainstate db.
*
* @return True if the set was successful, False if the set failed
* due to wipe_block_tree_db incompatibility.
*/
BITCOINKERNEL_API void kernel_chainstate_manager_options_set_wipe_chainstate_db(
BITCOINKERNEL_API bool BITCOINKERNEL_WARN_UNUSED_RESULT kernel_chainstate_manager_options_set_wipe_chainstate_db(
kernel_ChainstateManagerOptions* chainstate_manager_options,
bool wipe_chainstate_db) BITCOINKERNEL_ARG_NONNULL(1);

Expand Down
8 changes: 4 additions & 4 deletions src/kernel/bitcoinkernel_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -401,19 +401,19 @@ class ChainstateManagerOptions
{
}

void SetWipeBlockTreeDb(bool wipe_block_tree) const noexcept
bool SetWipeBlockTreeDb(bool wipe_block_tree) const noexcept
{
kernel_chainstate_manager_options_set_wipe_block_tree_db(m_options.get(), wipe_block_tree);
return kernel_chainstate_manager_options_set_wipe_block_tree_db(m_options.get(), wipe_block_tree);
}

void SetBlockTreeDbInMemory(bool block_tree_db_in_memory) const noexcept
{
kernel_chainstate_manager_options_set_block_tree_db_in_memory(m_options.get(), block_tree_db_in_memory);
}

void SetWipeChainstateDb(bool wipe_chainstate) const noexcept
bool SetWipeChainstateDb(bool wipe_chainstate) const noexcept
{
kernel_chainstate_manager_options_set_wipe_chainstate_db(m_options.get(), wipe_chainstate);
return kernel_chainstate_manager_options_set_wipe_chainstate_db(m_options.get(), wipe_chainstate);
}

void SetChainstateDbInMemory(bool chainstate_db_in_memory) const noexcept
Expand Down
7 changes: 4 additions & 3 deletions src/test/kernel/test_kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ void chainman_test()

ChainstateManagerOptions chainman_opts{context, test_directory.m_directory.string(), (test_directory.m_directory / "blocks").string()};
assert(chainman_opts);
assert(!chainman_opts.SetWipeBlockTreeDb(true)); // Can't set wipe_block_tree_db to true when wipe_chainstate_db is false
chainman_opts.SetWorkerThreads(4);

ChainMan chainman{context, chainman_opts};
Expand All @@ -392,11 +393,11 @@ std::unique_ptr<ChainMan> create_chainman(TestDirectory& test_directory,
assert(chainman_opts);

if (reindex) {
chainman_opts.SetWipeBlockTreeDb(reindex);
chainman_opts.SetWipeChainstateDb(reindex);
assert(chainman_opts.SetWipeChainstateDb(reindex));
assert(chainman_opts.SetWipeBlockTreeDb(reindex));
}
if (wipe_chainstate) {
chainman_opts.SetWipeChainstateDb(wipe_chainstate);
assert(chainman_opts.SetWipeChainstateDb(wipe_chainstate));
}
if (block_tree_db_in_memory) {
chainman_opts.SetBlockTreeDbInMemory(block_tree_db_in_memory);
Expand Down

0 comments on commit eb783d8

Please sign in to comment.