Skip to content

Commit

Permalink
fix(network): bootstrap dht and peers
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 ff75a8d commit 92422cb
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
7 changes: 7 additions & 0 deletions core/network/host/discovery.go
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"time"

"berty.tech/core/pkg/tracing"
discovery "github.com/libp2p/go-libp2p-discovery"
pstore "github.com/libp2p/go-libp2p-peerstore"
)
Expand All @@ -22,6 +23,9 @@ func NewBertyDiscovery(ctx context.Context, discoveries []discovery.Discovery) 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()

t := time.Now()

waitChans := []chan struct{}{}
Expand Down Expand Up @@ -51,6 +55,9 @@ func (d *BertyDiscovery) Advertise(ctx context.Context, ns string, opts ...disco
}

func (d *BertyDiscovery) FindPeers(ctx context.Context, ns string, opts ...discovery.Option) (<-chan pstore.PeerInfo, error) {
tracer := tracing.EnterFunc(ctx, ns, opts)
defer tracer.Finish()

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

for i := range d.discoveries {
Expand Down
11 changes: 5 additions & 6 deletions core/network/host/routing.go
Expand Up @@ -3,12 +3,12 @@ package host
import (
"context"

"berty.tech/core/pkg/tracing"
datastore "github.com/ipfs/go-datastore"
syncdatastore "github.com/ipfs/go-datastore/sync"
host "github.com/libp2p/go-libp2p-host"
kaddht "github.com/libp2p/go-libp2p-kad-dht"
routing "github.com/libp2p/go-libp2p-routing"
"go.uber.org/zap"
)

var _ routing.IpfsRouting = (*BertyRouting)(nil)
Expand All @@ -18,6 +18,8 @@ type BertyRouting struct {
}

func NewBertyRouting(ctx context.Context, h host.Host, dhtSvc bool) (*BertyRouting, error) {
tracer := tracing.EnterFunc(ctx, h, dhtSvc)
defer tracer.Finish()
// TODO: use go-libp2p-routing-helpers
ds := syncdatastore.MutexWrap(datastore.NewMapDatastore())
var dht *kaddht.IpfsDHT
Expand All @@ -28,12 +30,9 @@ func NewBertyRouting(ctx context.Context, h host.Host, dhtSvc bool) (*BertyRouti
dht = kaddht.NewDHTClient(ctx, h, ds)
}

err := dht.BootstrapWithConfig(ctx, kaddht.DefaultBootstrapConfig)
if err != nil {
if closeErr := h.Close(); closeErr != nil {
logger().Error("failed to close host", zap.Error(closeErr))
}
if err := dht.Bootstrap(ctx); err != nil {
return nil, err
}

return &BertyRouting{dht}, nil
}
4 changes: 4 additions & 0 deletions core/network/network.go
Expand Up @@ -57,6 +57,10 @@ func New(ctx context.Context, opts ...config.Option) (*Network, error) {
net.host.SetStreamHandler(ProtocolID, net.handleEnvelope)
net.logHostInfos()

if net.Bootstrap(ctx, false, cfg.Bootstrap...); err != nil {
return nil, err
}

return net, nil
}

Expand Down

0 comments on commit 92422cb

Please sign in to comment.