Skip to content

Commit

Permalink
chore: use mutex in state
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno committed Jan 7, 2025
1 parent e9653c4 commit d483265
Show file tree
Hide file tree
Showing 50 changed files with 131 additions and 100 deletions.
6 changes: 3 additions & 3 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ func NewController(
}

func (c *Controller) Fetch(ctx context.Context) (
statePkg.State,
*statePkg.State,
[]*types.QueryInfo,
) {
data := statePkg.State{}
data := statePkg.NewState()
queries := []*types.QueryInfo{}
fetchersStatus := FetchersStatuses{}

Expand All @@ -63,7 +63,7 @@ func (c *Controller) Fetch(ctx context.Context) (
fetcherData, fetcherQueries := fetcher.Fetch(ctx, fetcherDependenciesData...)

mutex.Lock()
data[fetcher.Name()] = fetcherData
data.Set(fetcher.Name(), fetcherData)
queries = append(queries, fetcherQueries...)
fetchersStatus[fetcher.Name()] = true
mutex.Unlock()
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ func TestControllerFetcherEnabled(t *testing.T) {

data, queryInfos := controller.Fetch(context.Background())
assert.Empty(t, queryInfos)
assert.Len(t, data, 2)
assert.Equal(t, 2, data.Length())
}
2 changes: 1 addition & 1 deletion pkg/generators/active_set_tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func NewActiveSetTokensGenerator(chains []*configPkg.Chain) *ActiveSetTokensGene
return &ActiveSetTokensGenerator{Chains: chains}
}

func (g *ActiveSetTokensGenerator) Generate(state statePkg.State) []prometheus.Collector {
func (g *ActiveSetTokensGenerator) Generate(state *statePkg.State) []prometheus.Collector {
validators, ok := statePkg.StateGet[fetchersPkg.ValidatorsData](state, constants.FetcherNameValidators)
if !ok {
return []prometheus.Collector{}
Expand Down
14 changes: 7 additions & 7 deletions pkg/generators/active_set_tokens_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
func TestActiveSetTokensGeneratorNoValidators(t *testing.T) {
t.Parallel()

state := statePkg.State{}
state := statePkg.NewState()
generator := NewActiveSetTokensGenerator([]*config.Chain{})
results := generator.Generate(state)
assert.Empty(t, results)
Expand All @@ -30,7 +30,7 @@ func TestActiveSetTokensGeneratorNoValidators(t *testing.T) {
func TestActiveSetTokensGeneratorNoStakingParams(t *testing.T) {
t.Parallel()

state := statePkg.State{}
state := statePkg.NewState()
state.Set(constants.FetcherNameValidators, fetchers.ValidatorsData{})
generator := NewActiveSetTokensGenerator([]*config.Chain{})
results := generator.Generate(state)
Expand All @@ -41,7 +41,7 @@ func TestActiveSetTokensGeneratorNoChainValidators(t *testing.T) {
t.Parallel()

chains := []*config.Chain{{Name: "chain"}}
state := statePkg.State{}
state := statePkg.NewState()
state.Set(constants.FetcherNameValidators, fetchers.ValidatorsData{})
state.Set(constants.FetcherNameStakingParams, fetchers.StakingParamsData{})
generator := NewActiveSetTokensGenerator(chains)
Expand All @@ -57,7 +57,7 @@ func TestActiveSetTokensGeneratorNoChainStakingParams(t *testing.T) {
t.Parallel()

chains := []*config.Chain{{Name: "chain"}}
state := statePkg.State{}
state := statePkg.NewState()
state.Set(constants.FetcherNameValidators, fetchers.ValidatorsData{
Validators: map[string]*types.ValidatorsResponse{
"chain": {},
Expand All @@ -81,7 +81,7 @@ func TestActiveSetTokensGeneratorNotEnoughValidators(t *testing.T) {
BaseDenom: "uatom",
Denoms: config.DenomInfos{{Denom: "uatom", DisplayDenom: "atom", DenomExponent: 6}},
}}
state := statePkg.State{}
state := statePkg.NewState()
state.Set(constants.FetcherNameValidators, fetchers.ValidatorsData{
Validators: map[string]*types.ValidatorsResponse{
"chain": {
Expand Down Expand Up @@ -120,7 +120,7 @@ func TestActiveSetTokensGeneratorEnoughValidators(t *testing.T) {
BaseDenom: "uatom",
Denoms: config.DenomInfos{{Denom: "uatom", DisplayDenom: "atom", DenomExponent: 6}},
}}
state := statePkg.State{}
state := statePkg.NewState()
state.Set(constants.FetcherNameValidators, fetchers.ValidatorsData{
Validators: map[string]*types.ValidatorsResponse{
"chain": {
Expand Down Expand Up @@ -159,7 +159,7 @@ func TestActiveSetTokensGeneratorDenomIgnored(t *testing.T) {
BaseDenom: "uatom",
Denoms: config.DenomInfos{{Denom: "uatom", Ignore: null.BoolFrom(true)}},
}}
state := statePkg.State{}
state := statePkg.NewState()
state.Set(constants.FetcherNameValidators, fetchers.ValidatorsData{
Validators: map[string]*types.ValidatorsResponse{
"chain": {
Expand Down
2 changes: 1 addition & 1 deletion pkg/generators/balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func NewBalanceGenerator(chains []*config.Chain) *BalanceGenerator {
return &BalanceGenerator{Chains: chains}
}

func (g *BalanceGenerator) Generate(state statePkg.State) []prometheus.Collector {
func (g *BalanceGenerator) Generate(state *statePkg.State) []prometheus.Collector {
data, ok := statePkg.StateGet[fetchersPkg.BalanceData](state, constants.FetcherNameBalance)
if !ok {
return []prometheus.Collector{}
Expand Down
4 changes: 2 additions & 2 deletions pkg/generators/balance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
func TestBalanceGeneratorNoState(t *testing.T) {
t.Parallel()

state := statePkg.State{}
state := statePkg.NewState()
generator := NewBalanceGenerator([]*config.Chain{})
results := generator.Generate(state)
assert.Empty(t, results)
Expand All @@ -28,7 +28,7 @@ func TestBalanceGeneratorNoState(t *testing.T) {
func TestBalanceGeneratorNotEmptyState(t *testing.T) {
t.Parallel()

state := statePkg.State{}
state := statePkg.NewState()
state.Set(constants.FetcherNameBalance, fetchers.BalanceData{
Balances: map[string]map[string][]types.Amount{
"chain": {
Expand Down
2 changes: 1 addition & 1 deletion pkg/generators/commission.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func NewCommissionGenerator(chains []*config.Chain) *CommissionGenerator {
return &CommissionGenerator{Chains: chains}
}

func (g *CommissionGenerator) Generate(state statePkg.State) []prometheus.Collector {
func (g *CommissionGenerator) Generate(state *statePkg.State) []prometheus.Collector {
data, ok := statePkg.StateGet[fetchersPkg.CommissionData](state, constants.FetcherNameCommission)
if !ok {
return []prometheus.Collector{}
Expand Down
4 changes: 2 additions & 2 deletions pkg/generators/commission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
func TestCommissionGeneratorNoState(t *testing.T) {
t.Parallel()

state := statePkg.State{}
state := statePkg.NewState()
generator := NewCommissionGenerator([]*config.Chain{})
results := generator.Generate(state)
assert.Empty(t, results)
Expand All @@ -28,7 +28,7 @@ func TestCommissionGeneratorNoState(t *testing.T) {
func TestCommissionGeneratorNotEmptyState(t *testing.T) {
t.Parallel()

state := statePkg.State{}
state := statePkg.NewState()
state.Set(constants.FetcherNameCommission, fetchers.CommissionData{
Commissions: map[string]map[string][]types.Amount{
"chain": {
Expand Down
2 changes: 1 addition & 1 deletion pkg/generators/consumer_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func NewConsumerInfoGenerator(chains []*config.Chain) *ConsumerInfoGenerator {
return &ConsumerInfoGenerator{Chains: chains}
}

func (g *ConsumerInfoGenerator) Generate(state statePkg.State) []prometheus.Collector {
func (g *ConsumerInfoGenerator) Generate(state *statePkg.State) []prometheus.Collector {
consumerInfos, ok := statePkg.StateGet[fetchersPkg.ConsumerInfoData](state, constants.FetcherNameConsumerInfo)
if !ok {
return []prometheus.Collector{}
Expand Down
4 changes: 2 additions & 2 deletions pkg/generators/consumer_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
func TestConsumerInfoGeneratorNoState(t *testing.T) {
t.Parallel()

state := statePkg.State{}
state := statePkg.NewState()
generator := NewConsumerInfoGenerator([]*config.Chain{})
results := generator.Generate(state)
assert.Empty(t, results)
Expand All @@ -28,7 +28,7 @@ func TestConsumerInfoGeneratorNoState(t *testing.T) {
func TestConsumerInfoGeneratorNotEmptyState(t *testing.T) {
t.Parallel()

state := statePkg.State{}
state := statePkg.NewState()
state.Set(constants.FetcherNameConsumerInfo, fetchers.ConsumerInfoData{
Info: map[string]map[string]types.ConsumerChainInfo{
"provider": {
Expand Down
2 changes: 1 addition & 1 deletion pkg/generators/consumer_needs_to_sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func NewConsumerNeedsToSignGenerator(chains []*config.Chain) *ConsumerNeedsToSig
return &ConsumerNeedsToSignGenerator{Chains: chains}
}

func (g *ConsumerNeedsToSignGenerator) Generate(state statePkg.State) []prometheus.Collector {
func (g *ConsumerNeedsToSignGenerator) Generate(state *statePkg.State) []prometheus.Collector {
allValidatorsConsumers, ok := statePkg.StateGet[fetchersPkg.ValidatorConsumersData](state, constants.FetcherNameValidatorConsumers)
if !ok {
return []prometheus.Collector{}
Expand Down
6 changes: 3 additions & 3 deletions pkg/generators/consumer_needs_to_sign_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
func TestConsumerNeedsToSignGeneratorNoState(t *testing.T) {
t.Parallel()

state := statePkg.State{}
state := statePkg.NewState()
generator := NewConsumerNeedsToSignGenerator([]*config.Chain{})
results := generator.Generate(state)
assert.Empty(t, results)
Expand All @@ -26,7 +26,7 @@ func TestConsumerNeedsToSignGeneratorNoState(t *testing.T) {
func TestConsumerNeedsToSignGeneratorNoConsumerInfo(t *testing.T) {
t.Parallel()

state := statePkg.State{}
state := statePkg.NewState()
state.Set(constants.FetcherNameValidatorConsumers, fetchers.ValidatorConsumersData{
Infos: map[string]map[string]map[string]bool{
"provider": {
Expand All @@ -45,7 +45,7 @@ func TestConsumerNeedsToSignGeneratorNoConsumerInfo(t *testing.T) {
func TestConsumerNeedsToSignGeneratorNotEmptyState(t *testing.T) {
t.Parallel()

state := statePkg.State{}
state := statePkg.NewState()
state.Set(constants.FetcherNameValidatorConsumers, fetchers.ValidatorConsumersData{
Infos: map[string]map[string]map[string]bool{
"provider": {
Expand Down
2 changes: 1 addition & 1 deletion pkg/generators/delegations.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func NewDelegationsGenerator() *DelegationsGenerator {
return &DelegationsGenerator{}
}

func (g *DelegationsGenerator) Generate(state statePkg.State) []prometheus.Collector {
func (g *DelegationsGenerator) Generate(state *statePkg.State) []prometheus.Collector {
data, ok := statePkg.StateGet[fetchersPkg.DelegationsData](state, constants.FetcherNameDelegations)
if !ok {
return []prometheus.Collector{}
Expand Down
4 changes: 2 additions & 2 deletions pkg/generators/delegations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
func TestDelegationsGeneratorNoState(t *testing.T) {
t.Parallel()

state := statePkg.State{}
state := statePkg.NewState()
generator := NewDelegationsGenerator()
results := generator.Generate(state)
assert.Empty(t, results)
Expand All @@ -24,7 +24,7 @@ func TestDelegationsGeneratorNoState(t *testing.T) {
func TestDelegationsGeneratorNotEmptyState(t *testing.T) {
t.Parallel()

state := statePkg.State{}
state := statePkg.NewState()
state.Set(constants.FetcherNameDelegations, fetchers.DelegationsData{
Delegations: map[string]map[string]uint64{
"chain": {
Expand Down
2 changes: 1 addition & 1 deletion pkg/generators/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ import (
)

type Generator interface {
Generate(state statePkg.State) []prometheus.Collector
Generate(state *statePkg.State) []prometheus.Collector
}
2 changes: 1 addition & 1 deletion pkg/generators/inflation.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func NewInflationGenerator() *InflationGenerator {
return &InflationGenerator{}
}

func (g *InflationGenerator) Generate(state statePkg.State) []prometheus.Collector {
func (g *InflationGenerator) Generate(state *statePkg.State) []prometheus.Collector {
data, ok := statePkg.StateGet[fetchersPkg.InflationData](state, constants.FetcherNameInflation)
if !ok {
return []prometheus.Collector{}
Expand Down
4 changes: 2 additions & 2 deletions pkg/generators/inflation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
func TestInflationGeneratorNoState(t *testing.T) {
t.Parallel()

state := statePkg.State{}
state := statePkg.NewState()
generator := NewInflationGenerator()
results := generator.Generate(state)
assert.Empty(t, results)
Expand All @@ -26,7 +26,7 @@ func TestInflationGeneratorNoState(t *testing.T) {
func TestInflationGeneratorNotEmptyState(t *testing.T) {
t.Parallel()

state := statePkg.State{}
state := statePkg.NewState()
state.Set(constants.FetcherNameInflation, fetchers.InflationData{
Inflation: map[string]math.LegacyDec{
"chain": math.LegacyMustNewDecFromStr("0.1"),
Expand Down
2 changes: 1 addition & 1 deletion pkg/generators/is_consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func NewIsConsumerGenerator(chains []*config.Chain) *IsConsumerGenerator {
return &IsConsumerGenerator{Chains: chains}
}

func (g *IsConsumerGenerator) Generate(state statePkg.State) []prometheus.Collector {
func (g *IsConsumerGenerator) Generate(state *statePkg.State) []prometheus.Collector {
isConsumerGauge := prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: constants.MetricsPrefix + "is_consumer",
Expand Down
2 changes: 1 addition & 1 deletion pkg/generators/is_consumer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
func TestIsConsumerGenerator(t *testing.T) {
t.Parallel()

state := statePkg.State{}
state := statePkg.NewState()
chains := []*config.Chain{
{
Name: "chain",
Expand Down
2 changes: 1 addition & 1 deletion pkg/generators/node_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func NewNodeInfoGenerator() *NodeInfoGenerator {
return &NodeInfoGenerator{}
}

func (g *NodeInfoGenerator) Generate(state statePkg.State) []prometheus.Collector {
func (g *NodeInfoGenerator) Generate(state *statePkg.State) []prometheus.Collector {
nodeInfos, ok := statePkg.StateGet[fetchersPkg.NodeInfoData](state, constants.FetcherNameNodeInfo)
if !ok {
return []prometheus.Collector{}
Expand Down
4 changes: 2 additions & 2 deletions pkg/generators/node_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
func TestNodeInfoGeneratorNoState(t *testing.T) {
t.Parallel()

state := statePkg.State{}
state := statePkg.NewState()
generator := NewNodeInfoGenerator()
results := generator.Generate(state)
assert.Empty(t, results)
Expand All @@ -25,7 +25,7 @@ func TestNodeInfoGeneratorNoState(t *testing.T) {
func TestNodeInfoGeneratorNotEmptyState(t *testing.T) {
t.Parallel()

state := statePkg.State{}
state := statePkg.NewState()
state.Set(constants.FetcherNameNodeInfo, fetchers.NodeInfoData{
NodeInfos: map[string]*types.NodeInfoResponse{
"chain": {
Expand Down
2 changes: 1 addition & 1 deletion pkg/generators/price.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func NewPriceGenerator() *PriceGenerator {
return &PriceGenerator{}
}

func (g *PriceGenerator) Generate(state statePkg.State) []prometheus.Collector {
func (g *PriceGenerator) Generate(state *statePkg.State) []prometheus.Collector {
data, ok := statePkg.StateGet[fetchersPkg.PriceData](state, constants.FetcherNamePrice)
if !ok {
return []prometheus.Collector{}
Expand Down
4 changes: 2 additions & 2 deletions pkg/generators/price_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
func TestPriceGeneratorNoState(t *testing.T) {
t.Parallel()

state := statePkg.State{}
state := statePkg.NewState()
generator := NewPriceGenerator()
results := generator.Generate(state)
assert.Empty(t, results)
Expand All @@ -24,7 +24,7 @@ func TestPriceGeneratorNoState(t *testing.T) {
func TestPriceGeneratorNotEmptyState(t *testing.T) {
t.Parallel()

state := statePkg.State{}
state := statePkg.NewState()
state.Set(constants.FetcherNamePrice, fetchers.PriceData{
Prices: map[string]map[string]fetchers.PriceInfo{
"chain": {
Expand Down
2 changes: 1 addition & 1 deletion pkg/generators/rewards.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func NewRewardsGenerator(chains []*config.Chain) *RewardsGenerator {
return &RewardsGenerator{Chains: chains}
}

func (g *RewardsGenerator) Generate(state statePkg.State) []prometheus.Collector {
func (g *RewardsGenerator) Generate(state *statePkg.State) []prometheus.Collector {
data, ok := statePkg.StateGet[fetchersPkg.RewardsData](state, constants.FetcherNameRewards)
if !ok {
return []prometheus.Collector{}
Expand Down
4 changes: 2 additions & 2 deletions pkg/generators/rewards_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
func TestRewardsGeneratorNoState(t *testing.T) {
t.Parallel()

state := statePkg.State{}
state := statePkg.NewState()
generator := NewRewardsGenerator([]*config.Chain{})
results := generator.Generate(state)
assert.Empty(t, results)
Expand All @@ -28,7 +28,7 @@ func TestRewardsGeneratorNoState(t *testing.T) {
func TestRewardsGeneratorNotEmptyState(t *testing.T) {
t.Parallel()

state := statePkg.State{}
state := statePkg.NewState()
state.Set(constants.FetcherNameRewards, fetchers.RewardsData{
Rewards: map[string]map[string][]types.Amount{
"chain": {
Expand Down
2 changes: 1 addition & 1 deletion pkg/generators/self_delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func NewSelfDelegationGenerator(chains []*config.Chain) *SelfDelegationGenerator
return &SelfDelegationGenerator{Chains: chains}
}

func (g *SelfDelegationGenerator) Generate(state statePkg.State) []prometheus.Collector {
func (g *SelfDelegationGenerator) Generate(state *statePkg.State) []prometheus.Collector {
data, ok := statePkg.StateGet[fetchersPkg.SelfDelegationData](state, constants.FetcherNameSelfDelegation)
if !ok {
return []prometheus.Collector{}
Expand Down
Loading

0 comments on commit d483265

Please sign in to comment.