core.go 6.27 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"
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"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
30 31
)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	ctxc.ContextCloser
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
77 78
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
79
// NewIpfsNode constructs a new IpfsNode based on the given config.
80
func NewIpfsNode(cfg *config.Config, online bool) (n *IpfsNode, err error) {
81 82
	success := false // flip to true after all sub-system inits succeed
	defer func() {
83 84
		if !success && n != nil {
			n.Close()
85 86
		}
	}()
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 97 98 99 100
	// derive this from a higher context.
	ctx := context.TODO()
	n = &IpfsNode{
		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
101 102 103
		return nil, err
	}

104 105 106
	// setup peerstore + local peer identity
	n.Peerstore = peer.NewPeerstore()
	n.Identity, err = initIdentity(n.Config, n.Peerstore, online)
107 108 109 110
	if err != nil {
		return nil, err
	}

111
	// setup online services
112
	if online {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
113

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

118
		muxMap := &mux.ProtocolMap{
Jeromy's avatar
Jeromy committed
119 120 121
			mux.ProtocolID_Routing:    dhtService,
			mux.ProtocolID_Exchange:   exchangeService,
			mux.ProtocolID_Diagnostic: diagService,
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
122
			// add protocol services here.
123 124 125 126
		}

		// setup the network
		n.Network, err = inet.NewIpfsNetwork(ctx, n.Identity, n.Peerstore, muxMap)
127 128 129
		if err != nil {
			return nil, err
		}
130
		n.AddCloserChild(n.Network)
131

132 133 134
		// setup diagnostics service
		n.Diagnostics = diag.NewDiagnostics(n.Identity, n.Network, diagService)
		diagService.SetHandler(n.Diagnostics)
Jeromy's avatar
Jeromy committed
135

136 137
		// setup routing service
		dhtRouting := dht.NewDHT(ctx, n.Identity, n.Peerstore, n.Network, dhtService, n.Datastore)
138
		// TODO(brian): perform this inside NewDHT factory method
139 140
		dhtService.SetHandler(dhtRouting) // wire the handler to the service.
		n.Routing = dhtRouting
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
141

142
		// setup exchange service
143
		const alwaysSendToPeer = true // use YesManStrategy
144 145
		n.Exchange = bitswap.NetMessageSession(ctx, n.Identity, n.Network, exchangeService, n.Routing, n.Datastore, alwaysSendToPeer)
		// ok, this function call is ridiculous o/ consider making it simpler.
146

147
		go initConnections(ctx, n.Config, n.Peerstore, dhtRouting)
148
	}
149

150 151
	// TODO(brian): when offline instantiate the BlockService with a bitswap
	// session that simply doesn't return blocks
152
	n.Blocks, err = bserv.NewBlockService(n.Datastore, n.Exchange)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
153 154 155 156
	if err != nil {
		return nil, err
	}

157 158 159
	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
160
	if err != nil {
161
		n.Pinning = pin.NewPinner(n.Datastore, n.DAG)
Jeromy's avatar
Jeromy committed
162
	}
163

164
	success = true
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
165
	return n, nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
166
}
167

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

173 174 175 176
	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
177 178 179 180 181 182 183
	// 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
	}

184
	// address is optional
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
185 186
	if len(cfg.Addresses.Swarm) > 0 {
		maddr, err := ma.NewMultiaddr(cfg.Addresses.Swarm)
187 188 189 190
		if err != nil {
			return nil, err
		}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
191
		peer.AddAddress(maddr)
192 193
	}

194
	// when not online, don't need to parse private keys (yet)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
195
	if online {
196 197 198 199 200
		skb, err := base64.StdEncoding.DecodeString(cfg.Identity.PrivKey)
		if err != nil {
			return nil, err
		}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
201
		if err := peer.LoadAndVerifyKeyPair(skb); err != nil {
202 203
			return nil, err
		}
204 205
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
206
	return peer, nil
207
}
208

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
209
func initConnections(ctx context.Context, cfg *config.Config, pstore peer.Peerstore, route *dht.IpfsDHT) {
210
	for _, p := range cfg.Bootstrap {
211
		if p.PeerID == "" {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
212
			log.Errorf("error: peer does not include PeerID. %v", p)
213 214
		}

215 216
		maddr, err := ma.NewMultiaddr(p.Address)
		if err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
217
			log.Error(err)
218 219 220
			continue
		}

221
		// setup peer
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
222 223
		npeer, err := pstore.Get(peer.DecodePrettyID(p.PeerID))
		if err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
224
			log.Errorf("Bootstrapping error: %v", err)
225 226
			continue
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
227
		npeer.AddAddress(maddr)
228

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
229
		if _, err = route.Connect(ctx, npeer); err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
230
			log.Errorf("Bootstrapping error: %v", err)
231 232 233
		}
	}
}