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 weighted operations run simulation #1055

Merged
merged 16 commits into from
Nov 2, 2022
Prev Previous commit
Next Next commit
add migrate
  • Loading branch information
GNaD13 committed Oct 24, 2022
commit 1ee28be083a8f48d64a2e3d764c205b08ca4d05d
1 change: 1 addition & 0 deletions app/params/weights.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ const (
DefaultWeightMsgExecuteContract int = 100
DefaultWeightMsgUpdateAdmin int = 100
Copy link
Contributor

Choose a reason for hiding this comment

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

I would expect this to happen quire rarely. How about 25?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have changed

DefaultWeightMsgClearAdmin int = 100
Copy link
Contributor

Choose a reason for hiding this comment

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

I would expect this to happen quire rarely. How about 10?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have changed

DefaultWeightMsgMigrateContract int = 100
Copy link
Contributor

Choose a reason for hiding this comment

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

I would expect more instantiations than migrations. How about 50?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have changed

)
142 changes: 142 additions & 0 deletions x/wasm/simulation/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@ package simulation

import (
"encoding/json"
"fmt"
"math/rand"
"os"

wasmvmtypes "github.com/CosmWasm/wasmvm/types"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/simapp/helpers"
simappparams "github.com/cosmos/cosmos-sdk/simapp/params"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/module"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/cosmos/cosmos-sdk/x/simulation"
"github.com/gogo/protobuf/proto"

"github.com/CosmWasm/wasmd/app/params"
wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
Expand All @@ -29,6 +32,7 @@ const (
OpWeightMsgExecuteContract = "op_weight_msg_execute_contract"
OpWeightMsgUpdateAdmin = "op_weight_msg_update_admin"
OpWeightMsgClearAdmin = "op_weight_msg_clear_admin"
OpWeightMsgMigrateContract = "op_weight_msg_migrate_contract"
OpReflectContractPath = "op_reflect_contract_path"
)

Expand Down Expand Up @@ -58,6 +62,7 @@ func WeightedOperations(
weightMsgExecuteContract int
weightMsgUpdateAdmin int
weightMsgClearAdmin int
weightMsgMigrateContract int
wasmContractPath string
)

Expand Down Expand Up @@ -86,6 +91,11 @@ func WeightedOperations(
weightMsgClearAdmin = params.DefaultWeightMsgClearAdmin
},
)
simstate.AppParams.GetOrGenerate(simstate.Cdc, OpWeightMsgMigrateContract, &weightMsgMigrateContract, nil,
func(_ *rand.Rand) {
weightMsgMigrateContract = params.DefaultWeightMsgMigrateContract
},
)
simstate.AppParams.GetOrGenerate(simstate.Cdc, OpReflectContractPath, &wasmContractPath, nil,
func(_ *rand.Rand) {
wasmContractPath = ""
Expand Down Expand Up @@ -141,6 +151,138 @@ func WeightedOperations(
DefaultSimulationClearAdminContractSelector,
),
),
simulation.NewWeightedOperation(
weightMsgMigrateContract,
SimulateMsgMigrateContract(
ak,
bk,
wasmKeeper,
DefaultSimulationMigrateContractSelector,
DefaultSimulationMigrateCodeIDSelector,
),
),
}
}

type MsgMigrateContractSelector func(sdk.Context, WasmKeeper, string) (sdk.AccAddress, types.ContractInfo)
type MsgMigrateCodeIDSelector func(sdk.Context, WasmKeeper, uint64) uint64

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

func DefaultSimulationMigrateCodeIDSelector(ctx sdk.Context, wasmKeeper WasmKeeper, currentCodeID uint64) uint64 {
var codeID uint64
wasmKeeper.IterateCodeInfos(ctx, func(u uint64, info types.CodeInfo) bool {
if (info.InstantiateConfig.Permission != types.AccessTypeEverybody) || (u == currentCodeID) {
return false
}
codeID = u
return true
})
return codeID
}

func SimulateMsgMigrateContract(
ak types.AccountKeeper,
bk BankKeeper,
wasmKeeper WasmKeeper,
contractSelector MsgMigrateContractSelector,
codeIDSelector MsgMigrateCodeIDSelector,
) simtypes.Operation {
return func(
r *rand.Rand,
app *baseapp.BaseApp,
ctx sdk.Context,
accs []simtypes.Account,
chainID string,
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
simAccount, _ := simtypes.RandomAcc(r, accs)
ctAddress, _ := contractSelector(ctx, wasmKeeper, simAccount.Address.String())
if ctAddress == nil {
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/ibc_reflect.wasm")
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, types.MsgMigrateContract{}.Type(), "no new wasmbyte code"), nil, err
}

permission := types.AccessTypeOnlyAddress
config := permission.With(simAccount.Address)

msg := types.MsgStoreCode{
Sender: simAccount.Address.String(),
WASMByteCode: wasmBz,
InstantiatePermission: &config,
}

txCtx := BuildOperationInput(r, app, ctx, &msg, simAccount, ak, bk, nil)
account := txCtx.AccountKeeper.GetAccount(txCtx.Context, txCtx.SimAccount.Address)
spendable := txCtx.Bankkeeper.SpendableCoins(txCtx.Context, account.GetAddress())

var fees sdk.Coins
fees, err = simtypes.RandomFees(txCtx.R, txCtx.Context, spendable)
if err != nil {
return simtypes.NoOpMsg(txCtx.ModuleName, txCtx.MsgType, "unable to generate fees"), nil, err
}

tx, err := helpers.GenTx(
txCtx.TxGen,
[]sdk.Msg{txCtx.Msg},
fees,
helpers.DefaultGenTxGas,
txCtx.Context.ChainID(),
[]uint64{account.GetAccountNumber()},
[]uint64{account.GetSequence()},
txCtx.SimAccount.PrivKey,
)
if err != nil {
return simtypes.NoOpMsg(txCtx.ModuleName, txCtx.MsgType, "unable to generate mock tx"), nil, err
}

_, data, err := app.Deliver(txCtx.TxGen.TxEncoder(), tx)
if err != nil {
return simtypes.NoOpMsg(txCtx.ModuleName, txCtx.MsgType, "unable to deliver tx"), nil, err
}

var result sdk.TxMsgData
err = proto.Unmarshal(data.Data, &result)
if err != nil {
return simtypes.NoOpMsg(txCtx.ModuleName, txCtx.MsgType, "unable to read response data"), nil, err
}

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

Choose a reason for hiding this comment

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

Let's avoid Printfs

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have removed


var msgStoreCodeRespone types.MsgStoreCodeResponse
err = proto.Unmarshal(result.Data[0].Data, &msgStoreCodeRespone)
if err != nil {
return simtypes.NoOpMsg(txCtx.ModuleName, txCtx.MsgType, "unable to unmarshall response data"), nil, err
}

fmt.Printf("%v\n", msgStoreCodeRespone.CodeID)

migrateMsg := types.MsgMigrateContract{
Sender: simAccount.Address.String(),
Contract: ctAddress.String(),
CodeID: msgStoreCodeRespone.CodeID,
Msg: []byte(`{}`),
}

txCtx = BuildOperationInput(r, app, ctx, &migrateMsg, simAccount, ak, bk, nil)
return simulation.GenAndDeliverTxWithRandFees(txCtx)
}
}

Expand Down