Skip to content

Commit

Permalink
Add BandProtocol & Delete RPC
Browse files Browse the repository at this point in the history
  • Loading branch information
wlsaud619 committed Jun 16, 2020
1 parent e0df925 commit 2b92d29
Show file tree
Hide file tree
Showing 62 changed files with 1,748 additions and 803 deletions.
Binary file added chains/bandprotocol/exporter/.exporter.go.swp
Binary file not shown.
166 changes: 166 additions & 0 deletions chains/bandprotocol/exporter/exporter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
package exporter

import (
"fmt"
"time"
"go.uber.org/zap"
"strconv"

// rpc "github.com/node-a-team/Cosmos-IE/chains/bandprotocol/getData/rpc"
rest "github.com/node-a-team/Cosmos-IE/chains/bandprotocol/getData/rest"
metric "github.com/node-a-team/Cosmos-IE/chains/bandprotocol/exporter/metric"
utils "github.com/node-a-team/Cosmos-IE/utils"

"github.com/prometheus/client_golang/prometheus"
)

var (
previousBlockHeight int64

)

func Start(log *zap.Logger) {

gaugesNamespaceList := metric.GaugesNamespaceList

var gauges []prometheus.Gauge = make([]prometheus.Gauge, len(gaugesNamespaceList))
var gaugesDenom []prometheus.Gauge = make([]prometheus.Gauge, len(metric.DenomList)*3) // wallet, rewards, commission


// nomal guages
for i := 0; i < len(gaugesNamespaceList); i++ {
gauges[i] = utils.NewGauge("exporter", gaugesNamespaceList[i], "")
prometheus.MustRegister(gauges[i])
}


// denom gagues
count := 0
for i := 0; i < len(metric.DenomList)*3; i += 3 {
gaugesDenom[i] = utils.NewGauge("exporter_balances", metric.DenomList[count], "")
gaugesDenom[i+1] = utils.NewGauge("exporter_commission", metric.DenomList[count], "")
gaugesDenom[i+2] = utils.NewGauge("exporter_rewards", metric.DenomList[count], "")
prometheus.MustRegister(gaugesDenom[i])
prometheus.MustRegister(gaugesDenom[i+1])
prometheus.MustRegister(gaugesDenom[i+2])

count++
}


// labels
labels := []string{"chainId", "moniker", "operatorAddress", "accountAddress", "consHexAddress"}
gaugesForLabel := utils.NewCounterVec("exporter", "labels", "", labels)

prometheus.MustRegister(gaugesForLabel)


for {
func() {
defer func() {

if r := recover(); r != nil {
//Error Log
}

time.Sleep(500 * time.Millisecond)

}()

blockData := rest.GetBlocks(log)
currentBlockHeight, _:= strconv.ParseInt(blockData.Block.Header.Height, 10, 64)

if previousBlockHeight != currentBlockHeight {

fmt.Println("")
log.Info("RPC-Server", zap.Bool("Success", true), zap.String("err", "nil"), zap.String("Get Data", "Block Height: " +fmt.Sprint(currentBlockHeight)))


// restData, consHexAddr := rest.GetData(currentBlockHeight, log)
// rpcData := rpc.GetData(currentBlockHeight, consHexAddr, log)

restData := rest.GetData(currentBlockHeight, blockData, log)

// metric.SetMetric(currentBlockHeight, restData, rpcData, log)
metric.SetMetric(currentBlockHeight, restData, log)

metricData := metric.GetMetric()
denomList := metric.GetDenomList()

count := 0
for i := 0; i < len(denomList); i++ {

for _, value := range metricData.Validator.Account.Balances {
if value.Denom == denomList[i] {
gaugesDenom[count].Set(utils.StringToFloat64(value.Amount))
count++
}
}
for _, value := range metricData.Validator.Account.Commission {
if value.Denom == denomList[i] {
gaugesDenom[count].Set(utils.StringToFloat64(value.Amount))
count++
}
}
for _, value := range metricData.Validator.Account.Rewards {
if value.Denom == denomList[i] {
gaugesDenom[count].Set(utils.StringToFloat64(value.Amount))
count++
}
}
}


gaugesValue := [...]float64{
float64(metricData.Network.BlockHeight),

metricData.Network.Staking.NotBondedTokens,
metricData.Network.Staking.BondedTokens,
metricData.Network.Staking.TotalSupply,
metricData.Network.Staking.BondedRatio,

metricData.Network.Gov.TotalProposalCount,
metricData.Network.Gov.VotingProposalCount,

metricData.Validator.VotingPower,
metricData.Validator.MinSelfDelegation,
metricData.Validator.JailStatus,

metricData.Validator.Proposer.Ranking,
metricData.Validator.Proposer.Status,

metricData.Validator.Delegation.Shares,
metricData.Validator.Delegation.Ratio,
metricData.Validator.Delegation.DelegatorCount,
metricData.Validator.Delegation.Self,

metricData.Validator.Commission.Rate,
metricData.Validator.Commission.MaxRate,
metricData.Validator.Commission.MaxChangeRate,
metricData.Validator.Commit.VoteType,
metricData.Validator.Commit.PrecommitStatus,

metricData.Network.Minting.Inflation,
metricData.Network.Minting.ActualInflation,
}

for i := 0; i < len(gaugesNamespaceList); i++ {
gauges[i].Set(gaugesValue[i])
}


gaugesForLabel.WithLabelValues(metricData.Network.ChainID,
metricData.Validator.Moniker,
metricData.Validator.Address.Operator,
metricData.Validator.Address.Account,
metricData.Validator.Address.ConsensusHex,
).Add(0)

}

previousBlockHeight = currentBlockHeight
}()
}
}


