Skip to content

Commit 9027680

Browse files
theunisipa
authored andcommitted
net: handle version push in InitializeNode
1 parent 7588b85 commit 9027680

File tree

4 files changed

+44
-41
lines changed

4 files changed

+44
-41
lines changed

src/main.cpp

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "init.h"
1919
#include "merkleblock.h"
2020
#include "net.h"
21+
#include "netbase.h"
2122
#include "policy/fees.h"
2223
#include "policy/policy.h"
2324
#include "pow.h"
@@ -354,11 +355,36 @@ void UpdatePreferredDownload(CNode* node, CNodeState* state)
354355
nPreferredDownload += state->fPreferredDownload;
355356
}
356357

357-
void InitializeNode(NodeId nodeid, const CNode *pnode) {
358+
void PushNodeVersion(CNode *pnode, CConnman& connman, int64_t nTime)
359+
{
360+
ServiceFlags nLocalNodeServices = pnode->GetLocalServices();
361+
uint64_t nonce = pnode->GetLocalNonce();
362+
int nNodeStartingHeight = pnode->GetMyStartingHeight();
363+
NodeId nodeid = pnode->GetId();
364+
CAddress addr = pnode->addr;
365+
366+
CAddress addrYou = (addr.IsRoutable() && !IsProxy(addr) ? addr : CAddress(CService(), addr.nServices));
367+
CAddress addrMe = CAddress(CService(), nLocalNodeServices);
368+
369+
connman.PushMessageWithVersion(pnode, INIT_PROTO_VERSION, NetMsgType::VERSION, PROTOCOL_VERSION, (uint64_t)nLocalNodeServices, nTime, addrYou, addrMe,
370+
nonce, strSubVersion, nNodeStartingHeight, ::fRelayTxes);
371+
372+
if (fLogIPs)
373+
LogPrint("net", "send version message: version %d, blocks=%d, us=%s, them=%s, peer=%d\n", PROTOCOL_VERSION, nNodeStartingHeight, addrMe.ToString(), addrYou.ToString(), nodeid);
374+
else
375+
LogPrint("net", "send version message: version %d, blocks=%d, us=%s, peer=%d\n", PROTOCOL_VERSION, nNodeStartingHeight, addrMe.ToString(), nodeid);
376+
}
377+
378+
void InitializeNode(CNode *pnode, CConnman& connman) {
358379
CAddress addr = pnode->addr;
359380
std::string addrName = pnode->addrName;
360-
LOCK(cs_main);
361-
mapNodeState.emplace_hint(mapNodeState.end(), std::piecewise_construct, std::forward_as_tuple(nodeid), std::forward_as_tuple(addr, std::move(addrName)));
381+
NodeId nodeid = pnode->GetId();
382+
{
383+
LOCK(cs_main);
384+
mapNodeState.emplace_hint(mapNodeState.end(), std::piecewise_construct, std::forward_as_tuple(nodeid), std::forward_as_tuple(addr, std::move(addrName)));
385+
}
386+
if(!pnode->fInbound)
387+
PushNodeVersion(pnode, connman, GetTime());
362388
}
363389

364390
void FinalizeNode(NodeId nodeid, bool& fUpdateConnectionTime) {
@@ -5118,7 +5144,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
51185144

51195145
// Be shy and don't send version until we hear
51205146
if (pfrom->fInbound)
5121-
connman.PushVersion(pfrom, GetAdjustedTime());
5147+
PushNodeVersion(pfrom, connman, GetAdjustedTime());
51225148

51235149
pfrom->fClient = !(pfrom->nServices & NODE_NETWORK);
51245150

src/net.cpp

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -393,21 +393,15 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo
393393
NodeId id = GetNewNodeId();
394394
uint64_t nonce = GetDeterministicRandomizer(RANDOMIZER_ID_LOCALHOSTNONCE).Write(id).Finalize();
395395
CNode* pnode = new CNode(id, nLocalServices, GetBestHeight(), hSocket, addrConnect, CalculateKeyedNetGroup(addrConnect), nonce, pszDest ? pszDest : "", false);
396-
397-
398-
PushVersion(pnode, GetTime());
399-
400-
GetNodeSignals().InitializeNode(pnode->GetId(), pnode);
396+
pnode->nServicesExpected = ServiceFlags(addrConnect.nServices & nRelevantServices);
397+
pnode->nTimeConnected = GetTime();
401398
pnode->AddRef();
402-
399+
GetNodeSignals().InitializeNode(pnode, *this);
403400
{
404401
LOCK(cs_vNodes);
405402
vNodes.push_back(pnode);
406403
}
407404

408-
pnode->nServicesExpected = ServiceFlags(addrConnect.nServices & nRelevantServices);
409-
pnode->nTimeConnected = GetTime();
410-
411405
return pnode;
412406
} else if (!proxyConnectionFailed) {
413407
// If connecting to the node failed, and failure is not caused by a problem connecting to
@@ -418,24 +412,6 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo
418412
return NULL;
419413
}
420414

421-
void CConnman::PushVersion(CNode* pnode, int64_t nTime)
422-
{
423-
ServiceFlags nLocalNodeServices = pnode->GetLocalServices();
424-
CAddress addrYou = (pnode->addr.IsRoutable() && !IsProxy(pnode->addr) ? pnode->addr : CAddress(CService(), pnode->addr.nServices));
425-
CAddress addrMe = CAddress(CService(), nLocalNodeServices);
426-
uint64_t nonce = pnode->GetLocalNonce();
427-
int nNodeStartingHeight = pnode->nMyStartingHeight;
428-
NodeId id = pnode->GetId();
429-
430-
PushMessageWithVersion(pnode, INIT_PROTO_VERSION, NetMsgType::VERSION, PROTOCOL_VERSION, (uint64_t)nLocalNodeServices, nTime, addrYou, addrMe,
431-
nonce, strSubVersion, nNodeStartingHeight, ::fRelayTxes);
432-
433-
if (fLogIPs)
434-
LogPrint("net", "send version message: version %d, blocks=%d, us=%s, them=%s, peer=%d\n", PROTOCOL_VERSION, nNodeStartingHeight, addrMe.ToString(), addrYou.ToString(), id);
435-
else
436-
LogPrint("net", "send version message: version %d, blocks=%d, us=%s, peer=%d\n", PROTOCOL_VERSION, nNodeStartingHeight, addrMe.ToString(), id);
437-
}
438-
439415
void CConnman::DumpBanlist()
440416
{
441417
SweepBanned(); // clean unused entries (if bantime has expired)
@@ -1036,9 +1012,9 @@ void CConnman::AcceptConnection(const ListenSocket& hListenSocket) {
10361012
uint64_t nonce = GetDeterministicRandomizer(RANDOMIZER_ID_LOCALHOSTNONCE).Write(id).Finalize();
10371013

10381014
CNode* pnode = new CNode(id, nLocalServices, GetBestHeight(), hSocket, addr, CalculateKeyedNetGroup(addr), nonce, "", true);
1039-
GetNodeSignals().InitializeNode(pnode->GetId(), pnode);
10401015
pnode->AddRef();
10411016
pnode->fWhitelisted = whitelisted;
1017+
GetNodeSignals().InitializeNode(pnode, *this);
10421018

10431019
LogPrint("net", "connection from %s accepted\n", addr.ToString());
10441020

@@ -2130,7 +2106,7 @@ bool CConnman::Start(boost::thread_group& threadGroup, CScheduler& scheduler, st
21302106
uint64_t nonce = GetDeterministicRandomizer(RANDOMIZER_ID_LOCALHOSTNONCE).Write(id).Finalize();
21312107

21322108
pnodeLocalHost = new CNode(id, nLocalServices, GetBestHeight(), INVALID_SOCKET, CAddress(CService(local, 0), nLocalServices), 0, nonce);
2133-
GetNodeSignals().InitializeNode(pnodeLocalHost->GetId(), pnodeLocalHost);
2109+
GetNodeSignals().InitializeNode(pnodeLocalHost, *this);
21342110
}
21352111

21362112
//

src/net.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,6 @@ class CConnman
163163
PushMessageWithVersionAndFlag(pnode, 0, 0, sCommand, std::forward<Args>(args)...);
164164
}
165165

166-
void PushVersion(CNode* pnode, int64_t nTime);
167-
168-
169166
template<typename Callable>
170167
bool ForEachNodeContinueIf(Callable&& func)
171168
{
@@ -462,7 +459,7 @@ struct CNodeSignals
462459
{
463460
boost::signals2::signal<bool (CNode*, CConnman&), CombinerAll> ProcessMessages;
464461
boost::signals2::signal<bool (CNode*, CConnman&), CombinerAll> SendMessages;
465-
boost::signals2::signal<void (NodeId, const CNode*)> InitializeNode;
462+
boost::signals2::signal<void (CNode*, CConnman&)> InitializeNode;
466463
boost::signals2::signal<void (NodeId, bool&)> FinalizeNode;
467464
};
468465

@@ -722,6 +719,10 @@ class CNode
722719
return nLocalHostNonce;
723720
}
724721

722+
int GetMyStartingHeight() const {
723+
return nMyStartingHeight;
724+
}
725+
725726
int GetRefCount()
726727
{
727728
assert(nRefCount >= 0);

src/test/DoS_tests.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ BOOST_AUTO_TEST_CASE(DoS_banning)
5050
CAddress addr1(ip(0xa0b0c001), NODE_NONE);
5151
CNode dummyNode1(id++, NODE_NETWORK, 0, INVALID_SOCKET, addr1, 0, 0, "", true);
5252
dummyNode1.SetSendVersion(PROTOCOL_VERSION);
53-
GetNodeSignals().InitializeNode(dummyNode1.GetId(), &dummyNode1);
53+
GetNodeSignals().InitializeNode(&dummyNode1, *connman);
5454
dummyNode1.nVersion = 1;
5555
Misbehaving(dummyNode1.GetId(), 100); // Should get banned
5656
SendMessages(&dummyNode1, *connman);
@@ -60,7 +60,7 @@ BOOST_AUTO_TEST_CASE(DoS_banning)
6060
CAddress addr2(ip(0xa0b0c002), NODE_NONE);
6161
CNode dummyNode2(id++, NODE_NETWORK, 0, INVALID_SOCKET, addr2, 1, 1, "", true);
6262
dummyNode2.SetSendVersion(PROTOCOL_VERSION);
63-
GetNodeSignals().InitializeNode(dummyNode2.GetId(), &dummyNode2);
63+
GetNodeSignals().InitializeNode(&dummyNode2, *connman);
6464
dummyNode2.nVersion = 1;
6565
Misbehaving(dummyNode2.GetId(), 50);
6666
SendMessages(&dummyNode2, *connman);
@@ -78,7 +78,7 @@ BOOST_AUTO_TEST_CASE(DoS_banscore)
7878
CAddress addr1(ip(0xa0b0c001), NODE_NONE);
7979
CNode dummyNode1(id++, NODE_NETWORK, 0, INVALID_SOCKET, addr1, 3, 1, "", true);
8080
dummyNode1.SetSendVersion(PROTOCOL_VERSION);
81-
GetNodeSignals().InitializeNode(dummyNode1.GetId(), &dummyNode1);
81+
GetNodeSignals().InitializeNode(&dummyNode1, *connman);
8282
dummyNode1.nVersion = 1;
8383
Misbehaving(dummyNode1.GetId(), 100);
8484
SendMessages(&dummyNode1, *connman);
@@ -101,7 +101,7 @@ BOOST_AUTO_TEST_CASE(DoS_bantime)
101101
CAddress addr(ip(0xa0b0c001), NODE_NONE);
102102
CNode dummyNode(id++, NODE_NETWORK, 0, INVALID_SOCKET, addr, 4, 4, "", true);
103103
dummyNode.SetSendVersion(PROTOCOL_VERSION);
104-
GetNodeSignals().InitializeNode(dummyNode.GetId(), &dummyNode);
104+
GetNodeSignals().InitializeNode(&dummyNode, *connman);
105105
dummyNode.nVersion = 1;
106106

107107
Misbehaving(dummyNode.GetId(), 100);

0 commit comments

Comments
 (0)