From 4e9230ea7a846013777a9d7227f2ac0dec8678ec Mon Sep 17 00:00:00 2001 From: Anton Evangelatov Date: Fri, 1 Mar 2019 12:20:37 +0100 Subject: [PATCH] swarm: enable p2p/discovery and disable dynamic dialling (#19189) --- cmd/swarm/main.go | 4 ++-- swarm/api/inspector.go | 9 +++++++++ swarm/network/kademlia.go | 27 ++++++++++++++++++++++++++- 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/cmd/swarm/main.go b/cmd/swarm/main.go index 4d63255d7a..06d5b0bb2b 100644 --- a/cmd/swarm/main.go +++ b/cmd/swarm/main.go @@ -287,8 +287,8 @@ func bzzd(ctx *cli.Context) error { //setup the ethereum node utils.SetNodeConfig(ctx, &cfg) - //always disable discovery from p2p package - swarm discovery is done with the `hive` protocol - cfg.P2P.NoDiscovery = true + //disable dynamic dialing from p2p/discovery + cfg.P2P.NoDial = true stack, err := node.New(&cfg) if err != nil { diff --git a/swarm/api/inspector.go b/swarm/api/inspector.go index 6706b32e65..ea3c4c0494 100644 --- a/swarm/api/inspector.go +++ b/swarm/api/inspector.go @@ -18,6 +18,7 @@ package api import ( "context" + "fmt" "github.com/ethereum/go-ethereum/swarm/network" "github.com/ethereum/go-ethereum/swarm/storage" @@ -38,6 +39,14 @@ func (inspector *Inspector) Hive() string { return inspector.hive.String() } +func (inspector *Inspector) ListKnown() []string { + res := []string{} + for _, v := range inspector.hive.Kademlia.ListKnown() { + res = append(res, fmt.Sprintf("%v", v)) + } + return res +} + type HasInfo struct { Addr string `json:"address"` Has bool `json:"has"` diff --git a/swarm/network/kademlia.go b/swarm/network/kademlia.go index 723f17f1ca..304f9cd778 100644 --- a/swarm/network/kademlia.go +++ b/swarm/network/kademlia.go @@ -140,6 +140,7 @@ func (k *Kademlia) Register(peers ...*BzzAddr) error { defer k.lock.Unlock() var known, size int for _, p := range peers { + log.Trace("kademlia trying to register", "addr", p) // error if self received, peer should know better // and should be punished for this if bytes.Equal(p.Address(), k.base) { @@ -149,10 +150,22 @@ func (k *Kademlia) Register(peers ...*BzzAddr) error { k.addrs, _, found, _ = pot.Swap(k.addrs, p, Pof, func(v pot.Val) pot.Val { // if not found if v == nil { + log.Trace("registering new peer", "addr", p) // insert new offline peer into conns return newEntry(p) } - // found among known peers, do nothing + + e := v.(*entry) + + // if underlay address is different, still add + if !bytes.Equal(e.BzzAddr.UAddr, p.UAddr) { + log.Trace("underlay addr is different, so add again", "new", p, "old", e.BzzAddr) + // insert new offline peer into conns + return newEntry(p) + } + + log.Trace("found among known peers, underlay addr is same, do nothing", "new", p, "old", e.BzzAddr) + return v }) if found { @@ -417,6 +430,18 @@ func (k *Kademlia) Off(p *Peer) { } } +func (k *Kademlia) ListKnown() []*BzzAddr { + res := []*BzzAddr{} + + k.addrs.Each(func(val pot.Val) bool { + e := val.(*entry) + res = append(res, e.BzzAddr) + return true + }) + + return res +} + // EachConn is an iterator with args (base, po, f) applies f to each live peer // that has proximity order po or less as measured from the base // if base is nil, kademlia base address is used