Skip to content

Commit

Permalink
fix: pubsub discovery
Browse files Browse the repository at this point in the history
Signed-off-by: gfanton <8671905+gfanton@users.noreply.github.com>
  • Loading branch information
gfanton committed Jul 6, 2023
1 parent c219763 commit f6eff43
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/aead/ecdh v0.2.0
github.com/berty/emitter-go v0.0.0-20221031144724-5dae963c3622
github.com/berty/go-libp2p-mock v1.0.1
github.com/berty/go-libp2p-pubsub v0.9.4-0.20230706070911-6e35c0f470b8
github.com/berty/go-libp2p-rendezvous v0.5.0
github.com/buicongtan1997/protoc-gen-swagger-config v0.0.0-20200705084907-1342b78c1a7e
github.com/daixiang0/gci v0.8.2
Expand Down
2 changes: 2 additions & 0 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 40 additions & 1 deletion service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ import (
"context"
"encoding/hex"
"fmt"
"math/rand"
mrand "math/rand"
"path/filepath"
"sync"
"sync/atomic"
"time"
"unsafe"

pubsub_fix "github.com/berty/go-libp2p-pubsub"
ds "github.com/ipfs/go-datastore"
ds_sync "github.com/ipfs/go-datastore/sync"
ipfs_interface "github.com/ipfs/interface-go-ipfs-core"
Expand All @@ -18,6 +21,7 @@ import (
"github.com/libp2p/go-libp2p/core/event"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/network"
backoff "github.com/libp2p/go-libp2p/p2p/discovery/backoff"
"github.com/libp2p/go-libp2p/p2p/host/eventbus"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
Expand All @@ -29,6 +33,7 @@ import (
"berty.tech/go-orbit-db/baseorbitdb"
"berty.tech/go-orbit-db/iface"
"berty.tech/go-orbit-db/pubsub/directchannel"
"berty.tech/go-orbit-db/pubsub/pubsubraw"
"berty.tech/weshnet/internal/bertyversion"
"berty.tech/weshnet/internal/datastoreutil"
"berty.tech/weshnet/pkg/bertyvcissuer"
Expand Down Expand Up @@ -168,7 +173,7 @@ func (opts *Opts) applyDefaults(ctx context.Context) error {
mrepo := ipfs_mobile.NewRepoMobile("", repo)
mnode, err = ipfsutil.NewIPFSMobile(ctx, mrepo, &ipfsutil.MobileOptions{
ExtraOpts: map[string]bool{
"pubsub": true,
"pubsub": false,
},
})
if err != nil {
Expand Down Expand Up @@ -218,15 +223,49 @@ func (opts *Opts) applyDefaults(ctx context.Context) error {
}
}

if opts.PubSub == nil {
var err error

rng := rand.New(rand.NewSource(time.Now().UnixNano()))
popts := []pubsub_fix.Option{
pubsub_fix.WithMessageSigning(true),
pubsub_fix.WithPeerExchange(true),
}

backoffstrat := backoff.NewExponentialBackoff(
time.Second*10, time.Hour,
backoff.FullJitter,
time.Second, 10.0, 0, rng)

cacheSize := 100
dialTimeout := time.Second * 20
backoffconnector := func(host host.Host) (*backoff.BackoffConnector, error) {
return backoff.NewBackoffConnector(host, cacheSize, dialTimeout, backoffstrat)
}

adaptater := tinder.NewDiscoveryAdaptater(opts.Logger.Named("disc"), opts.TinderService)
popts = append(popts, pubsub_fix.WithDiscovery(adaptater, pubsub_fix.WithDiscoverConnector(backoffconnector)))

// pubsub.DiscoveryPollInterval = m.Node.Protocol.PollInterval
ps, err := pubsub_fix.NewGossipSub(ctx, opts.Host, popts...)
if err != nil {
return fmt.Errorf("unable to init gossipsub: %w", err)
}

opts.PubSub = (*pubsub.PubSub)(unsafe.Pointer(ps))
}

if opts.OrbitDB == nil {
orbitDirectory := InMemoryDirectory
if opts.DatastoreDir != InMemoryDirectory {
orbitDirectory = filepath.Join(opts.DatastoreDir, NamespaceOrbitDBDirectory)
}

pubsub := pubsubraw.NewPubSub(opts.PubSub, opts.Host.ID(), opts.Logger, nil)
odbOpts := &NewOrbitDBOptions{
NewOrbitDBOptions: baseorbitdb.NewOrbitDBOptions{
Directory: &orbitDirectory,
PubSub: pubsub,
Logger: opts.Logger,
},
PrometheusRegister: opts.PrometheusRegister,
Expand Down

0 comments on commit f6eff43

Please sign in to comment.