Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
MissingNO57 committed Oct 16, 2024
1 parent 8ff02d7 commit 410fcae
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 35 deletions.
10 changes: 5 additions & 5 deletions app/upgrade_cudos.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down
49 changes: 44 additions & 5 deletions app/upgrade_cudos_distribution.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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)

Expand Down
36 changes: 11 additions & 25 deletions cmd/fetchd/cmd/cudos_merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/fetchai/fetchd/app"
"github.com/spf13/cobra"
"github.com/tendermint/tendermint/libs/log"
"math"
"os"
)

Expand Down Expand Up @@ -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
}
}

}
}

Expand Down

0 comments on commit 410fcae

Please sign in to comment.