Skip to content

Commit 2edc29e

Browse files
laanwjcodablock
authored andcommitted
Merge bitcoin#10756: net processing: swap out signals for an interface class
2525b97 net: stop both net/net_processing before destroying them (Cory Fields) 80e2e9d net: drop unused connman param (Cory Fields) 8ad663c net: use an interface class rather than signals for message processing (Cory Fields) 28f11e9 net: pass CConnman via pointer rather than reference (Cory Fields) Pull request description: See individual commits. Benefits: - Allows us to begin moving stuff out of CNode and into CNodeState (after bitcoin#10652 and follow-ups) - Drops boost dependency and overhead - Drops global signal registration - Friendlier backtraces Tree-SHA512: af2038c959dbec25f0c90c74c88dc6a630e6b9e984adf52aceadd6954aa463b6aadfccf979c2459a9f3354326b5077ee02048128eda2a649236fadb595b66ee3
1 parent f8c310a commit 2edc29e

File tree

8 files changed

+180
-201
lines changed

8 files changed

+180
-201
lines changed

src/init.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -243,12 +243,14 @@ void PrepareShutdown()
243243
}
244244
#endif
245245
MapPort(false);
246+
247+
// Because these depend on each-other, we make sure that neither can be
248+
// using the other before destroying them.
246249
UnregisterValidationInterface(peerLogic.get());
247-
peerLogic.reset();
248250
if (g_connman) {
249-
// make sure to stop all threads before g_connman is reset to nullptr as these threads might still be accessing it
250251
g_connman->Stop();
251252
}
253+
peerLogic.reset();
252254
g_connman.reset();
253255

254256
if (!fLiteMode && !fRPCInWarmup) {
@@ -263,7 +265,6 @@ void PrepareShutdown()
263265
flatdb6.Dump(sporkManager);
264266
}
265267

266-
UnregisterNodeSignals(GetNodeSignals());
267268
if (fDumpMempoolLater && gArgs.GetArg("-persistmempool", DEFAULT_PERSIST_MEMPOOL)) {
268269
DumpMempool();
269270
}
@@ -1588,7 +1589,6 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
15881589

15891590
peerLogic.reset(new PeerLogicValidation(&connman));
15901591
RegisterValidationInterface(peerLogic.get());
1591-
RegisterNodeSignals(GetNodeSignals());
15921592

15931593
// sanitize comments per BIP-0014, format user agent and check total size
15941594
std::vector<std::string> uacomments;
@@ -2127,6 +2127,7 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
21272127
connOptions.nMaxFeeler = 1;
21282128
connOptions.nBestHeight = chainActive.Height();
21292129
connOptions.uiInterface = &uiInterface;
2130+
connOptions.m_msgproc = peerLogic.get();
21302131
connOptions.nSendBufferMaxSize = 1000*gArgs.GetArg("-maxsendbuffer", DEFAULT_MAXSENDBUFFER);
21312132
connOptions.nReceiveFloodSize = 1000*gArgs.GetArg("-maxreceivebuffer", DEFAULT_MAXRECEIVEBUFFER);
21322133

src/net.cpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,6 @@ std::string strSubVersion;
9999

100100
unordered_limitedmap<uint256, int64_t, StaticSaltedHasher> mapAlreadyAskedFor(MAX_INV_SZ, MAX_INV_SZ * 2);
101101

