From 25972d01e4e9a3ad12cf803e99fb16c2c2cd17a7 Mon Sep 17 00:00:00 2001 From: Sairam kola Date: Thu, 7 Dec 2023 18:14:56 +0530 Subject: [PATCH 1/2] update validator data --- queryengine/native/native_types.go | 44 +++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/queryengine/native/native_types.go b/queryengine/native/native_types.go index 925a258..50d60ce 100644 --- a/queryengine/native/native_types.go +++ b/queryengine/native/native_types.go @@ -3,6 +3,7 @@ package queryengine import ( "context" "strconv" + "sync" "time" csr "github.com/Canto-Network/Canto/v6/x/csr/types" @@ -28,6 +29,8 @@ type Validator struct { Description staking.Description `json:"description"` // commission defines the commission rate. Commission string `json:"commission"` + // number of delegators. + Delegators int `json:"delegators"` } // get all Validators for staking @@ -41,9 +44,31 @@ func GetValidators(ctx context.Context, queryClient staking.QueryClient) ([]Vali if err != nil { return nil, nil, err } - allValidators := new([]Validator) + allValidators := []Validator{} + validatorDelegationsMap := make(map[string]int) validatorMap := make(map[string]string) + + var wg sync.WaitGroup + wg.Add(len(respValidators.Validators)) + var validatorDelegationsErr error + for _, validator := range respValidators.Validators { + go func() { + defer wg.Done() + delegations, err := queryClient.ValidatorDelegations(ctx, &staking.QueryValidatorDelegationsRequest{ + ValidatorAddr: validator.OperatorAddress, + Pagination: &query.PageRequest{ + Limit: 1000, + }, + }) + + if err == nil { + validatorDelegationsMap[validator.OperatorAddress] = len(delegations.DelegationResponses) + } else { + validatorDelegationsErr = err + } + }() + valResponse := Validator{ OperatorAddress: validator.OperatorAddress, Jailed: validator.Jailed, @@ -52,10 +77,21 @@ func GetValidators(ctx context.Context, queryClient staking.QueryClient) ([]Vali Description: validator.Description, Commission: validator.Commission.CommissionRates.Rate.String(), } - *allValidators = append(*allValidators, valResponse) - validatorMap[validator.OperatorAddress] = GeneralResultToString(valResponse) + allValidators = append(allValidators, valResponse) } - return *allValidators, validatorMap, nil + wg.Wait() + + if validatorDelegationsErr != nil { + return nil, nil, err + } + + for index, validator := range allValidators { + validator.Delegators = validatorDelegationsMap[validator.OperatorAddress] + allValidators[index] = validator + validatorMap[validator.OperatorAddress] = GeneralResultToString(validator) + } + + return allValidators, validatorMap, nil } func GetStakingAPR(ctx context.Context, stakingQueryClient staking.QueryClient, inflationQueryClient inflation.QueryClient) (string, error) { //get pool From b3d6eef17984388e893489fb9c37f7a906c5a86e Mon Sep 17 00:00:00 2001 From: Sairam kola Date: Fri, 8 Dec 2023 14:24:58 +0530 Subject: [PATCH 2/2] update validators --- queryengine/native/native_types.go | 58 +++++++++++++++++++----------- 1 file changed, 37 insertions(+), 21 deletions(-) diff --git a/queryengine/native/native_types.go b/queryengine/native/native_types.go index 50d60ce..dadf1a8 100644 --- a/queryengine/native/native_types.go +++ b/queryengine/native/native_types.go @@ -33,6 +33,12 @@ type Validator struct { Delegators int `json:"delegators"` } +type ValidatorDelegationsResponse struct { + Validator string + Delegations int + Error error +} + // get all Validators for staking // will return full response string and mapping of operator address to response string func GetValidators(ctx context.Context, queryClient staking.QueryClient) ([]Validator, map[string]string, error) { @@ -45,29 +51,36 @@ func GetValidators(ctx context.Context, queryClient staking.QueryClient) ([]Vali return nil, nil, err } allValidators := []Validator{} - validatorDelegationsMap := make(map[string]int) - validatorMap := make(map[string]string) + validatorMap := make(map[string]Validator) + validatorToResultStringMap := make(map[string]string) var wg sync.WaitGroup wg.Add(len(respValidators.Validators)) - var validatorDelegationsErr error + resultChan := make(chan ValidatorDelegationsResponse, len(respValidators.Validators)) for _, validator := range respValidators.Validators { - go func() { + go func(valAddress string) { defer wg.Done() delegations, err := queryClient.ValidatorDelegations(ctx, &staking.QueryValidatorDelegationsRequest{ - ValidatorAddr: validator.OperatorAddress, + ValidatorAddr: valAddress, Pagination: &query.PageRequest{ Limit: 1000, }, }) - if err == nil { - validatorDelegationsMap[validator.OperatorAddress] = len(delegations.DelegationResponses) - } else { - validatorDelegationsErr = err + if err != nil { + resultChan <- ValidatorDelegationsResponse{ + Validator: valAddress, + Error: err, + } + return } - }() + resultChan <- ValidatorDelegationsResponse{ + Validator: valAddress, + Delegations: len(delegations.DelegationResponses), + } + + }(validator.OperatorAddress) valResponse := Validator{ OperatorAddress: validator.OperatorAddress, @@ -77,21 +90,24 @@ func GetValidators(ctx context.Context, queryClient staking.QueryClient) ([]Vali Description: validator.Description, Commission: validator.Commission.CommissionRates.Rate.String(), } - allValidators = append(allValidators, valResponse) + validatorMap[valResponse.OperatorAddress] = valResponse } - wg.Wait() - if validatorDelegationsErr != nil { - return nil, nil, err - } + wg.Wait() + close(resultChan) - for index, validator := range allValidators { - validator.Delegators = validatorDelegationsMap[validator.OperatorAddress] - allValidators[index] = validator - validatorMap[validator.OperatorAddress] = GeneralResultToString(validator) + for result := range resultChan { + if result.Error != nil { + return nil, nil, result.Error + } else { + validator := validatorMap[result.Validator] + validator.Delegators = result.Delegations + validatorMap[result.Validator] = validator + validatorToResultStringMap[result.Validator] = GeneralResultToString(validator) + allValidators = append(allValidators, validator) + } } - - return allValidators, validatorMap, nil + return allValidators, validatorToResultStringMap, nil } func GetStakingAPR(ctx context.Context, stakingQueryClient staking.QueryClient, inflationQueryClient inflation.QueryClient) (string, error) { //get pool