From 9f7e1178f8d1ff8fdb721a55be249b03dab6cbce Mon Sep 17 00:00:00 2001 From: TheCharlatan Date: Fri, 13 Dec 2024 12:37:43 +0100 Subject: [PATCH] Add reindexer binary --- src/kernel/CMakeLists.txt | 14 +++++ src/kernel/reindexer.cpp | 110 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 124 insertions(+) create mode 100644 src/kernel/reindexer.cpp diff --git a/src/kernel/CMakeLists.txt b/src/kernel/CMakeLists.txt index 4ec107a51e8732..c59f05359c670c 100644 --- a/src/kernel/CMakeLists.txt +++ b/src/kernel/CMakeLists.txt @@ -158,3 +158,17 @@ target_link_libraries(kernel-bitcoin-chainstate set_target_properties(kernel-bitcoin-chainstate PROPERTIES SKIP_BUILD_RPATH OFF ) + +add_executable(kernel-reindexer + reindexer.cpp +) + +target_link_libraries(kernel-reindexer + PRIVATE + core_interface + bitcoinkernel +) + +set_target_properties(kernel-reindexer PROPERTIES + SKIP_BUILD_RPATH OFF +) diff --git a/src/kernel/reindexer.cpp b/src/kernel/reindexer.cpp new file mode 100644 index 00000000000000..5f88d5c4d4cdc7 --- /dev/null +++ b/src/kernel/reindexer.cpp @@ -0,0 +1,110 @@ +// Copyright (c) 2024-present The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#include "kernel/bitcoinkernel.h" +#include + +#include +#include +#include + +class TestLog +{ +public: + void LogMessage(const char* message) + { + std::cout << "kernel: " << message; + } +}; + +class ReindexKernelNotifications : public KernelNotifications +{ +public: + void FlushErrorHandler(const char* error) override + { + std::cout << error << std::endl; + assert(0); + } + void FatalErrorHandler(const char* error) override + { + std::cout << error << std::endl; + assert(0); + } +}; + +Context create_context(ReindexKernelNotifications& notifications, kernel_ChainType chain_type) +{ + ContextOptions options{}; + ChainParams params{chain_type}; + options.SetChainParams(params); + options.SetNotifications(notifications); + return Context{options}; +} + +void run_reindex(std::filesystem::path path_root, std::filesystem::path path_blocks, Context& context) +{ + ChainstateManagerOptions chainman_opts{context, path_root}; + assert(chainman_opts); + BlockManagerOptions blockman_opts{context, path_blocks}; + assert(blockman_opts); + auto chainman{ChainMan{context, chainman_opts, blockman_opts}}; + assert(chainman); + ChainstateLoadOptions chainstate_load_opts{}; + chainstate_load_opts.SetWipeChainstateDb(true); + chainstate_load_opts.SetWipeBlockTreeDb(true); + assert(chainman.LoadChainstate(chainstate_load_opts)); + chainman.ImportBlocks({}); +} + +std::optional string_to_chain_type(const std::string& chainTypeStr) { + if (chainTypeStr == "mainnet") { + return kernel_CHAIN_TYPE_MAINNET; + } else if (chainTypeStr == "testnet") { + return kernel_CHAIN_TYPE_TESTNET; + } else if (chainTypeStr == "signet") { + return kernel_CHAIN_TYPE_SIGNET; + } else if (chainTypeStr == "regtest") { + return kernel_CHAIN_TYPE_REGTEST; + } else { + return std::nullopt; + } +} + +int main(int argc, char* argv[]) { + if (argc != 3) { + std::cout << "Usage: " << std::endl; + return 1; + } + + std::string data_dir_raw{argv[1]}; + std::cout << data_dir_raw; + std::filesystem::path data_dir{data_dir_raw}; + std::string chain_type_raw{argv[2]}; + + kernel_LoggingOptions logging_options = { + .log_timestamps = true, + .log_time_micros = true, + .log_threadnames = false, + .log_sourcelocations = false, + .always_print_category_levels = true, + }; + assert(kernel_enable_log_category(kernel_LogCategory::kernel_LOG_REINDEX)); + Logger logger{std::make_unique(TestLog{}), logging_options}; + + kernel_ChainType chain_type; + if (auto maybe_chain_type{string_to_chain_type(chain_type_raw)}) { + chain_type = *maybe_chain_type; + } else { + std::cout << "Error: invalid chain type string. Valid values are \"mainnet\", \"testnet\", \"signet\", \"regtest\"" << std::endl; + return 1; + } + + ReindexKernelNotifications notifications{}; + auto context = create_context(notifications, chain_type); + assert(context); + run_reindex(data_dir, data_dir / "blocks", context); + + std::cout << "Reindex completed"; +} +