core.go 19.8 KB
Newer Older
1 2 3 4
/*
Package core implements the IpfsNode object and related methods.

Packages underneath core/ provide a (relatively) stable, low-level API
5 6 7 8
to carry out most IPFS-related tasks.  For more details on the other
interfaces and how core/... fits into the bigger IPFS picture, see:

  $ godoc github.com/ipfs/go-ipfs
9
*/
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
10 11
package core

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
12
import (
13
	"context"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
14
	"errors"
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
15
	"fmt"
16
	"io"
17
	"io/ioutil"
Jeromy's avatar
Jeromy committed
18
	"net"
19 20
	"os"
	"strings"
21
	"time"
22

23 24
	bstore "github.com/ipfs/go-ipfs/blocks/blockstore"
	bserv "github.com/ipfs/go-ipfs/blockservice"
25
	diag "github.com/ipfs/go-ipfs/diagnostics"
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
	exchange "github.com/ipfs/go-ipfs/exchange"
	bitswap "github.com/ipfs/go-ipfs/exchange/bitswap"
	bsnet "github.com/ipfs/go-ipfs/exchange/bitswap/network"
	rp "github.com/ipfs/go-ipfs/exchange/reprovide"
	mount "github.com/ipfs/go-ipfs/fuse/mount"
	merkledag "github.com/ipfs/go-ipfs/merkledag"
	mfs "github.com/ipfs/go-ipfs/mfs"
	namesys "github.com/ipfs/go-ipfs/namesys"
	ipnsrp "github.com/ipfs/go-ipfs/namesys/republisher"
	path "github.com/ipfs/go-ipfs/path"
	pin "github.com/ipfs/go-ipfs/pin"
	repo "github.com/ipfs/go-ipfs/repo"
	config "github.com/ipfs/go-ipfs/repo/config"
	nilrouting "github.com/ipfs/go-ipfs/routing/none"
	offroute "github.com/ipfs/go-ipfs/routing/offline"
	ft "github.com/ipfs/go-ipfs/unixfs"
Jeromy's avatar
Jeromy committed
42

Jeromy's avatar
Jeromy committed
43
	p2phost "gx/ipfs/QmPTGbC34bPKaUm9wTxBo7zSCac7pDuG42ZmnXC718CKZZ/go-libp2p-host"
44 45 46 47
	discovery "gx/ipfs/QmQHmMFyhfp2ZXnbYWqAWhEideDCNDM6hzJwqCU29Y5zV2/go-libp2p/p2p/discovery"
	p2pbhost "gx/ipfs/QmQHmMFyhfp2ZXnbYWqAWhEideDCNDM6hzJwqCU29Y5zV2/go-libp2p/p2p/host/basic"
	rhost "gx/ipfs/QmQHmMFyhfp2ZXnbYWqAWhEideDCNDM6hzJwqCU29Y5zV2/go-libp2p/p2p/host/routed"
	ping "gx/ipfs/QmQHmMFyhfp2ZXnbYWqAWhEideDCNDM6hzJwqCU29Y5zV2/go-libp2p/p2p/protocol/ping"
Jeromy's avatar
Jeromy committed
48
	ds "gx/ipfs/QmRWDav6mzWseLWeYfVd5fvUKiVe9xNH29YfMF438fG364/go-datastore"
49
	goprocess "gx/ipfs/QmSF8fPo3jgVBAy8fpdjjYqgG87dkJgUprRBHRd2tmfgpP/goprocess"
Jeromy's avatar
Jeromy committed
50
	mamask "gx/ipfs/QmSMZwvs3n4GBikZ7hKzT17c3bk65FmyZo2JqtJ16swqCv/multiaddr-filter"
Jeromy's avatar
Jeromy committed
51
	logging "gx/ipfs/QmSpJByNKFX1sCsHBEp3R73FL4NF6FnQTEGyNAXHm2GS52/go-log"
Jeromy's avatar
Jeromy committed
52
	b58 "gx/ipfs/QmT8rehPR3F6bmwL6zjUN8XpiDBFFpMP2myPdC6ApsWfJf/go-base58"
53
	mssmux "gx/ipfs/QmTfjLsou9ic6L4KqCcmbLSZcdiFu8q1v6njKp121pbbXx/go-smux-multistream"
54 55
	ma "gx/ipfs/QmUAQaWbKxGCUTuoQVvvicbQNZ9APF5pDGWyAZSe93AtKH/go-multiaddr"
	addrutil "gx/ipfs/QmVDnc2zvyQm8LhT72n22THcshvH7j3qPMnhvjerQER62T/go-addr-util"
56
	spdy "gx/ipfs/QmWUNsat6Jb19nC5CiJCDXepTkxjdxi3eZqeoB6mrmmaGu/go-smux-spdystream"
Jeromy's avatar
Jeromy committed
57 58
	swarm "gx/ipfs/QmWfxnAiQ5TnnCgiX9ikVUKFNHRgGhbgKdx5DoKPELD7P4/go-libp2p-swarm"
	metrics "gx/ipfs/QmY2otvyPM2sTaDsczo7Yuosg98sUMCJ9qx1gpPaAPTS9B/go-libp2p-metrics"
59
	dht "gx/ipfs/QmYTccce26rGtEbE7SpnSeRcJkT4uqa7aPyzRXufisiTEd/go-libp2p-kad-dht"
60
	u "gx/ipfs/Qmb912gdngC1UWwTkhuW8knyRbcWeu5kqkxBpveLmW8bSr/go-ipfs-util"
61
	mplex "gx/ipfs/QmbPSoTDH3JFKkSxsCQDyVMFT76KcQz7ELWEetyyg3aeDA/go-smux-multiplex"
Jeromy's avatar
Jeromy committed
62
	routing "gx/ipfs/QmbkGVaN9W6RYJK4Ws5FvMKXKDqdRQ5snhtaa92qP6L8eU/go-libp2p-routing"
63
	yamux "gx/ipfs/Qmbn7RYyWzBVXiUp9jZ1dA4VADHy9DtS7iZLwfhEUQvm3U/go-smux-yamux"
Jeromy's avatar
Jeromy committed
64
	cid "gx/ipfs/QmcTcsTvfaeEBRFo1TkFgT8sRmgi1n1LTZpecfVP8fzpGD/go-cid"
65
	floodsub "gx/ipfs/QmdnGKG7c5kHayCaevCwDb12sWxHAJNFxjXu3R9iBLmAbD/floodsub"
Jeromy's avatar
Jeromy committed
66
	pstore "gx/ipfs/QmeXj9VAjmYQZxpmVz7VzccbJrpmr8qkCDSjfVNsPTWTYU/go-libp2p-peerstore"
67
	smux "gx/ipfs/QmeZBgYBHvxMukGK5ojg28BCNLB9SeXqT7XXg6o7r2GbJy/go-stream-muxer"
68 69
	peer "gx/ipfs/QmfMmLGoKzCHDN7cGgk64PJr4iipzidDRME8HABSJqvmhC/go-libp2p-peer"
	ic "gx/ipfs/QmfWDLQjGjVe4fr5CoztYW2DYYjRysMJrFe1RCsXLPTf46/go-libp2p-crypto"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
70 71
)

