From dc504f57b3b524bb0e6f4a5face81b3216f45153 Mon Sep 17 00:00:00 2001 From: TheCharlatan Date: Tue, 28 May 2024 10:32:52 +0200 Subject: [PATCH 01/25] 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. Co-authored-by: stickies-v --- .github/ci-test-each-commit-exec.py | 2 + .github/workflows/ci.yml | 6 +- CMakeLists.txt | 3 + ci/test/00_setup_env_mac_native.sh | 2 +- ...up_env_native_nowallet_libbitcoinkernel.sh | 2 +- ci/test/00_setup_env_win64.sh | 2 +- src/CMakeLists.txt | 3 + src/kernel/CMakeLists.txt | 7 + src/kernel/bitcoinkernel.cpp | 246 +++++++++- src/kernel/bitcoinkernel.h | 341 ++++++++++++++ src/kernel/bitcoinkernel_wrapper.h | 445 ++++++++++++++++++ src/test/kernel/CMakeLists.txt | 16 + src/test/kernel/test_kernel.cpp | 350 ++++++++++++++ 13 files changed, 1417 insertions(+), 8 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/.github/ci-test-each-commit-exec.py b/.github/ci-test-each-commit-exec.py index 3b2eaeeb5964..959a1579aea5 100755 --- a/.github/ci-test-each-commit-exec.py +++ b/.github/ci-test-each-commit-exec.py @@ -42,6 +42,8 @@ def main(): "-DBUILD_BENCH=ON", "-DBUILD_FUZZ_BINARY=ON", "-DWITH_USDT=ON", + "-DBUILD_KERNEL_LIB=ON", + "-DBUILD_KERNEL_TEST=ON", "-DCMAKE_CXX_FLAGS=-Wno-error=unused-member-function", ]) run(["cmake", "--build", "build", "-j", str(num_procs)]) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f5a6f9ab90f4..51bc4ed3b2db 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -202,7 +202,7 @@ jobs: job-type: [standard, fuzz] include: - job-type: standard - generate-options: '-DBUILD_GUI=ON -DWITH_ZMQ=ON -DBUILD_BENCH=ON -DWERROR=ON' + generate-options: '-DBUILD_GUI=ON -DWITH_ZMQ=ON -DBUILD_BENCH=ON -DBUILD_KERNEL_LIB=ON -DWERROR=ON' job-name: 'Windows native, VS 2022' - job-type: fuzz generate-options: '-DVCPKG_MANIFEST_NO_DEFAULT_FEATURES=ON -DVCPKG_MANIFEST_FEATURES="wallet" -DBUILD_GUI=OFF -DBUILD_FOR_FUZZING=ON -DWERROR=ON' @@ -280,7 +280,7 @@ jobs: $exeName = $_.Name # Skip as they currently do not have manifests - if ($exeName -eq "fuzz.exe" -or $exeName -eq "bench_bitcoin.exe" -or $exeName -eq "test_bitcoin-qt.exe") { + if ($exeName -eq "fuzz.exe" -or $exeName -eq "bench_bitcoin.exe" -or $exeName -eq "test_bitcoin-qt.exe" -or $exeName -eq "test_kernel.exe") { Write-Host "Skipping $exeName (no manifest present)" return } @@ -399,7 +399,7 @@ jobs: $exeName = $_.Name # Skip as they currently do not have manifests - if ($exeName -eq "fuzz.exe" -or $exeName -eq "bench_bitcoin.exe") { + if ($exeName -eq "fuzz.exe" -or $exeName -eq "bench_bitcoin.exe" -or $exeName -eq "test_kernel.exe") { Write-Host "Skipping $exeName (no manifest present)" return } diff --git a/CMakeLists.txt b/CMakeLists.txt index 4c10fb73fad9..2e5196c2fbe3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -105,6 +105,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) if(ENABLE_WALLET) @@ -210,6 +211,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) @@ -668,6 +670,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}") message(" external signer ..................... ${ENABLE_EXTERNAL_SIGNER}") diff --git a/ci/test/00_setup_env_mac_native.sh b/ci/test/00_setup_env_mac_native.sh index 41a3bc458777..3584246c2572 100755 --- a/ci/test/00_setup_env_mac_native.sh +++ b/ci/test/00_setup_env_mac_native.sh @@ -12,7 +12,7 @@ export CONTAINER_NAME="ci_mac_native" # macos does not use a container, but the export PIP_PACKAGES="--break-system-packages zmq" export GOAL="install deploy" export CMAKE_GENERATOR="Ninja" -export BITCOIN_CONFIG="-DBUILD_GUI=ON -DWITH_ZMQ=ON -DREDUCE_EXPORTS=ON -DCMAKE_EXE_LINKER_FLAGS='-Wl,-stack_size -Wl,0x80000'" +export BITCOIN_CONFIG="-DBUILD_GUI=ON -DWITH_ZMQ=ON -DBUILD_KERNEL_LIB=ON -DBUILD_KERNEL_TEST=ON -DREDUCE_EXPORTS=ON -DCMAKE_EXE_LINKER_FLAGS='-Wl,-stack_size -Wl,0x80000'" export CI_OS_NAME="macos" export NO_DEPENDS=1 export OSX_SDK="" diff --git a/ci/test/00_setup_env_native_nowallet_libbitcoinkernel.sh b/ci/test/00_setup_env_native_nowallet_libbitcoinkernel.sh index 58561e552040..b1e0c65e246c 100755 --- a/ci/test/00_setup_env_native_nowallet_libbitcoinkernel.sh +++ b/ci/test/00_setup_env_native_nowallet_libbitcoinkernel.sh @@ -13,4 +13,4 @@ export PACKAGES="python3-zmq python3-pip clang-16 llvm-16 libc++abi-16-dev libc+ export PIP_PACKAGES="--break-system-packages pycapnp" export DEP_OPTS="NO_WALLET=1 CC=clang-16 CXX='clang++-16 -stdlib=libc++'" export GOAL="install" -export BITCOIN_CONFIG="-DREDUCE_EXPORTS=ON -DBUILD_UTIL_CHAINSTATE=ON -DBUILD_KERNEL_LIB=ON -DBUILD_SHARED_LIBS=ON" +export BITCOIN_CONFIG="-DREDUCE_EXPORTS=ON -DBUILD_UTIL_CHAINSTATE=ON -DBUILD_KERNEL_LIB=ON -DBUILD_KERNEL_TEST=ON -DBUILD_SHARED_LIBS=ON" diff --git a/ci/test/00_setup_env_win64.sh b/ci/test/00_setup_env_win64.sh index 13e794bd6d1d..71e1e36f1d08 100755 --- a/ci/test/00_setup_env_win64.sh +++ b/ci/test/00_setup_env_win64.sh @@ -14,5 +14,5 @@ export PACKAGES="g++-mingw-w64-x86-64-posix nsis" export RUN_UNIT_TESTS=false export RUN_FUNCTIONAL_TESTS=false export GOAL="deploy" -export BITCOIN_CONFIG="-DREDUCE_EXPORTS=ON -DBUILD_GUI_TESTS=OFF \ +export BITCOIN_CONFIG="-DREDUCE_EXPORTS=ON -DBUILD_GUI_TESTS=OFF -DBUILD_KERNEL_LIB=ON -DBUILD_KERNEL_TEST=ON \ -DCMAKE_CXX_FLAGS='-Wno-error=maybe-uninitialized'" diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f9934bb59060..7bb5857f47ed 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -404,6 +404,9 @@ endif() if(BUILD_KERNEL_LIB) add_subdirectory(kernel) + if (BUILD_KERNEL_TEST) + add_subdirectory(test/kernel) + endif() endif() if(BUILD_UTIL_CHAINSTATE) diff --git a/src/kernel/CMakeLists.txt b/src/kernel/CMakeLists.txt index c45ce7819e98..7b2f3c7d11f3 100644 --- a/src/kernel/CMakeLists.txt +++ b/src/kernel/CMakeLists.txt @@ -110,6 +110,11 @@ set_target_properties(bitcoinkernel PROPERTIES add_custom_target(libbitcoinkernel) add_dependencies(libbitcoinkernel bitcoinkernel) +get_target_property(bitcoinkernel_type bitcoinkernel TYPE) +if(bitcoinkernel_type STREQUAL "STATIC_LIBRARY") + target_compile_definitions(bitcoinkernel PUBLIC BITCOINKERNEL_STATIC) +endif() + configure_file(${PROJECT_SOURCE_DIR}/libbitcoinkernel.pc.in ${PROJECT_BINARY_DIR}/libbitcoinkernel.pc @ONLY) install(FILES ${PROJECT_BINARY_DIR}/libbitcoinkernel.pc DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig" COMPONENT libbitcoinkernel) @@ -124,3 +129,5 @@ install(TARGETS bitcoinkernel DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT libbitcoinkernel ) + +install(FILES bitcoinkernel.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} COMPONENT libbitcoinkernel) diff --git a/src/kernel/bitcoinkernel.cpp b/src/kernel/bitcoinkernel.cpp index 9096b6f71de7..4a6b254be233 100644 --- a/src/kernel/bitcoinkernel.cpp +++ b/src/kernel/bitcoinkernel.cpp @@ -1,11 +1,253 @@ -// 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. + +#define BITCOINKERNEL_BUILD + +#include + +#include +#include +#include +#include