Skip to content

Commit 471e5f8

Browse files
committed
Merge bitcoin#16839: Replace Connman and BanMan globals with NodeContext local
362ded4 Avoid using g_rpc_node global in wallet code (Russell Yanofsky) 8922d7f scripted-diff: Remove g_connman, g_banman globals (Russell Yanofsky) e6f4f89 Pass NodeContext, ConnMan, BanMan references more places (Russell Yanofsky) 4d5448c MOVEONLY: Move NodeContext struct to node/context.h (Russell Yanofsky) 301bd41 scripted-diff: Rename InitInterfaces to NodeContext (Russell Yanofsky) Pull request description: This change is mainly a naming / organization change intended to simplify bitcoin#10102. It: - Renames struct InitInterfaces to struct NodeContext and moves it from src/init.h to src/node/context.h. This is a cosmetic change intended to make the point of the struct more obvious. - Gets rid of BanMan and ConnMan globals making them NodeContext members instead. Getting rid of these globals has been talked about in past as a way to implement testing and simulations. Making them NodeContext members is a way of keeping them accessible without the globals. - Splits g_rpc_interfaces global into g_rpc_node and g_rpc_chain globals. This better separates node and wallet rpc methods. Node RPC methods should have access NodeContext, while wallet RPC methods should only have indirect access to node functionality via interfaces::Chain. - Adds NodeContext& references to interfaces::Chain class and the interfaces::MakeChain() function. This is needed to access ConnMan and BanMan instances without the globals. - Gets rid of redundant Node and Chain instances in Qt tests. This is needed due to the previous MakeChain change, and also makes test setup a little more straightforward. More cleanup could be done in the future, but it will require deduplication of bitcoind, bitcoin-qt, and TestingSetup init code. ACKs for top commit: laanwj: ACK 362ded4 Tree-SHA512: 9ae6ff1e33423291d1e52056bac95e0874538390892a6e83c4c115b3c73155a8827c0191b46eb3d14e3b3f6c23ccb08095490880fbc3188026319c71739f7db2
2 parents f129170 + 362ded4 commit 471e5f8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+336
-211
lines changed

src/Makefile.am

+2
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ BITCOIN_CORE_H = \
157157
netmessagemaker.h \
158158
node/coin.h \
159159
node/coinstats.h \
160+
node/context.h \
160161
node/psbt.h \
161162
node/transaction.h \
162163
noui.h \
@@ -285,6 +286,7 @@ libbitcoin_server_a_SOURCES = \
285286
net_processing.cpp \
286287
node/coin.cpp \
287288
node/coinstats.cpp \
289+
node/context.cpp \
288290
node/psbt.cpp \
289291
node/transaction.cpp \
290292
noui.cpp \

src/banman.h

-1
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,4 @@ class BanMan
6666
const int64_t m_default_ban_time;
6767
};
6868

69-
extern std::unique_ptr<BanMan> g_banman;
7069
#endif

src/bench/coin_selection.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <bench/bench.h>
66
#include <interfaces/chain.h>
7+
#include <node/context.h>
78
#include <wallet/coinselection.h>
89
#include <wallet/wallet.h>
910