Jeromy's avatar
Jeromy committed
72
const IpnsValidatorTag = "ipns"
73
const kSizeBlockstoreWriteCache = 100
74
const kReprovideFrequency = time.Hour * 12
75
const discoveryConnTimeout = time.Second * 30
Jeromy's avatar
Jeromy committed
76

Jeromy's avatar
Jeromy committed
77
var log = logging.Logger("core")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
78

79 80 81 82 83
type mode int

const (
	// zero value is not a valid mode, must be explicitly set
	invalidMode mode = iota
84
	localMode
85 86 87 88
	offlineMode
	onlineMode
)

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
89
// IpfsNode is IPFS Core module. It represents an IPFS instance.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
90 91
type IpfsNode struct {

92
	// Self
93
	Identity peer.ID // the local node's identity
94

95
	Repo repo.Repo
96 97

	// Local node
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
98 99 100
	Pinning    pin.Pinner // the pinning manager
	Mounts     Mounts     // current mount state, if any.
	PrivateKey ic.PrivKey // the local node's private Key
101 102

	// Services
Jeromy's avatar
Jeromy committed
103
	Peerstore  pstore.Peerstore     // storage for other Peer instances
104
	Blockstore bstore.GCBlockstore  // the block store (lower level)
105
	Blocks     bserv.BlockService   // the block service, get/add blocks.
106 107
	DAG        merkledag.DAGService // the merkle dag service, get/add objects.
	Resolver   *path.Resolver       // the path resolution system
Jeromy's avatar
Jeromy committed
108
	Reporter   metrics.Reporter
109
	Discovery  discovery.Service
Jeromy's avatar
Jeromy committed
110
	FilesRoot  *mfs.Root
111 112

	// Online
113 114 115 116 117 118
	PeerHost     p2phost.Host        // the network host (server+client)
	Bootstrapper io.Closer           // the periodic bootstrapper
	Routing      routing.IpfsRouting // the routing system. recommend ipfs-dht
	Exchange     exchange.Interface  // the block exchange + strategy (bitswap)
	Namesys      namesys.NameSystem  // the name system, resolves paths to hashes
	Diagnostics  *diag.Diagnostics   // the diagnostics service
Jeromy's avatar
Jeromy committed
119 120
	Ping         *ping.PingService
	Reprovider   *rp.Reprovider // the value reprovider system
Jeromy's avatar
Jeromy committed
121
	IpnsRepub    *ipnsrp.Republisher
122

Jeromy's avatar
Jeromy committed
123 124
	Floodsub *floodsub.PubSub

125 126
	proc goprocess.Process
	ctx  context.Context
127

Jeromy's avatar
Jeromy committed
128
	mode         mode
129
	localModeSet bool
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
130 131
}