183 changes: 183 additions & 0 deletions chains/bandprotocol/exporter/metric/metric.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
package metric

import (
"go.uber.org/zap"

rest "github.com/node-a-team/Cosmos-IE/chains/bandprotocol/getData/rest"
// rpc "github.com/node-a-team/Cosmos-IE/chains/bandprotocol/getData/rpc"
utils "github.com/node-a-team/Cosmos-IE/utils"
)

var (
metricData metric

DenomList = []string{"uband"}
GaugesNamespaceList = [...]string{"blockHeight",
"notBondedTokens",
"bondedTokens",
"totalSupply",
"bondedRatio",
"totalProposalCount",
"votingProposalCount",
"votingPower",
"minSelfDelegation",
"jailStatus",
"proposerRanking",
"proposerStatus",
"delegationShares",
"delegationRatio",
"delegatorCount",
"delegationSelf",
"commissionRate",
"commissionMaxRate",
"commissionMaxChangeRate",
"commitVoteType",
"precommitStatus",
"inflation",
"actualInflation",
}
)

type metric struct {

Network struct {
ChainID string
BlockHeight int64
PrecommitRate float64

Staking struct {
NotBondedTokens float64
BondedTokens float64
TotalSupply float64
BondedRatio float64
}

Minting struct {
Inflation float64
ActualInflation float64
}

Gov struct{
TotalProposalCount float64
VotingProposalCount float64
}
}

Validator struct {
Moniker string
VotingPower float64
MinSelfDelegation float64
JailStatus float64



Address struct {
Account string
Operator string
ConsensusHex string
}
Proposer struct {
Ranking float64
Status float64
}

Delegation struct {
Shares float64
Ratio float64
DelegatorCount float64
Self float64
}

Commission struct {
Rate float64
MaxRate float64
MaxChangeRate float64
}

Account struct {
Balances []rest.Coin
Commission []rest.Coin
Rewards []rest.Coin
}

Commit struct {
VoteType float64
PrecommitStatus float64
}

}
}



