-
Notifications
You must be signed in to change notification settings - Fork 775
Rename beacon to boostrapper, define bootstrappers in JSON file for cross-language compatiblity #1439
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rename beacon to boostrapper, define bootstrappers in JSON file for cross-language compatiblity #1439
Changes from 27 commits
e77d476
671370d
4ca21a7
db00eca
afa65c8
ce679ca
f86d352
b3bd5b4
d20c3ac
ba89041
6659f38
d38f33a
bd17aaf
770ec1c
9caf16a
fcef58e
6dfc25e
3d0d936
9425eb1
1b4809a
0749f29
4d9f66c
911dea6
fc499b9
d115c33
ef42f4a
2bb8be2
e52a4d7
b16d85f
da33ca3
bf5eeac
256fd3a
8001e89
e4f5868
ce36ed0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,8 +62,11 @@ const ( | |
|
||
var ( | ||
// Deprecated key --> deprecation message (i.e. which key replaces it) | ||
// TODO: deprecate "BootstrapIDsKey" and "BootstrapIPsKey" | ||
deprecatedKeys = map[string]string{ | ||
NetworkCompressionEnabledKey: fmt.Sprintf("use --%s instead", NetworkCompressionTypeKey), | ||
BootstrapIDsKey: fmt.Sprintf("use --%s instead", BootstrappersKey), | ||
BootstrapIPsKey: fmt.Sprintf("use --%s instead", BootstrappersKey), | ||
gyuho marked this conversation as resolved.
Show resolved
Hide resolved
|
||
GenesisConfigFileKey: fmt.Sprintf("use --%s instead", GenesisFileKey), | ||
GenesisConfigContentKey: fmt.Sprintf("use --%s instead", GenesisFileContentKey), | ||
InboundConnUpgradeThrottlerCooldownKey: fmt.Sprintf("use --%s instead", NetworkInboundConnUpgradeThrottlerCooldownKey), | ||
|
@@ -532,6 +535,12 @@ func getBootstrapConfig(v *viper.Viper, networkID uint32) (node.BootstrapConfig, | |
BootstrapAncestorsMaxContainersReceived: int(v.GetUint(BootstrapAncestorsMaxContainersReceivedKey)), | ||
} | ||
|
||
if v.IsSet(BootstrappersKey) { | ||
s := v.GetString(BootstrappersKey) | ||
return config, json.Unmarshal([]byte(s), &config.Bootstrappers) | ||
} | ||
|
||
// "--bootstrappers" is not set, so fallback to old flags | ||
ipsSet := v.IsSet(BootstrapIPsKey) | ||
idsSet := v.IsSet(BootstrapIDsKey) | ||
if ipsSet && !idsSet { | ||
|
@@ -541,39 +550,48 @@ func getBootstrapConfig(v *viper.Viper, networkID uint32) (node.BootstrapConfig, | |
return node.BootstrapConfig{}, fmt.Errorf("set %q but didn't set %q", BootstrapIDsKey, BootstrapIPsKey) | ||
} | ||
|
||
bootstrapIPs, bootstrapIDs := genesis.SampleBeacons(networkID, 5) | ||
bootstrappers := genesis.SampleBootstrappers(networkID, 5) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code is really pretty jank and confusing. Should we replace There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1000, I think combining them into a single flag value of JSON format is much simpler. |
||
if ipsSet { | ||
bootstrapIPs = strings.Split(v.GetString(BootstrapIPsKey), ",") | ||
} | ||
for _, ip := range bootstrapIPs { | ||
if ip == "" { | ||
continue | ||
} | ||
addr, err := ips.ToIPPort(ip) | ||
if err != nil { | ||
return node.BootstrapConfig{}, fmt.Errorf("couldn't parse bootstrap ip %s: %w", ip, err) | ||
bootstrapIPs := strings.Split(v.GetString(BootstrapIPsKey), ",") | ||
|
||
bootstrappers = make([]genesis.Bootstrapper, 0, len(bootstrapIPs)) | ||
for _, bootstrapIP := range bootstrapIPs { | ||
ip := strings.TrimSpace(bootstrapIP) | ||
if ip == "" { | ||
continue | ||
} | ||
|
||
addr, err := ips.ToIPPort(ip) | ||
if err != nil { | ||
return node.BootstrapConfig{}, fmt.Errorf("couldn't parse bootstrap ip %s: %w", ip, err) | ||
} | ||
bootstrappers = append(bootstrappers, genesis.Bootstrapper{ID: ids.EmptyNodeID, IP: ips.IPDesc(addr)}) | ||
} | ||
config.BootstrapIPs = append(config.BootstrapIPs, addr) | ||
} | ||
config.Bootstrappers = bootstrappers | ||
|
||
if idsSet { | ||
bootstrapIDs = strings.Split(v.GetString(BootstrapIDsKey), ",") | ||
} | ||
for _, id := range bootstrapIDs { | ||
if id == "" { | ||
continue | ||
} | ||
nodeID, err := ids.NodeIDFromString(id) | ||
if err != nil { | ||
return node.BootstrapConfig{}, fmt.Errorf("couldn't parse bootstrap peer id %s: %w", id, err) | ||
bootstrapIDs := strings.Split(v.GetString(BootstrapIDsKey), ",") | ||
|
||
bootstrapNodeIDs := make([]ids.NodeID, 0, len(bootstrapIDs)) | ||
for _, bootstrapID := range bootstrapIDs { | ||
id := strings.TrimSpace(bootstrapID) | ||
if id == "" { | ||
continue | ||
} | ||
nodeID, err := ids.NodeIDFromString(id) | ||
if err != nil { | ||
return node.BootstrapConfig{}, fmt.Errorf("couldn't parse bootstrap peer id %s: %w", nodeID, err) | ||
} | ||
bootstrapNodeIDs = append(bootstrapNodeIDs, nodeID) | ||
} | ||
config.BootstrapIDs = append(config.BootstrapIDs, nodeID) | ||
} | ||
|
||
lenIPs := len(config.BootstrapIPs) | ||
lenIDs := len(config.BootstrapIDs) | ||
if lenIPs != lenIDs { | ||
return node.BootstrapConfig{}, fmt.Errorf("expected the number of bootstrapIPs (%d) to match the number of bootstrapIDs (%d)", lenIPs, lenIDs) | ||
if len(config.Bootstrappers) != len(bootstrapNodeIDs) { | ||
return node.BootstrapConfig{}, fmt.Errorf("expected the number of bootstrapIPs (%d) to match the number of bootstrapNodeIDs (%d)", len(config.Bootstrappers), len(bootstrapNodeIDs)) | ||
} | ||
for i, nodeID := range bootstrapNodeIDs { | ||
config.Bootstrappers[i].ID = nodeID | ||
} | ||
} | ||
|
||
return config, nil | ||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package genesis | ||
|
||
import ( | ||
_ "embed" | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/ava-labs/avalanchego/ids" | ||
"github.com/ava-labs/avalanchego/utils/constants" | ||
"github.com/ava-labs/avalanchego/utils/ips" | ||
"github.com/ava-labs/avalanchego/utils/math" | ||
"github.com/ava-labs/avalanchego/utils/sampler" | ||
) | ||
|
||
var ( | ||
//go:embed bootstrappers.json | ||
bootstrappersPerNetworkJSON []byte | ||
|
||
bootstrappersPerNetwork map[string][]Bootstrapper | ||
) | ||
|
||
func init() { | ||
if err := json.Unmarshal(bootstrappersPerNetworkJSON, &bootstrappersPerNetwork); err != nil { | ||
panic(fmt.Sprintf("failed to decode bootstrappers.json %v", err)) | ||
} | ||
} | ||
|
||
// Represents the relationship between the nodeID and the nodeIP. | ||
// The bootstrapper is sometimes called "anchor" or "beacon" node. | ||
type Bootstrapper struct { | ||
ID ids.NodeID `json:"id"` | ||
IP ips.IPDesc `json:"ip"` | ||
} | ||
|
||
// SampleBootstrappers returns the some beacons this node should connect to | ||
func SampleBootstrappers(networkID uint32, count int) []Bootstrapper { | ||
networkName := constants.NetworkIDToNetworkName[networkID] | ||
bootstrappers := bootstrappersPerNetwork[networkName] | ||
count = math.Min(count, len(bootstrappers)) | ||
|
||
s := sampler.NewUniform() | ||
s.Initialize(uint64(len(bootstrappers))) | ||
indices, _ := s.Sample(count) | ||
|
||
sampled := make([]Bootstrapper, 0, len(indices)) | ||
for _, index := range indices { | ||
sampled = append(sampled, bootstrappers[int(index)]) | ||
} | ||
|
||
return sampled | ||
} |
Uh oh!
There was an error while loading. Please reload this page.