From eeb4fc20c578b1e428a92d64cc9f8f903a677580 Mon Sep 17 00:00:00 2001 From: Carl Dong Date: Wed, 2 Mar 2022 20:04:12 -0500 Subject: test: Use Set/UnsetGlobals in BasicTestingSetup ...instead of calling initialization functions directly and having to keep around a ECCVerifyHandle member variable. This makes the initialization codepath of our tests more closely resemble those of AppInitMain and potentially eases the review of subsequent commit removing init::{Set,Unset}Globals. [META] In a future commit, we will introduce a kernel::Context which calls init::{Set,Unset}Globals in its ctor and dtor. It will be owned by node::NodeContext, so in the end, this patchset won't have made the previously local ECCVerifyHandle global. --- src/test/util/setup_common.cpp | 6 +++--- src/test/util/setup_common.h | 1 - 2 files changed, 3 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/test/util/setup_common.cpp b/src/test/util/setup_common.cpp index b7566bd1fa..7b70ace759 100644 --- a/src/test/util/setup_common.cpp +++ b/src/test/util/setup_common.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -125,8 +126,7 @@ BasicTestingSetup::BasicTestingSetup(const std::string& chainName, const std::ve InitLogging(*m_node.args); AppInitParameterInteraction(*m_node.args); LogInstance().StartLogging(); - SHA256AutoDetect(); - ECC_Start(); + init::SetGlobals(); SetupEnvironment(); SetupNetworking(); InitSignatureCache(); @@ -146,7 +146,7 @@ BasicTestingSetup::~BasicTestingSetup() LogInstance().DisconnectTestLogger(); fs::remove_all(m_path_root); gArgs.ClearArgs(); - ECC_Stop(); + init::UnsetGlobals(); } ChainTestingSetup::ChainTestingSetup(const std::string& chainName, const std::vector& extra_args) diff --git a/src/test/util/setup_common.h b/src/test/util/setup_common.h index a1b7525cf4..3030271827 100644 --- a/src/test/util/setup_common.h +++ b/src/test/util/setup_common.h @@ -81,7 +81,6 @@ static constexpr CAmount CENT{1000000}; * This just configures logging, data dir and chain parameters. */ struct BasicTestingSetup { - ECCVerifyHandle globalVerifyHandle; node::NodeContext m_node; explicit BasicTestingSetup(const std::string& chainName = CBaseChainParams::MAIN, const std::vector& extra_args = {}); -- cgit v1.2.3 From 7d03feef8156ef37a4efa01dc591467bc7d957bf Mon Sep 17 00:00:00 2001 From: Carl Dong Date: Wed, 25 May 2022 14:55:44 -0400 Subject: kernel: Introduce empty and unused kernel::Context [META] In the next commit, we will move the init::{Set,Unset}Globals logic into this struct. Co-Authored-By: Ryan Ofsky --- src/Makefile.am | 1 + src/kernel/context.h | 20 ++++++++++++++++++++ src/node/context.h | 4 ++++ 3 files changed, 25 insertions(+) create mode 100644 src/kernel/context.h (limited to 'src') diff --git a/src/Makefile.am b/src/Makefile.am index a6e9048949..39b19b5e5c 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -172,6 +172,7 @@ BITCOIN_CORE_H = \ interfaces/wallet.h \ kernel/chainstatemanager_opts.h \ kernel/coinstats.h \ + kernel/context.h \ key.h \ key_io.h \ logging.h \ diff --git a/src/kernel/context.h b/src/kernel/context.h new file mode 100644 index 0000000000..e304dcb006 --- /dev/null +++ b/src/kernel/context.h @@ -0,0 +1,20 @@ +// Copyright (c) 2022 The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#ifndef BITCOIN_KERNEL_CONTEXT_H +#define BITCOIN_KERNEL_CONTEXT_H + +namespace kernel { +//! Context struct holding the kernel library's logically global state, and +//! passed to external libbitcoin_kernel functions which need access to this +//! state. The kernel libary API is a work in progress, so state organization +//! and member list will evolve over time. +//! +//! State stored directly in this struct should be simple. More complex state +//! should be stored to std::unique_ptr members pointing to opaque types. +struct Context { +}; +} // namespace kernel + +#endif // BITCOIN_KERNEL_CONTEXT_H diff --git a/src/node/context.h b/src/node/context.h index 91ba456219..31be308787 100644 --- a/src/node/context.h +++ b/src/node/context.h @@ -5,6 +5,8 @@ #ifndef BITCOIN_NODE_CONTEXT_H #define BITCOIN_NODE_CONTEXT_H +#include + #include #include #include @@ -39,6 +41,8 @@ namespace node { //! any member functions. It should just be a collection of references that can //! be used without pulling in unwanted dependencies or functionality. struct NodeContext { + //! libbitcoin_kernel context + std::unique_ptr kernel; //! Init interface for initializing current process and connecting to other processes. interfaces::Init* init{nullptr}; std::unique_ptr addrman; -- cgit v1.2.3 From fed085a1a4cd2787202752b6a0d98e42dce97f09 Mon Sep 17 00:00:00 2001 From: Carl Dong Date: Wed, 25 May 2022 14:31:54 -0400 Subject: init: Initialize globals with kernel::Context's life ...instead of explicitly calling init::{Set,Unset}Globals. Cool thing about this is that in both the testing and bitcoin-chainstate codepaths, we no longer need to explicitly unset globals. The kernel::Context goes out of scope and the globals are unset "automatically". Also construct kernel::Context outside of AppInitSanityChecks() --- src/Makefile.am | 3 ++- src/bitcoin-chainstate.cpp | 6 +++--- src/bitcoind.cpp | 3 +++ src/init.cpp | 5 +---- src/init.h | 3 +++ src/init/common.cpp | 20 -------------------- src/init/common.h | 2 -- src/kernel/context.cpp | 33 +++++++++++++++++++++++++++++++++ src/kernel/context.h | 11 +++++++++++ src/node/context.cpp | 1 + src/node/interfaces.cpp | 12 ++++++++++-- src/test/util/setup_common.cpp | 3 +-- src/test/util/setup_common.h | 2 +- 13 files changed, 69 insertions(+), 35 deletions(-) create mode 100644 src/kernel/context.cpp (limited to 'src') diff --git a/src/Makefile.am b/src/Makefile.am index 39b19b5e5c..765947f035 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -357,6 +357,7 @@ libbitcoin_node_a_SOURCES = \ index/txindex.cpp \ init.cpp \ kernel/coinstats.cpp \ + kernel/context.cpp \ mapport.cpp \ net.cpp \ netgroup.cpp \ @@ -865,8 +866,8 @@ libbitcoinkernel_la_SOURCES = \ flatfile.cpp \ fs.cpp \ hash.cpp \ - init/common.cpp \ kernel/coinstats.cpp \ + kernel/context.cpp \ key.cpp \ logging.cpp \ node/blockstorage.cpp \ diff --git a/src/bitcoin-chainstate.cpp b/src/bitcoin-chainstate.cpp index 99aa23fb06..6749ed5918 100644 --- a/src/bitcoin-chainstate.cpp +++ b/src/bitcoin-chainstate.cpp @@ -11,6 +11,8 @@ // // It is part of the libbitcoinkernel project. +#include + #include #include #include @@ -49,7 +51,7 @@ int main(int argc, char* argv[]) SelectParams(CBaseChainParams::MAIN); const CChainParams& chainparams = Params(); - init::SetGlobals(); // ECC_Start, etc. + kernel::Context kernel_context{}; // Necessary for CheckInputScripts (eventually called by ProcessNewBlock), // which will try the script cache first and fall back to actually @@ -254,6 +256,4 @@ epilogue: } } GetMainSignals().UnregisterBackgroundSignalScheduler(); - - init::UnsetGlobals(); } diff --git a/src/bitcoind.cpp b/src/bitcoind.cpp index bc063faed1..0cf9ad49dc 100644 --- a/src/bitcoind.cpp +++ b/src/bitcoind.cpp @@ -188,11 +188,14 @@ static bool AppInit(NodeContext& node, int argc, char* argv[]) // InitError will have been called with detailed error, which ends up on console return false; } + + node.kernel = std::make_unique(); if (!AppInitSanityChecks()) { // InitError will have been called with detailed error, which ends up on console return false; } + if (args.GetBoolArg("-daemon", DEFAULT_DAEMON) || args.GetBoolArg("-daemonwait", DEFAULT_DAEMONWAIT)) { #if HAVE_DECL_FORK tfm::format(std::cout, PACKAGE_NAME " starting\n"); diff --git a/src/init.cpp b/src/init.cpp index 045808cc71..aaabbd9af6 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -304,7 +304,7 @@ void Shutdown(NodeContext& node) node.chain_clients.clear(); UnregisterAllValidationInterfaces(); GetMainSignals().UnregisterBackgroundSignalScheduler(); - init::UnsetGlobals(); + node.kernel.reset(); node.mempool.reset(); node.fee_estimator.reset(); node.chainman.reset(); @@ -1092,9 +1092,6 @@ static bool LockDataDirectory(bool probeOnly) bool AppInitSanityChecks() { // ********************************************************* Step 4: sanity checks - - init::SetGlobals(); - if (!init::SanityChecks()) { return InitError(strprintf(_("Initialization sanity check failed. %s is shutting down."), PACKAGE_NAME)); } diff --git a/src/init.h b/src/init.h index 1e22771dc2..4251fa33ae 100644 --- a/src/init.h +++ b/src/init.h @@ -19,6 +19,9 @@ class ArgsManager; namespace interfaces { struct BlockAndHeaderTipInfo; } +namespace kernel { +struct Context; +} namespace node { struct NodeContext; } // namespace node diff --git a/src/init/common.cpp b/src/init/common.cpp index 788abb9821..e5dc097bc3 100644 --- a/src/init/common.cpp +++ b/src/init/common.cpp @@ -7,12 +7,10 @@ #endif #include -#include #include #include #include #include -#include #include #include #include @@ -20,28 +18,10 @@ #include #include -#include #include #include -static std::unique_ptr globalVerifyHandle; - namespace init { -void SetGlobals() -{ - std::string sha256_algo = SHA256AutoDetect(); - LogPrintf("Using the '%s' SHA256 implementation\n", sha256_algo); - RandomInit(); - ECC_Start(); - globalVerifyHandle.reset(new ECCVerifyHandle()); -} - -void UnsetGlobals() -{ - globalVerifyHandle.reset(); - ECC_Stop(); -} - bool SanityChecks() { if (!ECC_InitSanityCheck()) { diff --git a/src/init/common.h b/src/init/common.h index fc4bc1b280..bbd5771840 100644 --- a/src/init/common.h +++ b/src/init/common.h @@ -11,8 +11,6 @@ class ArgsManager; namespace init { -void SetGlobals(); -void UnsetGlobals(); /** * Ensure a usable environment with all * necessary library support. diff --git a/src/kernel/context.cpp b/src/kernel/context.cpp new file mode 100644 index 0000000000..15413c1840 --- /dev/null +++ b/src/kernel/context.cpp @@ -0,0 +1,33 @@ +// Copyright (c) 2022 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 +#include + +#include + + +namespace kernel { + +Context::Context() +{ + std::string sha256_algo = SHA256AutoDetect(); + LogPrintf("Using the '%s' SHA256 implementation\n", sha256_algo); + RandomInit(); + ECC_Start(); + ecc_verify_handle.reset(new ECCVerifyHandle()); +} + +Context::~Context() +{ + ecc_verify_handle.reset(); + ECC_Stop(); +} + +} // namespace kernel diff --git a/src/kernel/context.h b/src/kernel/context.h index e304dcb006..0a08511564 100644 --- a/src/kernel/context.h +++ b/src/kernel/context.h @@ -5,6 +5,10 @@ #ifndef BITCOIN_KERNEL_CONTEXT_H #define BITCOIN_KERNEL_CONTEXT_H +#include + +class ECCVerifyHandle; + namespace kernel { //! Context struct holding the kernel library's logically global state, and //! passed to external libbitcoin_kernel functions which need access to this @@ -14,6 +18,13 @@ namespace kernel { //! State stored directly in this struct should be simple. More complex state //! should be stored to std::unique_ptr members pointing to opaque types. struct Context { + std::unique_ptr ecc_verify_handle; + + //! Declare default constructor and destructor that are not inline, so code + //! instantiating the kernel::Context struct doesn't need to #include class + //! definitions for all the unique_ptr members. + Context(); + ~Context(); }; } // namespace kernel diff --git a/src/node/context.cpp b/src/node/context.cpp index 4787efa1de..d80b8ca7a7 100644 --- a/src/node/context.cpp +++ b/src/node/context.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include diff --git a/src/node/interfaces.cpp b/src/node/interfaces.cpp index 4810ae1f68..40defd5bab 100644 --- a/src/node/interfaces.cpp +++ b/src/node/interfaces.cpp @@ -90,8 +90,16 @@ public: uint32_t getLogCategories() override { return LogInstance().GetCategoryMask(); } bool baseInitialize() override { - return AppInitBasicSetup(gArgs) && AppInitParameterInteraction(gArgs, /*use_syscall_sandbox=*/false) && AppInitSanityChecks() && - AppInitLockDataDirectory() && AppInitInterfaces(*m_context); + if (!AppInitBasicSetup(gArgs)) return false; + if (!AppInitParameterInteraction(gArgs, /*use_syscall_sandbox=*/false)) return false; + + m_context->kernel = std::make_unique(); + if (!AppInitSanityChecks()) return false; + + if (!AppInitLockDataDirectory()) return false; + if (!AppInitInterfaces(*m_context)) return false; + + return true; } bool appInitMain(interfaces::BlockAndHeaderTipInfo* tip_info) override { diff --git a/src/test/util/setup_common.cpp b/src/test/util/setup_common.cpp index 7b70ace759..61af5d4418 100644 --- a/src/test/util/setup_common.cpp +++ b/src/test/util/setup_common.cpp @@ -126,7 +126,7 @@ BasicTestingSetup::BasicTestingSetup(const std::string& chainName, const std::ve InitLogging(*m_node.args); AppInitParameterInteraction(*m_node.args); LogInstance().StartLogging(); - init::SetGlobals(); + m_node.kernel = std::make_unique(); SetupEnvironment(); SetupNetworking(); InitSignatureCache(); @@ -146,7 +146,6 @@ BasicTestingSetup::~BasicTestingSetup() LogInstance().DisconnectTestLogger(); fs::remove_all(m_path_root); gArgs.ClearArgs(); - init::UnsetGlobals(); } ChainTestingSetup::ChainTestingSetup(const std::string& chainName, const std::vector& extra_args) diff --git a/src/test/util/setup_common.h b/src/test/util/setup_common.h index 3030271827..5c31cfc22b 100644 --- a/src/test/util/setup_common.h +++ b/src/test/util/setup_common.h @@ -81,7 +81,7 @@ static constexpr CAmount CENT{1000000}; * This just configures logging, data dir and chain parameters. */ struct BasicTestingSetup { - node::NodeContext m_node; + node::NodeContext m_node; // keep as first member to be destructed last explicit BasicTestingSetup(const std::string& chainName = CBaseChainParams::MAIN, const std::vector& extra_args = {}); ~BasicTestingSetup(); -- cgit v1.2.3 From 265d6393bf9ef52e7ef7de97ca9c031da82a5ad1 Mon Sep 17 00:00:00 2001 From: Carl Dong Date: Fri, 27 May 2022 16:47:05 -0400 Subject: Move init::SanityCheck to kernel::SanityCheck --- src/Makefile.am | 3 +++ src/bitcoin-chainstate.cpp | 7 ++++++- src/bitcoind.cpp | 2 +- src/init.cpp | 6 ++++-- src/init.h | 2 +- src/init/common.cpp | 19 ------------------- src/init/common.h | 5 ----- src/kernel/checks.cpp | 33 +++++++++++++++++++++++++++++++++ src/kernel/checks.h | 19 +++++++++++++++++++ src/node/interfaces.cpp | 2 +- 10 files changed, 68 insertions(+), 30 deletions(-) create mode 100644 src/kernel/checks.cpp create mode 100644 src/kernel/checks.h (limited to 'src') diff --git a/src/Makefile.am b/src/Makefile.am index 765947f035..ba0e9ac736 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -171,6 +171,7 @@ BITCOIN_CORE_H = \ interfaces/node.h \ interfaces/wallet.h \ kernel/chainstatemanager_opts.h \ + kernel/checks.h \ kernel/coinstats.h \ kernel/context.h \ key.h \ @@ -356,6 +357,7 @@ libbitcoin_node_a_SOURCES = \ index/coinstatsindex.cpp \ index/txindex.cpp \ init.cpp \ + kernel/checks.cpp \ kernel/coinstats.cpp \ kernel/context.cpp \ mapport.cpp \ @@ -866,6 +868,7 @@ libbitcoinkernel_la_SOURCES = \ flatfile.cpp \ fs.cpp \ hash.cpp \ + kernel/checks.cpp \ kernel/coinstats.cpp \ kernel/context.cpp \ key.cpp \ diff --git a/src/bitcoin-chainstate.cpp b/src/bitcoin-chainstate.cpp index 6749ed5918..3f2b298c1f 100644 --- a/src/bitcoin-chainstate.cpp +++ b/src/bitcoin-chainstate.cpp @@ -11,12 +11,12 @@ // // It is part of the libbitcoinkernel project. +#include #include #include #include #include -#include #include #include #include @@ -26,6 +26,7 @@ #include #include +#include #include #include #include @@ -52,6 +53,10 @@ int main(int argc, char* argv[]) const CChainParams& chainparams = Params(); kernel::Context kernel_context{}; + // We can't use a goto here, but we can use an assert since none of the + // things instantiated so far requires running the epilogue to be torn down + // properly + assert(kernel::SanityChecks(kernel_context)); // Necessary for CheckInputScripts (eventually called by ProcessNewBlock), // which will try the script cache first and fall back to actually diff --git a/src/bitcoind.cpp b/src/bitcoind.cpp index 0cf9ad49dc..92e73d7c2a 100644 --- a/src/bitcoind.cpp +++ b/src/bitcoind.cpp @@ -190,7 +190,7 @@ static bool AppInit(NodeContext& node, int argc, char* argv[]) } node.kernel = std::make_unique(); - if (!AppInitSanityChecks()) + if (!AppInitSanityChecks(*node.kernel)) { // InitError will have been called with detailed error, which ends up on console return false; diff --git a/src/init.cpp b/src/init.cpp index aaabbd9af6..108f5c99d3 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -9,6 +9,8 @@ #include +#include + #include #include #include @@ -1089,10 +1091,10 @@ static bool LockDataDirectory(bool probeOnly) return true; } -bool AppInitSanityChecks() +bool AppInitSanityChecks(const kernel::Context& kernel) { // ********************************************************* Step 4: sanity checks - if (!init::SanityChecks()) { + if (!kernel::SanityChecks(kernel)) { return InitError(strprintf(_("Initialization sanity check failed. %s is shutting down."), PACKAGE_NAME)); } diff --git a/src/init.h b/src/init.h index 4251fa33ae..e8e6a55eba 100644 --- a/src/init.h +++ b/src/init.h @@ -50,7 +50,7 @@ bool AppInitParameterInteraction(const ArgsManager& args, bool use_syscall_sandb * @note This can be done before daemonization. Do not call Shutdown() if this function fails. * @pre Parameters should be parsed and config file should be read, AppInitParameterInteraction should have been called. */ -bool AppInitSanityChecks(); +bool AppInitSanityChecks(const kernel::Context& kernel); /** * Lock bitcoin core data directory. * @note This should only be done after daemonization. Do not call Shutdown() if this function fails. diff --git a/src/init/common.cpp b/src/init/common.cpp index e5dc097bc3..d4e45454d2 100644 --- a/src/init/common.cpp +++ b/src/init/common.cpp @@ -8,10 +8,8 @@ #include #include -#include #include #include -#include #include #include #include @@ -22,23 +20,6 @@ #include namespace init { -bool SanityChecks() -{ - if (!ECC_InitSanityCheck()) { - return InitError(Untranslated("Elliptic curve cryptography sanity check failure. Aborting.")); - } - - if (!Random_SanityCheck()) { - return InitError(Untranslated("OS cryptographic RNG sanity check failure. Aborting.")); - } - - if (!ChronoSanityCheck()) { - return InitError(Untranslated("Clock epoch mismatch. Aborting.")); - } - - return true; -} - void AddLoggingArgs(ArgsManager& argsman) { argsman.AddArg("-debuglogfile=", strprintf("Specify location of debug log file. Relative paths will be prefixed by a net-specific datadir location. (-nodebuglogfile to disable; default: %s)", DEFAULT_DEBUGLOGFILE), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS); diff --git a/src/init/common.h b/src/init/common.h index bbd5771840..2c7f485908 100644 --- a/src/init/common.h +++ b/src/init/common.h @@ -11,11 +11,6 @@ class ArgsManager; namespace init { -/** - * Ensure a usable environment with all - * necessary library support. - */ -bool SanityChecks(); void AddLoggingArgs(ArgsManager& args); void SetLoggingOptions(const ArgsManager& args); void SetLoggingCategories(const ArgsManager& args); diff --git a/src/kernel/checks.cpp b/src/kernel/checks.cpp new file mode 100644 index 0000000000..a25617bea5 --- /dev/null +++ b/src/kernel/checks.cpp @@ -0,0 +1,33 @@ +// Copyright (c) 2022 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 +#include + +#include + +namespace kernel { + +bool SanityChecks(const Context&) { + if (!ECC_InitSanityCheck()) { + return InitError(Untranslated("Elliptic curve cryptography sanity check failure. Aborting.")); + } + + if (!Random_SanityCheck()) { + return InitError(Untranslated("OS cryptographic RNG sanity check failure. Aborting.")); + } + + if (!ChronoSanityCheck()) { + return InitError(Untranslated("Clock epoch mismatch. Aborting.")); + } + + return true; +} + +} diff --git a/src/kernel/checks.h b/src/kernel/checks.h new file mode 100644 index 0000000000..786281fa2c --- /dev/null +++ b/src/kernel/checks.h @@ -0,0 +1,19 @@ +// Copyright (c) 2022 The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#ifndef BITCOIN_KERNEL_CHECKS_H +#define BITCOIN_KERNEL_CHECKS_H + +namespace kernel { + +struct Context; + +/** + * Ensure a usable environment with all necessary library support. + */ +bool SanityChecks(const Context&); + +} + +#endif // BITCOIN_KERNEL_CHECKS_H diff --git a/src/node/interfaces.cpp b/src/node/interfaces.cpp index 40defd5bab..7752fb0f65 100644 --- a/src/node/interfaces.cpp +++ b/src/node/interfaces.cpp @@ -94,7 +94,7 @@ public: if (!AppInitParameterInteraction(gArgs, /*use_syscall_sandbox=*/false)) return false; m_context->kernel = std::make_unique(); - if (!AppInitSanityChecks()) return false; + if (!AppInitSanityChecks(*m_context->kernel)) return false; if (!AppInitLockDataDirectory()) return false; if (!AppInitInterfaces(*m_context)) return false; -- cgit v1.2.3 From d87784ac87364fc977bbf9769c8bdb72dea8cbf9 Mon Sep 17 00:00:00 2001 From: Carl Dong Date: Wed, 25 May 2022 18:02:54 -0400 Subject: kernel: SanityChecks: Return an error struct This reduces libbitcoinkernel's coupling with ui_interface and translation. --- src/bitcoin-chainstate.cpp | 2 +- src/init.cpp | 16 +++++++++++++++- src/kernel/checks.cpp | 15 ++++++--------- src/kernel/checks.h | 10 +++++++++- 4 files changed, 31 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/bitcoin-chainstate.cpp b/src/bitcoin-chainstate.cpp index 3f2b298c1f..1817aa1a53 100644 --- a/src/bitcoin-chainstate.cpp +++ b/src/bitcoin-chainstate.cpp @@ -56,7 +56,7 @@ int main(int argc, char* argv[]) // We can't use a goto here, but we can use an assert since none of the // things instantiated so far requires running the epilogue to be torn down // properly - assert(kernel::SanityChecks(kernel_context)); + assert(!kernel::SanityChecks(kernel_context).has_value()); // Necessary for CheckInputScripts (eventually called by ProcessNewBlock), // which will try the script cache first and fall back to actually diff --git a/src/init.cpp b/src/init.cpp index 108f5c99d3..d0fd6074b1 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -1094,7 +1094,21 @@ static bool LockDataDirectory(bool probeOnly) bool AppInitSanityChecks(const kernel::Context& kernel) { // ********************************************************* Step 4: sanity checks - if (!kernel::SanityChecks(kernel)) { + auto maybe_error = kernel::SanityChecks(kernel); + + if (maybe_error.has_value()) { + switch (maybe_error.value()) { + case kernel::SanityCheckError::ERROR_ECC: + InitError(Untranslated("Elliptic curve cryptography sanity check failure. Aborting.")); + break; + case kernel::SanityCheckError::ERROR_RANDOM: + InitError(Untranslated("OS cryptographic RNG sanity check failure. Aborting.")); + break; + case kernel::SanityCheckError::ERROR_CHRONO: + InitError(Untranslated("Clock epoch mismatch. Aborting.")); + break; + } // no default case, so the compiler can warn about missing cases + return InitError(strprintf(_("Initialization sanity check failed. %s is shutting down."), PACKAGE_NAME)); } diff --git a/src/kernel/checks.cpp b/src/kernel/checks.cpp index a25617bea5..2a1dd3bfa2 100644 --- a/src/kernel/checks.cpp +++ b/src/kernel/checks.cpp @@ -5,29 +5,26 @@ #include #include -#include #include #include -#include - -#include namespace kernel { -bool SanityChecks(const Context&) { +std::optional SanityChecks(const Context&) +{ if (!ECC_InitSanityCheck()) { - return InitError(Untranslated("Elliptic curve cryptography sanity check failure. Aborting.")); + return SanityCheckError::ERROR_ECC; } if (!Random_SanityCheck()) { - return InitError(Untranslated("OS cryptographic RNG sanity check failure. Aborting.")); + return SanityCheckError::ERROR_RANDOM; } if (!ChronoSanityCheck()) { - return InitError(Untranslated("Clock epoch mismatch. Aborting.")); + return SanityCheckError::ERROR_CHRONO; } - return true; + return std::nullopt; } } diff --git a/src/kernel/checks.h b/src/kernel/checks.h index 786281fa2c..80b207f607 100644 --- a/src/kernel/checks.h +++ b/src/kernel/checks.h @@ -5,14 +5,22 @@ #ifndef BITCOIN_KERNEL_CHECKS_H #define BITCOIN_KERNEL_CHECKS_H +#include + namespace kernel { struct Context; +enum class SanityCheckError { + ERROR_ECC, + ERROR_RANDOM, + ERROR_CHRONO, +}; + /** * Ensure a usable environment with all necessary library support. */ -bool SanityChecks(const Context&); +std::optional SanityChecks(const Context&); } -- cgit v1.2.3