constructor: move libp2p related stuff to subpackage

License: MIT
Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
This commit is contained in:
Łukasz Magiera 2019-04-23 12:35:51 +02:00
parent cc2d66ff06
commit da473d286f
13 changed files with 112 additions and 94 deletions

View File

@ -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"
@ -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)
}

View File

@ -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"
@ -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

View File

@ -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.
@ -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)
}

View File

@ -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
@ -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
}
@ -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
@ -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
})

View File

@ -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"
@ -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()
@ -65,7 +66,7 @@ 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())
@ -73,7 +74,7 @@ func Files(mctx MetricsCtx, lc fx.Lifecycle, repo repo.Repo, dag format.DAGServi
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:
@ -114,4 +115,3 @@ func Files(mctx MetricsCtx, lc fx.Lifecycle, repo repo.Repo, dag format.DAGServi
return root, err
}
type MetricsCtx context.Context

View File

@ -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"
@ -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(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(P2PHost),
fx.Provide(libp2p.P2PHost),
fx.Provide(NewDiscoveryHandler),
fx.Provide(libp2p.NewDiscoveryHandler),
fx.Invoke(AutoNATService),
fx.Invoke(P2PPNetChecker),
fx.Invoke(StartListening),
fx.Invoke(SetupDiscovery),
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
@ -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(

View File

@ -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

View File

@ -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
}

View File

@ -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"
@ -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

View File

@ -1,4 +1,4 @@
package node
package libp2p
import (
"bytes"
@ -45,10 +45,11 @@ import (
mamask "github.com/whyrusleeping/multiaddr-filter"
"go.uber.org/fx"
"github.com/ipfs/go-ipfs/core/node/helpers"
"github.com/ipfs/go-ipfs/repo"
)
var log = logging.Logger("node")
var log = logging.Logger("p2pnode")
type HostOption func(ctx context.Context, id peer.ID, ps peerstore.Peerstore, options ...libp2p.Option) (host.Host, error)
type RoutingOption func(context.Context, host.Host, datastore.Batching, record.Validator) (routing.IpfsRouting, error)
@ -396,13 +397,13 @@ type P2PHostOut struct {
Routing BaseRouting
}
func P2PHost(mctx MetricsCtx, lc fx.Lifecycle, params P2PHostIn) (out P2PHostOut, err error) {
func P2PHost(mctx helpers.MetricsCtx, lc fx.Lifecycle, params P2PHostIn) (out P2PHostOut, err error) {
opts := []libp2p.Option{libp2p.NoListenAddrs}
for _, o := range params.Opts {
opts = append(opts, o...)
}
ctx := lifecycleCtx(mctx, lc)
ctx := helpers.LifecycleCtx(mctx, lc)
opts = append(opts, libp2p.Routing(func(h host.Host) (routing.PeerRouting, error) {
r, err := params.RoutingOption(ctx, h, params.Repo.Datastore(), params.Validator)
@ -501,9 +502,9 @@ type p2pPSRoutingIn struct {
PubSub *pubsub.PubSub `optional:"true"`
}
func P2PPubsubRouter(mctx MetricsCtx, lc fx.Lifecycle, in p2pPSRoutingIn) (p2pRouterOut, *namesys.PubsubValueStore) {
func P2PPubsubRouter(mctx helpers.MetricsCtx, lc fx.Lifecycle, in p2pPSRoutingIn) (p2pRouterOut, *namesys.PubsubValueStore) {
psRouter := namesys.NewPubsubValueStore(
lifecycleCtx(mctx, lc),
helpers.LifecycleCtx(mctx, lc),
in.Host,
in.BaseRouting,
in.PubSub,
@ -523,7 +524,7 @@ func P2PPubsubRouter(mctx MetricsCtx, lc fx.Lifecycle, in p2pPSRoutingIn) (p2pRo
}, psRouter
}
func AutoNATService(mctx MetricsCtx, lc fx.Lifecycle, cfg *config.Config, host host.Host) error {
func AutoNATService(mctx helpers.MetricsCtx, lc fx.Lifecycle, cfg *config.Config, host host.Host) error {
if !cfg.Swarm.EnableAutoNATService {
return nil
}
@ -532,11 +533,11 @@ func AutoNATService(mctx MetricsCtx, lc fx.Lifecycle, cfg *config.Config, host h
opts = append(opts, libp2p.DefaultTransports, libp2p.Transport(libp2pquic.NewTransport))
}
_, err := autonat.NewAutoNATService(lifecycleCtx(mctx, lc), host, opts...)
_, err := autonat.NewAutoNATService(helpers.LifecycleCtx(mctx, lc), host, opts...)
return err
}
func Pubsub(mctx MetricsCtx, lc fx.Lifecycle, host host.Host, cfg *config.Config) (service *pubsub.PubSub, err error) {
func Pubsub(mctx helpers.MetricsCtx, lc fx.Lifecycle, host host.Host, cfg *config.Config) (service *pubsub.PubSub, err error) {
var pubsubOptions []pubsub.Option
if cfg.Pubsub.DisableSigning {
pubsubOptions = append(pubsubOptions, pubsub.WithMessageSigning(false))
@ -550,10 +551,10 @@ func Pubsub(mctx MetricsCtx, lc fx.Lifecycle, host host.Host, cfg *config.Config
case "":
fallthrough
case "floodsub":
service, err = pubsub.NewFloodSub(lifecycleCtx(mctx, lc), host, pubsubOptions...)
service, err = pubsub.NewFloodSub(helpers.LifecycleCtx(mctx, lc), host, pubsubOptions...)
case "gossipsub":
service, err = pubsub.NewGossipSub(lifecycleCtx(mctx, lc), host, pubsubOptions...)
service, err = pubsub.NewGossipSub(helpers.LifecycleCtx(mctx, lc), host, pubsubOptions...)
default:
err = fmt.Errorf("Unknown pubsub router %s", cfg.Pubsub.Router)

View File

@ -10,6 +10,7 @@ import (
"github.com/libp2p/go-libp2p-routing"
"go.uber.org/fx"
"github.com/ipfs/go-ipfs/core/node/helpers"
"github.com/ipfs/go-ipfs/exchange/reprovide"
"github.com/ipfs/go-ipfs/pin"
"github.com/ipfs/go-ipfs/provider"
@ -18,12 +19,12 @@ import (
const kReprovideFrequency = time.Hour * 12
func ProviderQueue(mctx MetricsCtx, lc fx.Lifecycle, repo repo.Repo) (*provider.Queue, error) {
return provider.NewQueue(lifecycleCtx(mctx, lc), "provider-v1", repo.Datastore())
func ProviderQueue(mctx helpers.MetricsCtx, lc fx.Lifecycle, repo repo.Repo) (*provider.Queue, error) {
return provider.NewQueue(helpers.LifecycleCtx(mctx, lc), "provider-v1", repo.Datastore())
}
func ProviderCtor(mctx MetricsCtx, lc fx.Lifecycle, queue *provider.Queue, rt routing.IpfsRouting) provider.Provider {
p := provider.NewProvider(lifecycleCtx(mctx, lc), queue, rt)
func ProviderCtor(mctx helpers.MetricsCtx, lc fx.Lifecycle, queue *provider.Queue, rt routing.IpfsRouting) provider.Provider {
p := provider.NewProvider(helpers.LifecycleCtx(mctx, lc), queue, rt)
lc.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
@ -38,7 +39,7 @@ func ProviderCtor(mctx MetricsCtx, lc fx.Lifecycle, queue *provider.Queue, rt ro
return p
}
func ReproviderCtor(mctx MetricsCtx, lc fx.Lifecycle, cfg *config.Config, bs BaseBlocks, ds format.DAGService, pinning pin.Pinner, rt routing.IpfsRouting) (*reprovide.Reprovider, error) {
func ReproviderCtor(mctx helpers.MetricsCtx, lc fx.Lifecycle, cfg *config.Config, bs BaseBlocks, ds format.DAGService, pinning pin.Pinner, rt routing.IpfsRouting) (*reprovide.Reprovider, error) {
var keyProvider reprovide.KeyChanFunc
switch cfg.Reprovider.Strategy {
@ -53,7 +54,7 @@ func ReproviderCtor(mctx MetricsCtx, lc fx.Lifecycle, cfg *config.Config, bs Bas
default:
return nil, fmt.Errorf("unknown reprovider strategy '%s'", cfg.Reprovider.Strategy)
}
return reprovide.NewReprovider(lifecycleCtx(mctx, lc), rt, keyProvider), nil
return reprovide.NewReprovider(helpers.LifecycleCtx(mctx, lc), rt, keyProvider), nil
}
func Reprovider(cfg *config.Config, reprovider *reprovide.Reprovider) error {

View File

@ -12,6 +12,7 @@ import (
config "github.com/ipfs/go-ipfs-config"
"go.uber.org/fx"
"github.com/ipfs/go-ipfs/core/node/helpers"
"github.com/ipfs/go-ipfs/filestore"
"github.com/ipfs/go-ipfs/repo"
"github.com/ipfs/go-ipfs/thirdparty/cidv0v1"
@ -37,8 +38,8 @@ func DatastoreCtor(repo repo.Repo) datastore.Datastore {
type BaseBlocks blockstore.Blockstore
func BaseBlockstoreCtor(permanent bool, nilRepo bool) func(mctx MetricsCtx, repo repo.Repo, cfg *config.Config, lc fx.Lifecycle) (bs BaseBlocks, err error) {
return func(mctx MetricsCtx, repo repo.Repo, cfg *config.Config, lc fx.Lifecycle) (bs BaseBlocks, err error) {
func BaseBlockstoreCtor(permanent bool, nilRepo bool) func(mctx helpers.MetricsCtx, repo repo.Repo, cfg *config.Config, lc fx.Lifecycle) (bs BaseBlocks, err error) {
return func(mctx helpers.MetricsCtx, repo repo.Repo, cfg *config.Config, lc fx.Lifecycle) (bs BaseBlocks, err error) {
rds := &retrystore.Datastore{
Batching: repo.Datastore(),
Delay: time.Millisecond * 200,

View File

@ -6,11 +6,10 @@ import (
"testing"
"github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid"
"github.com/ipfs/go-ipfs/core"
"github.com/ipfs/go-ipfs/core/mock"
"github.com/ipfs/go-ipfs/core/node"
cid "github.com/ipfs/go-cid"
"github.com/ipfs/go-ipfs/core/node/libp2p"
mocknet "github.com/libp2p/go-libp2p/p2p/net/mock"
)
@ -27,7 +26,7 @@ func TestBitswapWithoutRouting(t *testing.T) {
n, err := core.NewNode(ctx, &core.BuildCfg{
Online: true,
Host: coremock.MockHostOption(mn),
Routing: node.NilRouterOption, // no routing
Routing: libp2p.NilRouterOption, // no routing
})
if err != nil {
t.Fatal(err)