core.go 5.51 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 39
	// storage for other Peer instances
	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
go fmt  
Juan Batiz-Benet committed
68
	if cfg == nil {
Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
69
		return nil, fmt.Errorf("configuration required")
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
70 71 72 73 74 75 76
	}

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

77 78 79 80 81
	local, err := initIdentity(cfg)
	if err != nil {
		return nil, err
	}

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

84 85 86 87
	// 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
88
	var (
89
		net inet.Network
90
		// TODO: refactor so we can use IpfsRouting interface instead of being DHT-specific
91
		route           *dht.IpfsDHT
92
		exchangeSession exchange.Interface
93 94
	)

95
	if online {
96
		// add protocol services here.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
97 98
		ctx := context.TODO() // derive this from a higher context.

99 100 101 102 103 104 105
		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
106 107 108
			return nil, err
		}

109 110 111
		net, err = inet.NewIpfsNetwork(context.TODO(), local, &mux.ProtocolMap{
			mux.ProtocolID_Routing:  dhtService,
			mux.ProtocolID_Exchange: exchangeService,
112
		})
113 114 115
		if err != nil {
			return nil, err
		}
116

117 118 119
		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
120

121
		exchangeSession = bitswap.NetMessageSession(ctx, exchangeService, local, d, route)
122

123
		// TODO(brian): pass a context to initConnections
124
		go initConnections(cfg, route)
125
	}
126

127 128
	// TODO(brian): when offline instantiate the BlockService with a bitswap
	// session that simply doesn't return blocks
129
	bs, err := bserv.NewBlockService(d, exchangeSession)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
130 131 132 133
	if err != nil {
		return nil, err
	}

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

136
	return &IpfsNode{
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
137
		Config:    cfg,
138
		Peerstore: &peerstore,
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
139
		Datastore: d,
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
140
		Blocks:    bs,
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
141 142
		DAG:       dag,
		Resolver:  &path.Resolver{DAG: dag},
143
		Exchange:  exchangeSession,
144 145 146
		Identity:  local,
		Routing:   route,
	}, nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
147
}
148

149
func initIdentity(cfg *config.Config) (*peer.Peer, error) {
150 151 152 153
	if cfg.Identity == nil {
		return nil, errors.New("Identity was not set in config (was ipfs init run?)")
	}

154 155 156 157
	if len(cfg.Identity.PeerID) == 0 {
		return nil, errors.New("No peer ID in config! (was ipfs init run?)")
	}

158 159 160 161 162 163 164 165
	// address is optional
	var addresses []*ma.Multiaddr
	if len(cfg.Identity.Address) > 0 {
		maddr, err := ma.NewMultiaddr(cfg.Identity.Address)
		if err != nil {
			return nil, err
		}

166
		addresses = []*ma.Multiaddr{maddr}
167 168
	}

169
	skb, err := base64.StdEncoding.DecodeString(cfg.Identity.PrivKey)
170 171 172 173
	if err != nil {
		return nil, err
	}

174 175 176
	sk, err := ci.UnmarshalPrivateKey(skb)
	if err != nil {
		return nil, err
177 178
	}

179
	return &peer.Peer{
180
		ID:        peer.ID(b58.Decode(cfg.Identity.PeerID)),
181
		Addresses: addresses,
182 183
		PrivKey:   sk,
		PubKey:    sk.GetPublic(),
184 185
	}, nil
}
186

187
func initConnections(cfg *config.Config, route *dht.IpfsDHT) {
188
	for _, p := range cfg.Peers {
189 190 191 192
		if p.PeerID == "" {
			u.PErr("error: peer does not include PeerID. %v\n", p)
		}

193 194 195 196 197 198
		maddr, err := ma.NewMultiaddr(p.Address)
		if err != nil {
			u.PErr("error: %v\n", err)
			continue
		}

199 200 201 202 203
		// setup peer
		npeer := &peer.Peer{ID: peer.DecodePrettyID(p.PeerID)}
		npeer.AddAddress(maddr)

		_, err = route.Connect(npeer)
204 205 206 207 208
		if err != nil {
			u.PErr("Bootstrapping error: %v\n", err)
		}
	}
}
209

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