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

This commit is contained in:
Péter Szilágyi 2020-02-17 12:01:03 +02:00
parent 37531b1884
commit 36a1e0b67d
No known key found for this signature in database
GPG Key ID: E9AE538CEDF8293D
2 changed files with 20 additions and 44 deletions

View File

@ -50,9 +50,6 @@ const (
// txChanSize is the size of channel listening to NewTxsEvent. // txChanSize is the size of channel listening to NewTxsEvent.
// The number is referenced from the size of tx pool. // The number is referenced from the size of tx pool.
txChanSize = 4096 txChanSize = 4096
// minimim number of peers to broadcast entire blocks and transactions too.
minBroadcastPeers = 4
) )
var ( var (
@ -830,14 +827,7 @@ func (pm *ProtocolManager) BroadcastBlock(block *types.Block, propagate bool) {
return return
} }
// Send the block to a subset of our peers // Send the block to a subset of our peers
transferLen := int(math.Sqrt(float64(len(peers)))) transfer := peers[:int(math.Sqrt(float64(len(peers))))]
if transferLen < minBroadcastPeers {
transferLen = minBroadcastPeers
}
if transferLen > len(peers) {
transferLen = len(peers)
}
transfer := peers[:transferLen]
for _, peer := range transfer { for _, peer := range transfer {
peer.AsyncSendNewBlock(block, td) peer.AsyncSendNewBlock(block, td)
} }
@ -866,14 +856,7 @@ func (pm *ProtocolManager) BroadcastTransactions(txs types.Transactions, propaga
peers := pm.peers.PeersWithoutTx(tx.Hash()) peers := pm.peers.PeersWithoutTx(tx.Hash())
// Send the block to a subset of our peers // Send the block to a subset of our peers
transferLen := int(math.Sqrt(float64(len(peers)))) transfer := peers[:int(math.Sqrt(float64(len(peers))))]
if transferLen < minBroadcastPeers {
transferLen = minBroadcastPeers
}
if transferLen > len(peers) {
transferLen = len(peers)
}
transfer := peers[:transferLen]
for _, peer := range transfer { for _, peer := range transfer {
txset[peer] = append(txset[peer], tx.Hash()) txset[peer] = append(txset[peer], tx.Hash())
} }

View File

@ -554,12 +554,12 @@ func TestBroadcastBlock(t *testing.T) {
broadcastExpected int broadcastExpected int
}{ }{
{1, 1}, {1, 1},
{2, 2}, {2, 1},
{3, 3}, {3, 1},
{4, 4}, {4, 2},
{5, 4}, {5, 2},
{9, 4}, {9, 3},
{12, 4}, {12, 3},
{16, 4}, {16, 4},
{26, 5}, {26, 5},
{100, 10}, {100, 10},
@ -592,6 +592,7 @@ func testBroadcastBlock(t *testing.T, totalPeers, broadcastExpected int) {
for i := 0; i < totalPeers; i++ { for i := 0; i < totalPeers; i++ {
peer, _ := newTestPeer(fmt.Sprintf("peer %d", i), eth63, pm, true) peer, _ := newTestPeer(fmt.Sprintf("peer %d", i), eth63, pm, true)
defer peer.close() defer peer.close()
peers = append(peers, peer) peers = append(peers, peer)
} }
chain, _ := core.GenerateChain(gspec.Config, genesis, ethash.NewFaker(), db, 1, func(i int, gen *core.BlockGen) {}) chain, _ := core.GenerateChain(gspec.Config, genesis, ethash.NewFaker(), db, 1, func(i int, gen *core.BlockGen) {})
@ -608,31 +609,23 @@ func testBroadcastBlock(t *testing.T, totalPeers, broadcastExpected int) {
} }
}(peer) }(peer)
} }
timeout := time.After(2 * time.Second) var received int
var receivedCount int
outer:
for { for {
select { select {
case err = <-errCh:
break outer
case <-doneCh: case <-doneCh:
receivedCount++ received++
if receivedCount == totalPeers {
break outer case <-time.After(100 * time.Millisecond):
if received != broadcastExpected {
t.Errorf("broadcast count mismatch: have %d, want %d", received, broadcastExpected)
} }
case <-timeout: return
break outer
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 // Tests that a propagated malformed block (uncles or transactions don't match