132 133 134 135 136 137 138 139
// Mounts defines what the node's mount state is. This should
// perhaps be moved to the daemon or mount. It's here because
// it needs to be accessible across daemon requests.
type Mounts struct {
	Ipfs mount.Mount
	Ipns mount.Mount
}

140
func (n *IpfsNode) startOnlineServices(ctx context.Context, routingOption RoutingOption, hostOption HostOption, do DiscoveryOption, pubsub, mplex bool) error {
141 142

	if n.PeerHost != nil { // already online.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
143
		return errors.New("node already online")
144 145 146
	}

	// load private key
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
147
	if err := n.LoadPrivateKey(); err != nil {
148 149 150
		return err
	}

Jeromy's avatar
Jeromy committed
151
	// get undialable addrs from config
152 153 154 155
	cfg, err := n.Repo.Config()
	if err != nil {
		return err
	}
Jeromy's avatar
Jeromy committed
156
	var addrfilter []*net.IPNet
157
	for _, s := range cfg.Swarm.AddrFilters {
Jeromy's avatar
Jeromy committed
158 159
		f, err := mamask.NewMask(s)
		if err != nil {
160
			return fmt.Errorf("incorrectly formatted address filter in config: %s", s)
Jeromy's avatar
Jeromy committed
161 162 163 164
		}
		addrfilter = append(addrfilter, f)
	}

165 166 167 168 169
	if !cfg.Swarm.DisableBandwidthMetrics {
		// Set reporter
		n.Reporter = metrics.NewBandwidthCounter()
	}

170 171 172
	tpt := makeSmuxTransport(mplex)

	peerhost, err := hostOption(ctx, n.Identity, n.Peerstore, n.Reporter, addrfilter, tpt)
173
	if err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
174
		return err
175 176
	}

177
	if err := n.startOnlineServicesWithHost(ctx, peerhost, routingOption); err != nil {
178
		return err
179 180 181
	}

	// Ok, now we're ready to listen.
182
	if err := startListening(ctx, n.PeerHost, cfg); err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
183
		return err
184
	}
185

186
	n.Reprovider = rp.NewReprovider(n.Routing, n.Blockstore)
187 188 189 190 191 192 193 194 195 196 197 198 199 200

	if cfg.Reprovider.Interval != "0" {
		interval := kReprovideFrequency
		if cfg.Reprovider.Interval != "" {
			dur, err := time.ParseDuration(cfg.Reprovider.Interval)
			if err != nil {
				return err
			}

			interval = dur
		}

		go n.Reprovider.ProvideEvery(ctx, interval)
	}
201

Jeromy's avatar
Jeromy committed
202 203 204
	if pubsub {
		n.Floodsub = floodsub.NewFloodSub(ctx, peerhost)
	}
Jeromy's avatar
Jeromy committed
205

206
	// setup local discovery
