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

Add gov proposal simulations #1107

Merged
merged 10 commits into from
Dec 9, 2022
16 changes: 11 additions & 5 deletions app/params/weights.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,15 @@ const (
DefaultWeightMsgClearAdmin int = 10
DefaultWeightMsgMigrateContract int = 50

DefaultWeightStoreCodeProposal int = 5
DefaultWeightInstantiateContractProposal int = 5
DefaultWeightUpdateAdminProposal int = 5
DefaultWeightExecuteContractProposal int = 5
DefaultWeightClearAdminProposal int = 5
DefaultWeightStoreCodeProposal int = 5
DefaultWeightInstantiateContractProposal int = 5
DefaultWeightUpdateAdminProposal int = 5
DefaultWeightExecuteContractProposal int = 5
DefaultWeightClearAdminProposal int = 5
DefaultWeightMigrateContractProposal int = 5
DefaultWeightSudoContractProposal int = 5
DefaultWeightPinCodesProposal int = 5
DefaultWeightUnpinCodesProposal int = 5
DefaultWeightUpdateInstantiateConfigProposal int = 5
DefaultWeightStoreAndInstantiateContractProposal int = 5
)
195 changes: 188 additions & 7 deletions x/wasm/simulation/proposals.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,17 @@ import (
)

const (
WeightStoreCodeProposal = "weight_store_code_proposal"
WeightInstantiateContractProposal = "weight_instantiate_contract_proposal"
WeightUpdateAdminProposal = "weight_update_admin_proposal"
WeightExeContractProposal = "weight_execute_contract_proposal"
WeightClearAdminProposal = "weight_clear_admin_proposal"
WeightStoreCodeProposal = "weight_store_code_proposal"
WeightInstantiateContractProposal = "weight_instantiate_contract_proposal"
WeightUpdateAdminProposal = "weight_update_admin_proposal"
WeightExeContractProposal = "weight_execute_contract_proposal"
WeightClearAdminProposal = "weight_clear_admin_proposal"
WeightMigrateContractProposal = "weight_migrate_contract_proposal"
WeightSudoContractProposal = "weight_sudo_contract_proposal"
WeightPinCodesProposal = "weight_pin_codes_proposal"
WeightUnpinCodesProposal = "weight_unpin_codes_proposal"
WeightUpdateInstantiateConfigProposal = "weight_update_instantiate_config_proposal"
WeightStoreAndInstantiateContractProposal = "weight_store_and_instantiate_contract_proposal"
)

func ProposalContents(bk BankKeeper, wasmKeeper WasmKeeper) []simtypes.WeightedProposalContent {
Expand Down Expand Up @@ -60,9 +66,57 @@ func ProposalContents(bk BankKeeper, wasmKeeper WasmKeeper) []simtypes.WeightedP
params.DefaultWeightClearAdminProposal,
SimulateClearAdminProposal(
wasmKeeper,
DefaultSimulateClearAdminProposalContractSelector,
DefaultSimulateContractSelector,
),
),
simulation.NewWeightedProposalContent(
WeightMigrateContractProposal,
params.DefaultWeightMigrateContractProposal,
SimulateMigrateContractProposal(
wasmKeeper,
DefaultSimulateContractSelector,
DefaultSimulationCodeIDSelector,
),
),
// simulation.NewWeightedProposalContent(
// WeightSudoContractProposal,
// params.DefaultWeightSudoContractProposal,
// SimulateSudoContractProposal(
// wasmKeeper,
// DefaultSimulateContractSelector,
// ),
// ),
simulation.NewWeightedProposalContent(
WeightPinCodesProposal,
params.DefaultWeightPinCodesProposal,
SimulatePinContractProposal(
wasmKeeper,
DefaultSimulationCodeIDSelector,
),
),
simulation.NewWeightedProposalContent(
WeightUnpinCodesProposal,
params.DefaultWeightUnpinCodesProposal,
SimulateUnpinContractProposal(
wasmKeeper,
DefaultSimulationCodeIDSelector,
),
),
simulation.NewWeightedProposalContent(
WeightUpdateInstantiateConfigProposal,
params.DefaultWeightUpdateInstantiateConfigProposal,
SimulateUpdateInstantiateConfigProposal(
wasmKeeper,
DefaultSimulationCodeIDSelector,
),
),
// simulation.NewWeightedProposalContent(
// WeightStoreAndInstantiateContractProposal,
// params.DefaultWeightStoreAndInstantiateContractProposal,
// SimulateStoreAndInstantiateContractProposal(
// wasmKeeper,
// ),
// ),
}
}

