Skip to content

Commit

Permalink
feat: add consumer commission (#69)
Browse files Browse the repository at this point in the history
* feat: add consumer commission

* feat: add tests + refactor
  • Loading branch information
freak12techno authored Jun 27, 2024
1 parent 95d607b commit 560faca
Show file tree
Hide file tree
Showing 11 changed files with 692 additions and 26 deletions.
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 @@ func NewApp(configPath string, filesystem fs.FS, version string) *App {
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),
}

generators := []generatorsPkg.Generator{
Expand All @@ -120,6 +121,7 @@ func NewApp(configPath string, filesystem fs.FS, version string) *App {
generatorsPkg.NewConsumerInfoGenerator(appConfig.Chains),
generatorsPkg.NewConsumerNeedsToSignGenerator(appConfig.Chains),
generatorsPkg.NewValidatorActiveGenerator(appConfig.Chains, logger),
generatorsPkg.NewValidatorCommissionRateGenerator(appConfig.Chains, logger),
}

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
}

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
}
}
Loading

0 comments on commit 560faca

Please sign in to comment.