From e9585b76ca67e40183064366239b98e144678198 Mon Sep 17 00:00:00 2001 From: atheesh Date: Mon, 27 Jun 2022 15:41:45 +0530 Subject: [PATCH 1/2] migrate slashing tests --- testutil/sims/app_helpers.go | 56 ++++++++++++++++++++++++++++++ x/slashing/app_test.go | 67 +++++++++++++++++++++--------------- 2 files changed, 96 insertions(+), 27 deletions(-) diff --git a/testutil/sims/app_helpers.go b/testutil/sims/app_helpers.go index 61a5d5211924..fb1eebee979a 100644 --- a/testutil/sims/app_helpers.go +++ b/testutil/sims/app_helpers.go @@ -4,8 +4,10 @@ import ( "context" "encoding/json" "fmt" + "testing" "time" + "github.com/stretchr/testify/require" abci "github.com/tendermint/tendermint/abci/types" tmjson "github.com/tendermint/tendermint/libs/json" "github.com/tendermint/tendermint/libs/log" @@ -13,11 +15,14 @@ import ( tmtypes "github.com/tendermint/tendermint/types" dbm "github.com/tendermint/tm-db" + bam "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 +248,54 @@ 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( + t *testing.T, txCfg client.TxConfig, app *bam.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( + txCfg, + msgs, + sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 0)}, + DefaultGenTxGas, + chainID, + accNums, + accSeqs, + priv..., + ) + require.NoError(t, err) + txBytes, err := txCfg.TxEncoder()(tx) + require.Nil(t, err) + + // Must simulate now as CheckTx doesn't run Msgs anymore + _, res, err := app.Simulate(txBytes) + + if expSimPass { + require.NoError(t, err) + require.NotNil(t, res) + } else { + require.Error(t, err) + require.Nil(t, res) + } + + // Simulate a sending a transaction and committing a block + app.BeginBlock(abci.RequestBeginBlock{Header: header}) + gInfo, res, err := app.SimDeliver(txCfg.TxEncoder(), tx) + + if expPass { + require.NoError(t, err) + require.NotNil(t, res) + } else { + require.Error(t, err) + require.Nil(t, res) + } + + app.EndBlock(abci.RequestEndBlock{}) + app.Commit() + + return gInfo, res, err +} diff --git a/x/slashing/app_test.go b/x/slashing/app_test.go index e9ece2c690a1..7ef3c6ae73f8 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,49 @@ 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}) + checkBalance(t, ctx, bankKeeper, addr1, sdk.Coins{genCoin}) description := stakingtypes.NewDescription("foo_moniker", "", "", "", "") commission := stakingtypes.NewCommissionRates(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec()) @@ -69,25 +83,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(t, txConfig, app.BaseApp, header, []sdk.Msg{createValidatorMsg}, "", []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(t, txConfig, app.BaseApp, header, []sdk.Msg{unjailMsg}, "", []uint64{0}, []uint64{1}, false, false, priv1) require.Error(t, err) require.Nil(t, res) require.True(t, errors.Is(types.ErrValidatorNotJailed, err)) From 84b009cd62babe74649609094bdbeee93e93e1ca Mon Sep 17 00:00:00 2001 From: atheesh Date: Tue, 28 Jun 2022 15:46:08 +0530 Subject: [PATCH 2/2] update `SignCheckDeliver` --- testutil/sims/app_helpers.go | 68 +++++++++++++++++++++++------------- x/slashing/app_test.go | 5 +-- 2 files changed, 47 insertions(+), 26 deletions(-) diff --git a/testutil/sims/app_helpers.go b/testutil/sims/app_helpers.go index fb1eebee979a..feb61f7ac8c3 100644 --- a/testutil/sims/app_helpers.go +++ b/testutil/sims/app_helpers.go @@ -4,10 +4,8 @@ import ( "context" "encoding/json" "fmt" - "testing" "time" - "github.com/stretchr/testify/require" abci "github.com/tendermint/tendermint/abci/types" tmjson "github.com/tendermint/tendermint/libs/json" "github.com/tendermint/tendermint/libs/log" @@ -15,7 +13,7 @@ import ( tmtypes "github.com/tendermint/tendermint/types" dbm "github.com/tendermint/tm-db" - bam "github.com/cosmos/cosmos-sdk/baseapp" + "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" @@ -253,12 +251,11 @@ func NewAppOptionsWithFlagHome(homePath string) servertypes.AppOptions { // 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( - t *testing.T, txCfg client.TxConfig, app *bam.BaseApp, header tmproto.Header, msgs []sdk.Msg, +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( - txCfg, + txConfig, msgs, sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 0)}, DefaultGenTxGas, @@ -267,35 +264,58 @@ func SignCheckDeliver( accSeqs, priv..., ) - require.NoError(t, err) - txBytes, err := txCfg.TxEncoder()(tx) - require.Nil(t, err) + if err != nil { + return sdk.GasInfo{}, nil, err + } - // Must simulate now as CheckTx doesn't run Msgs anymore - _, res, err := app.Simulate(txBytes) + 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 { - require.NoError(t, err) - require.NotNil(t, res) + if err != nil { + return sdk.GasInfo{}, nil, err + } + + if res == nil { + return sdk.GasInfo{}, nil, fmt.Errorf("Simulate() returned no result") + } } else { - require.Error(t, err) - require.Nil(t, res) + 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 - app.BeginBlock(abci.RequestBeginBlock{Header: header}) - gInfo, res, err := app.SimDeliver(txCfg.TxEncoder(), tx) - + ba.BeginBlock(abci.RequestBeginBlock{Header: header}) + gInfo, res, err := ba.SimDeliver(txConfig.TxEncoder(), tx) if expPass { - require.NoError(t, err) - require.NotNil(t, res) + if err != nil { + return gInfo, nil, err + } + + if res == nil { + return gInfo, nil, fmt.Errorf("SimDeliver() returned no result") + } } else { - require.Error(t, err) - require.Nil(t, res) + 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") + } } - app.EndBlock(abci.RequestEndBlock{}) - app.Commit() + 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 7ef3c6ae73f8..d4d7f72a56ad 100644 --- a/x/slashing/app_test.go +++ b/x/slashing/app_test.go @@ -72,6 +72,7 @@ func TestSlashingMsgs(t *testing.T) { ctx := app.BaseApp.NewContext(false, tmproto.Header{}) banktestutil.FundAccount(bankKeeper, ctx, addr1, sdk.Coins{genCoin}) + app.Commit() checkBalance(t, ctx, bankKeeper, addr1, sdk.Coins{genCoin}) description := stakingtypes.NewDescription("foo_moniker", "", "", "", "") @@ -83,7 +84,7 @@ func TestSlashingMsgs(t *testing.T) { require.NoError(t, err) header := tmproto.Header{Height: app.LastBlockHeight() + 1} - _, _, err = simtestutil.SignCheckDeliver(t, txConfig, 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) checkBalance(t, ctx, bankKeeper, addr1, sdk.Coins{genCoin.Sub(bondCoin)}) @@ -100,7 +101,7 @@ func TestSlashingMsgs(t *testing.T) { // unjail should fail with unknown validator header = tmproto.Header{Height: app.LastBlockHeight() + 1} - _, res, err := simtestutil.SignCheckDeliver(t, txConfig, 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))