forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrappers.go
54 lines (43 loc) · 1.47 KB
/
bootstrappers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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
}