Skip to content

Commit fed085a

Browse files
committed
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()
1 parent 7d03fee commit fed085a

File tree

13 files changed

+69
-35
lines changed

13 files changed

+69
-35
lines changed

src/Makefile.am

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@ libbitcoin_node_a_SOURCES = \
357357
index/txindex.cpp \
358358
init.cpp \
359359
kernel/coinstats.cpp \
360+
kernel/context.cpp \
360361
mapport.cpp \
361362
net.cpp \
362363
netgroup.cpp \
@@ -865,8 +866,8 @@ libbitcoinkernel_la_SOURCES = \
865866
flatfile.cpp \
866867
fs.cpp \
867868
hash.cpp \
868-
init/common.cpp \
869869
kernel/coinstats.cpp \
870+
kernel/context.cpp \
870871
key.cpp \
871872
logging.cpp \
872873
node/blockstorage.cpp \

src/bitcoin-chainstate.cpp

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

14+
#include <kernel/context.h>
15+
1416
#include <chainparams.h>
1517
#include <consensus/validation.h>
1618
#include <core_io.h>
@@ -49,7 +51,7 @@ int main(int argc, char* argv[])
4951
SelectParams(CBaseChainParams::MAIN);
5052
const CChainParams& chainparams = Params();
5153

52-
init::SetGlobals(); // ECC_Start, etc.
54+
kernel::Context kernel_context{};
5355

5456
// Necessary for CheckInputScripts (eventually called by ProcessNewBlock),
5557
// which will try the script cache first and fall back to actually
@@ -254,6 +256,4 @@ int main(int argc, char* argv[])
254256
}
255257
}
256258
GetMainSignals().UnregisterBackgroundSignalScheduler();
257-
258-
init::UnsetGlobals();
259259
}

src/bitcoind.cpp

Lines changed: 3 additions & 0 deletions
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+
192+
node.kernel = std::make_unique<kernel::Context>();
191193
if (!AppInitSanityChecks())
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: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ void Shutdown(NodeContext& node)
304304
node.chain_clients.clear();
305305
UnregisterAllValidationInterfaces();
306306
GetMainSignals().UnregisterBackgroundSignalScheduler();
307-
init::UnsetGlobals();
307+
node.kernel.reset();
308308
node.mempool.reset();
309309
node.fee_estimator.reset();
310310
node.chainman.reset();
@@ -1092,9 +1092,6 @@ static bool LockDataDirectory(bool probeOnly)
10921092
bool AppInitSanityChecks()
10931093
{
10941094
// ********************************************************* Step 4: sanity checks
1095-
1096-
init::SetGlobals();
1097-
10981095
if (!init::SanityChecks()) {
10991096
return InitError(strprintf(_("Initialization sanity check failed. %s is shutting down."), PACKAGE_NAME));
11001097
}

src/init.h

Lines changed: 3 additions & 0 deletions
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

src/init/common.cpp

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,41 +7,21 @@
77
#endif
88

99
#include <clientversion.h>
10-
#include <crypto/sha256.h>
1110
#include <fs.h>
1211
#include <key.h>
1312
#include <logging.h>
1413
#include <node/ui_interface.h>
15-
#include <pubkey.h>
1614
#include <random.h>
1715
#include <tinyformat.h>
1816
#include <util/system.h>
1917
#include <util/time.h>
2018
#include <util/translation.h>
2119

2220
#include <algorithm>
23-
#include <memory>
2421
#include <string>
2522
#include <vector>
2623

27-
static std::unique_ptr<ECCVerifyHandle> globalVerifyHandle;
28-
2924
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-
4525
bool SanityChecks()
4626
{
4727
if (!ECC_InitSanityCheck()) {

src/init/common.h

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

1313
namespace init {
14-
void SetGlobals();
15-
void UnsetGlobals();
1614
/**
1715
* Ensure a usable environment with all
1816
* necessary library support.

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

src/kernel/context.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
#ifndef BITCOIN_KERNEL_CONTEXT_H
66
#define BITCOIN_KERNEL_CONTEXT_H
77

8+
#include <memory>
9+
10+
class ECCVerifyHandle;
11+
812
namespace kernel {
913
//! Context struct holding the kernel library's logically global state, and
1014
//! passed to external libbitcoin_kernel functions which need access to this
@@ -14,6 +18,13 @@ namespace kernel {
1418
//! State stored directly in this struct should be simple. More complex state
1519
//! should be stored to std::unique_ptr members pointing to opaque types.
1620
struct Context {
21+
std::unique_ptr<ECCVerifyHandle> ecc_verify_handle;
22+
23+
//! Declare default constructor and destructor that are not inline, so code
24+
//! instantiating the kernel::Context struct doesn't need to #include class
25+
//! definitions for all the unique_ptr members.
26+
Context();
27+
~Context();
1728
};
1829
} // namespace kernel
1930

src/node/context.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <addrman.h>
88
#include <banman.h>
99
#include <interfaces/chain.h>
10+
#include <kernel/context.h>
1011
#include <net.h>
1112
#include <net_processing.h>
1213
#include <netgroup.h>

0 commit comments

Comments
 (0)