Jeromy's avatar
Jeromy committed
207
	if do != nil {
Jeromy's avatar
Jeromy committed
208
		service, err := do(ctx, n.PeerHost)
Jeromy's avatar
Jeromy committed
209
		if err != nil {
Jeromy's avatar
Jeromy committed
210 211 212 213
			log.Error("mdns error: ", err)
		} else {
			service.RegisterNotifee(n)
			n.Discovery = service
Jeromy's avatar
Jeromy committed
214
		}
215 216
	}

217
	return n.Bootstrap(DefaultBootstrapConfig)
218 219
}

220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
func makeSmuxTransport(mplexExp bool) smux.Transport {
	mstpt := mssmux.NewBlankTransport()

	ymxtpt := &yamux.Transport{
		AcceptBacklog:          8192,
		ConnectionWriteTimeout: time.Second * 10,
		KeepAliveInterval:      time.Second * 30,
		EnableKeepAlive:        true,
		MaxStreamWindowSize:    uint32(1024 * 512),
		LogOutput:              ioutil.Discard,
	}

	mstpt.AddTransport("/yamux/1.0.0", ymxtpt)

	mstpt.AddTransport("/spdy/3.1.0", spdy.Transport)

	if mplexExp {
		mstpt.AddTransport("/mplex/6.7.0", mplex.DefaultTransport)
	}

	// Allow muxer preference order overriding
	if prefs := os.Getenv("LIBP2P_MUX_PREFS"); prefs != "" {
		mstpt.OrderPreference = strings.Fields(prefs)
	}

	return mstpt
}

Jeromy's avatar
Jeromy committed
248 249
func setupDiscoveryOption(d config.Discovery) DiscoveryOption {
	if d.MDNS.Enabled {
Jeromy's avatar
Jeromy committed
250
		return func(ctx context.Context, h p2phost.Host) (discovery.Service, error) {
Jeromy's avatar
Jeromy committed
251 252 253
			if d.MDNS.Interval == 0 {
				d.MDNS.Interval = 5
			}
Jeromy's avatar
Jeromy committed
254
			return discovery.NewMdnsService(ctx, h, time.Duration(d.MDNS.Interval)*time.Second)
Jeromy's avatar
Jeromy committed
255 256 257 258 259
		}
	}
	return nil
}

Jeromy's avatar
Jeromy committed
260
func (n *IpfsNode) HandlePeerFound(p pstore.PeerInfo) {
261
	log.Warning("trying peer info: ", p)
262
	ctx, cancel := context.WithTimeout(n.Context(), discoveryConnTimeout)
rht's avatar
rht committed
263
	defer cancel()
264
	if err := n.PeerHost.Connect(ctx, p); err != nil {
265 266 267 268
		log.Warning("Failed to connect to peer found by discovery: ", err)
	}
}

269 270
// startOnlineServicesWithHost  is the set of services which need to be
// initialized with the host and _before_ we start listening.
271
func (n *IpfsNode) startOnlineServicesWithHost(ctx context.Context, host p2phost.Host, routingOption RoutingOption) error {
272
	// setup diagnostics service
273
	n.Diagnostics = diag.NewDiagnostics(n.Identity, host)
Jeromy's avatar
Jeromy committed
274
	n.Ping = ping.NewPingService(host)
275 276

	// setup routing service
277
	r, err := routingOption(ctx, host, n.Repo.Datastore())
Jeromy's avatar
Jeromy committed
278
	if err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
279
		return err
280
	}
Jeromy's avatar
Jeromy committed
281
	n.Routing = r
282

283 284 285
	// Wrap standard peer host with routing system to allow unknown peer lookups
	n.PeerHost = rhost.Wrap(host, n.Routing)

286 287
	// setup exchange service
	const alwaysSendToPeer = true // use YesManStrategy
288
	bitswapNetwork := bsnet.NewFromIpfsHost(n.PeerHost, n.Routing)
289 290
	n.Exchange = bitswap.New(ctx, n.Identity, bitswapNetwork, n.Blockstore, alwaysSendToPeer)

291 292 293 294 295
	size, err := n.getCacheSize()
	if err != nil {
		return err
	}

296
	// setup name system
297
	n.Namesys = namesys.NewNameSystem(n.Routing, n.Repo.Datastore(), size)
298

