Skip to content

Commit 265d639

Browse files
committed
Move init::SanityCheck to kernel::SanityCheck
1 parent fed085a commit 265d639

File tree

10 files changed

+68
-30
lines changed

10 files changed

+68
-30
lines changed

src/Makefile.am

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ BITCOIN_CORE_H = \
171171
interfaces/node.h \
172172
interfaces/wallet.h \
173173
kernel/chainstatemanager_opts.h \
174+
kernel/checks.h \
174175
kernel/coinstats.h \
175176
kernel/context.h \
176177
key.h \
@@ -356,6 +357,7 @@ libbitcoin_node_a_SOURCES = \
356357
index/coinstatsindex.cpp \
357358
index/txindex.cpp \
358359
init.cpp \
360+
kernel/checks.cpp \
359361
kernel/coinstats.cpp \
360362
kernel/context.cpp \
361363
mapport.cpp \
@@ -866,6 +868,7 @@ libbitcoinkernel_la_SOURCES = \
866868
flatfile.cpp \
867869
fs.cpp \
868870
hash.cpp \
871+
kernel/checks.cpp \
869872
kernel/coinstats.cpp \
870873
kernel/context.cpp \
871874
key.cpp \

src/bitcoin-chainstate.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
//
1212
// It is part of the libbitcoinkernel project.
1313

14+
#include <kernel/checks.h>
1415
#include <kernel/context.h>
1516

1617
#include <chainparams.h>
1718
#include <consensus/validation.h>
1819
#include <core_io.h>
19-
#include <init/common.h>
2020
#include <node/blockstorage.h>
2121
#include <node/chainstate.h>
2222
#include <scheduler.h>
@@ -26,6 +26,7 @@
2626
#include <validation.h>
2727
#include <validationinterface.h>
2828

29+
#include <cassert>
2930
#include <filesystem>
3031
#include <functional>
3132
#include <iosfwd>
@@ -52,6 +53,10 @@ int main(int argc, char* argv[])
5253
const CChainParams& chainparams = Params();
5354

5455
kernel::Context kernel_context{};
56+
// We can't use a goto here, but we can use an assert since none of the
57+
// things instantiated so far requires running the epilogue to be torn down
58+
// properly
59+
assert(kernel::SanityChecks(kernel_context));
5560

5661
// Necessary for CheckInputScripts (eventually called by ProcessNewBlock),
5762
// which will try the script cache first and fall back to actually

src/bitcoind.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ static bool AppInit(NodeContext& node, int argc, char* argv[])
190190
}
191191

192192
node.kernel = std::make_unique<kernel::Context>();
193-
if (!AppInitSanityChecks())
193+
if (!AppInitSanityChecks(*node.kernel))
194194
{
195195
// InitError will have been called with detailed error, which ends up on console
196196
return false;

src/init.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
#include <init.h>
1111

12+
#include <kernel/checks.h>
13+
1214
#include <addrman.h>
1315
#include <banman.h>
1416
#include <blockfilter.h>
@@ -1089,10 +1091,10 @@ static bool LockDataDirectory(bool probeOnly)
10891091
return true;
10901092
}
10911093

1092-
bool AppInitSanityChecks()
1094+
bool AppInitSanityChecks(const kernel::Context& kernel)
10931095
{
10941096
// ********************************************************* Step 4: sanity checks
1095-
if (!init::SanityChecks()) {
1097+
if (!kernel::SanityChecks(kernel)) {
10961098
return InitError(strprintf(_("Initialization sanity check failed. %s is shutting down."), PACKAGE_NAME));
10971099
}
10981100

src/init.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ bool AppInitParameterInteraction(const ArgsManager& args, bool use_syscall_sandb
5050
* @note This can be done before daemonization. Do not call Shutdown() if this function fails.
5151
* @pre Parameters should be parsed and config file should be read, AppInitParameterInteraction should have been called.
5252
*/
53-
bool AppInitSanityChecks();
53+
bool AppInitSanityChecks(const kernel::Context& kernel);
5454
/**
5555
* Lock bitcoin core data directory.
5656
* @note This should only be done after daemonization. Do not call Shutdown() if this function fails.

src/init/common.cpp

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@
88

99
#include <clientversion.h>
1010
#include <fs.h>
11-
#include <key.h>
1211
#include <logging.h>
1312
#include <node/ui_interface.h>
14-
#include <random.h>
1513
#include <tinyformat.h>
1614
#include <util/system.h>
1715
#include <util/time.h>
@@ -22,23 +20,6 @@
2220
#include <vector>
2321

2422
namespace init {
25-
bool SanityChecks()
26-
{
27-
if (!ECC_InitSanityCheck()) {
28-
return InitError(Untranslated("Elliptic curve cryptography sanity check failure. Aborting."));
29-
}
30-
31-
if (!Random_SanityCheck()) {
32-
return InitError(Untranslated("OS cryptographic RNG sanity check failure. Aborting."));
33-
}
34-
35-
if (!ChronoSanityCheck()) {
36-
return InitError(Untranslated("Clock epoch mismatch. Aborting."));
37-
}
38-
39-
return true;
40-
}
41-
4223
void AddLoggingArgs(ArgsManager& argsman)
4324
{
4425
argsman.AddArg("-debuglogfile=<file>", 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);

src/init/common.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@
1111
class ArgsManager;
1212

1313
namespace init {
14-
/**
15-
* Ensure a usable environment with all
16-
* necessary library support.
17-
*/
18-
bool SanityChecks();
1914
void AddLoggingArgs(ArgsManager& args);
2015
void SetLoggingOptions(const ArgsManager& args);
2116
void SetLoggingCategories(const ArgsManager& args);

src/kernel/checks.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright (c) 2022 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <kernel/checks.h>
6+
7+
#include <key.h>
8+
#include <node/ui_interface.h>
9+
#include <random.h>
10+
#include <util/time.h>
11+
#include <util/translation.h>
12+
13+
#include <memory>
14+
15+
namespace kernel {
16+
17+
bool SanityChecks(const Context&) {
18+
if (!ECC_InitSanityCheck()) {
19+
return InitError(Untranslated("Elliptic curve cryptography sanity check failure. Aborting."));
20+
}
21+
22+
if (!Random_SanityCheck()) {
23+
return InitError(Untranslated("OS cryptographic RNG sanity check failure. Aborting."));
24+
}
25+
26+
if (!ChronoSanityCheck()) {
27+
return InitError(Untranslated("Clock epoch mismatch. Aborting."));
28+
}
29+
30+
return true;
31+
}
32+
33+
}

src/kernel/checks.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright (c) 2022 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#ifndef BITCOIN_KERNEL_CHECKS_H
6+
#define BITCOIN_KERNEL_CHECKS_H
7+
8+
namespace kernel {
9+
10+
struct Context;
11+
12+
/**
13+
* Ensure a usable environment with all necessary library support.
14+
*/
15+
bool SanityChecks(const Context&);
16+
17+
}
18+
19+
#endif // BITCOIN_KERNEL_CHECKS_H

src/node/interfaces.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class NodeImpl : public Node
9494
if (!AppInitParameterInteraction(gArgs, /*use_syscall_sandbox=*/false)) return false;
9595

9696
m_context->kernel = std::make_unique<kernel::Context>();
97-
if (!AppInitSanityChecks()) return false;
97+
if (!AppInitSanityChecks(*m_context->kernel)) return false;
9898

9999
if (!AppInitLockDataDirectory()) return false;
100100
if (!AppInitInterfaces(*m_context)) return false;

0 commit comments

Comments
 (0)