core.go 8.12 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 31
	routing "github.com/jbenet/go-ipfs/routing"
	dht "github.com/jbenet/go-ipfs/routing/dht"
	u "github.com/jbenet/go-ipfs/util"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
32
	ctxc "github.com/jbenet/go-ipfs/util/ctxcloser"
33
	debugerror "github.com/jbenet/go-ipfs/util/debugerror"
Jeromy's avatar
Jeromy committed
34
	"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 u.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(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 114 115
	// derive this from a higher context.
	ctx := context.TODO()
	n = &IpfsNode{
Brian Tiger Chow's avatar
Brian Tiger Chow committed
116 117
		onlineMode: online,
		Config:     cfg,
118
	}
119
	n.ContextCloser = ctxc.NewContextCloser(ctx, n.teardown)
Jeromy's avatar
Jeromy committed
120
	ctx = n.Context()
121 122 123

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

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

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

137
	// setup online services
138
	if online {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
139

140 141
		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
142
		diagService := netservice.NewService(ctx, nil)     // nil handler for now, need to patch it
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
143

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

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

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

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

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

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

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

180
		n.Exchange = bitswap.New(ctx, n.Identity, bitswapNetwork, n.Routing, blockstore, alwaysSendToPeer)
181

182
		go initConnections(ctx, n.Config, n.Peerstore, dhtRouting)
183
	}
184

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

192 193 194
	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
195
	if err != nil {
196
		n.Pinning = pin.NewPinner(n.Datastore, n.DAG)
Jeromy's avatar
Jeromy committed
197
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
198 199
	n.Resolver = &path.Resolver{DAG: n.DAG}

200
	success = true
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
201
	return n, nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
202
}
203

Brian Tiger Chow's avatar
Brian Tiger Chow committed
204 205 206 207 208
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
209 210
}

Brian Tiger Chow's avatar
Brian Tiger Chow committed
211 212
func (n *IpfsNode) OnlineMode() bool {
	return n.onlineMode
Brian Tiger Chow's avatar
Brian Tiger Chow committed
213 214
}

215 216
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
217
		return nil, debugerror.New("Identity was not set in config (was ipfs init run?)")
218 219
	}

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

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

Jeromy's avatar
Jeromy committed
236
	self.SetVersions(handshake.ClientVersion, handshake.IpfsVersion.String())
237

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

Brian Tiger Chow's avatar
Brian Tiger Chow committed
245
		if err := self.LoadAndVerifyKeyPair(skb); err != nil {
246 247
			return nil, err
		}
248 249
	}

Brian Tiger Chow's avatar
Brian Tiger Chow committed
250
	return self, nil
251
}
252

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
253
func initConnections(ctx context.Context, cfg *config.Config, pstore peer.Peerstore, route *dht.IpfsDHT) {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
254 255
	// TODO consider stricter error handling
	// TODO consider Criticalf error logging
256
	for _, p := range cfg.Bootstrap {
257
		if p.PeerID == "" {
258
			log.Criticalf("error: peer does not include PeerID. %v", p)
259 260
		}

261 262
		maddr, err := ma.NewMultiaddr(p.Address)
		if err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
263
			log.Error(err)
264 265 266
			continue
		}

267
		// setup peer
268 269 270 271
		id, err := peer.DecodePrettyID(p.PeerID)
		if err != nil {
			// return err
		}
272
		npeer, err := pstore.FindOrCreate(id)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
273
		if err != nil {
274
			log.Criticalf("Bootstrapping error: %v", err)
275 276
			continue
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
277
		npeer.AddAddress(maddr)
278

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
279
		if _, err = route.Connect(ctx, npeer); err != nil {
280
			log.Criticalf("Bootstrapping error: %v", err)
Brian Tiger Chow's avatar
Brian Tiger Chow committed
281
			continue
282
		}
Brian Tiger Chow's avatar
Brian Tiger Chow committed
283
		log.Event(ctx, "bootstrap", npeer)
284 285
	}
}
286 287 288

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

289 290 291 292 293
	var err error
	listen := make([]ma.Multiaddr, len(cfg.Addresses.Swarm))
	for i, addr := range cfg.Addresses.Swarm {

		listen[i], err = ma.NewMultiaddr(addr)
294
		if err != nil {
295
			return nil, fmt.Errorf("Failure to parse config.Addresses.Swarm[%d]: %s", i, cfg.Addresses.Swarm)
296 297 298 299 300
		}
	}

	return listen, nil
}