Skip to content

Commit

Permalink
constructor: move libp2p related stuff to subpackage
Browse files Browse the repository at this point in the history
  • Loading branch information
magik6k committed Apr 23, 2019
1 parent cc2d66f commit 9aebf5e
Show file tree
Hide file tree
Showing 13 changed files with 115 additions and 97 deletions.
8 changes: 4 additions & 4 deletions cmd/ipfs/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
coreapi "github.com/ipfs/go-ipfs/core/coreapi"
corehttp "github.com/ipfs/go-ipfs/core/corehttp"
corerepo "github.com/ipfs/go-ipfs/core/corerepo"
"github.com/ipfs/go-ipfs/core/node"
libp2p "github.com/ipfs/go-ipfs/core/node/libp2p"
nodeMount "github.com/ipfs/go-ipfs/fuse/node"
fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"
migrate "github.com/ipfs/go-ipfs/repo/fsrepo/migrations"
Expand Down Expand Up @@ -324,11 +324,11 @@ func daemonFunc(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment
case routingOptionSupernodeKwd:
return errors.New("supernode routing was never fully implemented and has been removed")
case routingOptionDHTClientKwd:
ncfg.Routing = node.DHTClientOption
ncfg.Routing = libp2p.DHTClientOption
case routingOptionDHTKwd:
ncfg.Routing = node.DHTOption
ncfg.Routing = libp2p.DHTOption
case routingOptionNoneKwd:
ncfg.Routing = node.NilRouterOption
ncfg.Routing = libp2p.NilRouterOption
default:
return fmt.Errorf("unrecognized routing option: %s", routingOption)
}
Expand Down
9 changes: 5 additions & 4 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
version "github.com/ipfs/go-ipfs"
"github.com/ipfs/go-ipfs/core/bootstrap"
"github.com/ipfs/go-ipfs/core/node"
"github.com/ipfs/go-ipfs/core/node/libp2p"
rp "github.com/ipfs/go-ipfs/exchange/reprovide"
"github.com/ipfs/go-ipfs/filestore"
"github.com/ipfs/go-ipfs/fuse/mount"
Expand Down Expand Up @@ -68,10 +69,10 @@ type IpfsNode struct {
Repo repo.Repo

// Local node
Pinning pin.Pinner // the pinning manager
Mounts Mounts `optional:"true"` // current mount state, if any.
PrivateKey ic.PrivKey // the local node's private Key
PNetFingerprint node.PNetFingerprint `optional:"true"` // fingerprint of private network
Pinning pin.Pinner // the pinning manager
Mounts Mounts `optional:"true"` // current mount state, if any.
PrivateKey ic.PrivKey // the local node's private Key
PNetFingerprint libp2p.PNetFingerprint `optional:"true"` // fingerprint of private network

// Services
Peerstore pstore.Peerstore `optional:"true"` // storage for other Peer instances
Expand Down
14 changes: 7 additions & 7 deletions core/mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ package coremock

import (
"context"
libp2p2 "github.com/ipfs/go-ipfs/core/node/libp2p"

commands "github.com/ipfs/go-ipfs/commands"
core "github.com/ipfs/go-ipfs/core"
"github.com/ipfs/go-ipfs/core/node"
"github.com/ipfs/go-ipfs/commands"
"github.com/ipfs/go-ipfs/core"
"github.com/ipfs/go-ipfs/repo"

datastore "github.com/ipfs/go-datastore"
"github.com/ipfs/go-datastore"
syncds "github.com/ipfs/go-datastore/sync"
config "github.com/ipfs/go-ipfs-config"
libp2p "github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p"
host "github.com/libp2p/go-libp2p-host"
peer "github.com/libp2p/go-libp2p-peer"
pstore "github.com/libp2p/go-libp2p-peerstore"
mocknet "github.com/libp2p/go-libp2p/p2p/net/mock"
testutil "github.com/libp2p/go-testutil"
"github.com/libp2p/go-testutil"
)

// NewMockNode constructs an IpfsNode for use in tests.
Expand All @@ -30,7 +30,7 @@ func NewMockNode() (*core.IpfsNode, error) {
})
}

