From e52e7939346739ef305b47e13103cec15c1a35fa Mon Sep 17 00:00:00 2001 From: Christopher Goes Date: Wed, 23 Jan 2019 14:21:53 +0100 Subject: [PATCH] Add comments about truncation --- x/distribution/keeper/delegation.go | 3 +++ x/distribution/keeper/validator.go | 1 + 2 files changed, 4 insertions(+) diff --git a/x/distribution/keeper/delegation.go b/x/distribution/keeper/delegation.go index a29f417e7605..ac2807ef23ee 100644 --- a/x/distribution/keeper/delegation.go +++ b/x/distribution/keeper/delegation.go @@ -16,6 +16,7 @@ func (k Keeper) initializeDelegation(ctx sdk.Context, val sdk.ValAddress, del sd // calculate delegation stake in tokens // we don't store directly, so multiply delegation shares * (tokens per share) + // note: necessary to truncate so we don't allow withdrawing more rewards than owed stake := delegation.GetShares().MulTruncate(validator.GetDelegatorShareExRate()) k.SetDelegatorStartingInfo(ctx, val, del, types.NewDelegatorStartingInfo(previousPeriod, stake, uint64(ctx.BlockHeight()))) } @@ -32,6 +33,7 @@ func (k Keeper) calculateDelegationRewardsBetween(ctx sdk.Context, val sdk.Valid starting := k.GetValidatorHistoricalRewards(ctx, val.GetOperator(), startingPeriod) ending := k.GetValidatorHistoricalRewards(ctx, val.GetOperator(), endingPeriod) difference := ending.CumulativeRewardRatio.Minus(starting.CumulativeRewardRatio) + // note: necessary to truncate so we don't allow withdrawing more rewards than owed rewards = difference.MulDecTruncate(stake) return } @@ -54,6 +56,7 @@ func (k Keeper) calculateDelegationRewards(ctx sdk.Context, val sdk.Validator, d func(height uint64, event types.ValidatorSlashEvent) (stop bool) { endingPeriod := event.ValidatorPeriod rewards = rewards.Plus(k.calculateDelegationRewardsBetween(ctx, val, startingPeriod, endingPeriod, stake)) + // note: necessary to truncate so we don't allow withdrawing more rewards than owed stake = stake.MulTruncate(sdk.OneDec().Sub(event.Fraction)) startingPeriod = endingPeriod return false diff --git a/x/distribution/keeper/validator.go b/x/distribution/keeper/validator.go index aac0bbb72a72..f29e192d3444 100644 --- a/x/distribution/keeper/validator.go +++ b/x/distribution/keeper/validator.go @@ -38,6 +38,7 @@ func (k Keeper) incrementValidatorPeriod(ctx sdk.Context, val sdk.Validator) uin current = sdk.DecCoins{} } else { + // note: necessary to truncate so we don't allow withdrawing more rewards than owed current = rewards.Rewards.QuoDecTruncate(sdk.NewDecFromInt(val.GetTokens())) }