mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-12-13 06:03:54 +01:00
Merge #8085: p2p: Begin encapsulation
0103c5bnet: move MAX_FEELER_CONNECTIONS into connman (Cory Fields)e700cd0Convert ForEachNode* functions to take a templated function argument rather than a std::function to eliminate std::function overhead (Jeremy Rubin)d1a2295Made the ForEachNode* functions in src/net.cpp more pragmatic and self documenting (Jeremy Rubin)98591c5net: move vNodesDisconnected into CConnman (Cory Fields)fa2f8bcnet: add nSendBufferMaxSize/nReceiveFloodSize to CConnection::Options (Cory Fields)a19553bnet: Introduce CConnection::Options to avoid passing so many params (Cory Fields)bafa5fcnet: Drop StartNode/StopNode and use CConnman directly (Cory Fields)e81a602net: pass CClientUIInterface into CConnman (Cory Fields)f60b905net: Pass best block known height into CConnman (Cory Fields)fdf69ffnet: move max/max-outbound to CConnman (Cory Fields)8a59369net: move semOutbound to CConnman (Cory Fields)bd72937net: move nLocalServices/nRelevantServices to CConnman (Cory Fields)be9c796net: move SendBufferSize/ReceiveFloodSize to CConnman (Cory Fields)63cafa6net: move send/recv statistics to CConnman (Cory Fields)adf5d4cnet: SocketSendData returns written size (Cory Fields)ee44fa9net: move messageHandlerCondition to CConnman (Cory Fields)960cf2enet: move nLocalHostNonce to CConnman (Cory Fields)551e088net: move nLastNodeId to CConnman (Cory Fields)6c19d92net: move whitelist functions into CConnman (Cory Fields)53347f0net: create generic functor accessors and move vNodes to CConnman (Cory Fields)c0569c7net: Add most functions needed for vNodes to CConnman (Cory Fields)8ae2dacnet: move added node functions to CConnman (Cory Fields)502dd3anet: Add oneshot functions to CConnman (Cory Fields)a0f3d3cnet: move ban and addrman functions into CConnman (Cory Fields)aaf018enet: handle nodesignals in CConnman (Cory Fields)b1a5f43net: move OpenNetworkConnection into CConnman (Cory Fields)02137f1net: Move socket binding into CConnman (Cory Fields)5b446ddnet: Pass CConnection to wallet rather than using the global (Cory Fields)8d58c4dnet: Pass CConnman around as needed (Cory Fields)d7349canet: Add rpc error for missing/disabled p2p functionality (Cory Fields)cd16f48net: Create CConnman to encapsulate p2p connections (Cory Fields)d93b14dnet: move CBanDB and CAddrDB out of net.h/cpp (Cory Fields)531214fgui: add NodeID to the peer table (Cory Fields)
This commit is contained in:
54
src/init.cpp
54
src/init.cpp
@@ -42,6 +42,7 @@
|
||||
#endif
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <memory>
|
||||
|
||||
#ifndef WIN32
|
||||
#include <signal.h>
|
||||
@@ -70,6 +71,7 @@ static const bool DEFAULT_REST_ENABLE = false;
|
||||
static const bool DEFAULT_DISABLE_SAFEMODE = false;
|
||||
static const bool DEFAULT_STOPAFTERBLOCKIMPORT = false;
|
||||
|
||||
std::unique_ptr<CConnman> g_connman;
|
||||
|
||||
#if ENABLE_ZMQ
|
||||
static CZMQNotificationInterface* pzmqNotificationInterface = NULL;
|
||||
@@ -197,7 +199,10 @@ void Shutdown()
|
||||
if (pwalletMain)
|
||||
pwalletMain->Flush(false);
|
||||
#endif
|
||||
StopNode();
|
||||
MapPort(false);
|
||||
g_connman->Stop();
|
||||
g_connman.reset();
|
||||
|
||||
StopTorControl();
|
||||
UnregisterNodeSignals(GetNodeSignals());
|
||||
|
||||
@@ -269,11 +274,11 @@ void HandleSIGHUP(int)
|
||||
fReopenDebugLog = true;
|
||||
}
|
||||
|
||||
bool static Bind(const CService &addr, unsigned int flags) {
|
||||
bool static Bind(CConnman& connman, const CService &addr, unsigned int flags) {
|
||||
if (!(flags & BF_EXPLICIT) && IsLimited(addr))
|
||||
return false;
|
||||
std::string strError;
|
||||
if (!BindListenPort(addr, strError, (flags & BF_WHITELIST) != 0)) {
|
||||
if (!connman.BindListenPort(addr, strError, (flags & BF_WHITELIST) != 0)) {
|
||||
if (flags & BF_REPORT_ERROR)
|
||||
return InitError(strError);
|
||||
return false;
|
||||
@@ -864,7 +869,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
|
||||
// Make sure enough file descriptors are available
|
||||
int nBind = std::max((int)mapArgs.count("-bind") + (int)mapArgs.count("-whitebind"), 1);
|
||||
int nUserMaxConnections = GetArg("-maxconnections", DEFAULT_MAX_PEER_CONNECTIONS);
|
||||
nMaxConnections = std::max(nUserMaxConnections, 0);
|
||||
int nMaxConnections = std::max(nUserMaxConnections, 0);
|
||||
|
||||
// Trim requested connection counts, to fit into system limitations
|
||||
nMaxConnections = std::max(std::min(nMaxConnections, (int)(FD_SETSIZE - nBind - MIN_CORE_FILEDESCRIPTORS)), 0);
|
||||
@@ -985,6 +990,9 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
|
||||
// Option to startup with mocktime set (used for regression testing):
|
||||
SetMockTime(GetArg("-mocktime", 0)); // SetMockTime(0) is a no-op
|
||||
|
||||
ServiceFlags nLocalServices = NODE_NETWORK;
|
||||
ServiceFlags nRelevantServices = NODE_NETWORK;
|
||||
|
||||
if (GetBoolArg("-peerbloomfilters", DEFAULT_PEERBLOOMFILTERS))
|
||||
nLocalServices = ServiceFlags(nLocalServices | NODE_BLOOM);
|
||||
|
||||
@@ -1108,6 +1116,10 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
|
||||
#endif // ENABLE_WALLET
|
||||
// ********************************************************* Step 6: network initialization
|
||||
|
||||
assert(!g_connman);
|
||||
g_connman = std::unique_ptr<CConnman>(new CConnman());
|
||||
CConnman& connman = *g_connman;
|
||||
|
||||
RegisterNodeSignals(GetNodeSignals());
|
||||
|
||||
// sanitize comments per BIP-0014, format user agent and check total size
|
||||
@@ -1145,7 +1157,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
|
||||
LookupSubNet(net.c_str(), subnet);
|
||||
if (!subnet.IsValid())
|
||||
return InitError(strprintf(_("Invalid netmask specified in -whitelist: '%s'"), net));
|
||||
CNode::AddWhitelistedRange(subnet);
|
||||
connman.AddWhitelistedRange(subnet);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1197,7 +1209,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
|
||||
CService addrBind;
|
||||
if (!Lookup(strBind.c_str(), addrBind, GetListenPort(), false))
|
||||
return InitError(ResolveErrMsg("bind", strBind));
|
||||
fBound |= Bind(addrBind, (BF_EXPLICIT | BF_REPORT_ERROR));
|
||||
fBound |= Bind(connman, addrBind, (BF_EXPLICIT | BF_REPORT_ERROR));
|
||||
}
|
||||
BOOST_FOREACH(const std::string& strBind, mapMultiArgs["-whitebind"]) {
|
||||
CService addrBind;
|
||||
@@ -1205,14 +1217,14 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
|
||||
return InitError(ResolveErrMsg("whitebind", strBind));
|
||||
if (addrBind.GetPort() == 0)
|
||||
return InitError(strprintf(_("Need to specify a port with -whitebind: '%s'"), strBind));
|
||||
fBound |= Bind(addrBind, (BF_EXPLICIT | BF_REPORT_ERROR | BF_WHITELIST));
|
||||
fBound |= Bind(connman, addrBind, (BF_EXPLICIT | BF_REPORT_ERROR | BF_WHITELIST));
|
||||
}
|
||||
}
|
||||
else {
|
||||
struct in_addr inaddr_any;
|
||||
inaddr_any.s_addr = INADDR_ANY;
|
||||
fBound |= Bind(CService(in6addr_any, GetListenPort()), BF_NONE);
|
||||
fBound |= Bind(CService(inaddr_any, GetListenPort()), !fBound ? BF_REPORT_ERROR : BF_NONE);
|
||||
fBound |= Bind(connman, CService(in6addr_any, GetListenPort()), BF_NONE);
|
||||
fBound |= Bind(connman, CService(inaddr_any, GetListenPort()), !fBound ? BF_REPORT_ERROR : BF_NONE);
|
||||
}
|
||||
if (!fBound)
|
||||
return InitError(_("Failed to listen on any port. Use -listen=0 if you want this."));
|
||||
@@ -1229,7 +1241,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
|
||||
}
|
||||
|
||||
BOOST_FOREACH(const std::string& strDest, mapMultiArgs["-seednode"])
|
||||
AddOneShot(strDest);
|
||||
connman.AddOneShot(strDest);
|
||||
|
||||
#if ENABLE_ZMQ
|
||||
pzmqNotificationInterface = CZMQNotificationInterface::CreateWithArguments(mapArgs);
|
||||
@@ -1239,7 +1251,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
|
||||
}
|
||||
#endif
|
||||
if (mapArgs.count("-maxuploadtarget")) {
|
||||
CNode::SetMaxOutboundTarget(GetArg("-maxuploadtarget", DEFAULT_MAX_UPLOAD_TARGET)*1024*1024);
|
||||
connman.SetMaxOutboundTarget(GetArg("-maxuploadtarget", DEFAULT_MAX_UPLOAD_TARGET)*1024*1024);
|
||||
}
|
||||
|
||||
// ********************************************************* Step 7: load block chain
|
||||
@@ -1505,7 +1517,25 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
|
||||
if (GetBoolArg("-listenonion", DEFAULT_LISTEN_ONION))
|
||||
StartTorControl(threadGroup, scheduler);
|
||||
|
||||
StartNode(threadGroup, scheduler);
|
||||
Discover(threadGroup);
|
||||
|
||||
// Map ports with UPnP
|
||||
MapPort(GetBoolArg("-upnp", DEFAULT_UPNP));
|
||||
|
||||
std::string strNodeError;
|
||||
CConnman::Options connOptions;
|
||||
connOptions.nLocalServices = nLocalServices;
|
||||
connOptions.nRelevantServices = nRelevantServices;
|
||||
connOptions.nMaxConnections = nMaxConnections;
|
||||
connOptions.nMaxOutbound = std::min(MAX_OUTBOUND_CONNECTIONS, connOptions.nMaxConnections);
|
||||
connOptions.nMaxFeeler = 1;
|
||||
connOptions.nBestHeight = chainActive.Height();
|
||||
connOptions.uiInterface = &uiInterface;
|
||||
connOptions.nSendBufferMaxSize = 1000*GetArg("-maxsendbuffer", DEFAULT_MAXSENDBUFFER);
|
||||
connOptions.nReceiveFloodSize = 1000*GetArg("-maxreceivebuffer", DEFAULT_MAXRECEIVEBUFFER);
|
||||
|
||||
if(!connman.Start(threadGroup, scheduler, strNodeError, connOptions))
|
||||
return InitError(strNodeError);
|
||||
|
||||
// ********************************************************* Step 12: finished
|
||||
|
||||
|
||||
Reference in New Issue
Block a user