From e6653809a6e65887416906a05c77629e28691e79 Mon Sep 17 00:00:00 2001 From: lash Date: Mon, 17 Dec 2018 17:18:19 +0100 Subject: [PATCH] swarm/network: Remove commented code and useless kad Pof member --- swarm/network/discovery.go | 2 +- swarm/network/hive.go | 4 +- swarm/network/kademlia.go | 56 +++++++++---------- .../simulations/discovery/discovery_test.go | 7 --- swarm/network/stream/common_test.go | 2 +- swarm/pss/pss.go | 2 +- 6 files changed, 33 insertions(+), 40 deletions(-) diff --git a/swarm/network/discovery.go b/swarm/network/discovery.go index 21703e70f1..c6f5224301 100644 --- a/swarm/network/discovery.go +++ b/swarm/network/discovery.go @@ -161,7 +161,7 @@ func (d *Peer) handleSubPeersMsg(msg *subPeersMsg) error { d.setDepth(msg.Depth) var peers []*BzzAddr d.kad.EachConn(d.Over(), 255, func(p *Peer, po int, isproxbin bool) bool { - if pob, _ := pof(d, d.kad.BaseAddr(), 0); pob > po { + if pob, _ := Pof(d, d.kad.BaseAddr(), 0); pob > po { return false } if !d.seen(p.BzzAddr) { diff --git a/swarm/network/hive.go b/swarm/network/hive.go index 6c6ef7f5e8..155a19c339 100644 --- a/swarm/network/hive.go +++ b/swarm/network/hive.go @@ -244,7 +244,9 @@ func (h *Hive) savePeers() error { return nil } -// Healthy is an api proxy to the underlying kademlia healthy function +// Healthy works as an API proxy to the corresponding kademlia.Healthy function +// It evaluates the healthiness based on the addresses passed as argument +// in relation to the base address of the hive instance the method is called on func (h *Hive) Healthy(addrs [][]byte) *Health { k := NewKademlia(h.BaseAddr(), NewKadParams()) for _, a := range addrs { diff --git a/swarm/network/kademlia.go b/swarm/network/kademlia.go index 2a3ddc4968..c941808314 100644 --- a/swarm/network/kademlia.go +++ b/swarm/network/kademlia.go @@ -49,7 +49,7 @@ a guaranteed constant maximum limit on the number of hops needed to reach one node from the other. */ -var pof = pot.DefaultPof(256) +var Pof = pot.DefaultPof(256) // KadParams holds the config params for Kademlia type KadParams struct { @@ -81,15 +81,14 @@ func NewKadParams() *KadParams { // Kademlia is a table of live peers and a db of known peers (node records) type Kademlia struct { lock sync.RWMutex - *KadParams // Kademlia configuration parameters - base []byte // immutable baseaddress of the table - addrs *pot.Pot // pots container for known peer addresses - conns *pot.Pot // pots container for live peer connections - depth uint8 // stores the last current depth of saturation - nDepth int // stores the last neighbourhood depth - nDepthC chan int // returned by DepthC function to signal neighbourhood depth change - addrCountC chan int // returned by AddrCountC function to signal peer count change - Pof func(pot.Val, pot.Val, int) (int, bool) `json:"-"` // function for calculating kademlia routing distance between two addresses + *KadParams // Kademlia configuration parameters + base []byte // immutable baseaddress of the table + addrs *pot.Pot // pots container for known peer addresses + conns *pot.Pot // pots container for live peer connections + depth uint8 // stores the last current depth of saturation + nDepth int // stores the last neighbourhood depth + nDepthC chan int // returned by DepthC function to signal neighbourhood depth change + addrCountC chan int // returned by AddrCountC function to signal peer count change } // NewKademlia creates a Kademlia table for base address addr @@ -104,7 +103,6 @@ func NewKademlia(addr []byte, params *KadParams) *Kademlia { KadParams: params, addrs: pot.NewPot(nil, 0), conns: pot.NewPot(nil, 0), - Pof: pof, } } @@ -147,7 +145,7 @@ func (k *Kademlia) Register(peers ...*BzzAddr) error { return fmt.Errorf("add peers: %x is self", k.base) } var found bool - k.addrs, _, found, _ = pot.Swap(k.addrs, p, pof, func(v pot.Val) pot.Val { + k.addrs, _, found, _ = pot.Swap(k.addrs, p, Pof, func(v pot.Val) pot.Val { // if not found if v == nil { // insert new offline peer into conns @@ -181,7 +179,7 @@ func (k *Kademlia) SuggestPeer() (a *BzzAddr, o int, want bool) { // if there is a callable neighbour within the current proxBin, connect // this makes sure nearest neighbour set is fully connected var ppo int - k.addrs.EachNeighbour(k.base, pof, func(val pot.Val, po int) bool { + k.addrs.EachNeighbour(k.base, Pof, func(val pot.Val, po int) bool { if po < depth { return false } @@ -200,7 +198,7 @@ func (k *Kademlia) SuggestPeer() (a *BzzAddr, o int, want bool) { var bpo []int prev := -1 - k.conns.EachBin(k.base, pof, 0, func(po, size int, f func(func(val pot.Val, i int) bool) bool) bool { + k.conns.EachBin(k.base, Pof, 0, func(po, size int, f func(func(val pot.Val, i int) bool) bool) bool { prev++ for ; prev < po; prev++ { bpo = append(bpo, prev) @@ -221,7 +219,7 @@ func (k *Kademlia) SuggestPeer() (a *BzzAddr, o int, want bool) { // try to select a candidate peer // find the first callable peer nxt := bpo[0] - 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 if po >= depth { return false @@ -253,7 +251,7 @@ func (k *Kademlia) On(p *Peer) (uint8, bool) { k.lock.Lock() defer k.lock.Unlock() var ins bool - k.conns, _, _, _ = pot.Swap(k.conns, p, pof, func(v pot.Val) pot.Val { + k.conns, _, _, _ = pot.Swap(k.conns, p, Pof, func(v pot.Val) pot.Val { // if not found live if v == nil { ins = true @@ -267,7 +265,7 @@ func (k *Kademlia) On(p *Peer) (uint8, bool) { a := newEntry(p.BzzAddr) a.conn = p // insert new online peer into addrs - k.addrs, _, _, _ = pot.Swap(k.addrs, p, pof, func(v pot.Val) pot.Val { + k.addrs, _, _, _ = pot.Swap(k.addrs, p, Pof, func(v pot.Val) pot.Val { return a }) // send new address count value only if the peer is inserted @@ -333,7 +331,7 @@ func (k *Kademlia) Off(p *Peer) { defer k.lock.Unlock() var del bool if !p.BzzPeer.LightNode { - k.addrs, _, _, _ = pot.Swap(k.addrs, p, pof, func(v pot.Val) pot.Val { + k.addrs, _, _, _ = pot.Swap(k.addrs, p, Pof, func(v pot.Val) pot.Val { // v cannot be nil, must check otherwise we overwrite entry if v == nil { panic(fmt.Sprintf("connected peer not found %v", p)) @@ -346,7 +344,7 @@ func (k *Kademlia) Off(p *Peer) { } if del { - k.conns, _, _, _ = pot.Swap(k.conns, p, pof, func(_ pot.Val) pot.Val { + k.conns, _, _, _ = pot.Swap(k.conns, p, Pof, func(_ pot.Val) pot.Val { // v cannot be nil, but no need to check return nil }) @@ -366,7 +364,7 @@ func (k *Kademlia) EachBin(base []byte, pof pot.Pof, o int, eachBinFunc func(con var endPo int kadDepth := depthForPot(k.conns, k.MinProxBinSize, k.base) - k.conns.EachBin(base, pof, o, func(po, size int, f func(func(val pot.Val, i int) bool) bool) bool { + k.conns.EachBin(base, Pof, o, func(po, size int, f func(func(val pot.Val, i int) bool) bool) bool { if startPo > 0 && endPo != k.MaxProxDisplay { startPo = endPo + 1 } @@ -399,7 +397,7 @@ func (k *Kademlia) eachConn(base []byte, o int, f func(*Peer, int, bool) bool) { base = k.base } depth := depthForPot(k.conns, k.MinProxBinSize, k.base) - k.conns.EachNeighbour(base, pof, func(val pot.Val, po int) bool { + k.conns.EachNeighbour(base, Pof, func(val pot.Val, po int) bool { if po > o { return true } @@ -421,7 +419,7 @@ func (k *Kademlia) eachAddr(base []byte, o int, f func(*BzzAddr, int, bool) bool base = k.base } depth := depthForPot(k.conns, k.MinProxBinSize, k.base) - k.addrs.EachNeighbour(base, pof, func(val pot.Val, po int) bool { + k.addrs.EachNeighbour(base, Pof, func(val pot.Val, po int) bool { if po > o { return true } @@ -464,9 +462,9 @@ func depthForPot(p *pot.Pot, minProxBinSize int, pivotAddr []byte) (depth int) { return true } - p.EachNeighbour(pivotAddr, pof, f) + p.EachNeighbour(pivotAddr, Pof, f) - p.EachBin(pivotAddr, pof, 0, func(po int, _ int, _ func(func(pot.Val, int) bool) bool) bool { + p.EachBin(pivotAddr, Pof, 0, func(po int, _ int, _ func(func(pot.Val, int) bool) bool) bool { if po == depth { if maxDepth == depth { return false @@ -538,7 +536,7 @@ func (k *Kademlia) string() string { depth := depthForPot(k.conns, k.MinProxBinSize, k.base) rest := k.conns.Size() - k.conns.EachBin(k.base, pof, 0, func(po, size int, f func(func(val pot.Val, i int) bool) bool) bool { + k.conns.EachBin(k.base, Pof, 0, func(po, size int, f func(func(val pot.Val, i int) bool) bool) bool { var rowlen int if po >= k.MaxProxDisplay { po = k.MaxProxDisplay - 1 @@ -557,7 +555,7 @@ func (k *Kademlia) string() string { return true }) - k.addrs.EachBin(k.base, pof, 0, func(po, size int, f func(func(val pot.Val, i int) bool) bool) bool { + k.addrs.EachBin(k.base, Pof, 0, func(po, size int, f func(func(val pot.Val, i int) bool) bool) bool { var rowlen int if po >= k.MaxProxDisplay { po = k.MaxProxDisplay - 1 @@ -614,7 +612,7 @@ func NewPeerPotMap(kads []*Kademlia) map[string]*PeerPot { np := pot.NewPot(nil, 0) for _, k := range kads { - np, _, _ = pot.Add(np, k.base, pof) + np, _, _ = pot.Add(np, k.base, Pof) } ppmap := make(map[string]*PeerPot) @@ -630,7 +628,7 @@ func NewPeerPotMap(kads []*Kademlia) map[string]*PeerPot { var nns [][]byte // iterate through the neighbours, going from the deepest to the shallowest - np.EachNeighbour(a, pof, func(val pot.Val, po int) bool { + np.EachNeighbour(a, Pof, func(val pot.Val, po int) bool { addr := val.([]byte) // po == 256 means that addr is the pivot address(self) // we do not include self in the map @@ -661,7 +659,7 @@ func NewPeerPotMap(kads []*Kademlia) map[string]*PeerPot { // TODO move to separate testing tools file func (k *Kademlia) saturation() int { prev := -1 - k.addrs.EachBin(k.base, pof, 0, func(po, size int, f func(func(val pot.Val, i int) bool) bool) bool { + k.addrs.EachBin(k.base, Pof, 0, func(po, size int, f func(func(val pot.Val, i int) bool) bool) bool { prev++ return prev == po && size >= k.MinProxBinSize }) diff --git a/swarm/network/simulations/discovery/discovery_test.go b/swarm/network/simulations/discovery/discovery_test.go index 0086178eb7..ec26a1598d 100644 --- a/swarm/network/simulations/discovery/discovery_test.go +++ b/swarm/network/simulations/discovery/discovery_test.go @@ -269,7 +269,6 @@ func discoverySimulation(nodes, conns int, adapter adapters.NodeAdapter) (*simul for _, a := range addrs { kads = append(kads, network.NewKademlia(a, network.NewKadParams())) } - //ppmap := network.NewPeerPotMap(kads) check := func(ctx context.Context, id enode.ID) (bool, error) { select { case <-ctx.Done(): @@ -287,8 +286,6 @@ func discoverySimulation(nodes, conns int, adapter adapters.NodeAdapter) (*simul } healthy := &network.Health{} - //pp := ppmap[id.String()] - //if err := client.Call(&healthy, "hive_healthy", pp.BaseAddr(), pp); err != nil { if err := client.Call(&healthy, "hive_healthy", addrs); err != nil { return false, fmt.Errorf("error getting node health: %s", err) } @@ -412,8 +409,6 @@ func discoveryPersistenceSimulation(nodes, conns int, adapter adapters.NodeAdapt healthy := &network.Health{} addr := id.String() log.Error("before hive healthy call") - //pp := ppmap[addr] - // if err := client.Call(&healthy, "hive_healthy", pp.BaseAddr(), pp); err != nil { if err := client.Call(&healthy, "hive_healthy", addrs); err != nil { return fmt.Errorf("error getting node health: %s", err) } @@ -493,8 +488,6 @@ func discoveryPersistenceSimulation(nodes, conns int, adapter adapters.NodeAdapt return false, fmt.Errorf("error getting node client: %s", err) } healthy := &network.Health{} - //pp := ppmap[id.String()] - //if err := client.Call(&healthy, "hive_healthy", pp.BaseAddr(), pp); err != nil { if err := client.Call(&healthy, "hive_healthy", addrs); err != nil { return false, fmt.Errorf("error getting node health: %s", err) } diff --git a/swarm/network/stream/common_test.go b/swarm/network/stream/common_test.go index e0a7f7e120..919a4b847f 100644 --- a/swarm/network/stream/common_test.go +++ b/swarm/network/stream/common_test.go @@ -57,7 +57,7 @@ var ( bucketKeyRegistry = simulation.BucketKey("registry") chunkSize = 4096 - pof = pot.DefaultPof(256) + pof = network.Pof ) func init() { diff --git a/swarm/pss/pss.go b/swarm/pss/pss.go index 1bc28890f9..4a76d427dc 100644 --- a/swarm/pss/pss.go +++ b/swarm/pss/pss.go @@ -513,7 +513,7 @@ func (p *Pss) isSelfPossibleRecipient(msg *PssMsg, prox bool) bool { } depth := p.Kademlia.NeighbourhoodDepth() - po, _ := p.Kademlia.Pof(p.Kademlia.BaseAddr(), msg.To, 0) + po, _ := network.Pof(p.Kademlia.BaseAddr(), msg.To, 0) log.Trace("selfpossible", "po", po, "depth", depth) return depth <= po