Skip to content

Commit

Permalink
fix some minor
Browse files Browse the repository at this point in the history
  • Loading branch information
GNaD13 committed Nov 1, 2022
2 parents 25872b2 + fe50854 commit 68631da
Show file tree
Hide file tree
Showing 6 changed files with 302 additions and 18 deletions.
16 changes: 11 additions & 5 deletions app/params/weights.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,16 @@ const (
DefaultWeightTextProposal int = 5
DefaultWeightParamChangeProposal int = 5

DefaultWeightMsgStoreCode int = 100
DefaultWeightMsgInstantiateContract int = 50
DefaultWeightMsgStoreCode int = 50
DefaultWeightMsgInstantiateContract int = 100
DefaultWeightMsgExecuteContract int = 100
DefaultWeightMsgUpdateAdmin int = 100
DefaultWeightMsgClearAdmin int = 100
DefaultWeightMsgMigrateContract int = 100
DefaultWeightMsgUpdateAdmin int = 25
DefaultWeightMsgClearAdmin int = 10
DefaultWeightMsgMigrateContract int = 50

DefaultWeightStoreCodeProposal int = 5
DefaultWeightInstantiateContractProposal int = 5
DefaultWeightUpdateAdminProposal int = 5
DefaultWeightExecuteContractProposal int = 5
DefaultWeightClearAdminProposal int = 5
)
7 changes: 7 additions & 0 deletions x/wasm/keeper/testdata/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,17 @@ import (
//go:embed reflect.wasm
var reflectContract []byte

//go:embed reflect_1_1.wasm
var migrateReflectContract []byte

func ReflectContractWasm() []byte {
return reflectContract
}

func MigrateReflectContractWasm() []byte {
return migrateReflectContract
}

// ReflectHandleMsg is used to encode handle messages
type ReflectHandleMsg struct {
Reflect *ReflectPayload `json:"reflect_msg,omitempty"`
Expand Down
4 changes: 2 additions & 2 deletions x/wasm/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,8 @@ func (AppModule) GenerateGenesisState(simState *module.SimulationState) {
}

// ProposalContents doesn't return any content functions for governance proposals.
func (AppModule) ProposalContents(simState module.SimulationState) []simtypes.WeightedProposalContent {
return nil
func (am AppModule) ProposalContents(simState module.SimulationState) []simtypes.WeightedProposalContent {
return simulation.ProposalContents(am.bankKeeper, am.keeper)
}

// RandomizedParams creates randomized bank param changes for the simulator.
Expand Down
20 changes: 9 additions & 11 deletions x/wasm/simulation/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,7 @@ func SimulateMsgMigrateContract(
return simtypes.NoOpMsg(types.ModuleName, types.MsgMigrateContract{}.Type(), "no contract instance available"), nil, nil
}

var wasmBz []byte
var err error
wasmBz, err = os.ReadFile("./../x/wasm/keeper/testdata/reflect_1_1.wasm")
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, types.MsgMigrateContract{}.Type(), "no new wasmbyte code"), nil, err
}
wasmBz := testdata.MigrateReflectContractWasm()

permission := types.AccessTypeOnlyAddress
config := permission.With(simAccount.Address)
Expand All @@ -234,7 +229,7 @@ func SimulateMsgMigrateContract(
spendable := txCtx.Bankkeeper.SpendableCoins(txCtx.Context, account.GetAddress())

var fees sdk.Coins
fees, err = simtypes.RandomFees(txCtx.R, txCtx.Context, spendable)
fees, err := simtypes.RandomFees(txCtx.R, txCtx.Context, spendable)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, types.MsgMigrateContract{}.Type(), "unable to generate fees"), nil, err
}
Expand All @@ -250,7 +245,7 @@ func SimulateMsgMigrateContract(
txCtx.SimAccount.PrivKey,
)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, types.MsgMigrateContract{}.Type(), "unable to generate mock tx"), nil, err
return simtypes.NoOpMsg(types.ModuleName, types.MsgMigrateContract{}.Type(), "unable to generate tx"), nil, err
}

