From c9a4978680fb8367a5f542f6835e4cd035138352 Mon Sep 17 00:00:00 2001 From: Christopher Goes Date: Mon, 14 Jan 2019 16:41:47 +0100 Subject: [PATCH] Simulation fixes; hopefully --- x/distribution/keeper/calculation.go | 8 +++++--- x/distribution/simulation/invariants.go | 9 --------- x/staking/keeper/slash.go | 3 ++- 3 files changed, 7 insertions(+), 13 deletions(-) diff --git a/x/distribution/keeper/calculation.go b/x/distribution/keeper/calculation.go index 661b9526cbdf..7dd999127529 100644 --- a/x/distribution/keeper/calculation.go +++ b/x/distribution/keeper/calculation.go @@ -99,10 +99,12 @@ func (k Keeper) calculateDelegationRewards(ctx sdk.Context, val sdk.Validator, d stake := startingInfo.Stake // iterate through slashes and withdraw with calculated staking for sub-intervals + // these offsets are dependent on *when* slashes happen - namely, in BeginBlock, after rewards are allocated... + // ... so we don't reduce stake for slashes which happened in the *first* block, because the delegation wouldn't have existed startingHeight := startingInfo.Height + 1 - endingHeight := uint64(ctx.BlockHeight()) - // if we're still in the same block, no slashes can yet have happened - if endingHeight > startingHeight { + // ... or slashes which happened in *this* block, since they would have happened after reward allocation + endingHeight := uint64(ctx.BlockHeight()) - 1 + if endingHeight >= startingHeight { k.IterateValidatorSlashEventsBetween(ctx, del.GetValidatorAddr(), startingHeight, endingHeight, func(height uint64, event types.ValidatorSlashEvent) (stop bool) { endingPeriod := event.ValidatorPeriod - 1 rewards = rewards.Plus(k.calculateDelegationRewardsBetween(ctx, val, startingPeriod, endingPeriod, stake)) diff --git a/x/distribution/simulation/invariants.go b/x/distribution/simulation/invariants.go index dccd08478e96..4c46d67dbb6b 100644 --- a/x/distribution/simulation/invariants.go +++ b/x/distribution/simulation/invariants.go @@ -42,8 +42,6 @@ func CanWithdrawInvariant(k distr.Keeper, sk staking.Keeper) simulation.Invarian // cache, we don't want to write changes ctx, _ = ctx.CacheContext() - outstanding := k.GetOutstandingRewards(ctx) - // iterate over all bonded validators, withdraw commission sk.IterateValidators(ctx, func(_ int64, val sdk.Validator) (stop bool) { _ = k.WithdrawValidatorCommission(ctx, val.GetOperator()) @@ -62,13 +60,6 @@ func CanWithdrawInvariant(k distr.Keeper, sk staking.Keeper) simulation.Invarian return fmt.Errorf("Negative remaining coins: %v", remaining) } - fmt.Printf("Outstanding: %v, remaining %v\n", outstanding, remaining) - - // bound at 0.01% error - if len(remaining) > 0 && remaining[0].Amount.Quo(outstanding[0].Amount).GT(sdk.OneDec().Quo(sdk.NewDec(10000))) { - return fmt.Errorf("Error too high - outstanding %v, remaining %v", outstanding, remaining) - } - return nil } } diff --git a/x/staking/keeper/slash.go b/x/staking/keeper/slash.go index 81f53ccac767..7e6aad97b9c4 100644 --- a/x/staking/keeper/slash.go +++ b/x/staking/keeper/slash.go @@ -55,7 +55,8 @@ func (k Keeper) Slash(ctx sdk.Context, consAddr sdk.ConsAddress, infractionHeigh operatorAddress := validator.GetOperator() k.BeforeValidatorModified(ctx, operatorAddress) - k.BeforeValidatorSlashed(ctx, operatorAddress, slashFactor) + // we need to calculate the *effective* slash fraction for distribution + k.BeforeValidatorSlashed(ctx, operatorAddress, slashAmountDec.Quo(sdk.NewDecFromInt(validator.GetTokens()))) // Track remaining slash amount for the validator // This will decrease when we slash unbondings and