Skip to content

Commit

Permalink
chore: use math.LegacyDec for validator fields
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno committed Jun 22, 2024
1 parent 9c3b1e6 commit be22cf3
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 17 deletions.
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ require (
cloud.google.com/go/iam v1.1.6 // indirect
cloud.google.com/go/storage v1.36.0 // indirect
cosmossdk.io/errors v1.0.0-beta.7 // indirect
cosmossdk.io/math v1.0.0-beta.2 // indirect
cosmossdk.io/math v1.3.0 // indirect
filippo.io/edwards25519 v1.0.0-rc.1 // indirect
github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 // indirect
github.com/99designs/keyring v1.2.1 // indirect
Expand Down Expand Up @@ -140,6 +140,7 @@ require (
go.opentelemetry.io/otel/metric v1.25.0 // indirect
go.opentelemetry.io/proto/otlp v1.1.0 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/exp v0.0.0-20221205204356-47842c84f3db // indirect
golang.org/x/net v0.23.0 // indirect
golang.org/x/oauth2 v0.17.0 // indirect
golang.org/x/sync v0.6.0 // indirect
Expand All @@ -157,7 +158,7 @@ require (
gopkg.in/ini.v1 v1.66.6 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
)

replace github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ cosmossdk.io/errors v1.0.0-beta.7 h1:gypHW76pTQGVnHKo6QBkb4yFOJjC+sUGRc5Al3Odj1w
cosmossdk.io/errors v1.0.0-beta.7/go.mod h1:mz6FQMJRku4bY7aqS/Gwfcmr/ue91roMEKAmDUDpBfE=
cosmossdk.io/math v1.0.0-beta.2 h1:17hSVc9ne1c31IaLDfjRojtN+y4Rd2N8H/6Fht2sBzw=
cosmossdk.io/math v1.0.0-beta.2/go.mod h1:u/MXvf8wbUbCsAEyQSSYXXMsczAsFX48e2D6JI86T4o=
cosmossdk.io/math v1.3.0 h1:RC+jryuKeytIiictDslBP9i1fhkVm6ZDmZEoNP316zE=
cosmossdk.io/math v1.3.0/go.mod h1:vnRTxewy+M7BtXBNFybkuhSH4WfedVAAnERHgVFhp3k=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
filippo.io/edwards25519 v1.0.0-rc.1 h1:m0VOOB23frXZvAOK44usCgLWvtsxIoMCTBGJZlpmGfU=
filippo.io/edwards25519 v1.0.0-rc.1/go.mod h1:N1IkdkCkiLB6tki+MYJoSx2JTY9NUlxZE7eHn5EwJns=
Expand Down Expand Up @@ -493,6 +495,7 @@ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/gofuzz v0.0.0-20170612174753-24818f796faf/go.mod h1:HP5RmnzzSNb993RKQDq4+1A4ia9nllfqcQFTQJedwGI=
Expand Down Expand Up @@ -1280,6 +1283,8 @@ golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u0
golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM=
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
golang.org/x/exp v0.0.0-20200331195152-e8c3332aa8e5/go.mod h1:4M0jN8W1tt0AVLNr8HDosyJCDCDuyL9N9+3m7wDWgKw=
golang.org/x/exp v0.0.0-20221205204356-47842c84f3db h1:D/cFflL63o2KSLJIwjlcIt8PR064j/xsmdEJL/YvY/o=
golang.org/x/exp v0.0.0-20221205204356-47842c84f3db/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/exp/typeparams v0.0.0-20220218215828-6cf2b201936e/go.mod h1:AbB0pIl9nAr9wVwH+Z2ZpaocVmF5I4GyWCDIsVjR0bk=
golang.org/x/image v0.0.0-20180708004352-c73c2afc3b81/go.mod h1:ux5Hcp/YLpHSI86hEcLt0YII63i6oz57MZXIpbrjZUs=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
Expand Down Expand Up @@ -1953,3 +1958,4 @@ sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o=
sigs.k8s.io/yaml v1.2.0/go.mod h1:yfXDCHCao9+ENCvLSE62v9VSji2MKu5jeNfTrofGhJc=
sigs.k8s.io/yaml v1.3.0 h1:a2VclLzOGrwOHDiV8EfBGhvjHvP46CtW5j6POvhYGGo=
sigs.k8s.io/yaml v1.3.0/go.mod h1:GeOyir5tyXNByN85N/dRIT9es5UQNerPYEKK56eTBm8=
sigs.k8s.io/yaml v1.4.0/go.mod h1:Ejl7/uTz7PSA4eKMyQCUTnhZYNmLIl+5c2lQPGR2BPY=
4 changes: 2 additions & 2 deletions pkg/generators/active_set_tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ func (g *ActiveSetTokensGenerator) Generate(state *statePkg.State) []prometheus.
})