102-
// Signals for message handling
103-
static CNodeSignals g_signals;
104-
CNodeSignals& GetNodeSignals() { return g_signals; }
105-
106102
void CConnman::AddOneShot(const std::string& strDest)
107103
{
108104
LOCK(cs_vOneShots);
@@ -1201,7 +1197,7 @@ void CConnman::AcceptConnection(const ListenSocket& hListenSocket) {
12011197
CNode* pnode = new CNode(id, nLocalServices, GetBestHeight(), hSocket, addr, CalculateKeyedNetGroup(addr), nonce, addr_bind, "", true);
12021198
pnode->AddRef();
12031199
pnode->fWhitelisted = whitelisted;
1204-
GetNodeSignals().InitializeNode(pnode, *this);
1200+
m_msgproc->InitializeNode(pnode);
12051201

12061202
if (fLogIPs) {
12071203
LogPrint(BCLog::NET, "connection from %s accepted\n", addr.ToString());
@@ -2187,7 +2183,7 @@ bool CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFai
21872183
if (fConnectToMasternode)
21882184
pnode->fMasternode = true;
21892185

2190-
GetNodeSignals().InitializeNode(pnode, *this);
2186+
m_msgproc->InitializeNode(pnode);
21912187
{
21922188
LOCK(cs_vNodes);
21932189
vNodes.push_back(pnode);
@@ -2214,16 +2210,16 @@ void CConnman::ThreadMessageHandler()
22142210
continue;
22152211

22162212
// Receive messages
2217-
bool fMoreNodeWork = GetNodeSignals().ProcessMessages(pnode, *this, flagInterruptMsgProc);
2213+
bool fMoreNodeWork = m_msgproc->ProcessMessages(pnode, flagInterruptMsgProc);
22182214
fMoreWork |= (fMoreNodeWork && !pnode->fPauseSend);
22192215
if (flagInterruptMsgProc)
22202216
return;
2221-
22222217
// Send messages
22232218
{
22242219
LOCK(pnode->cs_sendProcessing);
2225-
GetNodeSignals().SendMessages(pnode, *this, flagInterruptMsgProc);
2220+
m_msgproc->SendMessages(pnode, flagInterruptMsgProc);
22262221
}
2222+
22272223
if (flagInterruptMsgProc)
22282224
return;
22292225
}
@@ -2546,6 +2542,7 @@ bool CConnman::Start(CScheduler& scheduler, const Options& connOptions)
25462542
//
25472543
// Start threads
25482544
//
2545+
assert(m_msgproc);
25492546
InterruptSocks5(false);
25502547
interruptNet.reset();
25512548
flagInterruptMsgProc = false;
@@ -2709,9 +2706,10 @@ void CConnman::DeleteNode(CNode* pnode)
27092706
{
27102707
assert(pnode);
27112708
bool fUpdateConnectionTime = false;
2712-
GetNodeSignals().FinalizeNode(pnode->GetId(), fUpdateConnectionTime);
2713-
if(fUpdateConnectionTime)
2709+
m_msgproc->FinalizeNode(pnode->GetId(), fUpdateConnectionTime);
2710+
if(fUpdateConnectionTime) {
27142711
addrman.Connected(pnode->addr);
2712+
}
27152713
delete pnode;
27162714
}
27172715

src/net.h

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
#include <arpa/inet.h>
3838
#endif
3939

40-
#include <boost/signals2/signal.hpp>
4140

4241
// "Optimistic send" was introduced in the beginning of the Bitcoin project. I assume this was done because it was
4342
// thought that "send" would be very cheap when the send buffer is empty. This is not true, as shown by profiling.
@@ -140,7 +139,7 @@ struct CSerializedNetMsg
140139
std::string command;
141140
};
142141

143-
142+
class NetEventsInterface;
144143
class CConnman
145144
{
146145
public:
@@ -161,6 +160,7 @@ class CConnman
161160
int nMaxFeeler = 0;
162161
int nBestHeight = 0;
163162
CClientUIInterface* uiInterface = nullptr;
163+
NetEventsInterface* m_msgproc = nullptr;
164164
unsigned int nSendBufferMaxSize = 0;
165165
unsigned int nReceiveFloodSize = 0;
166166
uint64_t nMaxOutboundTimeframe = 0;
@@ -178,6 +178,7 @@ class CConnman
178178
nMaxFeeler = connOptions.nMaxFeeler;
179179
nBestHeight = connOptions.nBestHeight;
180180
clientInterface = connOptions.uiInterface;
181+
m_msgproc = connOptions.m_msgproc;
181182
nSendBufferMaxSize = connOptions.nSendBufferMaxSize;
182183
nReceiveFloodSize = connOptions.nReceiveFloodSize;
183184
nMaxOutboundTimeframe = connOptions.nMaxOutboundTimeframe;
@@ -537,6 +538,7 @@ class CConnman
537538
int nMaxFeeler;
538539
std::atomic<int> nBestHeight;
539540
CClientUIInterface* clientInterface;
541+
NetEventsInterface* m_msgproc;
540542

541543
/** SipHasher seeds for deterministic randomness */
542544
const uint64_t nSeed0, nSeed1;
@@ -584,19 +586,18 @@ struct CombinerAll
584586
}
585587
};
586588

587-
// Signals for message handling
588-
struct CNodeSignals
589+
/**
590+
* Interface for message handling
591+
*/
592+
class NetEventsInterface
589593
{
590-
boost::signals2::signal<bool (CNode*, CConnman&, std::atomic<bool>&), CombinerAll> ProcessMessages;
591-
boost::signals2::signal<bool (CNode*, CConnman&, std::atomic<bool>&), CombinerAll> SendMessages;
592-
boost::signals2::signal<void (CNode*, CConnman&)> InitializeNode;
593-
boost::signals2::signal<void (NodeId, bool&)> FinalizeNode;
594+
public:
595+
virtual bool ProcessMessages(CNode* pnode, std::atomic<bool>& interrupt) = 0;
596+
virtual bool SendMessages(CNode* pnode, std::atomic<bool>& interrupt) = 0;
597+
virtual void InitializeNode(CNode* pnode) = 0;
598+
virtual void FinalizeNode(NodeId id, bool& update_connection_time) = 0;
594599
};
595600

596-
597-
CNodeSignals& GetNodeSignals();
598-
599-
600601
enum
601602
{
602603
LOCAL_NONE, // unknown

0 commit comments

Comments
 (0)