Skip to content

Commit

Permalink
Use LTE instead of Equal (cosmos#4503)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderbez authored and rigelrozanski committed Jun 6, 2019
1 parent 3180e68 commit 86da70d
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions x/distribution/keeper/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"+
Expand Down

0 comments on commit 86da70d

Please sign in to comment.