Skip to content

Commit 8922d7f

Browse files
committed
scripted-diff: Remove g_connman, g_banman globals
-BEGIN VERIFY SCRIPT- sed -i 's:#include <interfaces/chain.h>:#include <banman.h>\n#include <interfaces/chain.h>\n#include <net.h>\n#include <net_processing.h>:' src/node/context.cpp sed -i 's/namespace interfaces {/class BanMan;\nclass CConnman;\nclass PeerLogicValidation;\n&/' src/node/context.h sed -i 's/std::unique_ptr<interfaces::Chain> chain/std::unique_ptr<CConnman> connman;\n std::unique_ptr<PeerLogicValidation> peer_logic;\n std::unique_ptr<BanMan> banman;\n &/' src/node/context.h sed -i '/std::unique_ptr<[^>]\+> \(g_connman\|g_banman\|peerLogic\);/d' src/banman.h src/net.h src/init.cpp sed -i 's/g_connman/m_context.connman/g' src/interfaces/node.cpp sed -i 's/g_banman/m_context.banman/g' src/interfaces/node.cpp sed -i 's/g_connman/m_node.connman/g' src/interfaces/chain.cpp src/test/setup_common.cpp sed -i 's/g_banman/m_node.banman/g' src/test/setup_common.cpp sed -i 's/g_connman/node.connman/g' src/init.cpp src/node/transaction.cpp sed -i 's/g_banman/node.banman/g' src/init.cpp sed -i 's/peerLogic/node.peer_logic/g' src/init.cpp sed -i 's/g_connman/g_rpc_node->connman/g' src/rpc/mining.cpp src/rpc/net.cpp src/rpc/rawtransaction.cpp sed -i 's/g_banman/g_rpc_node->banman/g' src/rpc/net.cpp sed -i 's/std::shared_ptr<CWallet> wallet =/node.context()->connman = std::move(test.m_node.connman);\n &/' src/qt/test/wallettests.cpp -END VERIFY SCRIPT-
1 parent e6f4f89 commit 8922d7f

File tree

11 files changed

+102
-98
lines changed

11 files changed

+102
-98
lines changed

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/init.cpp

+17-20
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,6 @@ static const bool DEFAULT_STOPAFTERBLOCKIMPORT = false;
8585
// Dump addresses to banlist.dat every 15 minutes (900s)
8686
static constexpr int DUMP_BANS_INTERVAL = 60 * 15;
8787

88-
std::unique_ptr<CConnman> g_connman;
89-
std::unique_ptr<PeerLogicValidation> peerLogic;
90-
std::unique_ptr<BanMan> g_banman;
9188

9289
#ifdef WIN32
9390
// Win32 LevelDB doesn't use filedescriptors, and the ones used for
@@ -163,8 +160,8 @@ void Interrupt(NodeContext& node)
163160
InterruptREST();
164161
InterruptTorControl();
165162
InterruptMapPort();
166-
if (g_connman)
167-
g_connman->Interrupt();
163+
if (node.connman)
164+
node.connman->Interrupt();
168165
if (g_txindex) {
169166
g_txindex->Interrupt();
170167
}
@@ -197,8 +194,8 @@ void Shutdown(NodeContext& node)
197194

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

@@ -211,9 +208,9 @@ void Shutdown(NodeContext& node)
211208

212209
// After the threads that potentially access these pointers have been stopped,
213210
// destruct and reset all to nullptr.
214-
peerLogic.reset();
215-
g_connman.reset();
216-
g_banman.reset();
211+
node.peer_logic.reset();
212+
node.connman.reset();
213+
node.banman.reset();
217214
g_txindex.reset();
218215
DestroyAllBlockFilterIndexes();
219216

@@ -1315,13 +1312,13 @@ bool AppInitMain(NodeContext& node)
13151312
// is not yet setup and may end up being set up twice if we
13161313
// need to reindex later.
13171314

1318-
assert(!g_banman);
1319-
g_banman = MakeUnique<BanMan>(GetDataDir() / "banlist.dat", &uiInterface, gArgs.GetArg("-bantime", DEFAULT_MISBEHAVING_BANTIME));
1320-
assert(!g_connman);
1321-
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())));
13221319

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

