mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 18:32:23 +00:00
p2p/discover: shuffle in appendLiveNodes
This commit is contained in:
parent
9ec2553844
commit
de7e1c7d31
2 changed files with 14 additions and 2 deletions
|
|
@ -100,6 +100,7 @@ type ReadPacket struct {
|
||||||
type randomSource interface {
|
type randomSource interface {
|
||||||
Intn(int) int
|
Intn(int) int
|
||||||
Int63n(int64) int64
|
Int63n(int64) int64
|
||||||
|
Shuffle(int, func(int, int))
|
||||||
}
|
}
|
||||||
|
|
||||||
// reseedingRandom is a random number generator that tracks when it was last re-seeded.
|
// reseedingRandom is a random number generator that tracks when it was last re-seeded.
|
||||||
|
|
@ -130,3 +131,9 @@ func (r *reseedingRandom) Int63n(n int64) int64 {
|
||||||
defer r.mu.Unlock()
|
defer r.mu.Unlock()
|
||||||
return r.cur.Int63n(n)
|
return r.cur.Int63n(n)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *reseedingRandom) Shuffle(n int, swap func(i, j int)) {
|
||||||
|
r.mu.Lock()
|
||||||
|
defer r.mu.Unlock()
|
||||||
|
r.cur.Shuffle(n, swap)
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -79,7 +79,7 @@ type Table struct {
|
||||||
refreshReq chan chan struct{}
|
refreshReq chan chan struct{}
|
||||||
revalResponseCh chan revalidationResponse
|
revalResponseCh chan revalidationResponse
|
||||||
addNodeCh chan addNodeOp
|
addNodeCh chan addNodeOp
|
||||||
addNodeHandled chan 1bool
|
addNodeHandled chan bool
|
||||||
trackRequestCh chan trackRequestOp
|
trackRequestCh chan trackRequestOp
|
||||||
initDone chan struct{}
|
initDone chan struct{}
|
||||||
closeReq chan struct{}
|
closeReq chan struct{}
|
||||||
|
|
@ -279,12 +279,17 @@ func (tab *Table) appendLiveNodes(dist uint, result []*enode.Node) []*enode.Node
|
||||||
}
|
}
|
||||||
|
|
||||||
tab.mutex.Lock()
|
tab.mutex.Lock()
|
||||||
defer tab.mutex.Unlock()
|
|
||||||
for _, n := range tab.bucketAtDistance(int(dist)).entries {
|
for _, n := range tab.bucketAtDistance(int(dist)).entries {
|
||||||
if n.isValidatedLive {
|
if n.isValidatedLive {
|
||||||
result = append(result, n.Node)
|
result = append(result, n.Node)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
tab.mutex.Unlock()
|
||||||
|
|
||||||
|
// Shuffle result to avoid always returning same nodes in FINDNODE/v5.
|
||||||
|
tab.rand.Shuffle(len(result), func(i, j int) {
|
||||||
|
result[i], result[j] = result[j], result[i]
|
||||||
|
})
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue