Skip to content

Commit

Permalink
Improve iterate validators dist. invariant (#3818)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderbez authored Mar 7, 2019
1 parent 006496f commit d7fc4e9
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions x/distribution/simulation/invariants.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,24 +59,30 @@ func CanWithdrawInvariant(k distr.Keeper, sk types.StakingKeeper) sdk.Invariant

var remaining sdk.DecCoins

valDelegationAddrs := make(map[string][]sdk.AccAddress)
for _, del := range sk.GetAllSDKDelegations(ctx) {
valAddr := del.GetValidatorAddr().String()
valDelegationAddrs[valAddr] = append(valDelegationAddrs[valAddr], del.GetDelegatorAddr())
}

// iterate over all validators
sk.IterateValidators(ctx, func(_ int64, val sdk.Validator) (stop bool) {
_ = k.WithdrawValidatorCommission(ctx, val.GetOperator())
// TODO fetch delegations just for the validator, requires sdk.ValidatorSet change
// iterate over all current delegations, withdraw rewards
dels := sk.GetAllSDKDelegations(ctx)
for _, delegation := range dels {
if delegation.GetValidatorAddr().String() == val.GetOperator().String() {
err := k.WithdrawDelegationRewards(ctx, delegation.GetDelegatorAddr(), delegation.GetValidatorAddr())
if err != nil {

delegationAddrs, ok := valDelegationAddrs[val.GetOperator().String()]
if ok {
for _, delAddr := range delegationAddrs {
if err := k.WithdrawDelegationRewards(ctx, delAddr, val.GetOperator()); err != nil {
panic(err)
}
}
}

remaining = k.GetValidatorOutstandingRewards(ctx, val.GetOperator())
if len(remaining) > 0 && remaining[0].Amount.LT(sdk.ZeroDec()) {
return true
}

return false
})

Expand Down

0 comments on commit d7fc4e9

Please sign in to comment.