Skip to content

Commit

Permalink
kernel: remove kernel_BlockManagerOptions
Browse files Browse the repository at this point in the history
Instead, expose it via kernel_ChainstateManagerOptions
  • Loading branch information
stickies-v committed Feb 6, 2025
1 parent caf693b commit e130acf
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 175 deletions.
6 changes: 2 additions & 4 deletions src/bitcoin-chainstate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,11 @@ int main(int argc, char* argv[])
Context context{options};
assert(context);

ChainstateManagerOptions chainman_opts{context, abs_datadir.string()};
ChainstateManagerOptions chainman_opts{context, abs_datadir.string(), (abs_datadir / "blocks").string()};
assert(chainman_opts);
chainman_opts.SetWorkerThreads(4);
BlockManagerOptions blockman_opts{context, abs_datadir.string(), (abs_datadir / "blocks").string()};
assert(blockman_opts);

auto chainman{std::make_unique<ChainMan>(context, chainman_opts, blockman_opts)};
auto chainman{std::make_unique<ChainMan>(context, chainman_opts)};
if (!*chainman) {
return 1;
}
Expand Down
85 changes: 35 additions & 50 deletions src/kernel/bitcoinkernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,19 +275,31 @@ class Context
struct ChainstateManagerOptionsWrapper {
ChainstateManager::Options* chainman_options;
node::ChainstateLoadOptions* chainstate_load_options;
node::BlockManager::Options* blockman_options;

ChainstateManagerOptionsWrapper(const Context* context, const fs::path& data_dir)
ChainstateManagerOptionsWrapper(const Context* context, const fs::path& data_dir, const fs::path& blocks_dir)
: chainman_options{new ChainstateManager::Options{
.chainparams = *context->m_chainparams,
.datadir = data_dir,
.notifications = *context->m_notifications,
.signals = context->m_signals.get()}},
chainstate_load_options{new node::ChainstateLoadOptions{}} {}
chainstate_load_options{new node::ChainstateLoadOptions{}},
blockman_options{new node::BlockManager::Options{
.chainparams = *context->m_chainparams,
.blocks_dir = blocks_dir,
.notifications = *context->m_notifications,
.block_tree_db_params = DBParams{
.path = data_dir / "blocks" / "index",
.cache_bytes = kernel::CacheSizes{DEFAULT_KERNEL_CACHE}.block_tree_db,
}}}
{
}

~ChainstateManagerOptionsWrapper()
{
delete chainman_options;
delete chainstate_load_options;
delete blockman_options;
}

// Prevent copying
Expand Down Expand Up @@ -355,16 +367,16 @@ ChainstateManager::Options* cast_chainstate_manager_options(kernel_ChainstateMan
return reinterpret_cast<ChainstateManagerOptionsWrapper*>(options)->chainman_options;
}

const node::BlockManager::Options* cast_const_block_manager_options(const kernel_BlockManagerOptions* options)
const node::BlockManager::Options* cast_const_block_manager_options(const kernel_ChainstateManagerOptions* options)
{
assert(options);
return reinterpret_cast<const node::BlockManager::Options*>(options);
return reinterpret_cast<const ChainstateManagerOptionsWrapper*>(options)->blockman_options;
}

node::BlockManager::Options* cast_block_manager_options(kernel_BlockManagerOptions* options)
node::BlockManager::Options* cast_block_manager_options(kernel_ChainstateManagerOptions* options)
{
assert(options);
return reinterpret_cast<node::BlockManager::Options*>(options);
return reinterpret_cast<ChainstateManagerOptionsWrapper*>(options)->blockman_options;
}

ChainstateManager* cast_chainstate_manager(kernel_ChainstateManager* chainman)
Expand Down Expand Up @@ -733,16 +745,23 @@ kernel_BlockValidationResult kernel_get_block_validation_result_from_block_valid
assert(false);
}

