From 86da70d8aa6b13cd27c0d759313ffcb0b9406150 Mon Sep 17 00:00:00 2001 From: Alexander Bezobchuk Date: Thu, 6 Jun 2019 16:32:14 -0400 Subject: [PATCH] Use LTE instead of Equal (#4503) --- x/distribution/keeper/delegation.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/x/distribution/keeper/delegation.go b/x/distribution/keeper/delegation.go index 07a43a4889..9755bc5715 100644 --- a/x/distribution/keeper/delegation.go +++ b/x/distribution/keeper/delegation.go @@ -98,14 +98,16 @@ func (k Keeper) calculateDelegationRewards(ctx sdk.Context, val exported.Validat // when multiplied by slash fractions (see above). We could only use equals if // we had arbitrary-precision rationals. currentStake := val.TokensFromShares(del.GetShares()) - if stake.GT(currentStake) { + if stake.GT(currentStake) { // Account for rounding inconsistencies between: + // // currentStake: calculated as in staking with a single computation // stake: calculated as an accumulation of stake // calculations across validator's distribution periods + // // These inconsistencies are due to differing order of operations which - // will inevitably have different accumulated rounding and may lead to + // will inevitably have different accumulated rounding and may lead to // the smallest decimal place being one greater in stake than // currentStake. When we calculated slashing by period, even if we // round down for each slash fraction, it's possible due to how much is @@ -118,7 +120,8 @@ func (k Keeper) calculateDelegationRewards(ctx sdk.Context, val exported.Validat // A small amount of this error is tolerated and corrected for, // however any greater amount should be considered a breach in expected // behaviour. - if stake.Equal(currentStake.Add(sdk.SmallestDec().MulInt64(3))) { + marginOfErr := sdk.SmallestDec().MulInt64(3) + if stake.LTE(currentStake.Add(marginOfErr)) { stake = currentStake } else { panic(fmt.Sprintf("calculated final stake for delegator %s greater than current stake"+