Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

feat: add proposal simulator interface function and tests #4466

6 changes: 6 additions & 0 deletions modules/apps/27-interchain-accounts/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ var (
_ module.AppModule = (*AppModule)(nil)
_ module.AppModuleBasic = (*AppModuleBasic)(nil)
_ module.AppModuleSimulation = (*AppModule)(nil)
_ module.HasProposalMsgs = AppModule{}
_ appmodule.AppModule = (*AppModule)(nil)

_ porttypes.IBCModule = (*host.IBCModule)(nil)
Expand Down Expand Up @@ -218,6 +219,11 @@ func (AppModule) GenerateGenesisState(simState *module.SimulationState) {
simulation.RandomizedGenState(simState)
}

// ProposalMsgs returns msgs used for governance proposals for simulations.
func (AppModule) ProposalMsgs(simState module.SimulationState) []simtypes.WeightedProposalMsg {
return simulation.ProposalMsgs()
}

// WeightedOperations is unimplemented.
func (AppModule) WeightedOperations(simState module.SimulationState) []simtypes.WeightedOperation {
return nil
Expand Down
60 changes: 60 additions & 0 deletions modules/apps/27-interchain-accounts/simulation/proposals.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package simulation

import (
"math/rand"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/address"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/cosmos/cosmos-sdk/x/simulation"

controllertypes "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts/controller/types"
"github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts/host/types"
)

// Simulation operation weights constants
const (
DefaultWeightMsgUpdateParams int = 100

OpWeightMsgUpdateParams = "op_weight_msg_update_params" // #nosec
)

// ProposalMsgs defines the module weighted proposals' contents
func ProposalMsgs() []simtypes.WeightedProposalMsg {
charleenfei marked this conversation as resolved.
Show resolved Hide resolved
return []simtypes.WeightedProposalMsg{
simulation.NewWeightedProposalMsg(
OpWeightMsgUpdateParams,
DefaultWeightMsgUpdateParams,
SimulateHostMsgUpdateParams,
),
simulation.NewWeightedProposalMsg(
OpWeightMsgUpdateParams,
DefaultWeightMsgUpdateParams,
SimulateControllerMsgUpdateParams,
),
charleenfei marked this conversation as resolved.
Show resolved Hide resolved
}
}

// SimulateHostMsgUpdateParams returns a random MsgUpdateParams for the host module
func SimulateHostMsgUpdateParams(r *rand.Rand, _ sdk.Context, _ []simtypes.Account) sdk.Msg {
var signer sdk.AccAddress = address.Module("gov")
params := types.DefaultParams()
params.HostEnabled = false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we randomise this value as SDK does?

Copy link
Contributor Author

@charleenfei charleenfei Sep 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, we can do this! I guess I just wanted to keep it one value to make testing easier 😅

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the purpose of simulations is that it performs random operations, we don't utilize simulations so this is mostly for chains which run sdk simulations


return &types.MsgUpdateParams{
Signer: signer.String(),
Params: params,
}
}

// SimulateControllerMsgUpdateParams returns a random MsgUpdateParams for the controller module
func SimulateControllerMsgUpdateParams(r *rand.Rand, _ sdk.Context, _ []simtypes.Account) sdk.Msg {
var signer sdk.AccAddress = address.Module("gov")
params := controllertypes.DefaultParams()
params.ControllerEnabled = false

return &controllertypes.MsgUpdateParams{
Signer: signer.String(),
Params: params,
}
}
56 changes: 56 additions & 0 deletions modules/apps/27-interchain-accounts/simulation/proposals_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package simulation_test

import (
"math/rand"
"testing"

"github.com/stretchr/testify/require"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/address"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"

cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"

controllertypes "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts/controller/types"
"github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts/host/types"
"github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts/simulation"
)

func TestProposalMsgs(t *testing.T) {
// initialize parameters
s := rand.NewSource(1)
r := rand.New(s)

ctx := sdk.NewContext(nil, cmtproto.Header{}, true, nil)
accounts := simtypes.RandomAccounts(r, 3)

// execute ProposalMsgs function
weightedProposalMsgs := simulation.ProposalMsgs()
require.Equal(t, 2, len(weightedProposalMsgs))
w0 := weightedProposalMsgs[0]

// tests w0 interface:
require.Equal(t, simulation.OpWeightMsgUpdateParams, w0.AppParamsKey())
require.Equal(t, simulation.DefaultWeightMsgUpdateParams, w0.DefaultWeight())

msg := w0.MsgSimulatorFn()(r, ctx, accounts)
msgUpdateHostParams, ok := msg.(*types.MsgUpdateParams)
require.True(t, ok)

require.Equal(t, sdk.AccAddress(address.Module("gov")).String(), msgUpdateHostParams.Signer)
require.Equal(t, msgUpdateHostParams.Params.HostEnabled, false)

w1 := weightedProposalMsgs[1]

// tests w1 interface:
require.Equal(t, simulation.OpWeightMsgUpdateParams, w1.AppParamsKey())
require.Equal(t, simulation.DefaultWeightMsgUpdateParams, w1.DefaultWeight())

msg1 := w1.MsgSimulatorFn()(r, ctx, accounts)
msgUpdateControllerParams, ok := msg1.(*controllertypes.MsgUpdateParams)
require.True(t, ok)

require.Equal(t, sdk.AccAddress(address.Module("gov")).String(), msgUpdateControllerParams.Signer)
require.Equal(t, msgUpdateControllerParams.Params.ControllerEnabled, false)
}
14 changes: 10 additions & 4 deletions modules/apps/transfer/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ import (
)

var (
_ module.AppModule = (*AppModule)(nil)
_ module.AppModuleBasic = (*AppModuleBasic)(nil)
_ appmodule.AppModule = (*AppModule)(nil)
_ porttypes.IBCModule = (*IBCModule)(nil)
_ module.AppModule = (*AppModule)(nil)
_ module.AppModuleBasic = (*AppModuleBasic)(nil)
_ module.HasProposalMsgs = AppModule{}
_ appmodule.AppModule = (*AppModule)(nil)
_ porttypes.IBCModule = (*IBCModule)(nil)
)

// AppModuleBasic is the IBC Transfer AppModuleBasic
Expand Down Expand Up @@ -154,6 +155,11 @@ func (AppModule) GenerateGenesisState(simState *module.SimulationState) {
simulation.RandomizedGenState(simState)
}

// ProposalMsgs returns msgs used for governance proposals for simulations.
func (AppModule) ProposalMsgs(simState module.SimulationState) []simtypes.WeightedProposalMsg {
return simulation.ProposalMsgs()
}

// RegisterStoreDecoder registers a decoder for transfer module's types
func (am AppModule) RegisterStoreDecoder(sdr simtypes.StoreDecoderRegistry) {
sdr[types.StoreKey] = simulation.NewDecodeStore(am.keeper)
Expand Down
42 changes: 42 additions & 0 deletions modules/apps/transfer/simulation/proposals.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package simulation

import (
"math/rand"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/address"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/cosmos/cosmos-sdk/x/simulation"

"github.com/cosmos/ibc-go/v7/modules/apps/transfer/types"
)

// Simulation operation weights constants
const (
DefaultWeightMsgUpdateParams int = 100

OpWeightMsgUpdateParams = "op_weight_msg_update_params" // #nosec
)

// ProposalMsgs defines the module weighted proposals' contents
func ProposalMsgs() []simtypes.WeightedProposalMsg {
return []simtypes.WeightedProposalMsg{
simulation.NewWeightedProposalMsg(
OpWeightMsgUpdateParams,
DefaultWeightMsgUpdateParams,
SimulateMsgUpdateParams,
),
}
}

// SimulateMsgUpdateParams returns a random MsgUpdateParams
func SimulateMsgUpdateParams(r *rand.Rand, _ sdk.Context, _ []simtypes.Account) sdk.Msg {
var signer sdk.AccAddress = address.Module("gov")
params := types.DefaultParams()
params.SendEnabled = false
charleenfei marked this conversation as resolved.
Show resolved Hide resolved

return &types.MsgUpdateParams{
Signer: signer.String(),
Params: params,
}
}
43 changes: 43 additions & 0 deletions modules/apps/transfer/simulation/proposals_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package simulation_test

import (
"math/rand"
"testing"

"github.com/stretchr/testify/require"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/address"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"

cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"

"github.com/cosmos/ibc-go/v7/modules/apps/transfer/simulation"
"github.com/cosmos/ibc-go/v7/modules/apps/transfer/types"
)

func TestProposalMsgs(t *testing.T) {
// initialize parameters
s := rand.NewSource(1)
r := rand.New(s)

ctx := sdk.NewContext(nil, cmtproto.Header{}, true, nil)
accounts := simtypes.RandomAccounts(r, 3)

// execute ProposalMsgs function
weightedProposalMsgs := simulation.ProposalMsgs()
require.Equal(t, len(weightedProposalMsgs), 1)

w0 := weightedProposalMsgs[0]

// tests w0 interface:
require.Equal(t, simulation.OpWeightMsgUpdateParams, w0.AppParamsKey())
require.Equal(t, simulation.DefaultWeightMsgUpdateParams, w0.DefaultWeight())

msg := w0.MsgSimulatorFn()(r, ctx, accounts)
msgUpdateParams, ok := msg.(*types.MsgUpdateParams)
require.True(t, ok)

require.Equal(t, sdk.AccAddress(address.Module("gov")).String(), msgUpdateParams.Signer)
require.EqualValues(t, msgUpdateParams.Params.SendEnabled, false)
}
6 changes: 6 additions & 0 deletions modules/core/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ var (
_ module.AppModule = (*AppModule)(nil)
_ module.AppModuleBasic = (*AppModuleBasic)(nil)
_ module.AppModuleSimulation = (*AppModule)(nil)
_ module.HasProposalMsgs = AppModule{}
_ appmodule.AppModule = (*AppModule)(nil)
)

Expand Down Expand Up @@ -193,6 +194,11 @@ func (AppModule) GenerateGenesisState(simState *module.SimulationState) {
simulation.RandomizedGenState(simState)
}

// ProposalMsgs returns msgs used for governance proposals for simulations.
func (AppModule) ProposalMsgs(simState module.SimulationState) []simtypes.WeightedProposalMsg {
return simulation.ProposalMsgs()
}

// RegisterStoreDecoder registers a decoder for ibc module's types
func (am AppModule) RegisterStoreDecoder(sdr simtypes.StoreDecoderRegistry) {
sdr[exported.StoreKey] = simulation.NewDecodeStore(*am.keeper)
Expand Down
60 changes: 60 additions & 0 deletions modules/core/simulation/proposals.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package simulation

import (
"math/rand"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/address"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/cosmos/cosmos-sdk/x/simulation"

"github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
connectiontypes "github.com/cosmos/ibc-go/v7/modules/core/03-connection/types"
)

// Simulation operation weights constants
const (
DefaultWeightMsgUpdateParams int = 100

OpWeightMsgUpdateParams = "op_weight_msg_update_params" // #nosec
)

// ProposalMsgs defines the module weighted proposals' contents
func ProposalMsgs() []simtypes.WeightedProposalMsg {
return []simtypes.WeightedProposalMsg{
simulation.NewWeightedProposalMsg(
OpWeightMsgUpdateParams,
DefaultWeightMsgUpdateParams,
SimulateClientMsgUpdateParams,
),
simulation.NewWeightedProposalMsg(
OpWeightMsgUpdateParams,
DefaultWeightMsgUpdateParams,
SimulateConnectionMsgUpdateParams,
),
charleenfei marked this conversation as resolved.
Show resolved Hide resolved
}
}

// SimulateClientMsgUpdateParams returns a random MsgUpdateParams for 02-client
func SimulateClientMsgUpdateParams(r *rand.Rand, _ sdk.Context, _ []simtypes.Account) sdk.Msg {
var signer sdk.AccAddress = address.Module("gov")
params := types.DefaultParams()
params.AllowedClients = []string{"06-solomachine", "07-tendermint"}

return &types.MsgUpdateParams{
Signer: signer.String(),
Params: params,
}
}

// SimulateConnectionMsgUpdateParams returns a random MsgUpdateParams 03-connection
func SimulateConnectionMsgUpdateParams(r *rand.Rand, _ sdk.Context, _ []simtypes.Account) sdk.Msg {
var signer sdk.AccAddress = address.Module("gov")
params := connectiontypes.DefaultParams()
params.MaxExpectedTimePerBlock = uint64(100)

return &connectiontypes.MsgUpdateParams{
Signer: signer.String(),
Params: params,
}
}
57 changes: 57 additions & 0 deletions modules/core/simulation/proposals_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package simulation_test

import (
"math/rand"
"testing"

"github.com/stretchr/testify/require"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/address"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"

cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"

"github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
connectiontypes "github.com/cosmos/ibc-go/v7/modules/core/03-connection/types"
"github.com/cosmos/ibc-go/v7/modules/core/simulation"
)

func TestProposalMsgs(t *testing.T) {
// initialize parameters
s := rand.NewSource(1)
r := rand.New(s)

ctx := sdk.NewContext(nil, cmtproto.Header{}, true, nil)
accounts := simtypes.RandomAccounts(r, 3)

// execute ProposalMsgs function
weightedProposalMsgs := simulation.ProposalMsgs()
require.Equal(t, 2, len(weightedProposalMsgs))

w0 := weightedProposalMsgs[0]

// tests w0 interface:
require.Equal(t, simulation.OpWeightMsgUpdateParams, w0.AppParamsKey())
require.Equal(t, simulation.DefaultWeightMsgUpdateParams, w0.DefaultWeight())

msg := w0.MsgSimulatorFn()(r, ctx, accounts)
msgUpdateParams, ok := msg.(*types.MsgUpdateParams)
require.True(t, ok)

require.Equal(t, sdk.AccAddress(address.Module("gov")).String(), msgUpdateParams.Signer)
require.EqualValues(t, []string{"06-solomachine", "07-tendermint"}, msgUpdateParams.Params.AllowedClients)

w1 := weightedProposalMsgs[1]

// tests w1 interface:
require.Equal(t, simulation.OpWeightMsgUpdateParams, w1.AppParamsKey())
require.Equal(t, simulation.DefaultWeightMsgUpdateParams, w1.DefaultWeight())

msg1 := w1.MsgSimulatorFn()(r, ctx, accounts)
msgUpdateConnectionParams, ok := msg1.(*connectiontypes.MsgUpdateParams)
require.True(t, ok)

require.Equal(t, sdk.AccAddress(address.Module("gov")).String(), msgUpdateParams.Signer)
require.EqualValues(t, uint64(100), msgUpdateConnectionParams.Params.MaxExpectedTimePerBlock)
}
Loading