Expand Down Expand Up @@ -196,7 +250,7 @@ func SimulateUpdateAdminProposal(wasmKeeper WasmKeeper, contractSelector UpdateA

type ClearAdminContractSelector func(sdk.Context, WasmKeeper) sdk.AccAddress

func DefaultSimulateClearAdminProposalContractSelector(
func DefaultSimulateContractSelector(
ctx sdk.Context,
wasmKeeper WasmKeeper,
) sdk.AccAddress {
Expand All @@ -223,3 +277,130 @@ func SimulateClearAdminProposal(wasmKeeper WasmKeeper, contractSelector ClearAdm
)
}
}

type MigrateContractProposalContractSelector func(sdk.Context, WasmKeeper) sdk.AccAddress

// Simulate migrate contract proposal
func SimulateMigrateContractProposal(wasmKeeper WasmKeeper, contractSelector MigrateContractProposalContractSelector, codeSelector CodeIDSelector) simtypes.ContentSimulatorFn {
return func(r *rand.Rand, ctx sdk.Context, accs []simtypes.Account) simtypes.Content {
ctAddress := contractSelector(ctx, wasmKeeper)
if ctAddress == nil {
return nil
}

codeID := codeSelector(ctx, wasmKeeper)
if codeID == 0 {
return nil
}

return types.NewMigrateContractProposal(
simtypes.RandStringOfLength(r, 10),
simtypes.RandStringOfLength(r, 10),
ctAddress.String(),
codeID,
[]byte(`{}`),
)
}
}

type SudoContractProposalContractSelector func(sdk.Context, WasmKeeper) sdk.AccAddress

// Simulate sudo contract proposal
func SimulateSudoContractProposal(wasmKeeper WasmKeeper, contractSelector SudoContractProposalContractSelector) simtypes.ContentSimulatorFn {
return func(r *rand.Rand, ctx sdk.Context, accs []simtypes.Account) simtypes.Content {
ctAddress := contractSelector(ctx, wasmKeeper)
if ctAddress == nil {
return nil
}

return types.NewSudoContractProposal(
simtypes.RandStringOfLength(r, 10),
simtypes.RandStringOfLength(r, 10),
ctAddress.String(),
[]byte(`{}`),
)
}
}

// Simulate pin contract proposal
func SimulatePinContractProposal(wasmKeeper WasmKeeper, codeSelector CodeIDSelector) simtypes.ContentSimulatorFn {
return func(r *rand.Rand, ctx sdk.Context, accs []simtypes.Account) simtypes.Content {
codeID := codeSelector(ctx, wasmKeeper)
Copy link
Contributor

Choose a reason for hiding this comment

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

👍 I checked the doc and the execution is idempotent so it does not really matter if it was pinned before

if codeID == 0 {
return nil
}

return types.NewPinCodesProposal(
simtypes.RandStringOfLength(r, 10),
simtypes.RandStringOfLength(r, 10),
[]uint64{codeID},
)
}
}

// Simulate unpin contract proposal
func SimulateUnpinContractProposal(wasmKeeper WasmKeeper, codeSelector CodeIDSelector) simtypes.ContentSimulatorFn {
return func(r *rand.Rand, ctx sdk.Context, accs []simtypes.Account) simtypes.Content {
codeID := codeSelector(ctx, wasmKeeper)
if codeID == 0 {
return nil
}

return types.NewUnpinCodesProposal(
simtypes.RandStringOfLength(r, 10),
simtypes.RandStringOfLength(r, 10),
[]uint64{codeID},
)
}
}

// Simulate update instantiate config proposal
func SimulateUpdateInstantiateConfigProposal(wasmKeeper WasmKeeper, codeSelector CodeIDSelector) simtypes.ContentSimulatorFn {
return func(r *rand.Rand, ctx sdk.Context, accs []simtypes.Account) simtypes.Content {
codeID := codeSelector(ctx, wasmKeeper)
if codeID == 0 {
return nil
}

simAccount, _ := simtypes.RandomAcc(r, accs)
permission := wasmKeeper.GetParams(ctx).InstantiateDefaultPermission
config := permission.With(simAccount.Address)

configUpdate := types.AccessConfigUpdate{
CodeID: codeID,
InstantiatePermission: config,
}

return types.NewUpdateInstantiateConfigProposal(
simtypes.RandStringOfLength(r, 10),
simtypes.RandStringOfLength(r, 10),
configUpdate,
)
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

}
}

func SimulateStoreAndInstantiateContractProposal(wasmKeeper WasmKeeper) simtypes.ContentSimulatorFn {
return func(r *rand.Rand, ctx sdk.Context, accs []simtypes.Account) simtypes.Content {
simAccount, _ := simtypes.RandomAcc(r, accs)
adminAccount, _ := simtypes.RandomAcc(r, accs)

wasmBz := testdata.ReflectContractWasm()
permission := wasmKeeper.GetParams(ctx).InstantiateDefaultPermission.With(simAccount.Address)

return types.NewStoreAndInstantiateContractProposal(
simtypes.RandStringOfLength(r, 10),
simtypes.RandStringOfLength(r, 10),
simAccount.Address.String(),
wasmBz,
"",
"",
[]byte{},
&permission,
false,
adminAccount.Address.String(),
simtypes.RandStringOfLength(r, 10),
[]byte(`{}`),
sdk.Coins{},
)
}
}
66 changes: 66 additions & 0 deletions x/wasm/types/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,22 @@ func (p StoreAndInstantiateContractProposal) MarshalYAML() (interface{}, error)
}, nil
}

