Skip to content

Commit

Permalink
update bootstrapPeers to be func() []peer.AddrInfo (#716)
Browse files Browse the repository at this point in the history
* update bootstrapPeers to internally be func() []peer.AddrInfo

* add BootstrapPeersFunc and add test
  • Loading branch information
noot authored Jun 25, 2021
1 parent 6fff2a3 commit 7cf0c1e
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 9 deletions.
13 changes: 7 additions & 6 deletions dht.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,10 @@ type IpfsDHT struct {

autoRefresh bool

// A set of bootstrap peers to fallback on if all other attempts to fix
// A function returning a set of bootstrap peers to fallback on if all other attempts to fix
// the routing table fail (or, e.g., this is the first time this node is
// connecting to the network).
bootstrapPeers []peer.AddrInfo
bootstrapPeers func() []peer.AddrInfo

maxRecordAge time.Duration

Expand Down Expand Up @@ -473,15 +473,16 @@ func (dht *IpfsDHT) fixLowPeers(ctx context.Context) {
// We should first use non-bootstrap peers we knew of from previous
// snapshots of the Routing Table before we connect to the bootstrappers.
// See https://github.com/libp2p/go-libp2p-kad-dht/issues/387.
if dht.routingTable.Size() == 0 {
if len(dht.bootstrapPeers) == 0 {
if dht.routingTable.Size() == 0 && dht.bootstrapPeers != nil {
bootstrapPeers := dht.bootstrapPeers()
if len(bootstrapPeers) == 0 {
// No point in continuing, we have no peers!
return
}

found := 0
for _, i := range rand.Perm(len(dht.bootstrapPeers)) {
ai := dht.bootstrapPeers[i]
for _, i := range rand.Perm(len(bootstrapPeers)) {
ai := bootstrapPeers[i]
err := dht.Host().Connect(ctx, ai)
if err == nil {
found++
Expand Down
13 changes: 12 additions & 1 deletion dht_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,18 @@ func RoutingTableFilter(filter RouteTableFilterFunc) Option {
// and refresh our Routing Table if it becomes empty.
func BootstrapPeers(bootstrappers ...peer.AddrInfo) Option {
return func(c *dhtcfg.Config) error {
c.BootstrapPeers = bootstrappers
c.BootstrapPeers = func() []peer.AddrInfo {
return bootstrappers
}
return nil
}
}

// BootstrapPeersFunc configures the function that returns the bootstrapping nodes that we will
// connect to to seed and refresh our Routing Table if it becomes empty.
func BootstrapPeersFunc(getBootstrapPeers func() []peer.AddrInfo) Option {
return func(c *dhtcfg.Config) error {
c.BootstrapPeers = getBootstrapPeers
return nil
}
}
Expand Down
33 changes: 33 additions & 0 deletions dht_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2051,6 +2051,39 @@ func TestBootStrapWhenRTIsEmpty(t *testing.T) {
}, 5*time.Second, 500*time.Millisecond)
}

func TestBootstrapPeersFunc(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var lock sync.Mutex

bootstrapFuncA := func() []peer.AddrInfo {
return []peer.AddrInfo{}
}
dhtA := setupDHT(ctx, t, false, BootstrapPeersFunc(bootstrapFuncA))

bootstrapPeersB := []peer.AddrInfo{}
bootstrapFuncB := func() []peer.AddrInfo {
lock.Lock()
defer lock.Unlock()
return bootstrapPeersB
}

dhtB := setupDHT(ctx, t, false, BootstrapPeersFunc(bootstrapFuncB))
require.Equal(t, 0, len(dhtB.host.Network().Peers()))

addrA := peer.AddrInfo{
ID: dhtA.self,
Addrs: dhtA.host.Addrs(),
}

lock.Lock()
bootstrapPeersB = []peer.AddrInfo{addrA}
lock.Unlock()

dhtB.fixLowPeers(ctx)
require.NotEqual(t, 0, len(dhtB.host.Network().Peers()))
}

func TestPreconnectedNodes(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand Down
2 changes: 1 addition & 1 deletion fullrt/dht.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func NewFullRT(h host.Host, protocolPrefix protocol.ID, options ...Option) (*Ful

var bsPeers []*peer.AddrInfo

for _, ai := range dhtcfg.BootstrapPeers {
for _, ai := range dhtcfg.BootstrapPeers() {
tmpai := ai
bsPeers = append(bsPeers, &tmpai)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ type Config struct {
DiversityFilter peerdiversity.PeerIPGroupFilter
}

BootstrapPeers []peer.AddrInfo
BootstrapPeers func() []peer.AddrInfo

// test specific Config options
DisableFixLowPeers bool
Expand Down

0 comments on commit 7cf0c1e

Please sign in to comment.