From d9f07d3f36f1836ca7d722df4da23efcd1899186 Mon Sep 17 00:00:00 2001 From: Jay Jie Date: Sat, 16 Sep 2023 23:14:12 -0700 Subject: [PATCH] chore: fix proposal bug & refactor around config --- config/main.go | 18 +++++++++-------- exporter/main.go | 8 ++++---- main.go | 3 +-- rest/akash.go | 6 +++++- rest/gov.go | 13 ++++++------- rest/gravity.go | 31 ++++++++++++++++++++++++------ rest/node.go | 5 +++-- rest/rest.go | 50 ++++++++++++++++++++++++------------------------ rest/routes.go | 2 +- 9 files changed, 80 insertions(+), 56 deletions(-) diff --git a/config/main.go b/config/main.go index 2bc88a4..55f7895 100644 --- a/config/main.go +++ b/config/main.go @@ -61,10 +61,6 @@ func (config Config) CheckInputs(chainList map[string][]string) { } // TO-DO add more robust checks - if config.SDKVersion == "" { - log.Fatal("SDK version was not provided") - } - if config.OperatorAddr == "" { log.Fatal("Operator address was not provided") } @@ -166,10 +162,6 @@ func GetChainList() map[string][]string { return chainList } -func (config Config) GetSDKVersion() string { - return config.SDKVersion -} - func (config Config) IsLegacySDKVersion() bool { var legacy bool = false @@ -179,3 +171,13 @@ func (config Config) IsLegacySDKVersion() bool { return legacy } + +func (config Config) IsGravityBridgeEnabled() bool { + var enabled bool = false + + if config.Chain == "gravity" || config.Chain == "umee" { + enabled = true + } + + return enabled +} diff --git a/exporter/main.go b/exporter/main.go index 4277b0b..919fe0d 100644 --- a/exporter/main.go +++ b/exporter/main.go @@ -14,7 +14,7 @@ import ( "github.com/prometheus/client_golang/prometheus/promhttp" ) -func Start(config config.Config, port string, logger *zap.Logger) { +func Start(config *config.Config, port string, logger *zap.Logger) { http.Handle("/metrics", promhttp.Handler()) go Run(config, logger) @@ -26,7 +26,7 @@ func Start(config config.Config, port string, logger *zap.Logger) { } -func Run(cfg config.Config, log *zap.Logger) { +func Run(cfg *config.Config, log *zap.Logger) { cl := config.GetChainList() denomList := config.GetDenomList(cfg.Chain, cl) @@ -39,12 +39,12 @@ func Run(cfg config.Config, log *zap.Logger) { go func() { for { var block rest.Blocks - block.GetInfo(cfg) + block.GetInfo(*cfg) currentBlockHeight, _ := strconv.ParseInt(block.Block.Header.Height, 10, 64) if previousBlockHeight != currentBlockHeight { fmt.Println("--------------------------- Start ---------------------------") - block.GetLastBlockTimestamp(cfg, currentBlockHeight) + block.GetLastBlockTimestamp(*cfg, currentBlockHeight) zap.L().Info("\t", zap.Bool("Success", true), zap.String("Last block timestamp", block.Block.Header.LastTimestamp)) zap.L().Info("\t", zap.Bool("Success", true), zap.String("Current block timestamp", block.Block.Header.Timestamp)) zap.L().Info("\t", zap.Bool("Success", true), zap.String("Current block height", fmt.Sprint(currentBlockHeight))) diff --git a/main.go b/main.go index 6575288..6747360 100644 --- a/main.go +++ b/main.go @@ -42,7 +42,6 @@ func main() { cfg := config.Config{ Chain: os.Getenv("CHAIN"), - SDKVersion: os.Getenv("SDK_VERSION"), OperatorAddr: os.Getenv("OPERATOR_ADDR"), RestAddr: os.Getenv("REST_ADDR"), RpcAddr: os.Getenv("RPC_ADDR"), @@ -71,5 +70,5 @@ func main() { rest.RPCAddr = rpcAddr rest.OperAddr = operAddr - exporter.Start(cfg, listeningPort, logger) + exporter.Start(&cfg, listeningPort, logger) } diff --git a/rest/akash.go b/rest/akash.go index 2c18070..e88fa8f 100644 --- a/rest/akash.go +++ b/rest/akash.go @@ -4,6 +4,7 @@ import ( "encoding/json" "strconv" + "github.com/jim380/Cendermint/config" "go.uber.org/zap" ) @@ -107,7 +108,10 @@ type GroupSpec struct { } `json:"resources"` } -func (rd *RESTData) getAkashDeployments() { +func (rd *RESTData) getAkashDeployments(cfg config.Config) { + if cfg.Chain != "akash" { + return + } var deployments, activeDeployments akashDeployments route := getDeploymentsRoute() diff --git a/rest/gov.go b/rest/gov.go index 9441829..0f474e5 100644 --- a/rest/gov.go +++ b/rest/gov.go @@ -40,14 +40,13 @@ func (rd *RESTData) getGovInfo(cfg config.Config) { var ( g gov gi govInfo - totalProposals []string proposalsInVoting []string inVotingVoted int inVotingDidNotVote int ) route := getProposalsRoute(cfg) - res, err := HttpQuery(RESTAddr + route + "?pagination.limit=1000") + res, err := HttpQuery(RESTAddr + route + "?pagination.limit=2000") if err != nil { zap.L().Fatal("", zap.Bool("Success", false), zap.String("err", err.Error())) } @@ -58,13 +57,14 @@ func (rd *RESTData) getGovInfo(cfg config.Config) { zap.L().Fatal("", zap.Bool("Success", false), zap.String("err", string(res))) } - for _, value := range g.Proposals { - totalProposals = append(totalProposals, value.ProposalID) + totalProposals := g.Proposals + for _, value := range totalProposals { if value.Status == "PROPOSAL_STATUS_VOTING_PERIOD" { proposalsInVoting = append(proposalsInVoting, value.ProposalID) } } - zap.L().Info("\t", zap.Bool("Success", true), zap.String("Total proposals count", totalProposals[len(totalProposals)-1])) + + zap.L().Info("\t", zap.Bool("Success", true), zap.String("Total proposals count", strconv.Itoa(len(totalProposals)))) zap.L().Info("\t", zap.Bool("Success", true), zap.String("Proposals in voting", strconv.Itoa(len(proposalsInVoting)))) for _, value := range proposalsInVoting { @@ -80,8 +80,7 @@ func (rd *RESTData) getGovInfo(cfg config.Config) { inVotingDidNotVote++ } } - totalProposalsCount, _ := strconv.ParseFloat(totalProposals[len(totalProposals)-1], 64) - gi.TotalProposalCount = totalProposalsCount + gi.TotalProposalCount = float64(len(totalProposals)) gi.VotingProposalCount = float64(len(proposalsInVoting)) gi.InVotingVotedCount = float64(inVotingVoted) gi.InVotingDidNotVoteCount = float64(inVotingDidNotVote) diff --git a/rest/gravity.go b/rest/gravity.go index abee70f..80e557d 100644 --- a/rest/gravity.go +++ b/rest/gravity.go @@ -4,6 +4,7 @@ import ( "encoding/json" "os" + "github.com/jim380/Cendermint/config" utils "github.com/jim380/Cendermint/utils" "go.uber.org/zap" ) @@ -132,7 +133,10 @@ func (rd *RESTData) getUmeePrice() { rd.GravityInfo.UMEEPrice = p.UMEEPrice } -func (rd *RESTData) getBatchFees() { +func (rd *RESTData) getBatchFees(cfg config.Config) { + if !cfg.IsGravityBridgeEnabled() { + return + } var b batchFees route := getBatchFeesRoute() @@ -151,7 +155,10 @@ func (rd *RESTData) getBatchFees() { rd.GravityInfo.BatchFees = feesTotal } -func (rd *RESTData) getBatchesFees() { +func (rd *RESTData) getBatchesFees(cfg config.Config) { + if !cfg.IsGravityBridgeEnabled() { + return + } var b batches route := getBatchesFeesRoute() @@ -172,7 +179,10 @@ func (rd *RESTData) getBatchesFees() { rd.GravityInfo.BatchesFees = feesTotal } -func (rd *RESTData) getBridgeFees() { +func (rd *RESTData) getBridgeFees(cfg config.Config) { + if !cfg.IsGravityBridgeEnabled() { + return + } var p ethPrice var bf float64 @@ -189,7 +199,10 @@ func (rd *RESTData) getBridgeFees() { rd.GravityInfo.BridgeFees = bf } -func (rd *RESTData) getBridgeParams() { +func (rd *RESTData) getBridgeParams(cfg config.Config) { + if !cfg.IsGravityBridgeEnabled() { + return + } var params gravityParams rd.GravityInfo.GravityActive = 0.0 @@ -209,7 +222,10 @@ func (rd *RESTData) getBridgeParams() { } } -func (rd *RESTData) getOracleEventNonce() { +func (rd *RESTData) getOracleEventNonce(cfg config.Config) { + if !cfg.IsGravityBridgeEnabled() { + return + } var evt oracleEventNonce orchAddr := os.Getenv("UMEE_ORCH_ADDR") @@ -223,7 +239,10 @@ func (rd *RESTData) getOracleEventNonce() { rd.GravityInfo.EventNonce = evt.EventNonce } -func (rd *RESTData) getValSet() { +func (rd *RESTData) getValSet(cfg config.Config) { + if !cfg.IsGravityBridgeEnabled() { + return + } var vs valSetInfo var vsResult map[string]string = make(map[string]string) diff --git a/rest/node.go b/rest/node.go index c73f4b0..25dedf9 100644 --- a/rest/node.go +++ b/rest/node.go @@ -28,10 +28,10 @@ type appVersion struct { SDKVersion string `json:"cosmos_sdk_version"` } -func (rd *RESTData) getNodeInfo(cfg config.Config) { +func (rd *RESTData) getNodeInfo(cfg *config.Config) { var nodeInfo nodeInfo - route := getNodeInfoRoute(cfg) + route := getNodeInfoRoute() res, err := HttpQuery(RESTAddr + route) if err != nil { zap.L().Fatal("", zap.Bool("Success", false), zap.String("err", err.Error())) @@ -47,4 +47,5 @@ func (rd *RESTData) getNodeInfo(cfg config.Config) { zap.L().Info("", zap.Bool("Success", true), zap.String("SDK version", nodeInfo.Application.SDKVersion)) rd.NodeInfo = nodeInfo + cfg.SDKVersion = nodeInfo.Application.SDKVersion } diff --git a/rest/rest.go b/rest/rest.go index bfd4d1b..81a350e 100644 --- a/rest/rest.go +++ b/rest/rest.go @@ -57,7 +57,7 @@ func (rpc RPCData) new() *RPCData { return &RPCData{Validatorsets: make(map[string][]string)} } -func GetData(cfg config.Config, blockHeight int64, blockData Blocks, denom string) *RESTData { +func GetData(cfg *config.Config, blockHeight int64, blockData Blocks, denom string) *RESTData { // rpc var rpcData RPCData rpc := rpcData.new() @@ -67,26 +67,27 @@ func GetData(cfg config.Config, blockHeight int64, blockData Blocks, denom strin AccAddr = utils.GetAccAddrFromOperAddr(OperAddr) rd := restData.new(blockHeight) + rd.getNodeInfo(cfg) // get node info first + wg := sync.WaitGroup{} wg.Add(1) go func() { - rpc.getConsensusDump(cfg) - rd.getStakingPool(cfg, denom) - rd.getSlashingParams(cfg) - rd.getInflation(cfg, denom) - rd.getGovInfo(cfg) - // TO-DO fix finding validator in active set for sdk version >= v0.46 - rd.getValidatorsets(cfg, blockHeight) - rd.getValidator(cfg) - + rpc.getConsensusDump(*cfg) + rd.getStakingPool(*cfg, denom) + 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 !found { zap.L().Fatal("", zap.Bool("Success", false), zap.String("err", "Validator not found in the active set")) } - rd.getBalances(cfg) - rd.getRewardsCommission(cfg) - rd.getSigningInfo(cfg, valMap[0]) + rd.getBalances(*cfg) + rd.getRewardsCommission(*cfg) + rd.getSigningInfo(*cfg, valMap[0]) consHexAddr := utils.Bech32AddrToHexAddr(valMap[0]) rd.getCommit(blockData, consHexAddr) @@ -96,20 +97,19 @@ func GetData(cfg config.Config, blockHeight int64, blockData Blocks, denom strin 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))) zap.L().Info("\t", zap.Bool("Success", true), zap.String("Commission", fmt.Sprint(rd.Commission))) - rd.getIBCChannels(cfg) - rd.getIBCConnections(cfg) - rd.getNodeInfo(cfg) - rd.getTxInfo(cfg, blockHeight) + rd.getIBCChannels(*cfg) + rd.getIBCConnections(*cfg) + rd.getTxInfo(*cfg, blockHeight) rd.computerTPS(blockData) - rd.getUpgradeInfo(cfg) - rd.getAkashDeployments() + rd.getUpgradeInfo(*cfg) + rd.getAkashDeployments(*cfg) // gravity - rd.getBridgeParams() - rd.getValSet() - rd.getOracleEventNonce() - rd.getBatchFees() - rd.getBatchesFees() - rd.getBridgeFees() + rd.getBridgeParams(*cfg) + rd.getValSet(*cfg) + rd.getOracleEventNonce(*cfg) + rd.getBatchFees(*cfg) + rd.getBatchesFees(*cfg) + rd.getBridgeFees(*cfg) wg.Done() }() wg.Wait() diff --git a/rest/routes.go b/rest/routes.go index da7caf9..449663f 100644 --- a/rest/routes.go +++ b/rest/routes.go @@ -86,7 +86,7 @@ func getProposalsRoute(cfg config.Config) string { return "/cosmos/gov/v1beta1/proposals" } -func getNodeInfoRoute(cfg config.Config) string { +func getNodeInfoRoute() string { return "/cosmos/base/tendermint/v1beta1/node_info" }