Remove DI module dependency on BuildCfg
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
Łukasz Magiera committed
Apr 3, 2019 at 16:13 UTC
fd0c06a825f52be86f4b8a9f9ed2a1b6ac68920e
4 files changed
+141
-124
core/builder.go
+12
-8
@@ -105,18 +105,22 @@ func NewNode(ctx context.Context, cfg *BuildCfg) (*IpfsNode, error) {
105
return cfg.Repo
106
})
107
108
- // TODO: Remove this, use only for passing node config
109
- cfgOption := fx.Provide(func() *node.BuildCfg {
110
- return (*node.BuildCfg)(cfg)
111
- })
112
-
108
metricsCtx := fx.Provide(func() node.MetricsCtx {
109
return node.MetricsCtx(ctx)
110
})
111
112
+ hostOption := fx.Provide(func() node.HostOption {
113
+ return cfg.Host
114
+ })
115
+
116
+ routingOption := fx.Provide(func() node.RoutingOption {
117
+ return cfg.Routing
118
+ })
119
+
120
params := fx.Options(
121
repoOption,
119
- cfgOption,
122
+ hostOption,
123
+ routingOption,
124
metricsCtx,
125
)
126
@@ -137,10 +141,10 @@ func NewNode(ctx context.Context, cfg *BuildCfg) (*IpfsNode, error) {
141
fx.Provide(baseProcess),
142
143
params,
140
- node.Storage,
144
+ node.Storage((*node.BuildCfg)(cfg)),
145
node.Identity,
146
node.IPNS,
143
- node.Networked(cfg.Online),
147
+ node.Networked((*node.BuildCfg)(cfg)),
148
149
fx.Invoke(setupSharding),
150
core/node/groups.go
+41
-23
@@ -9,24 +9,20 @@ import (
9
"github.com/ipfs/go-ipfs/provider"
10
)
11
12
-var LibP2P = fx.Options(
12
+var BaseLibP2P = fx.Options(
13
fx.Provide(P2PAddrFilters),
14
fx.Provide(P2PBandwidthCounter),
15
fx.Provide(P2PPNet),
16
fx.Provide(P2PAddrsFactory),
17
fx.Provide(P2PConnectionManager),
18
- fx.Provide(P2PSmuxTransport),
18
fx.Provide(P2PNatPortMap),
19
fx.Provide(P2PRelay),
20
fx.Provide(P2PAutoRealy),
21
fx.Provide(P2PDefaultTransports),
22
fx.Provide(P2PQUIC),
23
25
- fx.Provide(P2PHostOption),
24
fx.Provide(P2PHost),
27
- fx.Provide(P2POnlineRouting),
25
29
- fx.Provide(Pubsub),
26
fx.Provide(NewDiscoveryHandler),
27
28
fx.Invoke(AutoNATService),
@@ -35,12 +31,26 @@ var LibP2P = fx.Options(
31
fx.Invoke(SetupDiscovery),
32
)
33
38
-var Storage = fx.Options(
39
- fx.Provide(RepoConfig),
40
- fx.Provide(DatastoreCtor),
41
- fx.Provide(BaseBlockstoreCtor),
42
- fx.Provide(GcBlockstoreCtor),
43
-)
34
+func LibP2P(cfg *BuildCfg) fx.Option {
35
+ return fx.Options(
36
+ BaseLibP2P,
37
+
38
+ MaybeProvide(P2PNoSecurity, cfg.DisableEncryptedConnections),
39
+ MaybeProvide(Pubsub, cfg.getOpt("pubsub") || cfg.getOpt("ipnsps")),
40
+
41
+ fx.Provide(P2PSmuxTransport(cfg.getOpt("mplex"))),
42
+ fx.Provide(P2POnlineRouting(cfg.getOpt("ipnsps"))),
43
+ )
44
+}
45
+
46
+func Storage(cfg *BuildCfg) fx.Option {
47
+ return fx.Options(
48
+ fx.Provide(RepoConfig),
49
+ fx.Provide(DatastoreCtor),
50
+ fx.Provide(BaseBlockstoreCtor(cfg.Permanent, cfg.NilRepo)),
51
+ fx.Provide(GcBlockstoreCtor),
52
+ )
53
+}
54
55
var Identity = fx.Options(
56
fx.Provide(PeerID),
@@ -61,18 +71,19 @@ var Providers = fx.Options(
71
fx.Invoke(provider.Provider.Run),
72
)
73
64
-var Online = fx.Options(
65
- fx.Provide(OnlineExchangeCtor),
66
- fx.Provide(OnlineNamesysCtor),
74
+func Online(cfg *BuildCfg) fx.Option {
75
+ return fx.Options(
76
+ fx.Provide(OnlineExchangeCtor),
77
+ fx.Provide(OnlineNamesysCtor),
78
68
- fx.Invoke(IpnsRepublisher),
79
+ fx.Invoke(IpnsRepublisher),
80
70
- fx.Provide(p2p.NewP2P),
71
-
72
- LibP2P,
73
- Providers,
74
-)
81
+ fx.Provide(p2p.NewP2P),
82
83
+ LibP2P(cfg),
84
+ Providers,
85
+ )
86
+}
87
var Offline = fx.Options(
88
fx.Provide(offline.Exchange),
89
fx.Provide(OfflineNamesysCtor),
@@ -80,9 +91,16 @@ var Offline = fx.Options(
91
fx.Provide(provider.NewOfflineProvider),
92
)
93
83
-func Networked(online bool) fx.Option {
84
- if online {
85
- return Online
94
+func Networked(cfg *BuildCfg) fx.Option {
95
+ if cfg.Online {
96
+ return Online(cfg)
97
}
98
return Offline
99
}
100
+
101
+func MaybeProvide(opt interface{}, enable bool) fx.Option {
102
+ if enable {
103
+ return fx.Provide(opt)
104
+ }
105
+ return fx.Options()
106
+}
core/node/libp2p.go
+51
-58
@@ -320,9 +320,11 @@ func makeSmuxTransportOption(mplexExp bool) libp2p.Option {
320
return libp2p.ChainOptions(opts...)
321
}
322
323
-func P2PSmuxTransport(bcfg *BuildCfg) (opts Libp2pOpts, err error) {
324
- opts.Opts = append(opts.Opts, makeSmuxTransportOption(bcfg.getOpt("mplex")))
325
- return
323
+func P2PSmuxTransport(mplex bool) func() (opts Libp2pOpts, err error) {
324
+ return func() (opts Libp2pOpts, err error) {
325
+ opts.Opts = append(opts.Opts, makeSmuxTransportOption(mplex))
326
+ return
327
+ }
328
}
329
330
func P2PNatPortMap(cfg *config.Config) (opts Libp2pOpts, err error) {
@@ -366,15 +368,23 @@ func P2PQUIC(cfg *config.Config) (opts Libp2pOpts, err error) {
368
return
369
}
370
371
+func P2PNoSecurity() (opts Libp2pOpts) {
372
+ opts.Opts = append(opts.Opts, libp2p.NoSecurity)
373
+ // TODO: shouldn't this be Errorf to guarantee visibility?
374
+ log.Warningf(`Your IPFS node has been configured to run WITHOUT ENCRYPTED CONNECTIONS.
375
+ You will not be able to connect to any nodes configured to use encrypted connections`)
376
+ return opts
377
+}
378
+
379
type P2PHostIn struct {
380
fx.In
381
372
- BCfg *BuildCfg
373
- Repo repo.Repo
374
- Validator record.Validator
375
- HostOption HostOption
376
- ID peer.ID
377
- Peerstore peerstore.Peerstore
382
+ Repo repo.Repo
383
+ Validator record.Validator
384
+ HostOption HostOption
385
+ RoutingOption RoutingOption
386
+ ID peer.ID
387
+ Peerstore peerstore.Peerstore
388
389
Opts [][]libp2p.Option `group:"libp2p"`
390
}
@@ -404,7 +414,7 @@ func P2PHost(mctx MetricsCtx, lc fx.Lifecycle, params P2PHostIn) (out P2PHostOut
414
})
415
416
opts = append(opts, libp2p.Routing(func(h host.Host) (routing.PeerRouting, error) {
407
- r, err := params.BCfg.Routing(ctx, h, params.Repo.Datastore(), params.Validator)
417
+ r, err := params.RoutingOption(ctx, h, params.Repo.Datastore(), params.Validator)
418
out.Routing = r
419
return r, err
420
}))
@@ -417,7 +427,7 @@ func P2PHost(mctx MetricsCtx, lc fx.Lifecycle, params P2PHostIn) (out P2PHostOut
427
// this code is necessary just for tests: mock network constructions
428
// ignore the libp2p constructor options that actually construct the routing!
429
if out.Routing == nil {
420
- r, err := params.BCfg.Routing(ctx, out.Host, params.Repo.Datastore(), params.Validator)
430
+ r, err := params.RoutingOption(ctx, out.Host, params.Repo.Datastore(), params.Validator)
431
if err != nil {
432
return P2PHostOut{}, err
433
}
@@ -461,11 +471,10 @@ func P2PHost(mctx MetricsCtx, lc fx.Lifecycle, params P2PHostIn) (out P2PHostOut
471
type p2pRoutingIn struct {
472
fx.In
473
464
- BCfg *BuildCfg
474
Repo repo.Repo
475
Validator record.Validator
476
Host host.Host
468
- PubSub *pubsub.PubSub
477
+ PubSub *pubsub.PubSub `optional:"true"`
478
479
BaseRouting BaseRouting
480
}
@@ -474,36 +483,38 @@ type p2pRoutingOut struct {
483
fx.Out
484
485
IpfsRouting routing.IpfsRouting
477
- PSRouter *namesys.PubsubValueStore // TODO: optional
478
-}
479
-
480
-func P2POnlineRouting(mctx MetricsCtx, lc fx.Lifecycle, in p2pRoutingIn) (out p2pRoutingOut) {
481
- out.IpfsRouting = in.BaseRouting
482
-
483
- if in.BCfg.getOpt("ipnsps") {
484
- out.PSRouter = namesys.NewPubsubValueStore(
485
- lifecycleCtx(mctx, lc),
486
- in.Host,
487
- in.BaseRouting,
488
- in.PubSub,
489
- in.Validator,
490
- )
491
-
492
- out.IpfsRouting = routinghelpers.Tiered{
493
- Routers: []routing.IpfsRouting{
494
- // Always check pubsub first.
495
- &routinghelpers.Compose{
496
- ValueStore: &routinghelpers.LimitedValueStore{
497
- ValueStore: out.PSRouter,
498
- Namespaces: []string{"ipns"},
486
+ PSRouter *namesys.PubsubValueStore
487
+}
488
+
489
+func P2POnlineRouting(ipnsps bool) func(mctx MetricsCtx, lc fx.Lifecycle, in p2pRoutingIn) (out p2pRoutingOut) {
490
+ return func(mctx MetricsCtx, lc fx.Lifecycle, in p2pRoutingIn) (out p2pRoutingOut) {
491
+ out.IpfsRouting = in.BaseRouting
492
+
493
+ if ipnsps {
494
+ out.PSRouter = namesys.NewPubsubValueStore(
495
+ lifecycleCtx(mctx, lc),
496
+ in.Host,
497
+ in.BaseRouting,
498
+ in.PubSub,
499
+ in.Validator,
500
+ )
501
+
502
+ out.IpfsRouting = routinghelpers.Tiered{
503
+ Routers: []routing.IpfsRouting{
504
+ // Always check pubsub first.
505
+ &routinghelpers.Compose{
506
+ ValueStore: &routinghelpers.LimitedValueStore{
507
+ ValueStore: out.PSRouter,
508
+ Namespaces: []string{"ipns"},
509
+ },
510
},
511
+ in.BaseRouting,
512
},
501
- in.BaseRouting,
502
- },
503
- Validator: in.Validator,
513
+ Validator: in.Validator,
514
+ }
515
}
516
+ return out
517
}
506
- return out
518
}
519
520
func AutoNATService(mctx MetricsCtx, lc fx.Lifecycle, cfg *config.Config, host host.Host) error {
@@ -519,11 +530,7 @@ func AutoNATService(mctx MetricsCtx, lc fx.Lifecycle, cfg *config.Config, host h
530
return err
531
}
532
522
-func Pubsub(mctx MetricsCtx, lc fx.Lifecycle, host host.Host, bcfg *BuildCfg, cfg *config.Config) (service *pubsub.PubSub, err error) {
523
- if !(bcfg.getOpt("pubsub") || bcfg.getOpt("ipnsps")) {
524
- return nil, nil // TODO: mark optional
525
- }
526
-
533
+func Pubsub(mctx MetricsCtx, lc fx.Lifecycle, host host.Host, cfg *config.Config) (service *pubsub.PubSub, err error) {
534
var pubsubOptions []pubsub.Option
535
if cfg.Pubsub.DisableSigning {
536
pubsubOptions = append(pubsubOptions, pubsub.WithMessageSigning(false))
@@ -581,17 +588,3 @@ func StartListening(host host.Host, cfg *config.Config) error {
588
log.Infof("Swarm listening at: %s", addrs)
589
return nil
590
}
584
-
585
-func P2PHostOption(bcfg *BuildCfg) (hostOption HostOption, err error) {
586
- hostOption = bcfg.Host
587
- if bcfg.DisableEncryptedConnections {
588
- innerHostOption := hostOption
589
- hostOption = func(ctx context.Context, id peer.ID, ps peerstore.Peerstore, options ...libp2p.Option) (host.Host, error) {
590
- return innerHostOption(ctx, id, ps, append(options, libp2p.NoSecurity)...)
591
- }
592
- // TODO: shouldn't this be Errorf to guarantee visibility?
593
- log.Warningf(`Your IPFS node has been configured to run WITHOUT ENCRYPTED CONNECTIONS.
594
- You will not be able to connect to any nodes configured to use encrypted connections`)
595
- }
596
- return hostOption, nil
597
-}
core/node/storage.go
+37
-35
@@ -37,46 +37,48 @@ func DatastoreCtor(repo repo.Repo) datastore.Datastore {
37
38
type BaseBlocks blockstore.Blockstore
39
40
-func BaseBlockstoreCtor(mctx MetricsCtx, repo repo.Repo, cfg *config.Config, bcfg *BuildCfg, lc fx.Lifecycle) (bs BaseBlocks, err error) {
41
- rds := &retrystore.Datastore{
42
- Batching: repo.Datastore(),
43
- Delay: time.Millisecond * 200,
44
- Retries: 6,
45
- TempErrFunc: isTooManyFDError,
46
- }
47
- // hash security
48
- bs = blockstore.NewBlockstore(rds)
49
- bs = &verifbs.VerifBS{Blockstore: bs}
50
-
51
- opts := blockstore.DefaultCacheOpts()
52
- opts.HasBloomFilterSize = cfg.Datastore.BloomFilterSize
53
- if !bcfg.Permanent {
54
- opts.HasBloomFilterSize = 0
55
- }
40
+func BaseBlockstoreCtor(permanent bool, nilRepo bool) func(mctx MetricsCtx, repo repo.Repo, cfg *config.Config, lc fx.Lifecycle) (bs BaseBlocks, err error) {
41
+ return func(mctx MetricsCtx, repo repo.Repo, cfg *config.Config, lc fx.Lifecycle) (bs BaseBlocks, err error) {
42
+ rds := &retrystore.Datastore{
43
+ Batching: repo.Datastore(),
44
+ Delay: time.Millisecond * 200,
45
+ Retries: 6,
46
+ TempErrFunc: isTooManyFDError,
47
+ }
48
+ // hash security
49
+ bs = blockstore.NewBlockstore(rds)
50
+ bs = &verifbs.VerifBS{Blockstore: bs}
51
57
- if !bcfg.NilRepo {
58
- ctx, cancel := context.WithCancel(mctx)
59
-
60
- lc.Append(fx.Hook{
61
- OnStop: func(context context.Context) error {
62
- cancel()
63
- return nil
64
- },
65
- })
66
- bs, err = blockstore.CachedBlockstore(ctx, bs, opts)
67
- if err != nil {
68
- return nil, err
52
+ opts := blockstore.DefaultCacheOpts()
53
+ opts.HasBloomFilterSize = cfg.Datastore.BloomFilterSize
54
+ if !permanent {
55
+ opts.HasBloomFilterSize = 0
56
}
70
- }
57
72
- bs = blockstore.NewIdStore(bs)
73
- bs = cidv0v1.NewBlockstore(bs)
58
+ if !nilRepo {
59
+ ctx, cancel := context.WithCancel(mctx)
60
+
61
+ lc.Append(fx.Hook{
62
+ OnStop: func(context context.Context) error {
63
+ cancel()
64
+ return nil
65
+ },
66
+ })
67
+ bs, err = blockstore.CachedBlockstore(ctx, bs, opts)
68
+ if err != nil {
69
+ return nil, err
70
+ }
71
+ }
72
75
- if cfg.Datastore.HashOnRead { // TODO: review: this is how it was done originally, is there a reason we can't just pass this directly?
76
- bs.HashOnRead(true)
77
- }
73
+ bs = blockstore.NewIdStore(bs)
74
+ bs = cidv0v1.NewBlockstore(bs)
75
79
- return
76
+ if cfg.Datastore.HashOnRead { // TODO: review: this is how it was done originally, is there a reason we can't just pass this directly?
77
+ bs.HashOnRead(true)
78
+ }
79
+
80
+ return
81
+ }
82
}
83
84
func GcBlockstoreCtor(repo repo.Repo, bb BaseBlocks, cfg *config.Config) (gclocker blockstore.GCLocker, gcbs blockstore.GCBlockstore, bs blockstore.Blockstore, fstore *filestore.Filestore) {