@@ -28,7 +29,8 @@ static void addCoin(const CAmount& nValue, const CWallet& wallet, std::vector<st
2829
// (https://github.com/bitcoin/bitcoin/issues/7883#issuecomment-224807484)
2930
static void CoinSelection(benchmark::State& state)
3031
{
31-
auto chain = interfaces::MakeChain();
32+
NodeContext node;
33+
auto chain = interfaces::MakeChain(node);
3234
const CWallet wallet(chain.get(), WalletLocation(), WalletDatabase::CreateDummy());
3335
std::vector<std::unique_ptr<CWalletTx>> wtxs;
3436
LOCK(wallet.cs_wallet);
@@ -60,7 +62,8 @@ static void CoinSelection(benchmark::State& state)
6062
}
6163

6264
typedef std::set<CInputCoin> CoinSet;
63-
static auto testChain = interfaces::MakeChain();
65+
static NodeContext testNode;
66+
static auto testChain = interfaces::MakeChain(testNode);
6467
static const CWallet testWallet(testChain.get(), WalletLocation(), WalletDatabase::CreateDummy());
6568
std::vector<std::unique_ptr<CWalletTx>> wtxn;
6669

src/bench/wallet_balance.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <bench/bench.h>
66
#include <interfaces/chain.h>
7+
#include <node/context.h>
78
#include <optional.h>
89
#include <test/util.h>
910
#include <validationinterface.h>
@@ -13,7 +14,8 @@ static void WalletBalance(benchmark::State& state, const bool set_dirty, const b
1314
{
1415
const auto& ADDRESS_WATCHONLY = ADDRESS_BCRT1_UNSPENDABLE;
1516

16-
std::unique_ptr<interfaces::Chain> chain = interfaces::MakeChain();
17+
NodeContext node;
18+
std::unique_ptr<interfaces::Chain> chain = interfaces::MakeChain(node);
1719
CWallet wallet{chain.get(), WalletLocation(), WalletDatabase::CreateMock()};
1820
{
1921
bool first_run;

src/bitcoind.cpp

+9-8
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <compat.h>
1313
#include <init.h>
1414
#include <interfaces/chain.h>
15+
#include <node/context.h>
1516
#include <noui.h>
1617
#include <shutdown.h>
1718
#include <ui_interface.h>
@@ -24,13 +25,13 @@
2425

2526
const std::function<std::string(const char*)> G_TRANSLATION_FUN = nullptr;
2627

27-
static void WaitForShutdown()
28+
static void WaitForShutdown(NodeContext& node)
2829
{
2930
while (!ShutdownRequested())
3031
{
3132
MilliSleep(200);
3233
}
33-
Interrupt();
34+
Interrupt(node);
3435
}
3536

3637
//////////////////////////////////////////////////////////////////////////////
@@ -39,8 +40,8 @@ static void WaitForShutdown()
3940
//
4041
static bool AppInit(int argc, char* argv[])
4142
{
42-
InitInterfaces interfaces;
43-
interfaces.chain = interfaces::MakeChain();
43+
NodeContext node;
44+
node.chain = interfaces::MakeChain(node);
4445

4546
bool fRet = false;
4647

@@ -142,7 +143,7 @@ static bool AppInit(int argc, char* argv[])
142143
// If locking the data directory failed, exit immediately
143144
return false;
144145
}
145-
fRet = AppInitMain(interfaces);
146+
fRet = AppInitMain(node);
146147
}
147148
catch (const std::exception& e) {
148149
PrintExceptionContinue(&e, "AppInit()");
@@ -152,11 +153,11 @@ static bool AppInit(int argc, char* argv[])
152153

153154
if (!fRet)
154155
{
155-
Interrupt();
156+
Interrupt(node);
156157
} else {
157-
WaitForShutdown();
158+
WaitForShutdown(node);
158159
}
159-
Shutdown(interfaces);
160+
Shutdown(node);
160161

161162
return fRet;
162163
}

src/dummywallet.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class DummyWalletInit : public WalletInitInterface {
1919
bool HasWalletSupport() const override {return false;}
2020
void AddWalletOptions() const override;
2121
bool ParameterInteraction() const override {return true;}
22-
void Construct(InitInterfaces& interfaces) const override {LogPrintf("No wallet support compiled in!\n");}
22+
void Construct(NodeContext& node) const override {LogPrintf("No wallet support compiled in!\n");}
2323
};
2424

2525
void DummyWalletInit::AddWalletOptions() const

src/init.cpp

+32-33
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <net_permissions.h>
3030
#include <net_processing.h>
3131
#include <netbase.h>
32+
#include <node/context.h>
3233
#include <policy/feerate.h>
3334
#include <policy/fees.h>
3435
#include <policy/policy.h>
@@ -84,9 +85,6 @@ static const bool DEFAULT_STOPAFTERBLOCKIMPORT = false;
8485
// Dump addresses to banlist.dat every 15 minutes (900s)
8586
static constexpr int DUMP_BANS_INTERVAL = 60 * 15;
8687

87-
std::unique_ptr<CConnman> g_connman;
88-
std::unique_ptr<PeerLogicValidation> peerLogic;
89-
std::unique_ptr<BanMan> g_banman;
9088

9189
#ifdef WIN32
9290
// Win32 LevelDB doesn't use filedescriptors, and the ones used for
@@ -154,23 +152,23 @@ static std::unique_ptr<ECCVerifyHandle> globalVerifyHandle;
154152
static boost::thread_group threadGroup;
155153
static CScheduler scheduler;
156154

157-
void Interrupt()
155+
void Interrupt(NodeContext& node)
158156
{
159157
InterruptHTTPServer();
160158
InterruptHTTPRPC();
161159
InterruptRPC();
162160
InterruptREST();
163161
InterruptTorControl();
164162
InterruptMapPort();
165-
if (g_connman)
166-
g_connman->Interrupt();
163+
if (node.connman)
164+
node.connman->Interrupt();
167165
if (g_txindex) {
168166
g_txindex->Interrupt();
169167
}
170168
ForEachBlockFilterIndex([](BlockFilterIndex& index) { index.Interrupt(); });
171169
}
172170

173-
void Shutdown(InitInterfaces& interfaces)
171+
void Shutdown(NodeContext& node)
174172
{
175173
LogPrintf("%s: In progress...\n", __func__);
176174
static CCriticalSection cs_Shutdown;
@@ -189,15 +187,15 @@ void Shutdown(InitInterfaces& interfaces)
189187
StopREST();
190188
StopRPC();
191189
StopHTTPServer();
192-
for (const auto& client : interfaces.chain_clients) {
190+
for (const auto& client : node.chain_clients) {
193191
client->flush();
194192
}
195193
StopMapPort();
196194

197195
// Because these depend on each-other, we make sure that neither can be
198196
// using the other before destroying them.
199-
if (peerLogic) UnregisterValidationInterface(peerLogic.get());
200-
if (g_connman) g_connman->Stop();
197+
if (node.peer_logic) UnregisterValidationInterface(node.peer_logic.get());
198+
if (node.connman) node.connman->Stop();
201199
if (g_txindex) g_txindex->Stop();
202200
ForEachBlockFilterIndex([](BlockFilterIndex& index) { index.Stop(); });
203201

@@ -210,9 +208,9 @@ void Shutdown(InitInterfaces& interfaces)
210208

211209
// After the threads that potentially access these pointers have been stopped,
212210
// destruct and reset all to nullptr.
213-
peerLogic.reset();
214-
g_connman.reset();
215-
g_banman.reset();
211+
node.peer_logic.reset();
212+
node.connman.reset();
213+
node.banman.reset();
216214
g_txindex.reset();
217215
DestroyAllBlockFilterIndexes();
218216

@@ -261,7 +259,7 @@ void Shutdown(InitInterfaces& interfaces)
261259
}
262260
pblocktree.reset();
263261
}
264-
for (const auto& client : interfaces.chain_clients) {
262+
for (const auto& client : node.chain_clients) {
265263
client->stop();
266264
}
267265

@@ -280,7 +278,7 @@ void Shutdown(InitInterfaces& interfaces)
280278
} catch (const fs::filesystem_error& e) {
281279
LogPrintf("%s: Unable to remove PID file: %s\n", __func__, fsbridge::get_filesystem_error_message(e));
282280
}
283-
interfaces.chain_clients.clear();
281+
node.chain_clients.clear();
284282
UnregisterAllValidationInterfaces();
285283
GetMainSignals().UnregisterBackgroundSignalScheduler();
286284
GetMainSignals().UnregisterWithMempoolSignals(mempool);
@@ -1207,7 +1205,7 @@ bool AppInitLockDataDirectory()
12071205
return true;
12081206
}
12091207

1210-
bool AppInitMain(InitInterfaces& interfaces)
1208+
bool AppInitMain(NodeContext& node)
12111209
{
12121210
const CChainParams& chainparams = Params();
12131211
// ********************************************************* Step 4a: application initialization
@@ -1275,16 +1273,16 @@ bool AppInitMain(InitInterfaces& interfaces)
12751273
// according to -wallet and -disablewallet options. This only constructs
12761274
// the interfaces, it doesn't load wallet data. Wallets actually get loaded
12771275
// when load() and start() interface methods are called below.
1278-
g_wallet_init_interface.Construct(interfaces);
1276+
g_wallet_init_interface.Construct(node);
12791277

12801278
/* Register RPC commands regardless of -server setting so they will be
12811279
* available in the GUI RPC console even if external calls are disabled.
12821280
*/
12831281
RegisterAllCoreRPCCommands(tableRPC);
1284-
for (const auto& client : interfaces.chain_clients) {
1282+
for (const auto& client : node.chain_clients) {
12851283
client->registerRpcs();
12861284
}
1287-
g_rpc_interfaces = &interfaces;
1285+
g_rpc_node = &node;
12881286
#if ENABLE_ZMQ
12891287
RegisterZMQRPCCommands(tableRPC);
12901288
#endif
@@ -1302,7 +1300,7 @@ bool AppInitMain(InitInterfaces& interfaces)
13021300
}
13031301

13041302
// ********************************************************* Step 5: verify wallet database integrity
1305-
for (const auto& client : interfaces.chain_clients) {
1303+
for (const auto& client : node.chain_clients) {
13061304
if (!client->verify()) {
13071305
return false;
13081306
}
@@ -1314,13 +1312,13 @@ bool AppInitMain(InitInterfaces& interfaces)
13141312
// is not yet setup and may end up being set up twice if we
13151313
// need to reindex later.
13161314

1317-
assert(!g_banman);
1318-
g_banman = MakeUnique<BanMan>(GetDataDir() / "banlist.dat", &uiInterface, gArgs.GetArg("-bantime", DEFAULT_MISBEHAVING_BANTIME));
1319-
assert(!g_connman);
1320-
g_connman = std::unique_ptr<CConnman>(new CConnman(GetRand(std::numeric_limits<uint64_t>::max()), GetRand(std::numeric_limits<uint64_t>::max())));
1315+
assert(!node.banman);
1316+
node.banman = MakeUnique<BanMan>(GetDataDir() / "banlist.dat", &uiInterface, gArgs.GetArg("-bantime", DEFAULT_MISBEHAVING_BANTIME));
1317+
assert(!node.connman);
1318+
node.connman = std::unique_ptr<CConnman>(new CConnman(GetRand(std::numeric_limits<uint64_t>::max()), GetRand(std::numeric_limits<uint64_t>::max())));
13211319

1322-
peerLogic.reset(new PeerLogicValidation(g_connman.get(), g_banman.get(), scheduler));
1323-
RegisterValidationInterface(peerLogic.get());
1320+
node.peer_logic.reset(new PeerLogicValidation(node.connman.get(), node.banman.get(), scheduler));
1321+
RegisterValidationInterface(node.peer_logic.get());
13241322

13251323
// sanitize comments per BIP-0014, format user agent and check total size
13261324
std::vector<std::string> uacomments;
@@ -1661,7 +1659,7 @@ bool AppInitMain(InitInterfaces& interfaces)
16611659
}
16621660

16631661
// ********************************************************* Step 9: load wallet
1664-
for (const auto& client : interfaces.chain_clients) {
1662+
for (const auto& client : node.chain_clients) {
16651663
if (!client->load()) {
16661664
return false;
16671665
}
@@ -1765,8 +1763,8 @@ bool AppInitMain(InitInterfaces& interfaces)
17651763
connOptions.nMaxFeeler = 1;
17661764
connOptions.nBestHeight = chain_active_height;
17671765
connOptions.uiInterface = &uiInterface;
1768-
connOptions.m_banman = g_banman.get();
1769-
connOptions.m_msgproc = peerLogic.get();
1766+
connOptions.m_banman = node.banman.get();
1767+
connOptions.m_msgproc = node.peer_logic.get();
17701768
connOptions.nSendBufferMaxSize = 1000*gArgs.GetArg("-maxsendbuffer", DEFAULT_MAXSENDBUFFER);
17711769
connOptions.nReceiveFloodSize = 1000*gArgs.GetArg("-maxreceivebuffer", DEFAULT_MAXRECEIVEBUFFER);
17721770
connOptions.m_added_nodes = gArgs.GetArgs("-addnode");
@@ -1806,7 +1804,7 @@ bool AppInitMain(InitInterfaces& interfaces)
18061804
connOptions.m_specified_outgoing = connect;
18071805
}
18081806
}
1809-
if (!g_connman->Start(scheduler, connOptions)) {
1807+
if (!node.connman->Start(scheduler, connOptions)) {
18101808
return false;
18111809
}
18121810

@@ -1815,12 +1813,13 @@ bool AppInitMain(InitInterfaces& interfaces)
18151813
SetRPCWarmupFinished();
18161814
uiInterface.InitMessage(_("Done loading").translated);
18171815

1818-
for (const auto& client : interfaces.chain_clients) {
1816+
for (const auto& client : node.chain_clients) {
18191817
client->start(scheduler);
18201818
}
18211819

1822-
scheduler.scheduleEvery([]{
1823-
g_banman->DumpBanlist();
1820+
BanMan* banman = node.banman.get();
1821+
scheduler.scheduleEvery([banman]{
1822+
banman->DumpBanlist();
18241823
}, DUMP_BANS_INTERVAL * 1000);
18251824

18261825
return true;

src/init.h

+5-17
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,14 @@
1010
#include <string>
1111
#include <util/system.h>
1212

13-
namespace interfaces {
14-
class Chain;
15-
class ChainClient;
16-
} // namespace interfaces
17-
18-
//! Pointers to interfaces used during init and destroyed on shutdown.
19-
struct InitInterfaces
20-
{
21-
std::unique_ptr<interfaces::Chain> chain;
22-
std::vector<std::unique_ptr<interfaces::ChainClient>> chain_clients;
23-
};
24-
25-
namespace boost
26-
{
13+
struct NodeContext;
14+
namespace boost {
2715
class thread_group;
2816
} // namespace boost
2917

3018
/** Interrupt threads */
31-
void Interrupt();
32-
void Shutdown(InitInterfaces& interfaces);
19+
void Interrupt(NodeContext& node);
20+
void Shutdown(NodeContext& node);
3321
//!Initialize the logging infrastructure
3422
void InitLogging();
3523
//!Parameter interaction: change current parameters depending on various rules
@@ -63,7 +51,7 @@ bool AppInitLockDataDirectory();
6351
* @note This should only be done after daemonization. Call Shutdown() if this function fails.
6452
* @pre Parameters should be parsed and config file should be read, AppInitLockDataDirectory should have been called.
6553
*/
66-
bool AppInitMain(InitInterfaces& interfaces);
54+
bool AppInitMain(NodeContext& node);
6755

6856
/**
6957
* Setup the arguments for gArgs

0 commit comments

Comments
 (0)