Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

constructor: cleanup some things #6246

Merged
merged 3 commits into from
Apr 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
22 changes: 14 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,16 +73,17 @@ 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
}

// options creates fx option group from this build config
func (cfg *BuildCfg) options(ctx context.Context) fx.Option {
err := cfg.fillDefaults()
if err != nil {
Expand All @@ -94,15 +100,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
19 changes: 12 additions & 7 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 All @@ -24,7 +25,8 @@ import (
"go.uber.org/fx"
)

func BlockServiceCtor(lc fx.Lifecycle, bs blockstore.Blockstore, rem exchange.Interface) blockservice.BlockService {
// BlockService creates new blockservice which provides an interface to fetch content-addressable blocks
func BlockService(lc fx.Lifecycle, bs blockstore.Blockstore, rem exchange.Interface) blockservice.BlockService {
bsvc := blockservice.New(bs, rem)

lc.Append(fx.Hook{
Expand All @@ -36,6 +38,7 @@ func BlockServiceCtor(lc fx.Lifecycle, bs blockstore.Blockstore, rem exchange.In
return bsvc
}

// Pinning creates new pinner which tells GC which blocks should be kept
func Pinning(bstore blockstore.Blockstore, ds format.DAGService, repo repo.Repo) (pin.Pinner, error) {
internalDag := merkledag.NewDAGService(blockservice.New(bstore, offline.Exchange(bstore)))
pinning, err := pin.LoadPinner(repo.Datastore(), ds, internalDag)
Expand All @@ -50,13 +53,15 @@ func Pinning(bstore blockstore.Blockstore, ds format.DAGService, repo repo.Repo)
return pinning, nil
}

func DagCtor(bs blockservice.BlockService) format.DAGService {
// Dag creates new DAGService
func Dag(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 {
// OnlineExchange creates new LibP2P backed block exchange (BitSwap)
func OnlineExchange(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 +70,16 @@ 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) {
// Files loads persisted MFS root
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 +120,3 @@ func Files(mctx MetricsCtx, lc fx.Lifecycle, repo repo.Repo, dag format.DAGServi
return root, err
}

type MetricsCtx context.Context
75 changes: 42 additions & 33 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,62 +14,66 @@ 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.AddrFilters),
fx.Provide(libp2p.BandwidthCounter),
fx.Provide(libp2p.PNet),
fx.Provide(libp2p.AddrsFactory),
fx.Provide(libp2p.ConnectionManager),
fx.Provide(libp2p.NatPortMap),
fx.Provide(libp2p.Relay),
fx.Provide(libp2p.AutoRealy),
fx.Provide(libp2p.DefaultTransports),
fx.Provide(libp2p.QUIC),

fx.Provide(libp2p.Host),

fx.Provide(libp2p.DiscoveryHandler),

fx.Invoke(libp2p.AutoNATService),
fx.Invoke(libp2p.PNetChecker),
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.Security(!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.SmuxTransport(cfg.getOpt("mplex"))),
fx.Provide(libp2p.Routing),
fx.Provide(libp2p.BaseRouting),
maybeProvide(libp2p.PubsubRouter, cfg.getOpt("ipnsps")),
)

return opts
}

// Storage groups units which setup datastore based persistence and blockstore layers
func Storage(cfg *BuildCfg) fx.Option {
return fx.Options(
fx.Provide(RepoConfig),
fx.Provide(DatastoreCtor),
fx.Provide(Datastore),
fx.Provide(BaseBlockstoreCtor(cfg.Permanent, cfg.NilRepo)),
fx.Provide(GcBlockstoreCtor),
)
}

// Identity groups units providing cryptographic identity
var Identity = fx.Options(
fx.Provide(PeerID),
fx.Provide(PrivateKey),
fx.Provide(Peerstore),
fx.Provide(libp2p.Peerstore),
)

// IPNS groups namesys related units
var IPNS = fx.Options(
fx.Provide(RecordValidator),
)

// Providers groups units managing provider routing records
var Providers = fx.Options(
fx.Provide(ProviderQueue),
fx.Provide(ProviderCtor),
Expand All @@ -77,30 +82,33 @@ var Providers = fx.Options(
fx.Invoke(Reprovider),
)

// Online groups online-only units
func Online(cfg *BuildCfg) fx.Option {
return fx.Options(
fx.Provide(OnlineExchangeCtor),
fx.Provide(OnlineNamesysCtor),
fx.Provide(OnlineExchange),
fx.Provide(OnlineNamesys),

fx.Invoke(IpnsRepublisher),

fx.Provide(p2p.NewP2P),
fx.Provide(p2p.New),

LibP2P(cfg),
Providers,
)
}

// Offline groups offline alternatives to Online units
var Offline = fx.Options(
fx.Provide(offline.Exchange),
fx.Provide(OfflineNamesysCtor),
fx.Provide(OfflineNamesys),
fx.Provide(offroute.NewOfflineRouter),
fx.Provide(provider.NewOfflineProvider),
)

// Core groups basic IPFS services
var Core = fx.Options(
fx.Provide(BlockServiceCtor),
fx.Provide(DagCtor),
fx.Provide(BlockService),
fx.Provide(Dag),
fx.Provide(resolver.NewBasicResolver),
fx.Provide(Pinning),
fx.Provide(Files),
Expand All @@ -113,6 +121,7 @@ func Networked(cfg *BuildCfg) fx.Option {
return Offline
}

// IPFS builds a group of fx Options based on the passed BuildCfg
func IPFS(ctx context.Context, cfg *BuildCfg) fx.Option {
if cfg == nil {
cfg = new(BuildCfg)
Expand Down
Loading