Skip to content

Commit e6f4f89

Browse files
committed
Pass NodeContext, ConnMan, BanMan references more places
So g_connman and g_banman globals can be removed next commit.
1 parent 4d5448c commit e6f4f89

34 files changed

+154
-62
lines changed

src/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ libbitcoin_server_a_SOURCES = \
285285
net_processing.cpp \
286286
node/coin.cpp \
287287
node/coinstats.cpp \
288+
node/context.cpp \
288289
node/psbt.cpp \
289290
node/transaction.cpp \
290291
noui.cpp \

src/bench/coin_selection.cpp

Lines changed: 5 additions & 2 deletions
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

Lines changed: 3 additions & 1 deletion
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

Lines changed: 6 additions & 5 deletions
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
//////////////////////////////////////////////////////////////////////////////
@@ -40,7 +41,7 @@ static void WaitForShutdown()
4041
static bool AppInit(int argc, char* argv[])
4142
{
4243
NodeContext node;
43-
node.chain = interfaces::MakeChain();
44+
node.chain = interfaces::MakeChain(node);
4445

4546
bool fRet = false;
4647

@@ -152,9 +153,9 @@ 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
}
159160
Shutdown(node);
160161

src/init.cpp

Lines changed: 5 additions & 3 deletions
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>
@@ -154,7 +155,7 @@ static std::unique_ptr<ECCVerifyHandle> globalVerifyHandle;
154155
static boost::thread_group threadGroup;
155156
static CScheduler scheduler;
156157

157-
void Interrupt()
158+
void Interrupt(NodeContext& node)
158159
{
159160
InterruptHTTPServer();
160161
InterruptHTTPRPC();
@@ -1819,8 +1820,9 @@ bool AppInitMain(NodeContext& node)
18191820
client->start(scheduler);
18201821
}
18211822

1822-
scheduler.scheduleEvery([]{
1823-
g_banman->DumpBanlist();
1823+
BanMan* banman = g_banman.get();
1824+
scheduler.scheduleEvery([banman]{
1825+
banman->DumpBanlist();
18241826
}, DUMP_BANS_INTERVAL * 1000);
18251827

18261828
return true;

src/init.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,16 @@
77
#define BITCOIN_INIT_H
88

99
#include <memory>
10-
#include <node/context.h>
1110
#include <string>
1211
#include <util/system.h>
1312

14-
namespace boost
15-
{
13+
struct NodeContext;
14+
namespace boost {
1615
class thread_group;
1716
} // namespace boost
1817

1918
/** Interrupt threads */
20-
void Interrupt();
19+
void Interrupt(NodeContext& node);
2120
void Shutdown(NodeContext& node);
2221
//!Initialize the logging infrastructure
2322
void InitLogging();

src/interfaces/chain.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <net.h>
1212
#include <net_processing.h>
1313
#include <node/coin.h>
14+
#include <node/context.h>
1415
#include <node/transaction.h>
1516
#include <policy/fees.h>
1617
#include <policy/policy.h>
@@ -238,6 +239,7 @@ class RpcHandlerImpl : public Handler
238239
class ChainImpl : public Chain
239240
{
240241
public:
242+
explicit ChainImpl(NodeContext& node) : m_node(node) {}
241243
std::unique_ptr<Chain::Lock> lock(bool try_lock) override
242244
{
243245
auto result = MakeUnique<LockImpl>(::cs_main, "cs_main", __FILE__, __LINE__, try_lock);
@@ -286,7 +288,7 @@ class ChainImpl : public Chain
286288
}
287289
bool broadcastTransaction(const CTransactionRef& tx, std::string& err_string, const CAmount& max_tx_fee, bool relay) override
288290
{
289-
const TransactionError err = BroadcastTransaction(tx, err_string, max_tx_fee, relay, /*wait_callback*/ false);
291+
const TransactionError err = BroadcastTransaction(m_node, tx, err_string, max_tx_fee, relay, /*wait_callback*/ false);
290292
// Chain clients only care about failures to accept the tx to the mempool. Disregard non-mempool related failures.
291293
// Note: this will need to be updated if BroadcastTransactions() is updated to return other non-mempool failures
292294
// that Chain clients do not need to know about.
@@ -378,9 +380,10 @@ class ChainImpl : public Chain
378380
notifications.TransactionAddedToMempool(entry.GetSharedTx());
379381
}
380382
}
383+
NodeContext& m_node;
381384
};
382385
} // namespace
383386

384-
std::unique_ptr<Chain> MakeChain() { return MakeUnique<ChainImpl>(); }
387+
std::unique_ptr<Chain> MakeChain(NodeContext& node) { return MakeUnique<ChainImpl>(node); }
385388

386389
} // namespace interfaces

src/interfaces/chain.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class uint256;
2424
enum class RBFTransactionState;
2525
struct CBlockLocator;
2626
struct FeeCalculation;
27+
struct NodeContext;
2728

2829
namespace interfaces {
2930

@@ -291,7 +292,7 @@ class ChainClient
291292
};
292293

293294
//! Return implementation of Chain interface.
294-
std::unique_ptr<Chain> MakeChain();
295+
std::unique_ptr<Chain> MakeChain(NodeContext& node);
295296

296297
//! Return implementation of ChainClient interface for a wallet client. This
297298
//! function will be undefined in builds where ENABLE_WALLET is false.

src/interfaces/node.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <net_processing.h>
1717
#include <netaddress.h>
1818
#include <netbase.h>
19+
#include <node/context.h>
1920
#include <policy/feerate.h>
2021
#include <policy/fees.h>
2122
#include <policy/settings.h>
@@ -52,7 +53,6 @@ namespace {
5253
class NodeImpl : public Node
5354
{
5455
public:
55-
NodeImpl() { m_context.chain = MakeChain(); }
5656
void initError(const std::string& message) override { InitError(message); }
5757
bool parseParameters(int argc, const char* const argv[], std::string& error) override
5858
{
@@ -75,10 +75,14 @@ class NodeImpl : public Node
7575
return AppInitBasicSetup() && AppInitParameterInteraction() && AppInitSanityChecks() &&
7676
AppInitLockDataDirectory();
7777
}
78-
bool appInitMain() override { return AppInitMain(m_context); }
78+
bool appInitMain() override
79+
{
80+
m_context.chain = MakeChain(m_context);
81+
return AppInitMain(m_context);
82+
}
7983
void appShutdown() override
8084
{
81-
Interrupt();
85+
Interrupt(m_context);
8286
Shutdown(m_context);
8387
}
8488
void startShutdown() override { StartShutdown(); }
@@ -315,6 +319,7 @@ class NodeImpl : public Node
315319
/* verification progress is unused when a header was received */ 0);
316320
}));
317321
}
322+
NodeContext* context() override { return &m_context; }
318323
NodeContext m_context;
319324
};
320325

src/interfaces/node.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class RPCTimerInterface;
2828
class UniValue;
2929
class proxyType;
3030
struct CNodeStateStats;
31+
struct NodeContext;
3132
enum class WalletCreationStatus;
3233

3334
namespace interfaces {
@@ -254,6 +255,9 @@ class Node
254255
using NotifyHeaderTipFn =
255256
std::function<void(bool initial_download, int height, int64_t block_time, double verification_progress)>;
256257
virtual std::unique_ptr<Handler> handleNotifyHeaderTip(NotifyHeaderTipFn fn) = 0;
258+
259+
//! Return pointer to internal chain interface, useful for testing.
260+
virtual NodeContext* context() { return nullptr; }
257261
};
258262

259263
//! Return implementation of Node interface.

0 commit comments

Comments
 (0)