From e81dfc9ba6ac0b6d01ef22eea0f8755eeb978808 Mon Sep 17 00:00:00 2001 From: MSalopek Date: Mon, 17 Jul 2023 14:29:59 +0200 Subject: [PATCH 1/2] fix: handle liquid staked coins in sim; update sim ops --- types/simulation/account.go | 17 ++++++++++++++--- x/staking/simulation/operations.go | 3 +++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/types/simulation/account.go b/types/simulation/account.go index 5bc5bfe14dc6..1735a6a5d7fe 100644 --- a/types/simulation/account.go +++ b/types/simulation/account.go @@ -3,6 +3,7 @@ package simulation import ( "fmt" "math/rand" + "strings" "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" @@ -67,14 +68,24 @@ func FindAccount(accs []Account, address sdk.Address) (Account, bool) { // amount from the account's available balance. If the user doesn't have enough // funds for paying fees, it returns empty coins. func RandomFees(r *rand.Rand, ctx sdk.Context, spendableCoins sdk.Coins) (sdk.Coins, error) { - if spendableCoins.Empty() { + spendable := sdk.NewCoins() + // remove liquid staking denoms from spendable coins since fees cannot be paid in those denoms + valoperPrefix := fmt.Sprintf("%s%s%s", sdk.Bech32MainPrefix, sdk.PrefixValidator, sdk.PrefixOperator) + for _, coin := range spendableCoins { + if strings.Contains(coin.Denom, valoperPrefix) { + continue + } + spendable = append(spendable, coin) + } + + if spendable.Empty() { return nil, nil } - perm := r.Perm(len(spendableCoins)) + perm := r.Perm(len(spendable)) var randCoin sdk.Coin for _, index := range perm { - randCoin = spendableCoins[index] + randCoin = spendable[index] if !randCoin.Amount.IsZero() { break } diff --git a/x/staking/simulation/operations.go b/x/staking/simulation/operations.go index eb8dd643f0cd..f6814660056a 100644 --- a/x/staking/simulation/operations.go +++ b/x/staking/simulation/operations.go @@ -785,6 +785,9 @@ func SimulateMsgTokenizeShares(ak types.AccountKeeper, bk types.BankKeeper, k ke // check that tokenization would not exceed global cap params := k.GetParams(ctx) totalStaked := k.TotalBondedTokens(ctx).ToDec() + if totalStaked.IsZero() { + return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgTokenizeShares, "cannot happend - no validators bonded if stake is 0.0"), nil, nil // skip + } totalLiquidStaked := k.GetTotalLiquidStakedTokens(ctx).Add(tokenizeShareAmt).ToDec() liquidStakedPercent := totalLiquidStaked.Quo(totalStaked) if liquidStakedPercent.GT(params.GlobalLiquidStakingCap) { From 3de52586dbb607dece9859f58c91136515c67f96 Mon Sep 17 00:00:00 2001 From: MSalopek Date: Mon, 17 Jul 2023 21:14:50 +0200 Subject: [PATCH 2/2] test: use valoper bech prefix in sims --- types/simulation/account.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/types/simulation/account.go b/types/simulation/account.go index 1735a6a5d7fe..4fc6fd4dfdb2 100644 --- a/types/simulation/account.go +++ b/types/simulation/account.go @@ -70,9 +70,8 @@ func FindAccount(accs []Account, address sdk.Address) (Account, bool) { func RandomFees(r *rand.Rand, ctx sdk.Context, spendableCoins sdk.Coins) (sdk.Coins, error) { spendable := sdk.NewCoins() // remove liquid staking denoms from spendable coins since fees cannot be paid in those denoms - valoperPrefix := fmt.Sprintf("%s%s%s", sdk.Bech32MainPrefix, sdk.PrefixValidator, sdk.PrefixOperator) for _, coin := range spendableCoins { - if strings.Contains(coin.Denom, valoperPrefix) { + if strings.Contains(coin.Denom, sdk.Bech32PrefixValAddr) { continue } spendable = append(spendable, coin)