Skip to content

Commit

Permalink
chore: refactor denoms in validators info fetcher
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno committed Jun 24, 2024
1 parent c785b11 commit 11a2fe4
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 15 deletions.
2 changes: 1 addition & 1 deletion pkg/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),

Check warning on line 113 in pkg/app.go

View check run for this annotation

Codecov / codecov/patch

pkg/app.go#L110-L113

Added lines #L110 - L113 were not covered by tests
generatorsPkg.NewSingleValidatorInfoGenerator(appConfig.Chains, logger),
generatorsPkg.NewValidatorRankGenerator(appConfig.Chains, logger),
generatorsPkg.NewActiveSetTokensGenerator(appConfig.Chains),
Expand Down
28 changes: 21 additions & 7 deletions pkg/generators/validators_info.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package generators

import (
"main/pkg/config"
"main/pkg/constants"
fetchersPkg "main/pkg/fetchers"
statePkg "main/pkg/state"
Expand All @@ -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 {
Expand All @@ -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()
})
Expand All @@ -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 {
Expand Down
22 changes: 15 additions & 7 deletions pkg/generators/validators_info_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package generators

import (
"main/pkg/config"
"main/pkg/constants"
"main/pkg/fetchers"
statePkg "main/pkg/state"
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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,
},
Expand All @@ -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)

Expand All @@ -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)
}

Expand All @@ -93,7 +101,7 @@ func TestValidatorsInfoGeneratorConsumer(t *testing.T) {
},
},
})
generator := NewValidatorsInfoGenerator()
generator := NewValidatorsInfoGenerator([]*config.Chain{})
results := generator.Generate(state)
assert.Len(t, results, 2)

Expand Down

0 comments on commit 11a2fe4

Please sign in to comment.