feat: expose IpfsNode.Bootstrap() method
Brian Tiger Chow committed
Jan 10, 2015 at 19:25 UTC
2c3fb43350f825812bbf5320ca20824806e58733
2 files changed
+32
-14
core/bootstrap.go
+2
-13
@@ -29,7 +29,7 @@ func superviseConnections(parent context.Context,
29
h host.Host,
30
route *dht.IpfsDHT, // TODO depend on abstract interface for testing purposes
31
store peer.Peerstore,
32
- peers []config.BootstrapPeer) error {
32
+ peers []peer.PeerInfo) error {
33
34
for {
35
ctx, _ := context.WithTimeout(parent, connectiontimeout)
@@ -51,7 +51,7 @@ func bootstrap(ctx context.Context,
51
h host.Host,
52
r *dht.IpfsDHT,
53
ps peer.Peerstore,
54
- boots []config.BootstrapPeer) error {
54
+ bootstrapPeers []peer.PeerInfo) error {
55
56
connectedPeers := h.Network().Peers()
57
if len(connectedPeers) >= recoveryThreshold {
@@ -66,17 +66,6 @@ func bootstrap(ctx context.Context,
66
log.Event(ctx, "bootstrapStart", h.ID())
67
log.Debugf("%s bootstrapping to %d more nodes", h.ID(), numCxnsToCreate)
68
69
- var bootstrapPeers []peer.PeerInfo
70
- for _, bootstrap := range boots {
71
- p, err := toPeer(bootstrap)
72
- if err != nil {
73
- log.Event(ctx, "bootstrapError", h.ID(), lgbl.Error(err))
74
- log.Errorf("%s bootstrap error: %s", h.ID(), err)
75
- return err
76
- }
77
- bootstrapPeers = append(bootstrapPeers, p)
78
- }
79
-
69
var notConnected []peer.PeerInfo
70
for _, p := range bootstrapPeers {
71
if h.Network().Connectedness(p.ID) != inet.Connected {
core/core.go
+30
-1
@@ -32,6 +32,7 @@ import (
32
ds2 "github.com/jbenet/go-ipfs/util/datastore2"
33
debugerror "github.com/jbenet/go-ipfs/util/debugerror"
34
eventlog "github.com/jbenet/go-ipfs/util/eventlog"
35
+ lgbl "github.com/jbenet/go-ipfs/util/eventlog/loggables"
36
)
37
38
const IpnsValidatorTag = "ipns"
@@ -66,6 +67,11 @@ type IpfsNode struct {
67
Diagnostics *diag.Diagnostics // the diagnostics service
68
69
ctxgroup.ContextGroup
70
+
71
+ // dht allows node to Bootstrap when dht is present
72
+ // TODO privatize before merging. This is here temporarily during the
73
+ // migration of the TestNet constructor
74
+ DHT *dht.IpfsDHT
75
}
76
77
// Mounts defines what the node's mount state is. This should
@@ -185,6 +191,7 @@ func (n *IpfsNode) StartOnlineServices() error {
191
dhtRouting := dht.NewDHT(ctx, n.PeerHost, n.Datastore)
192
dhtRouting.Validators[IpnsValidatorTag] = namesys.ValidateIpnsRecord
193
n.Routing = dhtRouting
194
+ n.DHT = dhtRouting
195
n.AddChildGroup(dhtRouting)
196
197
// setup exchange service
@@ -202,7 +209,17 @@ func (n *IpfsNode) StartOnlineServices() error {
209
// an Exchange, Network, or Routing component and have the constructor
210
// manage the wiring. In that scenario, this dangling function is a bit
211
// awkward.
205
- go superviseConnections(ctx, n.PeerHost, dhtRouting, n.Peerstore, n.Config.Bootstrap)
212
+ var bootstrapPeers []peer.PeerInfo
213
+ for _, bootstrap := range n.Config.Bootstrap {
214
+ p, err := toPeer(bootstrap)
215
+ if err != nil {
216
+ log.Event(ctx, "bootstrapError", n.Identity, lgbl.Error(err))
217
+ log.Errorf("%s bootstrap error: %s", n.Identity, err)
218
+ return err
219
+ }
220
+ bootstrapPeers = append(bootstrapPeers, p)
221
+ }
222
+ go superviseConnections(ctx, n.PeerHost, dhtRouting, n.Peerstore, bootstrapPeers)
223
return nil
224
}
225
@@ -245,6 +262,18 @@ func (n *IpfsNode) OnlineMode() bool {
262
return n.onlineMode
263
}
264
265
+func (n *IpfsNode) Bootstrap(ctx context.Context, peers []peer.PeerInfo) error {
266
+ if n.DHT != nil {
267
+ for _, p := range peers {
268
+ // TODO bootstrap(ctx, n.PeerHost, n.DHT, n.Peerstore, peers)
269
+ if err := n.DHT.Connect(ctx, p.ID); err != nil {
270
+ return err
271
+ }
272
+ }
273
+ }
274
+ return nil
275
+}
276
+
277
func (n *IpfsNode) loadID() error {
278
if n.Identity != "" {
279
return debugerror.New("identity already loaded")