Skip to content

Commit 38fb008

Browse files
committed
constructor: move libp2p related stuff to subpackage
1 parent cc2d66f commit 38fb008

File tree

13 files changed

+115
-97
lines changed

13 files changed

+115
-97
lines changed

cmd/ipfs/daemon.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
coreapi "github.com/ipfs/go-ipfs/core/coreapi"
2121
corehttp "github.com/ipfs/go-ipfs/core/corehttp"
2222
corerepo "github.com/ipfs/go-ipfs/core/corerepo"
23-
"github.com/ipfs/go-ipfs/core/node"
23+
libp2p "github.com/ipfs/go-ipfs/core/node/libp2p"
2424
nodeMount "github.com/ipfs/go-ipfs/fuse/node"
2525
fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"
2626
migrate "github.com/ipfs/go-ipfs/repo/fsrepo/migrations"
@@ -324,11 +324,11 @@ func daemonFunc(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment
324324
case routingOptionSupernodeKwd:
325325
return errors.New("supernode routing was never fully implemented and has been removed")
326326
case routingOptionDHTClientKwd:
327-
ncfg.Routing = node.DHTClientOption
327+
ncfg.Routing = libp2p.DHTClientOption
328328
case routingOptionDHTKwd:
329-
ncfg.Routing = node.DHTOption
329+
ncfg.Routing = libp2p.DHTOption
330330
case routingOptionNoneKwd:
331-
ncfg.Routing = node.NilRouterOption
331+
ncfg.Routing = libp2p.NilRouterOption
332332
default:
333333
return fmt.Errorf("unrecognized routing option: %s", routingOption)
334334
}

