core.go 7.09 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
)

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

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

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

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

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

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
48
	// the local datastore
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
49
	Datastore ds.ThreadSafeDatastore
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
50

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

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

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

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

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

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

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

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

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

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

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

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

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

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

107 108 109
	// derive this from a higher context.
	ctx := context.TODO()
	n = &IpfsNode{
Brian Tiger Chow's avatar
Brian Tiger Chow committed
110
		onlineMode:    online,
111 112 113 114 115 116
		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
117 118 119
		return nil, err
	}

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

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

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

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

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

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

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

157 158
		// setup routing service
		dhtRouting := dht.NewDHT(ctx, n.Identity, n.Peerstore, n.Network, dhtService, n.Datastore)
159
		// TODO(brian): perform this inside NewDHT factory method
160 161
		dhtService.SetHandler(dhtRouting) // wire the handler to the service.
		n.Routing = dhtRouting
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
162
		n.AddCloserChild(dhtRouting)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
163

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

169
		go initConnections(ctx, n.Config, n.Peerstore, dhtRouting)
170
	}
171

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

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

187
	success = true
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
188
	return n, nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
189
}
190

Brian Tiger Chow's avatar
Brian Tiger Chow committed
191 192
func (n *IpfsNode) OnlineMode() bool {
	return n.onlineMode
Brian Tiger Chow's avatar
Brian Tiger Chow committed
193 194
}

195
func initIdentity(cfg *config.Config, peers peer.Peerstore, online bool) (peer.Peer, error) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
196
	if cfg.Identity.PeerID == "" {
197 198 199
		return nil, errors.New("Identity was not set in config (was ipfs init run?)")
	}

200 201 202 203
	if len(cfg.Identity.PeerID) == 0 {
		return nil, errors.New("No peer ID in config! (was ipfs init run?)")
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
204 205 206 207 208 209 210
	// get peer from peerstore (so it is constructed there)
	id := peer.ID(b58.Decode(cfg.Identity.PeerID))
	peer, err := peers.Get(id)
	if err != nil {
		return nil, err
	}

211
	// when not online, don't need to parse private keys (yet)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
212
	if online {
213 214 215 216 217
		skb, err := base64.StdEncoding.DecodeString(cfg.Identity.PrivKey)
		if err != nil {
			return nil, err
		}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
218
		if err := peer.LoadAndVerifyKeyPair(skb); err != nil {
219 220
			return nil, err
		}
221 222
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
223
	return peer, nil
224
}
225

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
226
func initConnections(ctx context.Context, cfg *config.Config, pstore peer.Peerstore, route *dht.IpfsDHT) {
227
	for _, p := range cfg.Bootstrap {
228
		if p.PeerID == "" {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
229
			log.Errorf("error: peer does not include PeerID. %v", p)
230 231
		}

232 233
		maddr, err := ma.NewMultiaddr(p.Address)
		if err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
234
			log.Error(err)
235 236 237
			continue
		}

238
		// setup peer
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
239 240
		npeer, err := pstore.Get(peer.DecodePrettyID(p.PeerID))
		if err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
241
			log.Errorf("Bootstrapping error: %v", err)
242 243
			continue
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
244
		npeer.AddAddress(maddr)
245

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
246
		if _, err = route.Connect(ctx, npeer); err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
247
			log.Errorf("Bootstrapping error: %v", err)
248 249 250
		}
	}
}
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265

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
}