core.go 4.46 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
	path "github.com/jbenet/go-ipfs/path"
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
21
	peer "github.com/jbenet/go-ipfs/peer"
22 23 24
	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
25 26
)

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

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

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
36 37
	// the map of other nodes (Peer instances)
	PeerMap *peer.Map
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
38

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

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

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

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

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

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

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

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

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

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

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

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

86
	if online {
87 88 89 90 91
		// add protocol services here.
		net, err := inet.NewIpfsNetwork(context.TODO(), local, &mux.ProtocolMap{
		// "1": dhtService,
		// "2": bitswapService,
		})
92 93 94
		if err != nil {
			return nil, err
		}
95 96

		route = dht.NewDHT(local, net, d)
97
		// TODO(brian): pass a context to DHT for its async operations
98 99
		route.Start()

100
		// TODO(brian): pass a context to bs for its async operations
101 102
		bitswapSession := bitswap.NewSession(context.TODO(), local, d, route)
		bitswapSession.SetStrategy(bitswap.YesManStrategy)
103

104
		// TODO(brian): pass a context to initConnections
105
		go initConnections(cfg, route)
106
	}
107

108 109
	// TODO(brian): when offline instantiate the BlockService with a bitswap
	// session that simply doesn't return blocks
110
	bs, err := bserv.NewBlockService(d, bitswapSession)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
111 112 113 114
	if err != nil {
		return nil, err
	}

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

117
	return &IpfsNode{
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
118
		Config:    cfg,
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
119
		PeerMap:   &peer.Map{},
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
120
		Datastore: d,
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
121
		Blocks:    bs,
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
122 123
		DAG:       dag,
		Resolver:  &path.Resolver{DAG: dag},
124
		BitSwap:   bitswapSession,
125 126 127
		Identity:  local,
		Routing:   route,
	}, nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
128
}
129

130
func initIdentity(cfg *config.Config) (*peer.Peer, error) {
131 132 133 134
	if cfg.Identity == nil {
		return nil, errors.New("Identity was not set in config (was ipfs init run?)")
	}

135 136 137 138
	if len(cfg.Identity.PeerID) == 0 {
		return nil, errors.New("No peer ID in config! (was ipfs init run?)")
	}

139 140 141 142 143 144 145 146
	// 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
		}

147
		addresses = []*ma.Multiaddr{maddr}
148 149
	}

150
	skb, err := base64.StdEncoding.DecodeString(cfg.Identity.PrivKey)
151 152 153 154
	if err != nil {
		return nil, err
	}

155 156 157
	sk, err := ci.UnmarshalPrivateKey(skb)
	if err != nil {
		return nil, err
158 159
	}

160
	return &peer.Peer{
161
		ID:        peer.ID(b58.Decode(cfg.Identity.PeerID)),
162
		Addresses: addresses,
163 164
		PrivKey:   sk,
		PubKey:    sk.GetPublic(),
165 166
	}, nil
}
167

168
func initConnections(cfg *config.Config, route *dht.IpfsDHT) {
169 170 171 172 173 174 175 176 177 178 179 180 181
	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)
		}
	}
}
182 183 184 185 186

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