From 44c65c46c43e64f675f487fe6a57306c8462ebde Mon Sep 17 00:00:00 2001 From: TheCharlatan Date: Tue, 28 May 2024 10:32:52 +0200 Subject: [PATCH 01/27] kernel: Introduce initial kernel C header API As a first step, implement the equivalent of what was implemented in the now deprecated libbitcoinconsensus header. Also add a test binary to exercise the header and library. Unlike the deprecated libbitcoinconsensus the kernel library can now use the hardware-accelerated sha256 implementations thanks for its statically-initialzed context. The functions kept around for backwards-compatibility in the libbitcoinconsensus header are not ported over. As a new header, it should not be burdened by previous implementations. Also add a new error code for handling invalid flag combinations, which would otherwise cause a crash. The macros used in the new C header were adapted from the libsecp256k1 header. To make use of the C header from C++ code, a C++ header is also introduced for wrapping the C header. This makes it safer and easier to use from C++ code. --- CMakeLists.txt | 3 + src/CMakeLists.txt | 3 + src/kernel/CMakeLists.txt | 3 + src/kernel/bitcoinkernel.cpp | 160 +++++++++++++++++++++- src/kernel/bitcoinkernel.h | 204 ++++++++++++++++++++++++++++ src/kernel/bitcoinkernel_wrapper.h | 105 ++++++++++++++ src/test/kernel/CMakeLists.txt | 13 ++ src/test/kernel/test_kernel.cpp | 160 ++++++++++++++++++++++ test/lint/lint-locale-dependence.py | 1 + 9 files changed, 650 insertions(+), 2 deletions(-) create mode 100644 src/kernel/bitcoinkernel.h create mode 100644 src/kernel/bitcoinkernel_wrapper.h create mode 100644 src/test/kernel/CMakeLists.txt create mode 100644 src/test/kernel/test_kernel.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 6a02a0ee5df4..9f1bdd4f0cb5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -86,6 +86,7 @@ option(BUILD_UTIL "Build bitcoin-util executable." ${BUILD_TESTS}) option(BUILD_UTIL_CHAINSTATE "Build experimental bitcoin-chainstate executable." OFF) option(BUILD_KERNEL_LIB "Build experimental bitcoinkernel library." ${BUILD_UTIL_CHAINSTATE}) +option(BUILD_KERNEL_TEST "Build tests for the experimental bitcoinkernel library." ${BUILD_KERNEL_LIB}) option(ENABLE_WALLET "Enable wallet." ON) option(WITH_SQLITE "Enable SQLite wallet support." ${ENABLE_WALLET}) @@ -216,6 +217,7 @@ if(BUILD_FOR_FUZZING) set(BUILD_UTIL OFF) set(BUILD_UTIL_CHAINSTATE OFF) set(BUILD_KERNEL_LIB OFF) + set(BUILD_KERNEL_TEST OFF) set(BUILD_WALLET_TOOL OFF) set(BUILD_GUI OFF) set(ENABLE_EXTERNAL_SIGNER OFF) @@ -598,6 +600,7 @@ message(" bitcoin-util ........................ ${BUILD_UTIL}") message(" bitcoin-wallet ...................... ${BUILD_WALLET_TOOL}") message(" bitcoin-chainstate (experimental) ... ${BUILD_UTIL_CHAINSTATE}") message(" libbitcoinkernel (experimental) ..... ${BUILD_KERNEL_LIB}") +message(" kernel-test (experimental) .......... ${BUILD_KERNEL_TEST}") message("Optional features:") message(" wallet support ...................... ${ENABLE_WALLET}") if(ENABLE_WALLET) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 22c850e77559..544c8fec146e 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -443,6 +443,9 @@ if(BUILD_FUZZ_BINARY) add_subdirectory(test/fuzz) endif() +if (BUILD_KERNEL_TEST) + add_subdirectory(test/kernel) +endif() install(TARGETS ${installable_targets} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} diff --git a/src/kernel/CMakeLists.txt b/src/kernel/CMakeLists.txt index 2e07ba042a40..a23e95192832 100644 --- a/src/kernel/CMakeLists.txt +++ b/src/kernel/CMakeLists.txt @@ -142,3 +142,6 @@ install(TARGETS bitcoinkernel DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT Kernel ) + +install(FILES bitcoinkernel.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} COMPONENT Kernel) + diff --git a/src/kernel/bitcoinkernel.cpp b/src/kernel/bitcoinkernel.cpp index bb101ba186a2..c9d11ecc7fe4 100644 --- a/src/kernel/bitcoinkernel.cpp +++ b/src/kernel/bitcoinkernel.cpp @@ -1,10 +1,166 @@ -// Copyright (c) 2022 The Bitcoin Core developers +// Copyright (c) 2022-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. +#include + +#include +#include +#include +#include