Skip to content

Commit

Permalink
chore: consolidate rest routes into one file; add sdk versioning
Browse files Browse the repository at this point in the history
  • Loading branch information
jim380 committed Sep 17, 2023
1 parent bc8f0d7 commit 8c282c5
Show file tree
Hide file tree
Showing 24 changed files with 414 additions and 147 deletions.
22 changes: 21 additions & 1 deletion config/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
import (
"fmt"
"log"
"strings"

sdktypes "github.com/cosmos/cosmos-sdk/types"
"github.com/jim380/Cendermint/utils"
Expand All @@ -12,6 +13,7 @@ import (

type Config struct {
Chain string
SDKVersion string
OperatorAddr string
RestAddr string
RpcAddr string
Expand Down Expand Up @@ -58,7 +60,11 @@ func (config Config) CheckInputs(chainList map[string][]string) {
}
}

// TODO add more robust checks
// 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")
}
Expand Down Expand Up @@ -159,3 +165,17 @@ func GetChainList() map[string][]string {

return chainList
}

func (config Config) GetSDKVersion() string {
return config.SDKVersion
}

func (config Config) IsLegacySDKVersion() bool {
var legacy bool = false

if strings.Contains(config.SDKVersion, "0.45") {
legacy = true
}

return legacy
}
17 changes: 8 additions & 9 deletions exporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import (
"github.com/prometheus/client_golang/prometheus/promhttp"
)

func Start(chain string, port string, logger *zap.Logger) {
func Start(config config.Config, port string, logger *zap.Logger) {
http.Handle("/metrics", promhttp.Handler())
go Run(chain, logger)
go Run(config, logger)

err := http.ListenAndServe(":"+port, nil)
if err != nil {
Expand All @@ -26,38 +26,37 @@ func Start(chain string, port string, logger *zap.Logger) {

}

func Run(chain string, log *zap.Logger) {
func Run(cfg config.Config, log *zap.Logger) {
cl := config.GetChainList()
denomList := config.GetDenomList(chain, cl)
denomList := config.GetDenomList(cfg.Chain, cl)

registerGauges(denomList)
counterVecs := registerLabels()

pollInterval, _ := strconv.Atoi(os.Getenv("POLL_INTERVAL"))
ticker := time.NewTicker(1 * time.Second).C
// ticker2 := time.NewTicker(40 * time.Second).C

go func() {
for {
var block rest.Blocks
block.GetInfo()
block.GetInfo(cfg)

currentBlockHeight, _ := strconv.ParseInt(block.Block.Header.Height, 10, 64)
if previousBlockHeight != currentBlockHeight {
fmt.Println("--------------------------- Start ---------------------------")
block.GetLastBlockTimestamp(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)))
select {
case <-ticker:
// fetch info from REST
restData := rest.GetData(chain, currentBlockHeight, block, denomList[0])
restData := rest.GetData(cfg, currentBlockHeight, block, denomList[0])
SetMetric(currentBlockHeight, restData, log)
// case <-ticker2:
// takes ~5-6 blocks to return results per request
// tends to halt the node too. Caution !!!
// restData := rest.GetDelegationsData(chain, currentBlockHeight, block, denomList[0])
// restData := rest.GetDelegationsData(cfg, chain, currentBlockHeight, block, denomList[0])
// SetMetric(currentBlockHeight, restData, log)
}

Expand Down
16 changes: 8 additions & 8 deletions exporter/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
// gauges/labels <-> value
func SetMetric(currentBlock int64, restData *rest.RESTData, log *zap.Logger) {
operAddr := rest.OperAddr
consPubKey := restData.Validators.ConsPubKey
consPubKey := restData.Validator.ConsPubKey
consAddr := restData.Validatorsets[consPubKey.Key][0]

// chain
Expand Down Expand Up @@ -51,17 +51,17 @@ func SetMetric(currentBlock int64, restData *rest.RESTData, log *zap.Logger) {

// validator info
metricData.Validator.VotingPower = utils.StringToFloat64(restData.Validatorsets[consPubKey.Key][1])
metricData.Validator.JailStatus = utils.BoolToFloat64(restData.Validators.Jailed)
metricData.Validator.MinSelfDelegation = utils.StringToFloat64(restData.Validators.MinSelfDelegation)
metricData.Validator.JailStatus = utils.BoolToFloat64(restData.Validator.Jailed)
metricData.Validator.MinSelfDelegation = utils.StringToFloat64(restData.Validator.MinSelfDelegation)
// validator delegation
metricData.Validator.Delegation.Shares = utils.StringToFloat64(restData.Validators.DelegatorShares)
metricData.Validator.Delegation.Shares = utils.StringToFloat64(restData.Validator.DelegatorShares)
metricData.Validator.Delegation.Ratio = metricData.Validator.Delegation.Shares / metricData.Network.Staking.BondedTokens
// metricData.Validator.Delegation.DelegatorCount = restData.Delegations.DelegationCount
// metricData.Validator.Delegation.Self = restData.Delegations.SelfDelegation
// validator commission
metricData.Validator.Commission.Rate = utils.StringToFloat64(restData.Validators.Commission.Commission.Rate)
metricData.Validator.Commission.MaxRate = utils.StringToFloat64(restData.Validators.Commission.Commission.Max_rate)
metricData.Validator.Commission.MaxChangeRate = utils.StringToFloat64(restData.Validators.Commission.Commission.Max_change_rate)
metricData.Validator.Commission.Rate = utils.StringToFloat64(restData.Validator.Commission.Commission.Rate)
metricData.Validator.Commission.MaxRate = utils.StringToFloat64(restData.Validator.Commission.Commission.Max_rate)
metricData.Validator.Commission.MaxChangeRate = utils.StringToFloat64(restData.Validator.Commission.Commission.Max_change_rate)
// validator signing
metricData.Validator.Commit.PrecommitStatus = restData.Commit.ValidatorPrecommitStatus
metricData.Validator.Proposer.Status = restData.Commit.ValidatorProposingStatus
Expand Down Expand Up @@ -129,7 +129,7 @@ func SetMetric(currentBlock int64, restData *rest.RESTData, log *zap.Logger) {

// labels node
metricData.Network.ChainID = restData.Commit.ChainId
metricData.Validator.Moniker = restData.Validators.Description.Moniker
metricData.Validator.Moniker = restData.Validator.Description.Moniker
metricData.Network.NodeInfo.Moniker = restData.NodeInfo.Default.Moniker
metricData.Network.NodeInfo.NodeID = restData.NodeInfo.Default.NodeID
metricData.Network.NodeInfo.TMVersion = restData.NodeInfo.Default.TMVersion
Expand Down
5 changes: 3 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
Expand Down Expand Up @@ -42,6 +42,7 @@ 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"),
Expand Down Expand Up @@ -70,5 +71,5 @@ func main() {
rest.RPCAddr = rpcAddr
rest.OperAddr = operAddr

exporter.Start(chain, listeningPort, logger)
exporter.Start(cfg, listeningPort, logger)
}
9 changes: 3 additions & 6 deletions rest/akash.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,18 +108,15 @@ type GroupSpec struct {
}

func (rd *RESTData) getAkashDeployments() {
// var deployments akashDeployments
var deployments, activeDeployments akashDeployments

// ?filters.state=active
res, err := HttpQuery(RESTAddr + "/akash/deployment/v1beta2/deployments/list")
route := getDeploymentsRoute()
res, err := HttpQuery(RESTAddr + route)
if err != nil {
zap.L().Fatal("", zap.Bool("Success", false), zap.String("err", err.Error()))
}
json.Unmarshal(res, &deployments)

// rd.AkashInfo.Deployments = deployments

// get total deployments count
totalDeploymentsCount, err := strconv.Atoi(deployments.Pagination.Total)
if err != nil {
Expand All @@ -128,7 +125,7 @@ func (rd *RESTData) getAkashDeployments() {
rd.AkashInfo.TotalDeployments = totalDeploymentsCount

// get active deployments count
resActive, err := HttpQuery(RESTAddr + "/akash/deployment/v1beta2/deployments/list?filters.state=active")
resActive, err := HttpQuery(RESTAddr + route + "?filters.state=active")
if err != nil {
zap.L().Fatal("", zap.Bool("Success", false), zap.String("err", err.Error()))
}
Expand Down
7 changes: 5 additions & 2 deletions rest/balances.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"strings"

"github.com/jim380/Cendermint/config"
"go.uber.org/zap"
)

Expand All @@ -16,10 +17,12 @@ type Coin struct {
Amount string
}

func (rd *RESTData) getBalances() {
func (rd *RESTData) getBalances(cfg config.Config) {
var b balances

res, err := HttpQuery(RESTAddr + "/cosmos/bank/v1beta1/balances/" + AccAddr)
route := getBalancesByAddressRoute(cfg)

res, err := HttpQuery(RESTAddr + route + AccAddr)
if err != nil {
zap.L().Fatal("", zap.Bool("Success", false), zap.String("err", err.Error()))
}
Expand Down
11 changes: 7 additions & 4 deletions rest/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strconv"
"strings"

"github.com/jim380/Cendermint/config"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -39,8 +40,9 @@ type lastCommit struct {
}
}

func (b *Blocks) GetInfo() Blocks {
res, err := HttpQuery(RESTAddr + "/blocks/latest")
func (b *Blocks) GetInfo(cfg config.Config) Blocks {
route := getBlockInfoRoute(cfg)
res, err := HttpQuery(RESTAddr + route)
if err != nil {
zap.L().Fatal("Connection to REST failed", zap.Bool("Success", false), zap.String("err", err.Error()))
}
Expand All @@ -54,9 +56,10 @@ func (b *Blocks) GetInfo() Blocks {
return *b
}

func (b *Blocks) GetLastBlockTimestamp(currentHeight int64) Blocks {
func (b *Blocks) GetLastBlockTimestamp(cfg config.Config, currentHeight int64) Blocks {
var lastBlock LastBlock
res, err := HttpQuery(RESTAddr + "/blocks/" + strconv.Itoa(int(currentHeight-1)))
route := getBlockByHeightRoute(cfg)
res, err := HttpQuery(RESTAddr + route + strconv.Itoa(int(currentHeight-1)))
if err != nil {
zap.L().Fatal("Connection to REST failed", zap.Bool("Success", false), zap.String("err", err.Error()))
}
Expand Down
10 changes: 6 additions & 4 deletions rest/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strconv"
"strings"

"github.com/jim380/Cendermint/config"
"github.com/jim380/Cendermint/utils"
"go.uber.org/zap"
)
Expand Down Expand Up @@ -56,7 +57,7 @@ type rpcValidators struct {
} `json:"validators"`
}

func (rpc *RPCData) getConsensusDump() {
func (rpc *RPCData) getConsensusDump(cfg config.Config) {
var cs ConsensusState
var vSetsResult map[string][]string = make(map[string][]string)

Expand All @@ -66,7 +67,7 @@ func (rpc *RPCData) getConsensusDump() {
}
json.Unmarshal(res, &cs)

conspubMonikerMap := rpc.getConspubMonikerMap()
conspubMonikerMap := rpc.getConspubMonikerMap(cfg)
// cs.Result.Validatorset.Validators is already sorted based on voting power
for index, validator := range cs.Result.Validatorset.Validators {
var prevote, precommit string
Expand Down Expand Up @@ -101,11 +102,12 @@ func (rpc *RPCData) getConsensusDump() {
zap.L().Info("", zap.Bool("Success", true), zap.String("# of validators from RPC", fmt.Sprint(len(rpc.Validatorsets))))
}

func (rpc *RPCData) getConspubMonikerMap() map[string]string {
func (rpc *RPCData) getConspubMonikerMap(cfg config.Config) map[string]string {
var v rpcValidators
var vResult map[string]string = make(map[string]string)

res, err := HttpQuery(RESTAddr + "/cosmos/staking/v1beta1/validators?status=BOND_STATUS_BONDED&pagination.limit=300")
route := getValidatorsRoute(cfg)
res, err := HttpQuery(RESTAddr + route + "?status=BOND_STATUS_BONDED&pagination.limit=300")
if err != nil {
zap.L().Fatal("", zap.Bool("Success", false), zap.String("err", err.Error()))
}
Expand Down
6 changes: 4 additions & 2 deletions rest/delegations.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"strings"

"github.com/jim380/Cendermint/config"
"go.uber.org/zap"
)

Expand All @@ -28,11 +29,12 @@ type delegationRes []struct {
}
}

func (rd *RESTData) getDelegations() {
func (rd *RESTData) getDelegations(cfg config.Config) {
var delInfo delegationsInfo
var delRes map[string][]string = make(map[string][]string)

res, err := HttpQuery(RESTAddr + "/cosmos/staking/v1beta1/validators/" + OperAddr + "/delegations" + "?pagination.limit=1000")
route := getValidatorByAddressRoute(cfg)
res, err := HttpQuery(RESTAddr + route + OperAddr + "/delegations" + "?pagination.limit=1000")
if err != nil {
zap.L().Fatal("", zap.Bool("Success", false), zap.String("err", err.Error()))
}
Expand Down
10 changes: 5 additions & 5 deletions rest/gov.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"go.uber.org/zap"

"github.com/jim380/Cendermint/config"
utils "github.com/jim380/Cendermint/utils"
)

Expand Down Expand Up @@ -35,7 +36,7 @@ type voteInfo struct {
} `json:"vote"`
}

func (rd *RESTData) getGovInfo() {
func (rd *RESTData) getGovInfo(cfg config.Config) {
var (
g gov
gi govInfo
Expand All @@ -45,7 +46,8 @@ func (rd *RESTData) getGovInfo() {
inVotingDidNotVote int
)

res, err := HttpQuery(RESTAddr + "/cosmos/gov/v1beta1/proposals?pagination.limit=1000")
route := getProposalsRoute(cfg)
res, err := HttpQuery(RESTAddr + route + "?pagination.limit=1000")
if err != nil {
zap.L().Fatal("", zap.Bool("Success", false), zap.String("err", err.Error()))
}
Expand All @@ -67,17 +69,15 @@ func (rd *RESTData) getGovInfo() {

for _, value := range proposalsInVoting {
var voteInfo voteInfo
res, err := HttpQuery(RESTAddr + "/cosmos/gov/v1beta1/proposals/" + value + "/votes/" + utils.GetAccAddrFromOperAddr(OperAddr))
res, err := HttpQuery(RESTAddr + route + value + "/votes/" + utils.GetAccAddrFromOperAddr(OperAddr))
if err != nil {
zap.L().Fatal("", zap.Bool("Success", false), zap.String("err", err.Error()))
}
json.Unmarshal(res, &voteInfo)
if voteInfo.Votes.Option != "" {
inVotingVoted++
//fmt.Println(value + ":Voter voted")
} else {
inVotingDidNotVote++
//fmt.Println(value + ":Voter didn't vote")
}
}
totalProposalsCount, _ := strconv.ParseFloat(totalProposals[len(totalProposals)-1], 64)
Expand Down
Loading

0 comments on commit 8c282c5

Please sign in to comment.