core.go 6.67 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
	ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
10 11
	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"
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"
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 30
)

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

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

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
36 37
	// the node's configuration
	Config *config.Config
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
38

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
39
	// the local node's identity
40
	Identity peer.Peer
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
41

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
42
	// storage for other Peer instances
43
	Peerstore peer.Peerstore
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
44

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
45
	// the local datastore
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
46
	Datastore ds.ThreadSafeDatastore
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
47

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
48
	// the network message stream
49
	Network inet.Network
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
50

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
51
	// the routing system. recommend ipfs-dht
52
	Routing routing.IpfsRouting
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
53

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
54
	// the block exchange + strategy (bitswap)
55
	Exchange exchange.Interface
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
56

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
57
	// the block service, get/add blocks.
58
	Blocks *bserv.BlockService
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
59

60 61 62
	// the merkle dag service, get/add objects.
	DAG *merkledag.DAGService

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
63
	// the path resolution system
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
64
	Resolver *path.Resolver
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
65

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

	// the diagnostics service
	Diagnostics *diag.Diagnostics
Jeromy's avatar
Jeromy committed
71 72 73

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

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
76
// NewIpfsNode constructs a new IpfsNode based on the given config.
77
func NewIpfsNode(cfg *config.Config, online bool) (*IpfsNode, error) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
78 79 80
	// derive this from a higher context.
	// cancel if we need to fail early.
	ctx, cancel := context.WithCancel(context.TODO())
81 82 83 84 85 86
	success := false // flip to true after all sub-system inits succeed
	defer func() {
		if !success {
			cancel()
		}
	}()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
87

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
88
	if cfg == nil {
Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
89
		return nil, fmt.Errorf("configuration required")
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
90 91 92 93 94 95 96
	}

	d, err := makeDatastore(cfg.Datastore)
	if err != nil {
		return nil, err
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
97
	peerstore := peer.NewPeerstore()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
98
	local, err := initIdentity(cfg, peerstore, online)
99 100 101 102
	if err != nil {
		return nil, err
	}

103 104 105 106
	// FIXME(brian): This is a bit dangerous. If any of the vars declared in
	// this block are assigned inside of the "if online" block using the ":="
	// declaration syntax, the compiler permits re-declaration. This is rather
	// undesirable
107
	var (
108
		net inet.Network
109
		// TODO: refactor so we can use IpfsRouting interface instead of being DHT-specific
110
		route           *dht.IpfsDHT
111
		exchangeSession exchange.Interface
Jeromy's avatar
Jeromy committed
112
		diagnostics     *diag.Diagnostics
113
		network         inet.Network
114 115
	)

116
	if online {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
117

118 119
		dhtService := netservice.NewService(nil)      // nil handler for now, need to patch it
		exchangeService := netservice.NewService(nil) // nil handler for now, need to patch it
Jeromy's avatar
Jeromy committed
120
		diagService := netservice.NewService(nil)
121 122 123 124 125

		if err := dhtService.Start(ctx); err != nil {
			return nil, err
		}
		if err := exchangeService.Start(ctx); err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
126 127
			return nil, err
		}
128 129 130
		if err := diagService.Start(ctx); err != nil {
			return nil, err
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
131

132
		net, err = inet.NewIpfsNetwork(ctx, local, peerstore, &mux.ProtocolMap{
Jeromy's avatar
Jeromy committed
133 134 135
			mux.ProtocolID_Routing:    dhtService,
			mux.ProtocolID_Exchange:   exchangeService,
			mux.ProtocolID_Diagnostic: diagService,
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
136
			// add protocol services here.
137
		})
138 139 140
		if err != nil {
			return nil, err
		}
141
		network = net
142

Jeromy's avatar
Jeromy committed
143
		diagnostics = diag.NewDiagnostics(local, net, diagService)
144
		diagService.SetHandler(diagnostics)
Jeromy's avatar
Jeromy committed
145

146
		route = dht.NewDHT(ctx, local, peerstore, net, dhtService, d)
147
		// TODO(brian): perform this inside NewDHT factory method
148
		dhtService.SetHandler(route) // wire the handler to the service.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
149

150
		const alwaysSendToPeer = true // use YesManStrategy
151
		exchangeSession = bitswap.NetMessageSession(ctx, local, net, exchangeService, route, d, alwaysSendToPeer)
152

153
		// TODO(brian): pass a context to initConnections
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
154
		go initConnections(ctx, cfg, peerstore, route)
155
	}
156

157 158
	// TODO(brian): when offline instantiate the BlockService with a bitswap
	// session that simply doesn't return blocks
159
	bs, err := bserv.NewBlockService(d, exchangeSession)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
160 161 162 163
	if err != nil {
		return nil, err
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
164
	dag := &merkledag.DAGService{Blocks: bs}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
165
	ns := namesys.NewNameSystem(route)
Jeromy's avatar
Jeromy committed
166 167 168 169
	p, err := pin.LoadPinner(d, dag)
	if err != nil {
		p = pin.NewPinner(d, dag)
	}
170

171
	success = true
172
	return &IpfsNode{
Jeromy's avatar
Jeromy committed
173 174 175 176 177 178 179 180 181 182 183
		Config:      cfg,
		Peerstore:   peerstore,
		Datastore:   d,
		Blocks:      bs,
		DAG:         dag,
		Resolver:    &path.Resolver{DAG: dag},
		Exchange:    exchangeSession,
		Identity:    local,
		Routing:     route,
		Namesys:     ns,
		Diagnostics: diagnostics,
184
		Network:     network,
Jeromy's avatar
Jeromy committed
185
		Pinning:     p,
186
	}, nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
187
}
188

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

194 195 196 197
	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
198 199 200 201 202 203 204
	// 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
	}

205
	// address is optional
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
206 207
	if len(cfg.Addresses.Swarm) > 0 {
		maddr, err := ma.NewMultiaddr(cfg.Addresses.Swarm)
208 209 210 211
		if err != nil {
			return nil, err
		}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
212
		peer.AddAddress(maddr)
213 214
	}

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
222
		if err := peer.LoadAndVerifyKeyPair(skb); err != nil {
223 224
			return nil, err
		}
225 226
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
227
	return peer, nil
228
}
229

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

236 237
		maddr, err := ma.NewMultiaddr(p.Address)
		if err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
238
			log.Error("%s", err)
239 240 241
			continue
		}

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
250
		if _, err = route.Connect(ctx, npeer); err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
251
			log.Error("Bootstrapping error: %v", err)
252 253 254
		}
	}
}