Skip to content

Commit

Permalink
Simulation fixes; hopefully
Browse files Browse the repository at this point in the history
  • Loading branch information
cwgoes committed Jan 14, 2019
1 parent d9f7ace commit c9a4978
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 13 deletions.
8 changes: 5 additions & 3 deletions x/distribution/keeper/calculation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
9 changes: 0 additions & 9 deletions x/distribution/simulation/invariants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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
}
}
3 changes: 2 additions & 1 deletion x/staking/keeper/slash.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c9a4978

Please sign in to comment.