Commit fa538ee7 authored by Felix Lange's avatar Felix Lange Committed by Péter Szilágyi

p2p/discover: improve randomness of ReadRandomNodes (#19799)

Make it select from all live nodes instead of selecting the heads of
random buckets.
parent 983f9236
...@@ -147,35 +147,18 @@ func (tab *Table) ReadRandomNodes(buf []*enode.Node) (n int) { ...@@ -147,35 +147,18 @@ func (tab *Table) ReadRandomNodes(buf []*enode.Node) (n int) {
tab.mutex.Lock() tab.mutex.Lock()
defer tab.mutex.Unlock() defer tab.mutex.Unlock()
// Find all non-empty buckets and get a fresh slice of their entries. var nodes []*enode.Node
var buckets [][]*node
for _, b := range &tab.buckets { for _, b := range &tab.buckets {
if len(b.entries) > 0 { for _, n := range b.entries {
buckets = append(buckets, b.entries) nodes = append(nodes, unwrapNode(n))
}
}
if len(buckets) == 0 {
return 0
}
// Shuffle the buckets.
for i := len(buckets) - 1; i > 0; i-- {
j := tab.rand.Intn(len(buckets))
buckets[i], buckets[j] = buckets[j], buckets[i]
}
// Move head of each bucket into buf, removing buckets that become empty.
var i, j int
for ; i < len(buf); i, j = i+1, (j+1)%len(buckets) {
b := buckets[j]
buf[i] = unwrapNode(b[0])
buckets[j] = b[1:]
if len(b) == 1 {
buckets = append(buckets[:j], buckets[j+1:]...)
} }
if len(buckets) == 0 {
break
} }
// Shuffle.
for i := 0; i < len(nodes); i++ {
j := tab.rand.Intn(len(nodes))
nodes[i], nodes[j] = nodes[j], nodes[i]
} }
return i + 1 return copy(buf, nodes)
} }
// getNode returns the node with the given ID or nil if it isn't in the table. // getNode returns the node with the given ID or nil if it isn't in the table.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment