swarm/network: WIP remove redundant "full" function

This commit is contained in:
lash 2018-12-12 14:51:45 +01:00
parent 75495e5030
commit 8f970c4e9a

View file

@ -599,7 +599,6 @@ func (k *Kademlia) string() string {
// used for testing only // used for testing only
type PeerPot struct { type PeerPot struct {
NNSet [][]byte NNSet [][]byte
EmptyBins []int
} }
// NewPeerPotMap creates a map of pot record of *BzzAddr with keys // NewPeerPotMap creates a map of pot record of *BzzAddr with keys
@ -648,15 +647,12 @@ func NewPeerPotMap(kadMinProxSize int, addrs [][]byte) map[string]*PeerPot {
prevPo = depth - 1 prevPo = depth - 1
return true return true
} }
for j := prevPo; j > po; j-- {
emptyBins = append(emptyBins, j)
}
prevPo = po - 1 prevPo = po - 1
return true return true
}) })
log.Trace(fmt.Sprintf("%x NNS: %s, emptyBins: %s", addrs[i][:4], LogAddrs(nns), logEmptyBins(emptyBins))) log.Trace(fmt.Sprintf("%x NNS: %s", addrs[i][:4], LogAddrs(nns)))
ppmap[common.Bytes2Hex(a)] = &PeerPot{nns, emptyBins} ppmap[common.Bytes2Hex(a)] = &PeerPot{nns}
} }
return ppmap return ppmap
} }
@ -677,44 +673,6 @@ func (k *Kademlia) saturation(n int) int {
return prev return prev
} }
// full returns true if all required bins have connected peers.
// It is used in Healthy function for testing only
func (k *Kademlia) full(emptyBins []int) (full bool) {
prev := 0
e := len(emptyBins)
ok := true
depth := depthForPot(k.conns, k.MinProxBinSize, k.base)
k.conns.EachBin(k.base, pof, 0, func(po, _ int, _ func(func(val pot.Val, i int) bool) bool) bool {
if po >= depth {
return false
}
if prev == depth+1 {
return true
}
for i := prev; i < po; i++ {
e--
if e < 0 {
ok = false
return false
}
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)))
if emptyBins[e] < i {
panic("incorrect peerpot")
}
ok = false
return false
}
}
prev = po + 1
return true
})
if !ok {
return false
}
return e == 0
}
// knowNearestNeighbours tests if all known nearest neighbours given as arguments // knowNearestNeighbours tests if all known nearest neighbours given as arguments
// are found in the addressbook // are found in the addressbook
// It is used in Healthy function for testing only // It is used in Healthy function for testing only
@ -774,7 +732,6 @@ type Health struct {
GotNN bool // whether node is connected to all its nearest neighbours GotNN bool // whether node is connected to all its nearest neighbours
CountNN int // amount of nearest neighbors connected to CountNN int // amount of nearest neighbors connected to
CulpritsNN [][]byte // which known NNs are missing CulpritsNN [][]byte // which known NNs are missing
Full bool // whether node has a peer in each kademlia bin (where there is such a peer)
Hive string Hive string
} }
@ -786,15 +743,6 @@ func (k *Kademlia) Healthy(pp *PeerPot) *Health {
defer k.lock.RUnlock() defer k.lock.RUnlock()
gotnn, countnn, culpritsnn := k.gotNearestNeighbours(pp.NNSet) gotnn, countnn, culpritsnn := k.gotNearestNeighbours(pp.NNSet)
knownn := k.knowNearestNeighbours(pp.NNSet) knownn := k.knowNearestNeighbours(pp.NNSet)
full := k.full(pp.EmptyBins)
log.Trace(fmt.Sprintf("%08x: healthy: knowNNs: %v, gotNNs: %v, full: %v\n", k.BaseAddr()[:4], knownn, gotnn, full)) log.Trace(fmt.Sprintf("%08x: healthy: knowNNs: %v, gotNNs: %v, full: %v\n", k.BaseAddr()[:4], knownn, gotnn, full))
return &Health{knownn, gotnn, countnn, culpritsnn, full, k.string()} return &Health{knownn, gotnn, countnn, culpritsnn, k.string()}
}
func logEmptyBins(ebs []int) string {
var ebss []string
for _, eb := range ebs {
ebss = append(ebss, fmt.Sprintf("%d", eb))
}
return strings.Join(ebss, ", ")
} }