func SetMetric(currentBlock int64, restData *rest.RESTData, log *zap.Logger) {

operAddr := rest.OperAddr
consPubKey := restData.Validators.ConsPubKey
consAddr := restData.Validatorsets[consPubKey][0]

//// network
metricData.Network.ChainID = restData.Commit.ChainId
metricData.Network.BlockHeight = currentBlock

metricData.Network.Staking.NotBondedTokens = utils.StringToFloat64(restData.StakingPool.Result.Not_bonded_tokens)
metricData.Network.Staking.BondedTokens = utils.StringToFloat64(restData.StakingPool.Result.Bonded_tokens)
metricData.Network.Staking.TotalSupply = restData.StakingPool.Result.Total_supply
metricData.Network.Staking.BondedRatio = metricData.Network.Staking.BondedTokens / metricData.Network.Staking.TotalSupply

// minting
metricData.Network.Minting.Inflation = restData.Inflation
metricData.Network.Minting.ActualInflation = metricData.Network.Minting.Inflation / metricData.Network.Staking.BondedRatio

// gov
metricData.Network.Gov.TotalProposalCount = restData.Gov.TotalProposalCount
metricData.Network.Gov.VotingProposalCount = restData.Gov.VotingProposalCount


//// validator
metricData.Validator.Moniker = restData.Validators.Description.Moniker
metricData.Validator.VotingPower = utils.StringToFloat64(restData.Validatorsets[consPubKey][1])
metricData.Validator.MinSelfDelegation = utils.StringToFloat64(restData.Validators.MinSelfDelegation)
metricData.Validator.JailStatus = utils.BoolToFloat64(restData.Validators.Jailed)

// address
metricData.Validator.Address.Operator = operAddr
metricData.Validator.Address.Account = utils.GetAccAddrFromOperAddr(operAddr, log)
metricData.Validator.Address.ConsensusHex = utils.Bech32AddrToHexAddr(consAddr, log)

// proposer
metricData.Validator.Proposer.Ranking = utils.StringToFloat64(restData.Validatorsets[consPubKey][3])
metricData.Validator.Proposer.Status = restData.Commit.ValidatorProposingStatus

// delegation
metricData.Validator.Delegation.Shares = utils.StringToFloat64(restData.Validators.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

// commission
metricData.Validator.Commission.Rate = utils.StringToFloat64(restData.Validators.Commission.Commission_rates.Rate)
metricData.Validator.Commission.MaxRate = utils.StringToFloat64(restData.Validators.Commission.Commission_rates.Max_rate)
metricData.Validator.Commission.MaxChangeRate = utils.StringToFloat64(restData.Validators.Commission.Commission_rates.Max_change_rate)

// account
metricData.Validator.Account.Balances = restData.Balances
metricData.Validator.Account.Commission = restData.Commission
metricData.Validator.Account.Rewards = restData.Rewards

// commit
// metricData.Validator.Commit.VoteType = restData.Commit.VoteType
metricData.Validator.Commit.PrecommitStatus = restData.Commit.ValidatorPrecommitStatus



}

func GetMetric() *metric {

return &metricData
}

func GetDenomList() []string {
return DenomList
}
34 changes: 34 additions & 0 deletions chains/bandprotocol/getData/rest/balances.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package rest

import (
"strings"
"go.uber.org/zap"
"encoding/json"
)

type balances struct {
Height string `json:"height"`
Result []Coin
}

type Coin struct {
Denom string
Amount string
}

func getBalances(accAddr string, log *zap.Logger) []Coin {

var b balances

res, _ := runRESTCommand("/bank/balances/" +accAddr)
json.Unmarshal(res, &b)
// log
if strings.Contains(string(res), "not found") {
// handle error
log.Fatal("REST-Server", zap.Bool("Success", false), zap.String("err", string(res),))
} else {
log.Info("REST-Server", zap.Bool("Success", true), zap.String("err", "nil"), zap.String("Get Data", "Staking Pool"),)
}

return b.Result
}
Loading

0 comments on commit 2b92d29

Please sign in to comment.