Skip to content

Commit

Permalink
fix: simulation failures (#12858)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
kocubinski and tac0turtle authored Aug 8, 2022
1 parent e2d6cbd commit 8f9a88c
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
18 changes: 14 additions & 4 deletions store/types/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion x/gov/simulation/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
7 changes: 5 additions & 2 deletions x/staking/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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 {
Expand Down
7 changes: 7 additions & 0 deletions x/staking/simulation/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, &paramsA)
cdc.MustUnmarshal(kvB.Value, &paramsB)

return fmt.Sprintf("%v\n%v", paramsA, paramsB)
default:
panic(fmt.Sprintf("invalid staking key prefix %X", kvA.Key[:1]))
}
Expand Down

0 comments on commit 8f9a88c

Please sign in to comment.