Skip to content

Commit

Permalink
feat(campaign): add simulation for msgEditCampaign (#712)
Browse files Browse the repository at this point in the history
* add simulation for `msgEditCampaign`

* fix tests
  • Loading branch information
giunatale authored Apr 13, 2022
1 parent f5d9d70 commit 5367ac4
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 6 deletions.
2 changes: 1 addition & 1 deletion x/campaign/client/cli/tx_edit_campaign.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ func CmdEditCampaign() *cobra.Command {

msg := types.NewMsgEditCampaign(
clientCtx.GetFromAddress().String(),
name,
campaignID,
name,
metadataBytes,
)

Expand Down
14 changes: 13 additions & 1 deletion x/campaign/module_simulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

const (
defaultWeightMsgCreateCampaign = 25
defaultWeightMsgEditCampaign = 20
defaultWeightMsgUpdateTotalSupply = 20
defaultWeightMsgInitializeMainnet = 5
defaultWeightMsgAddShares = 20
Expand All @@ -26,6 +27,7 @@ const (
defaultWeightMsgSendVouchers = 20

opWeightMsgCreateCampaign = "op_weight_msg_create_campaign"
opWeightMsgEditCampaign = "op_weight_msg_edit_campaign"
opWeightMsgUpdateTotalSupply = "op_weight_msg_update_total_supply"
opWeightMsgInitializeMainnet = "op_weight_msg_initialize_mainnet"
opWeightMsgAddShares = "op_weight_msg_add_shares"
Expand Down Expand Up @@ -70,6 +72,7 @@ func (am AppModule) RegisterStoreDecoder(_ sdk.StoreDecoderRegistry) {}
func (am AppModule) WeightedOperations(simState module.SimulationState) []simtypes.WeightedOperation {
var (
weightMsgCreateCampaign int
weightMsgEditCampaign int
weightMsgUpdateTotalSupply int
weightMsgInitializeMainnet int
weightMsgAddShares int
Expand All @@ -88,6 +91,11 @@ func (am AppModule) WeightedOperations(simState module.SimulationState) []simtyp
weightMsgCreateCampaign = defaultWeightMsgCreateCampaign
},
)
appParams.GetOrGenerate(cdc, opWeightMsgEditCampaign, &weightMsgEditCampaign, nil,
func(_ *rand.Rand) {
weightMsgEditCampaign = defaultWeightMsgEditCampaign
},
)
appParams.GetOrGenerate(cdc, opWeightMsgUpdateTotalSupply, &weightMsgUpdateTotalSupply, nil,
func(_ *rand.Rand) {
weightMsgUpdateTotalSupply = defaultWeightMsgUpdateTotalSupply
Expand Down Expand Up @@ -137,7 +145,11 @@ func (am AppModule) WeightedOperations(simState module.SimulationState) []simtyp
return []simtypes.WeightedOperation{
simulation.NewWeightedOperation(
weightMsgCreateCampaign,
campaignsim.SimulateMsgCreateCampaign(am.keeper, am.accountKeeper, am.bankKeeper, am.profileKeeper),
campaignsim.SimulateMsgCreateCampaign(am.accountKeeper, am.bankKeeper, am.profileKeeper, am.keeper),
),
simulation.NewWeightedOperation(
weightMsgEditCampaign,
campaignsim.SimulateMsgEditCampaign(am.accountKeeper, am.bankKeeper, am.profileKeeper, am.keeper),
),
simulation.NewWeightedOperation(
weightMsgInitializeMainnet,
Expand Down
41 changes: 39 additions & 2 deletions x/campaign/simulation/simulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ func deliverSimTx(

// SimulateMsgCreateCampaign simulates a MsgCreateCampaign message
func SimulateMsgCreateCampaign(
k keeper.Keeper,
ak types.AccountKeeper,
bk types.BankKeeper,
pk types.ProfileKeeper,
k keeper.Keeper,
) simtypes.Operation {
return func(
r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string,
Expand All @@ -77,7 +77,44 @@ func SimulateMsgCreateCampaign(
}
}

// TODO add SimulateMsgEditCampaign
// SimulateMsgEditCampaign simulates a MsgEditCampaign message
func SimulateMsgEditCampaign(ak types.AccountKeeper, bk types.BankKeeper, pk types.ProfileKeeper, k keeper.Keeper) simtypes.Operation {
return func(
r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string,
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
msg := &types.MsgEditCampaign{}

simAccount, campID, found := GetCoordSimAccountWithCampaignID(r, ctx, pk, k, accs, false, false)
if !found {
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "skip edit campaign"), nil, nil
}

var newName string
var newMetadata []byte

name := r.Intn(100) < 50
metadata := r.Intn(100) < 50
// ensure there is always a value to edit
if !name && !metadata {
metadata = true
}

if name {
newName = sample.CampaignName(r)
}
if metadata {
newMetadata = sample.Metadata(r, 20)
}

msg = types.NewMsgEditCampaign(
simAccount.Address.String(),
campID,
newName,
newMetadata,
)
return deliverSimTx(r, app, ctx, ak, bk, simAccount, msg, sdk.NewCoins())
}
}

// SimulateMsgUpdateTotalSupply simulates a MsgUpdateTotalSupply message
func SimulateMsgUpdateTotalSupply(
Expand Down
6 changes: 4 additions & 2 deletions x/campaign/types/msg_edit_campaign.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import (
spntypes "github.com/tendermint/spn/pkg/types"
)

const TypeMsgEditCampaign = "edit_campaign"

var _ sdk.Msg = &MsgEditCampaign{}

func NewMsgEditCampaign(coordinator, name string, campaignID uint64, metadata []byte) *MsgEditCampaign {
func NewMsgEditCampaign(coordinator string, campaignID uint64, name string, metadata []byte) *MsgEditCampaign {
return &MsgEditCampaign{
Coordinator: coordinator,
CampaignID: campaignID,
Expand All @@ -23,7 +25,7 @@ func (msg *MsgEditCampaign) Route() string {
}

func (msg *MsgEditCampaign) Type() string {
return "UpdateCampaignName"
return TypeMsgEditCampaign
}

func (msg *MsgEditCampaign) GetSigners() []sdk.AccAddress {
Expand Down

0 comments on commit 5367ac4

Please sign in to comment.