Skip to content

Commit

Permalink
feat: add proposal simulations for MsgRecoverClient and MsgIBCSoftwar…
Browse files Browse the repository at this point in the history
…eUpgrade (cosmos#4623)
  • Loading branch information
charleenfei authored Sep 13, 2023
1 parent 26f7fe3 commit baa4f40
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 18 deletions.
4 changes: 2 additions & 2 deletions modules/apps/27-interchain-accounts/simulation/proposals.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func ProposalMsgs() []simtypes.WeightedProposalMsg {
}
}

// SimulateHostMsgUpdateParams returns a random MsgUpdateParams for the host module
// SimulateHostMsgUpdateParams returns a MsgUpdateParams for the host module
func SimulateHostMsgUpdateParams(_ *rand.Rand, _ sdk.Context, _ []simtypes.Account) sdk.Msg {
var signer sdk.AccAddress = address.Module("gov")
params := types.DefaultParams()
Expand All @@ -47,7 +47,7 @@ func SimulateHostMsgUpdateParams(_ *rand.Rand, _ sdk.Context, _ []simtypes.Accou
}
}

// SimulateControllerMsgUpdateParams returns a random MsgUpdateParams for the controller module
// SimulateControllerMsgUpdateParams returns a MsgUpdateParams for the controller module
func SimulateControllerMsgUpdateParams(_ *rand.Rand, _ sdk.Context, _ []simtypes.Account) sdk.Msg {
var signer sdk.AccAddress = address.Module("gov")
params := controllertypes.DefaultParams()
Expand Down
2 changes: 1 addition & 1 deletion modules/apps/transfer/simulation/proposals.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func ProposalMsgs() []simtypes.WeightedProposalMsg {
}
}

// SimulateMsgUpdateParams returns a random MsgUpdateParams
// SimulateMsgUpdateParams returns a MsgUpdateParams
func SimulateMsgUpdateParams(_ *rand.Rand, _ sdk.Context, _ []simtypes.Account) sdk.Msg {
var gov sdk.AccAddress = address.Module("gov")
params := types.DefaultParams()
Expand Down
69 changes: 63 additions & 6 deletions modules/core/simulation/proposals.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package simulation

import (
"math/rand"
"time"

upgradetypes "cosmossdk.io/x/upgrade/types"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/address"
Expand All @@ -10,32 +13,46 @@ import (

"github.com/cosmos/ibc-go/v8/modules/core/02-client/types"
connectiontypes "github.com/cosmos/ibc-go/v8/modules/core/03-connection/types"
commitmenttypes "github.com/cosmos/ibc-go/v8/modules/core/23-commitment/types"
ibctm "github.com/cosmos/ibc-go/v8/modules/light-clients/07-tendermint"
)

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

OpWeightMsgUpdateParams = "op_weight_msg_update_params" // #nosec
OpWeightMsgUpdateParams = "op_weight_msg_update_params" // #nosec
OpWeightMsgRecoverClient = "op_weight_msg_recover_client" // #nosec
OpWeightMsgIBCSoftwareUpgrade = "op_weight_msg_schedule_ibc_software_upgrade" // #nosec
)

// ProposalMsgs defines the module weighted proposals' contents
func ProposalMsgs() []simtypes.WeightedProposalMsg {
return []simtypes.WeightedProposalMsg{
simulation.NewWeightedProposalMsg(
OpWeightMsgUpdateParams,
DefaultWeightMsgUpdateParams,
DefaultWeight,
SimulateClientMsgUpdateParams,
),
simulation.NewWeightedProposalMsg(
OpWeightMsgUpdateParams,
DefaultWeightMsgUpdateParams,
DefaultWeight,
SimulateConnectionMsgUpdateParams,
),
simulation.NewWeightedProposalMsg(
OpWeightMsgRecoverClient,
DefaultWeight,
SimulateClientMsgRecoverClient,
),
simulation.NewWeightedProposalMsg(
OpWeightMsgIBCSoftwareUpgrade,
DefaultWeight,
SimulateClientMsgScheduleIBCSoftwareUpgrade,
),
}
}

// SimulateClientMsgUpdateParams returns a random MsgUpdateParams for 02-client
// SimulateClientMsgUpdateParams returns a 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()
Expand All @@ -47,7 +64,47 @@ func SimulateClientMsgUpdateParams(r *rand.Rand, _ sdk.Context, _ []simtypes.Acc
}
}

// SimulateConnectionMsgUpdateParams returns a random MsgUpdateParams 03-connection
// SimulateClientMsgRecoverClient returns a MsgRecoverClient for 02-client
func SimulateClientMsgRecoverClient(r *rand.Rand, _ sdk.Context, _ []simtypes.Account) sdk.Msg {
var signer sdk.AccAddress = address.Module("gov")

return &types.MsgRecoverClient{
Signer: signer.String(),
SubjectClientId: "07-tendermint-0",
SubstituteClientId: "07-tendermint-1",
}
}

