core.go 6.12 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"
15
	ci "github.com/jbenet/go-ipfs/crypto"
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"
19 20
	inet "github.com/jbenet/go-ipfs/net"
	mux "github.com/jbenet/go-ipfs/net/mux"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
21
	netservice "github.com/jbenet/go-ipfs/net/service"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
22
	path "github.com/jbenet/go-ipfs/path"
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
23
	peer "github.com/jbenet/go-ipfs/peer"
24 25 26
	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
27 28
)

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

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
32 33
	// the node's configuration
	Config *config.Config
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
34

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
35 36
	// the local node's identity
	Identity *peer.Peer
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
37

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
38
	// storage for other Peer instances
39
	Peerstore peer.Peerstore
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
40

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
41 42
	// the local datastore
	Datastore ds.Datastore
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
43

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
44
	// the network message stream
45
	Network inet.Network
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
46

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
47
	// the routing system. recommend ipfs-dht
48
	Routing routing.IpfsRouting
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
49

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
50
	// the block exchange + strategy (bitswap)
51
	Exchange exchange.Interface
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
52

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
53
	// the block service, get/add blocks.
54
	Blocks *bserv.BlockService
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
55

56 57 58
	// the merkle dag service, get/add objects.
	DAG *merkledag.DAGService

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
59
	// the path resolution system
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
60
	Resolver *path.Resolver
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
61

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
62 63
	// the name system, resolves paths to hashes
	// Namesys *namesys.Namesys
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
64 65
}

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

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
78
	if cfg == nil {
Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
79
		return nil, fmt.Errorf("configuration required")
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
80 81 82 83 84 85 86
	}

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
87 88
	peerstore := peer.NewPeerstore()

89 90 91 92 93
	local, err := initIdentity(cfg, online)
	if err != nil {
		return nil, err
	}

94 95 96 97
	// 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
98
	var (
99
		net inet.Network
100
		// TODO: refactor so we can use IpfsRouting interface instead of being DHT-specific
101
		route           *dht.IpfsDHT
102
		exchangeSession exchange.Interface
103 104
	)

105
	if online {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
106

107 108 109 110 111 112 113
		dhtService := netservice.NewService(nil)      // nil handler for now, need to patch it
		exchangeService := netservice.NewService(nil) // nil handler for now, need to patch it

		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
114 115 116
			return nil, err
		}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
117
		net, err = inet.NewIpfsNetwork(context.TODO(), local, peerstore, &mux.ProtocolMap{
118 119
			mux.ProtocolID_Routing:  dhtService,
			mux.ProtocolID_Exchange: exchangeService,
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
120
			// add protocol services here.
121
		})
122 123 124
		if err != nil {
			return nil, err
		}
125

126 127 128
		route = dht.NewDHT(local, peerstore, net, dhtService, d)
		// TODO(brian): perform this inside NewDHT factory method
		dhtService.Handler = route // wire the handler to the service.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
129

130 131
		const alwaysSendToPeer = true // use YesManStrategy
		exchangeSession = bitswap.NetMessageSession(ctx, local, exchangeService, route, d, alwaysSendToPeer)
132

133
		// TODO(brian): pass a context to initConnections
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
134
		go initConnections(ctx, cfg, peerstore, route)
135
	}
136

137 138
	// TODO(brian): when offline instantiate the BlockService with a bitswap
	// session that simply doesn't return blocks
139
	bs, err := bserv.NewBlockService(d, exchangeSession)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
140 141 142 143
	if err != nil {
		return nil, err
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
144
	dag := &merkledag.DAGService{Blocks: bs}
145

146
	success = true
147
	return &IpfsNode{
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
148
		Config:    cfg,
149
		Peerstore: peerstore,
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
150
		Datastore: d,
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
151
		Blocks:    bs,
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
152 153
		DAG:       dag,
		Resolver:  &path.Resolver{DAG: dag},
154
		Exchange:  exchangeSession,
155 156 157
		Identity:  local,
		Routing:   route,
	}, nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
158
}
159

160
func initIdentity(cfg *config.Config, online bool) (*peer.Peer, error) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
161
	if cfg.Identity.PeerID == "" {
162 163 164
		return nil, errors.New("Identity was not set in config (was ipfs init run?)")
	}

165 166 167 168
	if len(cfg.Identity.PeerID) == 0 {
		return nil, errors.New("No peer ID in config! (was ipfs init run?)")
	}

169 170
	// address is optional
	var addresses []*ma.Multiaddr
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
171 172
	if len(cfg.Addresses.Swarm) > 0 {
		maddr, err := ma.NewMultiaddr(cfg.Addresses.Swarm)
173 174 175 176
		if err != nil {
			return nil, err
		}

177
		addresses = []*ma.Multiaddr{maddr}
178 179
	}

180 181 182 183
	var (
		sk ci.PrivKey
		pk ci.PubKey
	)
184

185
	// when not online, don't need to parse private keys (yet)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
186
	if online {
187 188 189 190 191 192 193 194 195 196 197
		skb, err := base64.StdEncoding.DecodeString(cfg.Identity.PrivKey)
		if err != nil {
			return nil, err
		}

		sk, err = ci.UnmarshalPrivateKey(skb)
		if err != nil {
			return nil, err
		}

		pk = sk.GetPublic()
198 199
	}

200
	return &peer.Peer{
201
		ID:        peer.ID(b58.Decode(cfg.Identity.PeerID)),
202
		Addresses: addresses,
203
		PrivKey:   sk,
204
		PubKey:    pk,
205 206
	}, nil
}
207

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

214 215 216 217 218 219
		maddr, err := ma.NewMultiaddr(p.Address)
		if err != nil {
			u.PErr("error: %v\n", err)
			continue
		}

220 221 222 223
		// setup peer
		npeer := &peer.Peer{ID: peer.DecodePrettyID(p.PeerID)}
		npeer.AddAddress(maddr)

224 225 226 227 228
		if err = pstore.Put(npeer); err != nil {
			u.PErr("Bootstrapping error: %v\n", err)
			continue
		}

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
235
// PinDagNode ensures a given node is stored persistently locally.
236
func (n *IpfsNode) PinDagNode(nd *merkledag.Node) error {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
237
	u.DOut("Pinning node. Currently No-Op\n")
238 239
	return nil
}