Skip to content

Commit 01f8cf5

Browse files
committed
Squashed 'depend/bitcoin/' changes from 5991a69ee00..2dc27e2860b
2dc27e2860b kernel: Add pure kernel bitcoin-chainstate 338126c048f kernel: Add functions to get the block hash from a block 56345a77968 kernel: Add block index utility functions to C header 9530f65d19b kernel: Add function to read block undo data from disk to C header f63860169fb kernel: Add functions to read block from disk to C header f3aa1f9bf0c kernel: Add function for copying block data to C header a70be77c908 kernel: Add functions for the block validation state to C header e4e3dc585a3 kernel: Add validation interface to C header d3c48b2bdc6 kernel: Add interrupt function to C header 7afbc516054 kernel: Add import blocks function to C header bad89c8b30b kernel: Add chainstate load options for in-memory dbs in C header 1dcedf3da97 kernel: Add options for reindexing in C header aeb68c7f022 kernel: Add block validation to C header 99136eb1656 kernel: Add chainstate loading when instantiating a ChainstateManager 0a2117ed501 kernel: Add chainstate manager option for setting worker threads 599dc235e98 kernel: Add chainstate manager object to C header f280a5f3ab8 kernel: Add notifications context option to C header 6701d12b017 kernel: Add chain params context option to C header f40f037a9cd kernel: Add kernel library context object 7a6c5c7abf2 kernel: Add logging to kernel library C header REVERT: 5991a69ee00 kernel: Add pure kernel bitcoin-chainstate REVERT: 05b7d136684 kernel: Add functions to get the block hash from a block REVERT: f18c792d843 kernel: Add block index utility functions to C header REVERT: 89f5bf04673 kernel: Add function to read block undo data from disk to C header REVERT: b4f71fc64e7 kernel: Add functions to read block from disk to C header REVERT: 41306f081ad kernel: Add function for copying block data to C header REVERT: 9385d9fc87e kernel: Add functions for the block validation state to C header REVERT: 0bd9a710358 kernel: Add validation interface to C header REVERT: 432710f3fc3 kernel: Add interrupt function to C header REVERT: cb164ae1eb2 kernel: Add import blocks function to C header REVERT: abd67fd93d0 kernel: Add chainstate load options for in-memory dbs in C header REVERT: b98c2748e94 kernel: Add options for reindexing in C header REVERT: 9d0efe1fc86 kernel: Add block validation to C header REVERT: 87e364fc1ec kernel: Add chainstate loading when instantiating a ChainstateManager REVERT: df1599b2d2a kernel: Add chainstate manager option for setting worker threads REVERT: fb767002e97 kernel: Add chainstate manager object to C header REVERT: 10b0fad2fd3 kernel: Add notifications context option to C header REVERT: 39e7ad8d0dc kernel: Add chain params context option to C header REVERT: 6285c353b89 kernel: Add kernel library context object REVERT: 98d10160b6a kernel: Add logging to kernel library C header git-subtree-dir: depend/bitcoin git-subtree-split: 2dc27e2860b97c2bffa5f18706917b21858e5594
1 parent bb72273 commit 01f8cf5

File tree

2 files changed

+58
-34
lines changed

2 files changed

+58
-34
lines changed

depend/bitcoin/src/kernel/bitcoinkernel.cpp

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,10 @@ class KernelValidationInterface final : public CValidationInterface
214214
};
215215

216216
struct ContextOptions {
217-
std::unique_ptr<const KernelNotifications> m_notifications;
218-
std::unique_ptr<const CChainParams> m_chainparams;
219-
std::unique_ptr<const KernelValidationInterface> m_validation_interface;
217+
mutable Mutex m_mutex;
218+
std::unique_ptr<const CChainParams> m_chainparams GUARDED_BY(m_mutex);
219+
std::unique_ptr<const KernelNotifications> m_notifications GUARDED_BY(m_mutex);
220+
std::unique_ptr<const KernelValidationInterface> m_validation_interface GUARDED_BY(m_mutex);
220221
};
221222