Jeromy's avatar
Jeromy committed
299
	// setup ipns republishing
300 301 302 303
	err = n.setupIpnsRepublisher()
	if err != nil {
		return err
	}
Jeromy's avatar
Jeromy committed
304

305 306 307
	return nil
}

308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
// getCacheSize returns cache life and cache size
func (n *IpfsNode) getCacheSize() (int, error) {
	cfg, err := n.Repo.Config()
	if err != nil {
		return 0, err
	}

	cs := cfg.Ipns.ResolveCacheSize
	if cs == 0 {
		cs = 128
	}
	if cs < 0 {
		return 0, fmt.Errorf("cannot specify negative resolve cache size")
	}
	return cs, nil
}

325
func (n *IpfsNode) setupIpnsRepublisher() error {
Jeromy's avatar
Jeromy committed
326 327 328 329
	cfg, err := n.Repo.Config()
	if err != nil {
		return err
	}
330 331 332 333

	n.IpnsRepub = ipnsrp.NewRepublisher(n.Routing, n.Repo.Datastore(), n.Peerstore)
	n.IpnsRepub.AddName(n.Identity)

Jeromy's avatar
Jeromy committed
334 335 336 337 338 339
	if cfg.Ipns.RepublishPeriod != "" {
		d, err := time.ParseDuration(cfg.Ipns.RepublishPeriod)
		if err != nil {
			return fmt.Errorf("failure to parse config setting IPNS.RepublishPeriod: %s", err)
		}

340
		if !u.Debug && (d < time.Minute || d > (time.Hour*24)) {
Jeromy's avatar
Jeromy committed
341 342 343 344 345 346
			return fmt.Errorf("config setting IPNS.RepublishPeriod is not between 1min and 1day: %s", d)
		}

		n.IpnsRepub.Interval = d
	}

347 348 349 350 351 352 353 354 355
	if cfg.Ipns.RecordLifetime != "" {
		d, err := time.ParseDuration(cfg.Ipns.RepublishPeriod)
		if err != nil {
			return fmt.Errorf("failure to parse config setting IPNS.RecordLifetime: %s", err)
		}

		n.IpnsRepub.RecordLifetime = d
	}

Jeromy's avatar
Jeromy committed
356 357
	n.Process().Go(n.IpnsRepub.Run)

358 359 360
	return nil
}

361 362 363 364 365 366 367 368 369 370 371 372
// Process returns the Process object
func (n *IpfsNode) Process() goprocess.Process {
	return n.proc
}

// Close calls Close() on the Process object
func (n *IpfsNode) Close() error {
	return n.proc.Close()
}

// Context returns the IpfsNode context
func (n *IpfsNode) Context() context.Context {
373 374 375
	if n.ctx == nil {
		n.ctx = context.TODO()
	}
376 377 378
	return n.ctx
}

379 380
// teardown closes owned children. If any errors occur, this function returns
// the first error.
Brian Tiger Chow's avatar
Brian Tiger Chow committed
381
func (n *IpfsNode) teardown() error {
382
	log.Debug("core is shutting down...")
383 384
	// owned objects are closed in this teardown to ensure that they're closed
	// regardless of which constructor was used to add them to the node.
Jeromy's avatar
Jeromy committed
385 386
	var closers []io.Closer

387
	// NOTE: The order that objects are added(closed) matters, if an object
Jeromy's avatar
Jeromy committed
388 389 390 391 392
	// needs to use another during its shutdown/cleanup process, it should be
	// closed before that other object

	if n.FilesRoot != nil {
		closers = append(closers, n.FilesRoot)
Jeromy's avatar
Jeromy committed
393
	}
394

395 396 397 398
	if n.Exchange != nil {
		closers = append(closers, n.Exchange)
	}

399
	if n.Mounts.Ipfs != nil && !n.Mounts.Ipfs.IsActive() {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
400 401
		closers = append(closers, mount.Closer(n.Mounts.Ipfs))
	}
402
	if n.Mounts.Ipns != nil && !n.Mounts.Ipns.IsActive() {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
403 404 405
		closers = append(closers, mount.Closer(n.Mounts.Ipns))
	}

Jeromy's avatar
Jeromy committed
406 407 408 409
	if dht, ok := n.Routing.(*dht.IpfsDHT); ok {
		closers = append(closers, dht.Process())
	}

Jeromy's avatar
Jeromy committed
410 411 412 413
	if n.Blocks != nil {
		closers = append(closers, n.Blocks)
	}

Jeromy's avatar
Jeromy committed
414 415
	if n.Bootstrapper != nil {
		closers = append(closers, n.Bootstrapper)
416 417
	}

Jeromy's avatar
Jeromy committed
418 419
	if n.PeerHost != nil {
		closers = append(closers, n.PeerHost)
420
	}
421

Jeromy's avatar
Jeromy committed
422 423 424
	// Repo closed last, most things need to preserve state here
	closers = append(closers, n.Repo)

425
	var errs []error
426
	for _, closer := range closers {
427 428
		if err := closer.Close(); err != nil {
			errs = append(errs, err)
429 430 431 432
		}
	}
	if len(errs) > 0 {
		return errs[0]
Brian Tiger Chow's avatar
Brian Tiger Chow committed
433 434
	}
	return nil
Brian Tiger Chow's avatar
Brian Tiger Chow committed
435 436
}

