From 265d6393bf9ef52e7ef7de97ca9c031da82a5ad1 Mon Sep 17 00:00:00 2001 From: Carl Dong Date: Fri, 27 May 2022 16:47:05 -0400 Subject: [PATCH] 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 diff --git a/src/Makefile.am b/src/Makefile.am index 765947f0357..ba0e9ac7368 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 6749ed5918b..3f2b298c1f3 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 0cf9ad49dce..92e73d7c2a6 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 aaabbd9af60..108f5c99d32 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 4251fa33ae9..e8e6a55ebad 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 e5dc097bc3c..d4e45454d23 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 bbd57718406..2c7f4859081 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 00000000000..a25617bea51 --- /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 00000000000..786281fa2c1 --- /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 40defd5bab8..7752fb0f65c 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;