Skip to content

Commit cfb18bf

Browse files
committed
Squashed 'libbitcoinkernel-sys/bitcoin/' changes from 34a8429ff3a..403c20980ec
403c20980ec kernel: Add pure kernel bitcoin-chainstate 2e1b262eff6 kernel: Add functions to get the block hash from a block f0b56a6052a kernel: Add block index utility functions to C header 9393cf98179 kernel: Add function to read block undo data from disk to C header e2ccfba0e3e kernel: Add functions to read block from disk to C header 0559575861c kernel: Add function for copying block data to C header 7b02e52237e kernel: Add functions for the block validation state to C header fdebacca7ad kernel: Add validation interface to C header f4ea5f49c6d kernel: Add interrupt function to C header 5f4b436aad9 kernel: Add import blocks function to C header c95a28fd80c kernel: Add chainstate load options for in-memory dbs in C header d6360557ef6 kernel: Add options for reindexing in C header a125867b9fe kernel: Add block validation to C header b2b75a0ef73 Kernel: Add chainstate loading to kernel C header d233003ff2a kernel: Add chainstate manager option for setting worker threads REVERT: 34a8429ff3a kernel: Add pure kernel bitcoin-chainstate REVERT: 6e9834d65e8 kernel: Add functions to get the block hash from a block REVERT: 5ade5c2c879 kernel: Add block index utility functions to C header REVERT: edc970c16fc kernel: Add function to read block undo data from disk to C header REVERT: 27ae05ee614 kernel: Add functions to read block from disk to C header REVERT: 2a31ff87c85 kernel: Add function for copying block data to C header REVERT: 18d44eb823d kernel: Add functions for the block validation state to C header REVERT: 8a338185fa9 kernel: Add validation interface to C header REVERT: abfb0549fdd kernel: Add interrupt function to C header REVERT: fdf772a2918 kernel: Add import blocks function to C header REVERT: a6175602cf5 kernel: Add chainstate load options for in-memory dbs in C header REVERT: 72c57446910 kernel: Add options for reindexing in C header REVERT: fe82b15ce02 kernel: Add block validation to C header REVERT: 56b8a64947d Kernel: Add chainstate loading to kernel C header REVERT: c3b34ce08d7 kernel: Add chainstate manager option for setting worker threads git-subtree-dir: libbitcoinkernel-sys/bitcoin git-subtree-split: 403c20980ec118f6efdd21d7c25646e20574583b
1 parent 4cef0f9 commit cfb18bf

File tree

5 files changed

+156
-218
lines changed

5 files changed

+156
-218
lines changed

src/kernel/bitcoin-chainstate.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ int main(int argc, char* argv[])
153153

154154
TestKernelNotifications notifications{};
155155
options.SetNotifications(notifications);
156+
TestValidationInterface validation_interface{};
157+
options.SetValidationInterface(validation_interface);
156158

157159
Context context{options};
158160
assert(context);

src/kernel/bitcoinkernel.cpp

Lines changed: 38 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,28 @@ class KernelNotifications : public kernel::Notifications
192192
}
193193
};
194194

195+
class KernelValidationInterface final : public CValidationInterface
196+
{
197+
public:
198+
const kernel_ValidationInterfaceCallbacks m_cbs;
199+
200+
explicit KernelValidationInterface(const kernel_ValidationInterfaceCallbacks vi_cbs) : m_cbs{vi_cbs} {}
201+
202+
protected:
203+
void BlockChecked(const CBlock& block, const BlockValidationState& stateIn) override
204+
{
205+
if (m_cbs.block_checked) {
206+
m_cbs.block_checked((void*) m_cbs.user_data,
207+
reinterpret_cast<const kernel_BlockPointer*>(&block),
208+
reinterpret_cast<const kernel_BlockValidationState*>(&stateIn));
209+
}
210+
}
211+
};
212+
195213
struct ContextOptions {
196214
std::unique_ptr<const KernelNotifications> m_notifications;
197215
std::unique_ptr<const CChainParams> m_chainparams;
216+
std::unique_ptr<const KernelValidationInterface> m_validation_interface;
198217
};
199218

200219
class Context
@@ -210,6 +229,8 @@ class Context
210229

211230
std::unique_ptr<const CChainParams> m_chainparams;
212231

232+
std::unique_ptr<KernelValidationInterface> m_validation_interface;
233+
213234
Context(const ContextOptions* options, bool& sane)
214235
: m_context{std::make_unique<kernel::Context>()},
215236
m_interrupt{std::make_unique<util::SignalInterrupt>()},
@@ -228,27 +249,19 @@ class Context
228249
m_chainparams = CChainParams::Main();
229250
}
230251

