Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: migrate slashing tests #12362

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions testutil/sims/app_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
68 changes: 41 additions & 27 deletions x/slashing/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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())
Expand All @@ -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))
Expand Down