Skip to content

Commit

Permalink
fix(network): do not create discovery if none is set
Browse files Browse the repository at this point in the history
Signed-off-by: Godefroy Ponsinet <godefroy.ponsinet@outlook.com>
  • Loading branch information
90dy committed Mar 8, 2019
1 parent 0c91499 commit e5e01e5
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 12 deletions.
Expand Up @@ -22,3 +22,4 @@ public String toString() {
return level;
}
}

1 change: 1 addition & 0 deletions core/cmd/berty/daemon.go
Expand Up @@ -160,6 +160,7 @@ func daemon(opts *daemonOptions) error {
network.New(ctx,
network.WithDefaultOptions(),
network.PrivateNetwork(swarmKey),
network.DisableMDNS(),
),
))
}
Expand Down
26 changes: 14 additions & 12 deletions core/network/driver.go
Expand Up @@ -154,19 +154,21 @@ func (net *Network) BootstrapPeer(ctx context.Context, bootstrapAddr string) err
}

func (net *Network) Discover(ctx context.Context) {
libp2p_discovery.Advertise(ctx, net.host.Discovery, "berty")
go func() {
for {
peers, err := libp2p_discovery.FindPeers(ctx, net.host.Discovery, "berty", 0)
if err != nil {
logger().Error("network discover error", zap.String("err", err.Error()))
continue
}
for _, pi := range peers {
net.Connect(ctx, pi)
if net.host.Discovery != nil {
libp2p_discovery.Advertise(ctx, net.host.Discovery, "berty")
go func() {
for {
peers, err := libp2p_discovery.FindPeers(ctx, net.host.Discovery, "berty", 0)
if err != nil {
logger().Error("network discover error", zap.String("err", err.Error()))
continue
}
for _, pi := range peers {
net.Connect(ctx, pi)
}
}
}
}()
}()
}
}

// Connect ensures there is a connection between this host and the peer with
Expand Down
13 changes: 13 additions & 0 deletions core/network/host/discovery.go
Expand Up @@ -2,6 +2,7 @@ package host

import (
"context"
"errors"
"time"

"berty.tech/core/pkg/tracing"
Expand All @@ -17,16 +18,24 @@ type BertyDiscovery struct {
}

func NewBertyDiscovery(ctx context.Context, discoveries []discovery.Discovery) discovery.Discovery {
if len(discoveries) == 0 {
return nil
}
d := &BertyDiscovery{
discoveries: discoveries,
}
return d
}

func (d *BertyDiscovery) Advertise(ctx context.Context, ns string, opts ...discovery.Option) (time.Duration, error) {

tracer := tracing.EnterFunc(ctx, ns, opts)
defer tracer.Finish()

if len(d.discoveries) == 0 {
return 0, errors.New("berty discovery: no discoveries for advertising")
}

t := time.Now()

waitChans := []chan struct{}{}
Expand Down Expand Up @@ -57,6 +66,10 @@ func (d *BertyDiscovery) FindPeers(ctx context.Context, ns string, opts ...disco
tracer := tracing.EnterFunc(ctx, ns, opts)
defer tracer.Finish()

if len(d.discoveries) == 0 {
return nil, errors.New("berty discovery: no discoveries to find peers")
}

globPiChan := make(chan pstore.PeerInfo, 1)

for i := range d.discoveries {
Expand Down
4 changes: 4 additions & 0 deletions core/network/host/routing.go
Expand Up @@ -35,6 +35,10 @@ func NewBertyRouting(ctx context.Context, h host.Host, dhtSvc bool) (*BertyRouti
dht = kaddht.NewDHTClient(ctx, h, ds)
}

if err := dht.Bootstrap(ctx); err != nil {
return nil, err
}

return &BertyRouting{IpfsRouting: dht}, nil
}

Expand Down

0 comments on commit e5e01e5

Please sign in to comment.