From 9d64a8be8e8c2fe86f85d21a01e3701b48ae62af Mon Sep 17 00:00:00 2001 From: Jay Jie Date: Tue, 8 Oct 2024 21:50:15 -0700 Subject: [PATCH] chore: add logic for when bonded > active --- rest/rest.go | 39 +++++++++++++++++++++++++++++++++------ rest/routes.go | 4 ++++ types/staking_params.go | 12 ++++++++++++ 3 files changed, 49 insertions(+), 6 deletions(-) create mode 100644 types/staking_params.go diff --git a/rest/rest.go b/rest/rest.go index f19dd14..ec75282 100644 --- a/rest/rest.go +++ b/rest/rest.go @@ -14,8 +14,6 @@ import ( var GetConspubMonikerMapWrapper = GetConspubMonikerMap -const activeSetSize = 180 - func GetConspubMonikerMap() map[string]string { var v types.RpcValidators var vResult map[string]string = make(map[string]string) @@ -39,13 +37,42 @@ func GetConspubMonikerMap() map[string]string { return tokensI > tokensJ }) - activeValidators := v.Validators - if len(activeValidators) > activeSetSize { - activeValidators = activeValidators[:activeSetSize] + bondedValidators := v.Validators + stakingParams := GetStakingParams() + + /* + * This logic only applies to chains (e.g. the Cosmos Hub) where + * a validator can be bonded but inactive (e.g. not signing blocks) + * e.g. max_validators=200 but only the top 180 are active + * + * For most other chains, if a validator is bonded, they are active + */ + + // TO-DO replace MaxValidators here with active validators + if len(bondedValidators) > stakingParams.Params.MaxValidators { + bondedValidators = bondedValidators[:stakingParams.Params.MaxValidators] } - for _, validator := range activeValidators { + for _, validator := range bondedValidators { vResult[validator.ConsPubKey.Key] = validator.Description.Moniker } return vResult } + +func GetStakingParams() types.StakingParams { + route := GetStakingParamsRoute() + res, err := utils.HttpQuery(constants.RESTAddr + route) + if err != nil { + zap.L().Fatal("Connection to REST failed", zap.Bool("Success", false), zap.String("err:", err.Error())) + return types.StakingParams{} + } + + var stakingParams types.StakingParams + err = json.Unmarshal(res, &stakingParams) + if err != nil { + zap.L().Fatal("Failed to unmarshal response", zap.Bool("Success", false), zap.String("err:", err.Error())) + return types.StakingParams{} + } + + return stakingParams +} diff --git a/rest/routes.go b/rest/routes.go index 4121906..fb19ce9 100644 --- a/rest/routes.go +++ b/rest/routes.go @@ -78,6 +78,10 @@ func GetUpgradeCurrentPlanRoute(cfg config.Config) string { return "/cosmos/upgrade/v1beta1/current_plan" } +func GetStakingParamsRoute() string { + return "/cosmos/staking/v1beta1/params" +} + /*********************** * IBC Routes ************************/ diff --git a/types/staking_params.go b/types/staking_params.go new file mode 100644 index 0000000..76c60ad --- /dev/null +++ b/types/staking_params.go @@ -0,0 +1,12 @@ +package types + +type StakingParams struct { + Params struct { + UnbondingTime string `json:"unbonding_time"` + MaxValidators int `json:"max_validators"` + MaxEntries int `json:"max_entries"` + HistoricalEntries int `json:"historical_entries"` + BondDenom string `json:"bond_denom"` + MinCommissionRate string `json:"min_commission_rate"` + } `json:"params"` +}