core/core.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
version "github.com/ipfs/go-ipfs"
1919
"github.com/ipfs/go-ipfs/core/bootstrap"
2020
"github.com/ipfs/go-ipfs/core/node"
21+
"github.com/ipfs/go-ipfs/core/node/libp2p"
2122
rp "github.com/ipfs/go-ipfs/exchange/reprovide"
2223
"github.com/ipfs/go-ipfs/filestore"
2324
"github.com/ipfs/go-ipfs/fuse/mount"
@@ -68,10 +69,10 @@ type IpfsNode struct {
6869
Repo repo.Repo
6970

7071
// Local node
71-
Pinning pin.Pinner // the pinning manager
72-
Mounts Mounts `optional:"true"` // current mount state, if any.
73-
PrivateKey ic.PrivKey // the local node's private Key
74-
PNetFingerprint node.PNetFingerprint `optional:"true"` // fingerprint of private network
72+
Pinning pin.Pinner // the pinning manager
73+
Mounts Mounts `optional:"true"` // current mount state, if any.
74+
PrivateKey ic.PrivKey // the local node's private Key
75+
PNetFingerprint libp2p.PNetFingerprint `optional:"true"` // fingerprint of private network
7576

7677
// Services
7778
Peerstore pstore.Peerstore `optional:"true"` // storage for other Peer instances

core/mock/mock.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@ package coremock
22

33
import (
44
"context"
5+
libp2p2 "github.com/ipfs/go-ipfs/core/node/libp2p"
56

6-
commands "github.com/ipfs/go-ipfs/commands"
7-
core "github.com/ipfs/go-ipfs/core"
8-
"github.com/ipfs/go-ipfs/core/node"
7+
"github.com/ipfs/go-ipfs/commands"
8+
"github.com/ipfs/go-ipfs/core"
99
"github.com/ipfs/go-ipfs/repo"
1010

11-
datastore "github.com/ipfs/go-datastore"
11+
"github.com/ipfs/go-datastore"
1212
syncds "github.com/ipfs/go-datastore/sync"
1313
config "github.com/ipfs/go-ipfs-config"
14-
libp2p "github.com/libp2p/go-libp2p"
14+
"github.com/libp2p/go-libp2p"
1515
host "github.com/libp2p/go-libp2p-host"
1616
peer "github.com/libp2p/go-libp2p-peer"
1717
pstore "github.com/libp2p/go-libp2p-peerstore"
1818
mocknet "github.com/libp2p/go-libp2p/p2p/net/mock"
19-
testutil "github.com/libp2p/go-testutil"
19+
"github.com/libp2p/go-testutil"
2020
)
2121

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

33-
func MockHostOption(mn mocknet.Mocknet) node.HostOption {
33+
func MockHostOption(mn mocknet.Mocknet) libp2p2.HostOption {
3434
return func(ctx context.Context, id peer.ID, ps pstore.Peerstore, _ ...libp2p.Option) (host.Host, error) {
3535
return mn.AddPeerWithPeerstore(id, ps)
3636
}

core/node/builder.go

+13-8
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,20 @@ import (
88

99
"go.uber.org/fx"
1010

11+
"github.com/ipfs/go-ipfs/core/node/helpers"
12+
"github.com/ipfs/go-ipfs/core/node/libp2p"
1113
"github.com/ipfs/go-ipfs/repo"
1214

1315
ds "github.com/ipfs/go-datastore"
1416
dsync "github.com/ipfs/go-datastore/sync"
1517
cfg "github.com/ipfs/go-ipfs-config"
18+
logging "github.com/ipfs/go-log"
1619
ci "github.com/libp2p/go-libp2p-crypto"
1720
peer "github.com/libp2p/go-libp2p-peer"
1821
)
1922

23+
var log = logging.Logger("node")
24+
2025
type BuildCfg struct {
2126
// If online is set, the node will have networking enabled
2227
Online bool
@@ -35,8 +40,8 @@ type BuildCfg struct {
3540
// If NilRepo is set, a Repo backed by a nil datastore will be constructed
3641
NilRepo bool
3742

38-
Routing RoutingOption
39-
Host HostOption
43+
Routing libp2p.RoutingOption
44+
Host libp2p.HostOption
4045
Repo repo.Repo
4146
}
4247

@@ -68,11 +73,11 @@ func (cfg *BuildCfg) fillDefaults() error {
6873
}
6974

7075
if cfg.Routing == nil {
71-
cfg.Routing = DHTOption
76+
cfg.Routing = libp2p.DHTOption
7277
}
7378

7479
if cfg.Host == nil {
75-
cfg.Host = DefaultHostOption
80+
cfg.Host = libp2p.DefaultHostOption
7681
}
7782

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

97-
metricsCtx := fx.Provide(func() MetricsCtx {
98-
return MetricsCtx(ctx)
102+
metricsCtx := fx.Provide(func() helpers.MetricsCtx {
103+
return helpers.MetricsCtx(ctx)
99104
})
100105

101-
hostOption := fx.Provide(func() HostOption {
106+
hostOption := fx.Provide(func() libp2p.HostOption {
102107
return cfg.Host
103108
})
104109

105-
routingOption := fx.Provide(func() RoutingOption {
110+
routingOption := fx.Provide(func() libp2p.RoutingOption {
106111
return cfg.Routing
107112
})
108113

core/node/core.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66

7+
"github.com/ipfs/go-ipfs/core/node/helpers"
78
"github.com/ipfs/go-ipfs/pin"
89
"github.com/ipfs/go-ipfs/repo"
910

@@ -54,9 +55,9 @@ func DagCtor(bs blockservice.BlockService) format.DAGService {
5455
return merkledag.NewDAGService(bs)
5556
}
5657

57-
func OnlineExchangeCtor(mctx MetricsCtx, lc fx.Lifecycle, host host.Host, rt routing.IpfsRouting, bs blockstore.GCBlockstore) exchange.Interface {
58+
func OnlineExchangeCtor(mctx helpers.MetricsCtx, lc fx.Lifecycle, host host.Host, rt routing.IpfsRouting, bs blockstore.GCBlockstore) exchange.Interface {
5859
bitswapNetwork := network.NewFromIpfsHost(host, rt)
59-
exch := bitswap.New(lifecycleCtx(mctx, lc), bitswapNetwork, bs)
60+
exch := bitswap.New(helpers.LifecycleCtx(mctx, lc), bitswapNetwork, bs)
6061
lc.Append(fx.Hook{
6162
OnStop: func(ctx context.Context) error {
6263
return exch.Close()
@@ -65,15 +66,15 @@ func OnlineExchangeCtor(mctx MetricsCtx, lc fx.Lifecycle, host host.Host, rt rou
6566
return exch
6667
}
6768

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

7475
var nd *merkledag.ProtoNode
7576
val, err := repo.Datastore().Get(dsk)
76-
ctx := lifecycleCtx(mctx, lc)
77+
ctx := helpers.LifecycleCtx(mctx, lc)
7778

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

117-
type MetricsCtx context.Context

core/node/groups.go

+27-26
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package node
33
import (
44
"context"
55

6+
"github.com/ipfs/go-ipfs/core/node/libp2p"
67
"github.com/ipfs/go-ipfs/p2p"
78
"github.com/ipfs/go-ipfs/provider"
89

@@ -13,38 +14,38 @@ import (
1314
)
1415

1516
var BaseLibP2P = fx.Options(
16-
fx.Provide(P2PAddrFilters),
17-
fx.Provide(P2PBandwidthCounter),
18-
fx.Provide(P2PPNet),
19-
fx.Provide(P2PAddrsFactory),
20-
fx.Provide(P2PConnectionManager),
21-
fx.Provide(P2PNatPortMap),
22-
fx.Provide(P2PRelay),
23-
fx.Provide(P2PAutoRealy),
24-
fx.Provide(P2PDefaultTransports),
25-
fx.Provide(P2PQUIC),
26-
27-
fx.Provide(P2PHost),
28-
29-
fx.Provide(NewDiscoveryHandler),
30-
31-
fx.Invoke(AutoNATService),
32-
fx.Invoke(P2PPNetChecker),
33-
fx.Invoke(StartListening),
34-
fx.Invoke(SetupDiscovery),
17+
fx.Provide(libp2p.P2PAddrFilters),
18+
fx.Provide(libp2p.P2PBandwidthCounter),
19+
fx.Provide(libp2p.P2PPNet),
20+
fx.Provide(libp2p.P2PAddrsFactory),
21+
fx.Provide(libp2p.P2PConnectionManager),
22+
fx.Provide(libp2p.P2PNatPortMap),
23+
fx.Provide(libp2p.P2PRelay),
24+
fx.Provide(libp2p.P2PAutoRealy),
25+
fx.Provide(libp2p.P2PDefaultTransports),
26+
fx.Provide(libp2p.P2PQUIC),
27+
28+
fx.Provide(libp2p.P2PHost),
29+
30+
fx.Provide(libp2p.NewDiscoveryHandler),
31+
32+
fx.Invoke(libp2p.AutoNATService),
33+
fx.Invoke(libp2p.P2PPNetChecker),
34+
fx.Invoke(libp2p.StartListening),
35+
fx.Invoke(libp2p.SetupDiscovery),
3536
)
3637

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

41-
fx.Provide(P2PSecurity(!cfg.DisableEncryptedConnections)),
42-
maybeProvide(Pubsub, cfg.getOpt("pubsub") || cfg.getOpt("ipnsps")),
42+
fx.Provide(libp2p.P2PSecurity(!cfg.DisableEncryptedConnections)),
43+
maybeProvide(libp2p.Pubsub, cfg.getOpt("pubsub") || cfg.getOpt("ipnsps")),
4344

44-
fx.Provide(P2PSmuxTransport(cfg.getOpt("mplex"))),
45-
fx.Provide(P2PRouting),
46-
fx.Provide(P2PBaseRouting),
47-
maybeProvide(P2PPubsubRouter, cfg.getOpt("ipnsps")),
45+
fx.Provide(libp2p.P2PSmuxTransport(cfg.getOpt("mplex"))),
46+
fx.Provide(libp2p.P2PRouting),
47+
fx.Provide(libp2p.P2PBaseRouting),
48+
maybeProvide(libp2p.P2PPubsubRouter, cfg.getOpt("ipnsps")),
4849
)
4950

5051
return opts
@@ -62,7 +63,7 @@ func Storage(cfg *BuildCfg) fx.Option {
6263
var Identity = fx.Options(
6364
fx.Provide(PeerID),
6465
fx.Provide(PrivateKey),
65-
fx.Provide(Peerstore),
66+
fx.Provide(libp2p.Peerstore),
6667
)
6768

6869
var IPNS = fx.Options(

core/node/helpers.go

-15
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,6 @@ import (
1010
"go.uber.org/fx"
1111
)
1212

13-
// lifecycleCtx creates a context which will be cancelled when lifecycle stops
14-
//
15-
// This is a hack which we need because most of our services use contexts in a
16-
// wrong way
17-
func lifecycleCtx(mctx MetricsCtx, lc fx.Lifecycle) context.Context {
18-
ctx, cancel := context.WithCancel(mctx)
19-
lc.Append(fx.Hook{
20-
OnStop: func(_ context.Context) error {
21-
cancel()
22-
return nil
23-
},
24-
})
25-
return ctx
26-
}
27-
2813
type lcProcess struct {
2914
fx.In
3015

core/node/helpers/helpers.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package helpers
2+
3+
import (
4+
"context"
5+
"go.uber.org/fx"
6+
)
7+
8+
type MetricsCtx context.Context
9+
10+
// LifecycleCtx creates a context which will be cancelled when lifecycle stops
11+
//
12+
// This is a hack which we need because most of our services use contexts in a
13+
// wrong way
14+
func LifecycleCtx(mctx MetricsCtx, lc fx.Lifecycle) context.Context {
15+
ctx, cancel := context.WithCancel(mctx)
16+
lc.Append(fx.Hook{
17+
OnStop: func(_ context.Context) error {
18+
cancel()
19+
return nil
20+
},
21+
})
22+
return ctx
23+
}

core/node/discovery.go core/node/libp2p/discovery.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
package node
1+
package libp2p
22

33
import (
44
"context"
55
"time"
66

77
"github.com/ipfs/go-ipfs-config"
8+
"github.com/ipfs/go-ipfs/core/node/helpers"
89
"github.com/libp2p/go-libp2p-host"
910
"github.com/libp2p/go-libp2p-peerstore"
1011
"github.com/libp2p/go-libp2p/p2p/discovery"
@@ -27,20 +28,20 @@ func (dh *discoveryHandler) HandlePeerFound(p peerstore.PeerInfo) {
2728
}
2829
}
2930

30-
func NewDiscoveryHandler(mctx MetricsCtx, lc fx.Lifecycle, host host.Host) *discoveryHandler {
31+
func NewDiscoveryHandler(mctx helpers.MetricsCtx, lc fx.Lifecycle, host host.Host) *discoveryHandler {
3132
return &discoveryHandler{
32-
ctx: lifecycleCtx(mctx, lc),
33+
ctx: helpers.LifecycleCtx(mctx, lc),
3334
host: host,
3435
}
3536
}
3637

37-
func SetupDiscovery(mctx MetricsCtx, lc fx.Lifecycle, cfg *config.Config, host host.Host, handler *discoveryHandler) error {
38+
func SetupDiscovery(mctx helpers.MetricsCtx, lc fx.Lifecycle, cfg *config.Config, host host.Host, handler *discoveryHandler) error {
3839
if cfg.Discovery.MDNS.Enabled {
3940
mdns := cfg.Discovery.MDNS
4041
if mdns.Interval == 0 {
4142
mdns.Interval = 5
4243
}
43-
service, err := discovery.NewMdnsService(lifecycleCtx(mctx, lc), host, time.Duration(mdns.Interval)*time.Second, discovery.ServiceTag)
44+
service, err := discovery.NewMdnsService(helpers.LifecycleCtx(mctx, lc), host, time.Duration(mdns.Interval)*time.Second, discovery.ServiceTag)
4445
if err != nil {
4546
log.Error("mdns error: ", err)
4647
return nil

0 commit comments

Comments
 (0)