_, data, err := app.Deliver(txCtx.TxGen.TxEncoder(), tx)
Expand All @@ -264,8 +259,6 @@ func SimulateMsgMigrateContract(
return simtypes.NoOpMsg(types.ModuleName, types.MsgMigrateContract{}.Type(), "unable to read response data"), nil, err
}

fmt.Printf("%v\n", result.Data[0].MsgType)

var msgStoreCodeRespone types.MsgStoreCodeResponse
err = proto.Unmarshal(result.Data[0].Data, &msgStoreCodeRespone)
if err != nil {
Expand Down Expand Up @@ -364,9 +357,14 @@ func SimulateMsgUpdateAmin(
return simtypes.NoOpMsg(types.ModuleName, types.MsgUpdateAdmin{}.Type(), "no contract instance available"), nil, nil
}

newAdmin, _ := simtypes.RandomAcc(r, accs)
if newAdmin.Address.String() == simAccount.Address.String() {
return simtypes.NoOpMsg(types.ModuleName, types.MsgUpdateAdmin{}.Type(), "new admin cannot be the same as current admin"), nil, nil
}

msg := types.MsgUpdateAdmin{
Sender: simAccount.Address.String(),
NewAdmin: simtypes.RandomAccounts(r, 1)[0].Address.String(),
NewAdmin: newAdmin.Address.String(),
Contract: ctAddress.String(),
}
txCtx := BuildOperationInput(r, app, ctx, &msg, simAccount, ak, bk, nil)
Expand Down
221 changes: 221 additions & 0 deletions x/wasm/simulation/proposals.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
package simulation

import (
"math/rand"

"github.com/CosmWasm/wasmd/app/params"
"github.com/CosmWasm/wasmd/x/wasm/keeper/testdata"
"github.com/CosmWasm/wasmd/x/wasm/types"
sdk "github.com/cosmos/cosmos-sdk/types"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/cosmos/cosmos-sdk/x/simulation"
)

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"
)

func ProposalContents(bk BankKeeper, wasmKeeper WasmKeeper) []simtypes.WeightedProposalContent {
return []simtypes.WeightedProposalContent{
// simulation.NewWeightedProposalContent(
// WeightStoreCodeProposal,
// params.DefaultWeightStoreCodeProposal,
// SimulateStoreCodeProposal(wasmKeeper),
// ),
simulation.NewWeightedProposalContent(
WeightInstantiateContractProposal,
params.DefaultWeightInstantiateContractProposal,
SimulateInstantiateContractProposal(
bk,
wasmKeeper,
DefaultSimulationCodeIDSelector,
),
),
simulation.NewWeightedProposalContent(
WeightUpdateAdminProposal,
params.DefaultWeightUpdateAdminProposal,
SimulateUpdateAdminProposal(
wasmKeeper,
DefaultSimulateUpdateAdminProposalContractSelector,
),
),
simulation.NewWeightedProposalContent(
WeightExeContractProposal,
params.DefaultWeightExecuteContractProposal,
SimulateExecuteContractProposal(
bk,
wasmKeeper,
DefaultSimulationExecuteContractSelector,
DefaultSimulationExecuteSenderSelector,
DefaultSimulationExecutePayloader,
),
),
simulation.NewWeightedProposalContent(
WeightClearAdminProposal,
params.DefaultWeightClearAdminProposal,
SimulateClearAdminProposal(
wasmKeeper,
DefaultSimulateClearAdminProposalContractSelector,
),
),
}
}

// simulate store code proposal (unused now)
// Current problem: out of gas (defaul gaswanted config of gov SimulateMsgSubmitProposal is 10_000_000)
// but this proposal may need more than it
func SimulateStoreCodeProposal(wasmKeeper WasmKeeper) simtypes.ContentSimulatorFn {
return func(r *rand.Rand, ctx sdk.Context, accs []simtypes.Account) simtypes.Content {
simAccount, _ := simtypes.RandomAcc(r, accs)

wasmBz := testdata.ReflectContractWasm()

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

return types.NewStoreCodeProposal(
simtypes.RandStringOfLength(r, 10),
simtypes.RandStringOfLength(r, 10),
simAccount.Address.String(),
wasmBz,
&permission,
false,
)
}
}