Brian Tiger Chow's avatar
Brian Tiger Chow committed
437
func (n *IpfsNode) OnlineMode() bool {
438 439 440 441 442 443
	switch n.mode {
	case onlineMode:
		return true
	default:
		return false
	}
Brian Tiger Chow's avatar
Brian Tiger Chow committed
444 445
}

446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
func (n *IpfsNode) SetLocal(isLocal bool) {
	if isLocal {
		n.mode = localMode
	}
	n.localModeSet = true
}

func (n *IpfsNode) LocalMode() bool {
	if !n.localModeSet {
		// programmer error should not happen
		panic("local mode not set")
	}
	switch n.mode {
	case localMode:
		return true
	default:
		return false
	}
}

466
func (n *IpfsNode) Bootstrap(cfg BootstrapConfig) error {
467 468

	// TODO what should return value be when in offlineMode?
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
469 470 471 472
	if n.Routing == nil {
		return nil
	}

473 474 475 476 477 478 479
	if n.Bootstrapper != nil {
		n.Bootstrapper.Close() // stop previous bootstrap process.
	}

	// if the caller did not specify a bootstrap peer function, get the
	// freshest bootstrap peers from config. this responds to live changes.
	if cfg.BootstrapPeers == nil {
Jeromy's avatar
Jeromy committed
480
		cfg.BootstrapPeers = func() []pstore.PeerInfo {
481
			ps, err := n.loadBootstrapPeers()
482
			if err != nil {
483
				log.Warning("failed to parse bootstrap peers from config")
484 485 486 487 488 489 490 491 492
				return nil
			}
			return ps
		}
	}

	var err error
	n.Bootstrapper, err = Bootstrap(n, cfg)
	return err
493 494
}

495 496
func (n *IpfsNode) loadID() error {
	if n.Identity != "" {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
497
		return errors.New("identity already loaded")
498 499
	}

500 501 502 503 504 505
	cfg, err := n.Repo.Config()
	if err != nil {
		return err
	}

	cid := cfg.Identity.PeerID
506
	if cid == "" {
507
		return errors.New("identity was not set in config (was 'ipfs init' run?)")
508 509
	}
	if len(cid) == 0 {
510
		return errors.New("no peer ID in config! (was 'ipfs init' run?)")
511 512
	}

513 514 515
	n.Identity = peer.ID(b58.Decode(cid))
	return nil
}
516

517 518 519 520 521 522 523 524
func (n *IpfsNode) GetKey(name string) (ic.PrivKey, error) {
	if name == "self" {
		return n.PrivateKey, nil
	} else {
		return n.Repo.Keystore().Get(name)
	}
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
525
func (n *IpfsNode) LoadPrivateKey() error {
526
	if n.Identity == "" || n.Peerstore == nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
527
		return errors.New("loaded private key out of order.")
528 529
	}

530
	if n.PrivateKey != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
531
		return errors.New("private key already loaded")
532 533
	}

534 535 536 537 538 539
	cfg, err := n.Repo.Config()
	if err != nil {
		return err
	}

	sk, err := loadPrivateKey(&cfg.Identity, n.Identity)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
540
	if err != nil {
541
		return err
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
542
	}
543

544 545
	n.PrivateKey = sk
	n.Peerstore.AddPrivKey(n.Identity, n.PrivateKey)
