mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-18 01:43:47 +00:00
swarm/network: Kademlia updates to full and SuggestPeer methods
Thanks to Viktor Tron.
This commit is contained in:
parent
c9283b79f9
commit
5d6e4bba63
1 changed files with 25 additions and 4 deletions
|
|
@ -258,16 +258,25 @@ func (k *Kademlia) SuggestPeer() (a OverlayAddr, o int, want bool) {
|
||||||
// try to select a candidate peer
|
// try to select a candidate peer
|
||||||
// find the first callable peer
|
// find the first callable peer
|
||||||
nxt := bpo[0]
|
nxt := bpo[0]
|
||||||
|
var i int
|
||||||
k.addrs.EachBin(k.base, pof, nxt, func(po, _ int, f func(func(pot.Val, int) bool) bool) bool {
|
k.addrs.EachBin(k.base, pof, nxt, func(po, _ int, f func(func(pot.Val, int) bool) bool) bool {
|
||||||
// for each bin (up until depth) we find callable candidate peers
|
// for each bin (up until depth) we find callable candidate peers
|
||||||
if po >= depth {
|
if po >= depth || po != nxt {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
f(func(val pot.Val, _ int) bool {
|
i++
|
||||||
|
ok := f(func(val pot.Val, _ int) bool {
|
||||||
a = k.callable(val)
|
a = k.callable(val)
|
||||||
return a == nil
|
return a == nil
|
||||||
})
|
})
|
||||||
|
if !ok {
|
||||||
return false
|
return false
|
||||||
|
}
|
||||||
|
if i >= len(bpo) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
nxt = bpo[i]
|
||||||
|
return true
|
||||||
})
|
})
|
||||||
// found a candidate
|
// found a candidate
|
||||||
if a != nil {
|
if a != nil {
|
||||||
|
|
@ -677,9 +686,18 @@ func (k *Kademlia) saturation(n int) int {
|
||||||
func (k *Kademlia) full(emptyBins []int) (full bool) {
|
func (k *Kademlia) full(emptyBins []int) (full bool) {
|
||||||
prev := 0
|
prev := 0
|
||||||
e := len(emptyBins)
|
e := len(emptyBins)
|
||||||
|
ok := true
|
||||||
|
depth := k.neighbourhoodDepth()
|
||||||
k.conns.EachBin(k.base, pof, 0, func(po, _ int, _ func(func(val pot.Val, i int) bool) bool) bool {
|
k.conns.EachBin(k.base, pof, 0, func(po, _ int, _ func(func(val pot.Val, i int) bool) bool) bool {
|
||||||
for i := prev; e > 0 && i < po; i++ {
|
for i := prev; i < po; i++ {
|
||||||
|
if prev == depth+1 {
|
||||||
|
return true
|
||||||
|
}
|
||||||
e--
|
e--
|
||||||
|
if e < 0 {
|
||||||
|
ok = false
|
||||||
|
return false
|
||||||
|
}
|
||||||
if emptyBins[e] != i {
|
if emptyBins[e] != i {
|
||||||
log.Trace(fmt.Sprintf("%08x po: %d, i: %d, e: %d, emptybins: %v", k.BaseAddr()[:4], po, i, e, logEmptyBins(emptyBins)))
|
log.Trace(fmt.Sprintf("%08x po: %d, i: %d, e: %d, emptybins: %v", k.BaseAddr()[:4], po, i, e, logEmptyBins(emptyBins)))
|
||||||
if emptyBins[e] < i {
|
if emptyBins[e] < i {
|
||||||
|
|
@ -691,6 +709,9 @@ func (k *Kademlia) full(emptyBins []int) (full bool) {
|
||||||
prev = po + 1
|
prev = po + 1
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
|
if !ok {
|
||||||
|
return false
|
||||||
|
}
|
||||||
return e == 0
|
return e == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue