Skip to content

Commit 26267c7

Browse files
authored
feat(nodebuilder/p2p): If node is a bootstrapper, automatically initiate connection to other hardcoded bootstrappers for the given network (#4182)
1 parent a87a175 commit 26267c7

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

nodebuilder/p2p/bootstrap.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package p2p
22

33
import (
4+
"context"
45
"os"
56
"strconv"
7+
"sync"
68

9+
"github.com/libp2p/go-libp2p/core/host"
710
"github.com/libp2p/go-libp2p/core/peer"
811
ma "github.com/multiformats/go-multiaddr"
912
)
@@ -82,3 +85,37 @@ func parseAddrInfos(addrs []string) ([]peer.AddrInfo, error) {
8285

8386
return infos, nil
8487
}
88+
89+
// connectToBootstrappers ensures that the bootstrapper node
90+
// initiates a connection to other hardcoded bootstrap peers
91+
// while the connectionManager hook adds them as mutual peers to prevent
92+
// trimming the connection. This will aid the network's connectivity.
93+
func connectToBootstrappers(ctx context.Context, h host.Host, network Network) error {
94+
if !isBootstrapper() {
95+
return nil
96+
}
97+
98+
boots, err := BootstrappersFor(network)
99+
if err != nil {
100+
return err
101+
}
102+
103+
wg := sync.WaitGroup{}
104+
wg.Add(len(boots))
105+
for _, b := range boots {
106+
go func() {
107+
defer wg.Done()
108+
109+
err := h.Connect(ctx, b)
110+
if err != nil {
111+
log.Errorw("bootstrap: failed to connect to bootstrapper", "id", b, "err", err)
112+
return
113+
}
114+
115+
log.Infow("bootstrap: successfully connected to bootstrapper", "id", b.String())
116+
}()
117+
}
118+
wg.Wait()
119+
120+
return nil
121+
}

nodebuilder/p2p/module.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ func ConstructModule(tp node.Type, cfg *Config) fx.Option {
4242
baseComponents,
4343
fx.Provide(infiniteResources),
4444
fx.Invoke(reachabilityCheck),
45+
fx.Invoke(connectToBootstrappers),
4546
)
4647
case node.Light:
4748
return fx.Module(

0 commit comments

Comments
 (0)