13261323
// sanitize comments per BIP-0014, format user agent and check total size
13271324
std::vector<std::string> uacomments;
@@ -1766,8 +1763,8 @@ bool AppInitMain(NodeContext& node)
17661763
connOptions.nMaxFeeler = 1;
17671764
connOptions.nBestHeight = chain_active_height;
17681765
connOptions.uiInterface = &uiInterface;
1769-
connOptions.m_banman = g_banman.get();
1770-
connOptions.m_msgproc = peerLogic.get();
1766+
connOptions.m_banman = node.banman.get();
1767+
connOptions.m_msgproc = node.peer_logic.get();
17711768
connOptions.nSendBufferMaxSize = 1000*gArgs.GetArg("-maxsendbuffer", DEFAULT_MAXSENDBUFFER);
17721769
connOptions.nReceiveFloodSize = 1000*gArgs.GetArg("-maxreceivebuffer", DEFAULT_MAXRECEIVEBUFFER);
17731770
connOptions.m_added_nodes = gArgs.GetArgs("-addnode");
@@ -1807,7 +1804,7 @@ bool AppInitMain(NodeContext& node)
18071804
connOptions.m_specified_outgoing = connect;
18081805
}
18091806
}
1810-
if (!g_connman->Start(scheduler, connOptions)) {
1807+
if (!node.connman->Start(scheduler, connOptions)) {
18111808
return false;
18121809
}
18131810

@@ -1820,7 +1817,7 @@ bool AppInitMain(NodeContext& node)
18201817
client->start(scheduler);
18211818
}
18221819

1823-
BanMan* banman = g_banman.get();
1820+
BanMan* banman = node.banman.get();
18241821
scheduler.scheduleEvery([banman]{
18251822
banman->DumpBanlist();
18261823
}, DUMP_BANS_INTERVAL * 1000);

src/interfaces/node.cpp