func MockHostOption(mn mocknet.Mocknet) node.HostOption {
func MockHostOption(mn mocknet.Mocknet) libp2p2.HostOption {
return func(ctx context.Context, id peer.ID, ps pstore.Peerstore, _ ...libp2p.Option) (host.Host, error) {
return mn.AddPeerWithPeerstore(id, ps)
}
Expand Down
21 changes: 13 additions & 8 deletions core/node/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,20 @@ import (

"go.uber.org/fx"

"github.com/ipfs/go-ipfs/core/node/helpers"
"github.com/ipfs/go-ipfs/core/node/libp2p"
"github.com/ipfs/go-ipfs/repo"

ds "github.com/ipfs/go-datastore"
dsync "github.com/ipfs/go-datastore/sync"
cfg "github.com/ipfs/go-ipfs-config"
logging "github.com/ipfs/go-log"
ci "github.com/libp2p/go-libp2p-crypto"
peer "github.com/libp2p/go-libp2p-peer"
)

var log = logging.Logger("node")

type BuildCfg struct {
// If online is set, the node will have networking enabled
Online bool
Expand All @@ -35,8 +40,8 @@ type BuildCfg struct {
// If NilRepo is set, a Repo backed by a nil datastore will be constructed
NilRepo bool

Routing RoutingOption
Host HostOption
Routing libp2p.RoutingOption
Host libp2p.HostOption
Repo repo.Repo
}

Expand Down Expand Up @@ -68,11 +73,11 @@ func (cfg *BuildCfg) fillDefaults() error {
}

if cfg.Routing == nil {
cfg.Routing = DHTOption
cfg.Routing = libp2p.DHTOption
}

if cfg.Host == nil {
cfg.Host = DefaultHostOption
cfg.Host = libp2p.DefaultHostOption
}

return nil
Expand All @@ -94,15 +99,15 @@ func (cfg *BuildCfg) options(ctx context.Context) fx.Option {
return cfg.Repo
})

metricsCtx := fx.Provide(func() MetricsCtx {
return MetricsCtx(ctx)
metricsCtx := fx.Provide(func() helpers.MetricsCtx {
return helpers.MetricsCtx(ctx)
})

hostOption := fx.Provide(func() HostOption {
hostOption := fx.Provide(func() libp2p.HostOption {
return cfg.Host
})

routingOption := fx.Provide(func() RoutingOption {
routingOption := fx.Provide(func() libp2p.RoutingOption {
return cfg.Routing
})

Expand Down
10 changes: 5 additions & 5 deletions core/node/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"

"github.com/ipfs/go-ipfs/core/node/helpers"
"github.com/ipfs/go-ipfs/pin"
"github.com/ipfs/go-ipfs/repo"

Expand Down Expand Up @@ -54,9 +55,9 @@ func DagCtor(bs blockservice.BlockService) format.DAGService {
return merkledag.NewDAGService(bs)
}

func OnlineExchangeCtor(mctx MetricsCtx, lc fx.Lifecycle, host host.Host, rt routing.IpfsRouting, bs blockstore.GCBlockstore) exchange.Interface {
func OnlineExchangeCtor(mctx helpers.MetricsCtx, lc fx.Lifecycle, host host.Host, rt routing.IpfsRouting, bs blockstore.GCBlockstore) exchange.Interface {
bitswapNetwork := network.NewFromIpfsHost(host, rt)
exch := bitswap.New(lifecycleCtx(mctx, lc), bitswapNetwork, bs)
exch := bitswap.New(helpers.LifecycleCtx(mctx, lc), bitswapNetwork, bs)
lc.Append(fx.Hook{
OnStop: func(ctx context.Context) error {
return exch.Close()
Expand All @@ -65,15 +66,15 @@ func OnlineExchangeCtor(mctx MetricsCtx, lc fx.Lifecycle, host host.Host, rt rou
return exch
}

func Files(mctx MetricsCtx, lc fx.Lifecycle, repo repo.Repo, dag format.DAGService) (*mfs.Root, error) {
func Files(mctx helpers.MetricsCtx, lc fx.Lifecycle, repo repo.Repo, dag format.DAGService) (*mfs.Root, error) {
dsk := datastore.NewKey("/local/filesroot")
pf := func(ctx context.Context, c cid.Cid) error {
return repo.Datastore().Put(dsk, c.Bytes())
}

var nd *merkledag.ProtoNode
val, err := repo.Datastore().Get(dsk)
ctx := lifecycleCtx(mctx, lc)
ctx := helpers.LifecycleCtx(mctx, lc)

switch {
case err == datastore.ErrNotFound || val == nil:
Expand Down Expand Up @@ -114,4 +115,3 @@ func Files(mctx MetricsCtx, lc fx.Lifecycle, repo repo.Repo, dag format.DAGServi
return root, err
}

type MetricsCtx context.Context
53 changes: 27 additions & 26 deletions core/node/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package node
import (
"context"

"github.com/ipfs/go-ipfs/core/node/libp2p"
"github.com/ipfs/go-ipfs/p2p"
"github.com/ipfs/go-ipfs/provider"

Expand All @@ -13,38 +14,38 @@ import (
)

var BaseLibP2P = fx.Options(
fx.Provide(P2PAddrFilters),
fx.Provide(P2PBandwidthCounter),
fx.Provide(P2PPNet),
fx.Provide(P2PAddrsFactory),
fx.Provide(P2PConnectionManager),
fx.Provide(P2PNatPortMap),
fx.Provide(P2PRelay),
fx.Provide(P2PAutoRealy),
fx.Provide(P2PDefaultTransports),
fx.Provide(P2PQUIC),

fx.Provide(P2PHost),

fx.Provide(NewDiscoveryHandler),

fx.Invoke(AutoNATService),
fx.Invoke(P2PPNetChecker),
fx.Invoke(StartListening),
fx.Invoke(SetupDiscovery),
fx.Provide(libp2p.P2PAddrFilters),
fx.Provide(libp2p.P2PBandwidthCounter),
fx.Provide(libp2p.P2PPNet),
fx.Provide(libp2p.P2PAddrsFactory),
fx.Provide(libp2p.P2PConnectionManager),
fx.Provide(libp2p.P2PNatPortMap),
fx.Provide(libp2p.P2PRelay),
fx.Provide(libp2p.P2PAutoRealy),
fx.Provide(libp2p.P2PDefaultTransports),
fx.Provide(libp2p.P2PQUIC),

fx.Provide(libp2p.P2PHost),

fx.Provide(libp2p.NewDiscoveryHandler),

fx.Invoke(libp2p.AutoNATService),
fx.Invoke(libp2p.P2PPNetChecker),
fx.Invoke(libp2p.StartListening),
fx.Invoke(libp2p.SetupDiscovery),
)

func LibP2P(cfg *BuildCfg) fx.Option {
opts := fx.Options(
BaseLibP2P,

fx.Provide(P2PSecurity(!cfg.DisableEncryptedConnections)),
maybeProvide(Pubsub, cfg.getOpt("pubsub") || cfg.getOpt("ipnsps")),
fx.Provide(libp2p.P2PSecurity(!cfg.DisableEncryptedConnections)),
maybeProvide(libp2p.Pubsub, cfg.getOpt("pubsub") || cfg.getOpt("ipnsps")),

fx.Provide(P2PSmuxTransport(cfg.getOpt("mplex"))),
fx.Provide(P2PRouting),
fx.Provide(P2PBaseRouting),
maybeProvide(P2PPubsubRouter, cfg.getOpt("ipnsps")),
fx.Provide(libp2p.P2PSmuxTransport(cfg.getOpt("mplex"))),
fx.Provide(libp2p.P2PRouting),
fx.Provide(libp2p.P2PBaseRouting),
maybeProvide(libp2p.P2PPubsubRouter, cfg.getOpt("ipnsps")),
)

return opts
Expand All @@ -62,7 +63,7 @@ func Storage(cfg *BuildCfg) fx.Option {
var Identity = fx.Options(
fx.Provide(PeerID),
fx.Provide(PrivateKey),
fx.Provide(Peerstore),
fx.Provide(libp2p.Peerstore),
)

var IPNS = fx.Options(
Expand Down
15 changes: 0 additions & 15 deletions core/node/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,6 @@ import (
"go.uber.org/fx"
)

// lifecycleCtx creates a context which will be cancelled when lifecycle stops
//
// This is a hack which we need because most of our services use contexts in a
// wrong way
func lifecycleCtx(mctx MetricsCtx, lc fx.Lifecycle) context.Context {
ctx, cancel := context.WithCancel(mctx)
lc.Append(fx.Hook{
OnStop: func(_ context.Context) error {
cancel()
return nil
},
})
return ctx
}

type lcProcess struct {
fx.In

Expand Down
23 changes: 23 additions & 0 deletions core/node/helpers/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package helpers

import (
"context"
"go.uber.org/fx"
)

type MetricsCtx context.Context

// LifecycleCtx creates a context which will be cancelled when lifecycle stops
//
// This is a hack which we need because most of our services use contexts in a
// wrong way
func LifecycleCtx(mctx MetricsCtx, lc fx.Lifecycle) context.Context {
ctx, cancel := context.WithCancel(mctx)
lc.Append(fx.Hook{
OnStop: func(_ context.Context) error {
cancel()
return nil
},
})
return ctx
}
11 changes: 6 additions & 5 deletions core/node/discovery.go → core/node/libp2p/discovery.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package node
package libp2p

import (
"context"
"time"

"github.com/ipfs/go-ipfs-config"
"github.com/ipfs/go-ipfs/core/node/helpers"
"github.com/libp2p/go-libp2p-host"
"github.com/libp2p/go-libp2p-peerstore"
"github.com/libp2p/go-libp2p/p2p/discovery"
Expand All @@ -27,20 +28,20 @@ func (dh *discoveryHandler) HandlePeerFound(p peerstore.PeerInfo) {
}
}

func NewDiscoveryHandler(mctx MetricsCtx, lc fx.Lifecycle, host host.Host) *discoveryHandler {
func NewDiscoveryHandler(mctx helpers.MetricsCtx, lc fx.Lifecycle, host host.Host) *discoveryHandler {
return &discoveryHandler{
ctx: lifecycleCtx(mctx, lc),
ctx: helpers.LifecycleCtx(mctx, lc),
host: host,
}
}

func SetupDiscovery(mctx MetricsCtx, lc fx.Lifecycle, cfg *config.Config, host host.Host, handler *discoveryHandler) error {
func SetupDiscovery(mctx helpers.MetricsCtx, lc fx.Lifecycle, cfg *config.Config, host host.Host, handler *discoveryHandler) error {
if cfg.Discovery.MDNS.Enabled {
mdns := cfg.Discovery.MDNS
if mdns.Interval == 0 {
mdns.Interval = 5
}
service, err := discovery.NewMdnsService(lifecycleCtx(mctx, lc), host, time.Duration(mdns.Interval)*time.Second, discovery.ServiceTag)
service, err := discovery.NewMdnsService(helpers.LifecycleCtx(mctx, lc), host, time.Duration(mdns.Interval)*time.Second, discovery.ServiceTag)
if err != nil {
log.Error("mdns error: ", err)
return nil
Expand Down
Loading

2 comments on commit 9aebf5e

@GitCop
Copy link

@GitCop GitCop commented on 9aebf5e Apr 23, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There were the following issues with your Pull Request

We ask for a few features in the commit message for Open Source licensing hygiene and commit message clarity.
git commit --amend can often help you quickly improve the commit message.
Guidelines and a script are available to help in the long run.
Your feedback on GitCop is welcome on this issue.


This message was auto-generated by https://gitcop.com

@GitCop
Copy link

@GitCop GitCop commented on 9aebf5e Apr 23, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There were the following issues with your Pull Request

We ask for a few features in the commit message for Open Source licensing hygiene and commit message clarity.
git commit --amend can often help you quickly improve the commit message.
Guidelines and a script are available to help in the long run.
Your feedback on GitCop is welcome on this issue.


This message was auto-generated by https://gitcop.com

Please sign in to comment.