Skip to content

Commit aac9c25

Browse files
committed
Merge bitcoin/bitcoin#25065: [kernel 2c/n] Introduce kernel::Context, encapsulate global init/teardown
d87784a kernel: SanityChecks: Return an error struct (Carl Dong) 265d639 Move init::SanityCheck to kernel::SanityCheck (Carl Dong) fed085a init: Initialize globals with kernel::Context's life (Carl Dong) 7d03fee kernel: Introduce empty and unused kernel::Context (Carl Dong) eeb4fc2 test: Use Set/UnsetGlobals in BasicTestingSetup (Carl Dong) Pull request description: The full `init/common.cpp` is dependent on things like ArgsManager (which we wish to remove from libbitcoinkernel in the future) and sanity checks. These aren't necessary for libbitcoinkernel so we only extract the portion that is necessary (namely `init::{Set,Unset}Globals()`. ACKs for top commit: theuni: ACK d87784a vasild: ACK d87784a Tree-SHA512: cd6b4923ea1865001b5f0caed9a4ff99c198d22bf74154d935dc09a47fda22ebe585ec912398cea69f722454ed1dbb4898faab5a2d02fb4c5e719c5c8d71a3f9
2 parents 2cf8c2c + d87784a commit aac9c25

16 files changed

+180
-65
lines changed

src/Makefile.am

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,9 @@ BITCOIN_CORE_H = \
171171
interfaces/node.h \
172172
interfaces/wallet.h \
173173
kernel/chainstatemanager_opts.h \
174+
kernel/checks.h \
174175
kernel/coinstats.h \
176+
kernel/context.h \
175177
key.h \
176178
key_io.h \
177179
logging.h \
@@ -356,7 +358,9 @@ libbitcoin_node_a_SOURCES = \
356358
index/coinstatsindex.cpp \
357359
index/txindex.cpp \
358360
init.cpp \
361+
kernel/checks.cpp \
359362
kernel/coinstats.cpp \
363+
kernel/context.cpp \
360364
mapport.cpp \
361365
net.cpp \
362366
netgroup.cpp \
@@ -866,8 +870,9 @@ libbitcoinkernel_la_SOURCES = \
866870
flatfile.cpp \
867871
fs.cpp \
868872
hash.cpp \
869-
init/common.cpp \
873+
kernel/checks.cpp \
870874
kernel/coinstats.cpp \
875+
kernel/context.cpp \
871876
key.cpp \
872877
logging.cpp \
873878
node/blockstorage.cpp \

src/bitcoin-chainstate.cpp

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

14+
#include <kernel/checks.h>
15+
#include <kernel/context.h>
16+
1417
#include <chainparams.h>
1518
#include <consensus/validation.h>
1619
#include <core_io.h>
17-
#include <init/common.h>
1820
#include <node/blockstorage.h>
1921
#include <node/chainstate.h>
2022
#include <scheduler.h>
@@ -24,6 +26,7 @@
2426
#include <validation.h>
2527
#include <validationinterface.h>
2628

29+
#include <cassert>
2730
#include <filesystem>
2831
#include <functional>
2932
#include <iosfwd>
@@ -49,7 +52,11 @@ int main(int argc, char* argv[])
4952
SelectParams(CBaseChainParams::MAIN);
5053
const CChainParams& chainparams = Params();
5154

52-
init::SetGlobals(); // ECC_Start, etc.
55+
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).has_value());
5360

5461
// Necessary for CheckInputScripts (eventually called by ProcessNewBlock),
5562
// which will try the script cache first and fall back to actually
@@ -254,6 +261,4 @@ int main(int argc, char* argv[])
254261
}
255262
}
256263
GetMainSignals().UnregisterBackgroundSignalScheduler();
257-
258-
init::UnsetGlobals();
259264
}

