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

fix: fix to prevent external filesystem dependency of simulations #695

Merged
merged 10 commits into from
Oct 18, 2022
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions x/wasm/keeper/testdata/reflect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package testdata

import (
_ "embed"
)

//go:embed reflect.wasm
var reflectContract []byte

func ReflectContractWasm() []byte {
return reflectContract
}
16 changes: 11 additions & 5 deletions x/wasm/simulation/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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 := os.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)
Copy link
Member

Choose a reason for hiding this comment

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

How about adding the unittest of this lines?

Copy link
Member Author

Choose a reason for hiding this comment

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

This is dead code, so I deleted it.

}
}

return simulation.WeightedOperations{
Expand Down
73 changes: 73 additions & 0 deletions x/wasm/simulation/operations_test.go
Original file line number Diff line number Diff line change
@@ -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
}