Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: speed up crisis invariant check (juno genesis) #12886

Closed
Closed
49 changes: 32 additions & 17 deletions x/distribution/keeper/invariants.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"fmt"
"sync"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/distribution/types"
Expand Down Expand Up @@ -67,36 +68,50 @@ func CanWithdrawInvariant(k Keeper) sdk.Invariant {
// cache, we don't want to write changes
ctx, _ = ctx.CacheContext()

var remaining sdk.DecCoins

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

// iterate over all validators
// get all validators so that we can iterate concurrently
// there might be a more elegant way to do this, but this seems to work pretty well
// and compared to the time the whole thing takes this iteration is negligible
Cashmaney marked this conversation as resolved.
Show resolved Hide resolved
var valList []stakingtypes.ValidatorI
k.stakingKeeper.IterateValidators(ctx, func(_ int64, val stakingtypes.ValidatorI) (stop bool) {
_, _ = k.WithdrawValidatorCommission(ctx, val.GetOperator())
valList = append(valList, val)
return false
})

delegationAddrs, ok := valDelegationAddrs[val.GetOperator().String()]
if ok {
for _, delAddr := range delegationAddrs {
if _, err := k.WithdrawDelegationRewards(ctx, delAddr, val.GetOperator()); err != nil {
panic(err)
broken := false
var remaining sdk.DecCoins
Cashmaney marked this conversation as resolved.
Show resolved Hide resolved
wg := new(sync.WaitGroup)
for _, val := range valList {
wg.Add(1)
go func(val stakingtypes.ValidatorI, wg *sync.WaitGroup) {
defer wg.Done()

_, _ = k.WithdrawValidatorCommission(ctx, val.GetOperator())

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.GetValidatorOutstandingRewardsCoins(ctx, val.GetOperator())
if len(remaining) > 0 && remaining[0].Amount.IsNegative() {
return true
}
remainingRewards := k.GetValidatorOutstandingRewardsCoins(ctx, val.GetOperator())
if len(remainingRewards) > 0 && remainingRewards[0].Amount.IsNegative() {
broken = true
remaining = k.GetValidatorOutstandingRewardsCoins(ctx, val.GetOperator())
}
}(val, wg)
Comment on lines +91 to +110

Check notice

Code scanning / CodeQL

Spawning a Go routine

Spawning a Go routine may be a possible source of non-determinism
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@marbar3778 is there an annotation we can add to comments/code to suppress such warnings?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't found one for codeql (the crypto.com version)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am checking now though

}

return false
})
wg.Wait()

broken := len(remaining) > 0 && remaining[0].Amount.IsNegative()
return sdk.FormatInvariant(types.ModuleName, "can withdraw",
fmt.Sprintf("remaining coins: %v\n", remaining)), broken
}
Expand Down