From 11a2fe4fdc42f7db598d545420e6ddb1046a1264 Mon Sep 17 00:00:00 2001 From: Sergey Date: Mon, 24 Jun 2024 12:59:01 +0300 Subject: [PATCH] chore: refactor denoms in validators info fetcher --- pkg/app.go | 2 +- pkg/generators/validators_info.go | 28 +++++++++++++++++++------- pkg/generators/validators_info_test.go | 22 +++++++++++++------- 3 files changed, 37 insertions(+), 15 deletions(-) diff --git a/pkg/app.go b/pkg/app.go index 0499c0e..8b8b015 100644 --- a/pkg/app.go +++ b/pkg/app.go @@ -110,7 +110,7 @@ func NewApp(configPath string, filesystem fs.FS, version string) *App { generatorsPkg.NewRewardsGenerator(appConfig.Chains), generatorsPkg.NewBalanceGenerator(appConfig.Chains), generatorsPkg.NewSelfDelegationGenerator(appConfig.Chains), - generatorsPkg.NewValidatorsInfoGenerator(), + generatorsPkg.NewValidatorsInfoGenerator(appConfig.Chains), generatorsPkg.NewSingleValidatorInfoGenerator(appConfig.Chains, logger), generatorsPkg.NewValidatorRankGenerator(appConfig.Chains, logger), generatorsPkg.NewActiveSetTokensGenerator(appConfig.Chains), diff --git a/pkg/generators/validators_info.go b/pkg/generators/validators_info.go index 2dea5c8..ffe2410 100644 --- a/pkg/generators/validators_info.go +++ b/pkg/generators/validators_info.go @@ -1,6 +1,7 @@ package generators import ( + "main/pkg/config" "main/pkg/constants" fetchersPkg "main/pkg/fetchers" statePkg "main/pkg/state" @@ -13,10 +14,11 @@ import ( ) type ValidatorsInfoGenerator struct { + Chains []*config.Chain } -func NewValidatorsInfoGenerator() *ValidatorsInfoGenerator { - return &ValidatorsInfoGenerator{} +func NewValidatorsInfoGenerator(chains []*config.Chain) *ValidatorsInfoGenerator { + return &ValidatorsInfoGenerator{Chains: chains} } func (g *ValidatorsInfoGenerator) Generate(state *statePkg.State) []prometheus.Collector { @@ -43,13 +45,18 @@ func (g *ValidatorsInfoGenerator) Generate(state *statePkg.State) []prometheus.C Name: constants.MetricsPrefix + "tokens_bonded_total", Help: "Total tokens bonded in chain", }, - []string{"chain"}, + []string{"chain", "denom"}, ) data, _ := dataRaw.(fetchersPkg.ValidatorsData) consumersData, _ := consumersDataRaw.(fetchersPkg.ConsumerValidatorsData) - for chain, validators := range data.Validators { + for _, chain := range g.Chains { + validators, ok := data.Validators[chain.Name] + if !ok { + continue + } + activeValidators := utils.Filter(validators.Validators, func(v types.Validator) bool { return v.Active() }) @@ -61,12 +68,19 @@ func (g *ValidatorsInfoGenerator) Generate(state *statePkg.State) []prometheus.C } validatorsCountGauge.With(prometheus.Labels{ - "chain": chain, + "chain": chain.Name, }).Set(float64(len(activeValidators))) + totalBondedAmount := &types.Amount{ + Amount: totalStake.MustFloat64(), + Denom: chain.BaseDenom, + } + totalBondedAmountConverted := chain.Denoms.Convert(totalBondedAmount) + totalBondedTokensGauge.With(prometheus.Labels{ - "chain": chain, - }).Set(totalStake.MustFloat64()) + "chain": chain.Name, + "denom": totalBondedAmountConverted.Denom, + }).Set(totalBondedAmountConverted.Amount) } for chain, validators := range consumersData.Validators { diff --git a/pkg/generators/validators_info_test.go b/pkg/generators/validators_info_test.go index 6404868..80dd0b3 100644 --- a/pkg/generators/validators_info_test.go +++ b/pkg/generators/validators_info_test.go @@ -1,6 +1,7 @@ package generators import ( + "main/pkg/config" "main/pkg/constants" "main/pkg/fetchers" statePkg "main/pkg/state" @@ -18,7 +19,7 @@ func TestValidatorsInfoGeneratorNoValidators(t *testing.T) { t.Parallel() state := statePkg.NewState() - generator := NewValidatorsInfoGenerator() + generator := NewValidatorsInfoGenerator([]*config.Chain{}) results := generator.Generate(state) assert.Empty(t, results) } @@ -28,7 +29,7 @@ func TestValidatorsInfoGeneratorNoConsumerValidators(t *testing.T) { state := statePkg.NewState() state.Set(constants.FetcherNameValidators, fetchers.ValidatorsData{}) - generator := NewValidatorsInfoGenerator() + generator := NewValidatorsInfoGenerator([]*config.Chain{}) results := generator.Generate(state) assert.Empty(t, results) } @@ -42,16 +43,16 @@ func TestValidatorsInfoGeneratorNotConsumer(t *testing.T) { "chain": { Validators: []types.Validator{ { - DelegatorShares: math.LegacyMustNewDecFromStr("2"), + DelegatorShares: math.LegacyMustNewDecFromStr("2000000"), OperatorAddress: "cosmosvaloper1c4k24jzduc365kywrsvf5ujz4ya6mwympnc4en", Status: constants.ValidatorStatusBonded, }, { - DelegatorShares: math.LegacyMustNewDecFromStr("1"), + DelegatorShares: math.LegacyMustNewDecFromStr("1000000"), OperatorAddress: "cosmosvaloper1xqz9pemz5e5zycaa89kys5aw6m8rhgsvw4328e", }, { - DelegatorShares: math.LegacyMustNewDecFromStr("3"), + DelegatorShares: math.LegacyMustNewDecFromStr("3000000"), OperatorAddress: "cosmosvaloper14lultfckehtszvzw4ehu0apvsr77afvyju5zzy", Status: constants.ValidatorStatusBonded, }, @@ -60,7 +61,13 @@ func TestValidatorsInfoGeneratorNotConsumer(t *testing.T) { }, }) state.Set(constants.FetcherNameConsumerValidators, fetchers.ConsumerValidatorsData{}) - generator := NewValidatorsInfoGenerator() + + chains := []*config.Chain{{ + Name: "chain", + BaseDenom: "uatom", + Denoms: config.DenomInfos{{Denom: "uatom", DisplayDenom: "atom", DenomCoefficient: 1000000}}, + }, {Name: "chain2"}} + generator := NewValidatorsInfoGenerator(chains) results := generator.Generate(state) assert.Len(t, results, 2) @@ -74,6 +81,7 @@ func TestValidatorsInfoGeneratorNotConsumer(t *testing.T) { assert.True(t, ok) assert.InEpsilon(t, float64(5), testutil.ToFloat64(totalBondedGauge.With(prometheus.Labels{ "chain": "chain", + "denom": "atom", })), 0.01) } @@ -93,7 +101,7 @@ func TestValidatorsInfoGeneratorConsumer(t *testing.T) { }, }, }) - generator := NewValidatorsInfoGenerator() + generator := NewValidatorsInfoGenerator([]*config.Chain{}) results := generator.Generate(state) assert.Len(t, results, 2)