core.go 7.71 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1 2
package core

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
3
import (
4
	"encoding/base64"
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
5
	"fmt"
6

7
	context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
8 9
	b58 "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-base58"
	ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
10

11
	bstore "github.com/jbenet/go-ipfs/blocks/blockstore"
12
	bserv "github.com/jbenet/go-ipfs/blockservice"
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
13
	config "github.com/jbenet/go-ipfs/config"
Jeromy's avatar
Jeromy committed
14
	diag "github.com/jbenet/go-ipfs/diagnostics"
15 16
	exchange "github.com/jbenet/go-ipfs/exchange"
	bitswap "github.com/jbenet/go-ipfs/exchange/bitswap"
17
	bsnet "github.com/jbenet/go-ipfs/exchange/bitswap/network"
18
	"github.com/jbenet/go-ipfs/exchange/offline"
19
	mount "github.com/jbenet/go-ipfs/fuse/mount"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
20
	merkledag "github.com/jbenet/go-ipfs/merkledag"
Jeromy's avatar
Jeromy committed
21
	namesys "github.com/jbenet/go-ipfs/namesys"
22
	inet "github.com/jbenet/go-ipfs/net"
23
	handshake "github.com/jbenet/go-ipfs/net/handshake"
24
	mux "github.com/jbenet/go-ipfs/net/mux"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
25
	netservice "github.com/jbenet/go-ipfs/net/service"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
26
	path "github.com/jbenet/go-ipfs/path"
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
27
	peer "github.com/jbenet/go-ipfs/peer"
Jeromy's avatar
Jeromy committed
28
	pin "github.com/jbenet/go-ipfs/pin"
29 30
	routing "github.com/jbenet/go-ipfs/routing"
	dht "github.com/jbenet/go-ipfs/routing/dht"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
31
	ctxc "github.com/jbenet/go-ipfs/util/ctxcloser"
32
	ds2 "github.com/jbenet/go-ipfs/util/datastore2"
33
	debugerror "github.com/jbenet/go-ipfs/util/debugerror"
34
	eventlog "github.com/jbenet/go-ipfs/util/eventlog"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
35 36
)

Jeromy's avatar
Jeromy committed
37
const IpnsValidatorTag = "ipns"
38
const kSizeBlockstoreWriteCache = 100
Jeromy's avatar
Jeromy committed
39

Brian Tiger Chow's avatar
Brian Tiger Chow committed
40
var log = eventlog.Logger("core")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
41

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

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
45 46
	// the node's configuration
	Config *config.Config
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
47

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
48
	// the local node's identity
49
	Identity peer.Peer
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
50

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
51
	// storage for other Peer instances
52
	Peerstore peer.Peerstore
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
53

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
54
	// the local datastore
55
	Datastore ds2.ThreadSafeDatastoreCloser
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
56

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
57
	// the network message stream
58
	Network inet.Network
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
59

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
60
	// the routing system. recommend ipfs-dht
61
	Routing routing.IpfsRouting
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
62

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
63
	// the block exchange + strategy (bitswap)
64
	Exchange exchange.Interface
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
65

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
66
	// the block service, get/add blocks.
67
	Blocks *bserv.BlockService
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
68

69
	// the merkle dag service, get/add objects.
70
	DAG merkledag.DAGService
71

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
72
	// the path resolution system
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
73
	Resolver *path.Resolver
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
74

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
75
	// the name system, resolves paths to hashes
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
76
	Namesys namesys.NameSystem
Jeromy's avatar
Jeromy committed
77 78 79

	// the diagnostics service
	Diagnostics *diag.Diagnostics
Jeromy's avatar
Jeromy committed
80 81 82

	// the pinning manager
	Pinning pin.Pinner
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
83

84 85 86
	// current mount state, if any.
	Mounts Mounts

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
87
	ctxc.ContextCloser
Brian Tiger Chow's avatar
Brian Tiger Chow committed
88

Brian Tiger Chow's avatar
Brian Tiger Chow committed
89
	onlineMode bool // alternatively, offline
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
90 91
}

