Skip to content

Commit

Permalink
fix: refactor redelegations distribution logic (#490)
Browse files Browse the repository at this point in the history
* fix: refactor redelegations distribution logic

* lint

* Update x/interchainstaking/types/rebalance_test.go

Co-authored-by: Alex Johnson <[email protected]>

* nits

* Update x/interchainstaking/types/rebalance.go

Co-authored-by: Alex Johnson <[email protected]>

* Update x/interchainstaking/types/rebalance.go

* Update x/interchainstaking/types/rebalance.go

* lint fix

---------

Co-authored-by: Alex Johnson <[email protected]>
Co-authored-by: Ajaz Ahmed Ansari <[email protected]>
  • Loading branch information
3 people authored Jul 11, 2023
1 parent dca0e3e commit 81cd188
Show file tree
Hide file tree
Showing 7 changed files with 575 additions and 123 deletions.
18 changes: 8 additions & 10 deletions x/interchainstaking/keeper/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func (k *Keeper) PrepareDelegationMessagesForShares(zone *types.Zone, coins sdk.
}

func (k *Keeper) DeterminePlanForDelegation(ctx sdk.Context, zone *types.Zone, amount sdk.Coins) (map[string]sdkmath.Int, error) {
currentAllocations, currentSum, _ := k.GetDelegationMap(ctx, zone)
currentAllocations, currentSum, _, _ := k.GetDelegationMap(ctx, zone)
targetAllocations, err := k.GetAggregateIntentOrDefault(ctx, zone)
if err != nil {
return nil, err
Expand Down Expand Up @@ -248,25 +248,23 @@ func (k *Keeper) WithdrawDelegationRewardsForResponse(ctx sdk.Context, zone *typ
return k.SubmitTx(ctx, msgs, zone.DelegationAddress, "", zone.MessagesPerTx)
}

func (k *Keeper) GetDelegationMap(ctx sdk.Context, zone *types.Zone) (out map[string]sdkmath.Int, sum sdkmath.Int, locked map[string]bool) {
func (k *Keeper) GetDelegationMap(ctx sdk.Context, zone *types.Zone) (out map[string]sdkmath.Int, sum sdkmath.Int, locked map[string]bool, lockedSum sdkmath.Int) {
out = make(map[string]sdkmath.Int)
locked = make(map[string]bool)
sum = sdk.ZeroInt()
lockedSum = sdk.ZeroInt()

k.IterateAllDelegations(ctx, zone, func(delegation types.Delegation) bool {
existing, found := out[delegation.ValidatorAddress]
if !found {
out[delegation.ValidatorAddress] = delegation.Amount.Amount
locked[delegation.ValidatorAddress] = delegation.RedelegationEnd != 0 && delegation.RedelegationEnd >= ctx.BlockTime().Unix()
} else {
out[delegation.ValidatorAddress] = existing.Add(delegation.Amount.Amount)
locked[delegation.ValidatorAddress] = locked[delegation.ValidatorAddress] || (delegation.RedelegationEnd != 0 && delegation.RedelegationEnd >= ctx.BlockTime().Unix())
out[delegation.ValidatorAddress] = delegation.Amount.Amount
if delegation.RedelegationEnd >= ctx.BlockTime().Unix() {
locked[delegation.ValidatorAddress] = true
lockedSum = lockedSum.Add(delegation.Amount.Amount)
}
sum = sum.Add(delegation.Amount.Amount)
return false
})

return out, sum, locked
return out, sum, locked, lockedSum
}

func (k *Keeper) MakePerformanceDelegation(ctx sdk.Context, zone *types.Zone, validator string) error {
Expand Down
4 changes: 2 additions & 2 deletions x/interchainstaking/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -630,12 +630,12 @@ func (k *Keeper) GetAggregateIntentOrDefault(ctx sdk.Context, z *types.Zone) (ty
}

func (k *Keeper) Rebalance(ctx sdk.Context, zone *types.Zone, epochNumber int64) error {
currentAllocations, currentSum, currentLocked := k.GetDelegationMap(ctx, zone)
currentAllocations, currentSum, currentLocked, lockedSum := k.GetDelegationMap(ctx, zone)
targetAllocations, err := k.GetAggregateIntentOrDefault(ctx, zone)
if err != nil {
return err
}
rebalances := types.DetermineAllocationsForRebalancing(currentAllocations, currentLocked, currentSum, targetAllocations, k.ZoneRedelegationRecords(ctx, zone.ChainId), k.Logger(ctx))
rebalances := types.DetermineAllocationsForRebalancing(currentAllocations, currentLocked, currentSum, lockedSum, targetAllocations, k.Logger(ctx))
msgs := make([]sdk.Msg, 0)
for _, rebalance := range rebalances {
msgs = append(msgs, &stakingtypes.MsgBeginRedelegate{DelegatorAddress: zone.DelegationAddress.Address, ValidatorSrcAddress: rebalance.Source, ValidatorDstAddress: rebalance.Target, Amount: sdk.NewCoin(zone.BaseDenom, rebalance.Amount)})
Expand Down
2 changes: 1 addition & 1 deletion x/interchainstaking/keeper/redemptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ func (k *Keeper) GCCompletedUnbondings(ctx sdk.Context, zone *types.Zone) error
}

func (k *Keeper) DeterminePlanForUndelegation(ctx sdk.Context, zone *types.Zone, amount sdk.Coins) (map[string]math.Int, error) {
currentAllocations, currentSum, _ := k.GetDelegationMap(ctx, zone)
currentAllocations, currentSum, _, _ := k.GetDelegationMap(ctx, zone)
availablePerValidator, _, err := k.GetUnlockedTokensForZone(ctx, zone)
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion x/interchainstaking/types/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (vi ValidatorIntents) Normalize() ValidatorIntents {
func DetermineAllocationsForDelegation(currentAllocations map[string]sdkmath.Int, currentSum sdkmath.Int, targetAllocations ValidatorIntents, amount sdk.Coins) map[string]sdkmath.Int {
input := amount[0].Amount
deltas := CalculateDeltas(currentAllocations, currentSum, targetAllocations)
minValue := MinDeltas(deltas)
minValue := MinDelta(deltas)
sum := sdk.ZeroInt()

// raise all deltas such that the minimum value is zero.
Expand Down
Loading

0 comments on commit 81cd188

Please sign in to comment.