mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-29 01:12:24 +08:00
feat: expose IpfsNode.Bootstrap() method
This commit is contained in:
@ -29,7 +29,7 @@ func superviseConnections(parent context.Context,
|
|||||||
h host.Host,
|
h host.Host,
|
||||||
route *dht.IpfsDHT, // TODO depend on abstract interface for testing purposes
|
route *dht.IpfsDHT, // TODO depend on abstract interface for testing purposes
|
||||||
store peer.Peerstore,
|
store peer.Peerstore,
|
||||||
peers []config.BootstrapPeer) error {
|
peers []peer.PeerInfo) error {
|
||||||
|
|
||||||
for {
|
for {
|
||||||
ctx, _ := context.WithTimeout(parent, connectiontimeout)
|
ctx, _ := context.WithTimeout(parent, connectiontimeout)
|
||||||
@ -51,7 +51,7 @@ func bootstrap(ctx context.Context,
|
|||||||
h host.Host,
|
h host.Host,
|
||||||
r *dht.IpfsDHT,
|
r *dht.IpfsDHT,
|
||||||
ps peer.Peerstore,
|
ps peer.Peerstore,
|
||||||
boots []config.BootstrapPeer) error {
|
bootstrapPeers []peer.PeerInfo) error {
|
||||||
|
|
||||||
connectedPeers := h.Network().Peers()
|
connectedPeers := h.Network().Peers()
|
||||||
if len(connectedPeers) >= recoveryThreshold {
|
if len(connectedPeers) >= recoveryThreshold {
|
||||||
@ -66,17 +66,6 @@ func bootstrap(ctx context.Context,
|
|||||||
log.Event(ctx, "bootstrapStart", h.ID())
|
log.Event(ctx, "bootstrapStart", h.ID())
|
||||||
log.Debugf("%s bootstrapping to %d more nodes", h.ID(), numCxnsToCreate)
|
log.Debugf("%s bootstrapping to %d more nodes", h.ID(), numCxnsToCreate)
|
||||||
|
|
||||||
var bootstrapPeers []peer.PeerInfo
|
|
||||||
for _, bootstrap := range boots {
|
|
||||||
p, err := toPeer(bootstrap)
|
|
||||||
if err != nil {
|
|
||||||
log.Event(ctx, "bootstrapError", h.ID(), lgbl.Error(err))
|
|
||||||
log.Errorf("%s bootstrap error: %s", h.ID(), err)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
bootstrapPeers = append(bootstrapPeers, p)
|
|
||||||
}
|
|
||||||
|
|
||||||
var notConnected []peer.PeerInfo
|
var notConnected []peer.PeerInfo
|
||||||
for _, p := range bootstrapPeers {
|
for _, p := range bootstrapPeers {
|
||||||
if h.Network().Connectedness(p.ID) != inet.Connected {
|
if h.Network().Connectedness(p.ID) != inet.Connected {
|
||||||
|
31
core/core.go
31
core/core.go
@ -32,6 +32,7 @@ import (
|
|||||||
ds2 "github.com/jbenet/go-ipfs/util/datastore2"
|
ds2 "github.com/jbenet/go-ipfs/util/datastore2"
|
||||||
debugerror "github.com/jbenet/go-ipfs/util/debugerror"
|
debugerror "github.com/jbenet/go-ipfs/util/debugerror"
|
||||||
eventlog "github.com/jbenet/go-ipfs/util/eventlog"
|
eventlog "github.com/jbenet/go-ipfs/util/eventlog"
|
||||||
|
lgbl "github.com/jbenet/go-ipfs/util/eventlog/loggables"
|
||||||
)
|
)
|
||||||
|
|
||||||
const IpnsValidatorTag = "ipns"
|
const IpnsValidatorTag = "ipns"
|
||||||
@ -66,6 +67,11 @@ type IpfsNode struct {
|
|||||||
Diagnostics *diag.Diagnostics // the diagnostics service
|
Diagnostics *diag.Diagnostics // the diagnostics service
|
||||||
|
|
||||||
ctxgroup.ContextGroup
|
ctxgroup.ContextGroup
|
||||||
|
|
||||||
|
// dht allows node to Bootstrap when dht is present
|
||||||
|
// TODO privatize before merging. This is here temporarily during the
|
||||||
|
// migration of the TestNet constructor
|
||||||
|
DHT *dht.IpfsDHT
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mounts defines what the node's mount state is. This should
|
// Mounts defines what the node's mount state is. This should
|
||||||
@ -185,6 +191,7 @@ func (n *IpfsNode) StartOnlineServices() error {
|
|||||||
dhtRouting := dht.NewDHT(ctx, n.PeerHost, n.Datastore)
|
dhtRouting := dht.NewDHT(ctx, n.PeerHost, n.Datastore)
|
||||||
dhtRouting.Validators[IpnsValidatorTag] = namesys.ValidateIpnsRecord
|
dhtRouting.Validators[IpnsValidatorTag] = namesys.ValidateIpnsRecord
|
||||||
n.Routing = dhtRouting
|
n.Routing = dhtRouting
|
||||||
|
n.DHT = dhtRouting
|
||||||
n.AddChildGroup(dhtRouting)
|
n.AddChildGroup(dhtRouting)
|
||||||
|
|
||||||
// setup exchange service
|
// setup exchange service
|
||||||
@ -202,7 +209,17 @@ func (n *IpfsNode) StartOnlineServices() error {
|
|||||||
// an Exchange, Network, or Routing component and have the constructor
|
// an Exchange, Network, or Routing component and have the constructor
|
||||||
// manage the wiring. In that scenario, this dangling function is a bit
|
// manage the wiring. In that scenario, this dangling function is a bit
|
||||||
// awkward.
|
// awkward.
|
||||||
go superviseConnections(ctx, n.PeerHost, dhtRouting, n.Peerstore, n.Config.Bootstrap)
|
var bootstrapPeers []peer.PeerInfo
|
||||||
|
for _, bootstrap := range n.Config.Bootstrap {
|
||||||
|
p, err := toPeer(bootstrap)
|
||||||
|
if err != nil {
|
||||||
|
log.Event(ctx, "bootstrapError", n.Identity, lgbl.Error(err))
|
||||||
|
log.Errorf("%s bootstrap error: %s", n.Identity, err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
bootstrapPeers = append(bootstrapPeers, p)
|
||||||
|
}
|
||||||
|
go superviseConnections(ctx, n.PeerHost, dhtRouting, n.Peerstore, bootstrapPeers)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -245,6 +262,18 @@ func (n *IpfsNode) OnlineMode() bool {
|
|||||||
return n.onlineMode
|
return n.onlineMode
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (n *IpfsNode) Bootstrap(ctx context.Context, peers []peer.PeerInfo) error {
|
||||||
|
if n.DHT != nil {
|
||||||
|
for _, p := range peers {
|
||||||
|
// TODO bootstrap(ctx, n.PeerHost, n.DHT, n.Peerstore, peers)
|
||||||
|
if err := n.DHT.Connect(ctx, p.ID); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (n *IpfsNode) loadID() error {
|
func (n *IpfsNode) loadID() error {
|
||||||
if n.Identity != "" {
|
if n.Identity != "" {
|
||||||
return debugerror.New("identity already loaded")
|
return debugerror.New("identity already loaded")
|
||||||
|
Reference in New Issue
Block a user