From 8f9a88cf228473f73064a8021c58dfb25edf528a Mon Sep 17 00:00:00 2001 From: Matt Kocubinski Date: Mon, 8 Aug 2022 16:48:26 -0500 Subject: [PATCH] fix: simulation failures (#12858) * fix: store compare bug causing simulation failures - handle err in staking/keeper/InitGenesis SetParams - handle Params type text render in staking * fix bug in x/gov simulation operations Co-authored-by: Marko --- store/types/utils.go | 18 ++++++++++++++---- x/gov/simulation/operations.go | 2 +- x/staking/keeper/genesis.go | 7 +++++-- x/staking/simulation/decoder.go | 7 +++++++ 4 files changed, 27 insertions(+), 7 deletions(-) diff --git a/store/types/utils.go b/store/types/utils.go index f0cf469871d9..7b69dacca30f 100644 --- a/store/types/utils.go +++ b/store/types/utils.go @@ -41,21 +41,31 @@ func DiffKVStores(a KVStore, b KVStore, prefixesToSkip [][]byte) (kvAs, kvBs []k if iterB.Valid() { kvB = kv.Pair{Key: iterB.Key(), Value: iterB.Value()} - - iterB.Next() } compareValue := true for _, prefix := range prefixesToSkip { // Skip value comparison if we matched a prefix - if bytes.HasPrefix(kvA.Key, prefix) || bytes.HasPrefix(kvB.Key, prefix) { + if bytes.HasPrefix(kvA.Key, prefix) { compareValue = false break } } - if compareValue && (!bytes.Equal(kvA.Key, kvB.Key) || !bytes.Equal(kvA.Value, kvB.Value)) { + if !compareValue { + // We're skipping this key due to an exclusion prefix. If it's present in B, iterate past it. If it's + // absent don't iterate. + if bytes.Equal(kvA.Key, kvB.Key) { + iterB.Next() + } + continue + } + + // always iterate B when comparing + iterB.Next() + + if !bytes.Equal(kvA.Key, kvB.Key) || !bytes.Equal(kvA.Value, kvB.Value) { kvAs = append(kvAs, kvA) kvBs = append(kvBs, kvB) } diff --git a/x/gov/simulation/operations.go b/x/gov/simulation/operations.go index 82839750d7c5..2bca30c4c12a 100644 --- a/x/gov/simulation/operations.go +++ b/x/gov/simulation/operations.go @@ -412,7 +412,7 @@ func randomDeposit( return nil, false, err } - minAmount = sdk.NewDecFromInt(minDepositAmount).Mul(minDepositPercent).RoundInt() + minAmount = sdk.NewDecFromInt(minDepositAmount).Mul(minDepositPercent).TruncateInt() } amount, err := simtypes.RandPositiveInt(r, minDepositAmount.Sub(minAmount)) diff --git a/x/staking/keeper/genesis.go b/x/staking/keeper/genesis.go index 908151c1d762..b990b600c5d8 100644 --- a/x/staking/keeper/genesis.go +++ b/x/staking/keeper/genesis.go @@ -3,9 +3,10 @@ package keeper import ( "fmt" - "cosmossdk.io/math" abci "github.com/tendermint/tendermint/abci/types" + "cosmossdk.io/math" + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/staking/types" ) @@ -26,7 +27,9 @@ func (k Keeper) InitGenesis(ctx sdk.Context, data *types.GenesisState) (res []ab // genesis.json are in block 0. ctx = ctx.WithBlockHeight(1 - sdk.ValidatorUpdateDelay) - k.SetParams(ctx, data.Params) + if err := k.SetParams(ctx, data.Params); err != nil { + panic(err) + } k.SetLastTotalPower(ctx, data.LastTotalPower) for _, validator := range data.Validators { diff --git a/x/staking/simulation/decoder.go b/x/staking/simulation/decoder.go index eec0ce22a01a..7fd3aea49d61 100644 --- a/x/staking/simulation/decoder.go +++ b/x/staking/simulation/decoder.go @@ -57,6 +57,13 @@ func NewDecodeStore(cdc codec.Codec) func(kvA, kvB kv.Pair) string { cdc.MustUnmarshal(kvB.Value, &redB) return fmt.Sprintf("%v\n%v", redA, redB) + case bytes.Equal(kvA.Key[:1], types.ParamsKey): + var paramsA, paramsB types.Params + + cdc.MustUnmarshal(kvA.Value, ¶msA) + cdc.MustUnmarshal(kvB.Value, ¶msB) + + return fmt.Sprintf("%v\n%v", paramsA, paramsB) default: panic(fmt.Sprintf("invalid staking key prefix %X", kvA.Key[:1])) }