sort.Slice(activeValidators, func(i, j int) bool {
return utils.StrToFloat64(activeValidators[i].DelegatorShares) > utils.StrToFloat64(activeValidators[j].DelegatorShares)
return activeValidators[i].DelegatorShares.GT(activeValidators[j].DelegatorShares)

Check warning on line 61 in pkg/generators/active_set_tokens.go

View check run for this annotation

Codecov / codecov/patch

pkg/generators/active_set_tokens.go#L61

Added line #L61 was not covered by tests
})

lastValidatorStake := utils.StrToFloat64(activeValidators[len(activeValidators)-1].DelegatorShares)
lastValidatorStake := activeValidators[len(activeValidators)-1].DelegatorShares.MustFloat64()

Check warning on line 64 in pkg/generators/active_set_tokens.go

View check run for this annotation

Codecov / codecov/patch

pkg/generators/active_set_tokens.go#L64

Added line #L64 was not covered by tests

if chainStakingParams != nil && len(activeValidators) >= chainStakingParams.StakingParams.MaxValidators {
activeSetTokensGauge.With(prometheus.Labels{
Expand Down
8 changes: 4 additions & 4 deletions pkg/generators/single_validator_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,22 +157,22 @@ func (g *SingleValidatorInfoGenerator) Generate(state *statePkg.State) []prometh
commissionGauge.With(prometheus.Labels{
"chain": chain.Name,
"address": validatorAddr.Address,
}).Set(utils.StrToFloat64(validator.Commission.CommissionRates.Rate))
}).Set(validator.Commission.CommissionRates.Rate.MustFloat64())

Check warning on line 160 in pkg/generators/single_validator_info.go

View check run for this annotation

Codecov / codecov/patch

pkg/generators/single_validator_info.go#L160

Added line #L160 was not covered by tests

commissionMaxGauge.With(prometheus.Labels{
"chain": chain.Name,
"address": validatorAddr.Address,
}).Set(utils.StrToFloat64(validator.Commission.CommissionRates.MaxRate))
}).Set(validator.Commission.CommissionRates.MaxRate.MustFloat64())

Check warning on line 165 in pkg/generators/single_validator_info.go

View check run for this annotation

Codecov / codecov/patch

pkg/generators/single_validator_info.go#L165

Added line #L165 was not covered by tests

commissionMaxChangeGauge.With(prometheus.Labels{
"chain": chain.Name,
"address": validatorAddr.Address,
}).Set(utils.StrToFloat64(validator.Commission.CommissionRates.MaxChangeRate))
}).Set(validator.Commission.CommissionRates.MaxChangeRate.MustFloat64())

Check warning on line 170 in pkg/generators/single_validator_info.go

View check run for this annotation

Codecov / codecov/patch

pkg/generators/single_validator_info.go#L170

Added line #L170 was not covered by tests

delegationsGauge.With(prometheus.Labels{
"chain": chain.Name,
"address": validatorAddr.Address,
}).Set(utils.StrToFloat64(validator.DelegatorShares))
}).Set(validator.DelegatorShares.MustFloat64())

Check warning on line 175 in pkg/generators/single_validator_info.go

View check run for this annotation

Codecov / codecov/patch

pkg/generators/single_validator_info.go#L175

Added line #L175 was not covered by tests
}
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/generators/validator_rank.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (g *ValidatorRankGenerator) Generate(state *statePkg.State) []prometheus.Co
})

