Skip to content

Commit

Permalink
Polish SimulateMsgMigrateContract
Browse files Browse the repository at this point in the history
  • Loading branch information
alpe committed Nov 1, 2022
1 parent 68631da commit 5697af6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 65 deletions.
73 changes: 11 additions & 62 deletions x/wasm/simulation/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,17 @@ 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 Down Expand Up @@ -104,7 +101,7 @@ func WeightedOperations(

var wasmBz []byte
if wasmContractPath == "" {
wasmBz = testdata.ReflectContractWasm()
wasmBz = testdata.MigrateReflectContractWasm()
} else {
var err error
wasmBz, err = os.ReadFile(wasmContractPath)
Expand Down Expand Up @@ -164,8 +161,10 @@ func WeightedOperations(
}
}

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

func DefaultSimulationMigrateContractSelector(ctx sdk.Context, wasmKeeper WasmKeeper, adminAddress string) (sdk.AccAddress, types.ContractInfo) {
var contractAddress sdk.AccAddress
Expand Down Expand Up @@ -208,73 +207,23 @@ func SimulateMsgMigrateContract(
chainID string,
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
simAccount, _ := simtypes.RandomAcc(r, accs)
ctAddress, _ := contractSelector(ctx, wasmKeeper, simAccount.Address.String())
ctAddress, info := contractSelector(ctx, wasmKeeper, simAccount.Address.String())
if ctAddress == nil {
return simtypes.NoOpMsg(types.ModuleName, types.MsgMigrateContract{}.Type(), "no contract instance available"), nil, nil
}

wasmBz := testdata.MigrateReflectContractWasm()

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(types.ModuleName, types.MsgMigrateContract{}.Type(), "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(types.ModuleName, types.MsgMigrateContract{}.Type(), "unable to generate tx"), nil, err
}

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

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

var msgStoreCodeRespone types.MsgStoreCodeResponse
err = proto.Unmarshal(result.Data[0].Data, &msgStoreCodeRespone)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, types.MsgMigrateContract{}.Type(), "unable to unmarshall response data"), nil, err
codeID := codeIDSelector(ctx, wasmKeeper, info.CodeID)
if codeID == 0 {
return simtypes.NoOpMsg(types.ModuleName, types.MsgMigrateContract{}.Type(), "no target contract available"), nil, nil
}

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

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

txCtx = BuildOperationInput(r, app, ctx, &migrateMsg, simAccount, ak, bk, nil)
txCtx := BuildOperationInput(r, app, ctx, &migrateMsg, simAccount, ak, bk, nil)
return simulation.GenAndDeliverTxWithRandFees(txCtx)
}
}
Expand Down
7 changes: 4 additions & 3 deletions x/wasm/simulation/proposals.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ 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"

"github.com/CosmWasm/wasmd/app/params"
"github.com/CosmWasm/wasmd/x/wasm/keeper/testdata"
"github.com/CosmWasm/wasmd/x/wasm/types"
)

const (
Expand Down

0 comments on commit 5697af6

Please sign in to comment.