Remove old constructor code
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
Łukasz Magiera committed
Apr 1, 2019 at 21:34 UTC
adbc85bf8a8cf44f7d579ac427d7ea727ffd3f02
2 files changed
+17
-783
core/builder.go
+2
-152
@@ -7,37 +7,23 @@ import (
7
"errors"
8
"os"
9
"syscall"
10
- "time"
10
11
"go.uber.org/fx"
12
13
"github.com/ipfs/go-ipfs/p2p"
14
"github.com/ipfs/go-ipfs/provider"
15
17
- filestore "github.com/ipfs/go-ipfs/filestore"
18
- namesys "github.com/ipfs/go-ipfs/namesys"
19
- pin "github.com/ipfs/go-ipfs/pin"
16
repo "github.com/ipfs/go-ipfs/repo"
21
- cidv0v1 "github.com/ipfs/go-ipfs/thirdparty/cidv0v1"
22
- "github.com/ipfs/go-ipfs/thirdparty/verifbs"
17
24
- bserv "github.com/ipfs/go-blockservice"
18
ds "github.com/ipfs/go-datastore"
26
- retry "github.com/ipfs/go-datastore/retrystore"
19
dsync "github.com/ipfs/go-datastore/sync"
28
- bstore "github.com/ipfs/go-ipfs-blockstore"
20
cfg "github.com/ipfs/go-ipfs-config"
21
offline "github.com/ipfs/go-ipfs-exchange-offline"
22
offroute "github.com/ipfs/go-ipfs-routing/offline"
32
- dag "github.com/ipfs/go-merkledag"
23
metrics "github.com/ipfs/go-metrics-interface"
24
resolver "github.com/ipfs/go-path/resolver"
35
- uio "github.com/ipfs/go-unixfs/io"
36
- libp2p "github.com/libp2p/go-libp2p"
25
ci "github.com/libp2p/go-libp2p-crypto"
38
- p2phost "github.com/libp2p/go-libp2p-host"
26
peer "github.com/libp2p/go-libp2p-peer"
40
- pstore "github.com/libp2p/go-libp2p-peerstore"
27
)
28
29
type BuildCfg struct {
@@ -245,6 +231,8 @@ func NewNode(ctx context.Context, cfg *BuildCfg) (*IpfsNode, error) {
231
)
232
233
go func() {
234
+ // Note that some services use contexts to signal shutting down, which is
235
+ // very suboptimal. This needs to be here until that's addressed somehow
236
<-ctx.Done()
237
app.Stop(context.Background())
238
}()
@@ -252,25 +240,6 @@ func NewNode(ctx context.Context, cfg *BuildCfg) (*IpfsNode, error) {
240
n.IsOnline = cfg.Online
241
n.app = app
242
255
- /* n := &IpfsNode{
256
- IsOnline: cfg.Online,
257
- Repo: cfg.Repo,
258
- ctx: ctx,
259
- Peerstore: pstoremem.NewPeerstore(),
260
- }
261
-
262
- n.RecordValidator = record.NamespacedValidator{
263
- "pk": record.PublicKeyValidator{},
264
- "ipns": ipns.Validator{KeyBook: n.Peerstore},
265
- }
266
- */
267
- // TODO: port to lifetimes
268
- // n.proc = goprocessctx.WithContextAndTeardown(ctx, n.teardown)
269
-
270
- /*if err := setupNode(ctx, n, cfg); err != nil {
271
- n.Close()
272
- return nil, err
273
- }*/
243
if app.Err() != nil {
244
return nil, app.Err()
245
}
@@ -295,122 +264,3 @@ func isTooManyFDError(err error) bool {
264
265
return false
266
}
298
-
299
-func setupNode(ctx context.Context, n *IpfsNode, cfg *BuildCfg) error {
300
- // setup local identity
301
- if err := n.loadID(); err != nil {
302
- return err
303
- }
304
-
305
- // load the private key (if present)
306
- if err := n.loadPrivateKey(); err != nil {
307
- return err
308
- }
309
-
310
- rds := &retry.Datastore{
311
- Batching: n.Repo.Datastore(),
312
- Delay: time.Millisecond * 200,
313
- Retries: 6,
314
- TempErrFunc: isTooManyFDError,
315
- }
316
-
317
- // hash security
318
- bs := bstore.NewBlockstore(rds)
319
- bs = &verifbs.VerifBS{Blockstore: bs}
320
-
321
- opts := bstore.DefaultCacheOpts()
322
- conf, err := n.Repo.Config()
323
- if err != nil {
324
- return err
325
- }
326
-
327
- // TEMP: setting global sharding switch here
328
- uio.UseHAMTSharding = conf.Experimental.ShardingEnabled
329
-
330
- opts.HasBloomFilterSize = conf.Datastore.BloomFilterSize
331
- if !cfg.Permanent {
332
- opts.HasBloomFilterSize = 0
333
- }
334
-
335
- if !cfg.NilRepo {
336
- bs, err = bstore.CachedBlockstore(ctx, bs, opts)
337
- if err != nil {
338
- return err
339
- }
340
- }
341
-
342
- bs = bstore.NewIdStore(bs)
343
-
344
- bs = cidv0v1.NewBlockstore(bs)
345
-
346
- n.BaseBlocks = bs
347
- n.GCLocker = bstore.NewGCLocker()
348
- n.Blockstore = bstore.NewGCBlockstore(bs, n.GCLocker)
349
-
350
- if conf.Experimental.FilestoreEnabled || conf.Experimental.UrlstoreEnabled {
351
- // hash security
352
- n.Filestore = filestore.NewFilestore(bs, n.Repo.FileManager())
353
- n.Blockstore = bstore.NewGCBlockstore(n.Filestore, n.GCLocker)
354
- n.Blockstore = &verifbs.VerifBSGC{GCBlockstore: n.Blockstore}
355
- }
356
-
357
- rcfg, err := n.Repo.Config()
358
- if err != nil {
359
- return err
360
- }
361
-
362
- if rcfg.Datastore.HashOnRead {
363
- bs.HashOnRead(true)
364
- }
365
-
366
- hostOption := cfg.Host
367
- if cfg.DisableEncryptedConnections {
368
- innerHostOption := hostOption
369
- hostOption = func(ctx context.Context, id peer.ID, ps pstore.Peerstore, options ...libp2p.Option) (p2phost.Host, error) {
370
- return innerHostOption(ctx, id, ps, append(options, libp2p.NoSecurity)...)
371
- }
372
- // TODO: shouldn't this be Errorf to guarantee visibility?
373
- log.Warningf(`Your IPFS node has been configured to run WITHOUT ENCRYPTED CONNECTIONS.
374
- You will not be able to connect to any nodes configured to use encrypted connections`)
375
- }
376
-
377
- if cfg.Online {
378
- do := setupDiscoveryOption(rcfg.Discovery)
379
- if err := n.startOnlineServices(ctx, cfg.Routing, hostOption, do, cfg.getOpt("pubsub"), cfg.getOpt("ipnsps"), cfg.getOpt("mplex")); err != nil {
380
- return err
381
- }
382
- } else {
383
- n.Exchange = offline.Exchange(n.Blockstore)
384
- n.Routing = offroute.NewOfflineRouter(n.Repo.Datastore(), n.RecordValidator)
385
- n.Namesys = namesys.NewNameSystem(n.Routing, n.Repo.Datastore(), 0)
386
- }
387
-
388
- n.Blocks = bserv.New(n.Blockstore, n.Exchange)
389
- n.DAG = dag.NewDAGService(n.Blocks)
390
-
391
- internalDag := dag.NewDAGService(bserv.New(n.Blockstore, offline.Exchange(n.Blockstore)))
392
- n.Pinning, err = pin.LoadPinner(n.Repo.Datastore(), n.DAG, internalDag)
393
- if err != nil {
394
- // TODO: we should move towards only running 'NewPinner' explicitly on
395
- // node init instead of implicitly here as a result of the pinner keys
396
- // not being found in the datastore.
397
- // this is kinda sketchy and could cause data loss
398
- n.Pinning = pin.NewPinner(n.Repo.Datastore(), n.DAG, internalDag)
399
- }
400
- n.Resolver = resolver.NewBasicResolver(n.DAG)
401
-
402
- // Provider
403
- queue, err := provider.NewQueue(ctx, "provider-v1", n.Repo.Datastore())
404
- if err != nil {
405
- return err
406
- }
407
- n.Provider = provider.NewProvider(ctx, queue, n.Routing)
408
-
409
- if cfg.Online {
410
- if err := n.startLateOnlineServices(ctx); err != nil {
411
- return err
412
- }
413
- }
414
-
415
- return n.loadFilesRoot()
416
-}
core/core.go
+15
-631
@@ -10,9 +10,7 @@ interfaces and how core/... fits into the bigger IPFS picture, see:
10
package core
11
12
import (
13
- "bytes"
13
"context"
15
- "errors"
14
"fmt"
15
"io"
16
"io/ioutil"
@@ -24,36 +22,32 @@ import (
22
23
version "github.com/ipfs/go-ipfs"
24
rp "github.com/ipfs/go-ipfs/exchange/reprovide"
27
- filestore "github.com/ipfs/go-ipfs/filestore"
28
- mount "github.com/ipfs/go-ipfs/fuse/mount"
29
- namesys "github.com/ipfs/go-ipfs/namesys"
25
+ "github.com/ipfs/go-ipfs/filestore"
26
+ "github.com/ipfs/go-ipfs/fuse/mount"
27
+ "github.com/ipfs/go-ipfs/namesys"
28
ipnsrp "github.com/ipfs/go-ipfs/namesys/republisher"
31
- p2p "github.com/ipfs/go-ipfs/p2p"
32
- pin "github.com/ipfs/go-ipfs/pin"
33
- provider "github.com/ipfs/go-ipfs/provider"
34
- repo "github.com/ipfs/go-ipfs/repo"
29
+ "github.com/ipfs/go-ipfs/p2p"
30
+ "github.com/ipfs/go-ipfs/pin"
31
+ "github.com/ipfs/go-ipfs/provider"
32
+ "github.com/ipfs/go-ipfs/repo"
33
36
- bitswap "github.com/ipfs/go-bitswap"
37
- bsnet "github.com/ipfs/go-bitswap/network"
34
bserv "github.com/ipfs/go-blockservice"
39
- cid "github.com/ipfs/go-cid"
35
+ "github.com/ipfs/go-cid"
36
ds "github.com/ipfs/go-datastore"
37
bstore "github.com/ipfs/go-ipfs-blockstore"
38
config "github.com/ipfs/go-ipfs-config"
39
exchange "github.com/ipfs/go-ipfs-exchange-interface"
40
nilrouting "github.com/ipfs/go-ipfs-routing/none"
45
- u "github.com/ipfs/go-ipfs-util"
41
ipld "github.com/ipfs/go-ipld-format"
42
logging "github.com/ipfs/go-log"
48
- merkledag "github.com/ipfs/go-merkledag"
49
- mfs "github.com/ipfs/go-mfs"
50
- resolver "github.com/ipfs/go-path/resolver"
43
+ "github.com/ipfs/go-merkledag"
44
+ "github.com/ipfs/go-mfs"
45
+ "github.com/ipfs/go-path/resolver"
46
ft "github.com/ipfs/go-unixfs"
52
- goprocess "github.com/jbenet/goprocess"
53
- libp2p "github.com/libp2p/go-libp2p"
47
+ "github.com/jbenet/goprocess"
48
+ "github.com/libp2p/go-libp2p"
49
autonat "github.com/libp2p/go-libp2p-autonat-svc"
50
circuit "github.com/libp2p/go-libp2p-circuit"
56
- connmgr "github.com/libp2p/go-libp2p-connmgr"
51
ic "github.com/libp2p/go-libp2p-crypto"
52
p2phost "github.com/libp2p/go-libp2p-host"
53
ifconnmgr "github.com/libp2p/go-libp2p-interface-connmgr"
@@ -62,17 +56,13 @@ import (
56
metrics "github.com/libp2p/go-libp2p-metrics"
57
peer "github.com/libp2p/go-libp2p-peer"
58
pstore "github.com/libp2p/go-libp2p-peerstore"
65
- pnet "github.com/libp2p/go-libp2p-pnet"
59
pubsub "github.com/libp2p/go-libp2p-pubsub"
60
psrouter "github.com/libp2p/go-libp2p-pubsub-router"
68
- quic "github.com/libp2p/go-libp2p-quic-transport"
61
record "github.com/libp2p/go-libp2p-record"
62
routing "github.com/libp2p/go-libp2p-routing"
71
- rhelpers "github.com/libp2p/go-libp2p-routing-helpers"
72
- discovery "github.com/libp2p/go-libp2p/p2p/discovery"
63
+ "github.com/libp2p/go-libp2p/p2p/discovery"
64
p2pbhost "github.com/libp2p/go-libp2p/p2p/host/basic"
74
- rhost "github.com/libp2p/go-libp2p/p2p/host/routed"
75
- identify "github.com/libp2p/go-libp2p/p2p/protocol/identify"
65
+ "github.com/libp2p/go-libp2p/p2p/protocol/identify"
66
mafilter "github.com/libp2p/go-maddr-filter"
67
smux "github.com/libp2p/go-stream-muxer"
68
ma "github.com/multiformats/go-multiaddr"
@@ -153,218 +143,6 @@ type Mounts struct {
143
Ipns mount.Mount
144
}
145
156
-func (n *IpfsNode) startOnlineServices(ctx context.Context, routingOption RoutingOption, hostOption HostOption, do DiscoveryOption, pubsub, ipnsps, mplex bool) error {
157
- if n.PeerHost != nil { // already online.
158
- return errors.New("node already online")
159
- }
160
-
161
- if n.PrivateKey == nil {
162
- return fmt.Errorf("private key not available")
163
- }
164
-
165
- // get undialable addrs from config
166
- cfg, err := n.Repo.Config()
167
- if err != nil {
168
- return err
169
- }
170
-
171
- var libp2pOpts []libp2p.Option
172
- for _, s := range cfg.Swarm.AddrFilters {
173
- f, err := mamask.NewMask(s)
174
- if err != nil {
175
- return fmt.Errorf("incorrectly formatted address filter in config: %s", s)
176
- }
177
- libp2pOpts = append(libp2pOpts, libp2p.FilterAddresses(f))
178
- }
179
-
180
- if !cfg.Swarm.DisableBandwidthMetrics {
181
- // Set reporter
182
- n.Reporter = metrics.NewBandwidthCounter()
183
- libp2pOpts = append(libp2pOpts, libp2p.BandwidthReporter(n.Reporter))
184
- }
185
-
186
- swarmkey, err := n.Repo.SwarmKey()
187
- if err != nil {
188
- return err
189
- }
190
-
191
- if swarmkey != nil {
192
- protec, err := pnet.NewProtector(bytes.NewReader(swarmkey))
193
- if err != nil {
194
- return fmt.Errorf("failed to configure private network: %s", err)
195
- }
196
- n.PNetFingerprint = protec.Fingerprint()
197
- go func() {
198
- t := time.NewTicker(30 * time.Second)
199
- <-t.C // swallow one tick
200
- for {
201
- select {
202
- case <-t.C:
203
- if ph := n.PeerHost; ph != nil {
204
- if len(ph.Network().Peers()) == 0 {
205
- log.Warning("We are in private network and have no peers.")
206
- log.Warning("This might be configuration mistake.")
207
- }
208
- }
209
- //case <-n.Process().Closing():
210
- // t.Stop()
211
- // return
212
- }
213
- }
214
- }()
215
-
216
- libp2pOpts = append(libp2pOpts, libp2p.PrivateNetwork(protec))
217
- }
218
-
219
- addrsFactory, err := makeAddrsFactory(cfg.Addresses)
220
- if err != nil {
221
- return err
222
- }
223
- if !cfg.Swarm.DisableRelay {
224
- addrsFactory = composeAddrsFactory(addrsFactory, filterRelayAddrs)
225
- }
226
- libp2pOpts = append(libp2pOpts, libp2p.AddrsFactory(addrsFactory))
227
-
228
- connm, err := constructConnMgr(cfg.Swarm.ConnMgr)
229
- if err != nil {
230
- return err
231
- }
232
- libp2pOpts = append(libp2pOpts, libp2p.ConnectionManager(connm))
233
-
234
- libp2pOpts = append(libp2pOpts, makeSmuxTransportOption(mplex))
235
-
236
- if !cfg.Swarm.DisableNatPortMap {
237
- libp2pOpts = append(libp2pOpts, libp2p.NATPortMap())
238
- }
239
-
240
- // disable the default listen addrs
241
- libp2pOpts = append(libp2pOpts, libp2p.NoListenAddrs)
242
-
243
- if cfg.Swarm.DisableRelay {
244
- // Enabled by default.
245
- libp2pOpts = append(libp2pOpts, libp2p.DisableRelay())
246
- } else {
247
- relayOpts := []circuit.RelayOpt{circuit.OptDiscovery}
248
- if cfg.Swarm.EnableRelayHop {
249
- relayOpts = append(relayOpts, circuit.OptHop)
250
- }
251
- libp2pOpts = append(libp2pOpts, libp2p.EnableRelay(relayOpts...))
252
- }
253
-
254
- // explicitly enable the default transports
255
- libp2pOpts = append(libp2pOpts, libp2p.DefaultTransports)
256
-
257
- if cfg.Experimental.QUIC {
258
- libp2pOpts = append(libp2pOpts, libp2p.Transport(quic.NewTransport))
259
- }
260
-
261
- // enable routing
262
- libp2pOpts = append(libp2pOpts, libp2p.Routing(func(h p2phost.Host) (routing.PeerRouting, error) {
263
- r, err := routingOption(ctx, h, n.Repo.Datastore(), n.RecordValidator)
264
- n.Routing = r
265
- return r, err
266
- }))
267
-
268
- // enable autorelay
269
- if cfg.Swarm.EnableAutoRelay {
270
- libp2pOpts = append(libp2pOpts, libp2p.EnableAutoRelay())
271
- }
272
-
273
- peerhost, err := hostOption(ctx, n.Identity, n.Peerstore, libp2pOpts...)
274
-
275
- if err != nil {
276
- return err
277
- }
278
-
279
- n.PeerHost = peerhost
280
-
281
- if err := n.startOnlineServicesWithHost(ctx, routingOption, pubsub, ipnsps); err != nil {
282
- return err
283
- }
284
-
285
- // Ok, now we're ready to listen.
286
- if err := startListening(n.PeerHost, cfg); err != nil {
287
- return err
288
- }
289
-
290
- n.P2P = p2p.NewP2P(n.Identity, n.PeerHost, n.Peerstore)
291
-
292
- // setup local discovery
293
- if do != nil {
294
- service, err := do(ctx, n.PeerHost)
295
- if err != nil {
296
- log.Error("mdns error: ", err)
297
- } else {
298
- service.RegisterNotifee(n)
299
- n.Discovery = service
300
- }
301
- }
302
-
303
- return n.Bootstrap(DefaultBootstrapConfig)
304
-}
305
-
306
-func constructConnMgr(cfg config.ConnMgr) (ifconnmgr.ConnManager, error) {
307
- switch cfg.Type {
308
- case "":
309
- // 'default' value is the basic connection manager
310
- return connmgr.NewConnManager(config.DefaultConnMgrLowWater, config.DefaultConnMgrHighWater, config.DefaultConnMgrGracePeriod), nil
311
- case "none":
312
- return nil, nil
313
- case "basic":
314
- grace, err := time.ParseDuration(cfg.GracePeriod)
315
- if err != nil {
316
- return nil, fmt.Errorf("parsing Swarm.ConnMgr.GracePeriod: %s", err)
317
- }
318
-
319
- return connmgr.NewConnManager(cfg.LowWater, cfg.HighWater, grace), nil
320
- default:
321
- return nil, fmt.Errorf("unrecognized ConnMgr.Type: %q", cfg.Type)
322
- }
323
-}
324
-
325
-func (n *IpfsNode) startLateOnlineServices(ctx context.Context) error {
326
- cfg, err := n.Repo.Config()
327
- if err != nil {
328
- return err
329
- }
330
-
331
- // Provider
332
-
333
- n.Provider.Run()
334
-
335
- // Reprovider
336
-
337
- var keyProvider rp.KeyChanFunc
338
-
339
- switch cfg.Reprovider.Strategy {
340
- case "all":
341
- fallthrough
342
- case "":
343
- keyProvider = rp.NewBlockstoreProvider(n.Blockstore)
344
- case "roots":
345
- keyProvider = rp.NewPinnedProvider(n.Pinning, n.DAG, true)
346
- case "pinned":
347
- keyProvider = rp.NewPinnedProvider(n.Pinning, n.DAG, false)
348
- default:
349
- return fmt.Errorf("unknown reprovider strategy '%s'", cfg.Reprovider.Strategy)
350
- }
351
- n.Reprovider = rp.NewReprovider(ctx, n.Routing, keyProvider)
352
-
353
- reproviderInterval := kReprovideFrequency
354
- if cfg.Reprovider.Interval != "" {
355
- dur, err := time.ParseDuration(cfg.Reprovider.Interval)
356
- if err != nil {
357
- return err
358
- }
359
-
360
- reproviderInterval = dur
361
- }
362
-
363
- go n.Reprovider.Run(reproviderInterval)
364
-
365
- return nil
366
-}
367
-
146
func makeAddrsFactory(cfg config.Addresses) (p2pbhost.AddrsFactory, error) {
147
var annAddrs []ma.Multiaddr
148
for _, addr := range cfg.Announce {
@@ -453,200 +231,6 @@ func makeSmuxTransportOption(mplexExp bool) libp2p.Option {
231
return libp2p.ChainOptions(opts...)
232
}
233
456
-func setupDiscoveryOption(d config.Discovery) DiscoveryOption {
457
- if d.MDNS.Enabled {
458
- return func(ctx context.Context, h p2phost.Host) (discovery.Service, error) {
459
- if d.MDNS.Interval == 0 {
460
- d.MDNS.Interval = 5
461
- }
462
- return discovery.NewMdnsService(ctx, h, time.Duration(d.MDNS.Interval)*time.Second, discovery.ServiceTag)
463
- }
464
- }
465
- return nil
466
-}
467
-
468
-// HandlePeerFound attempts to connect to peer from `PeerInfo`, if it fails
469
-// logs a warning log.
470
-func (n *IpfsNode) HandlePeerFound(p pstore.PeerInfo) {
471
- log.Warning("trying peer info: ", p)
472
- ctx, cancel := context.WithTimeout(n.Context(), discoveryConnTimeout)
473
- defer cancel()
474
- if err := n.PeerHost.Connect(ctx, p); err != nil {
475
- log.Warning("Failed to connect to peer found by discovery: ", err)
476
- }
477
-}
478
-
479
-// startOnlineServicesWithHost is the set of services which need to be
480
-// initialized with the host and _before_ we start listening.
481
-func (n *IpfsNode) startOnlineServicesWithHost(ctx context.Context, routingOption RoutingOption, enablePubsub bool, enableIpnsps bool) error {
482
- cfg, err := n.Repo.Config()
483
- if err != nil {
484
- return err
485
- }
486
-
487
- if cfg.Swarm.EnableAutoNATService {
488
- var opts []libp2p.Option
489
- if cfg.Experimental.QUIC {
490
- opts = append(opts, libp2p.DefaultTransports, libp2p.Transport(quic.NewTransport))
491
- }
492
-
493
- svc, err := autonat.NewAutoNATService(ctx, n.PeerHost, opts...)
494
- if err != nil {
495
- return err
496
- }
497
- n.AutoNAT = svc
498
- }
499
-
500
- if enablePubsub || enableIpnsps {
501
- var service *pubsub.PubSub
502
-
503
- var pubsubOptions []pubsub.Option
504
- if cfg.Pubsub.DisableSigning {
505
- pubsubOptions = append(pubsubOptions, pubsub.WithMessageSigning(false))
506
- }
507
-
508
- if cfg.Pubsub.StrictSignatureVerification {
509
- pubsubOptions = append(pubsubOptions, pubsub.WithStrictSignatureVerification(true))
510
- }
511
-
512
- switch cfg.Pubsub.Router {
513
- case "":
514
- fallthrough
515
- case "floodsub":
516
- service, err = pubsub.NewFloodSub(ctx, n.PeerHost, pubsubOptions...)
517
-
518
- case "gossipsub":
519
- service, err = pubsub.NewGossipSub(ctx, n.PeerHost, pubsubOptions...)
520
-
521
- default:
522
- err = fmt.Errorf("Unknown pubsub router %s", cfg.Pubsub.Router)
523
- }
524
-
525
- if err != nil {
526
- return err
527
- }
528
- n.PubSub = service
529
- }
530
-
531
- // this code is necessary just for tests: mock network constructions
532
- // ignore the libp2p constructor options that actually construct the routing!
533
- if n.Routing == nil {
534
- r, err := routingOption(ctx, n.PeerHost, n.Repo.Datastore(), n.RecordValidator)
535
- if err != nil {
536
- return err
537
- }
538
- n.Routing = r
539
- n.PeerHost = rhost.Wrap(n.PeerHost, n.Routing)
540
- }
541
-
542
- // TODO: I'm not a fan of type assertions like this but the
543
- // `RoutingOption` system doesn't currently provide access to the
544
- // IpfsNode.
545
- //
546
- // Ideally, we'd do something like:
547
- //
548
- // 1. Add some fancy method to introspect into tiered routers to extract
549
- // things like the pubsub router or the DHT (complicated, messy,
550
- // probably not worth it).
551
- // 2. Pass the IpfsNode into the RoutingOption (would also remove the
552
- // PSRouter case below.
553
- // 3. Introduce some kind of service manager? (my personal favorite but
554
- // that requires a fair amount of work).
555
- if dht, ok := n.Routing.(*dht.IpfsDHT); ok {
556
- n.DHT = dht
557
- }
558
-
559
- if enableIpnsps {
560
- n.PSRouter = psrouter.NewPubsubValueStore(
561
- ctx,
562
- n.PeerHost,
563
- n.Routing,
564
- n.PubSub,
565
- n.RecordValidator,
566
- )
567
- n.Routing = rhelpers.Tiered{
568
- Routers: []routing.IpfsRouting{
569
- // Always check pubsub first.
570
- &rhelpers.Compose{
571
- ValueStore: &rhelpers.LimitedValueStore{
572
- ValueStore: n.PSRouter,
573
- Namespaces: []string{"ipns"},
574
- },
575
- },
576
- n.Routing,
577
- },
578
- Validator: n.RecordValidator,
579
- }
580
- }
581
-
582
- // setup exchange service
583
- bitswapNetwork := bsnet.NewFromIpfsHost(n.PeerHost, n.Routing)
584
- n.Exchange = bitswap.New(ctx, bitswapNetwork, n.Blockstore)
585
-
586
- size, err := n.getCacheSize()
587
- if err != nil {
588
- return err
589
- }
590
-
591
- // setup name system
592
- n.Namesys = namesys.NewNameSystem(n.Routing, n.Repo.Datastore(), size)
593
-
594
- // setup ipns republishing
595
- return n.setupIpnsRepublisher()
596
-}
597
-
598
-// getCacheSize returns cache life and cache size
599
-func (n *IpfsNode) getCacheSize() (int, error) {
600
- cfg, err := n.Repo.Config()
601
- if err != nil {
602
- return 0, err
603
- }
604
-
605
- cs := cfg.Ipns.ResolveCacheSize
606
- if cs == 0 {
607
- cs = DefaultIpnsCacheSize
608
- }
609
- if cs < 0 {
610
- return 0, fmt.Errorf("cannot specify negative resolve cache size")
611
- }
612
- return cs, nil
613
-}
614
-
615
-func (n *IpfsNode) setupIpnsRepublisher() error {
616
- cfg, err := n.Repo.Config()
617
- if err != nil {
618
- return err
619
- }
620
-
621
- n.IpnsRepub = ipnsrp.NewRepublisher(n.Namesys, n.Repo.Datastore(), n.PrivateKey, n.Repo.Keystore())
622
-
623
- if cfg.Ipns.RepublishPeriod != "" {
624
- d, err := time.ParseDuration(cfg.Ipns.RepublishPeriod)
625
- if err != nil {
626
- return fmt.Errorf("failure to parse config setting IPNS.RepublishPeriod: %s", err)
627
- }
628
-
629
- if !u.Debug && (d < time.Minute || d > (time.Hour*24)) {
630
- return fmt.Errorf("config setting IPNS.RepublishPeriod is not between 1min and 1day: %s", d)
631
- }
632
-
633
- n.IpnsRepub.Interval = d
634
- }
635
-
636
- if cfg.Ipns.RecordLifetime != "" {
637
- d, err := time.ParseDuration(cfg.Ipns.RecordLifetime)
638
- if err != nil {
639
- return fmt.Errorf("failure to parse config setting IPNS.RecordLifetime: %s", err)
640
- }
641
-
642
- n.IpnsRepub.RecordLifetime = d
643
- }
644
-
645
- //n.Process().Go(n.IpnsRepub.Run)
646
-
647
- return nil
648
-}
649
-
234
// Close calls Close() on the App object
235
func (n *IpfsNode) Close() error {
236
return n.app.Stop(n.ctx)
@@ -660,68 +244,6 @@ func (n *IpfsNode) Context() context.Context {
244
return n.ctx
245
}
246
663
-// teardown closes owned children. If any errors occur, this function returns
664
-// the first error.
665
-func (n *IpfsNode) teardown() error {
666
- log.Debug("core is shutting down...")
667
- // owned objects are closed in this teardown to ensure that they're closed
668
- // regardless of which constructor was used to add them to the node.
669
- var closers []io.Closer
670
-
671
- // NOTE: The order that objects are added(closed) matters, if an object
672
- // needs to use another during its shutdown/cleanup process, it should be
673
- // closed before that other object
674
-
675
- if n.Provider != nil {
676
- closers = append(closers, n.Provider)
677
- }
678
-
679
- if n.FilesRoot != nil {
680
- closers = append(closers, n.FilesRoot)
681
- }
682
-
683
- if n.Exchange != nil {
684
- closers = append(closers, n.Exchange)
685
- }
686
-
687
- if n.Mounts.Ipfs != nil && !n.Mounts.Ipfs.IsActive() { //TODO
688
- closers = append(closers, mount.Closer(n.Mounts.Ipfs))
689
- }
690
- if n.Mounts.Ipns != nil && !n.Mounts.Ipns.IsActive() { // TODO
691
- closers = append(closers, mount.Closer(n.Mounts.Ipns))
692
- }
693
-
694
- if n.DHT != nil {
695
- closers = append(closers, n.DHT.Process())
696
- }
697
-
698
- if n.Blocks != nil {
699
- closers = append(closers, n.Blocks)
700
- }
701
-
702
- if n.Bootstrapper != nil {
703
- closers = append(closers, n.Bootstrapper)
704
- }
705
-
706
- if n.PeerHost != nil {
707
- closers = append(closers, n.PeerHost)
708
- }
709
-
710
- // Repo closed last, most things need to preserve state here
711
- closers = append(closers, n.Repo)
712
-
713
- var errs []error
714
- for _, closer := range closers {
715
- if err := closer.Close(); err != nil {
716
- errs = append(errs, err)
717
- }
718
- }
719
- if len(errs) > 0 {
720
- return errs[0]
721
- }
722
- return nil
723
-}
724
-
247
// Bootstrap will set and call the IpfsNodes bootstrap function.
248
func (n *IpfsNode) Bootstrap(cfg BootstrapConfig) error {
249
// TODO what should return value be when in offlineMode?
@@ -751,80 +273,6 @@ func (n *IpfsNode) Bootstrap(cfg BootstrapConfig) error {
273
return err
274
}
275
754
-func (n *IpfsNode) loadID() error {
755
- if n.Identity != "" {
756
- return errors.New("identity already loaded")
757
- }
758
-
759
- cfg, err := n.Repo.Config()
760
- if err != nil {
761
- return err
762
- }
763
-
764
- cid := cfg.Identity.PeerID
765
- if cid == "" {
766
- return errors.New("identity was not set in config (was 'ipfs init' run?)")
767
- }
768
- if len(cid) == 0 {
769
- return errors.New("no peer ID in config! (was 'ipfs init' run?)")
770
- }
771
-
772
- id, err := peer.IDB58Decode(cid)
773
- if err != nil {
774
- return fmt.Errorf("peer ID invalid: %s", err)
775
- }
776
-
777
- n.Identity = id
778
- return nil
779
-}
780
-
781
-// GetKey will return a key from the Keystore with name `name`.
782
-func (n *IpfsNode) GetKey(name string) (ic.PrivKey, error) {
783
- if name == "self" {
784
- if n.PrivateKey == nil {
785
- return nil, fmt.Errorf("private key not available")
786
- }
787
- return n.PrivateKey, nil
788
- } else {
789
- return n.Repo.Keystore().Get(name)
790
- }
791
-}
792
-
793
-// loadPrivateKey loads the private key *if* available
794
-func (n *IpfsNode) loadPrivateKey() error {
795
- if n.Identity == "" || n.Peerstore == nil {
796
- return errors.New("loaded private key out of order")
797
- }
798
-
799
- if n.PrivateKey != nil {
800
- log.Warning("private key already loaded")
801
- return nil
802
- }
803
-
804
- cfg, err := n.Repo.Config()
805
- if err != nil {
806
- return err
807
- }
808
-
809
- if cfg.Identity.PrivKey == "" {
810
- return nil
811
- }
812
-
813
- sk, err := loadPrivateKey(&cfg.Identity, n.Identity)
814
- if err != nil {
815
- return err
816
- }
817
-
818
- if err := n.Peerstore.AddPrivKey(n.Identity, sk); err != nil {
819
- return err
820
- }
821
- if err := n.Peerstore.AddPubKey(n.Identity, sk.GetPublic()); err != nil {
822
- return err
823
- }
824
- n.PrivateKey = sk
825
- return nil
826
-}
827
-
276
func (n *IpfsNode) loadBootstrapPeers() ([]pstore.PeerInfo, error) {
277
cfg, err := n.Repo.Config()
278
if err != nil {
@@ -838,70 +286,6 @@ func (n *IpfsNode) loadBootstrapPeers() ([]pstore.PeerInfo, error) {
286
return toPeerInfos(parsed), nil
287
}
288
841
-func (n *IpfsNode) loadFilesRoot() error {
842
- dsk := ds.NewKey("/local/filesroot")
843
- pf := func(ctx context.Context, c cid.Cid) error {
844
- return n.Repo.Datastore().Put(dsk, c.Bytes())
845
- }
846
-
847
- var nd *merkledag.ProtoNode
848
- val, err := n.Repo.Datastore().Get(dsk)
849
-
850
- switch {
851
- case err == ds.ErrNotFound || val == nil:
852
- nd = ft.EmptyDirNode()
853
- err := n.DAG.Add(n.Context(), nd)
854
- if err != nil {
855
- return fmt.Errorf("failure writing to dagstore: %s", err)
856
- }
857
- case err == nil:
858
- c, err := cid.Cast(val)
859
- if err != nil {
860
- return err
861
- }
862
-
863
- rnd, err := n.DAG.Get(n.Context(), c)
864
- if err != nil {
865
- return fmt.Errorf("error loading filesroot from DAG: %s", err)
866
- }
867
-
868
- pbnd, ok := rnd.(*merkledag.ProtoNode)
869
- if !ok {
870
- return merkledag.ErrNotProtobuf
871
- }
872
-
873
- nd = pbnd
874
- default:
875
- return err
876
- }
877
-
878
- mr, err := mfs.NewRoot(n.Context(), n.DAG, nd, pf)
879
- if err != nil {
880
- return err
881
- }
882
-
883
- n.FilesRoot = mr
884
- return nil
885
-}
886
-
887
-func loadPrivateKey(cfg *config.Identity, id peer.ID) (ic.PrivKey, error) {
888
- sk, err := cfg.DecodePrivateKey("passphrase todo!")
889
- if err != nil {
890
- return nil, err
891
- }
892
-
893
- id2, err := peer.IDFromPrivateKey(sk)
894
- if err != nil {
895
- return nil, err
896
- }
897
-
898
- if id2 != id {
899
- return nil, fmt.Errorf("private key in config does not match id: %s != %s", id, id2)
900
- }
901
-
902
- return sk, nil
903
-}
904
-
289
func listenAddresses(cfg *config.Config) ([]ma.Multiaddr, error) {
290
var listen []ma.Multiaddr
291
for _, addr := range cfg.Addresses.Swarm {