Skip to content

Commit

Permalink
Add reindexer binary
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCharlatan committed Dec 13, 2024
1 parent 6090df2 commit 9f7e117
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/kernel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
110 changes: 110 additions & 0 deletions src/kernel/reindexer.cpp
Original file line number Diff line number Diff line change
@@ -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 <kernel/bitcoinkernel_wrapper.h>

#include <cassert>
#include <filesystem>
#include <iostream>

class TestLog
{
public:
void LogMessage(const char* message)
{
std::cout << "kernel: " << message;
}
};

class ReindexKernelNotifications : public KernelNotifications<ReindexKernelNotifications>
{
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<kernel_ChainType> 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: <data_dir> <chain_type>" << 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>(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";
}

0 comments on commit 9f7e117

Please sign in to comment.