kernel_ChainstateManagerOptions* kernel_chainstate_manager_options_create(const kernel_Context* context_, const char* data_dir, size_t data_dir_len)
kernel_ChainstateManagerOptions* kernel_chainstate_manager_options_create(
const kernel_Context* context_,
const char* data_dir,
size_t data_dir_len,
const char* blocks_dir,
size_t blocks_dir_len)
{
try {
fs::path abs_blocks_dir{fs::absolute(fs::PathFromString({blocks_dir, blocks_dir_len}))};
fs::create_directories(abs_blocks_dir);
fs::path abs_data_dir{fs::absolute(fs::PathFromString({data_dir, data_dir_len}))};
fs::create_directories(abs_data_dir);
auto context{cast_const_context(context_)};
if (!context) {
return nullptr;
}
auto wrapper = new ChainstateManagerOptionsWrapper(context, abs_data_dir);
auto wrapper = new ChainstateManagerOptionsWrapper(context, abs_data_dir, abs_blocks_dir);
return reinterpret_cast<kernel_ChainstateManagerOptions*>(wrapper);
} catch (const std::exception& e) {
LogError("Failed to create chainstate manager options: %s", e.what());
Expand Down Expand Up @@ -779,62 +798,28 @@ void kernel_chainstate_manager_options_destroy(kernel_ChainstateManagerOptions*
}
}

kernel_BlockManagerOptions* kernel_block_manager_options_create(const kernel_Context* context_, const char* data_dir, size_t data_dir_len, const char* blocks_dir, size_t blocks_dir_len)
{
try {
fs::path abs_blocks_dir{fs::absolute(fs::PathFromString({blocks_dir, blocks_dir_len}))};
fs::create_directories(abs_blocks_dir);
fs::path abs_data_dir{fs::absolute(fs::PathFromString({data_dir, data_dir_len}))};
fs::create_directories(abs_data_dir);
auto context{cast_const_context(context_)};
if (!context) {
return nullptr;
}
kernel::CacheSizes cache_sizes{DEFAULT_KERNEL_CACHE};
return reinterpret_cast<kernel_BlockManagerOptions*>(new node::BlockManager::Options{
.chainparams = *context->m_chainparams,
.blocks_dir = abs_blocks_dir,
.notifications = *context->m_notifications,
.block_tree_db_params = DBParams{
.path = abs_data_dir / "blocks" / "index",
.cache_bytes = cache_sizes.block_tree_db,
}});
} catch (const std::exception& e) {
LogError("Failed to create block manager options; %s", e.what());
return nullptr;
}
}

void kernel_block_manager_options_destroy(kernel_BlockManagerOptions* options)
{
if (options) {
delete cast_const_block_manager_options(options);
}
}

void kernel_block_manager_options_set_wipe_block_tree_db(
kernel_BlockManagerOptions* block_manager_options_,
void 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(block_manager_options_)};
auto block_manager_options{cast_block_manager_options(chainstate_manager_opts_)};
block_manager_options->block_tree_db_params.wipe_data = wipe_block_tree_db;
}

