core.go 6.36 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"
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
go lint  
Juan Batiz-Benet committed
30
// IpfsNode is IPFS Core module. It represents an IPFS instance.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
31 32
type IpfsNode struct {

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

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

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

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

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

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

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

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

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

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

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

	// the routing publisher
	Publisher *namesys.IpnsPublisher
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
68 69
}

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

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

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

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

93 94 95 96 97
	local, err := initIdentity(cfg, online)
	if err != nil {
		return nil, err
	}

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

109
	if online {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
110

111 112 113 114 115 116 117
		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
118 119 120
			return nil, err
		}

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

130 131 132
		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
133

134 135
		const alwaysSendToPeer = true // use YesManStrategy
		exchangeSession = bitswap.NetMessageSession(ctx, local, exchangeService, route, d, alwaysSendToPeer)
136

137
		// TODO(brian): pass a context to initConnections
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
138
		go initConnections(ctx, cfg, peerstore, route)
139
	}
140

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
148
	dag := &merkledag.DAGService{Blocks: bs}
Jeromy's avatar
Jeromy committed
149
	resolve := namesys.NewMasterResolver(route, dag)
150
	publisher := namesys.NewPublisher(dag, route)
151

152
	success = true
153
	return &IpfsNode{
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
154
		Config:    cfg,
155
		Peerstore: peerstore,
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
156
		Datastore: d,
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
157
		Blocks:    bs,
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
158 159
		DAG:       dag,
		Resolver:  &path.Resolver{DAG: dag},
160
		Exchange:  exchangeSession,
161 162
		Identity:  local,
		Routing:   route,
Jeromy's avatar
Jeromy committed
163
		Namesys:   resolve,
164
		Publisher: publisher,
165
	}, nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
166
}
167

168
func initIdentity(cfg *config.Config, 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?)")
	}

177 178
	// address is optional
	var addresses []*ma.Multiaddr
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
179 180
	if len(cfg.Addresses.Swarm) > 0 {
		maddr, err := ma.NewMultiaddr(cfg.Addresses.Swarm)
181 182 183 184
		if err != nil {
			return nil, err
		}

185
		addresses = []*ma.Multiaddr{maddr}
186 187
	}

188 189 190 191
	var (
		sk ci.PrivKey
		pk ci.PubKey
	)
192

193
	// when not online, don't need to parse private keys (yet)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
194
	if online {
195 196 197 198 199 200 201 202 203 204 205
		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()
206 207
	}

208
	return &peer.Peer{
209
		ID:        peer.ID(b58.Decode(cfg.Identity.PeerID)),
210
		Addresses: addresses,
211
		PrivKey:   sk,
212
		PubKey:    pk,
213 214
	}, nil
}
215

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

222 223 224 225 226 227
		maddr, err := ma.NewMultiaddr(p.Address)
		if err != nil {
			u.PErr("error: %v\n", err)
			continue
		}

228 229 230 231
		// setup peer
		npeer := &peer.Peer{ID: peer.DecodePrettyID(p.PeerID)}
		npeer.AddAddress(maddr)

232 233 234 235 236
		if err = pstore.Put(npeer); err != nil {
			u.PErr("Bootstrapping error: %v\n", err)
			continue
		}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
237
		if _, err = route.Connect(ctx, npeer); err != nil {
238 239 240 241
			u.PErr("Bootstrapping error: %v\n", err)
		}
	}
}
242

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