Jeromy's avatar
Jeromy committed
546 547 548 549
	n.Peerstore.AddPubKey(n.Identity, sk.GetPublic())
	return nil
}

Jeromy's avatar
Jeromy committed
550
func (n *IpfsNode) loadBootstrapPeers() ([]pstore.PeerInfo, error) {
551 552 553 554 555 556
	cfg, err := n.Repo.Config()
	if err != nil {
		return nil, err
	}

	parsed, err := cfg.BootstrapPeers()
557 558 559 560 561 562
	if err != nil {
		return nil, err
	}
	return toPeerInfos(parsed), nil
}

Jeromy's avatar
Jeromy committed
563
func (n *IpfsNode) loadFilesRoot() error {
Jeromy's avatar
Jeromy committed
564
	dsk := ds.NewKey("/local/filesroot")
Jeromy's avatar
Jeromy committed
565 566
	pf := func(ctx context.Context, c *cid.Cid) error {
		return n.Repo.Datastore().Put(dsk, c.Bytes())
Jeromy's avatar
Jeromy committed
567 568
	}

569
	var nd *merkledag.ProtoNode
Jeromy's avatar
Jeromy committed
570 571 572 573
	val, err := n.Repo.Datastore().Get(dsk)

	switch {
	case err == ds.ErrNotFound || val == nil:
574
		nd = ft.EmptyDirNode()
Jeromy's avatar
Jeromy committed
575 576 577 578 579
		_, err := n.DAG.Add(nd)
		if err != nil {
			return fmt.Errorf("failure writing to dagstore: %s", err)
		}
	case err == nil:
Jeromy's avatar
Jeromy committed
580 581 582 583 584
		c, err := cid.Cast(val.([]byte))
		if err != nil {
			return err
		}

585
		rnd, err := n.DAG.Get(n.Context(), c)
Jeromy's avatar
Jeromy committed
586 587 588
		if err != nil {
			return fmt.Errorf("error loading filesroot from DAG: %s", err)
		}
589 590 591 592 593 594 595

		pbnd, ok := rnd.(*merkledag.ProtoNode)
		if !ok {
			return merkledag.ErrNotProtobuf
		}

		nd = pbnd
Jeromy's avatar
Jeromy committed
596 597 598 599 600 601 602 603 604 605 606 607 608
	default:
		return err
	}

	mr, err := mfs.NewRoot(n.Context(), n.DAG, nd, pf)
	if err != nil {
		return err
	}

	n.FilesRoot = mr
	return nil
}

Jeromy's avatar
Jeromy committed
609 610 611
// SetupOfflineRouting loads the local nodes private key and
// uses it to instantiate a routing system in offline mode.
// This is primarily used for offline ipns modifications.
Jeromy's avatar
Jeromy committed
612
func (n *IpfsNode) SetupOfflineRouting() error {
613 614 615 616
	if n.Routing != nil {
		// Routing was already set up
		return nil
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
617
	err := n.LoadPrivateKey()
Jeromy's avatar
Jeromy committed
618 619 620 621 622
	if err != nil {
		return err
	}

	n.Routing = offroute.NewOfflineRouter(n.Repo.Datastore(), n.PrivateKey)
623

624 625 626 627 628 629
	size, err := n.getCacheSize()
	if err != nil {
		return err
	}

	n.Namesys = namesys.NewNameSystem(n.Routing, n.Repo.Datastore(), size)
630

631
	return nil
632 633 634 635
}

func loadPrivateKey(cfg *config.Identity, id peer.ID) (ic.PrivKey, error) {
	sk, err := cfg.DecodePrivateKey("passphrase todo!")
636 637 638
	if err != nil {
		return nil, err
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
639

640 641 642 643
	id2, err := peer.IDFromPrivateKey(sk)
	if err != nil {
		return nil, err
	}
644

645 646
	if id2 != id {
		return nil, fmt.Errorf("private key in config does not match id: %s != %s", id, id2)
647 648
	}

649
	return sk, nil
650
}
651

652
func listenAddresses(cfg *config.Config) ([]ma.Multiaddr, error) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
653 654 655
	var listen []ma.Multiaddr
	for _, addr := range cfg.Addresses.Swarm {
		maddr, err := ma.NewMultiaddr(addr)
656
		if err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
657
			return nil, fmt.Errorf("Failure to parse config.Addresses.Swarm: %s", cfg.Addresses.Swarm)
658
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
659
		listen = append(listen, maddr)
660 661 662 663
	}

	return listen, nil
}
664

665
type HostOption func(ctx context.Context, id peer.ID, ps pstore.Peerstore, bwr metrics.Reporter, fs []*net.IPNet, tpt smux.Transport) (p2phost.Host, error)
Jeromy's avatar
Jeromy committed
666 667 668

var DefaultHostOption HostOption = constructPeerHost

669
// isolates the complex initialization steps
670
func constructPeerHost(ctx context.Context, id peer.ID, ps pstore.Peerstore, bwr metrics.Reporter, fs []*net.IPNet, tpt smux.Transport) (p2phost.Host, error) {
671 672

	// no addresses to begin with. we'll start later.
673
	swrm, err := swarm.NewSwarmWithProtector(ctx, nil, id, ps, nil, tpt, bwr)
674
	if err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
675
		return nil, err
676
	}
677

678 679
	network := (*swarm.Network)(swrm)

680 681 682 683
	for _, f := range fs {
		network.Swarm().Filters.AddDialFilter(f)
	}

Jeromy's avatar
Jeromy committed
684 685
	host := p2pbhost.New(network, p2pbhost.NATPortMap, bwr)

686 687 688 689 690 691 692
	return host, nil
}

