From 47f06b8d26dc28367b03752eade34a0456474a9b Mon Sep 17 00:00:00 2001 From: Jay Jie Date: Sat, 16 Sep 2023 23:44:51 -0700 Subject: [PATCH] refactor: move getValidator into getValidatorsets --- rest/rest.go | 15 ++++----------- rest/validatorsets.go | 18 +++++++++++++++++- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/rest/rest.go b/rest/rest.go index fae5aad..d46b752 100644 --- a/rest/rest.go +++ b/rest/rest.go @@ -77,22 +77,15 @@ func GetData(cfg *config.Config, blockHeight int64, blockData Blocks, denom stri rd.getSlashingParams(*cfg) rd.getInflation(*cfg, denom) rd.getGovInfo(*cfg) - rd.getValidatorsets(*cfg, blockHeight) - rd.getValidator(*cfg) - // TO-DO if consumer chain, use cosmoshub's ConsPubKey - valMap, found := rd.Validatorsets[rd.Validator.ConsPubKey.Key] - if len(rd.Validatorsets) != 0 && !found { - zap.L().Fatal("", zap.Bool("Success", false), zap.String("err", "Validator not found in the active set")) - } - + valInfo := rd.getValidatorsets(*cfg, blockHeight) rd.getBalances(*cfg) rd.getRewardsCommission(*cfg) - rd.getSigningInfo(*cfg, valMap[0]) + rd.getSigningInfo(*cfg, valInfo[0]) - consHexAddr := utils.Bech32AddrToHexAddr(valMap[0]) + consHexAddr := utils.Bech32AddrToHexAddr(valInfo[0]) rd.getCommit(blockData, consHexAddr) zap.L().Info("", zap.Bool("Success", true), zap.String("Moniker", rd.Validator.Description.Moniker)) - zap.L().Info("", zap.Bool("Success", true), zap.String("VP", valMap[1])) + zap.L().Info("", zap.Bool("Success", true), zap.String("VP", valInfo[1])) zap.L().Info("", zap.Bool("Success", true), zap.String("Precommit", fmt.Sprintf("%f", rd.Commit.ValidatorPrecommitStatus))) zap.L().Info("\t", zap.Bool("Success", true), zap.String("Balances", fmt.Sprint(rd.Balances))) zap.L().Info("\t", zap.Bool("Success", true), zap.String("Rewards", fmt.Sprint(rd.Rewards))) diff --git a/rest/validatorsets.go b/rest/validatorsets.go index e94b516..0c1c4a5 100644 --- a/rest/validatorsets.go +++ b/rest/validatorsets.go @@ -45,7 +45,7 @@ type consPubKeyValSet struct { Key string `json:"key"` } -func (rd *RESTData) getValidatorsets(cfg config.Config, currentBlockHeight int64) { +func (rd *RESTData) getValidatorsets(cfg config.Config, currentBlockHeight int64) []string { var vSetsResultFinal map[string][]string if cfg.IsLegacySDKVersion() { @@ -118,6 +118,22 @@ func (rd *RESTData) getValidatorsets(cfg config.Config, currentBlockHeight int64 zap.L().Debug("", zap.Bool("Success", true), zap.String(key, strings.Join(value, ", "))) } + if len(rd.Validatorsets) == 0 { + zap.L().Warn("", zap.Bool("Success", false), zap.String("err", "Validator set is empty")) + } + + rd.getValidator(cfg) + valInfo := rd.locateValidatorInActiveSet() + return valInfo +} + +// TO-DO if consumer chain, use cosmoshub's ConsPubKey +func (rd *RESTData) locateValidatorInActiveSet() []string { + valInfo, found := rd.Validatorsets[rd.Validator.ConsPubKey.Key] + if !found { + zap.L().Fatal("", zap.Bool("Success", false), zap.String("err", "Validator not found in the active set")) + } + return valInfo } func Sort(mapValue map[string][]string) map[string][]string {