Skip to content

Commit

Permalink
chore: add logic for when bonded > active
Browse files Browse the repository at this point in the history
  • Loading branch information
jim380 committed Oct 9, 2024
1 parent 059d83c commit 9d64a8b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 6 deletions.
39 changes: 33 additions & 6 deletions rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
4 changes: 4 additions & 0 deletions rest/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
************************/
Expand Down
12 changes: 12 additions & 0 deletions types/staking_params.go
Original file line number Diff line number Diff line change
@@ -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"`
}

0 comments on commit 9d64a8b

Please sign in to comment.