Skip to content

Commit d4635de

Browse files
authored
fix: fix to prevent external filesystem dependency of simulations (#695)
* fix: use go:embed * fix: remove unnecessary part * docs: update CHANGELOG.md * test: add test of WeightedOperations * fix: fix format * fix: delete wasmContractPath
1 parent f856957 commit d4635de

File tree

4 files changed

+88
-12
lines changed

4 files changed

+88
-12
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
8383
* (x/modules) [\#722](https://github.com/line/lbm-sdk/pull/722) Check error for `RegisterQueryHandlerClient` in all modules `RegisterGRPCGatewayRoutes`
8484
* (x/bank) [\#716](https://github.com/line/lbm-sdk/pull/716) remove useless DenomMetadata key function
8585
* (x/foundation) [\#704](https://github.com/line/lbm-sdk/pull/704) update x/foundation params
86+
* (x/wasm) [\#695](https://github.com/line/lbm-sdk/pull/695) fix to prevent external filesystem dependency of simulation
8687

8788
### Bug Fixes
8889
* (x/wasm) [\#453](https://github.com/line/lbm-sdk/pull/453) modify wasm grpc query api path

x/wasm/keeper/testdata/reflect.go

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package testdata
2+
3+
import (
4+
_ "embed"
5+
)
6+
7+
//go:embed reflect.wasm
8+
var reflectContract []byte
9+
10+
func ReflectContractWasm() []byte {
11+
return reflectContract
12+
}

x/wasm/simulation/operations.go

+2-12
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ package simulation
22

33
import (
44
"math/rand"
5-
"os"
65

76
"github.com/line/lbm-sdk/baseapp"
87
simappparams "github.com/line/lbm-sdk/simapp/params"
98
sdk "github.com/line/lbm-sdk/types"
109
"github.com/line/lbm-sdk/types/module"
1110
simtypes "github.com/line/lbm-sdk/types/simulation"
1211
"github.com/line/lbm-sdk/x/simulation"
12+
"github.com/line/lbm-sdk/x/wasm/keeper/testdata"
1313
"github.com/line/lbm-sdk/x/wasm/types"
1414
)
1515

@@ -37,7 +37,6 @@ func WeightedOperations(
3737
var (
3838
weightMsgStoreCode int
3939
weightMsgInstantiateContract int
40-
wasmContractPath string
4140
)
4241

4342
simstate.AppParams.GetOrGenerate(simstate.Cdc, OpWeightMsgStoreCode, &weightMsgStoreCode, nil,
@@ -51,17 +50,8 @@ func WeightedOperations(
5150
weightMsgInstantiateContract = simappparams.DefaultWeightMsgInstantiateContract
5251
},
5352
)
54-
simstate.AppParams.GetOrGenerate(simstate.Cdc, OpReflectContractPath, &wasmContractPath, nil,
55-
func(_ *rand.Rand) {
56-
// simulations are run from the `app` folder
57-
wasmContractPath = "../x/wasm/keeper/testdata/reflect.wasm"
58-
},
59-
)
6053

61-
wasmBz, err := os.ReadFile(wasmContractPath)
62-
if err != nil {
63-
panic(err)
64-
}
54+
wasmBz := testdata.ReflectContractWasm()
6555

6656
return simulation.WeightedOperations{
6757
simulation.NewWeightedOperation(

x/wasm/simulation/operations_test.go

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package simulation
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
7+
simappparams "github.com/line/lbm-sdk/simapp/params"
8+
"github.com/stretchr/testify/require"
9+
10+
"github.com/line/lbm-sdk/types/module"
11+
"github.com/line/lbm-sdk/x/simulation"
12+
"github.com/line/lbm-sdk/x/wasm/keeper"
13+
"github.com/line/lbm-sdk/x/wasm/types"
14+
)
15+
16+
func TestWeightedOperations(t *testing.T) {
17+
type args struct {
18+
simstate *module.SimulationState
19+
ak types.AccountKeeper
20+
bk simulation.BankKeeper
21+
wasmKeeper WasmKeeper
22+
wasmBz []byte
23+
}
24+
25+
params := args{
26+
simstate: &module.SimulationState{},
27+
wasmKeeper: makeKeeper(t).WasmKeeper,
28+
}
29+
30+
tests := []struct {
31+
name string
32+
args args
33+
want simulation.WeightedOperations
34+
}{
35+
{
36+
name: "execute success",
37+
args: args{
38+
simstate: &module.SimulationState{},
39+
},
40+
want: simulation.WeightedOperations{
41+
simulation.NewWeightedOperation(
42+
simappparams.DefaultWeightMsgStoreCode,
43+
SimulateMsgStoreCode(params.ak, params.bk, params.wasmKeeper, params.wasmBz)),
44+
simulation.NewWeightedOperation(
45+
simappparams.DefaultWeightMsgInstantiateContract,
46+
SimulateMsgInstantiateContract(params.ak, params.bk, params.wasmKeeper)),
47+
},
48+
},
49+
}
50+
51+
for _, tt := range tests {
52+
t.Run(tt.name, func(t *testing.T) {
53+
got := WeightedOperations(tt.args.simstate, tt.args.ak, tt.args.bk, tt.args.wasmKeeper)
54+
for i := range got {
55+
require.Equal(t, tt.want[i].Weight(), got[i].Weight(), "WeightedOperations().Weight()")
56+
57+
expected := reflect.TypeOf(tt.want[i].Op()).String()
58+
actual := reflect.TypeOf(got[i].Op()).String()
59+
60+
require.Equal(t, expected, actual, "return value type should be the same")
61+
}
62+
})
63+
}
64+
}
65+
66+
// Copy from keeper_test.go
67+
const SupportedFeatures = "iterator,staking,stargate"
68+
69+
// Copy from keeper_test.go
70+
func makeKeeper(t *testing.T) keeper.TestKeepers {
71+
_, keepers := keeper.CreateTestInput(t, false, SupportedFeatures, nil, nil)
72+
return keepers
73+
}

0 commit comments

Comments
 (0)