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

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

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

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

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

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

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

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
49
	// the local datastore
50
	Datastore u.ThreadSafeDatastoreCloser
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
51

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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
}