Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

eth: don't enforce minimum broadcast, fix broadcast test #20678

Merged
merged 1 commit into from
Feb 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 2 additions & 19 deletions eth/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ const (
// txChanSize is the size of channel listening to NewTxsEvent.
// The number is referenced from the size of tx pool.
txChanSize = 4096

// minimim number of peers to broadcast entire blocks and transactions too.
minBroadcastPeers = 4
)

var (
Expand Down Expand Up @@ -830,14 +827,7 @@ func (pm *ProtocolManager) BroadcastBlock(block *types.Block, propagate bool) {
return
}
// Send the block to a subset of our peers
transferLen := int(math.Sqrt(float64(len(peers))))
if transferLen < minBroadcastPeers {
transferLen = minBroadcastPeers
}
if transferLen > len(peers) {
transferLen = len(peers)
}
transfer := peers[:transferLen]
transfer := peers[:int(math.Sqrt(float64(len(peers))))]
for _, peer := range transfer {
peer.AsyncSendNewBlock(block, td)
}
Expand Down Expand Up @@ -866,14 +856,7 @@ func (pm *ProtocolManager) BroadcastTransactions(txs types.Transactions, propaga
peers := pm.peers.PeersWithoutTx(tx.Hash())

// Send the block to a subset of our peers
transferLen := int(math.Sqrt(float64(len(peers))))
if transferLen < minBroadcastPeers {
transferLen = minBroadcastPeers
}
if transferLen > len(peers) {
transferLen = len(peers)
}
transfer := peers[:transferLen]
transfer := peers[:int(math.Sqrt(float64(len(peers))))]
for _, peer := range transfer {
txset[peer] = append(txset[peer], tx.Hash())
}
Expand Down
43 changes: 18 additions & 25 deletions eth/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -554,12 +554,12 @@ func TestBroadcastBlock(t *testing.T) {
broadcastExpected int
}{
{1, 1},
{2, 2},
{3, 3},
{4, 4},
{5, 4},
{9, 4},
{12, 4},
{2, 1},
{3, 1},
{4, 2},
{5, 2},
{9, 3},
{12, 3},
{16, 4},
{26, 5},
{100, 10},
Expand Down Expand Up @@ -592,6 +592,7 @@ func testBroadcastBlock(t *testing.T, totalPeers, broadcastExpected int) {
for i := 0; i < totalPeers; i++ {
peer, _ := newTestPeer(fmt.Sprintf("peer %d", i), eth63, pm, true)
defer peer.close()

peers = append(peers, peer)
}
chain, _ := core.GenerateChain(gspec.Config, genesis, ethash.NewFaker(), db, 1, func(i int, gen *core.BlockGen) {})
Expand All @@ -608,31 +609,23 @@ func testBroadcastBlock(t *testing.T, totalPeers, broadcastExpected int) {
}
}(peer)
}
timeout := time.After(2 * time.Second)
var receivedCount int
outer:
var received int
for {
select {
case err = <-errCh:
break outer
case <-doneCh:
receivedCount++
if receivedCount == totalPeers {
break outer
received++

case <-time.After(100 * time.Millisecond):
if received != broadcastExpected {
t.Errorf("broadcast count mismatch: have %d, want %d", received, broadcastExpected)
}
case <-timeout:
break outer
return

case err = <-errCh:
t.Fatalf("broadcast failed: %v", err)
}
}
for _, peer := range peers {
peer.app.Close()
}
if err != nil {
t.Errorf("error matching block by peer: %v", err)
}
if receivedCount != broadcastExpected {
t.Errorf("block broadcast to %d peers, expected %d", receivedCount, broadcastExpected)
}

}

// Tests that a propagated malformed block (uncles or transactions don't match
Expand Down