func NewMigrateContractProposal(
title string,
description string,
contract string,
codeID uint64,
msg RawContractMessage,
) *MigrateContractProposal {
return &MigrateContractProposal{
Title: title,
Description: description,
Contract: contract,
CodeID: codeID,
Msg: msg,
}
}

// ProposalRoute returns the routing key of a parameter change proposal.
func (p MigrateContractProposal) ProposalRoute() string { return RouterKey }

Expand Down Expand Up @@ -468,6 +484,20 @@ func (p MigrateContractProposal) MarshalYAML() (interface{}, error) {
}, nil
}

func NewSudoContractProposal(
title string,
description string,
contract string,
msg RawContractMessage,
) *SudoContractProposal {
return &SudoContractProposal{
Title: title,
Description: description,
Contract: contract,
Msg: msg,
}
}

// ProposalRoute returns the routing key of a parameter change proposal.
func (p SudoContractProposal) ProposalRoute() string { return RouterKey }

Expand Down Expand Up @@ -678,6 +708,18 @@ func (p ClearAdminProposal) String() string {
`, p.Title, p.Description, p.Contract)
}

func NewPinCodesProposal(
title string,
description string,
codeIDs []uint64,
) *PinCodesProposal {
return &PinCodesProposal{
Title: title,
Description: description,
CodeIDs: codeIDs,
}
}

// ProposalRoute returns the routing key of a parameter change proposal.
func (p PinCodesProposal) ProposalRoute() string { return RouterKey }

Expand Down Expand Up @@ -710,6 +752,18 @@ func (p PinCodesProposal) String() string {
`, p.Title, p.Description, p.CodeIDs)
}

func NewUnpinCodesProposal(
title string,
description string,
codeIDs []uint64,
) *UnpinCodesProposal {
return &UnpinCodesProposal{
Title: title,
Description: description,
CodeIDs: codeIDs,
}
}

// ProposalRoute returns the routing key of a parameter change proposal.
func (p UnpinCodesProposal) ProposalRoute() string { return RouterKey }

Expand Down Expand Up @@ -764,6 +818,18 @@ func validateProposalCommons(title, description string) error {
return nil
}

func NewUpdateInstantiateConfigProposal(
title string,
description string,
accessConfigUpdates ...AccessConfigUpdate,
) *UpdateInstantiateConfigProposal {
return &UpdateInstantiateConfigProposal{
Title: title,
Description: description,
AccessConfigUpdates: accessConfigUpdates,
}
}

// ProposalRoute returns the routing key of a parameter change proposal.
func (p UpdateInstantiateConfigProposal) ProposalRoute() string { return RouterKey }

Expand Down