-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add consumer commission * feat: add tests + refactor
- Loading branch information
1 parent
95d607b
commit 560faca
Showing
11 changed files
with
692 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"rate":"0.100000000000000000"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
Oops, something went wrong.