sort.Slice(activeValidators, func(i, j int) bool {
return utils.StrToFloat64(activeValidators[i].DelegatorShares) > utils.StrToFloat64(activeValidators[j].DelegatorShares)
return activeValidators[i].DelegatorShares.GT(activeValidators[j].DelegatorShares)

Check warning on line 85 in pkg/generators/validator_rank.go

View check run for this annotation

Codecov / codecov/patch

pkg/generators/validator_rank.go#L85

Added line #L85 was not covered by tests
})

rank, found := utils.FindIndex(activeValidators, func(v types.Validator) bool {
Expand Down
9 changes: 5 additions & 4 deletions pkg/generators/validators_info.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package generators

import (
"cosmossdk.io/math"

Check failure on line 4 in pkg/generators/validators_info.go

View workflow job for this annotation

GitHub Actions / lint

File is not `goimports`-ed (goimports)
"main/pkg/constants"
fetchersPkg "main/pkg/fetchers"
statePkg "main/pkg/state"
Expand Down Expand Up @@ -54,13 +55,13 @@ func (g *ValidatorsInfoGenerator) Generate(state *statePkg.State) []prometheus.C
})

sort.Slice(activeValidators, func(i, j int) bool {
return utils.StrToFloat64(activeValidators[i].DelegatorShares) > utils.StrToFloat64(activeValidators[j].DelegatorShares)
return activeValidators[i].DelegatorShares.GT(activeValidators[j].DelegatorShares)

Check warning on line 58 in pkg/generators/validators_info.go

View check run for this annotation

Codecov / codecov/patch

pkg/generators/validators_info.go#L58

Added line #L58 was not covered by tests
})

var totalStake float64 = 0
totalStake := math.LegacyNewDec(0)

Check warning on line 61 in pkg/generators/validators_info.go

View check run for this annotation

Codecov / codecov/patch

pkg/generators/validators_info.go#L61

Added line #L61 was not covered by tests

for _, activeValidator := range activeValidators {
totalStake += utils.StrToFloat64(activeValidator.DelegatorShares)
totalStake = totalStake.Add(activeValidator.DelegatorShares)

Check warning on line 64 in pkg/generators/validators_info.go

View check run for this annotation

Codecov / codecov/patch

pkg/generators/validators_info.go#L64

Added line #L64 was not covered by tests
}

validatorsCountGauge.With(prometheus.Labels{
Expand All @@ -69,7 +70,7 @@ func (g *ValidatorsInfoGenerator) Generate(state *statePkg.State) []prometheus.C

totalBondedTokensGauge.With(prometheus.Labels{
"chain": chain,
}).Set(totalStake)
}).Set(totalStake.MustFloat64())

Check warning on line 73 in pkg/generators/validators_info.go

View check run for this annotation

Codecov / codecov/patch

pkg/generators/validators_info.go#L73

Added line #L73 was not covered by tests
}

for chain, validators := range consumersData.Validators {
Expand Down
8 changes: 4 additions & 4 deletions pkg/types/tendermint.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Validator struct {
Jailed bool `json:"jailed"`
Status string `json:"status"`
Tokens string `json:"tokens"`
DelegatorShares string `json:"delegator_shares"`
DelegatorShares math.LegacyDec `json:"delegator_shares"`
Description struct {
Moniker string `json:"moniker"`
Identity string `json:"identity"`
Expand All @@ -31,9 +31,9 @@ type Validator struct {
UnbondingTime time.Time `json:"unbonding_time"`
Commission struct {
CommissionRates struct {
Rate string `json:"rate"`
MaxRate string `json:"max_rate"`
MaxChangeRate string `json:"max_change_rate"`
Rate math.LegacyDec `json:"rate"`
MaxRate math.LegacyDec `json:"max_rate"`
MaxChangeRate math.LegacyDec `json:"max_change_rate"`
} `json:"commission_rates"`
UpdateTime time.Time `json:"update_time"`
} `json:"commission"`
Expand Down

0 comments on commit be22cf3

Please sign in to comment.