Skip to content

Commit 15877b5

Browse files
bruce-rileyevan-gray
authored andcommitted
Node/Devnet: Extract first guardian name from bootstrap peers
1 parent 87c754a commit 15877b5

File tree

3 files changed

+89
-8
lines changed

3 files changed

+89
-8
lines changed

node/cmd/guardiand/node.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -785,19 +785,17 @@ func runNode(cmd *cobra.Command, args []string) {
785785
p2pKey = devnet.DeterministicP2PPrivKeyByIndex(int64(idx))
786786

787787
if idx != 0 {
788+
firstGuardianName, err := devnet.GetFirstGuardianNameFromBootstrapPeers(*p2pBootstrap)
789+
if err != nil {
790+
logger.Fatal("failed to get first guardian name from bootstrap peers", zap.String("bootstrapPeers", *p2pBootstrap), zap.Error(err))
791+
}
788792
// try to connect to guardian-0
789793
for {
790-
// tilt uses this hostname format
791-
_, err := net.LookupIP("guardian-0.guardian")
792-
if err == nil {
793-
break
794-
}
795-
// load tests use this hostname format
796-
_, err = net.LookupIP("guardian-0")
794+
_, err := net.LookupIP(firstGuardianName)
797795
if err == nil {
798796
break
799797
}
800-
logger.Info("Error resolving guardian-0.guardian. Trying again...")
798+
logger.Info(fmt.Sprintf("Error resolving %s. Trying again...", firstGuardianName))
801799
time.Sleep(time.Second)
802800
}
803801
// TODO this is a hack. If this is not the bootstrap Guardian, we wait 10s such that the bootstrap Guardian has enough time to start.

node/pkg/devnet/hostname.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package devnet
22

33
import (
4+
"errors"
45
"fmt"
56
"os"
67
"strconv"
@@ -27,3 +28,16 @@ func GetDevnetIndex() (int, error) {
2728

2829
return i, nil
2930
}
31+
32+
// GetFirstGuardianNameFromBootstrapPeers extracts the hostname of the first peer from the bootstrap peers string.
33+
func GetFirstGuardianNameFromBootstrapPeers(bootstrapPeers string) (string, error) {
34+
peers := strings.Split(bootstrapPeers, ",")
35+
if len(peers) == 0 {
36+
return "", errors.New("failed to parse devnet bootstrap peers")
37+
}
38+
fields := strings.Split(peers[0], "/")
39+
if len(fields) < 3 {
40+
return "", errors.New("failed to parse devnet first bootstrap peer")
41+
}
42+
return fields[2], nil
43+
}

node/pkg/devnet/hostname_test.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package devnet
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func TestGetFirstGuardianNameFromBootstrapPeers(t *testing.T) {
11+
type test struct {
12+
label string
13+
bootstrapPeers string
14+
errText string // empty string means success
15+
expectedHostName string
16+
}
17+
18+
var tests = []test{
19+
{
20+
label: "Success with one bootstrap peer",
21+
bootstrapPeers: "/dns4/guardian-0.guardian/udp/8999/quic/p2p/12D3KooWL3XJ9EMCyZvmmGXL2LMiVBtrVa2BuESsJiXkSj7333Jw",
22+
errText: "",
23+
expectedHostName: "guardian-0.guardian",
24+
},
25+
{
26+
label: "Success with two bootstrap peer",
27+
bootstrapPeers: "/dns4/guardian-0.guardian/udp/8999/quic/p2p/12D3KooWL3XJ9EMCyZvmmGXL2LMiVBtrVa2BuESsJiXkSj7333Jw,/dns4/guardian-1.guardian/udp/8999/quic/p2p/12D3KooWL3XJ9EMCyZvmmGXL2LMiVBtrVa2BuESsJiXkSj7333Jx",
28+
errText: "",
29+
expectedHostName: "guardian-0.guardian",
30+
},
31+
{
32+
label: "Success when using IP",
33+
bootstrapPeers: "/dns4/10.121.2.4/udp/8999/quic/p2p/12D3KooWL3XJ9EMCyZvmmGXL2LMiVBtrVa2BuESsJiXkSj7333Jw",
34+
errText: "",
35+
expectedHostName: "10.121.2.4",
36+
},
37+
{
38+
label: "Empty bootstrap peers",
39+
bootstrapPeers: "",
40+
errText: "failed to parse devnet first bootstrap peer",
41+
expectedHostName: "",
42+
},
43+
{
44+
label: "Empty first bootstrap peer",
45+
bootstrapPeers: ",/dns4/guardian-0.guardian/udp/8999/quic/p2p/12D3KooWL3XJ9EMCyZvmmGXL2LMiVBtrVa2BuESsJiXkSj7333Jw",
46+
errText: "failed to parse devnet first bootstrap peer",
47+
expectedHostName: "",
48+
},
49+
{
50+
label: "No slashes",
51+
bootstrapPeers: ":dns4:10.121.2.4:udp:8999:quic:p2p:12D3KooWL3XJ9EMCyZvmmGXL2LMiVBtrVa2BuESsJiXkSj7333Jw",
52+
errText: "failed to parse devnet first bootstrap peer",
53+
expectedHostName: "",
54+
},
55+
}
56+
57+
for _, tst := range tests {
58+
t.Run(tst.label, func(t *testing.T) {
59+
hostName, err := GetFirstGuardianNameFromBootstrapPeers(tst.bootstrapPeers)
60+
if tst.errText == "" {
61+
require.NoError(t, err)
62+
assert.Equal(t, tst.expectedHostName, hostName)
63+
} else {
64+
require.ErrorContains(t, err, tst.errText)
65+
}
66+
})
67+
}
68+
69+
}

0 commit comments

Comments
 (0)