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

feat: add consumer commission #69

Merged
merged 2 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions assets/consumer-commission.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"rate":"0.100000000000000000"}
2 changes: 2 additions & 0 deletions pkg/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
fetchersPkg.NewNodeInfoFetcher(logger, appConfig.Chains, rpcs, tracer),
fetchersPkg.NewConsumerInfoFetcher(logger, appConfig.Chains, rpcs, tracer),
fetchersPkg.NewValidatorConsumersFetcher(logger, appConfig.Chains, rpcs, tracer),
fetchersPkg.NewConsumerCommissionFetcher(logger, appConfig.Chains, rpcs, tracer),

Check warning on line 99 in pkg/app.go

View check run for this annotation

Codecov / codecov/patch

pkg/app.go#L99

Added line #L99 was not covered by tests
Copy link
Contributor

@coderabbitai coderabbitai bot Jun 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addition of New Fetcher: ConsumerCommissionFetcher

The new fetcher for consumer commissions is correctly added to the list of fetchers. However, this line was not covered by tests according to the static analysis tool.

+ fetchersPkg.NewConsumerCommissionFetcher(logger, appConfig.Chains, rpcs, tracer),

Consider adding unit tests to ensure this fetcher is initialized correctly and functions as expected.

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
fetchersPkg.NewConsumerCommissionFetcher(logger, appConfig.Chains, rpcs, tracer),
fetchersPkg.NewConsumerCommissionFetcher(logger, appConfig.Chains, rpcs, tracer),
Tools
GitHub Check: codecov/patch

[warning] 99-99: pkg/app.go#L99
Added line #L99 was not covered by tests

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will add tests for app.go later

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@freak12techno, understood! If you need any assistance when adding those tests later, feel free to reach out. Looking forward to seeing the updates!


Learnings used
User: freak12techno
PR: QuokkaStake/cosmos-validators-exporter#64
File: pkg/app.go:105-112
Timestamp: 2024-06-25T19:32:53.262Z
Learning: User freak12techno has clarified that while specific tests for new generator functions in `app.go` exist, there is an overall lack of tests for `app.go` itself, which is currently expected and accepted.

User: freak12techno
PR: QuokkaStake/cosmos-validators-exporter#63
File: pkg/fetchers/consumer_info.go:34-40
Timestamp: 2024-06-23T11:24:17.378Z
Learning: User freak12techno plans to add unit tests for the `NewConsumerInfoFetcher` constructor later.

User: freak12techno
PR: QuokkaStake/cosmos-validators-exporter#63
File: pkg/fetchers/validators.go:28-34
Timestamp: 2024-06-23T11:24:50.154Z
Learning: The constructor function `NewValidatorsFetcher` in `pkg/fetchers/validators.go` is now covered by tests in `validators_test.go`.

User: freak12techno
PR: QuokkaStake/cosmos-validators-exporter#63
File: pkg/fetchers/soft_opt_out_threshold.go:28-34
Timestamp: 2024-06-23T11:25:06.862Z
Learning: The `NewSoftOptOutThresholdFetcher` constructor function in `pkg/fetchers/soft_opt_out_threshold.go` is now covered by tests as verified by the presence of its usage in `pkg/fetchers/soft_opt_out_threshold_test.go`.

User: freak12techno
PR: QuokkaStake/cosmos-validators-exporter#57
File: pkg/app.go:86-86
Timestamp: 2024-06-16T10:28:15.556Z
Learning: The `fetchers` array in `pkg/app.go` is iteratively processed, and each fetcher's `Fetch` method is invoked, confirming that all fetchers, including `ConsumerValidatorsFetcher`, are actively used to fetch data.

User: freak12techno
PR: QuokkaStake/cosmos-validators-exporter#63
File: pkg/fetchers/consumer_validators.go:40-40
Timestamp: 2024-06-23T16:11:23.143Z
Learning: The `TestConsumerValidatorsFetcherBase` method in `pkg/fetchers/consumer_validators_test.go` tests the initialization of `ConsumerValidatorsFetcher` with the `Chains` parameter.

}

