Skip to content

Commit

Permalink
WIP: update go-libp2p to v0.16.0
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed Oct 21, 2021
1 parent 5a61bed commit 03af870
Show file tree
Hide file tree
Showing 12 changed files with 115 additions and 429 deletions.
2 changes: 1 addition & 1 deletion core/corehttp/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestPeersTotal(t *testing.T) {
hosts := make([]*bhost.BasicHost, 4)
for i := 0; i < 4; i++ {
var err error
hosts[i], err = bhost.NewHost(ctx, swarmt.GenSwarm(t, ctx), nil)
hosts[i], err = bhost.NewHost(swarmt.GenSwarm(t), nil)
if err != nil {
t.Fatal(err)
}
Expand Down
6 changes: 3 additions & 3 deletions core/mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (
config "github.com/ipfs/go-ipfs-config"

"github.com/libp2p/go-libp2p"
host "github.com/libp2p/go-libp2p-core/host"
peer "github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/peer"
pstore "github.com/libp2p/go-libp2p-core/peerstore"
testutil "github.com/libp2p/go-libp2p-testing/net"

Expand All @@ -36,7 +36,7 @@ func NewMockNode() (*core.IpfsNode, error) {
}

func MockHostOption(mn mocknet.Mocknet) libp2p2.HostOption {
return func(ctx context.Context, id peer.ID, ps pstore.Peerstore, _ ...libp2p.Option) (host.Host, error) {
return func(id peer.ID, ps pstore.Peerstore, _ ...libp2p.Option) (host.Host, error) {
return mn.AddPeerWithPeerstore(id, ps)
}
}
Expand Down
6 changes: 4 additions & 2 deletions core/node/libp2p/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ func DiscoveryHandler(mctx helpers.MetricsCtx, lc fx.Lifecycle, host host.Host)
func SetupDiscovery(useMdns bool, mdnsInterval int) func(helpers.MetricsCtx, fx.Lifecycle, host.Host, *discoveryHandler) error {
return func(mctx helpers.MetricsCtx, lc fx.Lifecycle, host host.Host, handler *discoveryHandler) error {
if useMdns {
service := mdns.NewMdnsService(host, mdns.ServiceName)
service.RegisterNotifee(handler)
service := mdns.NewMdnsService(host, mdns.ServiceName, handler)
if err := service.Start(); err != nil {
return err
}

if mdnsInterval == 0 {
mdnsInterval = 5
Expand Down
10 changes: 5 additions & 5 deletions core/node/libp2p/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"context"

"github.com/libp2p/go-libp2p"
host "github.com/libp2p/go-libp2p-core/host"
peer "github.com/libp2p/go-libp2p-core/peer"
peerstore "github.com/libp2p/go-libp2p-core/peerstore"
routing "github.com/libp2p/go-libp2p-core/routing"
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/peerstore"
"github.com/libp2p/go-libp2p-core/routing"
record "github.com/libp2p/go-libp2p-record"
routedhost "github.com/libp2p/go-libp2p/p2p/host/routed"

Expand Down Expand Up @@ -64,7 +64,7 @@ func Host(mctx helpers.MetricsCtx, lc fx.Lifecycle, params P2PHostIn) (out P2PHo
return r, err
}))

out.Host, err = params.HostOption(ctx, params.ID, params.Peerstore, opts...)
out.Host, err = params.HostOption(params.ID, params.Peerstore, opts...)
if err != nil {
return P2PHostOut{}, err
}
Expand Down
13 changes: 6 additions & 7 deletions core/node/libp2p/hostopt.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
package libp2p

import (
"context"
"fmt"

"github.com/libp2p/go-libp2p"
host "github.com/libp2p/go-libp2p-core/host"
peer "github.com/libp2p/go-libp2p-core/peer"
peerstore "github.com/libp2p/go-libp2p-core/peerstore"
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/peerstore"
)

type HostOption func(ctx context.Context, id peer.ID, ps peerstore.Peerstore, options ...libp2p.Option) (host.Host, error)
type HostOption func(id peer.ID, ps peerstore.Peerstore, options ...libp2p.Option) (host.Host, error)

var DefaultHostOption HostOption = constructPeerHost

// isolates the complex initialization steps
func constructPeerHost(ctx context.Context, id peer.ID, ps peerstore.Peerstore, options ...libp2p.Option) (host.Host, error) {
func constructPeerHost(id peer.ID, ps peerstore.Peerstore, options ...libp2p.Option) (host.Host, error) {
pkey := ps.PrivKey(id)
if pkey == nil {
return nil, fmt.Errorf("missing private key for node ID: %s", id.Pretty())
}
options = append([]libp2p.Option{libp2p.Identity(pkey), libp2p.Peerstore(ps)}, options...)
return libp2p.New(ctx, options...)
return libp2p.New(options...)
}
8 changes: 2 additions & 6 deletions core/node/libp2p/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@ package libp2p

import (
"github.com/libp2p/go-libp2p"
relay "github.com/libp2p/go-libp2p-circuit"
)

func Relay(enableRelay, enableHop bool) func() (opts Libp2pOpts, err error) {
return func() (opts Libp2pOpts, err error) {
if enableRelay {
relayOpts := []relay.RelayOpt{}
if enableHop {
relayOpts = append(relayOpts, relay.OptHop)
}
opts.Opts = append(opts.Opts, libp2p.EnableRelay(relayOpts...))
// TODO: enable relay v2
opts.Opts = append(opts.Opts, libp2p.EnableRelay())
} else {
opts.Opts = append(opts.Opts, libp2p.DisableRelay())
}
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/go-ipfs-as-a-library/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/ipfs/go-ipfs-config v0.16.0
github.com/ipfs/go-ipfs-files v0.0.9
github.com/ipfs/interface-go-ipfs-core v0.5.1
github.com/libp2p/go-libp2p-core v0.9.0
github.com/libp2p/go-libp2p-core v0.11.0
github.com/multiformats/go-multiaddr v0.4.0
)

Expand Down
Loading

0 comments on commit 03af870

Please sign in to comment.