Skip to content

Commit

Permalink
Fix panic when backup bootstrap peer load and save funcs are nil
Browse files Browse the repository at this point in the history
A panic occurs when the first bootstrap round runs is these functions are not assigned in the configuration:
- `LoadBackupBootstrapPeers`
- `SaveBackupBootstrapPeers`

This fix assumes that it is acceptable for these functions to be nil, as it may be desirable to disable the bakcup peer load and save functionality.
  • Loading branch information
gammazero committed Jul 26, 2023
1 parent 4c35289 commit 5ee9170
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
9 changes: 8 additions & 1 deletion core/bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ func Bootstrap(id peer.ID, host host.Host, rt routing.Routing, cfg BootstrapConf
doneWithRound <- struct{}{}
close(doneWithRound) // it no longer blocks periodic

startSavePeersAsTemporaryBootstrapProc(cfg, host, proc)
if cfg.LoadBackupBootstrapPeers != nil && cfg.SaveBackupBootstrapPeers != nil {
startSavePeersAsTemporaryBootstrapProc(cfg, host, proc)
}

return proc, nil
}
Expand Down Expand Up @@ -243,6 +245,11 @@ func bootstrapRound(ctx context.Context, host host.Host, cfg BootstrapConfig) er
}
}

if cfg.LoadBackupBootstrapPeers == nil {
log.Debugf("not enough bootstrap peers to fill the remaining target of %d connections", numToDial)
return ErrNotEnoughBootstrapPeers
}

log.Debugf("not enough bootstrap peers to fill the remaining target of %d connections, trying backup list", numToDial)

tempBootstrapPeers := cfg.LoadBackupBootstrapPeers(ctx)
Expand Down
32 changes: 32 additions & 0 deletions core/bootstrap/bootstrap_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package bootstrap

import (
"crypto/rand"
"testing"
"time"

"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/test"
)
Expand All @@ -23,3 +27,31 @@ func TestRandomizeAddressList(t *testing.T) {
t.Fail()
}
}

func TestNoTempPeersLoadAndSave(t *testing.T) {
period := 500 * time.Millisecond
bootCfg := BootstrapConfigWithPeers(nil)
bootCfg.MinPeerThreshold = 2
bootCfg.Period = period

priv, pub, err := crypto.GenerateEd25519Key(rand.Reader)
if err != nil {
t.Fatal(err)
}
peerID, err := peer.IDFromPublicKey(pub)
if err != nil {
t.Fatal(err)
}
p2pHost, err := libp2p.New(libp2p.Identity(priv))
if err != nil {
t.Fatal(err)
}

bootstrapper, err := Bootstrap(peerID, p2pHost, nil, bootCfg)
if err != nil {
t.Fatal(err)
}

time.Sleep(4 * period)
bootstrapper.Close()
}

0 comments on commit 5ee9170

Please sign in to comment.