@@ -456,44 +456,51 @@ func (h *handler) BroadcastBlock(block *types.Block, propagate bool) {
456
456
}
457
457
}
458
458
459
- // BroadcastTransactions will propagate a batch of transactions to all peers which are not known to
459
+ // BroadcastTransactions will propagate a batch of transactions
460
+ // - To a square root of all peers
461
+ // - And, separately, as announcements to all peers which are not known to
460
462
// already have the given transaction.
461
- func (h * handler ) BroadcastTransactions (txs types.Transactions , propagate bool ) {
463
+ func (h * handler ) BroadcastTransactions (txs types.Transactions ) {
462
464
var (
463
- txset = make (map [* ethPeer ][]common.Hash )
464
- annos = make (map [* ethPeer ][]common.Hash )
465
+ annoCount int // Count of announcements made
466
+ annoPeers int
467
+ directCount int // Count of the txs sent directly to peers
468
+ directPeers int // Count of the peers that were sent transactions directly
469
+
470
+ txset = make (map [* ethPeer ][]common.Hash ) // Set peer->hash to transfer directly
471
+ annos = make (map [* ethPeer ][]common.Hash ) // Set peer->hash to announce
472
+
465
473
)
466
474
// Broadcast transactions to a batch of peers not knowing about it
467
- if propagate {
468
- for _ , tx := range txs {
469
- peers := h .peers .peersWithoutTransaction (tx .Hash ())
470
-
471
- // Send the block to a subset of our peers
472
- transfer := peers [:int (math .Sqrt (float64 (len (peers ))))]
473
- for _ , peer := range transfer {
474
- txset [peer ] = append (txset [peer ], tx .Hash ())
475
- }
476
- log .Trace ("Broadcast transaction" , "hash" , tx .Hash (), "recipients" , len (transfer ))
477
- }
478
- for peer , hashes := range txset {
479
- peer .AsyncSendTransactions (hashes )
480
- }
481
- return
482
- }
483
- // Otherwise only broadcast the announcement to peers
484
475
for _ , tx := range txs {
485
476
peers := h .peers .peersWithoutTransaction (tx .Hash ())
486
- for _ , peer := range peers {
477
+ // Send the tx unconditionally to a subset of our peers
478
+ numDirect := int (math .Sqrt (float64 (len (peers ))))
479
+ for _ , peer := range peers [:numDirect ] {
480
+ txset [peer ] = append (txset [peer ], tx .Hash ())
481
+ }
482
+ // For the remaining peers, send announcement only
483
+ for _ , peer := range peers [numDirect :] {
487
484
annos [peer ] = append (annos [peer ], tx .Hash ())
488
485
}
489
486
}
487
+ for peer , hashes := range txset {
488
+ directPeers ++
489
+ directCount += len (hashes )
490
+ peer .AsyncSendTransactions (hashes )
491
+ }
490
492
for peer , hashes := range annos {
493
+ annoPeers ++
494
+ annoCount += len (hashes )
491
495
if peer .Version () >= eth .ETH65 {
492
496
peer .AsyncSendPooledTransactionHashes (hashes )
493
497
} else {
494
498
peer .AsyncSendTransactions (hashes )
495
499
}
496
500
}
501
+ log .Debug ("Transaction broadcast" , "txs" , len (txs ),
502
+ "announce packs" , annoPeers , "announced hashes" , annoCount ,
503
+ "tx packs" , directPeers , "broadcast txs" , directCount )
497
504
}
498
505
499
506
// minedBroadcastLoop sends mined blocks to connected peers.
@@ -511,13 +518,10 @@ func (h *handler) minedBroadcastLoop() {
511
518
// txBroadcastLoop announces new transactions to connected peers.
512
519
func (h * handler ) txBroadcastLoop () {
513
520
defer h .wg .Done ()
514
-
515
521
for {
516
522
select {
517
523
case event := <- h .txsCh :
518
- h .BroadcastTransactions (event .Txs , true ) // First propagate transactions to peers
519
- h .BroadcastTransactions (event .Txs , false ) // Only then announce to the rest
520
-
524
+ h .BroadcastTransactions (event .Txs )
521
525
case <- h .txsSub .Err ():
522
526
return
523
527
}
0 commit comments