-
Notifications
You must be signed in to change notification settings - Fork 586
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add proposal simulator interface function and tests (#4466)
Co-authored-by: colin axner <25233464+colin-axner@users.noreply.github.com>
- Loading branch information
1 parent
40b727a
commit c4f947e
Showing
9 changed files
with
340 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
modules/apps/27-interchain-accounts/simulation/proposals.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
return []simtypes.WeightedProposalMsg{ | ||
simulation.NewWeightedProposalMsg( | ||
OpWeightMsgUpdateParams, | ||
DefaultWeightMsgUpdateParams, | ||
SimulateHostMsgUpdateParams, | ||
), | ||
simulation.NewWeightedProposalMsg( | ||
OpWeightMsgUpdateParams, | ||
DefaultWeightMsgUpdateParams, | ||
SimulateControllerMsgUpdateParams, | ||
), | ||
} | ||
} | ||
|
||
// 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 | ||
|
||
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
56
modules/apps/27-interchain-accounts/simulation/proposals_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
return &types.MsgUpdateParams{ | ||
Signer: signer.String(), | ||
Params: params, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
), | ||
} | ||
} | ||
|
||
// 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, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |