From d27e27758d51bc2aa125dc967691aacc4f3811d3 Mon Sep 17 00:00:00 2001 From: TheCharlatan Date: Wed, 5 Jun 2024 10:20:40 +0200 Subject: [PATCH] kernel: Add interrupt function to C header Calling interrupt can halt long-running functions associated with objects that were created through the passed-in context. --- src/kernel/bitcoinkernel.cpp | 5 +++++ src/kernel/bitcoinkernel.h | 10 ++++++++++ src/kernel/bitcoinkernel_wrapper.h | 5 +++++ src/test/kernel/test_kernel.cpp | 2 ++ 4 files changed, 22 insertions(+) diff --git a/src/kernel/bitcoinkernel.cpp b/src/kernel/bitcoinkernel.cpp index 0c5d811db08..1c146df0cb0 100644 --- a/src/kernel/bitcoinkernel.cpp +++ b/src/kernel/bitcoinkernel.cpp @@ -676,6 +676,11 @@ btck_Context* btck_context_copy(const btck_Context* context) return btck_Context::copy(context); } +int btck_context_interrupt(btck_Context* context) +{ + return (*btck_Context::get(context)->m_interrupt)() ? 0 : -1; +} + void btck_context_destroy(btck_Context* context) { delete context; diff --git a/src/kernel/bitcoinkernel.h b/src/kernel/bitcoinkernel.h index 651c54c48f1..8d7b2898251 100644 --- a/src/kernel/bitcoinkernel.h +++ b/src/kernel/bitcoinkernel.h @@ -704,6 +704,16 @@ BITCOINKERNEL_API btck_Context* BITCOINKERNEL_WARN_UNUSED_RESULT btck_context_cr BITCOINKERNEL_API btck_Context* BITCOINKERNEL_WARN_UNUSED_RESULT btck_context_copy( const btck_Context* context) BITCOINKERNEL_ARG_NONNULL(1); +/** + * @brief Interrupt can be used to halt long-running validation functions like + * when reindexing, importing or processing blocks. + * + * @param[in] context Non-null. + * @return 0 if the interrupt was successful, non-zero otherwise. + */ +BITCOINKERNEL_API int BITCOINKERNEL_WARN_UNUSED_RESULT btck_context_interrupt( + btck_Context* context) BITCOINKERNEL_ARG_NONNULL(1); + /** * Destroy the context. */ diff --git a/src/kernel/bitcoinkernel_wrapper.h b/src/kernel/bitcoinkernel_wrapper.h index ff16f5bc042..4c378d3a8fa 100644 --- a/src/kernel/bitcoinkernel_wrapper.h +++ b/src/kernel/bitcoinkernel_wrapper.h @@ -651,6 +651,11 @@ public: Context() : Handle{btck_context_create(ContextOptions{}.get())} {} + + bool interrupt() + { + return btck_context_interrupt(get()) == 0; + } }; class ChainstateManagerOptions : public UniqueHandle diff --git a/src/test/kernel/test_kernel.cpp b/src/test/kernel/test_kernel.cpp index 128f3f49fb5..cfe3b2d2d4a 100644 --- a/src/test/kernel/test_kernel.cpp +++ b/src/test/kernel/test_kernel.cpp @@ -634,6 +634,8 @@ BOOST_AUTO_TEST_CASE(btck_chainman_in_memory_tests) BOOST_CHECK(std::filesystem::exists(in_memory_test_directory.m_directory / "blocks")); BOOST_CHECK(!std::filesystem::exists(in_memory_test_directory.m_directory / "blocks" / "index")); BOOST_CHECK(!std::filesystem::exists(in_memory_test_directory.m_directory / "chainstate")); + + BOOST_CHECK(context.interrupt()); } BOOST_AUTO_TEST_CASE(btck_chainman_regtest_tests)