// SimulateClientMsgScheduleIBCSoftwareUpgrade returns a MsgScheduleIBCSoftwareUpgrade for 02-client
func SimulateClientMsgScheduleIBCSoftwareUpgrade(r *rand.Rand, _ sdk.Context, _ []simtypes.Account) sdk.Msg {
var signer sdk.AccAddress = address.Module("gov")

chainID := "chain-a-0"
ubdPeriod := time.Hour * 24 * 7 * 2
upgradePath := []string{"upgrade", "upgradedIBCState"}

upgradedClientState := &ibctm.ClientState{
ChainId: chainID,
UnbondingPeriod: ubdPeriod,
ProofSpecs: commitmenttypes.GetSDKSpecs(),
UpgradePath: upgradePath,
}
anyClient, err := types.PackClientState(upgradedClientState)
if err != nil {
panic(err)
}

return &types.MsgIBCSoftwareUpgrade{
Signer: signer.String(),
Plan: upgradetypes.Plan{
Name: "upgrade",
Height: 100,
},
UpgradedClientState: anyClient,
}
}

// SimulateConnectionMsgUpdateParams returns a MsgUpdateParams 03-connection
func SimulateConnectionMsgUpdateParams(r *rand.Rand, _ sdk.Context, _ []simtypes.Account) sdk.Msg {
var signer sdk.AccAddress = address.Module("gov")
params := connectiontypes.DefaultParams()
Expand Down
44 changes: 35 additions & 9 deletions modules/core/simulation/proposals_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package simulation_test
import (
"math/rand"
"testing"
"time"

"github.com/stretchr/testify/require"

Expand All @@ -12,9 +13,10 @@ import (

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

"github.com/cosmos/ibc-go/v8/modules/core/02-client/types"
clienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types"
connectiontypes "github.com/cosmos/ibc-go/v8/modules/core/03-connection/types"
"github.com/cosmos/ibc-go/v8/modules/core/simulation"
ibctm "github.com/cosmos/ibc-go/v8/modules/light-clients/07-tendermint"
)

func TestProposalMsgs(t *testing.T) {
Expand All @@ -27,31 +29,55 @@ func TestProposalMsgs(t *testing.T) {

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

w0 := weightedProposalMsgs[0]
require.Equal(t, 4, len(weightedProposalMsgs))

// tests w0 interface:
w0 := weightedProposalMsgs[0]
require.Equal(t, simulation.OpWeightMsgUpdateParams, w0.AppParamsKey())
require.Equal(t, simulation.DefaultWeightMsgUpdateParams, w0.DefaultWeight())
require.Equal(t, simulation.DefaultWeight, w0.DefaultWeight())

msg := w0.MsgSimulatorFn()(r, ctx, accounts)
msgUpdateParams, ok := msg.(*types.MsgUpdateParams)
msgUpdateParams, ok := msg.(*clienttypes.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:
w1 := weightedProposalMsgs[1]
require.Equal(t, simulation.OpWeightMsgUpdateParams, w1.AppParamsKey())
require.Equal(t, simulation.DefaultWeightMsgUpdateParams, w1.DefaultWeight())
require.Equal(t, simulation.DefaultWeight, 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)

// tests w2 interface:
w2 := weightedProposalMsgs[2]
require.Equal(t, simulation.OpWeightMsgRecoverClient, w2.AppParamsKey())
require.Equal(t, simulation.DefaultWeight, w2.DefaultWeight())

msg2 := w2.MsgSimulatorFn()(r, ctx, accounts)
msgRecoverClient, ok := msg2.(*clienttypes.MsgRecoverClient)
require.True(t, ok)

require.Equal(t, sdk.AccAddress(address.Module("gov")).String(), msgRecoverClient.Signer)
require.EqualValues(t, "07-tendermint-1", msgRecoverClient.SubstituteClientId)

// tests w3 interface:
w3 := weightedProposalMsgs[3]
require.Equal(t, simulation.OpWeightMsgIBCSoftwareUpgrade, w3.AppParamsKey())
require.Equal(t, simulation.DefaultWeight, w3.DefaultWeight())

msg3 := w3.MsgSimulatorFn()(r, ctx, accounts)
msgIBCSoftwareUpgrade, ok := msg3.(*clienttypes.MsgIBCSoftwareUpgrade)
require.True(t, ok)

require.Equal(t, sdk.AccAddress(address.Module("gov")).String(), msgIBCSoftwareUpgrade.Signer)
clientState, err := clienttypes.UnpackClientState(msgIBCSoftwareUpgrade.UpgradedClientState)
require.NoError(t, err)
require.EqualValues(t, time.Hour*24*7*2, clientState.(*ibctm.ClientState).UnbondingPeriod)
}

0 comments on commit baa4f40

Please sign in to comment.