222223
class Context
@@ -239,22 +240,23 @@ class Context
239240
m_interrupt{std::make_unique<util::SignalInterrupt>()},
240241
m_signals{std::make_unique<ValidationSignals>(std::make_unique<ImmediateTaskRunner>())}
241242
{
242-
if (options && options->m_notifications) {
243-
m_notifications = std::make_unique<KernelNotifications>(*options->m_notifications);
244-
} else {
243+
if (!options) {
245244
m_notifications = std::make_unique<KernelNotifications>(kernel_NotificationInterfaceCallbacks{
246245
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr});
247-
}
248-
249-
if (options && options->m_chainparams) {
250-
m_chainparams = std::make_unique<const CChainParams>(*options->m_chainparams);
251-
} else {
252246
m_chainparams = CChainParams::Main();
253-
}
247+
} else {
248+
LOCK(options->m_mutex);
249+
if (options->m_chainparams) {
250+
m_chainparams = std::make_unique<const CChainParams>(*options->m_chainparams);
251+
}
252+
if (options->m_notifications) {
253+
m_notifications = std::make_unique<KernelNotifications>(*options->m_notifications);
254+
}
255+
if (options->m_validation_interface) {
256+
m_validation_interface = std::make_unique<KernelValidationInterface>(*options->m_validation_interface);
257+
m_signals->RegisterValidationInterface(m_validation_interface.get());
258+
}
254259

255-
if (options && options->m_validation_interface) {
256-
m_validation_interface = std::make_unique<KernelValidationInterface>(*options->m_validation_interface);
257-
m_signals->RegisterValidationInterface(m_validation_interface.get());
258260
}
259261

260262
if (!kernel::SanityChecks(*m_context)) {
@@ -270,9 +272,10 @@ class Context
270272

271273
//! Helper struct to wrap the ChainstateManager-related Options
272274
struct ChainstateManagerOptions {
273-
ChainstateManager::Options m_chainman_options;
274-
node::BlockManager::Options m_blockman_options;
275-
node::ChainstateLoadOptions m_chainstate_load_options;
275+
mutable Mutex m_mutex;
276+
ChainstateManager::Options m_chainman_options GUARDED_BY(m_mutex);
277+
node::BlockManager::Options m_blockman_options GUARDED_BY(m_mutex);
278+
node::ChainstateLoadOptions m_chainstate_load_options GUARDED_BY(m_mutex);
276279

277280
ChainstateManagerOptions(const Context* context, const fs::path& data_dir, const fs::path& blocks_dir)
278281
: m_chainman_options{ChainstateManager::Options{
@@ -309,6 +312,7 @@ const CTxOut* cast_transaction_output(const kernel_TransactionOutput* transactio
309312
{
310313
assert(transaction_output);
311314
return reinterpret_cast<const CTxOut*>(transaction_output);
315+
312316
}
313317

314318
const ContextOptions* cast_const_context_options(const kernel_ContextOptions* options)
@@ -638,19 +642,22 @@ void kernel_context_options_set_chainparams(kernel_ContextOptions* options_, con
638642
auto options{cast_context_options(options_)};
639643
auto chain_params{cast_const_chain_params(chain_parameters)};
640644
// Copy the chainparams, so the caller can free it again
645+
LOCK(options->m_mutex);
641646
options->m_chainparams = std::make_unique<const CChainParams>(*chain_params);
642647
}
643648

644649
void kernel_context_options_set_notifications(kernel_ContextOptions* options_, kernel_NotificationInterfaceCallbacks notifications)
645650
{
646651
auto options{cast_context_options(options_)};
647652
// Copy the notifications, so the caller can free it again
653+
LOCK(options->m_mutex);
648654
options->m_notifications = std::make_unique<const KernelNotifications>(notifications);
649655
}
650656

651657
void kernel_context_options_set_validation_interface(kernel_ContextOptions* options_, kernel_ValidationInterfaceCallbacks vi_cbs)
652658
{
653659
auto options{cast_context_options(options_)};
660+
LOCK(options->m_mutex);
654661
options->m_validation_interface = std::make_unique<KernelValidationInterface>(KernelValidationInterface(vi_cbs));
655662
}
656663

@@ -739,6 +746,7 @@ kernel_ChainstateManagerOptions* kernel_chainstate_manager_options_create(const
739746
void kernel_chainstate_manager_options_set_worker_threads_num(kernel_ChainstateManagerOptions* opts_, int worker_threads)
740747
{
741748
auto opts{cast_chainstate_manager_options(opts_)};
749+
LOCK(opts->m_mutex);
742750
opts->m_chainman_options.worker_threads_num = worker_threads;
743751
}
744752

@@ -755,26 +763,29 @@ bool kernel_chainstate_manager_options_set_wipe_dbs(kernel_ChainstateManagerOpti
755763
LogError("Wiping the block tree db without also wiping the chainstate db is currently unsupported.");
756764
return false;
757765
}
758-
auto chainman_opts{cast_chainstate_manager_options(chainman_opts_)};
759-
chainman_opts->m_blockman_options.block_tree_db_params.wipe_data = wipe_block_tree_db;
760-
chainman_opts->m_chainstate_load_options.wipe_chainstate_db = wipe_chainstate_db;
766+
auto opts{cast_chainstate_manager_options(chainman_opts_)};
767+
LOCK(opts->m_mutex);
768+
opts->m_blockman_options.block_tree_db_params.wipe_data = wipe_block_tree_db;
769+
opts->m_chainstate_load_options.wipe_chainstate_db = wipe_chainstate_db;
761770
return true;
762771
}
763772

764773
void kernel_chainstate_manager_options_set_block_tree_db_in_memory(
765774
kernel_ChainstateManagerOptions* chainstate_load_opts_,
766775
bool block_tree_db_in_memory)
767776
{
768-
auto chainman_opts{cast_chainstate_manager_options(chainstate_load_opts_)};
769-
chainman_opts->m_blockman_options.block_tree_db_params.memory_only = block_tree_db_in_memory;
777+
auto opts{cast_chainstate_manager_options(chainstate_load_opts_)};
778+
LOCK(opts->m_mutex);
779+
opts->m_blockman_options.block_tree_db_params.memory_only = block_tree_db_in_memory;
770780
}
771781

772782
void kernel_chainstate_manager_options_set_chainstate_db_in_memory(
773783
kernel_ChainstateManagerOptions* chainstate_load_opts_,
774784
bool chainstate_db_in_memory)
775785
{
776-
auto chainman_opts{cast_chainstate_manager_options(chainstate_load_opts_)};
777-
chainman_opts->m_chainstate_load_options.coins_db_in_memory = chainstate_db_in_memory;
786+
auto opts{cast_chainstate_manager_options(chainstate_load_opts_)};
787+
LOCK(opts->m_mutex);
788+
opts->m_chainstate_load_options.coins_db_in_memory = chainstate_db_in_memory;
778789
}
779790

780791
kernel_ChainstateManager* kernel_chainstate_manager_create(
@@ -787,14 +798,15 @@ kernel_ChainstateManager* kernel_chainstate_manager_create(
787798
ChainstateManager* chainman;
788799

789800
try {
801+
LOCK(chainman_opts->m_mutex);
790802
chainman = new ChainstateManager{*context->m_interrupt, chainman_opts->m_chainman_options, chainman_opts->m_blockman_options};
791803
} catch (const std::exception& e) {
792804
LogError("Failed to create chainstate manager: %s", e.what());
793805
return nullptr;
794806
}
795807

796808
try {
797-
const auto& chainstate_load_opts{chainman_opts->m_chainstate_load_options};
809+
const auto chainstate_load_opts{WITH_LOCK(chainman_opts->m_mutex, return chainman_opts->m_chainstate_load_options)};
798810

799811
kernel::CacheSizes cache_sizes{DEFAULT_KERNEL_CACHE};
800812
auto [status, chainstate_err]{node::LoadChainstate(*chainman, cache_sizes, chainstate_load_opts)};

depend/bitcoin/src/kernel/bitcoinkernel.h

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,9 @@ typedef struct kernel_TransactionOutput kernel_TransactionOutput;
122122
*
123123
* Messages that were logged before a connection is created are buffered in a
124124
* 1MB buffer. Logging can alternatively be permanently disabled by calling
125-
* kernel_disable_logging().
125+
* kernel_disable_logging(). Functions changing the logging settings are global
126+
* (and not thread safe) and change the settings for all existing
127+
* kernel_LoggingConnection instances.
126128
*/
127129
typedef struct kernel_LoggingConnection kernel_LoggingConnection;
128130

@@ -557,15 +559,19 @@ BITCOINKERNEL_API bool BITCOINKERNEL_WARN_UNUSED_RESULT kernel_verify_script(
557559
/**
558560
* @brief This disables the global internal logger. No log messages will be
559561
* buffered internally anymore once this is called and the buffer is cleared.
560-
* This function should only be called once. Log messages will be buffered until
561-
* this function is called, or a logging connection is created.
562+
* This function should only be called once and is not thread or re-entry safe.
563+
* Log messages will be buffered until this function is called, or a logging
564+
* connection is created.
562565
*/
563566
BITCOINKERNEL_API void kernel_disable_logging();
564567

565568
/**
566-
* @brief Set the log level of the global internal logger. This does not enable
567-
* the selected categories. Use `kernel_enable_log_category` to start logging
568-
* from a specific, or all categories.
569+
* @brief Set the log level of the global internal logger. This does not
570+
* enable the selected categories. Use `kernel_enable_log_category` to start
571+
* logging from a specific, or all categories. This function is not thread
572+
* safe. Mutiple calls from different threads are allowed but must be
573+
* synchronized. This changes a global setting and will override settings for
574+
* all existing `kernelLoggingConnection instances.
569575
*
570576
* @param[in] category If kernel_LOG_ALL is chosen, all messages at the specified level
571577
* will be logged. Otherwise only messages from the specified category
@@ -575,14 +581,20 @@ BITCOINKERNEL_API void kernel_disable_logging();
575581
BITCOINKERNEL_API void kernel_add_log_level_category(const kernel_LogCategory category, kernel_LogLevel level);
576582

577583
/**
578-
* @brief Enable a specific log category for the global internal logger.
584+
* @brief Enable a specific log category for the global internal logger. This
585+
* function is not thread safe. Mutiple calls from different threads are
586+
* allowed but must be synchronized. This changes a global setting and will
587+
* override settings for all existing `kernelLoggingConnection instances.
579588
*
580589
* @param[in] category If kernel_LOG_ALL is chosen, all categories will be enabled.
581590
*/
582591
BITCOINKERNEL_API void kernel_enable_log_category(const kernel_LogCategory category);
583592

584593
/**
585-
* Disable a specific log category for the global internal logger.
594+
* @brief Disable a specific log category for the global internal logger. This
595+
* function is not thread safe. Mutiple calls from different threads are
596+
* allowed but must be synchronized. This changes a global setting and will
597+
* override settings for all existing `kernelLoggingConnection instances.
586598
*
587599
* @param[in] category If kernel_LOG_ALL is chosen, all categories will be disabled.
588600
*/

0 commit comments

Comments
 (0)