@cryptotaxi247 / kubo / commits / 3ac605744

Separate function to parse BuildCfg into Options

License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>

Łukasz Magiera committed Apr 3, 2019 at 16:34 UTC 3ac605744fc41c29e4e4fc85d3dec395df9f7f48
6 files changed +164 -168
core/builder.go
+3 -132
@@ -2,154 +2,25 @@ package core
2
3 import (
4 "context"
5 - "crypto/rand"
6 - "encoding/base64"
7 - "errors"
5
6 "go.uber.org/fx"
7
8 "github.com/ipfs/go-ipfs/core/bootstrap"
9 "github.com/ipfs/go-ipfs/core/node"
13 -
14 - repo "github.com/ipfs/go-ipfs/repo"
15 -
16 - ds "github.com/ipfs/go-datastore"
17 - dsync "github.com/ipfs/go-datastore/sync"
18 - cfg "github.com/ipfs/go-ipfs-config"
19 - metrics "github.com/ipfs/go-metrics-interface"
20 - resolver "github.com/ipfs/go-path/resolver"
21 - ci "github.com/libp2p/go-libp2p-crypto"
22 - peer "github.com/libp2p/go-libp2p-peer"
10 )
11
25 -type BuildCfg node.BuildCfg
26 -
27 -func (cfg *BuildCfg) fillDefaults() error {
28 - if cfg.Repo != nil && cfg.NilRepo {
29 - return errors.New("cannot set a Repo and specify nilrepo at the same time")
30 - }
31 -
32 - if cfg.Repo == nil {
33 - var d ds.Datastore
34 - if cfg.NilRepo {
35 - d = ds.NewNullDatastore()
36 - } else {
37 - d = ds.NewMapDatastore()
38 - }
39 - r, err := defaultRepo(dsync.MutexWrap(d))
40 - if err != nil {
41 - return err
42 - }
43 - cfg.Repo = r
44 - }
45 -
46 - if cfg.Routing == nil {
47 - cfg.Routing = node.DHTOption
48 - }
49 -
50 - if cfg.Host == nil {
51 - cfg.Host = node.DefaultHostOption
52 - }
53 -
54 - return nil
55 -}
56 -
57 -func defaultRepo(dstore repo.Datastore) (repo.Repo, error) {
58 - c := cfg.Config{}
59 - priv, pub, err := ci.GenerateKeyPairWithReader(ci.RSA, 1024, rand.Reader)
60 - if err != nil {
61 - return nil, err
62 - }
63 -
64 - pid, err := peer.IDFromPublicKey(pub)
65 - if err != nil {
66 - return nil, err
67 - }
68 -
69 - privkeyb, err := priv.Bytes()
70 - if err != nil {
71 - return nil, err
72 - }
73 -
74 - c.Bootstrap = cfg.DefaultBootstrapAddresses
75 - c.Addresses.Swarm = []string{"/ip4/0.0.0.0/tcp/4001"}
76 - c.Identity.PeerID = pid.Pretty()
77 - c.Identity.PrivKey = base64.StdEncoding.EncodeToString(privkeyb)
78 -
79 - return &repo.Mock{
80 - D: dstore,
81 - C: c,
82 - }, nil
83 -}
12 +type BuildCfg = node.BuildCfg // Alias for compatibility until we properly refactor the constructor interface
13
14 // NewNode constructs and returns an IpfsNode using the given cfg.
15 func NewNode(ctx context.Context, cfg *BuildCfg) (*IpfsNode, error) {
87 - if cfg == nil {
88 - cfg = new(BuildCfg)
89 - }
90 -
91 - err := cfg.fillDefaults()
92 - if err != nil {
93 - return nil, err
94 - }
95 -
96 - ctx = metrics.CtxScope(ctx, "ipfs")
97 -
98 - repoOption := fx.Provide(func(lc fx.Lifecycle) repo.Repo {
99 - lc.Append(fx.Hook{
100 - OnStop: func(ctx context.Context) error {
101 - return cfg.Repo.Close()
102 - },
103 - })
104 -
105 - return cfg.Repo
106 - })
107 -
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,
122 - hostOption,
123 - routingOption,
124 - metricsCtx,
125 - )
126 -
127 - core := fx.Options(
128 - fx.Provide(node.BlockServiceCtor),
129 - fx.Provide(node.DagCtor),
130 - fx.Provide(resolver.NewBasicResolver),
131 - fx.Provide(node.Pinning),
132 - fx.Provide(node.Files),
133 - )
134 -
16 n := &IpfsNode{
17 ctx: ctx,
18 }
19
20 app := fx.New(
140 - fx.NopLogger,
141 - fx.Provide(baseProcess),
142 -
143 - params,
144 - node.Storage((*node.BuildCfg)(cfg)),
145 - node.Identity,
146 - node.IPNS,
147 - node.Networked((*node.BuildCfg)(cfg)),
148 -
149 - fx.Invoke(setupSharding),
150 -
151 - core,
21 + node.IPFS(ctx, cfg),
22
23 + fx.NopLogger,
24 fx.Extract(n),
25 )
26
core/ncore.go deleted
-29
@@ -1,29 +0,0 @@
1 -package core
2 -
3 -import (
4 - "context"
5 -
6 - "github.com/jbenet/goprocess"
7 - "go.uber.org/fx"
8 -
9 - iconfig "github.com/ipfs/go-ipfs-config"
10 - uio "github.com/ipfs/go-unixfs/io"
11 -)
12 -
13 -////////////////////
14 -// libp2p
15 -
16 -func setupSharding(cfg *iconfig.Config) {
17 - // TEMP: setting global sharding switch here
18 - uio.UseHAMTSharding = cfg.Experimental.ShardingEnabled
19 -}
20 -
21 -func baseProcess(lc fx.Lifecycle) goprocess.Process {
22 - p := goprocess.WithParent(goprocess.Background())
23 - lc.Append(fx.Hook{
24 - OnStop: func(_ context.Context) error {
25 - return p.Close()
26 - },
27 - })
28 - return p
29 -}
core/node/builder.go
+68
@@ -1,6 +1,16 @@
1 package node
2
3 import (
4 + "crypto/rand"
5 + "encoding/base64"
6 + "errors"
7 +
8 + ds "github.com/ipfs/go-datastore"
9 + dsync "github.com/ipfs/go-datastore/sync"
10 + cfg "github.com/ipfs/go-ipfs-config"
11 + ci "github.com/libp2p/go-libp2p-crypto"
12 + peer "github.com/libp2p/go-libp2p-peer"
13 +
14 "github.com/ipfs/go-ipfs/repo"
15 )
16
@@ -34,3 +44,61 @@ func (cfg *BuildCfg) getOpt(key string) bool {
44
45 return cfg.ExtraOpts[key]
46 }
47 +
48 +func (cfg *BuildCfg) fillDefaults() error {
49 + if cfg.Repo != nil && cfg.NilRepo {
50 + return errors.New("cannot set a Repo and specify nilrepo at the same time")
51 + }
52 +
53 + if cfg.Repo == nil {
54 + var d ds.Datastore
55 + if cfg.NilRepo {
56 + d = ds.NewNullDatastore()
57 + } else {
58 + d = ds.NewMapDatastore()
59 + }
60 + r, err := defaultRepo(dsync.MutexWrap(d))
61 + if err != nil {
62 + return err
63 + }
64 + cfg.Repo = r
65 + }
66 +
67 + if cfg.Routing == nil {
68 + cfg.Routing = DHTOption
69 + }
70 +
71 + if cfg.Host == nil {
72 + cfg.Host = DefaultHostOption
73 + }
74 +
75 + return nil
76 +}
77 +
78 +func defaultRepo(dstore repo.Datastore) (repo.Repo, error) {
79 + c := cfg.Config{}
80 + priv, pub, err := ci.GenerateKeyPairWithReader(ci.RSA, 1024, rand.Reader)
81 + if err != nil {
82 + return nil, err
83 + }
84 +
85 + pid, err := peer.IDFromPublicKey(pub)
86 + if err != nil {
87 + return nil, err
88 + }
89 +
90 + privkeyb, err := priv.Bytes()
91 + if err != nil {
92 + return nil, err
93 + }
94 +
95 + c.Bootstrap = cfg.DefaultBootstrapAddresses
96 + c.Addresses.Swarm = []string{"/ip4/0.0.0.0/tcp/4001"}
97 + c.Identity.PeerID = pid.Pretty()
98 + c.Identity.PrivKey = base64.StdEncoding.EncodeToString(privkeyb)
99 +
100 + return &repo.Mock{
101 + D: dstore,
102 + C: c,
103 + }, nil
104 +}
core/node/groups.go
+68 -6
@@ -1,12 +1,17 @@
1 package node
2
3 import (
4 + "context"
5 +
6 offline "github.com/ipfs/go-ipfs-exchange-offline"
7 + "github.com/ipfs/go-metrics-interface"
8 + "github.com/ipfs/go-path/resolver"
9 "go.uber.org/fx"
10
11 offroute "github.com/ipfs/go-ipfs-routing/offline"
12 "github.com/ipfs/go-ipfs/p2p"
13 "github.com/ipfs/go-ipfs/provider"
14 + "github.com/ipfs/go-ipfs/repo"
15 )
16
17 var BaseLibP2P = fx.Options(
@@ -35,8 +40,8 @@ func LibP2P(cfg *BuildCfg) fx.Option {
40 return fx.Options(
41 BaseLibP2P,
42
38 - MaybeProvide(P2PNoSecurity, cfg.DisableEncryptedConnections),
39 - MaybeProvide(Pubsub, cfg.getOpt("pubsub") || cfg.getOpt("ipnsps")),
43 + maybeProvide(P2PNoSecurity, cfg.DisableEncryptedConnections),
44 + maybeProvide(Pubsub, cfg.getOpt("pubsub") || cfg.getOpt("ipnsps")),
45
46 fx.Provide(P2PSmuxTransport(cfg.getOpt("mplex"))),
47 fx.Provide(P2POnlineRouting(cfg.getOpt("ipnsps"))),
@@ -84,6 +89,7 @@ func Online(cfg *BuildCfg) fx.Option {
89 Providers,
90 )
91 }
92 +
93 var Offline = fx.Options(
94 fx.Provide(offline.Exchange),
95 fx.Provide(OfflineNamesysCtor),
@@ -91,6 +97,14 @@ var Offline = fx.Options(
97 fx.Provide(provider.NewOfflineProvider),
98 )
99
100 +var Core = fx.Options(
101 + fx.Provide(BlockServiceCtor),
102 + fx.Provide(DagCtor),
103 + fx.Provide(resolver.NewBasicResolver),
104 + fx.Provide(Pinning),
105 + fx.Provide(Files),
106 +)
107 +
108 func Networked(cfg *BuildCfg) fx.Option {
109 if cfg.Online {
110 return Online(cfg)
@@ -98,9 +112,57 @@ func Networked(cfg *BuildCfg) fx.Option {
112 return Offline
113 }
114
101 -func MaybeProvide(opt interface{}, enable bool) fx.Option {
102 - if enable {
103 - return fx.Provide(opt)
115 +func IPFS(ctx context.Context, cfg *BuildCfg) fx.Option {
116 + if cfg == nil {
117 + cfg = new(BuildCfg)
118 + }
119 +
120 + err := cfg.fillDefaults()
121 + if err != nil {
122 + return fx.Error(err)
123 }
105 - return fx.Options()
124 +
125 + ctx = metrics.CtxScope(ctx, "ipfs")
126 +
127 + repoOption := fx.Provide(func(lc fx.Lifecycle) repo.Repo {
128 + lc.Append(fx.Hook{
129 + OnStop: func(ctx context.Context) error {
130 + return cfg.Repo.Close()
131 + },
132 + })
133 +
134 + return cfg.Repo
135 + })
136 +
137 + metricsCtx := fx.Provide(func() MetricsCtx {
138 + return MetricsCtx(ctx)
139 + })
140 +
141 + hostOption := fx.Provide(func() HostOption {
142 + return cfg.Host
143 + })
144 +
145 + routingOption := fx.Provide(func() RoutingOption {
146 + return cfg.Routing
147 + })
148 +
149 + params := fx.Options(
150 + repoOption,
151 + hostOption,
152 + routingOption,
153 + metricsCtx,
154 + )
155 +
156 + return fx.Options(
157 + params,
158 + fx.Provide(baseProcess),
159 + fx.Invoke(setupSharding),
160 +
161 + Storage(cfg),
162 + Identity,
163 + IPNS,
164 + Networked(cfg),
165 +
166 + Core,
167 + )
168 }
core/node/helpers.go
+24
@@ -3,6 +3,8 @@ package node
3 import (
4 "context"
5
6 + config "github.com/ipfs/go-ipfs-config"
7 + uio "github.com/ipfs/go-unixfs/io"
8 "github.com/jbenet/goprocess"
9 "go.uber.org/fx"
10 )
@@ -41,3 +43,25 @@ func (lp *lcProcess) Run(f goprocess.ProcessFunc) {
43 },
44 })
45 }
46 +
47 +func maybeProvide(opt interface{}, enable bool) fx.Option {
48 + if enable {
49 + return fx.Provide(opt)
50 + }
51 + return fx.Options()
52 +}
53 +
54 +func setupSharding(cfg *config.Config) {
55 + // TEMP: setting global sharding switch here
56 + uio.UseHAMTSharding = cfg.Experimental.ShardingEnabled
57 +}
58 +
59 +func baseProcess(lc fx.Lifecycle) goprocess.Process {
60 + p := goprocess.WithParent(goprocess.Background())
61 + lc.Append(fx.Hook{
62 + OnStop: func(_ context.Context) error {
63 + return p.Close()
64 + },
65 + })
66 + return p
67 +}
core/node/libp2p.go
+1 -1
@@ -122,7 +122,7 @@ type Libp2pOpts struct {
122 Opts []libp2p.Option `group:"libp2p"`
123 }
124
125 -type PNetFingerprint []byte // TODO: find some better place
125 +type PNetFingerprint []byte
126 func P2PPNet(repo repo.Repo) (opts Libp2pOpts, fp PNetFingerprint, err error) {
127 swarmkey, err := repo.SwarmKey()
128 if err != nil || swarmkey == nil {