Skip to content

Commit

Permalink
swarm: enable p2p/discovery and disable dynamic dialling (#19189)
Browse files Browse the repository at this point in the history
  • Loading branch information
nonsense authored Mar 1, 2019
1 parent 94eca08 commit 4e9230e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
4 changes: 2 additions & 2 deletions cmd/swarm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 9 additions & 0 deletions swarm/api/inspector.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package api

import (
"context"
"fmt"

"github.com/ethereum/go-ethereum/swarm/network"
"github.com/ethereum/go-ethereum/swarm/storage"
Expand All @@ -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"`
Expand Down
27 changes: 26 additions & 1 deletion swarm/network/kademlia.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 4e9230e

Please sign in to comment.