@@ -394,6 +394,9 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo
394
394
uint64_t nonce = GetDeterministicRandomizer (RANDOMIZER_ID_LOCALHOSTNONCE).Write (id).Finalize ();
395
395
CNode* pnode = new CNode (id, nLocalServices, GetBestHeight (), hSocket, addrConnect, CalculateKeyedNetGroup (addrConnect), nonce, pszDest ? pszDest : " " , false );
396
396
397
+
398
+ PushVersion (pnode, GetTime ());
399
+
397
400
GetNodeSignals ().InitializeNode (pnode->GetId (), pnode);
398
401
pnode->AddRef ();
399
402
@@ -415,6 +418,24 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo
415
418
return NULL ;
416
419
}
417
420
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
+
418
439
void CConnman::DumpBanlist ()
419
440
{
420
441
SweepBanned (); // clean unused entries (if bantime has expired)
@@ -450,23 +471,6 @@ void CNode::CloseSocketDisconnect()
450
471
vRecvMsg.clear ();
451
472
}
452
473
453
- void CNode::PushVersion ()
454
- {
455
- int64_t nTime = (fInbound ? GetAdjustedTime () : GetTime ());
456
- CAddress addrYou = (addr.IsRoutable () && !IsProxy (addr) ? addr : CAddress (CService (), addr.nServices ));
457
- CAddress addrMe = CAddress (CService (), nLocalServices);
458
- if (fLogIPs )
459
- LogPrint (" net" , " send version message: version %d, blocks=%d, us=%s, them=%s, peer=%d\n " , PROTOCOL_VERSION, nMyStartingHeight, addrMe.ToString (), addrYou.ToString (), id);
460
- else
461
- LogPrint (" net" , " send version message: version %d, blocks=%d, us=%s, peer=%d\n " , PROTOCOL_VERSION, nMyStartingHeight, addrMe.ToString (), id);
462
- PushMessage (NetMsgType::VERSION, PROTOCOL_VERSION, (uint64_t )nLocalServices, nTime, addrYou, addrMe,
463
- nLocalHostNonce, strSubVersion, nMyStartingHeight, ::fRelayTxes );
464
- }
465
-
466
-
467
-
468
-
469
-
470
474
void CConnman::ClearBanned ()
471
475
{
472
476
{
@@ -2530,7 +2534,8 @@ CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, int nMyStartingHeightIn
2530
2534
filterInventoryKnown(50000 , 0.000001 ),
2531
2535
nLocalHostNonce(nLocalHostNonceIn),
2532
2536
nLocalServices(nLocalServicesIn),
2533
- nMyStartingHeight(nMyStartingHeightIn)
2537
+ nMyStartingHeight(nMyStartingHeightIn),
2538
+ nSendVersion(0 )
2534
2539
{
2535
2540
nServices = NODE_NONE;
2536
2541
nServicesExpected = NODE_NONE;
@@ -2587,10 +2592,6 @@ CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, int nMyStartingHeightIn
2587
2592
LogPrint (" net" , " Added connection to %s peer=%d\n " , addrName, id);
2588
2593
else
2589
2594
LogPrint (" net" , " Added connection peer=%d\n " , id);
2590
-
2591
- // Be shy and don't send version until we hear
2592
- if (hSocket != INVALID_SOCKET && !fInbound )
2593
- PushVersion ();
2594
2595
}
2595
2596
2596
2597
CNode::~CNode ()
@@ -2696,6 +2697,52 @@ void CNode::EndMessage(const char* pszCommand) UNLOCK_FUNCTION(cs_vSend)
2696
2697
LEAVE_CRITICAL_SECTION (cs_vSend);
2697
2698
}
2698
2699
2700
+ CDataStream CConnman::BeginMessage (CNode* pnode, int nVersion, int flags, const std::string& sCommand )
2701
+ {
2702
+ return {SER_NETWORK, (nVersion ? nVersion : pnode->GetSendVersion ()) | flags, CMessageHeader (Params ().MessageStart (), sCommand .c_str (), 0 ) };
2703
+ }
2704
+
2705
+ void CConnman::EndMessage (CDataStream& strm)
2706
+ {
2707
+ // Set the size
2708
+ assert (strm.size () >= CMessageHeader::HEADER_SIZE);
2709
+ unsigned int nSize = strm.size () - CMessageHeader::HEADER_SIZE;
2710
+ WriteLE32 ((uint8_t *)&strm[CMessageHeader::MESSAGE_SIZE_OFFSET], nSize);
2711
+ // Set the checksum
2712
+ uint256 hash = Hash (strm.begin () + CMessageHeader::HEADER_SIZE, strm.end ());
2713
+ memcpy ((char *)&strm[CMessageHeader::CHECKSUM_OFFSET], hash.begin (), CMessageHeader::CHECKSUM_SIZE);
2714
+
2715
+ }
2716
+
2717
+ void CConnman::PushMessage (CNode* pnode, CDataStream& strm, const std::string& sCommand )
2718
+ {
2719
+ if (strm.empty ())
2720
+ return ;
2721
+
2722
+ unsigned int nSize = strm.size () - CMessageHeader::HEADER_SIZE;
2723
+ LogPrint (" net" , " sending %s (%d bytes) peer=%d\n " , SanitizeString (sCommand .c_str ()), nSize, pnode->id );
2724
+
2725
+ size_t nBytesSent = 0 ;
2726
+ {
2727
+ LOCK (pnode->cs_vSend );
2728
+ if (pnode->hSocket == INVALID_SOCKET) {
2729
+ return ;
2730
+ }
2731
+ bool optimisticSend (pnode->vSendMsg .empty ());
2732
+ pnode->vSendMsg .emplace_back (strm.begin (), strm.end ());
2733
+
2734
+ // log total amount of bytes per command
2735
+ pnode->mapSendBytesPerMsgCmd [sCommand ] += strm.size ();
2736
+ pnode->nSendSize += strm.size ();
2737
+
2738
+ // If write queue empty, attempt "optimistic write"
2739
+ if (optimisticSend == true )
2740
+ nBytesSent = SocketSendData (pnode);
2741
+ }
2742
+ if (nBytesSent)
2743
+ RecordBytesSent (nBytesSent);
2744
+ }
2745
+
2699
2746
bool CConnman::ForNode (NodeId id, std::function<bool (CNode* pnode)> func)
2700
2747
{
2701
2748
CNode* found = nullptr ;
0 commit comments