From 99df803e899b9f868dcb12303a452b88e1a7a0bb Mon Sep 17 00:00:00 2001 From: Fabio Barone Date: Tue, 5 Feb 2019 21:33:39 -0500 Subject: [PATCH] swarm/network: re-added saturation func in Kademlia as it is used elsewhere --- swarm/network/kademlia.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/swarm/network/kademlia.go b/swarm/network/kademlia.go index 1e908614c3..5fd2babea2 100644 --- a/swarm/network/kademlia.go +++ b/swarm/network/kademlia.go @@ -683,6 +683,24 @@ func NewPeerPotMap(neighbourhoodSize int, addrs [][]byte) map[string]*PeerPot { return ppmap } +// saturation returns the smallest po value in which the node has less than MinBinSize peers +// if the iterator reaches neighbourhood radius, then the last bin + 1 is returned +func (k *Kademlia) saturation() int { + prev := -1 + radius := neighbourhoodRadiusForPot(k.conns, k.NeighbourhoodSize, k.base) + k.conns.EachBin(k.base, Pof, 0, func(po, size int, f func(func(val pot.Val) bool) bool) bool { + prev++ + if po >= radius { + return false + } + return prev == po && size >= k.MinBinSize + }) + if prev < 0 { + return 0 + } + return prev +} + // getUnsaturatedBins returns an array of ints; each item in that array corresponds // to the bin which is unsaturated (number of connections < k.MinBinSize). // The bin is considered unsaturated only if there are actual peers in that PeerPot's bin (peersPerBin)