252+
if (options && options->m_validation_interface) {
253+
m_validation_interface = std::make_unique<KernelValidationInterface>(*options->m_validation_interface);
254+
m_signals->RegisterValidationInterface(m_validation_interface.get());
255+
}
256+
231257
if (!kernel::SanityChecks(*m_context)) {
232258
sane = false;
233259
}
234260
}
235-
};
236261

237-
class KernelValidationInterface final : public CValidationInterface
238-
{
239-
public:
240-
const kernel_ValidationInterfaceCallbacks m_cbs;
241-
242-
explicit KernelValidationInterface(const kernel_ValidationInterfaceCallbacks vi_cbs) : m_cbs{vi_cbs} {}
243-
244-
protected:
245-
void BlockChecked(const CBlock& block, const BlockValidationState& stateIn) override
262+
~Context()
246263
{
247-
if (m_cbs.block_checked) {
248-
m_cbs.block_checked((void*) m_cbs.user_data,
249-
reinterpret_cast<const kernel_BlockPointer*>(&block),
250-
reinterpret_cast<const kernel_BlockValidationState*>(&stateIn));
251-
}
264+
m_signals->UnregisterValidationInterface(m_validation_interface.get());
252265
}
253266
};
254267

@@ -348,12 +361,6 @@ std::shared_ptr<CBlock>* cast_cblocksharedpointer(kernel_Block* block)
348361
return reinterpret_cast<std::shared_ptr<CBlock>*>(block);
349362
}
350363

