@@ -393,18 +393,15 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo
393
393
NodeId id = GetNewNodeId ();
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
-
397
- GetNodeSignals (). InitializeNode ( pnode->GetId (), pnode );
396
+ pnode-> nServicesExpected = ServiceFlags (addrConnect. nServices & nRelevantServices);
397
+ pnode->nTimeConnected = GetTime ( );
398
398
pnode->AddRef ();
399
-
399
+ GetNodeSignals (). InitializeNode (pnode, * this );
400
400
{
401
401
LOCK (cs_vNodes);
402
402
vNodes.push_back (pnode);
403
403
}
404
404
405
- pnode->nServicesExpected = ServiceFlags (addrConnect.nServices & nRelevantServices);
406
- pnode->nTimeConnected = GetTime ();
407
-
408
405
return pnode;
409
406
} else if (!proxyConnectionFailed) {
410
407
// If connecting to the node failed, and failure is not caused by a problem connecting to
@@ -450,23 +447,6 @@ void CNode::CloseSocketDisconnect()
450
447
vRecvMsg.clear ();
451
448
}
452
449
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
450
void CConnman::ClearBanned ()
471
451
{
472
452
{
@@ -1032,9 +1012,9 @@ void CConnman::AcceptConnection(const ListenSocket& hListenSocket) {
1032
1012
uint64_t nonce = GetDeterministicRandomizer (RANDOMIZER_ID_LOCALHOSTNONCE).Write (id).Finalize ();
1033
1013
1034
1014
CNode* pnode = new CNode (id, nLocalServices, GetBestHeight (), hSocket, addr, CalculateKeyedNetGroup (addr), nonce, " " , true );
1035
- GetNodeSignals ().InitializeNode (pnode->GetId (), pnode);
1036
1015
pnode->AddRef ();
1037
1016
pnode->fWhitelisted = whitelisted;
1017
+ GetNodeSignals ().InitializeNode (pnode, *this );
1038
1018
1039
1019
LogPrint (" net" , " connection from %s accepted\n " , addr.ToString ());
1040
1020
@@ -1059,7 +1039,7 @@ void CConnman::ThreadSocketHandler()
1059
1039
BOOST_FOREACH (CNode* pnode, vNodesCopy)
1060
1040
{
1061
1041
if (pnode->fDisconnect ||
1062
- (pnode->GetRefCount () <= 0 && pnode->vRecvMsg .empty () && pnode->nSendSize == 0 && pnode-> ssSend . empty () ))
1042
+ (pnode->GetRefCount () <= 0 && pnode->vRecvMsg .empty () && pnode->nSendSize == 0 ))
1063
1043
{
1064
1044
// remove from vNodes
1065
1045
vNodes.erase (remove (vNodes.begin (), vNodes.end (), pnode), vNodes.end ());
@@ -1163,10 +1143,6 @@ void CConnman::ThreadSocketHandler()
1163
1143
{
1164
1144
TRY_LOCK (pnode->cs_vSend , lockSend);
1165
1145
if (lockSend) {
1166
- if (pnode->nOptimisticBytesWritten ) {
1167
- RecordBytesSent (pnode->nOptimisticBytesWritten );
1168
- pnode->nOptimisticBytesWritten = 0 ;
1169
- }
1170
1146
if (!pnode->vSendMsg .empty ()) {
1171
1147
FD_SET (pnode->hSocket , &fdsetSend);
1172
1148
continue ;
@@ -2130,7 +2106,7 @@ bool CConnman::Start(boost::thread_group& threadGroup, CScheduler& scheduler, st
2130
2106
uint64_t nonce = GetDeterministicRandomizer (RANDOMIZER_ID_LOCALHOSTNONCE).Write (id).Finalize ();
2131
2107
2132
2108
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 );
2134
2110
}
2135
2111
2136
2112
//
@@ -2482,46 +2458,10 @@ int CConnman::GetBestHeight() const
2482
2458
return nBestHeight.load (std::memory_order_acquire);
2483
2459
}
2484
2460
2485
- void CNode::Fuzz (int nChance)
2486
- {
2487
- if (!fSuccessfullyConnected ) return ; // Don't fuzz initial handshake
2488
- if (GetRand (nChance) != 0 ) return ; // Fuzz 1 of every nChance messages
2489
-
2490
- switch (GetRand (3 ))
2491
- {
2492
- case 0 :
2493
- // xor a random byte with a random value:
2494
- if (!ssSend.empty ()) {
2495
- CDataStream::size_type pos = GetRand (ssSend.size ());
2496
- ssSend[pos] ^= (unsigned char )(GetRand (256 ));
2497
- }
2498
- break ;
2499
- case 1 :
2500
- // delete a random byte:
2501
- if (!ssSend.empty ()) {
2502
- CDataStream::size_type pos = GetRand (ssSend.size ());
2503
- ssSend.erase (ssSend.begin ()+pos);
2504
- }
2505
- break ;
2506
- case 2 :
2507
- // insert a random byte at a random position
2508
- {
2509
- CDataStream::size_type pos = GetRand (ssSend.size ());
2510
- char ch = (char )GetRand (256 );
2511
- ssSend.insert (ssSend.begin ()+pos, ch);
2512
- }
2513
- break ;
2514
- }
2515
- // Chance of more than one change half the time:
2516
- // (more changes exponentially less likely):
2517
- Fuzz (2 );
2518
- }
2519
-
2520
2461
unsigned int CConnman::GetReceiveFloodSize () const { return nReceiveFloodSize; }
2521
2462
unsigned int CConnman::GetSendBufferSize () const { return nSendBufferMaxSize; }
2522
2463
2523
2464
CNode::CNode (NodeId idIn, ServiceFlags nLocalServicesIn, int nMyStartingHeightIn, SOCKET hSocketIn, const CAddress& addrIn, uint64_t nKeyedNetGroupIn, uint64_t nLocalHostNonceIn, const std::string& addrNameIn, bool fInboundIn ) :
2524
- ssSend(SER_NETWORK, INIT_PROTO_VERSION),
2525
2465
addr(addrIn),
2526
2466
fInbound(fInboundIn ),
2527
2467
id(idIn),
@@ -2530,7 +2470,8 @@ CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, int nMyStartingHeightIn
2530
2470
filterInventoryKnown(50000 , 0.000001 ),
2531
2471
nLocalHostNonce(nLocalHostNonceIn),
2532
2472
nLocalServices(nLocalServicesIn),
2533
- nMyStartingHeight(nMyStartingHeightIn)
2473
+ nMyStartingHeight(nMyStartingHeightIn),
2474
+ nSendVersion(0 )
2534
2475
{
2535
2476
nServices = NODE_NONE;
2536
2477
nServicesExpected = NODE_NONE;
@@ -2577,7 +2518,6 @@ CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, int nMyStartingHeightIn
2577
2518
minFeeFilter = 0 ;
2578
2519
lastSentFeeFilter = 0 ;
2579
2520
nextSendTimeFeeFilter = 0 ;
2580
- nOptimisticBytesWritten = 0 ;
2581
2521
2582
2522
BOOST_FOREACH (const std::string &msg, getAllNetMessageTypes ())
2583
2523
mapRecvBytesPerMsgCmd[msg] = 0 ;
@@ -2587,10 +2527,6 @@ CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, int nMyStartingHeightIn
2587
2527
LogPrint (" net" , " Added connection to %s peer=%d\n " , addrName, id);
2588
2528
else
2589
2529
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
2530
}
2595
2531
2596
2532
CNode::~CNode ()
@@ -2635,65 +2571,50 @@ void CNode::AskFor(const CInv& inv)
2635
2571
mapAskFor.insert (std::make_pair (nRequestTime, inv));
2636
2572
}
2637
2573
2638
- void CNode ::BeginMessage (const char * pszCommand) EXCLUSIVE_LOCK_FUNCTION(cs_vSend )
2574
+ CDataStream CConnman ::BeginMessage (CNode* pnode, int nVersion, int flags, const std::string& sCommand )
2639
2575
{
2640
- ENTER_CRITICAL_SECTION (cs_vSend);
2641
- assert (ssSend.size () == 0 );
2642
- ssSend << CMessageHeader (Params ().MessageStart (), pszCommand, 0 );
2643
- LogPrint (" net" , " sending: %s " , SanitizeString (pszCommand));
2576
+ return {SER_NETWORK, (nVersion ? nVersion : pnode->GetSendVersion ()) | flags, CMessageHeader (Params ().MessageStart (), sCommand .c_str (), 0 ) };
2644
2577
}
2645
2578
2646
- void CNode::AbortMessage () UNLOCK_FUNCTION(cs_vSend )
2579
+ void CConnman::EndMessage (CDataStream& strm )
2647
2580
{
2648
- ssSend.clear ();
2649
-
2650
- LEAVE_CRITICAL_SECTION (cs_vSend);
2581
+ // Set the size
2582
+ assert (strm.size () >= CMessageHeader::HEADER_SIZE);
2583
+ unsigned int nSize = strm.size () - CMessageHeader::HEADER_SIZE;
2584
+ WriteLE32 ((uint8_t *)&strm[CMessageHeader::MESSAGE_SIZE_OFFSET], nSize);
2585
+ // Set the checksum
2586
+ uint256 hash = Hash (strm.begin () + CMessageHeader::HEADER_SIZE, strm.end ());
2587
+ memcpy ((char *)&strm[CMessageHeader::CHECKSUM_OFFSET], hash.begin (), CMessageHeader::CHECKSUM_SIZE);
2651
2588
2652
- LogPrint (" net" , " (aborted)\n " );
2653
2589
}
2654
2590
2655
- void CNode::EndMessage ( const char * pszCommand) UNLOCK_FUNCTION(cs_vSend )
2591
+ void CConnman::PushMessage (CNode* pnode, CDataStream& strm, const std::string& sCommand )
2656
2592
{
2657
- // The -*messagestest options are intentionally not documented in the help message,
2658
- // since they are only used during development to debug the networking code and are
2659
- // not intended for end-users.
2660
- if (mapArgs.count (" -dropmessagestest" ) && GetRand (GetArg (" -dropmessagestest" , 2 )) == 0 )
2661
- {
2662
- LogPrint (" net" , " dropmessages DROPPING SEND MESSAGE\n " );
2663
- AbortMessage ();
2664
- return ;
2665
- }
2666
- if (mapArgs.count (" -fuzzmessagestest" ))
2667
- Fuzz (GetArg (" -fuzzmessagestest" , 10 ));
2668
-
2669
- if (ssSend.size () == 0 )
2670
- {
2671
- LEAVE_CRITICAL_SECTION (cs_vSend);
2593
+ if (strm.empty ())
2672
2594
return ;
2673
- }
2674
- // Set the size
2675
- unsigned int nSize = ssSend.size () - CMessageHeader::HEADER_SIZE;
2676
- WriteLE32 ((uint8_t *)&ssSend[CMessageHeader::MESSAGE_SIZE_OFFSET], nSize);
2677
-
2678
- // log total amount of bytes per command
2679
- mapSendBytesPerMsgCmd[std::string (pszCommand)] += nSize + CMessageHeader::HEADER_SIZE;
2680
2595
2681
- // Set the checksum
2682
- uint256 hash = Hash (ssSend.begin () + CMessageHeader::HEADER_SIZE, ssSend.end ());
2683
- assert (ssSend.size () >= CMessageHeader::CHECKSUM_OFFSET + CMessageHeader::CHECKSUM_SIZE);
2684
- memcpy ((char *)&ssSend[CMessageHeader::CHECKSUM_OFFSET], hash.begin (), CMessageHeader::CHECKSUM_SIZE);
2596
+ unsigned int nSize = strm.size () - CMessageHeader::HEADER_SIZE;
2597
+ LogPrint (" net" , " sending %s (%d bytes) peer=%d\n " , SanitizeString (sCommand .c_str ()), nSize, pnode->id );
2685
2598
2686
- LogPrint (" net" , " (%d bytes) peer=%d\n " , nSize, id);
2687
-
2688
- std::deque<CSerializeData>::iterator it = vSendMsg.insert (vSendMsg.end (), CSerializeData ());
2689
- ssSend.GetAndClear (*it);
2690
- nSendSize += (*it).size ();
2599
+ size_t nBytesSent = 0 ;
2600
+ {
2601
+ LOCK (pnode->cs_vSend );
2602
+ if (pnode->hSocket == INVALID_SOCKET) {
2603
+ return ;
2604
+ }
2605
+ bool optimisticSend (pnode->vSendMsg .empty ());
2606
+ pnode->vSendMsg .emplace_back (strm.begin (), strm.end ());
2691
2607
2692
- // If write queue empty, attempt "optimistic write"
2693
- if (it == vSendMsg. begin ())
2694
- nOptimisticBytesWritten += SocketSendData ( this );
2608
+ // log total amount of bytes per command
2609
+ pnode-> mapSendBytesPerMsgCmd [ sCommand ] += strm. size ();
2610
+ pnode-> nSendSize += strm. size ( );
2695
2611
2696
- LEAVE_CRITICAL_SECTION (cs_vSend);
2612
+ // If write queue empty, attempt "optimistic write"
2613
+ if (optimisticSend == true )
2614
+ nBytesSent = SocketSendData (pnode);
2615
+ }
2616
+ if (nBytesSent)
2617
+ RecordBytesSent (nBytesSent);
2697
2618
}
2698
2619
2699
2620
bool CConnman::ForNode (NodeId id, std::function<bool (CNode* pnode)> func)
0 commit comments