src/bitcoind.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,11 +188,14 @@ static bool AppInit(NodeContext& node, int argc, char* argv[])
188188
// InitError will have been called with detailed error, which ends up on console
189189
return false;
190190
}
191-
if (!AppInitSanityChecks())
191+
192+
node.kernel = std::make_unique<kernel::Context>();
193+
if (!AppInitSanityChecks(*node.kernel))
192194
{
193195
// InitError will have been called with detailed error, which ends up on console
194196
return false;
195197
}
198+
196199
if (args.GetBoolArg("-daemon", DEFAULT_DAEMON) || args.GetBoolArg("-daemonwait", DEFAULT_DAEMONWAIT)) {
197200
#if HAVE_DECL_FORK
198201
tfm::format(std::cout, PACKAGE_NAME " starting\n");

src/init.cpp

Lines changed: 18 additions & 5 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>
@@ -304,7 +306,7 @@ void Shutdown(NodeContext& node)
304306
node.chain_clients.clear();
305307
UnregisterAllValidationInterfaces();
306308
GetMainSignals().UnregisterBackgroundSignalScheduler();
307-
init::UnsetGlobals();
309+
node.kernel.reset();
308310
node.mempool.reset();
309311
node.fee_estimator.reset();
310312
node.chainman.reset();
@@ -1089,13 +1091,24 @@ 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
1097+
auto maybe_error = kernel::SanityChecks(kernel);
1098+
1099+
if (maybe_error.has_value()) {
1100+
switch (maybe_error.value()) {
1101+
case kernel::SanityCheckError::ERROR_ECC:
1102+
InitError(Untranslated("Elliptic curve cryptography sanity check failure. Aborting."));
1103+
break;
1104+
case kernel::SanityCheckError::ERROR_RANDOM:
1105+
InitError(Untranslated("OS cryptographic RNG sanity check failure. Aborting."));
1106+
break;
1107+
case kernel::SanityCheckError::ERROR_CHRONO:
1108+
InitError(Untranslated("Clock epoch mismatch. Aborting."));
1109+
break;
1110+
} // no default case, so the compiler can warn about missing cases
10951111

1096-
init::SetGlobals();
1097-
1098-
if (!init::SanityChecks()) {
10991112
return InitError(strprintf(_("Initialization sanity check failed. %s is shutting down."), PACKAGE_NAME));
11001113
}
11011114

src/init.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ class ArgsManager;
1919
namespace interfaces {
2020
struct BlockAndHeaderTipInfo;
2121
}
22+
namespace kernel {
23+
struct Context;
24+
}
2225
namespace node {
2326
struct NodeContext;
2427
} // namespace node
@@ -47,7 +50,7 @@ bool AppInitParameterInteraction(const ArgsManager& args, bool use_syscall_sandb
4750
* @note This can be done before daemonization. Do not call Shutdown() if this function fails.
4851
* @pre Parameters should be parsed and config file should be read, AppInitParameterInteraction should have been called.
4952
*/
50-
bool AppInitSanityChecks();
53+
bool AppInitSanityChecks(const kernel::Context& kernel);
5154
/**
5255
* Lock bitcoin core data directory.
5356
* @note This should only be done after daemonization. Do not call Shutdown() if this function fails.

src/init/common.cpp

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,58 +7,19 @@
77
#endif
88

99
#include <clientversion.h>
10-
#include <crypto/sha256.h>
1110
#include <fs.h>
12-
#include <key.h>
1311
#include <logging.h>
1412
#include <node/ui_interface.h>
15-
#include <pubkey.h>
16-
#include <random.h>
1713
#include <tinyformat.h>
1814
#include <util/system.h>
1915
#include <util/time.h>
2016
#include <util/translation.h>
2117

2218
#include <algorithm>
23-
#include <memory>
2419
#include <string>
2520
#include <vector>
2621

27-
static std::unique_ptr<ECCVerifyHandle> globalVerifyHandle;
28-
2922
namespace init {
30-
void SetGlobals()
31-
{
32-
std::string sha256_algo = SHA256AutoDetect();
33-
LogPrintf("Using the '%s' SHA256 implementation\n", sha256_algo);
34-
RandomInit();
35-
ECC_Start();
36-
globalVerifyHandle.reset(new ECCVerifyHandle());
37-
}
38-
39-
void UnsetGlobals()
40-
{
41-
globalVerifyHandle.reset();
42-
ECC_Stop();
43-
}
44-
45-
bool SanityChecks()
46-
{
47-
if (!ECC_InitSanityCheck()) {
48-
return InitError(Untranslated("Elliptic curve cryptography sanity check failure. Aborting."));
49-
}
50-
51-
if (!Random_SanityCheck()) {
52-
return InitError(Untranslated("OS cryptographic RNG sanity check failure. Aborting."));
53-
}
54-
55-
if (!ChronoSanityCheck()) {
56-
return InitError(Untranslated("Clock epoch mismatch. Aborting."));
57-
}
58-
59-
return true;
60-
}
61-
6223
void AddLoggingArgs(ArgsManager& argsman)
6324
{
6425
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 & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,6 @@
1111
class ArgsManager;
1212

1313
namespace init {
14-
void SetGlobals();
15-
void UnsetGlobals();
16-
/**
17-
* Ensure a usable environment with all
18-
* necessary library support.
19-
*/
20-
bool SanityChecks();
2114
void AddLoggingArgs(ArgsManager& args);
2215
void SetLoggingOptions(const ArgsManager& args);
2316
void SetLoggingCategories(const ArgsManager& args);

src/kernel/checks.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 <random.h>
9+
#include <util/time.h>
10+
11+
namespace kernel {
12+
13+
std::optional<SanityCheckError> SanityChecks(const Context&)
14+
{
15+
if (!ECC_InitSanityCheck()) {
16+
return SanityCheckError::ERROR_ECC;
17+
}
18+
19+
if (!Random_SanityCheck()) {
20+
return SanityCheckError::ERROR_RANDOM;
21+
}
22+
23+
if (!ChronoSanityCheck()) {
24+
return SanityCheckError::ERROR_CHRONO;
25+
}
26+
27+
return std::nullopt;
28+
}
29+
30+
}

src/kernel/checks.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
#include <optional>
9+
10+
namespace kernel {
11+
12+
struct Context;
13+
14+
enum class SanityCheckError {
15+
ERROR_ECC,
16+
ERROR_RANDOM,
17+
ERROR_CHRONO,
18+
};
19+
20+
/**
21+
* Ensure a usable environment with all necessary library support.
22+
*/
23+
std::optional<SanityCheckError> SanityChecks(const Context&);
24+
25+
}
26+
27+
#endif // BITCOIN_KERNEL_CHECKS_H

src/kernel/context.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/context.h>
6+
7+
#include <crypto/sha256.h>
8+
#include <key.h>
9+
#include <logging.h>
10+
#include <pubkey.h>
11+
#include <random.h>
12+
13+
#include <string>
14+
15+
16+
namespace kernel {
17+
18+
Context::Context()
19+
{
20+
std::string sha256_algo = SHA256AutoDetect();
21+
LogPrintf("Using the '%s' SHA256 implementation\n", sha256_algo);
22+
RandomInit();
23+
ECC_Start();
24+
ecc_verify_handle.reset(new ECCVerifyHandle());
25+
}
26+
27+
Context::~Context()
28+
{
29+
ecc_verify_handle.reset();
30+
ECC_Stop();
31+
}
32+
33+
} // namespace kernel

0 commit comments

Comments
 (0)