351-
std::shared_ptr<KernelValidationInterface>* cast_validation_interface(kernel_ValidationInterface* interface)
352-
{
353-
assert(interface);
354-
return reinterpret_cast<std::shared_ptr<KernelValidationInterface>*>(interface);
355-
}
356-
357364
const BlockValidationState* cast_block_validation_state(const kernel_BlockValidationState* block_validation_state)
358365
{
359366
assert(block_validation_state);
@@ -633,6 +640,13 @@ void kernel_context_options_set_notifications(kernel_ContextOptions* options_, c
633640
options->m_notifications = std::make_unique<const KernelNotifications>(*notifications);
634641
}
635642

643+
void kernel_context_options_set_validation_interface(kernel_ContextOptions* options_, kernel_ValidationInterfaceCallbacks vi_cbs)
644+
{
645+
auto options{cast_context_options(options_)};
646+
options->m_validation_interface = std::make_unique<KernelValidationInterface>(KernelValidationInterface(vi_cbs));
647+
// return reinterpret_cast<kernel_ValidationInterface*>(new std::shared_ptr<KernelValidationInterface>(new KernelValidationInterface(vi_cbs)));
648+
}
649+
636650
void kernel_context_options_destroy(kernel_ContextOptions* options)
637651
{
638652
if (options) {
@@ -666,42 +680,6 @@ void kernel_context_destroy(kernel_Context* context)
666680
}
667681
}
668682

669-
kernel_ValidationInterface* kernel_validation_interface_create(kernel_ValidationInterfaceCallbacks vi_cbs)
670-
{
671-
return reinterpret_cast<kernel_ValidationInterface*>(new std::shared_ptr<KernelValidationInterface>(new KernelValidationInterface(vi_cbs)));
672-
}
673-
674-
bool kernel_validation_interface_register(kernel_Context* context_, kernel_ValidationInterface* validation_interface_)
675-
{
676-
auto context{cast_context(context_)};
677-
auto validation_interface{cast_validation_interface(validation_interface_)};
678-
if (!context->m_signals) {
679-
LogError("Cannot register validation interface with context that has no validation signals.\n");
680-
return false;
681-
}
682-
context->m_signals->RegisterSharedValidationInterface(*validation_interface);
683-
return true;
684-
}
685-
686-
bool kernel_validation_interface_unregister(kernel_Context* context_, kernel_ValidationInterface* validation_interface_)
687-
{
688-
auto context{cast_context(context_)};
689-
auto validation_interface{cast_validation_interface(validation_interface_)};
690-
if (!context->m_signals) {
691-
LogError("Cannot de-register validation interface with context that has no validation signals.\n");
692-
return false;
693-
}
694-
context->m_signals->UnregisterSharedValidationInterface(*validation_interface);
695-
return true;
696-
}
697-
698-
void kernel_validation_interface_destroy(kernel_ValidationInterface* validation_interface)
699-
{
700-
if (validation_interface) {
701-
delete cast_validation_interface(validation_interface);
702-
}
703-
}
704-
705683
kernel_ValidationMode kernel_get_validation_mode_from_block_validation_state(const kernel_BlockValidationState* block_validation_state_)
706684
{
707685
auto& block_validation_state = *cast_block_validation_state(block_validation_state_);
@@ -1035,7 +1013,7 @@ kernel_BlockIndex* kernel_get_block_index_from_genesis(const kernel_Context* con
10351013
return reinterpret_cast<kernel_BlockIndex*>(WITH_LOCK(::cs_main, return chainman->ActiveChain().Genesis()));
10361014
}
10371015

1038-
kernel_BlockIndex* kernel_get_block_index_by_hash(const kernel_Context* context_, kernel_ChainstateManager* chainman_, kernel_BlockHash* block_hash)
1016+
kernel_BlockIndex* kernel_get_block_index_from_hash(const kernel_Context* context_, kernel_ChainstateManager* chainman_, kernel_BlockHash* block_hash)
10391017
{
10401018
auto chainman{cast_chainstate_manager(chainman_)};
10411019

@@ -1048,7 +1026,7 @@ kernel_BlockIndex* kernel_get_block_index_by_hash(const kernel_Context* context_
10481026
return reinterpret_cast<kernel_BlockIndex*>(block_index);
10491027
}
10501028

1051-
kernel_BlockIndex* kernel_get_block_index_by_height(const kernel_Context* context_, kernel_ChainstateManager* chainman_, int height)
1029+
kernel_BlockIndex* kernel_get_block_index_from_height(const kernel_Context* context_, kernel_ChainstateManager* chainman_, int height)
10521030
{
10531031
auto chainman{cast_chainstate_manager(chainman_)};
10541032

@@ -1061,7 +1039,7 @@ kernel_BlockIndex* kernel_get_block_index_by_height(const kernel_Context* contex
10611039
return reinterpret_cast<kernel_BlockIndex*>(chainman->ActiveChain()[height]);
10621040
}
10631041

1064-
kernel_BlockIndex* kernel_get_next_block_index(const kernel_Context* context_, const kernel_BlockIndex* block_index_, kernel_ChainstateManager* chainman_)
1042+
kernel_BlockIndex* kernel_get_next_block_index(const kernel_Context* context_, kernel_ChainstateManager* chainman_, const kernel_BlockIndex* block_index_)
10651043
{
10661044
const auto block_index{cast_const_block_index(block_index_)};
10671045
auto chainman{cast_chainstate_manager(chainman_)};

src/kernel/bitcoinkernel.h

Lines changed: 21 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,21 @@ void kernel_context_options_set_notifications(
641641
const kernel_Notifications* notifications
642642
) BITCOINKERNEL_ARG_NONNULL(1, 2);
643643

644+
/**
645+
* @brief Set the validation interface callbacks for the context options. The
646+
* context created with the options will be configured for these validation
647+
* interface callbacks. The callbacks will then be triggered from validation
648+
* events issued by the chainstate manager created from the same context.
649+
*
650+
* @param[in] context_options Non-null, previously created with kernel_context_options_create.
651+
* @param[in] validation_interface_callbacks The callbacks used for passing validation information to the
652+
* user.
653+
*/
654+
void kernel_context_options_set_validation_interface(
655+
kernel_ContextOptions* context_options,
656+
kernel_ValidationInterfaceCallbacks validation_interface_callbacks
657+
) BITCOINKERNEL_ARG_NONNULL(1);
658+
644659
/**
645660
* Destroy the context options.
646661
*/
@@ -715,7 +730,8 @@ kernel_BlockManagerOptions* BITCOINKERNEL_WARN_UNUSED_RESULT kernel_block_manage
715730
*
716731
* @param[in] chainstate_manager_options Non-null, options to be set.
717732
* @param[in] worker_threads The number of worker threads that should be spawned in the thread pool
718-
* used for validation. The number should be greater than 0.
733+
* used for validation. The number must not be negative. When set to zero
734+
* no parallel verification is done.
719735
*/
720736
void kernel_chainstate_manager_options_set_worker_threads_num(
721737
kernel_ChainstateManagerOptions* chainstate_manager_options,
@@ -750,54 +766,6 @@ kernel_ChainstateManager* BITCOINKERNEL_WARN_UNUSED_RESULT kernel_chainstate_man
750766
*/
751767
void kernel_chainstate_manager_destroy(kernel_ChainstateManager* chainstate_manager, const kernel_Context* context);
752768

753-
/**
754-
* @brief Creates a new validation interface for consuming events issued by the
755-
* chainstate manager. The interface should be created and registered before the
756-
* chainstate manager is created to avoid missing validation events.
757-
*
758-
* @param[in] validation_interface_callbacks The callbacks used for passing validation information to the
759-
* user.
760-
* @return A validation interface. This should remain in memory for as
761-
* long as the user expects to receive validation events.
762-
*/
763-
kernel_ValidationInterface* BITCOINKERNEL_WARN_UNUSED_RESULT kernel_validation_interface_create(
764-
kernel_ValidationInterfaceCallbacks validation_interface_callbacks);
765-
766-
/**
767-
* @brief Register a validation interface with the internal task runner
768-
* associated with this context. This also registers it with the chainstate
769-
* manager if the chainstate manager is subsequently created with this context.
770-
*
771-
* @param[in] context Non-null, will register the validation interface with this context.
772-
* @param[in] validation_interface Non-null.
773-
* @return True on success.
774-
*/
775-
bool BITCOINKERNEL_WARN_UNUSED_RESULT kernel_validation_interface_register(
776-
kernel_Context* context,
777-
kernel_ValidationInterface* validation_interface
778-
) BITCOINKERNEL_ARG_NONNULL(1, 2);
779-
780-
/**
781-
* @brief Unregister a validation interface from the internal task runner
782-
* associated with this context. This should be done before destroying the
783-
* kernel context it was previously registered with.
784-
*
785-
* @param[in] context Non-null, will deregister the validation interface from this context.
786-
* @param[in] validation_interface Non-null.
787-
* @return True on success.
788-
*/
789-
bool BITCOINKERNEL_WARN_UNUSED_RESULT kernel_validation_interface_unregister(
790-
kernel_Context* context,
791-
kernel_ValidationInterface* validation_interface
792-
) BITCOINKERNEL_ARG_NONNULL(1, 2);
793-
794-
/**
795-
* Destroy the validation interface. This should be done after unregistering it
796-
* if the validation interface was previously registered with a chainstate
797-
* manager.
798-
*/
799-
void kernel_validation_interface_destroy(kernel_ValidationInterface* validation_interface);
800-
801769
/**
802770
* Create options for loading the chainstate.
803771
*/
@@ -1012,7 +980,7 @@ kernel_BlockIndex* BITCOINKERNEL_WARN_UNUSED_RESULT kernel_get_block_index_from_
1012980
* @param[in] block_hash Non-null.
1013981
* @return The block index of the block with the passed in hash, or null on error.
1014982
*/
1015-
kernel_BlockIndex* BITCOINKERNEL_WARN_UNUSED_RESULT kernel_get_block_index_by_hash(
983+
kernel_BlockIndex* BITCOINKERNEL_WARN_UNUSED_RESULT kernel_get_block_index_from_hash(
1016984
const kernel_Context* context,
1017985
kernel_ChainstateManager* chainstate_manager,
1018986
kernel_BlockHash* block_hash
@@ -1027,7 +995,7 @@ kernel_BlockIndex* BITCOINKERNEL_WARN_UNUSED_RESULT kernel_get_block_index_by_ha
1027995
* @param[in] block_height Height in the chain of the to be retrieved block index.
1028996
* @return The block index at a certain height in the currently active chain, or null on error.
1029997
*/
1030-
kernel_BlockIndex* BITCOINKERNEL_WARN_UNUSED_RESULT kernel_get_block_index_by_height(
998+
kernel_BlockIndex* BITCOINKERNEL_WARN_UNUSED_RESULT kernel_get_block_index_from_height(
1031999
const kernel_Context* context,
10321000
kernel_ChainstateManager* chainstate_manager,
10331001
int block_height
@@ -1045,8 +1013,8 @@ kernel_BlockIndex* BITCOINKERNEL_WARN_UNUSED_RESULT kernel_get_block_index_by_he
10451013
*/
10461014
kernel_BlockIndex* BITCOINKERNEL_WARN_UNUSED_RESULT kernel_get_next_block_index(
10471015
const kernel_Context* context,
1048-
const kernel_BlockIndex* block_index,
1049-
kernel_ChainstateManager* chainstate_manager
1016+
kernel_ChainstateManager* chainstate_manager,
1017+
const kernel_BlockIndex* block_index
10501018
) BITCOINKERNEL_ARG_NONNULL(1, 2, 3);
10511019

10521020
/**

0 commit comments

Comments
 (0)