From 1b836ecc23593c89cdb9659c82a0d96220218254 Mon Sep 17 00:00:00 2001 From: Daisuke Iuchi <42408108+da1suk8@users.noreply.github.com> Date: Tue, 4 Oct 2022 23:09:38 +0900 Subject: [PATCH 1/6] fix: use go:embed --- x/wasm/keeper/testdata/reflect.go | 62 +++++++++++++++++++++++++++++++ x/wasm/simulation/operations.go | 18 ++++++--- 2 files changed, 74 insertions(+), 6 deletions(-) create mode 100644 x/wasm/keeper/testdata/reflect.go diff --git a/x/wasm/keeper/testdata/reflect.go b/x/wasm/keeper/testdata/reflect.go new file mode 100644 index 0000000000..0cafa631bb --- /dev/null +++ b/x/wasm/keeper/testdata/reflect.go @@ -0,0 +1,62 @@ +package testdata + +import ( + _ "embed" + + "github.com/line/lbm-sdk/types" + typwasmvmtypes "github.com/line/wasmvm/types" +) + +//go:embed reflect.wasm +var reflectContract []byte + +func ReflectContractWasm() []byte { + return reflectContract +} + +// ReflectHandleMsg is used to encode handle messages +type ReflectHandleMsg struct { + Reflect *ReflectPayload `json:"reflect_msg,omitempty"` + ReflectSubMsg *ReflectSubPayload `json:"reflect_sub_msg,omitempty"` + ChangeOwner *OwnerPayload `json:"change_owner,omitempty"` +} + +type OwnerPayload struct { + Owner types.Address `json:"owner"` +} + +type ReflectPayload struct { + Msgs []typwasmvmtypes.CosmosMsg `json:"msgs"` +} + +type ReflectSubPayload struct { + Msgs []typwasmvmtypes.SubMsg `json:"msgs"` +} + +// ReflectQueryMsg is used to encode query messages +type ReflectQueryMsg struct { + Owner *struct{} `json:"owner,omitempty"` + Capitalized *Text `json:"capitalized,omitempty"` + Chain *ChainQuery `json:"chain,omitempty"` + SubMsgResult *SubCall `json:"sub_msg_result,omitempty"` +} + +type ChainQuery struct { + Request *typwasmvmtypes.QueryRequest `json:"request,omitempty"` +} + +type Text struct { + Text string `json:"text"` +} + +type SubCall struct { + ID uint64 `json:"id"` +} + +type OwnerResponse struct { + Owner string `json:"owner,omitempty"` +} + +type ChainResponse struct { + Data []byte `json:"data,omitempty"` +} diff --git a/x/wasm/simulation/operations.go b/x/wasm/simulation/operations.go index 4b2fafbc2a..7466ebe6bd 100644 --- a/x/wasm/simulation/operations.go +++ b/x/wasm/simulation/operations.go @@ -1,8 +1,8 @@ package simulation import ( - "io/ioutil" "math/rand" + "os" "github.com/line/lbm-sdk/baseapp" simappparams "github.com/line/lbm-sdk/simapp/params" @@ -10,6 +10,7 @@ import ( "github.com/line/lbm-sdk/types/module" simtypes "github.com/line/lbm-sdk/types/simulation" "github.com/line/lbm-sdk/x/simulation" + "github.com/line/lbm-sdk/x/wasm/keeper/testdata" "github.com/line/lbm-sdk/x/wasm/types" ) @@ -53,14 +54,19 @@ func WeightedOperations( ) simstate.AppParams.GetOrGenerate(simstate.Cdc, OpReflectContractPath, &wasmContractPath, nil, func(_ *rand.Rand) { - // simulations are run from the `app` folder - wasmContractPath = "../x/wasm/keeper/testdata/reflect.wasm" + wasmContractPath = "" }, ) - wasmBz, err := ioutil.ReadFile(wasmContractPath) - if err != nil { - panic(err) + var wasmBz []byte + if wasmContractPath == "" { + wasmBz = testdata.ReflectContractWasm() + } else { + var err error + wasmBz, err = os.ReadFile(wasmContractPath) + if err != nil { + panic(err) + } } return simulation.WeightedOperations{ From a2a0a92192333bf2134b687311b035cd9fd47cec Mon Sep 17 00:00:00 2001 From: Daisuke Iuchi <42408108+da1suk8@users.noreply.github.com> Date: Wed, 12 Oct 2022 17:06:43 +0900 Subject: [PATCH 2/6] fix: remove unnecessary part --- x/wasm/keeper/testdata/reflect.go | 50 ------------------------------- 1 file changed, 50 deletions(-) diff --git a/x/wasm/keeper/testdata/reflect.go b/x/wasm/keeper/testdata/reflect.go index 0cafa631bb..ad365ce0c2 100644 --- a/x/wasm/keeper/testdata/reflect.go +++ b/x/wasm/keeper/testdata/reflect.go @@ -2,9 +2,6 @@ package testdata import ( _ "embed" - - "github.com/line/lbm-sdk/types" - typwasmvmtypes "github.com/line/wasmvm/types" ) //go:embed reflect.wasm @@ -13,50 +10,3 @@ var reflectContract []byte func ReflectContractWasm() []byte { return reflectContract } - -// ReflectHandleMsg is used to encode handle messages -type ReflectHandleMsg struct { - Reflect *ReflectPayload `json:"reflect_msg,omitempty"` - ReflectSubMsg *ReflectSubPayload `json:"reflect_sub_msg,omitempty"` - ChangeOwner *OwnerPayload `json:"change_owner,omitempty"` -} - -type OwnerPayload struct { - Owner types.Address `json:"owner"` -} - -type ReflectPayload struct { - Msgs []typwasmvmtypes.CosmosMsg `json:"msgs"` -} - -type ReflectSubPayload struct { - Msgs []typwasmvmtypes.SubMsg `json:"msgs"` -} - -// ReflectQueryMsg is used to encode query messages -type ReflectQueryMsg struct { - Owner *struct{} `json:"owner,omitempty"` - Capitalized *Text `json:"capitalized,omitempty"` - Chain *ChainQuery `json:"chain,omitempty"` - SubMsgResult *SubCall `json:"sub_msg_result,omitempty"` -} - -type ChainQuery struct { - Request *typwasmvmtypes.QueryRequest `json:"request,omitempty"` -} - -type Text struct { - Text string `json:"text"` -} - -type SubCall struct { - ID uint64 `json:"id"` -} - -type OwnerResponse struct { - Owner string `json:"owner,omitempty"` -} - -type ChainResponse struct { - Data []byte `json:"data,omitempty"` -} From 630094dadf2f2d55a62c21a298ffddca37b0abec Mon Sep 17 00:00:00 2001 From: Daisuke Iuchi <42408108+da1suk8@users.noreply.github.com> Date: Wed, 12 Oct 2022 17:10:52 +0900 Subject: [PATCH 3/6] docs: update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index be30f71e56..19c013eca5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -77,6 +77,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (x/foundation) [\#698](https://github.com/line/lbm-sdk/pull/698) update x/group relevant logic in x/foundation * (x/auth,bank,foundation,wasm) [\#691](https://github.com/line/lbm-sdk/pull/691) change AccAddressFromBech32 to MustAccAddressFromBech32 * (x/wasm) [\#690](https://github.com/line/lbm-sdk/pull/690) fix to prevent accepting file name +* (x/wasm) [\#695](https://github.com/line/lbm-sdk/pull/695) fix to prevent external filesystem dependency of simulation ### Bug Fixes * (x/wasm) [\#453](https://github.com/line/lbm-sdk/pull/453) modify wasm grpc query api path From d57b37da7ac27ce755666475fd757b20a8bd9c8d Mon Sep 17 00:00:00 2001 From: Daisuke Iuchi <42408108+da1suk8@users.noreply.github.com> Date: Thu, 13 Oct 2022 17:59:01 +0900 Subject: [PATCH 4/6] test: add test of WeightedOperations --- x/wasm/simulation/operations_test.go | 73 ++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 x/wasm/simulation/operations_test.go diff --git a/x/wasm/simulation/operations_test.go b/x/wasm/simulation/operations_test.go new file mode 100644 index 0000000000..7e0f22e61c --- /dev/null +++ b/x/wasm/simulation/operations_test.go @@ -0,0 +1,73 @@ +package simulation + +import ( + "reflect" + "testing" + + simappparams "github.com/line/lbm-sdk/simapp/params" + "github.com/stretchr/testify/require" + + "github.com/line/lbm-sdk/types/module" + "github.com/line/lbm-sdk/x/simulation" + "github.com/line/lbm-sdk/x/wasm/keeper" + "github.com/line/lbm-sdk/x/wasm/types" +) + +func TestWeightedOperations(t *testing.T) { + type args struct { + simstate *module.SimulationState + ak types.AccountKeeper + bk simulation.BankKeeper + wasmKeeper WasmKeeper + wasmBz []byte + } + + params := args{ + simstate: &module.SimulationState{}, + wasmKeeper: makeKeeper(t).WasmKeeper, + } + + tests := []struct { + name string + args args + want simulation.WeightedOperations + }{ + { + name: "execute success", + args: args{ + simstate: &module.SimulationState{}, + }, + want: simulation.WeightedOperations{ + simulation.NewWeightedOperation( + simappparams.DefaultWeightMsgStoreCode, + SimulateMsgStoreCode(params.ak, params.bk, params.wasmKeeper, params.wasmBz)), + simulation.NewWeightedOperation( + simappparams.DefaultWeightMsgInstantiateContract, + SimulateMsgInstantiateContract(params.ak, params.bk, params.wasmKeeper)), + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := WeightedOperations(tt.args.simstate, tt.args.ak, tt.args.bk, tt.args.wasmKeeper) + for i := range got { + require.Equal(t, tt.want[i].Weight(), got[i].Weight(), "WeightedOperations().Weight()") + + expected := reflect.TypeOf(tt.want[i].Op()).String() + actual := reflect.TypeOf(got[i].Op()).String() + + require.Equal(t, expected, actual, "return value type should be the same") + } + }) + } +} + +// Copy from keeper_test.go +const SupportedFeatures = "iterator,staking,stargate" + +// Copy from keeper_test.go +func makeKeeper(t *testing.T) keeper.TestKeepers { + _, keepers := keeper.CreateTestInput(t, false, SupportedFeatures, nil, nil) + return keepers +} From 9cd704eea36615798faecf2d5a22ab9c95df7388 Mon Sep 17 00:00:00 2001 From: Daisuke Iuchi <42408108+da1suk8@users.noreply.github.com> Date: Thu, 13 Oct 2022 18:01:47 +0900 Subject: [PATCH 5/6] fix: fix format --- x/wasm/simulation/operations_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/wasm/simulation/operations_test.go b/x/wasm/simulation/operations_test.go index 7e0f22e61c..d5e476ed05 100644 --- a/x/wasm/simulation/operations_test.go +++ b/x/wasm/simulation/operations_test.go @@ -56,7 +56,7 @@ func TestWeightedOperations(t *testing.T) { expected := reflect.TypeOf(tt.want[i].Op()).String() actual := reflect.TypeOf(got[i].Op()).String() - + require.Equal(t, expected, actual, "return value type should be the same") } }) From 50c9f8f33f9f777313fbf66d6faee982fb59d85f Mon Sep 17 00:00:00 2001 From: Daisuke Iuchi <42408108+da1suk8@users.noreply.github.com> Date: Fri, 14 Oct 2022 18:38:59 +0900 Subject: [PATCH 6/6] fix: delete wasmContractPath --- x/wasm/simulation/operations.go | 18 +----------------- 1 file changed, 1 insertion(+), 17 deletions(-) diff --git a/x/wasm/simulation/operations.go b/x/wasm/simulation/operations.go index 7466ebe6bd..cf04af35d3 100644 --- a/x/wasm/simulation/operations.go +++ b/x/wasm/simulation/operations.go @@ -2,7 +2,6 @@ package simulation import ( "math/rand" - "os" "github.com/line/lbm-sdk/baseapp" simappparams "github.com/line/lbm-sdk/simapp/params" @@ -38,7 +37,6 @@ func WeightedOperations( var ( weightMsgStoreCode int weightMsgInstantiateContract int - wasmContractPath string ) simstate.AppParams.GetOrGenerate(simstate.Cdc, OpWeightMsgStoreCode, &weightMsgStoreCode, nil, @@ -52,22 +50,8 @@ func WeightedOperations( weightMsgInstantiateContract = simappparams.DefaultWeightMsgInstantiateContract }, ) - simstate.AppParams.GetOrGenerate(simstate.Cdc, OpReflectContractPath, &wasmContractPath, nil, - func(_ *rand.Rand) { - wasmContractPath = "" - }, - ) - var wasmBz []byte - if wasmContractPath == "" { - wasmBz = testdata.ReflectContractWasm() - } else { - var err error - wasmBz, err = os.ReadFile(wasmContractPath) - if err != nil { - panic(err) - } - } + wasmBz := testdata.ReflectContractWasm() return simulation.WeightedOperations{ simulation.NewWeightedOperation(