mirror of https://github.com/bitcoin/bitcoin.git
kernel: Add chain params context option to C header
As a first option, add the chainparams. For now these can only be instantiated with default values. In future they may be expanded to take their own options for regtest and signet configurations. This commit also introduces a unique pattern for setting the option values when calling the `*_set(...)` function.
This commit is contained in:
parent
cc89fcc6da
commit
86cd091d74
|
@ -18,6 +18,7 @@
|
|||
#include <script/script.h>
|
||||
#include <serialize.h>
|
||||
#include <streams.h>
|
||||
#include <sync.h>
|
||||
#include <tinyformat.h>
|
||||
#include <util/result.h>
|
||||
#include <util/signalinterrupt.h>
|
||||
|
@ -108,6 +109,11 @@ struct Handle {
|
|||
return *reinterpret_cast<const CPP*>(ptr);
|
||||
}
|
||||
|
||||
static CPP& get(C* ptr)
|
||||
{
|
||||
return *reinterpret_cast<CPP*>(ptr);
|
||||
}
|
||||
|
||||
static void operator delete(void* ptr)
|
||||
{
|
||||
delete reinterpret_cast<CPP*>(ptr);
|
||||
|
@ -228,6 +234,8 @@ struct LoggingConnection {
|
|||
};
|
||||
|
||||
struct ContextOptions {
|
||||
mutable Mutex m_mutex;
|
||||
std::unique_ptr<const CChainParams> m_chainparams GUARDED_BY(m_mutex);
|
||||
};
|
||||
|
||||
class Context
|
||||
|
@ -244,9 +252,19 @@ public:
|
|||
Context(const ContextOptions* options, bool& sane)
|
||||
: m_context{std::make_unique<kernel::Context>()},
|
||||
m_notifications{std::make_unique<kernel::Notifications>()},
|
||||
m_interrupt{std::make_unique<util::SignalInterrupt>()},
|
||||
m_chainparams{CChainParams::Main()}
|
||||
m_interrupt{std::make_unique<util::SignalInterrupt>()}
|
||||
{
|
||||
if (options) {
|
||||
LOCK(options->m_mutex);
|
||||
if (options->m_chainparams) {
|
||||
m_chainparams = std::make_unique<const CChainParams>(*options->m_chainparams);
|
||||
}
|
||||
}
|
||||
|
||||
if (!m_chainparams) {
|
||||
m_chainparams = CChainParams::Main();
|
||||
}
|
||||
|
||||
if (!kernel::SanityChecks(*m_context)) {
|
||||
sane = false;
|
||||
}
|
||||
|
@ -261,6 +279,7 @@ struct btck_ScriptPubkey : Handle<btck_ScriptPubkey, CScript> {};
|
|||
struct btck_LoggingConnection : Handle<btck_LoggingConnection, LoggingConnection> {};
|
||||
struct btck_ContextOptions : Handle<btck_ContextOptions, ContextOptions> {};
|
||||
struct btck_Context : Handle<btck_Context, std::shared_ptr<const Context>> {};
|
||||
struct btck_ChainParameters : Handle<btck_ChainParameters, std::unique_ptr<const CChainParams>> {};
|
||||
|
||||
btck_Transaction* btck_transaction_create(const void* raw_transaction, size_t raw_transaction_len)
|
||||
{
|
||||
|
@ -443,11 +462,51 @@ void btck_logging_connection_destroy(btck_LoggingConnection* connection)
|
|||
delete connection;
|
||||
}
|
||||
|
||||
btck_ChainParameters* btck_chain_parameters_create(const btck_ChainType chain_type)
|
||||
{
|
||||
switch (chain_type) {
|
||||
case btck_ChainType_MAINNET: {
|
||||
return btck_ChainParameters::create(CChainParams::Main());
|
||||
}
|
||||
case btck_ChainType_TESTNET: {
|
||||
return btck_ChainParameters::create(CChainParams::TestNet());
|
||||
}
|
||||
case btck_ChainType_TESTNET_4: {
|
||||
return btck_ChainParameters::create(CChainParams::TestNet4());
|
||||
}
|
||||
case btck_ChainType_SIGNET: {
|
||||
return btck_ChainParameters::create(CChainParams::SigNet({}));
|
||||
}
|
||||
case btck_ChainType_REGTEST: {
|
||||
return btck_ChainParameters::create(CChainParams::RegTest({}));
|
||||
}
|
||||
}
|
||||
assert(false);
|
||||
}
|
||||
|
||||
btck_ChainParameters* btck_chain_parameters_copy(const btck_ChainParameters* chain_parameters)
|
||||
{
|
||||
const auto& original = btck_ChainParameters::get(chain_parameters);
|
||||
return btck_ChainParameters::create(std::make_unique<const CChainParams>(*original));
|
||||
}
|
||||
|
||||
void btck_chain_parameters_destroy(btck_ChainParameters* chain_parameters)
|
||||
{
|
||||
delete chain_parameters;
|
||||
}
|
||||
|
||||
btck_ContextOptions* btck_context_options_create()
|
||||
{
|
||||
return btck_ContextOptions::create();
|
||||
}
|
||||
|
||||
void btck_context_options_set_chainparams(btck_ContextOptions* options, const btck_ChainParameters* chain_parameters)
|
||||
{
|
||||
// Copy the chainparams, so the caller can free it again
|
||||
LOCK(btck_ContextOptions::get(options).m_mutex);
|
||||
btck_ContextOptions::get(options).m_chainparams = std::make_unique<const CChainParams>(*btck_ChainParameters::get(chain_parameters));
|
||||
}
|
||||
|
||||
void btck_context_options_destroy(btck_ContextOptions* options)
|
||||
{
|
||||
delete options;
|
||||
|
|
|
@ -115,6 +115,15 @@ typedef struct btck_TransactionOutput btck_TransactionOutput;
|
|||
*/
|
||||
typedef struct btck_LoggingConnection btck_LoggingConnection;
|
||||
|
||||
/**
|
||||
* Opaque data structure for holding the chain parameters.
|
||||
*
|
||||
* These are eventually placed into a kernel context through the kernel context
|
||||
* options. The parameters describe the properties of a chain, and may be
|
||||
* instantiated for either mainnet, testnet, signet, or regtest.
|
||||
*/
|
||||
typedef struct btck_ChainParameters btck_ChainParameters;
|
||||
|
||||
/**
|
||||
* Opaque data structure for holding options for creating a new kernel context.
|
||||
*
|
||||
|
@ -216,6 +225,13 @@ typedef uint32_t btck_ScriptVerificationFlags;
|
|||
btck_ScriptVerificationFlags_WITNESS | \
|
||||
btck_ScriptVerificationFlags_TAPROOT))
|
||||
|
||||
typedef uint8_t btck_ChainType;
|
||||
#define btck_ChainType_MAINNET ((btck_ChainType)(0))
|
||||
#define btck_ChainType_TESTNET ((btck_ChainType)(1))
|
||||
#define btck_ChainType_TESTNET_4 ((btck_ChainType)(2))
|
||||
#define btck_ChainType_SIGNET ((btck_ChainType)(3))
|
||||
#define btck_ChainType_REGTEST ((btck_ChainType)(4))
|
||||
|
||||
/**
|
||||
* Function signature for serializing data.
|
||||
*/
|
||||
|
@ -496,6 +512,34 @@ BITCOINKERNEL_API void btck_logging_connection_destroy(btck_LoggingConnection* l
|
|||
|
||||
///@}
|
||||
|
||||
/** @name ChainParameters
|
||||
* Functions for working with chain parameters.
|
||||
*/
|
||||
///@{
|
||||
|
||||
/**
|
||||
* @brief Creates a chain parameters struct with default parameters based on the
|
||||
* passed in chain type.
|
||||
*
|
||||
* @param[in] chain_type Controls the chain parameters type created.
|
||||
* @return An allocated chain parameters opaque struct.
|
||||
*/
|
||||
BITCOINKERNEL_API btck_ChainParameters* BITCOINKERNEL_WARN_UNUSED_RESULT btck_chain_parameters_create(
|
||||
const btck_ChainType chain_type);
|
||||
|
||||
/**
|
||||
* Copy the chain parameters.
|
||||
*/
|
||||
BITCOINKERNEL_API btck_ChainParameters* BITCOINKERNEL_WARN_UNUSED_RESULT btck_chain_parameters_copy(
|
||||
const btck_ChainParameters* chain_parameters) BITCOINKERNEL_ARG_NONNULL(1);
|
||||
|
||||
/**
|
||||
* Destroy the chain parameters.
|
||||
*/
|
||||
BITCOINKERNEL_API void btck_chain_parameters_destroy(btck_ChainParameters* chain_parameters);
|
||||
|
||||
///@}
|
||||
|
||||
/** @name ContextOptions
|
||||
* Functions for working with context options.
|
||||
*/
|
||||
|
@ -506,6 +550,17 @@ BITCOINKERNEL_API void btck_logging_connection_destroy(btck_LoggingConnection* l
|
|||
*/
|
||||
BITCOINKERNEL_API btck_ContextOptions* BITCOINKERNEL_WARN_UNUSED_RESULT btck_context_options_create();
|
||||
|
||||
/**
|
||||
* @brief Sets the chain params for the context options. The context created
|
||||
* with the options will be configured for these chain parameters.
|
||||
*
|
||||
* @param[in] context_options Non-null, previously created by @ref btck_context_options_create.
|
||||
* @param[in] chain_parameters Is set to the context options.
|
||||
*/
|
||||
BITCOINKERNEL_API void btck_context_options_set_chainparams(
|
||||
btck_ContextOptions* context_options,
|
||||
const btck_ChainParameters* chain_parameters) BITCOINKERNEL_ARG_NONNULL(1, 2);
|
||||
|
||||
/**
|
||||
* Destroy the context options.
|
||||
*/
|
||||
|
|
|
@ -41,6 +41,14 @@ enum class LogLevel : btck_LogLevel {
|
|||
INFO_LEVEL = btck_LogLevel_INFO
|
||||
};
|
||||
|
||||
enum class ChainType : btck_ChainType {
|
||||
MAINNET = btck_ChainType_MAINNET,
|
||||
TESTNET = btck_ChainType_TESTNET,
|
||||
TESTNET_4 = btck_ChainType_TESTNET_4,
|
||||
SIGNET = btck_ChainType_SIGNET,
|
||||
REGTEST = btck_ChainType_REGTEST
|
||||
};
|
||||
|
||||
enum class ScriptVerifyStatus : btck_ScriptVerifyStatus {
|
||||
OK = btck_ScriptVerifyStatus_SCRIPT_VERIFY_OK,
|
||||
ERROR_INVALID_FLAGS_COMBINATION = btck_ScriptVerifyStatus_ERROR_INVALID_FLAGS_COMBINATION,
|
||||
|
@ -518,11 +526,25 @@ public:
|
|||
}
|
||||
};
|
||||
|
||||
class ChainParams : public Handle<btck_ChainParameters, btck_chain_parameters_copy, btck_chain_parameters_destroy>
|
||||
{
|
||||
public:
|
||||
ChainParams(ChainType chain_type)
|
||||
: Handle{btck_chain_parameters_create(static_cast<btck_ChainType>(chain_type))} {}
|
||||
|
||||
friend class ContextOptions;
|
||||
};
|
||||
|
||||
class ContextOptions : UniqueHandle<btck_ContextOptions, btck_context_options_destroy>
|
||||
{
|
||||
public:
|
||||
ContextOptions() : UniqueHandle{btck_context_options_create()} {}
|
||||
|
||||
void SetChainParams(ChainParams& chain_params)
|
||||
{
|
||||
btck_context_options_set_chainparams(get(), chain_params.get());
|
||||
}
|
||||
|
||||
friend class Context;
|
||||
};
|
||||
|
||||
|
|
|
@ -393,8 +393,17 @@ BOOST_AUTO_TEST_CASE(btck_context_tests)
|
|||
CheckHandle(context, context2);
|
||||
}
|
||||
|
||||
{ // test with context options
|
||||
{ // test with context options, but not options set
|
||||
ContextOptions options{};
|
||||
Context context{options};
|
||||
}
|
||||
|
||||
{ // test with context options
|
||||
ContextOptions options{};
|
||||
ChainParams params{ChainType::MAINNET};
|
||||
ChainParams regtest_params{ChainType::REGTEST};
|
||||
CheckHandle(params, regtest_params);
|
||||
options.SetChainParams(params);
|
||||
Context context{options};
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue