More constructor fixes
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
Łukasz Magiera committed
Mar 28, 2019 at 02:57 UTC
ccc576b69389cf489a953319c314867a002504a1
3 files changed
+47
-31
core/builder.go
+19
-12
@@ -162,6 +162,7 @@ func NewNode(ctx context.Context, cfg *BuildCfg) (*IpfsNode, error) {
162
163
storage := fx.Options(
164
fx.Provide(repoConfig),
165
+ fx.Provide(datastoreCtor),
166
fx.Provide(baseBlockstoreCtor),
167
fx.Provide(gcBlockstoreCtor),
168
)
@@ -169,23 +170,39 @@ func NewNode(ctx context.Context, cfg *BuildCfg) (*IpfsNode, error) {
170
ident := fx.Options(
171
fx.Provide(identity),
172
fx.Provide(privateKey),
173
+ fx.Provide(peerstore),
174
)
175
176
ipns := fx.Options(
177
fx.Provide(recordValidator),
178
)
179
180
+ providers := fx.Options(
181
+ fx.Provide(providerQueue),
182
+ fx.Provide(providerCtor),
183
+ fx.Provide(reproviderCtor),
184
+
185
+ fx.Invoke(reprovider),
186
+ fx.Invoke(provider.Provider.Run),
187
+ )
188
+
189
online := fx.Options(
190
fx.Provide(onlineExchangeCtor),
191
fx.Provide(onlineNamesysCtor),
192
193
fx.Invoke(ipnsRepublisher),
183
- fx.Invoke(provider.Provider.Run),
194
+
195
+ fx.Provide(p2p.NewP2P),
196
+
197
+ ipfsp2p,
198
+ providers,
199
)
200
if !cfg.Online {
201
online = fx.Options(
202
fx.Provide(offline.Exchange),
203
fx.Provide(offlineNamesysCtor),
204
+ fx.Provide(offroute.NewOfflineRouter),
205
+ fx.Provide(provider.NewOfflineProvider),
206
)
207
}
208
@@ -197,13 +214,6 @@ func NewNode(ctx context.Context, cfg *BuildCfg) (*IpfsNode, error) {
214
fx.Provide(files),
215
)
216
200
- providers := fx.Options(
201
- fx.Provide(providerQueue),
202
- fx.Provide(providerCtor),
203
- fx.Provide(reproviderCtor),
204
- fx.Invoke(reprovider),
205
- )
206
-
217
n := &IpfsNode{
218
ctx: ctx,
219
}
@@ -212,21 +222,18 @@ func NewNode(ctx context.Context, cfg *BuildCfg) (*IpfsNode, error) {
222
params,
223
storage,
224
ident,
215
- ipfsp2p,
225
ipns,
226
online,
227
228
fx.Invoke(setupSharding),
229
230
core,
222
- providers,
223
-
224
- fx.Provide(p2p.NewP2P),
231
232
fx.Extract(n),
233
)
234
235
n.IsOnline = cfg.Online
236
+ n.app = app
237
238
/* n := &IpfsNode{
239
IsOnline: cfg.Online,
core/core.go
+18
-14
@@ -20,6 +20,8 @@ import (
20
"strings"
21
"time"
22
23
+ "go.uber.org/fx"
24
+
25
version "github.com/ipfs/go-ipfs"
26
rp "github.com/ipfs/go-ipfs/exchange/reprovide"
27
filestore "github.com/ipfs/go-ipfs/filestore"
@@ -101,10 +103,10 @@ type IpfsNode struct {
103
Pinning pin.Pinner // the pinning manager
104
Mounts Mounts `optional:"true"` // current mount state, if any.
105
PrivateKey ic.PrivKey // the local node's private Key
104
- PNetFingerprint PNetFingerprint // fingerprint of private network
106
+ PNetFingerprint PNetFingerprint `optional:"true"` // fingerprint of private network
107
108
// Services
107
- Peerstore pstore.Peerstore // storage for other Peer instances
109
+ Peerstore pstore.Peerstore `optional:"true"` // storage for other Peer instances
110
Blockstore bstore.GCBlockstore // the block store (lower level)
111
Filestore *filestore.Filestore // the filestore blockstore
112
BaseBlocks bstore.Blockstore // the raw blockstore, no filestore wrapping
@@ -112,30 +114,32 @@ type IpfsNode struct {
114
Blocks bserv.BlockService // the block service, get/add blocks.
115
DAG ipld.DAGService // the merkle dag service, get/add objects.
116
Resolver *resolver.Resolver // the path resolution system
115
- Reporter metrics.Reporter
117
+ Reporter metrics.Reporter `optional:"true"`
118
Discovery discovery.Service `optional:"true"`
119
FilesRoot *mfs.Root
120
RecordValidator record.Validator
121
122
// Online
121
- PeerHost p2phost.Host // the network host (server+client)
123
+ PeerHost p2phost.Host `optional:"true"` // the network host (server+client)
124
Bootstrapper io.Closer `optional:"true"` // the periodic bootstrapper
123
- Routing routing.IpfsRouting // the routing system. recommend ipfs-dht
125
+ Routing routing.IpfsRouting `optional:"true"` // the routing system. recommend ipfs-dht
126
Exchange exchange.Interface // the block exchange + strategy (bitswap)
127
Namesys namesys.NameSystem // the name system, resolves paths to hashes
126
- Provider provider.Provider // the value provider system
127
- Reprovider *rp.Reprovider // the value reprovider system
128
+ Provider provider.Provider // the value provider system
129
+ Reprovider *rp.Reprovider `optional:"true"` // the value reprovider system
130
IpnsRepub *ipnsrp.Republisher `optional:"true"`
131
132
AutoNAT *autonat.AutoNATService `optional:"true"`
131
- PubSub *pubsub.PubSub
132
- PSRouter *psrouter.PubsubValueStore
133
- DHT *dht.IpfsDHT
134
- P2P *p2p.P2P
133
+ PubSub *pubsub.PubSub `optional:"true"`
134
+ PSRouter *psrouter.PubsubValueStore `optional:"true"`
135
+ DHT *dht.IpfsDHT `optional:"true"`
136
+ P2P *p2p.P2P `optional:"true"`
137
136
- proc goprocess.Process
138
+ proc goprocess.Process //TODO: remove
139
ctx context.Context
140
141
+ app *fx.App
142
+
143
// Flags
144
IsOnline bool `optional:"true"` // Online is set when networking is enabled.
145
IsDaemon bool `optional:"true"` // Daemon is set when running on a long-running daemon.
@@ -648,9 +652,9 @@ func (n *IpfsNode) Process() goprocess.Process {
652
return n.proc
653
}
654
651
-// Close calls Close() on the Process object
655
+// Close calls Close() on the App object
656
func (n *IpfsNode) Close() error {
653
- return n.proc.Close()
657
+ return n.app.Stop(n.ctx)
658
}
659
660
// Context returns the IpfsNode context
core/ncore.go
+10
-5
@@ -111,6 +111,10 @@ func privateKey(cfg *iconfig.Config, id peer.ID) (ic.PrivKey, error) {
111
return sk, nil
112
}
113
114
+func datastoreCtor(repo repo.Repo) ds.Datastore {
115
+ return repo.Datastore()
116
+}
117
+
118
func baseBlockstoreCtor(repo repo.Repo, cfg *iconfig.Config, bcfg *BuildCfg, lc fx.Lifecycle) (bs bstore.Blockstore, err error) {
119
rds := &retry.Datastore{
120
Batching: repo.Datastore(),
@@ -180,8 +184,6 @@ func recordValidator(ps pstore.Peerstore) record.Validator {
184
// libp2p
185
186
var ipfsp2p = fx.Options(
183
- fx.Provide(peerstore),
184
-
187
fx.Provide(p2pAddrFilters),
188
fx.Provide(p2pBandwidthCounter),
189
fx.Provide(p2pPNet),
@@ -411,6 +413,9 @@ func p2pHost(lc fx.Lifecycle, params p2pHostIn) (out p2pHostOut, err error) {
413
}))
414
415
out.Host, err = params.HostOption(ctx, params.ID, params.Peerstore, opts...)
416
+ if err != nil {
417
+ return p2pHostOut{}, err
418
+ }
419
420
// this code is necessary just for tests: mock network constructions
421
// ignore the libp2p constructor options that actually construct the routing!
@@ -758,14 +763,14 @@ func lifecycleCtx(lc fx.Lifecycle) context.Context {
763
}
764
765
func lcGoProc(lc fx.Lifecycle, processFunc goprocess.ProcessFunc) {
761
- proc := goprocess.Background()
766
+ proc := make(chan goprocess.Process, 1)
767
lc.Append(fx.Hook{
768
OnStart: func(ctx context.Context) error {
764
- proc.Go(processFunc)
769
+ proc <- goprocess.Go(processFunc)
770
return nil
771
},
772
OnStop: func(ctx context.Context) error {
768
- return proc.Close() // todo: respect ctx
773
+ return (<-proc).Close() // todo: respect ctx
774
},
775
})
776
}