+18-18
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,15 @@ class NodeImpl : public Node
100100
bool getProxy(Network net, proxyType& proxy_info) override { return GetProxy(net, proxy_info); }
101101
size_t getNodeCount(CConnman::NumConnections flags) override
102102
{
103-
return g_connman ? g_connman->GetNodeCount(flags) : 0;
103+
return m_context.connman ? m_context.connman->GetNodeCount(flags) : 0;
104104
}
105105
bool getNodesStats(NodesStats& stats) override
106106
{
107107
stats.clear();
108108

109-
if (g_connman) {
109+
if (m_context.connman) {
110110
std::vector<CNodeStats> stats_temp;
111-
g_connman->GetNodeStats(stats_temp);
111+
m_context.connman->GetNodeStats(stats_temp);
112112

113113
stats.reserve(stats_temp.size());
114114
for (auto& node_stats_temp : stats_temp) {
@@ -129,44 +129,44 @@ class NodeImpl : public Node
129129
}
130130
bool getBanned(banmap_t& banmap) override
131131
{
132-
if (g_banman) {
133-
g_banman->GetBanned(banmap);
132+
if (m_context.banman) {
133+
m_context.banman->GetBanned(banmap);
134134
return true;
135135
}
136136
return false;
137137
}
138138
bool ban(const CNetAddr& net_addr, BanReason reason, int64_t ban_time_offset) override
139139
{
140-
if (g_banman) {
141-
g_banman->Ban(net_addr, reason, ban_time_offset);
140+
if (m_context.banman) {
141+
m_context.banman->Ban(net_addr, reason, ban_time_offset);
142142
return true;
143143
}
144144
return false;
145145
}
146146
bool unban(const CSubNet& ip) override
147147
{
148-
if (g_banman) {
149-
g_banman->Unban(ip);
148+
if (m_context.banman) {
149+
m_context.banman->Unban(ip);
150150
return true;
151151
}
152152
return false;
153153
}
154154
bool disconnect(const CNetAddr& net_addr) override
155155
{
156-
if (g_connman) {
157-
return g_connman->DisconnectNode(net_addr);
156+
if (m_context.connman) {
157+
return m_context.connman->DisconnectNode(net_addr);
158158
}
159159
return false;
160160
}
161161
bool disconnect(NodeId id) override
162162
{
163-
if (g_connman) {
164-
return g_connman->DisconnectNode(id);
163+
if (m_context.connman) {
164+
return m_context.connman->DisconnectNode(id);
165165
}
166166
return false;
167167
}
168-
int64_t getTotalBytesRecv() override { return g_connman ? g_connman->GetTotalBytesRecv() : 0; }
169-
int64_t getTotalBytesSent() override { return g_connman ? g_connman->GetTotalBytesSent() : 0; }
168+
int64_t getTotalBytesRecv() override { return m_context.connman ? m_context.connman->GetTotalBytesRecv() : 0; }
169+
int64_t getTotalBytesSent() override { return m_context.connman ? m_context.connman->GetTotalBytesSent() : 0; }
170170
size_t getMempoolSize() override { return ::mempool.size(); }
171171
size_t getMempoolDynamicUsage() override { return ::mempool.DynamicMemoryUsage(); }
172172
bool getHeaderTip(int& height, int64_t& block_time) override
@@ -206,11 +206,11 @@ class NodeImpl : public Node
206206
bool getImporting() override { return ::fImporting; }
207207
void setNetworkActive(bool active) override
208208
{
209-
if (g_connman) {
210-
g_connman->SetNetworkActive(active);
209+
if (m_context.connman) {
210+
m_context.connman->SetNetworkActive(active);
211211
}
212212
}
213-
bool getNetworkActive() override { return g_connman && g_connman->GetNetworkActive(); }
213+
bool getNetworkActive() override { return m_context.connman && m_context.connman->GetNetworkActive(); }
214214
CFeeRate estimateSmartFee(int num_blocks, bool conservative, int* returned_target = nullptr) override
215215
{
216216
FeeCalculation fee_calc;

src/net.h

-2
Original file line numberDiff line numberDiff line change
@@ -480,8 +480,6 @@ class CConnman
480480

481481
friend struct CConnmanTest;
482482
};
483-
extern std::unique_ptr<CConnman> g_connman;
484-
extern std::unique_ptr<BanMan> g_banman;
485483
void Discover();
486484
void StartMapPort();
487485
void InterruptMapPort();

src/node/context.cpp

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

55
#include <node/context.h>
66

7+
#include <banman.h>
78
#include <interfaces/chain.h>
9+
#include <net.h>
10+
#include <net_processing.h>
811

912
NodeContext::NodeContext() {}
1013
NodeContext::~NodeContext() {}

src/node/context.h

+6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
#include <memory>
99
#include <vector>
1010

11+
class BanMan;
12+
class CConnman;
13+
class PeerLogicValidation;
1114
namespace interfaces {
1215
class Chain;
1316
class ChainClient;
@@ -25,6 +28,9 @@ class ChainClient;
2528
//! be used without pulling in unwanted dependencies or functionality.
2629
struct NodeContext
2730
{
31+
std::unique_ptr<CConnman> connman;
32+
std::unique_ptr<PeerLogicValidation> peer_logic;
33+
std::unique_ptr<BanMan> banman;
2834
std::unique_ptr<interfaces::Chain> chain;
2935
std::vector<std::unique_ptr<interfaces::ChainClient>> chain_clients;
3036

src/node/transaction.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
TransactionError BroadcastTransaction(NodeContext& node, const CTransactionRef tx, std::string& err_string, const CAmount& max_tx_fee, bool relay, bool wait_callback)
1818
{
1919
// BroadcastTransaction can be called by either sendrawtransaction RPC or wallet RPCs.
20-
// g_connman is assigned both before chain clients and before RPC server is accepting calls,
21-
// and reset after chain clients and RPC sever are stopped. g_connman should never be null here.
22-
assert(g_connman);
20+
// node.connman is assigned both before chain clients and before RPC server is accepting calls,
21+
// and reset after chain clients and RPC sever are stopped. node.connman should never be null here.
22+
assert(node.connman);
2323
std::promise<void> promise;
2424
uint256 hashTx = tx->GetHash();
2525
bool callback_set = false;
@@ -80,7 +80,7 @@ TransactionError BroadcastTransaction(NodeContext& node, const CTransactionRef t
8080
}
8181

8282
if (relay) {
83-
RelayTransaction(hashTx, *g_connman);
83+
RelayTransaction(hashTx, *node.connman);
8484
}
8585

8686
return TransactionError::OK;

src/qt/test/wallettests.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ void TestGUI(interfaces::Node& node)
133133
for (int i = 0; i < 5; ++i) {
134134
test.CreateAndProcessBlock({}, GetScriptForRawPubKey(test.coinbaseKey.GetPubKey()));
135135
}
136+
node.context()->connman = std::move(test.m_node.connman);
136137
std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(node.context()->chain.get(), WalletLocation(), WalletDatabase::CreateMock());
137138
bool firstRun;
138139
wallet->LoadWallet(firstRun);

src/rpc/mining.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -425,10 +425,10 @@ static UniValue getblocktemplate(const JSONRPCRequest& request)
425425
if (strMode != "template")
426426
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid mode");
427427

428-
if(!g_connman)
428+
if(!g_rpc_node->connman)
429429
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
430430

431-
if (g_connman->GetNodeCount(CConnman::CONNECTIONS_ALL) == 0)
431+
if (g_rpc_node->connman->GetNodeCount(CConnman::CONNECTIONS_ALL) == 0)
432432
throw JSONRPCError(RPC_CLIENT_NOT_CONNECTED, PACKAGE_NAME " is not connected!");
433433

434434
if (::ChainstateActive().IsInitialBlockDownload())

0 commit comments

Comments
 (0)