Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add generators test #62

Merged
merged 28 commits into from
Jun 22, 2024
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
ab61fb2
chore: add price generator test
freak12techno Jun 22, 2024
6345791
chore: add staking params generator
freak12techno Jun 22, 2024
d9e8ce1
chore: add slashing params generator
freak12techno Jun 22, 2024
72c2909
chore: add soft opt out threshold generator
freak12techno Jun 22, 2024
d011163
chore: add signing info generator
freak12techno Jun 22, 2024
9b0dac0
chore: use math.Int for string ints
freak12techno Jun 22, 2024
b363fc6
chore: add commission generator
freak12techno Jun 22, 2024
c4fd098
chore: add rewards generator
freak12techno Jun 22, 2024
af44cdc
chore: add self-delegation generator
freak12techno Jun 22, 2024
8d886f5
chore: add consumer info generator test
freak12techno Jun 22, 2024
73b222d
chore: use math.Int in consumer info
freak12techno Jun 22, 2024
19d80f0
chore: add uptime generator test
freak12techno Jun 22, 2024
1afc7cc
chore: add is consumer generator test
freak12techno Jun 22, 2024
e8ca0f8
chore: add node info generator test
freak12techno Jun 22, 2024
f17910e
chore: add denom coefficient generator test
freak12techno Jun 22, 2024
7e43402
chore: use uint64 in pagination
freak12techno Jun 22, 2024
fb158d7
chore: more coverage for denom coefficient generator test
freak12techno Jun 22, 2024
2da7e04
chore: add delegations generator test
freak12techno Jun 22, 2024
9c3b1e6
chore: add unbonds generator test
freak12techno Jun 22, 2024
be22cf3
chore: use math.LegacyDec for validator fields
freak12techno Jun 22, 2024
b8f39be
chore: use math.LegacyDec for balances
freak12techno Jun 22, 2024
d9ab357
chore: fix tests
freak12techno Jun 22, 2024
c881d20
chore: actually check values returned by metrics
freak12techno Jun 22, 2024
36fd2f0
chore: added active set tokens generator test
freak12techno Jun 22, 2024
69ed379
chore: validator rank generator test
freak12techno Jun 22, 2024
251377b
chore: validators info generator test
freak12techno Jun 22, 2024
6297a8d
chore: do not sort on validators info
freak12techno Jun 22, 2024
6ec167d
chore: single validator info generator test
freak12techno Jun 22, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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=
11 changes: 5 additions & 6 deletions pkg/fetchers/delegations.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"main/pkg/constants"
"main/pkg/tendermint"
"main/pkg/types"
"main/pkg/utils"
"sync"

"github.com/rs/zerolog"
Expand All @@ -21,7 +20,7 @@
}

type DelegationsData struct {
Delegations map[string]map[string]int64
Delegations map[string]map[string]uint64
}

func NewDelegationsFetcher(
Expand All @@ -43,15 +42,15 @@
) (interface{}, []*types.QueryInfo) {
var queryInfos []*types.QueryInfo

allDelegations := map[string]map[string]int64{}
allDelegations := map[string]map[string]uint64{}

Check warning on line 45 in pkg/fetchers/delegations.go

View check run for this annotation

Codecov / codecov/patch

pkg/fetchers/delegations.go#L45

Added line #L45 was not covered by tests
freak12techno marked this conversation as resolved.
Show resolved Hide resolved

var wg sync.WaitGroup
var mutex sync.Mutex

for _, chain := range q.Config.Chains {
allDelegations[chain.Name] = map[string]int64{}
allDelegations[chain.Name] = map[string]uint64{}

Check warning on line 51 in pkg/fetchers/delegations.go

View check run for this annotation

Codecov / codecov/patch

pkg/fetchers/delegations.go#L51

Added line #L51 was not covered by tests
for _, consumerChain := range chain.ConsumerChains {
allDelegations[consumerChain.Name] = map[string]int64{}
allDelegations[consumerChain.Name] = map[string]uint64{}

Check warning on line 53 in pkg/fetchers/delegations.go

View check run for this annotation

Codecov / codecov/patch

pkg/fetchers/delegations.go#L53

Added line #L53 was not covered by tests
freak12techno marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down Expand Up @@ -84,7 +83,7 @@
return
}

allDelegations[chain.Name][validator] = utils.StrToInt64(delegatorsResponse.Pagination.Total)
allDelegations[chain.Name][validator] = delegatorsResponse.Pagination.Total

Check warning on line 86 in pkg/fetchers/delegations.go

View check run for this annotation

Codecov / codecov/patch

pkg/fetchers/delegations.go#L86

Added line #L86 was not covered by tests
freak12techno marked this conversation as resolved.
Show resolved Hide resolved

// consumer chains do not have staking module, so no delegations, therefore
// we do not calculate it here
Expand Down
9 changes: 4 additions & 5 deletions pkg/fetchers/unbonds.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"main/pkg/constants"
"main/pkg/tendermint"
"main/pkg/types"
"main/pkg/utils"
"sync"

"github.com/rs/zerolog"
Expand All @@ -21,7 +20,7 @@
}

type UnbondsData struct {
Unbonds map[string]map[string]int64
Unbonds map[string]map[string]uint64
}

func NewUnbondsFetcher(
Expand All @@ -43,14 +42,14 @@
) (interface{}, []*types.QueryInfo) {
var queryInfos []*types.QueryInfo

allUnbonds := map[string]map[string]int64{}
allUnbonds := map[string]map[string]uint64{}

Check warning on line 45 in pkg/fetchers/unbonds.go

View check run for this annotation

Codecov / codecov/patch

pkg/fetchers/unbonds.go#L45

Added line #L45 was not covered by tests
freak12techno marked this conversation as resolved.
Show resolved Hide resolved

var wg sync.WaitGroup
var mutex sync.Mutex

for _, chain := range q.Config.Chains {
mutex.Lock()
allUnbonds[chain.Name] = map[string]int64{}
allUnbonds[chain.Name] = map[string]uint64{}

Check warning on line 52 in pkg/fetchers/unbonds.go

View check run for this annotation

Codecov / codecov/patch

pkg/fetchers/unbonds.go#L52

Added line #L52 was not covered by tests
mutex.Unlock()

rpc, _ := q.RPCs[chain.Name]
Expand Down Expand Up @@ -81,7 +80,7 @@
return
}

allUnbonds[chain.Name][validator] = utils.StrToInt64(unbondsResponse.Pagination.Total)
allUnbonds[chain.Name][validator] = unbondsResponse.Pagination.Total

Check warning on line 83 in pkg/fetchers/unbonds.go

View check run for this annotation

Codecov / codecov/patch

pkg/fetchers/unbonds.go#L83

Added line #L83 was not covered by tests

// consumer chains do not have staking module, so no unbonds, therefore
// we do not calculate it here
Expand Down
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)
freak12techno marked this conversation as resolved.
Show resolved Hide resolved
})