void kernel_block_manager_options_set_block_tree_db_in_memory(
kernel_BlockManagerOptions* chainstate_load_opts_,
void kernel_chainstate_manager_options_set_block_tree_db_in_memory(
kernel_ChainstateManagerOptions* chainstate_manager_opts_,
bool block_tree_db_in_memory)
{
auto block_manager_options{cast_block_manager_options(chainstate_load_opts_)};
auto block_manager_options{cast_block_manager_options(chainstate_manager_opts_)};
block_manager_options->block_tree_db_params.memory_only = block_tree_db_in_memory;
}

kernel_ChainstateManager* kernel_chainstate_manager_create(
const kernel_Context* context_,
const kernel_ChainstateManagerOptions* chainman_opts_,
const kernel_BlockManagerOptions* blockman_opts_)
const kernel_ChainstateManagerOptions* chainman_opts_)
{
auto chainman_opts{cast_const_chainstate_manager_options(chainman_opts_)};
auto blockman_opts{cast_const_block_manager_options(blockman_opts_)};
auto blockman_opts{cast_const_block_manager_options(chainman_opts_)};
auto context{cast_const_context(context_)};

ChainstateManager* chainman;
Expand Down
97 changes: 25 additions & 72 deletions src/kernel/bitcoinkernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,15 +183,6 @@ typedef struct kernel_BlockIndex kernel_BlockIndex;
*/
typedef struct kernel_ChainstateManagerOptions kernel_ChainstateManagerOptions;

/**
* Opaque data structure for holding options for creating a new chainstate
* manager.
*
* The chainstate manager has an internal block manager that takes its own set
* of parameters. It is initialized with default options.
*/
typedef struct kernel_BlockManagerOptions kernel_BlockManagerOptions;

/**
* Opaque data structure for holding a chainstate manager.
*
Expand Down Expand Up @@ -770,8 +761,30 @@ BITCOINKERNEL_API void kernel_context_destroy(kernel_Context* context);
BITCOINKERNEL_API kernel_ChainstateManagerOptions* BITCOINKERNEL_WARN_UNUSED_RESULT kernel_chainstate_manager_options_create(
const kernel_Context* context,
const char* data_directory,
size_t data_directory_len
) BITCOINKERNEL_ARG_NONNULL(1, 2);
size_t data_directory_len,
const char* blocks_directory,
size_t blocks_directory_len) BITCOINKERNEL_ARG_NONNULL(1, 2);

/**
* @brief Sets wipe block tree db in the chainstate manager options.
*
* @param[in] chainstate_manager_options Non-null, options to be set.
* @param[in] wipe_block_tree_db Set wipe block tree db.
*/
BITCOINKERNEL_API void kernel_chainstate_manager_options_set_wipe_block_tree_db(
kernel_ChainstateManagerOptions* chainstate_manager_options,
bool wipe_block_tree_db) BITCOINKERNEL_ARG_NONNULL(1);

/**
* @brief Sets block tree db in memory in the chainstate manager options.
*
* @param[in] chainstate_manager_options Non-null, options to be set.
* @param[in] block_tree_db_in_memory Set block tree db in memory.
*/

BITCOINKERNEL_API void kernel_chainstate_manager_options_set_block_tree_db_in_memory(
kernel_ChainstateManagerOptions* chainstate_manager_options,
bool block_tree_db_in_memory) BITCOINKERNEL_ARG_NONNULL(1);

/**
* @brief Sets wipe chainstate db in the chainstate manager options.
Expand Down Expand Up @@ -811,64 +824,6 @@ BITCOINKERNEL_API void kernel_chainstate_manager_options_set_worker_threads_num(
*/
BITCOINKERNEL_API void kernel_chainstate_manager_options_destroy(kernel_ChainstateManagerOptions* chainstate_manager_options);

///@}

/** @name BlockManagerOptions
* Functions for working with block manager options.
*/
///@{

/**
* @brief Create options for the block manager. The block manager is used
* internally by the chainstate manager for block storage and indexing.
*
* @param[in] context Non-null, the created options will associate with this kernel context
* for the duration of their lifetime. The same context needs to be used
* when instantiating the chainstate manager.
* @param[in] data_directory Non-null, path string of the directory containing the chainstate data.
* This is usually the same as the data directory used for the chainstate
* manager options. If the directory does not exist yet, it will be created.
* @param[in] blocks_directory Non-null, path string of the directory containing the block data. If
* the directory does not exist yet, it will be created.
* @return The allocated block manager options, or null on error.
*/
BITCOINKERNEL_API kernel_BlockManagerOptions* BITCOINKERNEL_WARN_UNUSED_RESULT kernel_block_manager_options_create(
const kernel_Context* context,
const char* data_directory,
size_t data_directory_len,
const char* blocks_directory,
size_t blocks_directory_len
) BITCOINKERNEL_ARG_NONNULL(1, 2);

/**
* @brief Sets wipe block tree db in the block manager options.
*
* @param[in] block_manager_options Non-null, created by @ref kernel_block_manager_options_create.
* @param[in] wipe_block_tree_db Set wipe block tree db.
*/
BITCOINKERNEL_API void kernel_block_manager_options_set_wipe_block_tree_db(
kernel_BlockManagerOptions* block_manager_options,
bool wipe_block_tree_db
) BITCOINKERNEL_ARG_NONNULL(1);

/**
* @brief Sets block tree db in memory in the block manager options.
*
* @param[in] block_manager_options Non-null, created by @ref kernel_block_manager_options_create.
* @param[in] block_tree_db_in_memory Set block tree db in memory.
*/
BITCOINKERNEL_API void kernel_block_manager_options_set_block_tree_db_in_memory(
kernel_BlockManagerOptions* block_manager_options,
bool block_tree_db_in_memory
) BITCOINKERNEL_ARG_NONNULL(1);

/**
* Destroy the block manager options.
*/
BITCOINKERNEL_API void kernel_block_manager_options_destroy(kernel_BlockManagerOptions* block_manager_options);

///@}

/** @name ChainstateManager
* Functions for chainstate management.
*/
Expand All @@ -881,16 +836,14 @@ BITCOINKERNEL_API void kernel_block_manager_options_destroy(kernel_BlockManagerO
* the passed in context also remains in memory.
*
* @param[in] chainstate_manager_options Non-null, created by @ref kernel_chainstate_manager_options_create.
* @param[in] block_manager_options Non-null, created by @ref kernel_block_manager_options_create.
* @param[in] context Non-null, the created chainstate manager will associate with this
* kernel context for the duration of its lifetime. The same context
* needs to be used for later interactions with the chainstate manager.
* @return The allocated chainstate manager, or null on error.
*/
BITCOINKERNEL_API kernel_ChainstateManager* BITCOINKERNEL_WARN_UNUSED_RESULT kernel_chainstate_manager_create(
const kernel_Context* context,
const kernel_ChainstateManagerOptions* chainstate_manager_options,
const kernel_BlockManagerOptions* block_manager_options) BITCOINKERNEL_ARG_NONNULL(1, 2, 3);
const kernel_ChainstateManagerOptions* chainstate_manager_options) BITCOINKERNEL_ARG_NONNULL(1, 2);

/**
* @brief May be called after kernel_chainstate_manager_load_chainstate to
Expand Down
56 changes: 17 additions & 39 deletions src/kernel/bitcoinkernel_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -393,9 +393,22 @@ class ChainstateManagerOptions
std::unique_ptr<kernel_ChainstateManagerOptions, Deleter> m_options;

public:
ChainstateManagerOptions(const Context& context, const std::string& data_dir) noexcept
: m_options{kernel_chainstate_manager_options_create(context.m_context.get(), data_dir.c_str(), data_dir.length())}
ChainstateManagerOptions(const Context& context, const std::string& data_dir, const std::string& blocks_dir) noexcept
: m_options{kernel_chainstate_manager_options_create(
context.m_context.get(),
data_dir.c_str(), data_dir.length(),
blocks_dir.c_str(), blocks_dir.length())}
{
}

void SetWipeBlockTreeDb(bool wipe_block_tree) const noexcept
{
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
Expand All @@ -419,40 +432,6 @@ class ChainstateManagerOptions
friend class ChainMan;
};

class BlockManagerOptions
{
private:
struct Deleter {
void operator()(kernel_BlockManagerOptions* ptr) const
{
kernel_block_manager_options_destroy(ptr);
}
};

std::unique_ptr<kernel_BlockManagerOptions, Deleter> m_options;

public:
BlockManagerOptions(const Context& context, const std::string& data_dir, const std::string& blocks_dir) noexcept
: m_options{kernel_block_manager_options_create(context.m_context.get(), data_dir.c_str(), data_dir.length(), blocks_dir.c_str(), blocks_dir.length())}
{
}

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

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

/** Check whether this BlockManagerOptions object is valid. */
explicit operator bool() const noexcept { return bool{m_options}; }

friend class ChainMan;
};

class Block
{
private:
Expand Down Expand Up @@ -585,11 +564,10 @@ class ChainMan
const Context& m_context;

public:
ChainMan(const Context& context, const ChainstateManagerOptions& chainman_opts, const BlockManagerOptions& blockman_opts) noexcept
ChainMan(const Context& context, const ChainstateManagerOptions& chainman_opts) noexcept
: m_chainman{kernel_chainstate_manager_create(
context.m_context.get(),
chainman_opts.m_options.get(),
blockman_opts.m_options.get())},
chainman_opts.m_options.get())},
m_context{context}
{
}
Expand Down
Loading

0 comments on commit e130acf

Please sign in to comment.