Skip to content

Commit 6ba90f7

Browse files
Rename beacon to boostrapper, define bootstrappers in JSON file for cross-language compatiblity (ava-labs#1439)
Co-authored-by: Stephen Buttolph <stephen@avalabs.org>
1 parent 243e313 commit 6ba90f7

File tree

11 files changed

+371
-218
lines changed

11 files changed

+371
-218
lines changed

config/config.go

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ const (
6262

6363
var (
6464
// Deprecated key --> deprecation message (i.e. which key replaces it)
65+
// TODO: deprecate "BootstrapIDsKey" and "BootstrapIPsKey"
6566
deprecatedKeys = map[string]string{
6667
NetworkCompressionEnabledKey: fmt.Sprintf("use --%s instead", NetworkCompressionTypeKey),
6768
GenesisConfigFileKey: fmt.Sprintf("use --%s instead", GenesisFileKey),
@@ -532,6 +533,8 @@ func getBootstrapConfig(v *viper.Viper, networkID uint32) (node.BootstrapConfig,
532533
BootstrapAncestorsMaxContainersReceived: int(v.GetUint(BootstrapAncestorsMaxContainersReceivedKey)),
533534
}
534535

536+
// TODO: Add a "BootstrappersKey" flag to more clearly enforce ID and IP
537+
// length equality.
535538
ipsSet := v.IsSet(BootstrapIPsKey)
536539
idsSet := v.IsSet(BootstrapIDsKey)
537540
if ipsSet && !idsSet {
@@ -540,40 +543,49 @@ func getBootstrapConfig(v *viper.Viper, networkID uint32) (node.BootstrapConfig,
540543
if !ipsSet && idsSet {
541544
return node.BootstrapConfig{}, fmt.Errorf("set %q but didn't set %q", BootstrapIDsKey, BootstrapIPsKey)
542545
}
543-
544-
bootstrapIPs, bootstrapIDs := genesis.SampleBeacons(networkID, 5)
545-
if ipsSet {
546-
bootstrapIPs = strings.Split(v.GetString(BootstrapIPsKey), ",")
546+
if !ipsSet && !idsSet {
547+
config.Bootstrappers = genesis.SampleBootstrappers(networkID, 5)
548+
return config, nil
547549
}
548-
for _, ip := range bootstrapIPs {
550+
551+
bootstrapIPs := strings.Split(v.GetString(BootstrapIPsKey), ",")
552+
config.Bootstrappers = make([]genesis.Bootstrapper, 0, len(bootstrapIPs))
553+
for _, bootstrapIP := range bootstrapIPs {
554+
ip := strings.TrimSpace(bootstrapIP)
549555
if ip == "" {
550556
continue
551557
}
558+
552559
addr, err := ips.ToIPPort(ip)
553560
if err != nil {
554561
return node.BootstrapConfig{}, fmt.Errorf("couldn't parse bootstrap ip %s: %w", ip, err)
555562
}
556-
config.BootstrapIPs = append(config.BootstrapIPs, addr)
563+
config.Bootstrappers = append(config.Bootstrappers, genesis.Bootstrapper{
564+
// ID is populated below
565+
IP: ips.IPDesc(addr),
566+
})
557567
}
558568

559-
if idsSet {
560-
bootstrapIDs = strings.Split(v.GetString(BootstrapIDsKey), ",")
561-
}
562-
for _, id := range bootstrapIDs {
569+
bootstrapIDs := strings.Split(v.GetString(BootstrapIDsKey), ",")
570+
bootstrapNodeIDs := make([]ids.NodeID, 0, len(bootstrapIDs))
571+
for _, bootstrapID := range bootstrapIDs {
572+
id := strings.TrimSpace(bootstrapID)
563573
if id == "" {
564574
continue
565575
}
576+
566577
nodeID, err := ids.NodeIDFromString(id)
567578
if err != nil {
568579
return node.BootstrapConfig{}, fmt.Errorf("couldn't parse bootstrap peer id %s: %w", id, err)
569580
}
570-
config.BootstrapIDs = append(config.BootstrapIDs, nodeID)
581+
bootstrapNodeIDs = append(bootstrapNodeIDs, nodeID)
571582
}
572583

573-
lenIPs := len(config.BootstrapIPs)
574-
lenIDs := len(config.BootstrapIDs)
575-
if lenIPs != lenIDs {
576-
return node.BootstrapConfig{}, fmt.Errorf("expected the number of bootstrapIPs (%d) to match the number of bootstrapIDs (%d)", lenIPs, lenIDs)
584+
if len(config.Bootstrappers) != len(bootstrapNodeIDs) {
585+
return node.BootstrapConfig{}, fmt.Errorf("expected the number of bootstrapIPs (%d) to match the number of bootstrapIDs (%d)", len(config.Bootstrappers), len(bootstrapNodeIDs))
586+
}
587+
for i, nodeID := range bootstrapNodeIDs {
588+
config.Bootstrappers[i].ID = nodeID
577589
}
578590

579591
return config, nil

config/flags.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ func addNodeFlags(fs *pflag.FlagSet) {
297297
fs.String(StateSyncIDsKey, "", "Comma separated list of state sync peer ids to connect to. Example: NodeID-JR4dVmy6ffUGAKCBDkyCbeZbyHQBeDsET,NodeID-8CrVPQZ4VSqgL8zTdvL14G8HqAfrBr4z")
298298

299299
// Bootstrapping
300+
// TODO: combine "BootstrapIPsKey" and "BootstrapIDsKey" into one flag
300301
fs.String(BootstrapIPsKey, "", "Comma separated list of bootstrap peer ips to connect to. Example: 127.0.0.1:9630,127.0.0.1:9631")
301302
fs.String(BootstrapIDsKey, "", "Comma separated list of bootstrap peer ids to connect to. Example: NodeID-JR4dVmy6ffUGAKCBDkyCbeZbyHQBeDsET,NodeID-8CrVPQZ4VSqgL8zTdvL14G8HqAfrBr4z")
302303
fs.Bool(RetryBootstrapKey, true, "Specifies whether bootstrap should be retried")

genesis/beacons.go

Lines changed: 0 additions & 150 deletions
This file was deleted.

genesis/bootstrappers.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
2+
// See the file LICENSE for licensing terms.
3+
4+
package genesis
5+
6+
import (
7+
"encoding/json"
8+
"fmt"
9+
10+
_ "embed"
11+
12+
"github.com/ava-labs/avalanchego/ids"
13+
"github.com/ava-labs/avalanchego/utils/constants"
14+
"github.com/ava-labs/avalanchego/utils/ips"
15+
"github.com/ava-labs/avalanchego/utils/math"
16+
"github.com/ava-labs/avalanchego/utils/sampler"
17+
)
18+
19+
var (
20+
//go:embed bootstrappers.json
21+
bootstrappersPerNetworkJSON []byte
22+
23+
bootstrappersPerNetwork map[string][]Bootstrapper
24+
)
25+
26+
func init() {
27+
if err := json.Unmarshal(bootstrappersPerNetworkJSON, &bootstrappersPerNetwork); err != nil {
28+
panic(fmt.Sprintf("failed to decode bootstrappers.json %v", err))
29+
}
30+
}
31+
32+
// Represents the relationship between the nodeID and the nodeIP.
33+
// The bootstrapper is sometimes called "anchor" or "beacon" node.
34+
type Bootstrapper struct {
35+
ID ids.NodeID `json:"id"`
36+
IP ips.IPDesc `json:"ip"`
37+
}
38+
39+
// SampleBootstrappers returns the some beacons this node should connect to
40+
func SampleBootstrappers(networkID uint32, count int) []Bootstrapper {
41+
networkName := constants.NetworkIDToNetworkName[networkID]
42+
bootstrappers := bootstrappersPerNetwork[networkName]
43+
count = math.Min(count, len(bootstrappers))
44+
45+
s := sampler.NewUniform()
46+
s.Initialize(uint64(len(bootstrappers)))
47+
indices, _ := s.Sample(count)
48+
49+
sampled := make([]Bootstrapper, 0, len(indices))
50+
for _, index := range indices {
51+
sampled = append(sampled, bootstrappers[int(index)])
52+
}
53+
return sampled
54+
}

0 commit comments

Comments
 (0)