generators := []generatorsPkg.Generator{
Expand All @@ -120,6 +121,7 @@
generatorsPkg.NewConsumerInfoGenerator(appConfig.Chains),
generatorsPkg.NewConsumerNeedsToSignGenerator(appConfig.Chains),
generatorsPkg.NewValidatorActiveGenerator(appConfig.Chains, logger),
generatorsPkg.NewValidatorCommissionRateGenerator(appConfig.Chains, logger),

Check warning on line 124 in pkg/app.go

View check run for this annotation

Codecov / codecov/patch

pkg/app.go#L124

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

return &App{
Expand Down
1 change: 1 addition & 0 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const (
FetcherNameCommission FetcherName = "commission"
FetcherNameDelegations FetcherName = "delegations"
FetcherNameValidatorConsumers FetcherName = "validator-consumers"
FetcherNameConsumerCommission FetcherName = "consumer-commission"

FetcherNameUnbonds FetcherName = "unbonds"
FetcherNameSigningInfo FetcherName = "signing-info"
Expand Down
110 changes: 110 additions & 0 deletions pkg/fetchers/consumer_commission.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package fetchers

import (
"context"
"main/pkg/config"
"main/pkg/constants"
"main/pkg/tendermint"
"main/pkg/types"
"sync"

"github.com/rs/zerolog"
"go.opentelemetry.io/otel/trace"
)

type ConsumerCommissionFetcher struct {
Logger zerolog.Logger
Chains []*config.Chain
RPCs map[string]*tendermint.RPCWithConsumers
Tracer trace.Tracer

wg sync.WaitGroup
mutex sync.Mutex

queryInfos []*types.QueryInfo
data map[string]map[string]*types.ConsumerCommissionResponse
}

type ConsumerCommissionData struct {
Commissions map[string]map[string]*types.ConsumerCommissionResponse
}

func NewConsumerCommissionFetcher(
logger *zerolog.Logger,
chains []*config.Chain,
rpcs map[string]*tendermint.RPCWithConsumers,
tracer trace.Tracer,
) *ConsumerCommissionFetcher {
return &ConsumerCommissionFetcher{
Logger: logger.With().Str("component", "consumer_commission_fetcher").Logger(),
Chains: chains,
RPCs: rpcs,
Tracer: tracer,
}
}

func (f *ConsumerCommissionFetcher) Fetch(
ctx context.Context,
) (interface{}, []*types.QueryInfo) {
f.queryInfos = []*types.QueryInfo{}
f.data = map[string]map[string]*types.ConsumerCommissionResponse{}

for _, chain := range f.Chains {
for _, consumer := range chain.ConsumerChains {
f.data[consumer.Name] = map[string]*types.ConsumerCommissionResponse{}
}
}

for _, chain := range f.Chains {
for _, validator := range chain.Validators {
if validator.ConsensusAddress == "" {
continue
}

rpc, _ := f.RPCs[chain.Name]

for _, consumerChain := range chain.ConsumerChains {
f.wg.Add(1)
go f.processChain(ctx, rpc.RPC, consumerChain, validator)
}
}
}

f.wg.Wait()

return ConsumerCommissionData{Commissions: f.data}, f.queryInfos
}
freak12techno marked this conversation as resolved.
Show resolved Hide resolved

func (f *ConsumerCommissionFetcher) Name() constants.FetcherName {
return constants.FetcherNameConsumerCommission
}

func (f *ConsumerCommissionFetcher) processChain(
ctx context.Context,
rpc *tendermint.RPC,
chain *config.ConsumerChain,
validator config.Validator,
) {
defer f.wg.Done()

commission, queryInfo, err := rpc.GetConsumerCommission(ctx, validator.ConsensusAddress, chain.ChainID)

f.mutex.Lock()
defer f.mutex.Unlock()

if queryInfo != nil {
f.queryInfos = append(f.queryInfos, queryInfo)
}

if err != nil {
f.Logger.Error().
Err(err).
Str("chain", chain.Name).
Msg("Error querying consumer commission")
return
}

if commission != nil {
f.data[chain.Name][validator.Address] = commission
}
}
freak12techno marked this conversation as resolved.
Show resolved Hide resolved
Loading
Loading