// startListening on the network addresses
func startListening(ctx context.Context, host p2phost.Host, cfg *config.Config) error {
	listenAddrs, err := listenAddresses(cfg)
	if err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
693
		return err
694 695
	}

696
	// make sure we error out if our config does not have addresses we can use
697
	log.Debugf("Config.Addresses.Swarm:%s", listenAddrs)
698
	filteredAddrs := addrutil.FilterUsableAddrs(listenAddrs)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
699
	log.Debugf("Config.Addresses.Swarm:%s (filtered)", filteredAddrs)
700
	if len(filteredAddrs) < 1 {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
701
		return fmt.Errorf("addresses in config not usable: %s", listenAddrs)
702 703
	}

704 705 706
	// Actually start listening:
	if err := host.Network().Listen(filteredAddrs...); err != nil {
		return err
707 708
	}

709
	// list out our addresses
710
	addrs, err := host.Network().InterfaceListenAddresses()
711
	if err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
712
		return err
713
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
714
	log.Infof("Swarm listening at: %s", addrs)
715
	return nil
716
}
717

Jeromy's avatar
Jeromy committed
718
func constructDHTRouting(ctx context.Context, host p2phost.Host, dstore repo.Datastore) (routing.IpfsRouting, error) {
719
	dhtRouting := dht.NewDHT(ctx, host, dstore)
720
	dhtRouting.Validator[IpnsValidatorTag] = namesys.IpnsRecordValidator
721
	dhtRouting.Selector[IpnsValidatorTag] = namesys.IpnsSelectorFunc
722 723
	return dhtRouting, nil
}
Jeromy's avatar
Jeromy committed
724

Jeromy's avatar
Jeromy committed
725 726 727 728 729 730 731
func constructClientDHTRouting(ctx context.Context, host p2phost.Host, dstore repo.Datastore) (routing.IpfsRouting, error) {
	dhtRouting := dht.NewDHTClient(ctx, host, dstore)
	dhtRouting.Validator[IpnsValidatorTag] = namesys.IpnsRecordValidator
	dhtRouting.Selector[IpnsValidatorTag] = namesys.IpnsSelectorFunc
	return dhtRouting, nil
}

Jeromy's avatar
Jeromy committed
732
type RoutingOption func(context.Context, p2phost.Host, repo.Datastore) (routing.IpfsRouting, error)
Jeromy's avatar
Jeromy committed
733

Jeromy's avatar
Jeromy committed
734
type DiscoveryOption func(context.Context, p2phost.Host) (discovery.Service, error)
Jeromy's avatar
Jeromy committed
735

736
var DHTOption RoutingOption = constructDHTRouting
Jeromy's avatar
Jeromy committed
737
var DHTClientOption RoutingOption = constructClientDHTRouting
738
var NilRouterOption RoutingOption = nilrouting.ConstructNilRouting