Skip to content

Commit e52e793

Browse files
committed
Add comments about truncation
1 parent 1beb1ff commit e52e793

File tree

2 files changed

+4
-0
lines changed

2 files changed

+4
-0
lines changed

x/distribution/keeper/delegation.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ func (k Keeper) initializeDelegation(ctx sdk.Context, val sdk.ValAddress, del sd
1616

1717
// calculate delegation stake in tokens
1818
// we don't store directly, so multiply delegation shares * (tokens per share)
19+
// note: necessary to truncate so we don't allow withdrawing more rewards than owed
1920
stake := delegation.GetShares().MulTruncate(validator.GetDelegatorShareExRate())
2021
k.SetDelegatorStartingInfo(ctx, val, del, types.NewDelegatorStartingInfo(previousPeriod, stake, uint64(ctx.BlockHeight())))
2122
}
@@ -32,6 +33,7 @@ func (k Keeper) calculateDelegationRewardsBetween(ctx sdk.Context, val sdk.Valid
3233
starting := k.GetValidatorHistoricalRewards(ctx, val.GetOperator(), startingPeriod)
3334
ending := k.GetValidatorHistoricalRewards(ctx, val.GetOperator(), endingPeriod)
3435
difference := ending.CumulativeRewardRatio.Minus(starting.CumulativeRewardRatio)
36+
// note: necessary to truncate so we don't allow withdrawing more rewards than owed
3537
rewards = difference.MulDecTruncate(stake)
3638
return
3739
}
@@ -54,6 +56,7 @@ func (k Keeper) calculateDelegationRewards(ctx sdk.Context, val sdk.Validator, d
5456
func(height uint64, event types.ValidatorSlashEvent) (stop bool) {
5557
endingPeriod := event.ValidatorPeriod
5658
rewards = rewards.Plus(k.calculateDelegationRewardsBetween(ctx, val, startingPeriod, endingPeriod, stake))
59+
// note: necessary to truncate so we don't allow withdrawing more rewards than owed
5760
stake = stake.MulTruncate(sdk.OneDec().Sub(event.Fraction))
5861
startingPeriod = endingPeriod
5962
return false

x/distribution/keeper/validator.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ func (k Keeper) incrementValidatorPeriod(ctx sdk.Context, val sdk.Validator) uin
3838

3939
current = sdk.DecCoins{}
4040
} else {
41+
// note: necessary to truncate so we don't allow withdrawing more rewards than owed
4142
current = rewards.Rewards.QuoDecTruncate(sdk.NewDecFromInt(val.GetTokens()))
4243
}
4344

0 commit comments

Comments
 (0)