test(gov): Remove simapp references from module tests and simulations (#13043)

## Description

0 occurences of "simapp" in x/gov.

closes: #12752

- [x] Move some x/gov module tests (those in x/gov root folder) to tests/integration, as they rely on a new module x/distribution
- [x] convert rest of x/gov module tests to use depinject
- [x] remove simapp references from x/gov/simulations
- [x] 0 occurences of "simapp" in x/gov



---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed 
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
This commit is contained in:
Amaury 2022-08-29 16:24:51 +02:00 committed by GitHub
parent b44aecad4b
commit 7b746717ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 417 additions and 245 deletions

View File

@ -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

View File

@ -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
}

View File

@ -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)
}

View File

@ -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

View File

@ -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

View File

@ -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))

View File

@ -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

View File

@ -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) {

View File

@ -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
}

View File

@ -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,

View File

@ -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(),

View File

@ -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())
}
// 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)
ctx := app.BaseApp.NewContext(isCheckTx, tmproto.Header{})
app.MintKeeper.SetParams(ctx, minttypes.DefaultParams())
app.MintKeeper.SetMinter(ctx, minttypes.DefaultInitialMinter())
return app, ctx
type suite struct {
cdc codec.Codec
AccountKeeper authkeeper.AccountKeeper
BankKeeper bankkeeper.Keeper
GovKeeper *keeper.Keeper
StakingKeeper *stakingkeeper.Keeper
App *runtime.App
}
func getTestingAccounts(t *testing.T, r *rand.Rand, app *simapp.SimApp, ctx sdk.Context, n int) []simtypes.Account {
// returns context and an app with updated mint keeper
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{})
res.App = app
return res, ctx
}
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