mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-03-17 19:10:40 +01:00
Merge #16839: Replace Connman and BanMan globals with NodeContext local
362ded410bAvoid using g_rpc_node global in wallet code (Russell Yanofsky)8922d7f6b7scripted-diff: Remove g_connman, g_banman globals (Russell Yanofsky)e6f4f895d5Pass NodeContext, ConnMan, BanMan references more places (Russell Yanofsky)4d5448c76bMOVEONLY: Move NodeContext struct to node/context.h (Russell Yanofsky)301bd41a2escripted-diff: Rename InitInterfaces to NodeContext (Russell Yanofsky) Pull request description: This change is mainly a naming / organization change intended to simplify #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: ACK362ded410bTree-SHA512: 9ae6ff1e33423291d1e52056bac95e0874538390892a6e83c4c115b3c73155a8827c0191b46eb3d14e3b3f6c23ccb08095490880fbc3188026319c71739f7db2
This commit is contained in:
65
src/init.cpp
65
src/init.cpp
@@ -29,6 +29,7 @@
|
||||
#include <net_permissions.h>
|
||||
#include <net_processing.h>
|
||||
#include <netbase.h>
|
||||
#include <node/context.h>
|
||||
#include <policy/feerate.h>
|
||||
#include <policy/fees.h>
|
||||
#include <policy/policy.h>
|
||||
@@ -84,9 +85,6 @@ static const bool DEFAULT_STOPAFTERBLOCKIMPORT = false;
|
||||
// Dump addresses to banlist.dat every 15 minutes (900s)
|
||||
static constexpr int DUMP_BANS_INTERVAL = 60 * 15;
|
||||
|
||||
std::unique_ptr<CConnman> g_connman;
|
||||
std::unique_ptr<PeerLogicValidation> peerLogic;
|
||||
std::unique_ptr<BanMan> g_banman;
|
||||
|
||||
#ifdef WIN32
|
||||
// Win32 LevelDB doesn't use filedescriptors, and the ones used for
|
||||
@@ -154,7 +152,7 @@ static std::unique_ptr<ECCVerifyHandle> globalVerifyHandle;
|
||||
static boost::thread_group threadGroup;
|
||||
static CScheduler scheduler;
|
||||
|
||||
void Interrupt()
|
||||
void Interrupt(NodeContext& node)
|
||||
{
|
||||
InterruptHTTPServer();
|
||||
InterruptHTTPRPC();
|
||||
@@ -162,15 +160,15 @@ void Interrupt()
|
||||
InterruptREST();
|
||||
InterruptTorControl();
|
||||
InterruptMapPort();
|
||||
if (g_connman)
|
||||
g_connman->Interrupt();
|
||||
if (node.connman)
|
||||
node.connman->Interrupt();
|
||||
if (g_txindex) {
|
||||
g_txindex->Interrupt();
|
||||
}
|
||||
ForEachBlockFilterIndex([](BlockFilterIndex& index) { index.Interrupt(); });
|
||||
}
|
||||
|
||||
void Shutdown(InitInterfaces& interfaces)
|
||||
void Shutdown(NodeContext& node)
|
||||
{
|
||||
LogPrintf("%s: In progress...\n", __func__);
|
||||
static CCriticalSection cs_Shutdown;
|
||||
@@ -189,15 +187,15 @@ void Shutdown(InitInterfaces& interfaces)
|
||||
StopREST();
|
||||
StopRPC();
|
||||
StopHTTPServer();
|
||||
for (const auto& client : interfaces.chain_clients) {
|
||||
for (const auto& client : node.chain_clients) {
|
||||
client->flush();
|
||||
}
|
||||
StopMapPort();
|
||||
|
||||
// Because these depend on each-other, we make sure that neither can be
|
||||
// using the other before destroying them.
|
||||
if (peerLogic) UnregisterValidationInterface(peerLogic.get());
|
||||
if (g_connman) g_connman->Stop();
|
||||
if (node.peer_logic) UnregisterValidationInterface(node.peer_logic.get());
|
||||
if (node.connman) node.connman->Stop();
|
||||
if (g_txindex) g_txindex->Stop();
|
||||
ForEachBlockFilterIndex([](BlockFilterIndex& index) { index.Stop(); });
|
||||
|
||||
@@ -210,9 +208,9 @@ void Shutdown(InitInterfaces& interfaces)
|
||||
|
||||
// After the threads that potentially access these pointers have been stopped,
|
||||
// destruct and reset all to nullptr.
|
||||
peerLogic.reset();
|
||||
g_connman.reset();
|
||||
g_banman.reset();
|
||||
node.peer_logic.reset();
|
||||
node.connman.reset();
|
||||
node.banman.reset();
|
||||
g_txindex.reset();
|
||||
DestroyAllBlockFilterIndexes();
|
||||
|
||||
@@ -261,7 +259,7 @@ void Shutdown(InitInterfaces& interfaces)
|
||||
}
|
||||
pblocktree.reset();
|
||||
}
|
||||
for (const auto& client : interfaces.chain_clients) {
|
||||
for (const auto& client : node.chain_clients) {
|
||||
client->stop();
|
||||
}
|
||||
|
||||
@@ -280,7 +278,7 @@ void Shutdown(InitInterfaces& interfaces)
|
||||
} catch (const fs::filesystem_error& e) {
|
||||
LogPrintf("%s: Unable to remove PID file: %s\n", __func__, fsbridge::get_filesystem_error_message(e));
|
||||
}
|
||||
interfaces.chain_clients.clear();
|
||||
node.chain_clients.clear();
|
||||
UnregisterAllValidationInterfaces();
|
||||
GetMainSignals().UnregisterBackgroundSignalScheduler();
|
||||
GetMainSignals().UnregisterWithMempoolSignals(mempool);
|
||||
@@ -1207,7 +1205,7 @@ bool AppInitLockDataDirectory()
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AppInitMain(InitInterfaces& interfaces)
|
||||
bool AppInitMain(NodeContext& node)
|
||||
{
|
||||
const CChainParams& chainparams = Params();
|
||||
// ********************************************************* Step 4a: application initialization
|
||||
@@ -1275,16 +1273,16 @@ bool AppInitMain(InitInterfaces& interfaces)
|
||||
// according to -wallet and -disablewallet options. This only constructs
|
||||
// the interfaces, it doesn't load wallet data. Wallets actually get loaded
|
||||
// when load() and start() interface methods are called below.
|
||||
g_wallet_init_interface.Construct(interfaces);
|
||||
g_wallet_init_interface.Construct(node);
|
||||
|
||||
/* Register RPC commands regardless of -server setting so they will be
|
||||
* available in the GUI RPC console even if external calls are disabled.
|
||||
*/
|
||||
RegisterAllCoreRPCCommands(tableRPC);
|
||||
for (const auto& client : interfaces.chain_clients) {
|
||||
for (const auto& client : node.chain_clients) {
|
||||
client->registerRpcs();
|
||||
}
|
||||
g_rpc_interfaces = &interfaces;
|
||||
g_rpc_node = &node;
|
||||
#if ENABLE_ZMQ
|
||||
RegisterZMQRPCCommands(tableRPC);
|
||||
#endif
|
||||
@@ -1302,7 +1300,7 @@ bool AppInitMain(InitInterfaces& interfaces)
|
||||
}
|
||||
|
||||
// ********************************************************* Step 5: verify wallet database integrity
|
||||
for (const auto& client : interfaces.chain_clients) {
|
||||
for (const auto& client : node.chain_clients) {
|
||||
if (!client->verify()) {
|
||||
return false;
|
||||
}
|
||||
@@ -1314,13 +1312,13 @@ bool AppInitMain(InitInterfaces& interfaces)
|
||||
// is not yet setup and may end up being set up twice if we
|
||||
// need to reindex later.
|
||||
|
||||
assert(!g_banman);
|
||||
g_banman = MakeUnique<BanMan>(GetDataDir() / "banlist.dat", &uiInterface, gArgs.GetArg("-bantime", DEFAULT_MISBEHAVING_BANTIME));
|
||||
assert(!g_connman);
|
||||
g_connman = std::unique_ptr<CConnman>(new CConnman(GetRand(std::numeric_limits<uint64_t>::max()), GetRand(std::numeric_limits<uint64_t>::max())));
|
||||
assert(!node.banman);
|
||||
node.banman = MakeUnique<BanMan>(GetDataDir() / "banlist.dat", &uiInterface, gArgs.GetArg("-bantime", DEFAULT_MISBEHAVING_BANTIME));
|
||||
assert(!node.connman);
|
||||
node.connman = std::unique_ptr<CConnman>(new CConnman(GetRand(std::numeric_limits<uint64_t>::max()), GetRand(std::numeric_limits<uint64_t>::max())));
|
||||
|
||||
peerLogic.reset(new PeerLogicValidation(g_connman.get(), g_banman.get(), scheduler));
|
||||
RegisterValidationInterface(peerLogic.get());
|
||||
node.peer_logic.reset(new PeerLogicValidation(node.connman.get(), node.banman.get(), scheduler));
|
||||
RegisterValidationInterface(node.peer_logic.get());
|
||||
|
||||
// sanitize comments per BIP-0014, format user agent and check total size
|
||||
std::vector<std::string> uacomments;
|
||||
@@ -1661,7 +1659,7 @@ bool AppInitMain(InitInterfaces& interfaces)
|
||||
}
|
||||
|
||||
// ********************************************************* Step 9: load wallet
|
||||
for (const auto& client : interfaces.chain_clients) {
|
||||
for (const auto& client : node.chain_clients) {
|
||||
if (!client->load()) {
|
||||
return false;
|
||||
}
|
||||
@@ -1765,8 +1763,8 @@ bool AppInitMain(InitInterfaces& interfaces)
|
||||
connOptions.nMaxFeeler = 1;
|
||||
connOptions.nBestHeight = chain_active_height;
|
||||
connOptions.uiInterface = &uiInterface;
|
||||
connOptions.m_banman = g_banman.get();
|
||||
connOptions.m_msgproc = peerLogic.get();
|
||||
connOptions.m_banman = node.banman.get();
|
||||
connOptions.m_msgproc = node.peer_logic.get();
|
||||
connOptions.nSendBufferMaxSize = 1000*gArgs.GetArg("-maxsendbuffer", DEFAULT_MAXSENDBUFFER);
|
||||
connOptions.nReceiveFloodSize = 1000*gArgs.GetArg("-maxreceivebuffer", DEFAULT_MAXRECEIVEBUFFER);
|
||||
connOptions.m_added_nodes = gArgs.GetArgs("-addnode");
|
||||
@@ -1806,7 +1804,7 @@ bool AppInitMain(InitInterfaces& interfaces)
|
||||
connOptions.m_specified_outgoing = connect;
|
||||
}
|
||||
}
|
||||
if (!g_connman->Start(scheduler, connOptions)) {
|
||||
if (!node.connman->Start(scheduler, connOptions)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -1815,12 +1813,13 @@ bool AppInitMain(InitInterfaces& interfaces)
|
||||
SetRPCWarmupFinished();
|
||||
uiInterface.InitMessage(_("Done loading").translated);
|
||||
|
||||
for (const auto& client : interfaces.chain_clients) {
|
||||
for (const auto& client : node.chain_clients) {
|
||||
client->start(scheduler);
|
||||
}
|
||||
|
||||
scheduler.scheduleEvery([]{
|
||||
g_banman->DumpBanlist();
|
||||
BanMan* banman = node.banman.get();
|
||||
scheduler.scheduleEvery([banman]{
|
||||
banman->DumpBanlist();
|
||||
}, DUMP_BANS_INTERVAL * 1000);
|
||||
|
||||
return true;
|
||||
|
||||
Reference in New Issue
Block a user