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

test: fix x/slashing operations test #12346

Merged
merged 7 commits into from
Jun 27, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
66 changes: 31 additions & 35 deletions x/slashing/simulation/operations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"testing"
"time"

"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
abci "github.com/tendermint/tendermint/abci/types"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
Expand Down Expand Up @@ -34,6 +33,7 @@ import (
type SimTestSuite struct {
suite.Suite

r *rand.Rand
ctx sdk.Context

app *runtime.App
Expand Down Expand Up @@ -68,29 +68,30 @@ func (suite *SimTestSuite) SetupTest() {
suite.ctx = app.BaseApp.NewContext(false, tmproto.Header{})

s := rand.NewSource(1)
r := rand.New(s)
accounts := simtypes.RandomAccounts(r, 3)
suite.r = rand.New(s)
accounts := simtypes.RandomAccounts(suite.r, 3)
suite.accs = accounts

ctx := app.BaseApp.NewContext(false, tmproto.Header{})
initAmt := suite.stakingKeeper.TokensFromConsensusPower(ctx, 200)
initAmt := suite.stakingKeeper.TokensFromConsensusPower(suite.ctx, 200)
initCoins := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initAmt))

// add coins to the accounts
for _, account := range accounts {
acc := suite.accountKeeper.NewAccountWithAddress(ctx, account.Address)
suite.accountKeeper.SetAccount(ctx, acc)
suite.Require().NoError(banktestutil.FundAccount(suite.bankKeeper, ctx, account.Address, initCoins))
for _, account := range suite.accs {
acc := suite.accountKeeper.NewAccountWithAddress(suite.ctx, account.Address)
suite.accountKeeper.SetAccount(suite.ctx, acc)
suite.Require().NoError(banktestutil.FundAccount(suite.bankKeeper, suite.ctx, account.Address, initCoins))
}

suite.mintKeeper.SetParams(ctx, minttypes.DefaultParams())
suite.mintKeeper.SetMinter(ctx, minttypes.DefaultInitialMinter())
suite.accs = accounts
suite.mintKeeper.SetParams(suite.ctx, minttypes.DefaultParams())
suite.mintKeeper.SetMinter(suite.ctx, minttypes.DefaultInitialMinter())
}

func TestSimTestSuite(t *testing.T) {
suite.Run(t, new(SimTestSuite))
}

// TestWeightedOperations tests the weights of the operations.
func (suite *SimTestSuite) TestWeightedOperations(t *testing.T) {
s := rand.NewSource(1)
r := rand.New(s)
func (suite *SimTestSuite) TestWeightedOperations() {
app, ctx, accs := suite.app, suite.ctx, suite.accs
ctx.WithChainID("test-chain")

Expand All @@ -105,38 +106,33 @@ func (suite *SimTestSuite) TestWeightedOperations(t *testing.T) {

weightesOps := simulation.WeightedOperations(appParams, cdc, suite.accountKeeper, suite.bankKeeper, suite.slashingKeeper, suite.stakingKeeper)
for i, w := range weightesOps {
operationMsg, _, err := w.Op()(r, app.BaseApp, ctx, accs, ctx.ChainID())
require.NoError(t, err)
operationMsg, _, err := w.Op()(suite.r, app.BaseApp, ctx, accs, ctx.ChainID())
suite.Require().NoError(err)

// the following checks are very much dependent from the ordering of the output given
// by WeightedOperations. if the ordering in WeightedOperations changes some tests
// will fail
suite.Require().Equal(t, expected[i].weight, w.Weight(), "weight should be the same")
suite.Require().Equal(t, expected[i].opMsgRoute, operationMsg.Route, "route should be the same")
suite.Require().Equal(t, expected[i].opMsgName, operationMsg.Name, "operation Msg name should be the same")
suite.Require().Equal(expected[i].weight, w.Weight(), "weight should be the same")
suite.Require().Equal(expected[i].opMsgRoute, operationMsg.Route, "route should be the same")
suite.Require().Equal(expected[i].opMsgName, operationMsg.Name, "operation Msg name should be the same")
}
}

// TestSimulateMsgUnjail tests the normal scenario of a valid message of type types.MsgUnjail.
// Abonormal scenarios, where the message is created by an errors, are not tested here.
func (suite *SimTestSuite) TestSimulateMsgUnjail(t *testing.T) {
// setup 3 accounts
s := rand.NewSource(5)
r := rand.New(s)
func (suite *SimTestSuite) TestSimulateMsgUnjail() {
app, ctx, accounts := suite.app, suite.ctx, suite.accs

blockTime := time.Now().UTC()
ctx = ctx.WithBlockTime(blockTime)

// remove genesis validator account
accounts = accounts[1:]

// setup accounts[0] as validator0
validator0 := suite.getTestingValidator0(ctx, accounts)

// setup validator0 by consensus address
suite.stakingKeeper.SetValidatorByConsAddr(ctx, validator0)
val0ConsAddress, err := validator0.GetConsAddr()
require.NoError(t, err)
suite.Require().NoError(err)
info := types.NewValidatorSigningInfo(val0ConsAddress, int64(4), int64(3),
time.Unix(2, 0), false, int64(10))
suite.slashingKeeper.SetValidatorSigningInfo(ctx, val0ConsAddress, info)
Expand All @@ -148,7 +144,7 @@ func (suite *SimTestSuite) TestSimulateMsgUnjail(t *testing.T) {
delTokens := suite.stakingKeeper.TokensFromConsensusPower(ctx, 2)
validator0, issuedShares := validator0.AddTokensFromDel(delTokens)
val0AccAddress, err := sdk.ValAddressFromBech32(validator0.OperatorAddress)
require.NoError(t, err)
suite.Require().NoError(err)
selfDelegation := stakingtypes.NewDelegation(val0AccAddress.Bytes(), validator0.GetOperator(), issuedShares)
suite.stakingKeeper.SetDelegation(ctx, selfDelegation)
suite.distrKeeper.SetDelegatorStartingInfo(ctx, validator0.GetOperator(), val0AccAddress.Bytes(), distrtypes.NewDelegatorStartingInfo(2, sdk.OneDec(), 200))
Expand All @@ -158,16 +154,16 @@ func (suite *SimTestSuite) TestSimulateMsgUnjail(t *testing.T) {

// execute operation
op := simulation.SimulateMsgUnjail(codec.NewProtoCodec(suite.interfaceRegistry), suite.accountKeeper, suite.bankKeeper, suite.slashingKeeper, suite.stakingKeeper)
operationMsg, futureOperations, err := op(r, app.BaseApp, ctx, accounts, "")
require.NoError(t, err)
operationMsg, futureOperations, err := op(suite.r, app.BaseApp, ctx, accounts, "")
suite.Require().NoError(err)

var msg types.MsgUnjail
types.ModuleCdc.UnmarshalJSON(operationMsg.Msg, &msg)

require.True(t, operationMsg.OK)
require.Equal(t, types.TypeMsgUnjail, msg.Type())
require.Equal(t, "cosmosvaloper17s94pzwhsn4ah25tec27w70n65h5t2scgxzkv2", msg.ValidatorAddr)
require.Len(t, futureOperations, 0)
suite.Require().True(operationMsg.OK)
suite.Require().Equal(types.TypeMsgUnjail, msg.Type())
suite.Require().Equal("cosmosvaloper1tnh2q55v8wyygtt9srz5safamzdengsn9dsd7z", msg.ValidatorAddr)
suite.Require().Len(futureOperations, 0)
}

func (suite *SimTestSuite) getTestingValidator0(ctx sdk.Context, accounts []simtypes.Account) stakingtypes.Validator {
Expand Down
18 changes: 13 additions & 5 deletions x/slashing/testutil/app.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,22 @@ modules:
config:
"@type": cosmos.app.runtime.v1alpha1.Module

app_name: SlashApp
app_name: SlashingApp

begin_blockers: [mint, staking, auth, bank, genutil, slashing, params]
end_blockers: [mint, staking, auth, bank, genutil, slashing, params]
init_genesis: [auth, bank, staking, mint, slashing, genutil, params]
begin_blockers:
[mint, distribution, staking, auth, bank, genutil, slashing, params]
end_blockers:
[staking, auth, bank, genutil, distribution, mint, slashing, params]
init_genesis:
[auth, bank, distribution, staking, mint, slashing, genutil, params]

- name: auth
config:
"@type": cosmos.auth.module.v1.Module
bech32_prefix: cosmos
module_account_permissions:
- account: fee_collector
- account: distribution
- account: mint
permissions: [minter]
- account: bonded_tokens_pool
Expand Down Expand Up @@ -44,8 +48,12 @@ modules:

- name: genutil
config:
"@type": cosmos.genutil.module.v1.Module
"@type": cosmos.genutil.module.v1.Module

- name: mint
config:
"@type": cosmos.mint.module.v1.Module

- name: distribution
config:
"@type": cosmos.distribution.module.v1.Module
1 change: 1 addition & 0 deletions x/slashing/testutil/app_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
_ "github.com/cosmos/cosmos-sdk/x/auth"
_ "github.com/cosmos/cosmos-sdk/x/auth/tx/module"
_ "github.com/cosmos/cosmos-sdk/x/bank"
_ "github.com/cosmos/cosmos-sdk/x/distribution"
_ "github.com/cosmos/cosmos-sdk/x/genutil"
_ "github.com/cosmos/cosmos-sdk/x/mint"
_ "github.com/cosmos/cosmos-sdk/x/params"
Expand Down