92 93 94 95 96 97 98 99
// 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
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
100
// NewIpfsNode constructs a new IpfsNode based on the given config.
101
func NewIpfsNode(ctx context.Context, cfg *config.Config, online bool) (n *IpfsNode, err error) {
102 103
	success := false // flip to true after all sub-system inits succeed
	defer func() {
104 105
		if !success && n != nil {
			n.Close()
106 107
		}
	}()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
108

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
109
	if cfg == nil {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
110
		return nil, debugerror.Errorf("configuration required")
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
111 112
	}

113
	n = &IpfsNode{
Brian Tiger Chow's avatar
Brian Tiger Chow committed
114 115
		onlineMode: online,
		Config:     cfg,
116
	}
117
	n.ContextCloser = ctxc.NewContextCloser(ctx, n.teardown)
118
	ctx = n.ContextCloser.Context()
119 120 121

	// setup datastore.
	if n.Datastore, err = makeDatastore(cfg.Datastore); err != nil {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
122
		return nil, debugerror.Wrap(err)
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
123 124
	}

125 126
	// setup peerstore + local peer identity
	n.Peerstore = peer.NewPeerstore()
127
	n.Identity, err = initIdentity(&n.Config.Identity, n.Peerstore, online)
128
	if err != nil {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
129
		return nil, debugerror.Wrap(err)
130 131
	}

132 133
	blockstore, err := bstore.WriteCached(bstore.NewBlockstore(n.Datastore), kSizeBlockstoreWriteCache)
	n.Exchange = offline.Exchange(blockstore)
134

135
	// setup online services
136
	if online {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
137

138 139
		dhtService := netservice.NewService(ctx, nil)      // nil handler for now, need to patch it
		exchangeService := netservice.NewService(ctx, nil) // nil handler for now, need to patch it
140
		diagService := netservice.NewService(ctx, nil)     // nil handler for now, need to patch it
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
141

142
		muxMap := &mux.ProtocolMap{
Jeromy's avatar
Jeromy committed
143 144 145
			mux.ProtocolID_Routing:    dhtService,
			mux.ProtocolID_Exchange:   exchangeService,
			mux.ProtocolID_Diagnostic: diagService,
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
146
			// add protocol services here.
147 148 149
		}

		// setup the network
150 151
		listenAddrs, err := listenAddresses(cfg)
		if err != nil {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
152
			return nil, debugerror.Wrap(err)
153 154 155
		}

		n.Network, err = inet.NewIpfsNetwork(ctx, listenAddrs, n.Identity, n.Peerstore, muxMap)
156
		if err != nil {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
157
			return nil, debugerror.Wrap(err)
158
		}
159
		n.AddCloserChild(n.Network)
160

161 162 163
		// setup diagnostics service
		n.Diagnostics = diag.NewDiagnostics(n.Identity, n.Network, diagService)
		diagService.SetHandler(n.Diagnostics)
Jeromy's avatar
Jeromy committed
164

165 166
		// setup routing service
		dhtRouting := dht.NewDHT(ctx, n.Identity, n.Peerstore, n.Network, dhtService, n.Datastore)
Jeromy's avatar
Jeromy committed
167
		dhtRouting.Validators[IpnsValidatorTag] = namesys.ValidateIpnsRecord
Jeromy's avatar
Jeromy committed
168

169
		// TODO(brian): perform this inside NewDHT factory method
170 171
		dhtService.SetHandler(dhtRouting) // wire the handler to the service.
		n.Routing = dhtRouting
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
172
		n.AddCloserChild(dhtRouting)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
173

174
		// setup exchange service
175
		const alwaysSendToPeer = true // use YesManStrategy
176
		bitswapNetwork := bsnet.NewFromIpfsNetwork(exchangeService, n.Network)
177

178
		n.Exchange = bitswap.New(ctx, n.Identity, bitswapNetwork, n.Routing, blockstore, alwaysSendToPeer)
179

180
		// TODO consider moving connection supervision into the Network. We've
Brian Tiger Chow's avatar
Brian Tiger Chow committed
181 182 183 184 185
		// discussed improvements to this Node constructor. One improvement
		// would be to make the node configurable, allowing clients to inject
		// an Exchange, Network, or Routing component and have the constructor
		// manage the wiring. In that scenario, this dangling function is a bit
		// awkward.
186
		go superviseConnections(ctx, n.Network, dhtRouting, n.Peerstore, n.Config.Bootstrap)
187
	}
188

189 190
	// TODO(brian): when offline instantiate the BlockService with a bitswap
	// session that simply doesn't return blocks
191
	n.Blocks, err = bserv.New(blockstore, n.Exchange)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
192
	if err != nil {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
193
		return nil, debugerror.Wrap(err)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
194 195
	}

196 197 198
	n.DAG = merkledag.NewDAGService(n.Blocks)
	n.Namesys = namesys.NewNameSystem(n.Routing)
	n.Pinning, err = pin.LoadPinner(n.Datastore, n.DAG)
Jeromy's avatar
Jeromy committed
199
	if err != nil {
200
		n.Pinning = pin.NewPinner(n.Datastore, n.DAG)
Jeromy's avatar
Jeromy committed
201
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
202 203
	n.Resolver = &path.Resolver{DAG: n.DAG}

204
	success = true
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
205
	return n, nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
206
}
207

Brian Tiger Chow's avatar
Brian Tiger Chow committed
208 209 210 211 212
func (n *IpfsNode) teardown() error {
	if err := n.Datastore.Close(); err != nil {
		return err
	}
	return nil
Brian Tiger Chow's avatar
Brian Tiger Chow committed
213 214
}

Brian Tiger Chow's avatar
Brian Tiger Chow committed
215 216
func (n *IpfsNode) OnlineMode() bool {
	return n.onlineMode
Brian Tiger Chow's avatar
Brian Tiger Chow committed
217 218
}

219 220
func initIdentity(cfg *config.Identity, peers peer.Peerstore, online bool) (peer.Peer, error) {
	if cfg.PeerID == "" {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
221
		return nil, debugerror.New("Identity was not set in config (was ipfs init run?)")
222 223
	}

224
	if len(cfg.PeerID) == 0 {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
225
		return nil, debugerror.New("No peer ID in config! (was ipfs init run?)")
226 227
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
228
	// get peer from peerstore (so it is constructed there)
229
	id := peer.ID(b58.Decode(cfg.PeerID))
230
	self, err := peers.FindOrCreate(id)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
231 232 233
	if err != nil {
		return nil, err
	}
234 235 236 237 238
	self.SetType(peer.Local)
	self, err = peers.Add(self)
	if err != nil {
		return nil, err
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
239

Jeromy's avatar
Jeromy committed
240
	self.SetVersions(handshake.ClientVersion, handshake.IpfsVersion.String())
241

242
	// when not online, don't need to parse private keys (yet)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
243
	if online {
244
		skb, err := base64.StdEncoding.DecodeString(cfg.PrivKey)
245 246 247 248
		if err != nil {
			return nil, err
		}

Brian Tiger Chow's avatar
Brian Tiger Chow committed
249
		if err := self.LoadAndVerifyKeyPair(skb); err != nil {
250 251
			return nil, err
		}
252 253
	}

Brian Tiger Chow's avatar
Brian Tiger Chow committed
254
	return self, nil
255
}
256

257 258
func listenAddresses(cfg *config.Config) ([]ma.Multiaddr, error) {

259 260 261 262 263
	var err error
	listen := make([]ma.Multiaddr, len(cfg.Addresses.Swarm))
	for i, addr := range cfg.Addresses.Swarm {

		listen[i], err = ma.NewMultiaddr(addr)
264
		if err != nil {
265
			return nil, fmt.Errorf("Failure to parse config.Addresses.Swarm[%d]: %s", i, cfg.Addresses.Swarm)
266 267 268 269 270
		}
	}

	return listen, nil
}