-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathcore.go
206 lines (166 loc) · 5.32 KB
/
core.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package core
import (
"encoding/base64"
"errors"
"fmt"
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
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"
bserv "github.com/jbenet/go-ipfs/blockservice"
config "github.com/jbenet/go-ipfs/config"
ci "github.com/jbenet/go-ipfs/crypto"
exchange "github.com/jbenet/go-ipfs/exchange"
bitswap "github.com/jbenet/go-ipfs/exchange/bitswap"
merkledag "github.com/jbenet/go-ipfs/merkledag"
inet "github.com/jbenet/go-ipfs/net"
mux "github.com/jbenet/go-ipfs/net/mux"
netservice "github.com/jbenet/go-ipfs/net/service"
path "github.com/jbenet/go-ipfs/path"
peer "github.com/jbenet/go-ipfs/peer"
routing "github.com/jbenet/go-ipfs/routing"
dht "github.com/jbenet/go-ipfs/routing/dht"
u "github.com/jbenet/go-ipfs/util"
)
// IpfsNode is IPFS Core module. It represents an IPFS instance.
type IpfsNode struct {
// the node's configuration
Config *config.Config
// the local node's identity
Identity *peer.Peer
// storage for other Peer instances
Peerstore *peer.Peerstore
// the local datastore
Datastore ds.Datastore
// the network message stream
Network inet.Network
// the routing system. recommend ipfs-dht
Routing routing.IpfsRouting
// the block exchange + strategy (bitswap)
Exchange exchange.Interface
// the block service, get/add blocks.
Blocks *bserv.BlockService
// the merkle dag service, get/add objects.
DAG *merkledag.DAGService
// the path resolution system
Resolver *path.Resolver
// the name system, resolves paths to hashes
// Namesys *namesys.Namesys
}
// NewIpfsNode constructs a new IpfsNode based on the given config.
func NewIpfsNode(cfg *config.Config, online bool) (*IpfsNode, error) {
if cfg == nil {
return nil, fmt.Errorf("configuration required")
}
d, err := makeDatastore(cfg.Datastore)
if err != nil {
return nil, err
}
local, err := initIdentity(cfg)
if err != nil {
return nil, err
}
peerstore := peer.NewPeerstore()
// 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
var (
net inet.Network
// TODO: refactor so we can use IpfsRouting interface instead of being DHT-specific
route *dht.IpfsDHT
exchangeSession exchange.Interface
)
if online {
// add protocol services here.
ctx := context.TODO() // derive this from a higher context.
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 {
return nil, err
}
net, err = inet.NewIpfsNetwork(context.TODO(), local, &mux.ProtocolMap{
mux.ProtocolID_Routing: dhtService,
mux.ProtocolID_Exchange: exchangeService,
})
if err != nil {
return nil, err
}
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.
// TODO(brian): pass a context to bs for its async operations
exchangeSession = bitswap.NewSession(ctx, exchangeService, local, d, route)
// TODO(brian): pass a context to initConnections
go initConnections(cfg, route)
}
// TODO(brian): when offline instantiate the BlockService with a bitswap
// session that simply doesn't return blocks
bs, err := bserv.NewBlockService(d, exchangeSession)
if err != nil {
return nil, err
}
dag := &merkledag.DAGService{Blocks: bs}
return &IpfsNode{
Config: cfg,
Peerstore: &peerstore,
Datastore: d,
Blocks: bs,
DAG: dag,
Resolver: &path.Resolver{DAG: dag},
Exchange: exchangeSession,
Identity: local,
Routing: route,
}, nil
}
func initIdentity(cfg *config.Config) (*peer.Peer, error) {
if cfg.Identity == nil {
return nil, errors.New("Identity was not set in config (was ipfs init run?)")
}
if len(cfg.Identity.PeerID) == 0 {
return nil, errors.New("No peer ID in config! (was ipfs init run?)")
}
// 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
}
addresses = []*ma.Multiaddr{maddr}
}
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
}
return &peer.Peer{
ID: peer.ID(b58.Decode(cfg.Identity.PeerID)),
Addresses: addresses,
PrivKey: sk,
PubKey: sk.GetPublic(),
}, nil
}
func initConnections(cfg *config.Config, route *dht.IpfsDHT) {
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)
}
}
}
func (n *IpfsNode) PinDagNode(nd *merkledag.Node) error {
u.POut("Pinning node. Currently No-Op\n")
return nil
}