mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-04-08 14:47:31 +02:00
kernel: Add chainstate manager object to C header
This is the main driver class for anything validation related, so expose it here. Creating the chainstate manager options will currently also trigger the creation of their respectively configured directories. The chainstate manager and block manager options are consolidated into a single object. The kernel might eventually introduce a separate block manager object for the purposes of being a light-weight block store reader. The chainstate manager will associate with the context with which it was created for the duration of its lifetime and it keeps it in memory with a shared pointer. The tests now also create dedicated temporary directories. This is similar to the behaviour in the existing unit test framework. Co-authored-by: stickies-v <stickies-v@protonmail.com>
This commit is contained in:
@@ -11,15 +11,36 @@
|
||||
#include <charconv>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <filesystem>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <random>
|
||||
#include <ranges>
|
||||
#include <span>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
|
||||
using namespace btck;
|
||||
|
||||
std::string random_string(uint32_t length)
|
||||
{
|
||||
const std::string chars = "0123456789"
|
||||
"abcdefghijklmnopqrstuvwxyz"
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
|
||||
static std::random_device rd;
|
||||
static std::default_random_engine dre{rd()};
|
||||
static std::uniform_int_distribution<> distribution(0, chars.size() - 1);
|
||||
|
||||
std::string random;
|
||||
random.reserve(length);
|
||||
for (uint32_t i = 0; i < length; i++) {
|
||||
random += chars[distribution(dre)];
|
||||
}
|
||||
return random;
|
||||
}
|
||||
|
||||
std::vector<std::byte> hex_string_to_byte_vec(std::string_view hex)
|
||||
{
|
||||
std::vector<std::byte> bytes;
|
||||
@@ -60,6 +81,20 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
struct TestDirectory {
|
||||
std::filesystem::path m_directory;
|
||||
TestDirectory(std::string directory_name)
|
||||
: m_directory{std::filesystem::temp_directory_path() / (directory_name + random_string(16))}
|
||||
{
|
||||
std::filesystem::create_directories(m_directory);
|
||||
}
|
||||
|
||||
~TestDirectory()
|
||||
{
|
||||
std::filesystem::remove_all(m_directory);
|
||||
}
|
||||
};
|
||||
|
||||
class TestKernelNotifications : public KernelNotifications
|
||||
{
|
||||
public:
|
||||
@@ -434,3 +469,38 @@ BOOST_AUTO_TEST_CASE(btck_context_tests)
|
||||
Context context{options};
|
||||
}
|
||||
}
|
||||
|
||||
Context create_context(std::shared_ptr<TestKernelNotifications> notifications, ChainType chain_type)
|
||||
{
|
||||
ContextOptions options{};
|
||||
ChainParams params{chain_type};
|
||||
options.SetChainParams(params);
|
||||
options.SetNotifications(notifications);
|
||||
auto context{Context{options}};
|
||||
return context;
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(btck_chainman_tests)
|
||||
{
|
||||
Logger logger{std::make_unique<TestLog>()};
|
||||
auto test_directory{TestDirectory{"chainman_test_bitcoin_kernel"}};
|
||||
|
||||
{ // test with default context
|
||||
Context context{};
|
||||
ChainstateManagerOptions chainman_opts{context, test_directory.m_directory.string(), (test_directory.m_directory / "blocks").string()};
|
||||
ChainMan chainman{context, chainman_opts};
|
||||
}
|
||||
|
||||
{ // test with default context options
|
||||
ContextOptions options{};
|
||||
Context context{options};
|
||||
ChainstateManagerOptions chainman_opts{context, test_directory.m_directory.string(), (test_directory.m_directory / "blocks").string()};
|
||||
ChainMan chainman{context, chainman_opts};
|
||||
}
|
||||
|
||||
auto notifications{std::make_shared<TestKernelNotifications>()};
|
||||
auto context{create_context(notifications, ChainType::MAINNET)};
|
||||
|
||||
ChainstateManagerOptions chainman_opts{context, test_directory.m_directory.string(), (test_directory.m_directory / "blocks").string()};
|
||||
ChainMan chainman{context, chainman_opts};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user