core.go 6.87 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 10 11
	ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/datastore.go"
	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"
25 26 27
	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
28 29
)

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

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

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

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

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

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

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

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

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

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

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

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

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

	// the diagnostics service
	Diagnostics *diag.Diagnostics
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
70 71
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
72
// NewIpfsNode constructs a new IpfsNode based on the given config.
73
func NewIpfsNode(cfg *config.Config, online bool) (*IpfsNode, error) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
74 75 76
	// derive this from a higher context.
	// cancel if we need to fail early.
	ctx, cancel := context.WithCancel(context.TODO())
77 78 79 80 81 82
	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
83

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
84
	if cfg == nil {
Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
85
		return nil, fmt.Errorf("configuration required")
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
86 87 88 89 90 91 92
	}

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
93
	peerstore := peer.NewPeerstore()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
94
	local, err := initIdentity(cfg, peerstore, online)
95 96 97 98
	if err != nil {
		return nil, err
	}

99 100 101 102
	// 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
103
	var (
104
		net inet.Network
105
		// TODO: refactor so we can use IpfsRouting interface instead of being DHT-specific
106
		route           *dht.IpfsDHT
107
		exchangeSession exchange.Interface
Jeromy's avatar
Jeromy committed
108
		diagnostics     *diag.Diagnostics
109
		network         inet.Network
110 111
	)

112
	if online {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
113

114 115
		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
116
		diagService := netservice.NewService(nil)
117 118 119 120 121

		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
122 123
			return nil, err
		}
124 125 126
		if err := diagService.Start(ctx); err != nil {
			return nil, err
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
127

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

Jeromy's avatar
Jeromy committed
139
		diagnostics = diag.NewDiagnostics(local, net, diagService)
140
		diagService.SetHandler(diagnostics)
Jeromy's avatar
Jeromy committed
141

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

146
		const alwaysSendToPeer = true // use YesManStrategy
147
		exchangeSession = bitswap.NetMessageSession(ctx, local, net, exchangeService, route, d, alwaysSendToPeer)
148

149
		// TODO(brian): pass a context to initConnections
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
150
		go initConnections(ctx, cfg, peerstore, route)
151
	}
152

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
160
	dag := &merkledag.DAGService{Blocks: bs}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
161
	ns := namesys.NewNameSystem(route)
162

163
	success = true
164
	return &IpfsNode{
Jeromy's avatar
Jeromy committed
165 166 167 168 169 170 171 172 173 174 175
		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,
176
		Network:     network,
177
	}, nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
178
}
179

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

185 186 187 188
	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
189 190 191 192 193 194 195
	// 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
	}

196
	// address is optional
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
197 198
	if len(cfg.Addresses.Swarm) > 0 {
		maddr, err := ma.NewMultiaddr(cfg.Addresses.Swarm)
199 200 201 202
		if err != nil {
			return nil, err
		}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
203
		peer.AddAddress(maddr)
204 205
	}

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
213
		if err := peer.LoadAndVerifyKeyPair(skb); err != nil {
214 215
			return nil, err
		}
216 217
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
218
	return peer, nil
219
}
220

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
221
func initConnections(ctx context.Context, cfg *config.Config, pstore peer.Peerstore, route *dht.IpfsDHT) {
222
	for _, p := range cfg.Bootstrap {
223
		if p.PeerID == "" {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
224
			log.Error("error: peer does not include PeerID. %v", p)
225 226
		}

227 228
		maddr, err := ma.NewMultiaddr(p.Address)
		if err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
229
			log.Error("%s", err)
230 231 232
			continue
		}

233
		// setup peer
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
234 235
		npeer, err := pstore.Get(peer.DecodePrettyID(p.PeerID))
		if err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
236
			log.Error("Bootstrapping error: %v", err)
237 238
			continue
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
239
		npeer.AddAddress(maddr)
240

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
241
		if _, err = route.Connect(ctx, npeer); err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
242
			log.Error("Bootstrapping error: %v", err)
243 244 245
		}
	}
}
246

247
// PinDagNode ensures a given node is stored persistently locally
248
func (n *IpfsNode) PinDagNode(nd *merkledag.Node) error {
249 250 251 252 253
	return n.PinDagNodeRecursively(nd, 1)
}

// PinDagNodeRecursively ensures a given node is stored persistently locally
func (n *IpfsNode) PinDagNodeRecursively(nd *merkledag.Node, depth int) error {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
254
	log.Debug("Pinning node recursively. Currently No-Op")
255 256
	return nil
}