core.go 4.82 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

	bitswap "github.com/jbenet/go-ipfs/bitswap"
14
	bserv "github.com/jbenet/go-ipfs/blockservice"
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
15
	config "github.com/jbenet/go-ipfs/config"
16
	ci "github.com/jbenet/go-ipfs/crypto"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
17
	merkledag "github.com/jbenet/go-ipfs/merkledag"
18 19
	inet "github.com/jbenet/go-ipfs/net"
	mux "github.com/jbenet/go-ipfs/net/mux"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
20
	netservice "github.com/jbenet/go-ipfs/net/service"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
21
	path "github.com/jbenet/go-ipfs/path"
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
22
	peer "github.com/jbenet/go-ipfs/peer"
23 24 25
	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
26 27
)

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

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

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

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

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

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

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

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

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

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

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

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

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
65
// NewIpfsNode constructs a new IpfsNode based on the given config.
66
func NewIpfsNode(cfg *config.Config, online bool) (*IpfsNode, error) {
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
67
	if cfg == nil {
Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
68
		return nil, fmt.Errorf("configuration required")
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
69 70 71 72 73 74 75
	}

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

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

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

83
	var (
84
		net *inet.Network
85
		// TODO: refactor so we can use IpfsRouting interface instead of being DHT-specific
86
		route *dht.IpfsDHT
87 88
	)

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

		dhts := netservice.Service(nil) // nil handler for now, need to patch it
		if err := dhts.Start(ctx); err != nil {
			return nil, err
		}

98
		net, err := inet.NewIpfsNetwork(context.TODO(), local, &mux.ProtocolMap{
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
99 100
			netservice.ProtocolID_Routing: dhtService,
			// netservice.ProtocolID_Bitswap: bitswapService,
101
		})
102 103 104
		if err != nil {
			return nil, err
		}
105

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
106
		route = dht.NewDHT(local, peerstore, net, dhts, d)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
107 108
		dhts.Handler = route // wire the handler to the service.

109
		// TODO(brian): pass a context to DHT for its async operations
110 111
		route.Start()

112
		// TODO(brian): pass a context to bs for its async operations
113
		bitswapSession := bitswap.NewSession(context.TODO(), local, d, route)
114

115
		// TODO(brian): pass a context to initConnections
116
		go initConnections(cfg, route)
117
	}
118

119 120
	// TODO(brian): when offline instantiate the BlockService with a bitswap
	// session that simply doesn't return blocks
121
	bs, err := bserv.NewBlockService(d, bitswapSession)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
122 123 124 125
	if err != nil {
		return nil, err
	}

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

128
	return &IpfsNode{
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
129
		Config:    cfg,
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
130
		Peerstore: peerstore,
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
131
		Datastore: d,
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
132
		Blocks:    bs,
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
133 134
		DAG:       dag,
		Resolver:  &path.Resolver{DAG: dag},
135
		BitSwap:   bitswapSession,
136 137 138
		Identity:  local,
		Routing:   route,
	}, nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
139
}
140

141
func initIdentity(cfg *config.Config) (*peer.Peer, error) {
142 143 144 145
	if cfg.Identity == nil {
		return nil, errors.New("Identity was not set in config (was ipfs init run?)")
	}

146 147 148 149
	if len(cfg.Identity.PeerID) == 0 {
		return nil, errors.New("No peer ID in config! (was ipfs init run?)")
	}

150 151 152 153 154 155 156 157
	// 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
		}

158
		addresses = []*ma.Multiaddr{maddr}
159 160
	}

161
	skb, err := base64.StdEncoding.DecodeString(cfg.Identity.PrivKey)
162 163 164 165
	if err != nil {
		return nil, err
	}

166 167 168
	sk, err := ci.UnmarshalPrivateKey(skb)
	if err != nil {
		return nil, err
169 170
	}

171
	return &peer.Peer{
172
		ID:        peer.ID(b58.Decode(cfg.Identity.PeerID)),
173
		Addresses: addresses,
174 175
		PrivKey:   sk,
		PubKey:    sk.GetPublic(),
176 177
	}, nil
}
178

179
func initConnections(cfg *config.Config, route *dht.IpfsDHT) {
180 181 182 183 184 185 186 187 188 189 190 191 192
	for _, p := range cfg.Peers {
		maddr, err := ma.NewMultiaddr(p.Address)
		if err != nil {
			u.PErr("error: %v\n", err)
			continue
		}

		_, err = route.Connect(maddr)
		if err != nil {
			u.PErr("Bootstrapping error: %v\n", err)
		}
	}
}
193 194 195 196 197

func (n *IpfsNode) PinDagNode(nd *merkledag.Node) error {
	u.POut("Pinning node. Currently No-Op\n")
	return nil
}