@@ -456,36 +456,32 @@ 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
+ txset = make (map [* ethPeer ][]common.Hash ) // Set peer->hash to transfer directly
466
+ annos = make (map [* ethPeer ][]common.Hash ) // Set peer->hash to announce
465
467
)
466
468
// 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
469
for _ , tx := range txs {
485
470
peers := h .peers .peersWithoutTransaction (tx .Hash ())
486
- for _ , peer := range peers {
471
+ // Send the tx unconditionally to a subset of our peers
472
+ numDirect := int (math .Sqrt (float64 (len (peers ))))
473
+ for _ , peer := range peers [:numDirect ] {
474
+ txset [peer ] = append (txset [peer ], tx .Hash ())
475
+ }
476
+ // For the remaining peers, send announcement only
477
+ for _ , peer := range peers [numDirect :] {
487
478
annos [peer ] = append (annos [peer ], tx .Hash ())
488
479
}
480
+ log .Trace ("Broadcast transaction" , "hash" , tx .Hash (), "direct" , numDirect ,
481
+ "announce" , len (peers )- numDirect )
482
+ }
483
+ for peer , hashes := range txset {
484
+ peer .AsyncSendTransactions (hashes )
489
485
}
490
486
for peer , hashes := range annos {
491
487
if peer .Version () >= eth .ETH65 {
@@ -515,8 +511,7 @@ func (h *handler) txBroadcastLoop() {
515
511
for {
516
512
select {
517
513
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
514
+ h .BroadcastTransactions (event .Txs )
520
515
521
516
case <- h .txsSub .Err ():
522
517
return
0 commit comments