// Simulate instantiate contract proposal
func SimulateInstantiateContractProposal(bk BankKeeper, wasmKeeper WasmKeeper, codeSelector CodeIDSelector) simtypes.ContentSimulatorFn {
return func(r *rand.Rand, ctx sdk.Context, accs []simtypes.Account) simtypes.Content {
simAccount, _ := simtypes.RandomAcc(r, accs)
// admin
adminAccount, _ := simtypes.RandomAcc(r, accs)
// get codeID
codeID := codeSelector(ctx, wasmKeeper)
if codeID == 0 {
return nil
}

return types.NewInstantiateContractProposal(
simtypes.RandStringOfLength(r, 10),
simtypes.RandStringOfLength(r, 10),
simAccount.Address.String(),
adminAccount.Address.String(),
codeID,
simtypes.RandStringOfLength(r, 10),
[]byte(`{}`),
sdk.Coins{},
)
}
}

// Simulate execute contract proposal
func SimulateExecuteContractProposal(
bk BankKeeper,
wasmKeeper WasmKeeper,
contractSelector MsgExecuteContractSelector,
senderSelector MsgExecuteSenderSelector,
payloader MsgExecutePayloader,
) simtypes.ContentSimulatorFn {
return func(r *rand.Rand, ctx sdk.Context, accs []simtypes.Account) simtypes.Content {
ctAddress := contractSelector(ctx, wasmKeeper)
if ctAddress == nil {
return nil
}

simAccount, err := senderSelector(wasmKeeper, ctx, ctAddress, accs)
if err != nil {
return nil
}

msg := types.MsgExecuteContract{
Sender: simAccount.Address.String(),
Contract: ctAddress.String(),
Funds: sdk.Coins{},
}

if err := payloader(&msg); err != nil {
return nil
}

return types.NewExecuteContractProposal(
simtypes.RandStringOfLength(r, 10),
simtypes.RandStringOfLength(r, 10),
simAccount.Address.String(),
ctAddress.String(),
msg.Msg,
sdk.Coins{},
)
}
}

type UpdateAdminContractSelector func(sdk.Context, WasmKeeper, string) (sdk.AccAddress, types.ContractInfo)

func DefaultSimulateUpdateAdminProposalContractSelector(
ctx sdk.Context,
wasmKeeper WasmKeeper,
adminAddress string,
) (sdk.AccAddress, types.ContractInfo) {
var contractAddr sdk.AccAddress
var contractInfo types.ContractInfo
wasmKeeper.IterateContractInfo(ctx, func(address sdk.AccAddress, info types.ContractInfo) bool {
if info.Admin != adminAddress {
return false
}
contractAddr = address
contractInfo = info
return true
})
return contractAddr, contractInfo
}

// Simulate update admin contract proposal
func SimulateUpdateAdminProposal(wasmKeeper WasmKeeper, contractSelector UpdateAdminContractSelector) simtypes.ContentSimulatorFn {
return func(r *rand.Rand, ctx sdk.Context, accs []simtypes.Account) simtypes.Content {
simAccount, _ := simtypes.RandomAcc(r, accs)
ctAddress, _ := contractSelector(ctx, wasmKeeper, simAccount.Address.String())
if ctAddress == nil {
return nil
}

return types.NewUpdateAdminProposal(
simtypes.RandStringOfLength(r, 10),
simtypes.RandStringOfLength(r, 10),
simtypes.RandomAccounts(r, 1)[0].Address.String(),
ctAddress.String(),
)
}
}

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

func DefaultSimulateClearAdminProposalContractSelector(
ctx sdk.Context,
wasmKeeper WasmKeeper,
) sdk.AccAddress {
var contractAddr sdk.AccAddress
wasmKeeper.IterateContractInfo(ctx, func(address sdk.AccAddress, info types.ContractInfo) bool {
contractAddr = address
return true
})
return contractAddr
}

// Simulate clear admin proposal
func SimulateClearAdminProposal(wasmKeeper WasmKeeper, contractSelector ClearAdminContractSelector) 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.NewClearAdminProposal(
simtypes.RandStringOfLength(r, 10),
simtypes.RandStringOfLength(r, 10),
ctAddress.String(),
)
}
}
Loading

0 comments on commit 68631da

Please sign in to comment.