diff --git a/testutil/sims/app_helpers.go b/testutil/sims/app_helpers.go index 61a5d5211924..feb61f7ac8c3 100644 --- a/testutil/sims/app_helpers.go +++ b/testutil/sims/app_helpers.go @@ -13,11 +13,14 @@ import ( tmtypes "github.com/tendermint/tendermint/types" dbm "github.com/tendermint/tm-db" + "github.com/cosmos/cosmos-sdk/baseapp" + "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/flags" "github.com/cosmos/cosmos-sdk/codec" codectypes "github.com/cosmos/cosmos-sdk/codec/types" cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" + cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" "github.com/cosmos/cosmos-sdk/depinject" "github.com/cosmos/cosmos-sdk/runtime" servertypes "github.com/cosmos/cosmos-sdk/server/types" @@ -243,3 +246,76 @@ func NewAppOptionsWithFlagHome(homePath string) servertypes.AppOptions { flags.FlagHome: homePath, } } + +// SignCheckDeliver checks a generated signed transaction and simulates a +// block commitment with the given transaction. A test assertion is made using +// the parameter 'expPass' against the result. A corresponding result is +// returned. +func SignCheckDeliver(txConfig client.TxConfig, ba *baseapp.BaseApp, header tmproto.Header, msgs []sdk.Msg, + chainID string, accNums, accSeqs []uint64, expSimPass, expPass bool, priv ...cryptotypes.PrivKey, +) (sdk.GasInfo, *sdk.Result, error) { + tx, err := GenSignedMockTx( + txConfig, + msgs, + sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 0)}, + DefaultGenTxGas, + chainID, + accNums, + accSeqs, + priv..., + ) + if err != nil { + return sdk.GasInfo{}, nil, err + } + + txBytes, err := txConfig.TxEncoder()(tx) + if err != nil { + return sdk.GasInfo{}, nil, err + } + + // Must simulate now as CheckTx doesn't run Msgs anymore + _, res, err := ba.Simulate(txBytes) + if expSimPass { + if err != nil { + return sdk.GasInfo{}, nil, err + } + + if res == nil { + return sdk.GasInfo{}, nil, fmt.Errorf("Simulate() returned no result") + } + } else { + if err == nil { + return sdk.GasInfo{}, nil, fmt.Errorf("Simulate() passed but should have failed") + } + + if res != nil { + return sdk.GasInfo{}, nil, fmt.Errorf("Simulate() returned a result") + } + } + + // Simulate a sending a transaction and committing a block + ba.BeginBlock(abci.RequestBeginBlock{Header: header}) + gInfo, res, err := ba.SimDeliver(txConfig.TxEncoder(), tx) + if expPass { + if err != nil { + return gInfo, nil, err + } + + if res == nil { + return gInfo, nil, fmt.Errorf("SimDeliver() returned no result") + } + } else { + if err == nil { + return gInfo, nil, fmt.Errorf("SimDeliver() passed but should have failed") + } + + if res != nil { + return gInfo, nil, fmt.Errorf("SimDeliver() returned a result") + } + } + + ba.EndBlock(abci.RequestEndBlock{}) + ba.Commit() + + return gInfo, res, err +} diff --git a/x/slashing/app_test.go b/x/slashing/app_test.go index e9ece2c690a1..d4d7f72a56ad 100644 --- a/x/slashing/app_test.go +++ b/x/slashing/app_test.go @@ -8,13 +8,17 @@ import ( abci "github.com/tendermint/tendermint/abci/types" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" + "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" - "github.com/cosmos/cosmos-sdk/simapp" + simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" sdk "github.com/cosmos/cosmos-sdk/types" - authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" - banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" + bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper" + banktestutil "github.com/cosmos/cosmos-sdk/x/bank/testutil" + "github.com/cosmos/cosmos-sdk/x/slashing/keeper" + "github.com/cosmos/cosmos-sdk/x/slashing/testutil" "github.com/cosmos/cosmos-sdk/x/slashing/types" + stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" ) @@ -26,39 +30,50 @@ var ( valAddr = sdk.AccAddress(valKey.PubKey().Address()) ) -func checkValidator(t *testing.T, app *simapp.SimApp, _ sdk.AccAddress, expFound bool) stakingtypes.Validator { - ctxCheck := app.BaseApp.NewContext(true, tmproto.Header{}) - validator, found := app.StakingKeeper.GetValidator(ctxCheck, sdk.ValAddress(addr1)) +func checkValidator(t *testing.T, ctxCheck sdk.Context, stakingKeeper *stakingkeeper.Keeper, addr sdk.ValAddress, expFound bool) stakingtypes.Validator { + validator, found := stakingKeeper.GetValidator(ctxCheck, addr) + require.Equal(t, expFound, found) return validator } -func checkValidatorSigningInfo(t *testing.T, app *simapp.SimApp, addr sdk.ConsAddress, expFound bool) types.ValidatorSigningInfo { - ctxCheck := app.BaseApp.NewContext(true, tmproto.Header{}) - signingInfo, found := app.SlashingKeeper.GetValidatorSigningInfo(ctxCheck, addr) +func checkValidatorSigningInfo(t *testing.T, ctxCheck sdk.Context, slashingKeeper keeper.Keeper, addr sdk.ConsAddress, expFound bool) types.ValidatorSigningInfo { + signingInfo, found := slashingKeeper.GetValidatorSigningInfo(ctxCheck, addr) require.Equal(t, expFound, found) return signingInfo } +// checkBalance checks the balance of an account. +func checkBalance(t *testing.T, ctxCheck sdk.Context, bankKeeper bankkeeper.Keeper, addr sdk.AccAddress, balances sdk.Coins) { + require.True(t, balances.IsEqual(bankKeeper.GetAllBalances(ctxCheck, addr))) +} + func TestSlashingMsgs(t *testing.T) { genTokens := sdk.TokensFromConsensusPower(42, sdk.DefaultPowerReduction) bondTokens := sdk.TokensFromConsensusPower(10, sdk.DefaultPowerReduction) genCoin := sdk.NewCoin(sdk.DefaultBondDenom, genTokens) bondCoin := sdk.NewCoin(sdk.DefaultBondDenom, bondTokens) - acc1 := &authtypes.BaseAccount{ - Address: addr1.String(), - } - accs := authtypes.GenesisAccounts{acc1} - balances := []banktypes.Balance{ - { - Address: addr1.String(), - Coins: sdk.Coins{genCoin}, - }, - } + var ( + txConfig client.TxConfig + bankKeeper bankkeeper.Keeper + stakingKeeper *stakingkeeper.Keeper + slashingKeeper keeper.Keeper + ) + + app, err := simtestutil.Setup(testutil.AppConfig, + &txConfig, + &bankKeeper, + &stakingKeeper, + &slashingKeeper, + ) + require.NoError(t, err) + + ctx := app.BaseApp.NewContext(false, tmproto.Header{}) - app := simapp.SetupWithGenesisAccounts(t, accs, balances...) - simapp.CheckBalance(t, app, addr1, sdk.Coins{genCoin}) + banktestutil.FundAccount(bankKeeper, ctx, addr1, sdk.Coins{genCoin}) + app.Commit() + checkBalance(t, ctx, bankKeeper, addr1, sdk.Coins{genCoin}) description := stakingtypes.NewDescription("foo_moniker", "", "", "", "") commission := stakingtypes.NewCommissionRates(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec()) @@ -69,25 +84,24 @@ func TestSlashingMsgs(t *testing.T) { require.NoError(t, err) header := tmproto.Header{Height: app.LastBlockHeight() + 1} - txGen := simapp.MakeTestEncodingConfig().TxConfig - _, _, err = simapp.SignCheckDeliver(t, txGen, app.BaseApp, header, []sdk.Msg{createValidatorMsg}, "", []uint64{0}, []uint64{0}, true, true, priv1) + _, _, err = simtestutil.SignCheckDeliver(txConfig, app.BaseApp, header, []sdk.Msg{createValidatorMsg}, simtestutil.SimAppChainID, []uint64{0}, []uint64{0}, true, true, priv1) require.NoError(t, err) - simapp.CheckBalance(t, app, addr1, sdk.Coins{genCoin.Sub(bondCoin)}) + checkBalance(t, ctx, bankKeeper, addr1, sdk.Coins{genCoin.Sub(bondCoin)}) header = tmproto.Header{Height: app.LastBlockHeight() + 1} app.BeginBlock(abci.RequestBeginBlock{Header: header}) - validator := checkValidator(t, app, addr1, true) + validator := checkValidator(t, ctx, stakingKeeper, sdk.ValAddress(addr1), true) require.Equal(t, sdk.ValAddress(addr1).String(), validator.OperatorAddress) require.Equal(t, stakingtypes.Bonded, validator.Status) require.True(sdk.IntEq(t, bondTokens, validator.BondedTokens())) unjailMsg := &types.MsgUnjail{ValidatorAddr: sdk.ValAddress(addr1).String()} - checkValidatorSigningInfo(t, app, sdk.ConsAddress(valAddr), true) + checkValidatorSigningInfo(t, ctx, slashingKeeper, sdk.ConsAddress(valAddr), true) // unjail should fail with unknown validator header = tmproto.Header{Height: app.LastBlockHeight() + 1} - _, res, err := simapp.SignCheckDeliver(t, txGen, app.BaseApp, header, []sdk.Msg{unjailMsg}, "", []uint64{0}, []uint64{1}, false, false, priv1) + _, res, err := simtestutil.SignCheckDeliver(txConfig, app.BaseApp, header, []sdk.Msg{unjailMsg}, simtestutil.SimAppChainID, []uint64{0}, []uint64{1}, false, false, priv1) require.Error(t, err) require.Nil(t, res) require.True(t, errors.Is(types.ErrValidatorNotJailed, err))