Skip to content

Commit

Permalink
feat: add rdvp to peering peers
Browse files Browse the repository at this point in the history
Signed-off-by: Guilhem Fanton <guilhem.fanton@gmail.com>
  • Loading branch information
gfanton committed Oct 9, 2020
1 parent b702636 commit 1ab2747
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 14 deletions.
10 changes: 8 additions & 2 deletions go/framework/bertybridge/bridge_protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
datastore "github.com/ipfs/go-datastore"
ds_sync "github.com/ipfs/go-datastore/sync"
ipfs_badger "github.com/ipfs/go-ds-badger"
ipfs_cfg "github.com/ipfs/go-ipfs-config"
"github.com/ipfs/go-ipfs/core"
"github.com/ipfs/go-ipfs/core/bootstrap"
ipfs_repo "github.com/ipfs/go-ipfs/repo"
Expand Down Expand Up @@ -201,6 +202,13 @@ func newProtocolBridge(ctx context.Context, logger *zap.Logger, config *Messenge
APIAddrs: defaultAPIAddrs,
APIConfig: APIConfig,
ExtraLibp2pOption: libp2p.ChainOptions(libp2p.Transport(mc.NewTransportConstructorWithLogger(logger))),
IpfsConfigPatch: func(cfg *ipfs_cfg.Config) error {
for _, p := range rdvpeers {
cfg.Peering.Peers = append(cfg.Peering.Peers, *p)
}

return nil
},
HostConfig: func(h host.Host, _ routing.Routing) error {
var err error
var rdvClients []tinder.AsyncableDriver
Expand Down Expand Up @@ -253,8 +261,6 @@ func newProtocolBridge(ctx context.Context, logger *zap.Logger, config *Messenge

bopts.BootstrapAddrs = defaultProtocolBootstrap

// should be a valid rendezvous peer
bopts.BootstrapAddrs = append(bopts.BootstrapAddrs, defaultProtocolRendezVousPeers...)
if len(config.swarmListeners) > 0 {
bopts.SwarmAddrs = append(bopts.SwarmAddrs, config.swarmListeners...)
}
Expand Down
8 changes: 8 additions & 0 deletions go/internal/initutil/ipfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

datastore "github.com/ipfs/go-datastore"
ipfs_cfg "github.com/ipfs/go-ipfs-config"
ipfs_core "github.com/ipfs/go-ipfs/core"
libp2p "github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p-core/host"
Expand Down Expand Up @@ -98,6 +99,13 @@ func (m *Manager) getLocalIPFS() (ipfsutil.ExtendedCoreAPI, *ipfs_core.IpfsNode,
NoAnnounce: noannounce,
DisableCorePubSub: true,
BootstrapAddrs: config.BertyDev.Bootstrap,
IpfsConfigPatch: func(cfg *ipfs_cfg.Config) error {
for _, p := range rdvpeers {
cfg.Peering.Peers = append(cfg.Peering.Peers, *p)
}

return nil
},
HostConfig: func(h host.Host, _ routing.Routing) error {
var err error

Expand Down
26 changes: 24 additions & 2 deletions go/internal/ipfsutil/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
ds "github.com/ipfs/go-datastore"
dsync "github.com/ipfs/go-datastore/sync"
config "github.com/ipfs/go-ipfs-config"
ipfs_cfg "github.com/ipfs/go-ipfs-config"
ipfs_core "github.com/ipfs/go-ipfs/core"
ipfs_coreapi "github.com/ipfs/go-ipfs/core/coreapi"
ipfs_node "github.com/ipfs/go-ipfs/core/node"
Expand All @@ -25,6 +26,8 @@ import (
"berty.tech/berty/v2/go/pkg/errcode"
)

type IpfsConfigPatcher func(*ipfs_cfg.Config) error

type CoreAPIOption func(context.Context, *ipfs_core.IpfsNode, ipfs_interface.CoreAPI) error

type CoreAPIConfig struct {
Expand All @@ -39,21 +42,33 @@ type CoreAPIConfig struct {
HostConfig func(host.Host, p2p_routing.Routing) error
ExtraLibp2pOption p2p.Option
DHTOption []p2p_dht.Option
IpfsConfigPatches []ConfigPatcher
IpfsConfigPatch IpfsConfigPatcher

Routing ipfs_libp2p.RoutingOption
Host ipfs_libp2p.HostOption

Options []CoreAPIOption
}

func ChainIpfsConfigPatch(ps ...IpfsConfigPatcher) IpfsConfigPatcher {
return func(c *ipfs_cfg.Config) error {
for _, p := range ps {
if err := p(c); err != nil {
return err
}
}

return nil
}
}

func NewCoreAPI(ctx context.Context, cfg *CoreAPIConfig) (ExtendedCoreAPI, *ipfs_core.IpfsNode, error) {
ds := dsync.MutexWrap(ds.NewMapDatastore())
return NewCoreAPIFromDatastore(ctx, ds, cfg)
}

func NewCoreAPIFromDatastore(ctx context.Context, ds ds.Batching, cfg *CoreAPIConfig) (ExtendedCoreAPI, *ipfs_core.IpfsNode, error) {
repo, err := CreateMockedRepo(ds, cfg.IpfsConfigPatches...)
repo, err := CreateMockedRepo(ds)
if err != nil {
return nil, nil, errcode.TODO.Wrap(err)
}
Expand Down Expand Up @@ -171,6 +186,13 @@ func updateRepoConfig(repo ipfs_repo.Repo, cfg *CoreAPIConfig) error {
rcfg.API = cfg.APIConfig
}

// apply patches
if cfg.IpfsConfigPatch != nil {
if err := cfg.IpfsConfigPatch(rcfg); err != nil {
return err
}
}

return repo.SetConfig(rcfg)
}

Expand Down
11 changes: 1 addition & 10 deletions go/internal/ipfsutil/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,12 @@ const defaultConnMgrGracePeriod = time.Second * 20
// @NOTE(gfanton): this will be removed with gomobile-ipfs
var plugins *ipfs_loader.PluginLoader

type ConfigPatcher func(*ipfs_cfg.Config) error

func CreateMockedRepo(dstore ipfs_ds.Batching, cPatchs ...ConfigPatcher) (ipfs_repo.Repo, error) {
func CreateMockedRepo(dstore ipfs_ds.Batching) (ipfs_repo.Repo, error) {
c, err := createBaseConfig()
if err != nil {
return nil, err
}

for _, p := range cPatchs {
err = p(c)
if err != nil {
return nil, errcode.TODO.Wrap(err)
}
}

return &ipfs_repo.Mock{
D: dstore,
C: *c,
Expand Down

0 comments on commit 1ab2747

Please sign in to comment.