core.go 7.58 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
	inet "github.com/jbenet/go-ipfs/net"
21
	handshake "github.com/jbenet/go-ipfs/net/handshake"
22
	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"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
30
	ctxc "github.com/jbenet/go-ipfs/util/ctxcloser"
Brian Tiger Chow's avatar
Brian Tiger Chow committed
31
	"github.com/jbenet/go-ipfs/util/debugerror"
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
51
	Datastore u.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 {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
106
		return nil, debugerror.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 113
		onlineMode: online,
		Config:     cfg,
114
	}
115
	n.ContextCloser = ctxc.NewContextCloser(ctx, n.teardown)
116 117 118

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

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

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
		listenAddrs, err := listenAddresses(cfg)
		if err != nil {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
146
			return nil, debugerror.Wrap(err)
147 148 149
		}

		n.Network, err = inet.NewIpfsNetwork(ctx, listenAddrs, n.Identity, n.Peerstore, muxMap)
150
		if err != nil {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
151
			return nil, debugerror.Wrap(err)
152
		}
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
	if err != nil {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
180
		return nil, debugerror.Wrap(err)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
181 182
	}

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) teardown() error {
	if err := n.Datastore.Close(); err != nil {
		return err
	}
	return nil
Brian Tiger Chow's avatar
Brian Tiger Chow committed
200 201
}

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

206 207
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
208
		return nil, debugerror.New("Identity was not set in config (was ipfs init run?)")
209 210
	}

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

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

Jeromy's avatar
Jeromy committed
227
	self.SetVersions(handshake.ClientVersion, handshake.IpfsVersion.String())
228

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

Brian Tiger Chow's avatar
Brian Tiger Chow committed
236
		if err := self.LoadAndVerifyKeyPair(skb); err != nil {
237 238
			return nil, err
		}
239 240
	}

Brian Tiger Chow's avatar
Brian Tiger Chow committed
241
	return self, nil
242
}
243

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

250 251
		maddr, err := ma.NewMultiaddr(p.Address)
		if err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
252
			log.Error(err)
253 254 255
			continue
		}

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
264
		if _, err = route.Connect(ctx, npeer); err != nil {
265
			log.Criticalf("Bootstrapping error: %v", err)
266 267 268
		}
	}
}
269 270 271

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

272 273 274 275 276
	var err error
	listen := make([]ma.Multiaddr, len(cfg.Addresses.Swarm))
	for i, addr := range cfg.Addresses.Swarm {

		listen[i], err = ma.NewMultiaddr(addr)
277
		if err != nil {
278
			return nil, fmt.Errorf("Failure to parse config.Addresses.Swarm[%d]: %s", i, cfg.Addresses.Swarm)
279 280 281 282 283
		}
	}

	return listen, nil
}