diff --git a/api/health/worker.go b/api/health/worker.go index 4bcdb3a22acc..4ccebc353875 100644 --- a/api/health/worker.go +++ b/api/health/worker.go @@ -12,6 +12,8 @@ import ( "github.com/prometheus/client_golang/prometheus" + "golang.org/x/exp/maps" + "github.com/ava-labs/avalanchego/utils" ) @@ -120,10 +122,7 @@ func (w *worker) runChecks(ctx context.Context) { // during this iteration. If [w.checks] is modified during this iteration of // [runChecks], then the added check will not be run until the next // iteration. - checks := make(map[string]Checker, len(w.checks)) - for name, checker := range w.checks { - checks[name] = checker - } + checks := maps.Clone(w.checks) w.checksLock.RUnlock() var wg sync.WaitGroup diff --git a/api/server/router.go b/api/server/router.go index 00d69c0d04f6..8bbc38cfc33f 100644 --- a/api/server/router.go +++ b/api/server/router.go @@ -10,6 +10,8 @@ import ( "sync" "github.com/gorilla/mux" + + "github.com/ava-labs/avalanchego/utils/set" ) var ( @@ -22,7 +24,7 @@ type router struct { router *mux.Router routeLock sync.Mutex - reservedRoutes map[string]bool // Reserves routes so that there can't be alias that conflict + reservedRoutes set.Set[string] // Reserves routes so that there can't be alias that conflict aliases map[string][]string // Maps a route to a set of reserved routes routes map[string]map[string]http.Handler // Maps routes to a handler } @@ -30,7 +32,7 @@ type router struct { func newRouter() *router { return &router{ router: mux.NewRouter(), - reservedRoutes: make(map[string]bool), + reservedRoutes: set.Set[string]{}, aliases: make(map[string][]string), routes: make(map[string]map[string]http.Handler), } @@ -68,7 +70,7 @@ func (r *router) AddRouter(base, endpoint string, handler http.Handler) error { } func (r *router) addRouter(base, endpoint string, handler http.Handler) error { - if r.reservedRoutes[base] { + if r.reservedRoutes.Contains(base) { return fmt.Errorf("couldn't route to %s as that route is either aliased or already maps to a handler", base) } @@ -113,13 +115,13 @@ func (r *router) AddAlias(base string, aliases ...string) error { defer r.routeLock.Unlock() for _, alias := range aliases { - if r.reservedRoutes[alias] { + if r.reservedRoutes.Contains(alias) { return fmt.Errorf("couldn't alias to %s as that route is either already aliased or already maps to a handler", alias) } } for _, alias := range aliases { - r.reservedRoutes[alias] = true + r.reservedRoutes.Add(alias) } r.aliases[base] = append(r.aliases[base], aliases...) diff --git a/snow/consensus/avalanche/topological.go b/snow/consensus/avalanche/topological.go index 84cd47c1266d..2dbcc8167a79 100644 --- a/snow/consensus/avalanche/topological.go +++ b/snow/consensus/avalanche/topological.go @@ -11,6 +11,8 @@ import ( "go.uber.org/zap" + "golang.org/x/exp/maps" + "github.com/ava-labs/avalanchego/ids" "github.com/ava-labs/avalanchego/snow" "github.com/ava-labs/avalanchego/snow/choices" @@ -315,9 +317,7 @@ func (ta *Topological) HealthCheck(ctx context.Context) (interface{}, error) { // the non-transitively applied votes. Also returns the list of leaf nodes. func (ta *Topological) calculateInDegree(responses bag.UniqueBag[ids.ID]) error { // Clear the kahn node set - for k := range ta.kahnNodes { - delete(ta.kahnNodes, k) - } + maps.Clear(ta.kahnNodes) // Clear the leaf set ta.leaves.Clear() diff --git a/utils/set/set.go b/utils/set/set.go index 7712b7206a42..b946c761896b 100644 --- a/utils/set/set.go +++ b/utils/set/set.go @@ -134,6 +134,8 @@ func (s Set[T]) CappedList(size int) []T { // Equals returns true if the sets contain the same elements func (s Set[T]) Equals(other Set[T]) bool { + // Using maps.Equals makes the build not work for some reason so do this + // manually. if s.Len() != other.Len() { return false } diff --git a/utils/sorting.go b/utils/sorting.go index 115bb0a28863..156b81caa783 100644 --- a/utils/sorting.go +++ b/utils/sorting.go @@ -83,6 +83,7 @@ func IsSortedAndUniqueByHash[T ~[]byte](s []T) bool { // Returns true iff the elements in [s] are unique. func IsUnique[T comparable](elts []T) bool { + // Can't use set.Set because it'd be a circular import. asMap := make(map[T]struct{}, len(elts)) for _, elt := range elts { if _, ok := asMap[elt]; ok { diff --git a/vms/platformvm/service.go b/vms/platformvm/service.go index d1e6f03472f9..b2bd4f93f1bc 100644 --- a/vms/platformvm/service.go +++ b/vms/platformvm/service.go @@ -14,6 +14,8 @@ import ( "go.uber.org/zap" + "golang.org/x/exp/maps" + "github.com/ava-labs/avalanchego/api" "github.com/ava-labs/avalanchego/cache" "github.com/ava-labs/avalanchego/database" @@ -288,10 +290,7 @@ utxoFor: response.UTXOIDs = append(response.UTXOIDs, &utxo.UTXOID) } - balances := map[ids.ID]uint64{} - for assetID, amount := range lockedStakeables { - balances[assetID] = amount - } + balances := maps.Clone(lockedStakeables) for assetID, amount := range lockedNotStakeables { newBalance, err := math.Add64(balances[assetID], amount) if err != nil {