core.go 7.34 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"
5
	"errors"
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
6
	"fmt"
7

8
	context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
9
	b58 "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-base58"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
10
	ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
11
	ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
12

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

Jeromy's avatar
Jeromy committed
34 35
const IpnsValidatorTag = "ipns"

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
36
var log = u.Logger("core")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
37

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

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
41 42
	// the node's configuration
	Config *config.Config
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
43

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
44
	// the local node's identity
45
	Identity peer.Peer
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
46

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
47
	// storage for other Peer instances
48
	Peerstore peer.Peerstore
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
49

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
50
	// the local datastore
Brian Tiger Chow's avatar
Brian Tiger Chow committed
51
	Datastore ds.ThreadSafeDatastoreCloser
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
52

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
53
	// the network message stream
54
	Network inet.Network
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
55

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
56
	// the routing system. recommend ipfs-dht
57
	Routing routing.IpfsRouting
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
58

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
59
	// the block exchange + strategy (bitswap)
60
	Exchange exchange.Interface
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
61

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
62
	// the block service, get/add blocks.
63
	Blocks *bserv.BlockService
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
64

65
	// the merkle dag service, get/add objects.
66
	DAG merkledag.DAGService
67

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
68
	// the path resolution system
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
69
	Resolver *path.Resolver
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
70

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
71
	// the name system, resolves paths to hashes
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
72
	Namesys namesys.NameSystem
Jeromy's avatar
Jeromy committed
73 74 75

	// the diagnostics service
	Diagnostics *diag.Diagnostics
Jeromy's avatar
Jeromy committed
76 77 78

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

80 81 82
	// current mount state, if any.
	Mounts Mounts

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
83
	ctxc.ContextCloser
Brian Tiger Chow's avatar
Brian Tiger Chow committed
84

Brian Tiger Chow's avatar
Brian Tiger Chow committed
85
	onlineMode bool // alternatively, offline
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
86 87
}

