forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rename beacon to boostrapper, define bootstrappers in JSON file for c…
…ross-language compatiblity (ava-labs#1439) Co-authored-by: Stephen Buttolph <[email protected]>
- Loading branch information
1 parent
243e313
commit 6ba90f7
Showing
11 changed files
with
371 additions
and
218 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
_ "embed" | ||
|
||
"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 | ||
} |
Oops, something went wrong.