From be14b4b72c79faac8013909d2bdaa724eb690df5 Mon Sep 17 00:00:00 2001 From: Sergey Date: Mon, 24 Jun 2024 01:10:29 +0300 Subject: [PATCH] chore: refactor denoms in rewards and self delegated fetcher --- pkg/app.go | 4 +-- pkg/generators/rewards.go | 31 +++++++++++++++------- pkg/generators/rewards_test.go | 30 ++++++++++++++++++---- pkg/generators/self_delegation.go | 29 +++++++++++++++------ pkg/generators/self_delegations_test.go | 34 +++++++++++++++++++++---- 5 files changed, 99 insertions(+), 29 deletions(-) diff --git a/pkg/app.go b/pkg/app.go index adb48bf..e86a62b 100644 --- a/pkg/app.go +++ b/pkg/app.go @@ -109,9 +109,9 @@ func NewApp(configPath string, filesystem fs.FS, version string) *App { generatorsPkg.NewDelegationsGenerator(), generatorsPkg.NewUnbondsGenerator(), generatorsPkg.NewSigningInfoGenerator(), - generatorsPkg.NewRewardsGenerator(), + generatorsPkg.NewRewardsGenerator(appConfig.Chains), generatorsPkg.NewBalanceGenerator(appConfig.Chains), - generatorsPkg.NewSelfDelegationGenerator(), + generatorsPkg.NewSelfDelegationGenerator(appConfig.Chains), generatorsPkg.NewValidatorsInfoGenerator(), generatorsPkg.NewSingleValidatorInfoGenerator(appConfig.Chains, logger), generatorsPkg.NewValidatorRankGenerator(appConfig.Chains, logger), diff --git a/pkg/generators/rewards.go b/pkg/generators/rewards.go index a845b4f..add3a50 100644 --- a/pkg/generators/rewards.go +++ b/pkg/generators/rewards.go @@ -1,6 +1,7 @@ package generators import ( + "main/pkg/config" "main/pkg/constants" fetchersPkg "main/pkg/fetchers" statePkg "main/pkg/state" @@ -9,10 +10,11 @@ import ( ) type RewardsGenerator struct { + Chains []*config.Chain } -func NewRewardsGenerator() *RewardsGenerator { - return &RewardsGenerator{} +func NewRewardsGenerator(chains []*config.Chain) *RewardsGenerator { + return &RewardsGenerator{Chains: chains} } func (g *RewardsGenerator) Generate(state *statePkg.State) []prometheus.Collector { @@ -31,14 +33,25 @@ func (g *RewardsGenerator) Generate(state *statePkg.State) []prometheus.Collecto data, _ := dataRaw.(fetchersPkg.RewardsData) - for chain, rewards := range data.Rewards { - for validator, validatorReward := range rewards { - for _, balance := range validatorReward { + for _, chain := range g.Chains { + chainRewards, ok := data.Rewards[chain.Name] + if !ok { + continue + } + + for _, validator := range chain.Validators { + validatorRewards, ok := chainRewards[validator.Address] + if !ok { + continue + } + + for _, balance := range validatorRewards { + amountConverted := chain.Denoms.Convert(&balance) selfDelegationRewardsTokens.With(prometheus.Labels{ - "chain": chain, - "address": validator, - "denom": balance.Denom, - }).Set(balance.Amount) + "chain": chain.Name, + "address": validator.Address, + "denom": amountConverted.Denom, + }).Set(amountConverted.Amount) } } } diff --git a/pkg/generators/rewards_test.go b/pkg/generators/rewards_test.go index 92d1b6b..8c63b14 100644 --- a/pkg/generators/rewards_test.go +++ b/pkg/generators/rewards_test.go @@ -1,6 +1,7 @@ package generators import ( + "main/pkg/config" "main/pkg/constants" "main/pkg/fetchers" statePkg "main/pkg/state" @@ -17,7 +18,7 @@ func TestRewardsGeneratorNoState(t *testing.T) { t.Parallel() state := statePkg.NewState() - generator := NewRewardsGenerator() + generator := NewRewardsGenerator([]*config.Chain{}) results := generator.Generate(state) assert.Empty(t, results) } @@ -30,21 +31,40 @@ func TestRewardsGeneratorNotEmptyState(t *testing.T) { Rewards: map[string]map[string][]types.Amount{ "chain": { "validator": []types.Amount{ - {Amount: 1, Denom: "denom"}, + {Amount: 100000, Denom: "uatom"}, + {Amount: 200000, Denom: "ustake"}, }, }, }, }) - generator := NewRewardsGenerator() + chains := []*config.Chain{ + { + Name: "chain", + Validators: []config.Validator{{Address: "validator"}, {Address: "validator2"}}, + Denoms: config.DenomInfos{ + {Denom: "uatom", DisplayDenom: "atom", DenomCoefficient: 1000000}, + }, + }, + { + Name: "chain2", + Validators: []config.Validator{{Address: "validator"}}, + }, + } + generator := NewRewardsGenerator(chains) results := generator.Generate(state) assert.NotEmpty(t, results) gauge, ok := results[0].(*prometheus.GaugeVec) assert.True(t, ok) - assert.InEpsilon(t, float64(1), testutil.ToFloat64(gauge.With(prometheus.Labels{ + assert.InEpsilon(t, 0.1, testutil.ToFloat64(gauge.With(prometheus.Labels{ + "chain": "chain", + "address": "validator", + "denom": "atom", + })), 0.01) + assert.InEpsilon(t, float64(200000), testutil.ToFloat64(gauge.With(prometheus.Labels{ "chain": "chain", "address": "validator", - "denom": "denom", + "denom": "ustake", })), 0.01) } diff --git a/pkg/generators/self_delegation.go b/pkg/generators/self_delegation.go index bd74285..af660df 100644 --- a/pkg/generators/self_delegation.go +++ b/pkg/generators/self_delegation.go @@ -1,6 +1,7 @@ package generators import ( + "main/pkg/config" "main/pkg/constants" fetchersPkg "main/pkg/fetchers" statePkg "main/pkg/state" @@ -9,10 +10,11 @@ import ( ) type SelfDelegationGenerator struct { + Chains []*config.Chain } -func NewSelfDelegationGenerator() *SelfDelegationGenerator { - return &SelfDelegationGenerator{} +func NewSelfDelegationGenerator(chains []*config.Chain) *SelfDelegationGenerator { + return &SelfDelegationGenerator{Chains: chains} } func (g *SelfDelegationGenerator) Generate(state *statePkg.State) []prometheus.Collector { @@ -31,13 +33,24 @@ func (g *SelfDelegationGenerator) Generate(state *statePkg.State) []prometheus.C data, _ := dataRaw.(fetchersPkg.SelfDelegationData) - for chain, delegations := range data.Delegations { - for validator, delegation := range delegations { + for _, chain := range g.Chains { + chainDelegations, ok := data.Delegations[chain.Name] + if !ok { + continue + } + + for _, validator := range chain.Validators { + validatorSelfDelegation, ok := chainDelegations[validator.Address] + if !ok { + continue + } + + amountConverted := chain.Denoms.Convert(validatorSelfDelegation) selfDelegatedTokensGauge.With(prometheus.Labels{ - "chain": chain, - "address": validator, - "denom": delegation.Denom, - }).Set(delegation.Amount) + "chain": chain.Name, + "address": validator.Address, + "denom": amountConverted.Denom, + }).Set(amountConverted.Amount) } } diff --git a/pkg/generators/self_delegations_test.go b/pkg/generators/self_delegations_test.go index f8aab84..841b473 100644 --- a/pkg/generators/self_delegations_test.go +++ b/pkg/generators/self_delegations_test.go @@ -1,6 +1,7 @@ package generators import ( + "main/pkg/config" "main/pkg/constants" "main/pkg/fetchers" statePkg "main/pkg/state" @@ -17,7 +18,7 @@ func TestSelfDelegationGeneratorNoState(t *testing.T) { t.Parallel() state := statePkg.NewState() - generator := NewSelfDelegationGenerator() + generator := NewSelfDelegationGenerator([]*config.Chain{}) results := generator.Generate(state) assert.Empty(t, results) } @@ -29,20 +30,43 @@ func TestSelfDelegationGeneratorNotEmptyState(t *testing.T) { state.Set(constants.FetcherNameSelfDelegation, fetchers.SelfDelegationData{ Delegations: map[string]map[string]*types.Amount{ "chain": { - "validator": {Amount: 1, Denom: "denom"}, + "validator": {Amount: 100000, Denom: "uatom"}, + "validator2": {Amount: 200000, Denom: "ustake"}, }, }, }) - generator := NewSelfDelegationGenerator() + chains := []*config.Chain{ + { + Name: "chain", + Validators: []config.Validator{ + {Address: "validator"}, + {Address: "validator2"}, + {Address: "validator3"}, + }, + Denoms: config.DenomInfos{ + {Denom: "uatom", DisplayDenom: "atom", DenomCoefficient: 1000000}, + }, + }, + { + Name: "chain2", + Validators: []config.Validator{{Address: "validator"}}, + }, + } + generator := NewSelfDelegationGenerator(chains) results := generator.Generate(state) assert.NotEmpty(t, results) gauge, ok := results[0].(*prometheus.GaugeVec) assert.True(t, ok) - assert.InEpsilon(t, float64(1), testutil.ToFloat64(gauge.With(prometheus.Labels{ + assert.InEpsilon(t, 0.1, testutil.ToFloat64(gauge.With(prometheus.Labels{ "chain": "chain", "address": "validator", - "denom": "denom", + "denom": "atom", + })), 0.01) + assert.InEpsilon(t, float64(200000), testutil.ToFloat64(gauge.With(prometheus.Labels{ + "chain": "chain", + "address": "validator2", + "denom": "ustake", })), 0.01) }