88 89 90 91 92 93 94 95
// 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
96
// NewIpfsNode constructs a new IpfsNode based on the given config.
97
func NewIpfsNode(cfg *config.Config, online bool) (n *IpfsNode, err error) {
98 99
	success := false // flip to true after all sub-system inits succeed
	defer func() {
100 101
		if !success && n != nil {
			n.Close()
102 103
		}
	}()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
104

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
105
	if cfg == nil {
Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
106
		return nil, fmt.Errorf("configuration required")
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
107 108
	}

109 110 111
	// derive this from a higher context.
	ctx := context.TODO()
	n = &IpfsNode{
Brian Tiger Chow's avatar
Brian Tiger Chow committed
112
		onlineMode:    online,
113 114 115 116 117 118
		Config:        cfg,
		ContextCloser: ctxc.NewContextCloser(ctx, nil),
	}

	// setup datastore.
	if n.Datastore, err = makeDatastore(cfg.Datastore); err != nil {
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
119 120 121
		return nil, err
	}

122 123
	// setup peerstore + local peer identity
	n.Peerstore = peer.NewPeerstore()
124
	n.Identity, err = initIdentity(&n.Config.Identity, n.Peerstore, online)
125 126 127 128
	if err != nil {
		return nil, err
	}

129
	// setup online services
130
	if online {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
131

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

136
		muxMap := &mux.ProtocolMap{
Jeromy's avatar
Jeromy committed
137 138 139
			mux.ProtocolID_Routing:    dhtService,
			mux.ProtocolID_Exchange:   exchangeService,
			mux.ProtocolID_Diagnostic: diagService,
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
140
			// add protocol services here.
141 142 143
		}

		// setup the network
144 145 146 147 148 149
		listenAddrs, err := listenAddresses(cfg)
		if err != nil {
			return nil, err
		}

		n.Network, err = inet.NewIpfsNetwork(ctx, listenAddrs, n.Identity, n.Peerstore, muxMap)
150 151 152
		if err != nil {
			return nil, err
		}
153
		n.AddCloserChild(n.Network)
154

155 156 157
		// setup diagnostics service
		n.Diagnostics = diag.NewDiagnostics(n.Identity, n.Network, diagService)
		diagService.SetHandler(n.Diagnostics)
Jeromy's avatar
Jeromy committed
158

159 160
		// setup routing service
		dhtRouting := dht.NewDHT(ctx, n.Identity, n.Peerstore, n.Network, dhtService, n.Datastore)
Jeromy's avatar
Jeromy committed
161
		dhtRouting.Validators[IpnsValidatorTag] = namesys.ValidateIpnsRecord
Jeromy's avatar
Jeromy committed
162

163
		// TODO(brian): perform this inside NewDHT factory method
164 165
		dhtService.SetHandler(dhtRouting) // wire the handler to the service.
		n.Routing = dhtRouting
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
166
		n.AddCloserChild(dhtRouting)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
167

168
		// setup exchange service
169
		const alwaysSendToPeer = true // use YesManStrategy
170 171
		bitswapNetwork := bsnet.NewFromIpfsNetwork(exchangeService, n.Network)
		n.Exchange = bitswap.New(ctx, n.Identity, bitswapNetwork, n.Routing, n.Datastore, alwaysSendToPeer)
172

173
		go initConnections(ctx, n.Config, n.Peerstore, dhtRouting)
174
	}
175

176 177
	// TODO(brian): when offline instantiate the BlockService with a bitswap
	// session that simply doesn't return blocks
178
	n.Blocks, err = bserv.NewBlockService(n.Datastore, n.Exchange)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
179 180 181 182
	if err != nil {
		return nil, err
	}

183 184 185
	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
186
	if err != nil {
187
		n.Pinning = pin.NewPinner(n.Datastore, n.DAG)
Jeromy's avatar
Jeromy committed
188
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
189 190
	n.Resolver = &path.Resolver{DAG: n.DAG}

191
	success = true
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
192
	return n, nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
193
}
194

Brian Tiger Chow's avatar
Brian Tiger Chow committed
195 196 197 198 199
func (n *IpfsNode) Close() {
	n.ContextCloser.Close()
	n.Datastore.Close()
}

Brian Tiger Chow's avatar
Brian Tiger Chow committed
200 201
func (n *IpfsNode) OnlineMode() bool {
	return n.onlineMode
Brian Tiger Chow's avatar
Brian Tiger Chow committed
202 203
}

204 205
func initIdentity(cfg *config.Identity, peers peer.Peerstore, online bool) (peer.Peer, error) {
	if cfg.PeerID == "" {
206 207 208
		return nil, errors.New("Identity was not set in config (was ipfs init run?)")
	}

209
	if len(cfg.PeerID) == 0 {
210 211 212
		return nil, errors.New("No peer ID in config! (was ipfs init run?)")
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
213
	// get peer from peerstore (so it is constructed there)
214
	id := peer.ID(b58.Decode(cfg.PeerID))
Brian Tiger Chow's avatar
Brian Tiger Chow committed
215
	self, err := peers.Get(id)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
216 217 218
	if err != nil {
		return nil, err
	}
219 220 221 222 223
	self.SetType(peer.Local)
	self, err = peers.Add(self)
	if err != nil {
		return nil, err
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
224

225
	// when not online, don't need to parse private keys (yet)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
226
	if online {
227
		skb, err := base64.StdEncoding.DecodeString(cfg.PrivKey)
228 229 230 231
		if err != nil {
			return nil, err
		}

Brian Tiger Chow's avatar
Brian Tiger Chow committed
232
		if err := self.LoadAndVerifyKeyPair(skb); err != nil {
233 234
			return nil, err
		}
235 236
	}

Brian Tiger Chow's avatar
Brian Tiger Chow committed
237
	return self, nil
238
}
239

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
240
func initConnections(ctx context.Context, cfg *config.Config, pstore peer.Peerstore, route *dht.IpfsDHT) {
241
	for _, p := range cfg.Bootstrap {
242
		if p.PeerID == "" {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
243
			log.Errorf("error: peer does not include PeerID. %v", p)
244 245
		}

246 247
		maddr, err := ma.NewMultiaddr(p.Address)
		if err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
248
			log.Error(err)
249 250 251
			continue
		}

252
		// setup peer
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
253 254
		npeer, err := pstore.Get(peer.DecodePrettyID(p.PeerID))
		if err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
255
			log.Errorf("Bootstrapping error: %v", err)
256 257
			continue
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
258
		npeer.AddAddress(maddr)
259

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
260
		if _, err = route.Connect(ctx, npeer); err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
261
			log.Errorf("Bootstrapping error: %v", err)
262 263 264
		}
	}
}
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279

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

	if len(cfg.Addresses.Swarm) > 0 {
		maddr, err := ma.NewMultiaddr(cfg.Addresses.Swarm)
		if err != nil {
			return nil, fmt.Errorf("Failure to parse config.Addresses.Swarm: %s", cfg.Addresses.Swarm)
		}

		listen = append(listen, maddr)
	}

	return listen, nil
}