lastValidatorStake := utils.StrToFloat64(activeValidators[len(activeValidators)-1].DelegatorShares)
lastValidatorStake := activeValidators[len(activeValidators)-1].DelegatorShares.MustFloat64()
freak12techno marked this conversation as resolved.
Show resolved Hide resolved

if chainStakingParams != nil && len(activeValidators) >= chainStakingParams.StakingParams.MaxValidators {
activeSetTokensGauge.With(prometheus.Labels{
Expand Down
139 changes: 139 additions & 0 deletions pkg/generators/active_set_tokens_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package generators

import (
"main/pkg/config"
"main/pkg/constants"
"main/pkg/fetchers"
statePkg "main/pkg/state"
"main/pkg/types"
"testing"

"cosmossdk.io/math"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/testutil"

"github.com/stretchr/testify/assert"
)

func TestActiveSetTokensGeneratorNoValidators(t *testing.T) {
t.Parallel()

state := statePkg.NewState()
generator := NewActiveSetTokensGenerator([]*config.Chain{})
results := generator.Generate(state)
assert.Empty(t, results)
}

func TestActiveSetTokensGeneratorNoStakingParams(t *testing.T) {
t.Parallel()

state := statePkg.NewState()
state.Set(constants.FetcherNameValidators, fetchers.ValidatorsData{})
generator := NewActiveSetTokensGenerator([]*config.Chain{})
results := generator.Generate(state)
assert.Empty(t, results)
}

func TestActiveSetTokensGeneratorNoChainValidators(t *testing.T) {
t.Parallel()

chains := []*config.Chain{{Name: "chain"}}
state := statePkg.NewState()
state.Set(constants.FetcherNameValidators, fetchers.ValidatorsData{})
state.Set(constants.FetcherNameStakingParams, fetchers.StakingParamsData{})
generator := NewActiveSetTokensGenerator(chains)
results := generator.Generate(state)
assert.NotEmpty(t, results)

gauge, ok := results[0].(*prometheus.GaugeVec)
assert.True(t, ok)
assert.Equal(t, 0, testutil.CollectAndCount(gauge))
}

func TestActiveSetTokensGeneratorNoChainStakingParams(t *testing.T) {
t.Parallel()

chains := []*config.Chain{{Name: "chain"}}
state := statePkg.NewState()
state.Set(constants.FetcherNameValidators, fetchers.ValidatorsData{
Validators: map[string]*types.ValidatorsResponse{
"chain": {},
},
})
state.Set(constants.FetcherNameStakingParams, fetchers.StakingParamsData{})
generator := NewActiveSetTokensGenerator(chains)
results := generator.Generate(state)
assert.NotEmpty(t, results)

gauge, ok := results[0].(*prometheus.GaugeVec)
assert.True(t, ok)
assert.Equal(t, 0, testutil.CollectAndCount(gauge))
}

func TestActiveSetTokensGeneratorNotEnoughValidators(t *testing.T) {
t.Parallel()

chains := []*config.Chain{{Name: "chain"}}
state := statePkg.NewState()
state.Set(constants.FetcherNameValidators, fetchers.ValidatorsData{
Validators: map[string]*types.ValidatorsResponse{
"chain": {
Validators: []types.Validator{
{DelegatorShares: math.LegacyMustNewDecFromStr("2"), Status: constants.ValidatorStatusBonded},
{DelegatorShares: math.LegacyMustNewDecFromStr("1"), Status: constants.ValidatorStatusBonded},
{DelegatorShares: math.LegacyMustNewDecFromStr("3"), Status: constants.ValidatorStatusBonded},
},
},
},
})
state.Set(constants.FetcherNameStakingParams, fetchers.StakingParamsData{
Params: map[string]*types.StakingParamsResponse{
"chain": {
StakingParams: types.StakingParams{MaxValidators: 100},
},
},
})
generator := NewActiveSetTokensGenerator(chains)
results := generator.Generate(state)
assert.NotEmpty(t, results)

gauge, ok := results[0].(*prometheus.GaugeVec)
assert.True(t, ok)
assert.Zero(t, testutil.ToFloat64(gauge.With(prometheus.Labels{
"chain": "chain",
})))
}

func TestActiveSetTokensGeneratorEnoughValidators(t *testing.T) {
t.Parallel()

chains := []*config.Chain{{Name: "chain"}}
state := statePkg.NewState()
state.Set(constants.FetcherNameValidators, fetchers.ValidatorsData{
Validators: map[string]*types.ValidatorsResponse{
"chain": {
Validators: []types.Validator{
{DelegatorShares: math.LegacyMustNewDecFromStr("2"), Status: constants.ValidatorStatusBonded},
{DelegatorShares: math.LegacyMustNewDecFromStr("1")},
{DelegatorShares: math.LegacyMustNewDecFromStr("3"), Status: constants.ValidatorStatusBonded},
},
},
},
})
state.Set(constants.FetcherNameStakingParams, fetchers.StakingParamsData{
Params: map[string]*types.StakingParamsResponse{
"chain": {
StakingParams: types.StakingParams{MaxValidators: 2},
},
},
})
generator := NewActiveSetTokensGenerator(chains)
results := generator.Generate(state)
assert.NotEmpty(t, results)

gauge, ok := results[0].(*prometheus.GaugeVec)
assert.True(t, ok)
assert.InEpsilon(t, float64(2), testutil.ToFloat64(gauge.With(prometheus.Labels{
"chain": "chain",
})), 0.01)
}
20 changes: 18 additions & 2 deletions pkg/generators/balance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import (
"main/pkg/types"
"testing"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/testutil"

"github.com/stretchr/testify/assert"
)

Expand All @@ -19,7 +22,7 @@ func TestBalanceGeneratorNoState(t *testing.T) {
assert.Empty(t, results)
}

func TestBlockSetValidators(t *testing.T) {
func TestBalanceGeneratorNotEmptyState(t *testing.T) {
t.Parallel()

state := statePkg.NewState()
Expand All @@ -36,5 +39,18 @@ func TestBlockSetValidators(t *testing.T) {

generator := NewBalanceGenerator()
results := generator.Generate(state)
assert.NotEmpty(t, results)
assert.Len(t, results, 1)

gauge, ok := results[0].(*prometheus.GaugeVec)
assert.True(t, ok)
assert.InEpsilon(t, float64(100), testutil.ToFloat64(gauge.With(prometheus.Labels{
"chain": "chain",
"address": "validator",
"denom": "uatom",
})), 0.01)
assert.InEpsilon(t, float64(200), testutil.ToFloat64(gauge.With(prometheus.Labels{
"chain": "chain",
"address": "validator",
"denom": "ustake",
})), 0.01)
}
50 changes: 50 additions & 0 deletions pkg/generators/commission_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package generators

import (
"main/pkg/constants"
"main/pkg/fetchers"
statePkg "main/pkg/state"
"main/pkg/types"
"testing"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/testutil"

"github.com/stretchr/testify/assert"
)

func TestCommissionGeneratorNoState(t *testing.T) {
t.Parallel()

state := statePkg.NewState()
generator := NewCommissionGenerator()
results := generator.Generate(state)
assert.Empty(t, results)
}

func TestCommissionGeneratorNotEmptyState(t *testing.T) {
t.Parallel()

state := statePkg.NewState()
state.Set(constants.FetcherNameCommission, fetchers.CommissionData{
Commissions: map[string]map[string][]types.Amount{
"chain": {
"validator": []types.Amount{
{Amount: 1, Denom: "denom"},
},
},
},
})

generator := NewCommissionGenerator()
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{
"chain": "chain",
"address": "validator",
"denom": "denom",
})), 0.01)
}
Loading
Loading