From 410fcae4077058d705ffe15e04fccb712adcd82a Mon Sep 17 00:00:00 2001 From: Jiri Date: Wed, 16 Oct 2024 11:20:53 +0200 Subject: [PATCH] Refactor --- app/upgrade_cudos.go | 10 +++---- app/upgrade_cudos_distribution.go | 49 +++++++++++++++++++++++++++---- cmd/fetchd/cmd/cudos_merge.go | 36 +++++++---------------- 3 files changed, 60 insertions(+), 35 deletions(-) diff --git a/app/upgrade_cudos.go b/app/upgrade_cudos.go index 7bea9a1b..c4dfed17 100644 --- a/app/upgrade_cudos.go +++ b/app/upgrade_cudos.go @@ -345,7 +345,7 @@ func ParseGenesisData(jsonData map[string]interface{}, genDoc *tmtypes.GenesisDo return nil, fmt.Errorf("failed to get unbonding delegations map: %w", err) } - distributionInfo, err := parseGenesisDistribution(jsonData, genesisData.Accounts) + distributionInfo, err := parseGenesisDistribution(jsonData, genesisData.Accounts, genesisData.Validators) if err != nil { return nil, fmt.Errorf("failed to get distribution module map: %w", err) } @@ -639,7 +639,7 @@ func parseGenesisDelegations(validators *OrderedMap[string, *ValidatorInfo], con func parseGenesisUnbondingDelegations(validators *OrderedMap[string, *ValidatorInfo], contracts *OrderedMap[string, *ContractInfo], cudosCfg *CudosMergeConfig) (*OrderedMap[string, *OrderedMap[string, sdk.Int]], error) { // Handle delegations - unbondedDelegatedBalanceMap := NewOrderedMap[string, *OrderedMap[string, sdk.Int]]() + unbondingDelegatedBalanceMap := NewOrderedMap[string, *OrderedMap[string, sdk.Int]]() for i := range validators.Iterate() { validatorOperatorAddress, validator := i.Key, i.Value @@ -664,14 +664,14 @@ func parseGenesisUnbondingDelegations(validators *OrderedMap[string, *ValidatorI } // Store delegation to delegated map - resolvedDelegatorMap, _ := unbondedDelegatedBalanceMap.GetOrSetDefault(resolvedDelegatorAddress, NewOrderedMap[string, sdk.Int]()) + resolvedDelegatorMap, _ := unbondingDelegatedBalanceMap.GetOrSetDefault(resolvedDelegatorAddress, NewOrderedMap[string, sdk.Int]()) resolvedDelegator, _ := resolvedDelegatorMap.GetOrSetDefault(validatorOperatorAddress, sdk.NewInt(0)) resolvedDelegatorMap.Set(validatorOperatorAddress, resolvedDelegator.Add(delegatorTokens)) - unbondedDelegatedBalanceMap.Set(resolvedDelegatorAddress, resolvedDelegatorMap) + unbondingDelegatedBalanceMap.Set(resolvedDelegatorAddress, resolvedDelegatorMap) } } - return unbondedDelegatedBalanceMap, nil + return unbondingDelegatedBalanceMap, nil } type DelegationInfo struct { diff --git a/app/upgrade_cudos_distribution.go b/app/upgrade_cudos_distribution.go index cac16832..95be0942 100644 --- a/app/upgrade_cudos_distribution.go +++ b/app/upgrade_cudos_distribution.go @@ -48,8 +48,11 @@ type DistributionInfo struct { DelegatorStartingInfos *OrderedMap[string, *OrderedMap[string, *DelegatorStartingInfo]] // validator_addr -> delegator_addr -> starting_info DelegatorWithdrawInfos *OrderedMap[string, string] // delegator_address -> withdraw_address - ValidatorSlashEvents *OrderedMap[string, *OrderedMap[uint64, *ValidatorSlashEvent]] // validatior_address -> height -> validator_slash_event + ValidatorSlashEvents *OrderedMap[string, *OrderedMap[uint64, *ValidatorSlashEvent]] // validator_address -> height -> validator_slash_event DistributionModuleAccountAddress string + + // Aggregated values + Rewards *OrderedMap[string, *OrderedMap[string, sdk.DecCoins]] // delegator_addr -> validator_addr -> reward } func parseDelegatorStartingInfos(distribution map[string]interface{}) (*OrderedMap[string, *OrderedMap[string, *DelegatorStartingInfo]], error) { @@ -242,7 +245,38 @@ func parseDelegatorWithdrawInfos(distribution map[string]interface{}) (*OrderedM return delegatorWithdrawInfos, nil } -func parseGenesisDistribution(jsonData map[string]interface{}, genesisAccounts *OrderedMap[string, *AccountInfo]) (*DistributionInfo, error) { +func aggregateRewards(distributionInfo *DistributionInfo, validators *OrderedMap[string, *ValidatorInfo]) error { + blockHeight := uint64(math.MaxUint64) + + if distributionInfo.Rewards == nil { + distributionInfo.Rewards = NewOrderedMap[string, *OrderedMap[string, sdk.DecCoins]]() + } + + // Withdraw all delegation rewards + for i := range distributionInfo.DelegatorStartingInfos.Iterate() { + validatorOperatorAddr, delegatorStartInfo := i.Key, i.Value + validator := validators.MustGet(validatorOperatorAddr) + + endingPeriod := UpdateValidatorData(distributionInfo, validator) + + for _, delegatorAddr := range delegatorStartInfo.Keys() { + delegation := validator.Delegations.MustGet(delegatorAddr) + + rewardsRaw, err := CalculateDelegationRewards(blockHeight, distributionInfo, validator, delegation, endingPeriod) + if err != nil { + return err + } + + delegatorRewards, _ := distributionInfo.Rewards.GetOrSetDefault(delegatorAddr, NewOrderedMap[string, sdk.DecCoins]()) + delegatorRewards.SetNew(validatorOperatorAddr, rewardsRaw) + } + + } + + return nil +} + +func parseGenesisDistribution(jsonData map[string]interface{}, genesisAccounts *OrderedMap[string, *AccountInfo], validators *OrderedMap[string, *ValidatorInfo]) (*DistributionInfo, error) { distribution := jsonData[distributiontypes.ModuleName].(map[string]interface{}) distributionInfo := DistributionInfo{} var err error @@ -292,6 +326,11 @@ func parseGenesisDistribution(jsonData map[string]interface{}, genesisAccounts * return nil, err } + err = aggregateRewards(&distributionInfo, validators) + if err != nil { + return nil, err + } + return &distributionInfo, nil } @@ -327,10 +366,10 @@ func withdrawGenesisDistributionRewards(logger log.Logger, genesisData *GenesisD blockHeight := uint64(math.MaxUint64) // Withdraw all delegation rewards - for _, validatorOpertorAddr := range genesisData.DistributionInfo.DelegatorStartingInfos.Keys() { - validator := genesisData.Validators.MustGet(validatorOpertorAddr) + for _, validatorOperatorAddr := range genesisData.DistributionInfo.DelegatorStartingInfos.Keys() { + validator := genesisData.Validators.MustGet(validatorOperatorAddr) - delegatorStartInfo := genesisData.DistributionInfo.DelegatorStartingInfos.MustGet(validatorOpertorAddr) + delegatorStartInfo := genesisData.DistributionInfo.DelegatorStartingInfos.MustGet(validatorOperatorAddr) endingPeriod := UpdateValidatorData(genesisData.DistributionInfo, validator) diff --git a/cmd/fetchd/cmd/cudos_merge.go b/cmd/fetchd/cmd/cudos_merge.go index 9592481b..5c427565 100644 --- a/cmd/fetchd/cmd/cudos_merge.go +++ b/cmd/fetchd/cmd/cudos_merge.go @@ -10,7 +10,6 @@ import ( "github.com/fetchai/fetchd/app" "github.com/spf13/cobra" "github.com/tendermint/tendermint/libs/log" - "math" "os" ) @@ -391,33 +390,20 @@ func printAccInfo(genesisData *app.GenesisData, address string, ctx client.Conte if err != nil { return err } - blockHeight := uint64(math.MaxUint64) - for _, validatorOpertorAddr := range genesisData.DistributionInfo.DelegatorStartingInfos.Keys() { - validator := genesisData.Validators.MustGet(validatorOpertorAddr) - delegatorStartInfo := genesisData.DistributionInfo.DelegatorStartingInfos.MustGet(validatorOpertorAddr) + if DelegatorRewards, exists := genesisData.DistributionInfo.Rewards.Get(address); exists { + for j := range DelegatorRewards.Iterate() { + validatorOperatorAddr, rewardDecAmount := j.Key, j.Value + rewardAmount, _ := rewardDecAmount.TruncateDecimal() + if !rewardAmount.IsZero() { + totalAvailableBalance = totalAvailableBalance.Add(rewardAmount...) - endingPeriod := app.UpdateValidatorData(genesisData.DistributionInfo, validator) - - if !delegatorStartInfo.Has(address) { - continue - } - delegation := validator.Delegations.MustGet(address) - - rewardsRaw, err := app.CalculateDelegationRewards(blockHeight, genesisData.DistributionInfo, validator, delegation, endingPeriod) - if err != nil { - return err - } - reward, _ := rewardsRaw.TruncateDecimal() - - if !reward.IsZero() { - - totalAvailableBalance = totalAvailableBalance.Add(reward...) - - err = ctx.PrintString(fmt.Sprintf("%s, %s\n", validatorOpertorAddr, reward)) - if err != nil { - return err + err = ctx.PrintString(fmt.Sprintf("%s, %s\n", validatorOperatorAddr, rewardAmount)) + if err != nil { + return err + } } + } }