diff --git a/simapp/params/weights.go b/simapp/params/weights.go index f5e89fff02a7..59e092969ed6 100644 --- a/simapp/params/weights.go +++ b/simapp/params/weights.go @@ -4,9 +4,6 @@ package params const ( DefaultWeightMsgSend int = 100 DefaultWeightMsgMultiSend int = 10 - DefaultWeightMsgDeposit int = 100 - DefaultWeightMsgVote int = 67 - DefaultWeightMsgVoteWeighted int = 33 DefaultWeightMsgCreateValidator int = 100 DefaultWeightMsgEditValidator int = 5 DefaultWeightMsgDelegate int = 100 diff --git a/tests/integration/gov/common_test.go b/tests/integration/gov/common_test.go new file mode 100644 index 000000000000..27e6932c6046 --- /dev/null +++ b/tests/integration/gov/common_test.go @@ -0,0 +1,29 @@ +package gov_test + +import ( + "testing" + + "cosmossdk.io/math" + sdk "github.com/cosmos/cosmos-sdk/types" + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" + "github.com/cosmos/cosmos-sdk/x/gov/types" + v1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1" + "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1" + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" + "github.com/stretchr/testify/require" +) + +var ( + valTokens = sdk.TokensFromConsensusPower(42, sdk.DefaultPowerReduction) + TestProposal = v1beta1.NewTextProposal("Test", "description") + TestDescription = stakingtypes.NewDescription("T", "E", "S", "T", "Z") + TestCommissionRates = stakingtypes.NewCommissionRates(math.LegacyZeroDec(), math.LegacyZeroDec(), math.LegacyZeroDec()) +) + +// mkTestLegacyContent creates a MsgExecLegacyContent for testing purposes. +func mkTestLegacyContent(t *testing.T) *v1.MsgExecLegacyContent { + msgContent, err := v1.NewLegacyContent(TestProposal, authtypes.NewModuleAddress(types.ModuleName).String()) + require.NoError(t, err) + + return msgContent +} diff --git a/tests/integration/gov/genesis_test.go b/tests/integration/gov/genesis_test.go new file mode 100644 index 000000000000..ef8ecf93f423 --- /dev/null +++ b/tests/integration/gov/genesis_test.go @@ -0,0 +1,159 @@ +package gov_test + +import ( + "encoding/json" + "testing" + + "github.com/stretchr/testify/require" + abci "github.com/tendermint/tendermint/abci/types" + tmproto "github.com/tendermint/tendermint/proto/tendermint/types" + + "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/runtime" + "github.com/cosmos/cosmos-sdk/testutil/configurator" + simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" + sdk "github.com/cosmos/cosmos-sdk/types" + _ "github.com/cosmos/cosmos-sdk/x/auth" + authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper" + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" + _ "github.com/cosmos/cosmos-sdk/x/bank" + bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper" + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" + _ "github.com/cosmos/cosmos-sdk/x/distribution" + distrkeeper "github.com/cosmos/cosmos-sdk/x/distribution/keeper" + distributiontypes "github.com/cosmos/cosmos-sdk/x/distribution/types" + "github.com/cosmos/cosmos-sdk/x/gov" + "github.com/cosmos/cosmos-sdk/x/gov/keeper" + "github.com/cosmos/cosmos-sdk/x/gov/types" + v1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1" + _ "github.com/cosmos/cosmos-sdk/x/params" + _ "github.com/cosmos/cosmos-sdk/x/staking" + stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper" + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" +) + +type suite struct { + cdc codec.Codec + app *runtime.App + AccountKeeper authkeeper.AccountKeeper + BankKeeper bankkeeper.Keeper + DistrKeeper distrkeeper.Keeper + GovKeeper *keeper.Keeper + StakingKeeper *stakingkeeper.Keeper + appBuilder *runtime.AppBuilder +} + +var appConfig = configurator.NewAppConfig( + configurator.ParamsModule(), + configurator.AuthModule(), + configurator.StakingModule(), + configurator.BankModule(), + configurator.GovModule(), + configurator.DistributionModule(), + configurator.MintModule(), +) + +func TestImportExportQueues(t *testing.T) { + var err error + + s1 := suite{} + s1.app, err = simtestutil.SetupWithConfiguration( + appConfig, + simtestutil.DefaultStartUpConfig(), + &s1.AccountKeeper, &s1.BankKeeper, &s1.DistrKeeper, &s1.GovKeeper, &s1.StakingKeeper, &s1.cdc, &s1.appBuilder, + ) + require.NoError(t, err) + + ctx := s1.app.BaseApp.NewContext(false, tmproto.Header{}) + addrs := simtestutil.AddTestAddrs(s1.BankKeeper, s1.StakingKeeper, ctx, 1, valTokens) + + header := tmproto.Header{Height: s1.app.LastBlockHeight() + 1} + s1.app.BeginBlock(abci.RequestBeginBlock{Header: header}) + + ctx = s1.app.BaseApp.NewContext(false, tmproto.Header{}) + // Create two proposals, put the second into the voting period + proposal1, err := s1.GovKeeper.SubmitProposal(ctx, []sdk.Msg{mkTestLegacyContent(t)}, "") + require.NoError(t, err) + proposalID1 := proposal1.Id + + proposal2, err := s1.GovKeeper.SubmitProposal(ctx, []sdk.Msg{mkTestLegacyContent(t)}, "") + require.NoError(t, err) + proposalID2 := proposal2.Id + + votingStarted, err := s1.GovKeeper.AddDeposit(ctx, proposalID2, addrs[0], s1.GovKeeper.GetParams(ctx).MinDeposit) + require.NoError(t, err) + require.True(t, votingStarted) + + proposal1, ok := s1.GovKeeper.GetProposal(ctx, proposalID1) + require.True(t, ok) + proposal2, ok = s1.GovKeeper.GetProposal(ctx, proposalID2) + require.True(t, ok) + require.True(t, proposal1.Status == v1.StatusDepositPeriod) + require.True(t, proposal2.Status == v1.StatusVotingPeriod) + + authGenState := s1.AccountKeeper.ExportGenesis(ctx) + bankGenState := s1.BankKeeper.ExportGenesis(ctx) + stakingGenState := s1.StakingKeeper.ExportGenesis(ctx) + distributionGenState := s1.DistrKeeper.ExportGenesis(ctx) + + // export the state and import it into a new app + govGenState := gov.ExportGenesis(ctx, s1.GovKeeper) + genesisState := s1.appBuilder.DefaultGenesis() + + genesisState[authtypes.ModuleName] = s1.cdc.MustMarshalJSON(authGenState) + genesisState[banktypes.ModuleName] = s1.cdc.MustMarshalJSON(bankGenState) + genesisState[types.ModuleName] = s1.cdc.MustMarshalJSON(govGenState) + genesisState[stakingtypes.ModuleName] = s1.cdc.MustMarshalJSON(stakingGenState) + genesisState[distributiontypes.ModuleName] = s1.cdc.MustMarshalJSON(distributionGenState) + + stateBytes, err := json.MarshalIndent(genesisState, "", " ") + require.NoError(t, err) + + s2 := suite{} + s2.app, err = simtestutil.SetupWithConfiguration( + appConfig, + simtestutil.DefaultStartUpConfig(), + &s2.AccountKeeper, &s2.BankKeeper, &s2.DistrKeeper, &s2.GovKeeper, &s2.StakingKeeper, &s2.cdc, &s2.appBuilder, + ) + require.NoError(t, err) + + s2.app.InitChain( + abci.RequestInitChain{ + Validators: []abci.ValidatorUpdate{}, + ConsensusParams: simtestutil.DefaultConsensusParams, + AppStateBytes: stateBytes, + }, + ) + + s2.app.Commit() + s2.app.BeginBlock(abci.RequestBeginBlock{Header: tmproto.Header{Height: s2.app.LastBlockHeight() + 1}}) + + header = tmproto.Header{Height: s2.app.LastBlockHeight() + 1} + s2.app.BeginBlock(abci.RequestBeginBlock{Header: header}) + + ctx2 := s2.app.BaseApp.NewContext(false, tmproto.Header{}) + + // Jump the time forward past the DepositPeriod and VotingPeriod + ctx2 = ctx2.WithBlockTime(ctx2.BlockHeader().Time.Add(*s2.GovKeeper.GetParams(ctx2).MaxDepositPeriod).Add(*s2.GovKeeper.GetParams(ctx2).VotingPeriod)) + + // Make sure that they are still in the DepositPeriod and VotingPeriod respectively + proposal1, ok = s2.GovKeeper.GetProposal(ctx2, proposalID1) + require.True(t, ok) + proposal2, ok = s2.GovKeeper.GetProposal(ctx2, proposalID2) + require.True(t, ok) + require.True(t, proposal1.Status == v1.StatusDepositPeriod) + require.True(t, proposal2.Status == v1.StatusVotingPeriod) + + macc := s2.GovKeeper.GetGovernanceAccount(ctx2) + require.Equal(t, sdk.Coins(s2.GovKeeper.GetParams(ctx2).MinDeposit), s2.BankKeeper.GetAllBalances(ctx2, macc.GetAddress())) + + // Run the endblocker. Check to make sure that proposal1 is removed from state, and proposal2 is finished VotingPeriod. + gov.EndBlocker(ctx2, s2.GovKeeper) + + proposal1, ok = s2.GovKeeper.GetProposal(ctx2, proposalID1) + require.False(t, ok) + + proposal2, ok = s2.GovKeeper.GetProposal(ctx2, proposalID2) + require.True(t, ok) + require.True(t, proposal2.Status == v1.StatusRejected) +} diff --git a/x/gov/module_test.go b/tests/integration/gov/module_test.go similarity index 100% rename from x/gov/module_test.go rename to tests/integration/gov/module_test.go diff --git a/testutil/configurator/configurator.go b/testutil/configurator/configurator.go index 92a003de0760..4c0c950ba48e 100644 --- a/testutil/configurator/configurator.go +++ b/testutil/configurator/configurator.go @@ -9,6 +9,7 @@ import ( feegrantmodulev1 "cosmossdk.io/api/cosmos/feegrant/module/v1" genutilmodulev1 "cosmossdk.io/api/cosmos/genutil/module/v1" govmodulev1 "cosmossdk.io/api/cosmos/gov/module/v1" + mintmodulev1 "cosmossdk.io/api/cosmos/mint/module/v1" paramsmodulev1 "cosmossdk.io/api/cosmos/params/module/v1" slashingmodulev1 "cosmossdk.io/api/cosmos/slashing/module/v1" stakingmodulev1 "cosmossdk.io/api/cosmos/staking/module/v1" @@ -197,6 +198,21 @@ func GovModule() ModuleOption { } } +func MintModule() ModuleOption { + return func(config *appConfig) { + config.moduleConfigs["mint"] = &appv1alpha1.ModuleConfig{ + Name: "mint", + Config: appconfig.WrapAny(&mintmodulev1.Module{}), + GolangBindings: []*appv1alpha1.GolangBinding{ + { + InterfaceType: "github.com/cosmos/cosmos-sdk/x/mint/types/types.StakingKeeper", + Implementation: "github.com/cosmos/cosmos-sdk/x/staking/keeper/*keeper.Keeper", + }, + }, + } + } +} + func OmitInitGenesis() ModuleOption { return func(config *appConfig) { config.setInitGenesis = false diff --git a/testutil/sims/weights.go b/testutil/sims/weights.go index c85345ffb74e..18403f95c62d 100644 --- a/testutil/sims/weights.go +++ b/testutil/sims/weights.go @@ -8,9 +8,6 @@ const ( DefaultWeightMsgWithdrawDelegationReward int = 50 DefaultWeightMsgWithdrawValidatorCommission int = 50 DefaultWeightMsgFundCommunityPool int = 50 - DefaultWeightMsgDeposit int = 100 - DefaultWeightMsgVote int = 67 - DefaultWeightMsgVoteWeighted int = 33 DefaultWeightMsgUnjail int = 100 DefaultWeightMsgCreateValidator int = 100 DefaultWeightMsgEditValidator int = 5 diff --git a/x/auth/client/testutil/suite.go b/x/auth/client/testutil/suite.go index fa0918d39561..5e6e78d1580b 100644 --- a/x/auth/client/testutil/suite.go +++ b/x/auth/client/testutil/suite.go @@ -1566,10 +1566,7 @@ func (s *IntegrationTestSuite) TestSignWithMultiSignersAminoJSON() { require.Equal(sdk.NewCoins(val0Coin, val1Coin), queryRes.Balances) } -// TODO to re-enable in #12274 func (s *IntegrationTestSuite) TestAuxSigner() { - s.T().Skip() - require := s.Require() val := s.network.Validators[0] val0Coin := sdk.NewCoin(fmt.Sprintf("%stoken", val.Moniker), sdk.NewInt(10)) diff --git a/x/feegrant/client/testutil/suite.go b/x/feegrant/client/testutil/suite.go index 7411976dd15a..dd4e3c7c61e2 100644 --- a/x/feegrant/client/testutil/suite.go +++ b/x/feegrant/client/testutil/suite.go @@ -713,8 +713,6 @@ func (s *IntegrationTestSuite) TestNewCmdRevokeFeegrant() { } func (s *IntegrationTestSuite) TestTxWithFeeGrant() { - s.T().Skip() // TODO to re-enable in #12274 - val := s.network.Validators[0] clientCtx := val.ClientCtx granter := val.Address @@ -804,8 +802,6 @@ func (s *IntegrationTestSuite) TestTxWithFeeGrant() { } func (s *IntegrationTestSuite) TestFilteredFeeAllowance() { - s.T().Skip() // TODO to re-enable in #12274 - val := s.network.Validators[0] granter := val.Address diff --git a/x/gov/abci_test.go b/x/gov/abci_test.go index 248c82a94add..bf7432feed96 100644 --- a/x/gov/abci_test.go +++ b/x/gov/abci_test.go @@ -9,7 +9,7 @@ import ( abci "github.com/tendermint/tendermint/abci/types" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" - "github.com/cosmos/cosmos-sdk/simapp" + simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/gov" "github.com/cosmos/cosmos-sdk/x/gov/keeper" @@ -21,16 +21,17 @@ import ( ) func TestTickExpiredDepositPeriod(t *testing.T) { - app := simapp.Setup(t, false) + suite := createTestSuite(t) + app := suite.App ctx := app.BaseApp.NewContext(false, tmproto.Header{}) - addrs := simapp.AddTestAddrs(app, ctx, 10, valTokens) + addrs := simtestutil.AddTestAddrs(suite.BankKeeper, suite.StakingKeeper, ctx, 10, valTokens) header := tmproto.Header{Height: app.LastBlockHeight() + 1} app.BeginBlock(abci.RequestBeginBlock{Header: header}) - govMsgSvr := keeper.NewMsgServerImpl(app.GovKeeper) + govMsgSvr := keeper.NewMsgServerImpl(suite.GovKeeper) - inactiveQueue := app.GovKeeper.InactiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) + inactiveQueue := suite.GovKeeper.InactiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) require.False(t, inactiveQueue.Valid()) inactiveQueue.Close() @@ -46,7 +47,7 @@ func TestTickExpiredDepositPeriod(t *testing.T) { require.NoError(t, err) require.NotNil(t, res) - inactiveQueue = app.GovKeeper.InactiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) + inactiveQueue = suite.GovKeeper.InactiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) require.False(t, inactiveQueue.Valid()) inactiveQueue.Close() @@ -54,36 +55,37 @@ func TestTickExpiredDepositPeriod(t *testing.T) { newHeader.Time = ctx.BlockHeader().Time.Add(time.Duration(1) * time.Second) ctx = ctx.WithBlockHeader(newHeader) - inactiveQueue = app.GovKeeper.InactiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) + inactiveQueue = suite.GovKeeper.InactiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) require.False(t, inactiveQueue.Valid()) inactiveQueue.Close() newHeader = ctx.BlockHeader() - newHeader.Time = ctx.BlockHeader().Time.Add(*app.GovKeeper.GetParams(ctx).MaxDepositPeriod) + newHeader.Time = ctx.BlockHeader().Time.Add(*suite.GovKeeper.GetParams(ctx).MaxDepositPeriod) ctx = ctx.WithBlockHeader(newHeader) - inactiveQueue = app.GovKeeper.InactiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) + inactiveQueue = suite.GovKeeper.InactiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) require.True(t, inactiveQueue.Valid()) inactiveQueue.Close() - gov.EndBlocker(ctx, app.GovKeeper) + gov.EndBlocker(ctx, suite.GovKeeper) - inactiveQueue = app.GovKeeper.InactiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) + inactiveQueue = suite.GovKeeper.InactiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) require.False(t, inactiveQueue.Valid()) inactiveQueue.Close() } func TestTickMultipleExpiredDepositPeriod(t *testing.T) { - app := simapp.Setup(t, false) + suite := createTestSuite(t) + app := suite.App ctx := app.BaseApp.NewContext(false, tmproto.Header{}) - addrs := simapp.AddTestAddrs(app, ctx, 10, valTokens) + addrs := simtestutil.AddTestAddrs(suite.BankKeeper, suite.StakingKeeper, ctx, 10, valTokens) header := tmproto.Header{Height: app.LastBlockHeight() + 1} app.BeginBlock(abci.RequestBeginBlock{Header: header}) - govMsgSvr := keeper.NewMsgServerImpl(app.GovKeeper) + govMsgSvr := keeper.NewMsgServerImpl(suite.GovKeeper) - inactiveQueue := app.GovKeeper.InactiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) + inactiveQueue := suite.GovKeeper.InactiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) require.False(t, inactiveQueue.Valid()) inactiveQueue.Close() @@ -99,7 +101,7 @@ func TestTickMultipleExpiredDepositPeriod(t *testing.T) { require.NoError(t, err) require.NotNil(t, res) - inactiveQueue = app.GovKeeper.InactiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) + inactiveQueue = suite.GovKeeper.InactiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) require.False(t, inactiveQueue.Valid()) inactiveQueue.Close() @@ -107,7 +109,7 @@ func TestTickMultipleExpiredDepositPeriod(t *testing.T) { newHeader.Time = ctx.BlockHeader().Time.Add(time.Duration(2) * time.Second) ctx = ctx.WithBlockHeader(newHeader) - inactiveQueue = app.GovKeeper.InactiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) + inactiveQueue = suite.GovKeeper.InactiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) require.False(t, inactiveQueue.Valid()) inactiveQueue.Close() @@ -124,16 +126,16 @@ func TestTickMultipleExpiredDepositPeriod(t *testing.T) { require.NotNil(t, res) newHeader = ctx.BlockHeader() - newHeader.Time = ctx.BlockHeader().Time.Add(*app.GovKeeper.GetParams(ctx).MaxDepositPeriod).Add(time.Duration(-1) * time.Second) + newHeader.Time = ctx.BlockHeader().Time.Add(*suite.GovKeeper.GetParams(ctx).MaxDepositPeriod).Add(time.Duration(-1) * time.Second) ctx = ctx.WithBlockHeader(newHeader) - inactiveQueue = app.GovKeeper.InactiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) + inactiveQueue = suite.GovKeeper.InactiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) require.True(t, inactiveQueue.Valid()) inactiveQueue.Close() - gov.EndBlocker(ctx, app.GovKeeper) + gov.EndBlocker(ctx, suite.GovKeeper) - inactiveQueue = app.GovKeeper.InactiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) + inactiveQueue = suite.GovKeeper.InactiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) require.False(t, inactiveQueue.Valid()) inactiveQueue.Close() @@ -141,31 +143,32 @@ func TestTickMultipleExpiredDepositPeriod(t *testing.T) { newHeader.Time = ctx.BlockHeader().Time.Add(time.Duration(5) * time.Second) ctx = ctx.WithBlockHeader(newHeader) - inactiveQueue = app.GovKeeper.InactiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) + inactiveQueue = suite.GovKeeper.InactiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) require.True(t, inactiveQueue.Valid()) inactiveQueue.Close() - gov.EndBlocker(ctx, app.GovKeeper) + gov.EndBlocker(ctx, suite.GovKeeper) - inactiveQueue = app.GovKeeper.InactiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) + inactiveQueue = suite.GovKeeper.InactiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) require.False(t, inactiveQueue.Valid()) inactiveQueue.Close() } func TestTickPassedDepositPeriod(t *testing.T) { - app := simapp.Setup(t, false) + suite := createTestSuite(t) + app := suite.App ctx := app.BaseApp.NewContext(false, tmproto.Header{}) - addrs := simapp.AddTestAddrs(app, ctx, 10, valTokens) + addrs := simtestutil.AddTestAddrs(suite.BankKeeper, suite.StakingKeeper, ctx, 10, valTokens) header := tmproto.Header{Height: app.LastBlockHeight() + 1} app.BeginBlock(abci.RequestBeginBlock{Header: header}) - govMsgSvr := keeper.NewMsgServerImpl(app.GovKeeper) + govMsgSvr := keeper.NewMsgServerImpl(suite.GovKeeper) - inactiveQueue := app.GovKeeper.InactiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) + inactiveQueue := suite.GovKeeper.InactiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) require.False(t, inactiveQueue.Valid()) inactiveQueue.Close() - activeQueue := app.GovKeeper.ActiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) + activeQueue := suite.GovKeeper.ActiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) require.False(t, activeQueue.Valid()) activeQueue.Close() @@ -183,7 +186,7 @@ func TestTickPassedDepositPeriod(t *testing.T) { proposalID := res.ProposalId - inactiveQueue = app.GovKeeper.InactiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) + inactiveQueue = suite.GovKeeper.InactiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) require.False(t, inactiveQueue.Valid()) inactiveQueue.Close() @@ -191,7 +194,7 @@ func TestTickPassedDepositPeriod(t *testing.T) { newHeader.Time = ctx.BlockHeader().Time.Add(time.Duration(1) * time.Second) ctx = ctx.WithBlockHeader(newHeader) - inactiveQueue = app.GovKeeper.InactiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) + inactiveQueue = suite.GovKeeper.InactiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) require.False(t, inactiveQueue.Valid()) inactiveQueue.Close() @@ -201,31 +204,32 @@ func TestTickPassedDepositPeriod(t *testing.T) { require.NoError(t, err) require.NotNil(t, res1) - activeQueue = app.GovKeeper.ActiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) + activeQueue = suite.GovKeeper.ActiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) require.False(t, activeQueue.Valid()) activeQueue.Close() } func TestTickPassedVotingPeriod(t *testing.T) { - app := simapp.Setup(t, false) + suite := createTestSuite(t) + app := suite.App ctx := app.BaseApp.NewContext(false, tmproto.Header{}) - addrs := simapp.AddTestAddrs(app, ctx, 10, valTokens) + addrs := simtestutil.AddTestAddrs(suite.BankKeeper, suite.StakingKeeper, ctx, 10, valTokens) SortAddresses(addrs) header := tmproto.Header{Height: app.LastBlockHeight() + 1} app.BeginBlock(abci.RequestBeginBlock{Header: header}) - govMsgSvr := keeper.NewMsgServerImpl(app.GovKeeper) + govMsgSvr := keeper.NewMsgServerImpl(suite.GovKeeper) - inactiveQueue := app.GovKeeper.InactiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) + inactiveQueue := suite.GovKeeper.InactiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) require.False(t, inactiveQueue.Valid()) inactiveQueue.Close() - activeQueue := app.GovKeeper.ActiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) + activeQueue := suite.GovKeeper.ActiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) require.False(t, activeQueue.Valid()) activeQueue.Close() - proposalCoins := sdk.Coins{sdk.NewCoin(sdk.DefaultBondDenom, app.StakingKeeper.TokensFromConsensusPower(ctx, 5))} + proposalCoins := sdk.Coins{sdk.NewCoin(sdk.DefaultBondDenom, suite.StakingKeeper.TokensFromConsensusPower(ctx, 5))} newProposalMsg, err := v1.NewMsgSubmitProposal([]sdk.Msg{mkTestLegacyContent(t)}, proposalCoins, addrs[0].String(), "") require.NoError(t, err) @@ -248,39 +252,40 @@ func TestTickPassedVotingPeriod(t *testing.T) { require.NotNil(t, res1) newHeader = ctx.BlockHeader() - newHeader.Time = ctx.BlockHeader().Time.Add(*app.GovKeeper.GetParams(ctx).MaxDepositPeriod).Add(*app.GovKeeper.GetParams(ctx).VotingPeriod) + newHeader.Time = ctx.BlockHeader().Time.Add(*suite.GovKeeper.GetParams(ctx).MaxDepositPeriod).Add(*suite.GovKeeper.GetParams(ctx).VotingPeriod) ctx = ctx.WithBlockHeader(newHeader) - inactiveQueue = app.GovKeeper.InactiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) + inactiveQueue = suite.GovKeeper.InactiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) require.False(t, inactiveQueue.Valid()) inactiveQueue.Close() - activeQueue = app.GovKeeper.ActiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) + activeQueue = suite.GovKeeper.ActiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) require.True(t, activeQueue.Valid()) activeProposalID := types.GetProposalIDFromBytes(activeQueue.Value()) - proposal, ok := app.GovKeeper.GetProposal(ctx, activeProposalID) + proposal, ok := suite.GovKeeper.GetProposal(ctx, activeProposalID) require.True(t, ok) require.Equal(t, v1.StatusVotingPeriod, proposal.Status) activeQueue.Close() - gov.EndBlocker(ctx, app.GovKeeper) + gov.EndBlocker(ctx, suite.GovKeeper) - activeQueue = app.GovKeeper.ActiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) + activeQueue = suite.GovKeeper.ActiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) require.False(t, activeQueue.Valid()) activeQueue.Close() } func TestProposalPassedEndblocker(t *testing.T) { - app := simapp.Setup(t, false) + suite := createTestSuite(t) + app := suite.App ctx := app.BaseApp.NewContext(false, tmproto.Header{}) - addrs := simapp.AddTestAddrs(app, ctx, 10, valTokens) + addrs := simtestutil.AddTestAddrs(suite.BankKeeper, suite.StakingKeeper, ctx, 10, valTokens) SortAddresses(addrs) - govMsgSvr := keeper.NewMsgServerImpl(app.GovKeeper) - stakingMsgSvr := stakingkeeper.NewMsgServerImpl(app.StakingKeeper) + govMsgSvr := keeper.NewMsgServerImpl(suite.GovKeeper) + stakingMsgSvr := stakingkeeper.NewMsgServerImpl(suite.StakingKeeper) header := tmproto.Header{Height: app.LastBlockHeight() + 1} app.BeginBlock(abci.RequestBeginBlock{Header: header}) @@ -288,78 +293,79 @@ func TestProposalPassedEndblocker(t *testing.T) { valAddr := sdk.ValAddress(addrs[0]) createValidators(t, stakingMsgSvr, ctx, []sdk.ValAddress{valAddr}, []int64{10}) - staking.EndBlocker(ctx, app.StakingKeeper) + staking.EndBlocker(ctx, suite.StakingKeeper) - macc := app.GovKeeper.GetGovernanceAccount(ctx) + macc := suite.GovKeeper.GetGovernanceAccount(ctx) require.NotNil(t, macc) - initialModuleAccCoins := app.BankKeeper.GetAllBalances(ctx, macc.GetAddress()) + initialModuleAccCoins := suite.BankKeeper.GetAllBalances(ctx, macc.GetAddress()) - proposal, err := app.GovKeeper.SubmitProposal(ctx, []sdk.Msg{mkTestLegacyContent(t)}, "") + proposal, err := suite.GovKeeper.SubmitProposal(ctx, []sdk.Msg{mkTestLegacyContent(t)}, "") require.NoError(t, err) - proposalCoins := sdk.Coins{sdk.NewCoin(sdk.DefaultBondDenom, app.StakingKeeper.TokensFromConsensusPower(ctx, 10))} + proposalCoins := sdk.Coins{sdk.NewCoin(sdk.DefaultBondDenom, suite.StakingKeeper.TokensFromConsensusPower(ctx, 10))} newDepositMsg := v1.NewMsgDeposit(addrs[0], proposal.Id, proposalCoins) res, err := govMsgSvr.Deposit(sdk.WrapSDKContext(ctx), newDepositMsg) require.NoError(t, err) require.NotNil(t, res) - macc = app.GovKeeper.GetGovernanceAccount(ctx) + macc = suite.GovKeeper.GetGovernanceAccount(ctx) require.NotNil(t, macc) - moduleAccCoins := app.BankKeeper.GetAllBalances(ctx, macc.GetAddress()) + moduleAccCoins := suite.BankKeeper.GetAllBalances(ctx, macc.GetAddress()) deposits := initialModuleAccCoins.Add(proposal.TotalDeposit...).Add(proposalCoins...) require.True(t, moduleAccCoins.IsEqual(deposits)) - err = app.GovKeeper.AddVote(ctx, proposal.Id, addrs[0], v1.NewNonSplitVoteOption(v1.OptionYes), "") + err = suite.GovKeeper.AddVote(ctx, proposal.Id, addrs[0], v1.NewNonSplitVoteOption(v1.OptionYes), "") require.NoError(t, err) newHeader := ctx.BlockHeader() - newHeader.Time = ctx.BlockHeader().Time.Add(*app.GovKeeper.GetParams(ctx).MaxDepositPeriod).Add(*app.GovKeeper.GetParams(ctx).VotingPeriod) + newHeader.Time = ctx.BlockHeader().Time.Add(*suite.GovKeeper.GetParams(ctx).MaxDepositPeriod).Add(*suite.GovKeeper.GetParams(ctx).VotingPeriod) ctx = ctx.WithBlockHeader(newHeader) - gov.EndBlocker(ctx, app.GovKeeper) + gov.EndBlocker(ctx, suite.GovKeeper) - macc = app.GovKeeper.GetGovernanceAccount(ctx) + macc = suite.GovKeeper.GetGovernanceAccount(ctx) require.NotNil(t, macc) - require.True(t, app.BankKeeper.GetAllBalances(ctx, macc.GetAddress()).IsEqual(initialModuleAccCoins)) + require.True(t, suite.BankKeeper.GetAllBalances(ctx, macc.GetAddress()).IsEqual(initialModuleAccCoins)) } func TestEndBlockerProposalHandlerFailed(t *testing.T) { - app := simapp.Setup(t, false) + suite := createTestSuite(t) + app := suite.App ctx := app.BaseApp.NewContext(false, tmproto.Header{}) - addrs := simapp.AddTestAddrs(app, ctx, 1, valTokens) + addrs := simtestutil.AddTestAddrs(suite.BankKeeper, suite.StakingKeeper, ctx, 1, valTokens) SortAddresses(addrs) - stakingMsgSvr := stakingkeeper.NewMsgServerImpl(app.StakingKeeper) + stakingMsgSvr := stakingkeeper.NewMsgServerImpl(suite.StakingKeeper) header := tmproto.Header{Height: app.LastBlockHeight() + 1} app.BeginBlock(abci.RequestBeginBlock{Header: header}) valAddr := sdk.ValAddress(addrs[0]) createValidators(t, stakingMsgSvr, ctx, []sdk.ValAddress{valAddr}, []int64{10}) - staking.EndBlocker(ctx, app.StakingKeeper) + staking.EndBlocker(ctx, suite.StakingKeeper) // Create a proposal where the handler will pass for the test proposal // because the value of contextKeyBadProposal is true. ctx = ctx.WithValue(contextKeyBadProposal, true) - proposal, err := app.GovKeeper.SubmitProposal(ctx, []sdk.Msg{mkTestLegacyContent(t)}, "") + proposal, err := suite.GovKeeper.SubmitProposal(ctx, []sdk.Msg{mkTestLegacyContent(t)}, "") require.NoError(t, err) - proposalCoins := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, app.StakingKeeper.TokensFromConsensusPower(ctx, 10))) + proposalCoins := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, suite.StakingKeeper.TokensFromConsensusPower(ctx, 10))) newDepositMsg := v1.NewMsgDeposit(addrs[0], proposal.Id, proposalCoins) - govMsgSvr := keeper.NewMsgServerImpl(app.GovKeeper) + govMsgSvr := keeper.NewMsgServerImpl(suite.GovKeeper) res, err := govMsgSvr.Deposit(sdk.WrapSDKContext(ctx), newDepositMsg) require.NoError(t, err) require.NotNil(t, res) - err = app.GovKeeper.AddVote(ctx, proposal.Id, addrs[0], v1.NewNonSplitVoteOption(v1.OptionYes), "") + err = suite.GovKeeper.AddVote(ctx, proposal.Id, addrs[0], v1.NewNonSplitVoteOption(v1.OptionYes), "") require.NoError(t, err) newHeader := ctx.BlockHeader() - newHeader.Time = ctx.BlockHeader().Time.Add(*app.GovKeeper.GetParams(ctx).MaxDepositPeriod).Add(*app.GovKeeper.GetParams(ctx).VotingPeriod) + newHeader.Time = ctx.BlockHeader().Time.Add(*suite.GovKeeper.GetParams(ctx).MaxDepositPeriod).Add(*suite.GovKeeper.GetParams(ctx).VotingPeriod) ctx = ctx.WithBlockHeader(newHeader) // Set the contextKeyBadProposal value to false so that the handler will fail @@ -367,7 +373,7 @@ func TestEndBlockerProposalHandlerFailed(t *testing.T) { ctx = ctx.WithValue(contextKeyBadProposal, false) // validate that the proposal fails/has been rejected - gov.EndBlocker(ctx, app.GovKeeper) + gov.EndBlocker(ctx, suite.GovKeeper) } func createValidators(t *testing.T, stakingMsgSvr stakingtypes.MsgServer, ctx sdk.Context, addrs []sdk.ValAddress, powerAmt []int64) { diff --git a/x/gov/common_test.go b/x/gov/common_test.go index 69e5e9075522..2c9212eaaa63 100644 --- a/x/gov/common_test.go +++ b/x/gov/common_test.go @@ -9,11 +9,22 @@ import ( "cosmossdk.io/math" "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" + "github.com/cosmos/cosmos-sdk/runtime" + "github.com/cosmos/cosmos-sdk/testutil/configurator" + simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" sdk "github.com/cosmos/cosmos-sdk/types" + _ "github.com/cosmos/cosmos-sdk/x/auth" + authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" + _ "github.com/cosmos/cosmos-sdk/x/bank" + bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper" + "github.com/cosmos/cosmos-sdk/x/gov/keeper" "github.com/cosmos/cosmos-sdk/x/gov/types" v1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1" "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1" + _ "github.com/cosmos/cosmos-sdk/x/params" + _ "github.com/cosmos/cosmos-sdk/x/staking" + stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" "github.com/stretchr/testify/require" ) @@ -86,3 +97,31 @@ var pubkeys = []cryptotypes.PubKey{ ed25519.GenPrivKey().PubKey(), ed25519.GenPrivKey().PubKey(), } + +type suite struct { + AccountKeeper authkeeper.AccountKeeper + BankKeeper bankkeeper.Keeper + GovKeeper *keeper.Keeper + StakingKeeper *stakingkeeper.Keeper + App *runtime.App +} + +func createTestSuite(t *testing.T) suite { + res := suite{} + + app, err := simtestutil.SetupWithConfiguration( + configurator.NewAppConfig( + configurator.ParamsModule(), + configurator.AuthModule(), + configurator.StakingModule(), + configurator.BankModule(), + configurator.GovModule(), + ), + simtestutil.DefaultStartUpConfig(), + &res.AccountKeeper, &res.BankKeeper, &res.GovKeeper, &res.StakingKeeper, + ) + require.NoError(t, err) + + res.App = app + return res +} diff --git a/x/gov/genesis_test.go b/x/gov/genesis_test.go index d7c0c6b677ec..c24995de7726 100644 --- a/x/gov/genesis_test.go +++ b/x/gov/genesis_test.go @@ -1,127 +1,22 @@ package gov_test import ( - "encoding/json" "testing" "github.com/stretchr/testify/require" - abci "github.com/tendermint/tendermint/abci/types" - "github.com/tendermint/tendermint/libs/log" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" - dbm "github.com/tendermint/tm-db" - "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" - distributiontypes "github.com/cosmos/cosmos-sdk/x/distribution/types" "github.com/cosmos/cosmos-sdk/x/gov" - "github.com/cosmos/cosmos-sdk/x/gov/types" v1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1" - stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" ) -func TestImportExportQueues(t *testing.T) { - app := simapp.Setup(t, false) - ctx := app.BaseApp.NewContext(false, tmproto.Header{}) - addrs := simapp.AddTestAddrs(app, ctx, 2, valTokens) - - SortAddresses(addrs) - - header := tmproto.Header{Height: app.LastBlockHeight() + 1} - app.BeginBlock(abci.RequestBeginBlock{Header: header}) - - ctx = app.BaseApp.NewContext(false, tmproto.Header{}) - // Create two proposals, put the second into the voting period - proposal1, err := app.GovKeeper.SubmitProposal(ctx, []sdk.Msg{mkTestLegacyContent(t)}, "") - require.NoError(t, err) - proposalID1 := proposal1.Id - - proposal2, err := app.GovKeeper.SubmitProposal(ctx, []sdk.Msg{mkTestLegacyContent(t)}, "") - require.NoError(t, err) - proposalID2 := proposal2.Id - - votingStarted, err := app.GovKeeper.AddDeposit(ctx, proposalID2, addrs[0], app.GovKeeper.GetParams(ctx).MinDeposit) - require.NoError(t, err) - require.True(t, votingStarted) - - proposal1, ok := app.GovKeeper.GetProposal(ctx, proposalID1) - require.True(t, ok) - proposal2, ok = app.GovKeeper.GetProposal(ctx, proposalID2) - require.True(t, ok) - require.True(t, proposal1.Status == v1.StatusDepositPeriod) - require.True(t, proposal2.Status == v1.StatusVotingPeriod) - - authGenState := app.AccountKeeper.ExportGenesis(ctx) - bankGenState := app.BankKeeper.ExportGenesis(ctx) - stakingGenState := app.StakingKeeper.ExportGenesis(ctx) - distributionGenState := app.DistrKeeper.ExportGenesis(ctx) - - // export the state and import it into a new app - govGenState := gov.ExportGenesis(ctx, app.GovKeeper) - genesisState := simapp.NewDefaultGenesisState(app.AppCodec()) - - genesisState[authtypes.ModuleName] = app.AppCodec().MustMarshalJSON(authGenState) - genesisState[banktypes.ModuleName] = app.AppCodec().MustMarshalJSON(bankGenState) - genesisState[types.ModuleName] = app.AppCodec().MustMarshalJSON(govGenState) - genesisState[stakingtypes.ModuleName] = app.AppCodec().MustMarshalJSON(stakingGenState) - genesisState[distributiontypes.ModuleName] = app.AppCodec().MustMarshalJSON(distributionGenState) - - stateBytes, err := json.MarshalIndent(genesisState, "", " ") - if err != nil { - panic(err) - } - - db := dbm.NewMemDB() - app2 := simapp.NewSimApp(log.NewNopLogger(), db, nil, true, simtestutil.NewAppOptionsWithFlagHome(simapp.DefaultNodeHome)) - - app2.InitChain( - abci.RequestInitChain{ - Validators: []abci.ValidatorUpdate{}, - ConsensusParams: simtestutil.DefaultConsensusParams, - AppStateBytes: stateBytes, - }, - ) - - app2.Commit() - app2.BeginBlock(abci.RequestBeginBlock{Header: tmproto.Header{Height: app2.LastBlockHeight() + 1}}) - - header = tmproto.Header{Height: app2.LastBlockHeight() + 1} - app2.BeginBlock(abci.RequestBeginBlock{Header: header}) - - ctx2 := app2.BaseApp.NewContext(false, tmproto.Header{}) - - // Jump the time forward past the DepositPeriod and VotingPeriod - ctx2 = ctx2.WithBlockTime(ctx2.BlockHeader().Time.Add(*app2.GovKeeper.GetParams(ctx2).MaxDepositPeriod).Add(*app2.GovKeeper.GetParams(ctx2).VotingPeriod)) - - // Make sure that they are still in the DepositPeriod and VotingPeriod respectively - proposal1, ok = app2.GovKeeper.GetProposal(ctx2, proposalID1) - require.True(t, ok) - proposal2, ok = app2.GovKeeper.GetProposal(ctx2, proposalID2) - require.True(t, ok) - require.True(t, proposal1.Status == v1.StatusDepositPeriod) - require.True(t, proposal2.Status == v1.StatusVotingPeriod) - - macc := app2.GovKeeper.GetGovernanceAccount(ctx2) - require.Equal(t, sdk.Coins(app2.GovKeeper.GetParams(ctx2).MinDeposit), app2.BankKeeper.GetAllBalances(ctx2, macc.GetAddress())) - - // Run the endblocker. Check to make sure that proposal1 is removed from state, and proposal2 is finished VotingPeriod. - gov.EndBlocker(ctx2, app2.GovKeeper) - - proposal1, ok = app2.GovKeeper.GetProposal(ctx2, proposalID1) - require.False(t, ok) - - proposal2, ok = app2.GovKeeper.GetProposal(ctx2, proposalID2) - require.True(t, ok) - require.True(t, proposal2.Status == v1.StatusRejected) -} - func TestImportExportQueues_ErrorUnconsistentState(t *testing.T) { - app := simapp.Setup(t, false) + suite := createTestSuite(t) + app := suite.App ctx := app.BaseApp.NewContext(false, tmproto.Header{}) require.Panics(t, func() { - gov.InitGenesis(ctx, app.AccountKeeper, app.BankKeeper, app.GovKeeper, &v1.GenesisState{ + gov.InitGenesis(ctx, suite.AccountKeeper, suite.BankKeeper, suite.GovKeeper, &v1.GenesisState{ Deposits: v1.Deposits{ { ProposalId: 1234, diff --git a/x/gov/simulation/operations.go b/x/gov/simulation/operations.go index bdd53e433f2f..9b101267ed57 100644 --- a/x/gov/simulation/operations.go +++ b/x/gov/simulation/operations.go @@ -7,9 +7,9 @@ import ( "github.com/cosmos/cosmos-sdk/baseapp" "github.com/cosmos/cosmos-sdk/codec" - simappparams "github.com/cosmos/cosmos-sdk/simapp/params" simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" sdk "github.com/cosmos/cosmos-sdk/types" + moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil" simtypes "github.com/cosmos/cosmos-sdk/types/simulation" "github.com/cosmos/cosmos-sdk/x/gov/keeper" "github.com/cosmos/cosmos-sdk/x/gov/types" @@ -34,6 +34,10 @@ const ( OpWeightMsgDeposit = "op_weight_msg_deposit" OpWeightMsgVote = "op_weight_msg_vote" OpWeightMsgVoteWeighted = "op_weight_msg_weighted_vote" + + DefaultWeightMsgDeposit = 100 + DefaultWeightMsgVote = 67 + DefaultWeightMsgVoteWeighted = 33 ) // WeightedOperations returns all the operations from the module with their respective weights @@ -46,19 +50,19 @@ func WeightedOperations(appParams simtypes.AppParams, cdc codec.JSONCodec, ak ty appParams.GetOrGenerate(cdc, OpWeightMsgDeposit, &weightMsgDeposit, nil, func(_ *rand.Rand) { - weightMsgDeposit = simappparams.DefaultWeightMsgDeposit + weightMsgDeposit = DefaultWeightMsgDeposit }, ) appParams.GetOrGenerate(cdc, OpWeightMsgVote, &weightMsgVote, nil, func(_ *rand.Rand) { - weightMsgVote = simappparams.DefaultWeightMsgVote + weightMsgVote = DefaultWeightMsgVote }, ) appParams.GetOrGenerate(cdc, OpWeightMsgVoteWeighted, &weightMsgVoteWeighted, nil, func(_ *rand.Rand) { - weightMsgVoteWeighted = simappparams.DefaultWeightMsgVoteWeighted + weightMsgVoteWeighted = DefaultWeightMsgVoteWeighted }, ) @@ -165,7 +169,7 @@ func SimulateMsgSubmitProposal(ak types.AccountKeeper, bk types.BankKeeper, k *k } } - txGen := simappparams.MakeTestEncodingConfig().TxConfig + txGen := moduletestutil.MakeTestEncodingConfig().TxConfig tx, err := simtestutil.GenSignedMockTx( r, txGen, @@ -256,7 +260,7 @@ func SimulateMsgDeposit(ak types.AccountKeeper, bk types.BankKeeper, k *keeper.K txCtx := simulation.OperationInput{ R: r, App: app, - TxGen: simappparams.MakeTestEncodingConfig().TxConfig, + TxGen: moduletestutil.MakeTestEncodingConfig().TxConfig, Cdc: nil, Msg: msg, MsgType: msg.Type(), @@ -306,7 +310,7 @@ func operationSimulateMsgVote(ak types.AccountKeeper, bk types.BankKeeper, k *ke txCtx := simulation.OperationInput{ R: r, App: app, - TxGen: simappparams.MakeTestEncodingConfig().TxConfig, + TxGen: moduletestutil.MakeTestEncodingConfig().TxConfig, Cdc: nil, Msg: msg, MsgType: msg.Type(), @@ -358,7 +362,7 @@ func operationSimulateMsgVoteWeighted(ak types.AccountKeeper, bk types.BankKeepe txCtx := simulation.OperationInput{ R: r, App: app, - TxGen: simappparams.MakeTestEncodingConfig().TxConfig, + TxGen: moduletestutil.MakeTestEncodingConfig().TxConfig, Cdc: nil, Msg: msg, MsgType: msg.Type(), diff --git a/x/gov/simulation/operations_test.go b/x/gov/simulation/operations_test.go index 4f1a6e331f11..267934307206 100644 --- a/x/gov/simulation/operations_test.go +++ b/x/gov/simulation/operations_test.go @@ -10,16 +10,26 @@ import ( abci "github.com/tendermint/tendermint/abci/types" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" - "github.com/cosmos/cosmos-sdk/simapp" - simappparams "github.com/cosmos/cosmos-sdk/simapp/params" + "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/runtime" + "github.com/cosmos/cosmos-sdk/testutil/configurator" + simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" sdk "github.com/cosmos/cosmos-sdk/types" simtypes "github.com/cosmos/cosmos-sdk/types/simulation" + _ "github.com/cosmos/cosmos-sdk/x/auth" + authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper" + _ "github.com/cosmos/cosmos-sdk/x/auth/tx/module" + _ "github.com/cosmos/cosmos-sdk/x/bank" + bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper" "github.com/cosmos/cosmos-sdk/x/bank/testutil" + "github.com/cosmos/cosmos-sdk/x/gov/keeper" "github.com/cosmos/cosmos-sdk/x/gov/simulation" "github.com/cosmos/cosmos-sdk/x/gov/types" v1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1" "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1" - minttypes "github.com/cosmos/cosmos-sdk/x/mint/types" + _ "github.com/cosmos/cosmos-sdk/x/params" + _ "github.com/cosmos/cosmos-sdk/x/staking" + stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper" ) type MockWeightedProposalContent struct { @@ -56,20 +66,21 @@ func mockWeightedProposalContent(n int) []simtypes.WeightedProposalContent { // TestWeightedOperations tests the weights of the operations. func TestWeightedOperations(t *testing.T) { - app, ctx := createTestApp(t, false) + suite, ctx := createTestSuite(t, false) + app := suite.App ctx.WithChainID("test-chain") - cdc := app.AppCodec() + cdc := suite.cdc appParams := make(simtypes.AppParams) - weightesOps := simulation.WeightedOperations(appParams, cdc, app.AccountKeeper, - app.BankKeeper, app.GovKeeper, mockWeightedProposalContent(3), + weightesOps := simulation.WeightedOperations(appParams, cdc, suite.AccountKeeper, + suite.BankKeeper, suite.GovKeeper, mockWeightedProposalContent(3), ) // setup 3 accounts s := rand.NewSource(1) r := rand.New(s) - accs := getTestingAccounts(t, r, app, ctx, 3) + accs := getTestingAccounts(t, r, suite.AccountKeeper, suite.BankKeeper, suite.StakingKeeper, ctx, 3) expected := []struct { weight int @@ -79,9 +90,9 @@ func TestWeightedOperations(t *testing.T) { {0, types.ModuleName, simulation.TypeMsgSubmitProposal}, {1, types.ModuleName, simulation.TypeMsgSubmitProposal}, {2, types.ModuleName, simulation.TypeMsgSubmitProposal}, - {simappparams.DefaultWeightMsgDeposit, types.ModuleName, simulation.TypeMsgDeposit}, - {simappparams.DefaultWeightMsgVote, types.ModuleName, simulation.TypeMsgVote}, - {simappparams.DefaultWeightMsgVoteWeighted, types.ModuleName, simulation.TypeMsgVoteWeighted}, + {simulation.DefaultWeightMsgDeposit, types.ModuleName, simulation.TypeMsgDeposit}, + {simulation.DefaultWeightMsgVote, types.ModuleName, simulation.TypeMsgVote}, + {simulation.DefaultWeightMsgVoteWeighted, types.ModuleName, simulation.TypeMsgVoteWeighted}, } for i, w := range weightesOps { @@ -100,18 +111,19 @@ func TestWeightedOperations(t *testing.T) { // TestSimulateMsgSubmitProposal tests the normal scenario of a valid message of type TypeMsgSubmitProposal. // Abnormal scenarios, where errors occur, are not tested here. func TestSimulateMsgSubmitProposal(t *testing.T) { - app, ctx := createTestApp(t, false) + suite, ctx := createTestSuite(t, false) + app := suite.App // setup 3 accounts s := rand.NewSource(1) r := rand.New(s) - accounts := getTestingAccounts(t, r, app, ctx, 3) + accounts := getTestingAccounts(t, r, suite.AccountKeeper, suite.BankKeeper, suite.StakingKeeper, ctx, 3) // begin a new block app.BeginBlock(abci.RequestBeginBlock{Header: tmproto.Header{Height: app.LastBlockHeight() + 1, AppHash: app.LastCommitID().Hash}}) // execute operation - op := simulation.SimulateMsgSubmitProposal(app.AccountKeeper, app.BankKeeper, app.GovKeeper, MockWeightedProposalContent{3}.ContentSimulatorFn()) + op := simulation.SimulateMsgSubmitProposal(suite.AccountKeeper, suite.BankKeeper, suite.GovKeeper, MockWeightedProposalContent{3}.ContentSimulatorFn()) operationMsg, _, err := op(r, app.BaseApp, ctx, accounts, "") require.NoError(t, err) @@ -132,33 +144,34 @@ func TestSimulateMsgSubmitProposal(t *testing.T) { // TestSimulateMsgDeposit tests the normal scenario of a valid message of type TypeMsgDeposit. // Abnormal scenarios, where errors occur, are not tested here. func TestSimulateMsgDeposit(t *testing.T) { - app, ctx := createTestApp(t, false) + suite, ctx := createTestSuite(t, false) + app := suite.App blockTime := time.Now().UTC() ctx = ctx.WithBlockTime(blockTime) // setup 3 accounts s := rand.NewSource(1) r := rand.New(s) - accounts := getTestingAccounts(t, r, app, ctx, 3) + accounts := getTestingAccounts(t, r, suite.AccountKeeper, suite.BankKeeper, suite.StakingKeeper, ctx, 3) // setup a proposal content := v1beta1.NewTextProposal("Test", "description") - contentMsg, err := v1.NewLegacyContent(content, app.GovKeeper.GetGovernanceAccount(ctx).GetAddress().String()) + contentMsg, err := v1.NewLegacyContent(content, suite.GovKeeper.GetGovernanceAccount(ctx).GetAddress().String()) require.NoError(t, err) submitTime := ctx.BlockHeader().Time - depositPeriod := app.GovKeeper.GetParams(ctx).MaxDepositPeriod + depositPeriod := suite.GovKeeper.GetParams(ctx).MaxDepositPeriod proposal, err := v1.NewProposal([]sdk.Msg{contentMsg}, 1, "", submitTime, submitTime.Add(*depositPeriod)) require.NoError(t, err) - app.GovKeeper.SetProposal(ctx, proposal) + suite.GovKeeper.SetProposal(ctx, proposal) // begin a new block app.BeginBlock(abci.RequestBeginBlock{Header: tmproto.Header{Height: app.LastBlockHeight() + 1, AppHash: app.LastCommitID().Hash, Time: blockTime}}) // execute operation - op := simulation.SimulateMsgDeposit(app.AccountKeeper, app.BankKeeper, app.GovKeeper) + op := simulation.SimulateMsgDeposit(suite.AccountKeeper, suite.BankKeeper, suite.GovKeeper) operationMsg, _, err := op(r, app.BaseApp, ctx, accounts, "") require.NoError(t, err) @@ -178,33 +191,34 @@ func TestSimulateMsgDeposit(t *testing.T) { // TestSimulateMsgVote tests the normal scenario of a valid message of type TypeMsgVote. // Abnormal scenarios, where errors occur, are not tested here. func TestSimulateMsgVote(t *testing.T) { - app, ctx := createTestApp(t, false) + suite, ctx := createTestSuite(t, false) + app := suite.App blockTime := time.Now().UTC() ctx = ctx.WithBlockTime(blockTime) // setup 3 accounts s := rand.NewSource(1) r := rand.New(s) - accounts := getTestingAccounts(t, r, app, ctx, 3) + accounts := getTestingAccounts(t, r, suite.AccountKeeper, suite.BankKeeper, suite.StakingKeeper, ctx, 3) // setup a proposal - govAcc := app.GovKeeper.GetGovernanceAccount(ctx).GetAddress().String() + govAcc := suite.GovKeeper.GetGovernanceAccount(ctx).GetAddress().String() contentMsg, err := v1.NewLegacyContent(v1beta1.NewTextProposal("Test", "description"), govAcc) require.NoError(t, err) submitTime := ctx.BlockHeader().Time - depositPeriod := app.GovKeeper.GetParams(ctx).MaxDepositPeriod + depositPeriod := suite.GovKeeper.GetParams(ctx).MaxDepositPeriod proposal, err := v1.NewProposal([]sdk.Msg{contentMsg}, 1, "", submitTime, submitTime.Add(*depositPeriod)) require.NoError(t, err) - app.GovKeeper.ActivateVotingPeriod(ctx, proposal) + suite.GovKeeper.ActivateVotingPeriod(ctx, proposal) // begin a new block app.BeginBlock(abci.RequestBeginBlock{Header: tmproto.Header{Height: app.LastBlockHeight() + 1, AppHash: app.LastCommitID().Hash, Time: blockTime}}) // execute operation - op := simulation.SimulateMsgVote(app.AccountKeeper, app.BankKeeper, app.GovKeeper) + op := simulation.SimulateMsgVote(suite.AccountKeeper, suite.BankKeeper, suite.GovKeeper) operationMsg, _, err := op(r, app.BaseApp, ctx, accounts, "") require.NoError(t, err) @@ -222,32 +236,33 @@ func TestSimulateMsgVote(t *testing.T) { // TestSimulateMsgVoteWeighted tests the normal scenario of a valid message of type TypeMsgVoteWeighted. // Abnormal scenarios, where errors occur, are not tested here. func TestSimulateMsgVoteWeighted(t *testing.T) { - app, ctx := createTestApp(t, false) + suite, ctx := createTestSuite(t, false) + app := suite.App blockTime := time.Now().UTC() ctx = ctx.WithBlockTime(blockTime) // setup 3 accounts s := rand.NewSource(1) r := rand.New(s) - accounts := getTestingAccounts(t, r, app, ctx, 3) + accounts := getTestingAccounts(t, r, suite.AccountKeeper, suite.BankKeeper, suite.StakingKeeper, ctx, 3) // setup a proposal - govAcc := app.GovKeeper.GetGovernanceAccount(ctx).GetAddress().String() + govAcc := suite.GovKeeper.GetGovernanceAccount(ctx).GetAddress().String() contentMsg, err := v1.NewLegacyContent(v1beta1.NewTextProposal("Test", "description"), govAcc) require.NoError(t, err) submitTime := ctx.BlockHeader().Time - depositPeriod := app.GovKeeper.GetParams(ctx).MaxDepositPeriod + depositPeriod := suite.GovKeeper.GetParams(ctx).MaxDepositPeriod proposal, err := v1.NewProposal([]sdk.Msg{contentMsg}, 1, "", submitTime, submitTime.Add(*depositPeriod)) require.NoError(t, err) - app.GovKeeper.ActivateVotingPeriod(ctx, proposal) + suite.GovKeeper.ActivateVotingPeriod(ctx, proposal) // begin a new block app.BeginBlock(abci.RequestBeginBlock{Header: tmproto.Header{Height: app.LastBlockHeight() + 1, AppHash: app.LastCommitID().Hash, Time: blockTime}}) // execute operation - op := simulation.SimulateMsgVoteWeighted(app.AccountKeeper, app.BankKeeper, app.GovKeeper) + op := simulation.SimulateMsgVoteWeighted(suite.AccountKeeper, suite.BankKeeper, suite.GovKeeper) operationMsg, _, err := op(r, app.BaseApp, ctx, accounts, "") require.NoError(t, err) @@ -262,28 +277,50 @@ func TestSimulateMsgVoteWeighted(t *testing.T) { require.Equal(t, simulation.TypeMsgVoteWeighted, msg.Type()) } +type suite struct { + cdc codec.Codec + AccountKeeper authkeeper.AccountKeeper + BankKeeper bankkeeper.Keeper + GovKeeper *keeper.Keeper + StakingKeeper *stakingkeeper.Keeper + App *runtime.App +} + // returns context and an app with updated mint keeper -func createTestApp(t *testing.T, isCheckTx bool) (*simapp.SimApp, sdk.Context) { - app := simapp.Setup(t, isCheckTx) +func createTestSuite(t *testing.T, isCheckTx bool) (suite, sdk.Context) { + res := suite{} + + app, err := simtestutil.Setup(configurator.NewAppConfig( + configurator.AuthModule(), + configurator.TxModule(), + configurator.ParamsModule(), + configurator.BankModule(), + configurator.StakingModule(), + configurator.GovModule(), + ), &res.AccountKeeper, &res.BankKeeper, &res.GovKeeper, &res.StakingKeeper, &res.cdc) + require.NoError(t, err) ctx := app.BaseApp.NewContext(isCheckTx, tmproto.Header{}) - app.MintKeeper.SetParams(ctx, minttypes.DefaultParams()) - app.MintKeeper.SetMinter(ctx, minttypes.DefaultInitialMinter()) - return app, ctx + res.App = app + return res, ctx } -func getTestingAccounts(t *testing.T, r *rand.Rand, app *simapp.SimApp, ctx sdk.Context, n int) []simtypes.Account { +func getTestingAccounts( + t *testing.T, r *rand.Rand, + accountKeeper authkeeper.AccountKeeper, bankKeeper bankkeeper.Keeper, stakingKeeper *stakingkeeper.Keeper, + ctx sdk.Context, n int, +) []simtypes.Account { accounts := simtypes.RandomAccounts(r, n) - initAmt := app.StakingKeeper.TokensFromConsensusPower(ctx, 200) + initAmt := stakingKeeper.TokensFromConsensusPower(ctx, 200) initCoins := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initAmt)) // add coins to the accounts for _, account := range accounts { - acc := app.AccountKeeper.NewAccountWithAddress(ctx, account.Address) - app.AccountKeeper.SetAccount(ctx, acc) - require.NoError(t, testutil.FundAccount(app.BankKeeper, ctx, account.Address, initCoins)) + acc := accountKeeper.NewAccountWithAddress(ctx, account.Address) + accountKeeper.SetAccount(ctx, acc) + require.NoError(t, testutil.FundAccount(bankKeeper, ctx, account.Address, initCoins)) } return accounts