From f856957435fee025082a65df37e4e96bbb72dc1f Mon Sep 17 00:00:00 2001 From: Sujong Lee Date: Tue, 18 Oct 2022 12:41:51 +0900 Subject: [PATCH 1/2] fix: check pubkey type from validator params (#724) * fix: check pubkey type from validator params * docs: add changelog * test: remove comments * Update CHANGELOG.md Co-authored-by: Youngtaek Yoon Co-authored-by: Youngtaek Yoon --- CHANGELOG.md | 1 + baseapp/params.go | 10 ++++++++++ baseapp/params_test.go | 1 + 3 files changed, 12 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9fb171aea7..8a6262a3a3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -103,6 +103,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (global) [\#694](https://github.com/line/lbm-sdk/pull/694) replace deprecated functions since go 1.16 or 1.17 * (x/bankplus) [\#705](https://github.com/line/lbm-sdk/pull/705) add missing blockedAddr checking in bankplus * (x/foundation) [\#712](https://github.com/line/lbm-sdk/pull/712) fix x/foundation EndBlocker +* (baseapp) [\#724](https://github.com/line/lbm-sdk/pull/724) add checking pubkey type from validator params * (x/staking) [\#726](https://github.com/line/lbm-sdk/pull/726) check allowedList size in StakeAuthorization.Accept() ### Breaking Changes diff --git a/baseapp/params.go b/baseapp/params.go index 502955412d..061c6a17ed 100644 --- a/baseapp/params.go +++ b/baseapp/params.go @@ -6,6 +6,7 @@ import ( abci "github.com/line/ostracon/abci/types" ocproto "github.com/line/ostracon/proto/ostracon/types" + octypes "github.com/line/ostracon/types" sdk "github.com/line/lbm-sdk/types" ) @@ -82,5 +83,14 @@ func ValidateValidatorParams(i interface{}) error { return errors.New("validator allowed pubkey types must not be empty") } + for _, pubKeyType := range v.PubKeyTypes { + switch pubKeyType { + case octypes.ABCIPubKeyTypeBls12WithEd25519, octypes.ABCIPubKeyTypeEd25519, octypes.ABCIPubKeyTypeSecp256k1, octypes.ABCIPubKeyTypeBls12: + continue + default: + return fmt.Errorf("not-allowed pubkey type: %s", pubKeyType) + } + } + return nil } diff --git a/baseapp/params_test.go b/baseapp/params_test.go index a421d13b65..340be7097a 100644 --- a/baseapp/params_test.go +++ b/baseapp/params_test.go @@ -58,6 +58,7 @@ func TestValidateValidatorParams(t *testing.T) { {ocproto.ValidatorParams{}, true}, {ocproto.ValidatorParams{PubKeyTypes: []string{}}, true}, {ocproto.ValidatorParams{PubKeyTypes: []string{"secp256k1"}}, false}, + {ocproto.ValidatorParams{PubKeyTypes: []string{"invalidPubKeyType"}}, true}, } for _, tc := range testCases { From d4635de5d33d9824154eaf1d90dfb12722ebf8de Mon Sep 17 00:00:00 2001 From: Daisuke Iuchi <42408108+da1suk8@users.noreply.github.com> Date: Tue, 18 Oct 2022 15:39:15 +0900 Subject: [PATCH 2/2] 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 --- CHANGELOG.md | 1 + x/wasm/keeper/testdata/reflect.go | 12 +++++ x/wasm/simulation/operations.go | 14 +----- x/wasm/simulation/operations_test.go | 73 ++++++++++++++++++++++++++++ 4 files changed, 88 insertions(+), 12 deletions(-) create mode 100644 x/wasm/keeper/testdata/reflect.go create mode 100644 x/wasm/simulation/operations_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 8a6262a3a3..a6521f6edf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -83,6 +83,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (x/modules) [\#722](https://github.com/line/lbm-sdk/pull/722) Check error for `RegisterQueryHandlerClient` in all modules `RegisterGRPCGatewayRoutes` * (x/bank) [\#716](https://github.com/line/lbm-sdk/pull/716) remove useless DenomMetadata key function * (x/foundation) [\#704](https://github.com/line/lbm-sdk/pull/704) update x/foundation params +* (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 diff --git a/x/wasm/keeper/testdata/reflect.go b/x/wasm/keeper/testdata/reflect.go new file mode 100644 index 0000000000..ad365ce0c2 --- /dev/null +++ b/x/wasm/keeper/testdata/reflect.go @@ -0,0 +1,12 @@ +package testdata + +import ( + _ "embed" +) + +//go:embed reflect.wasm +var reflectContract []byte + +func ReflectContractWasm() []byte { + return reflectContract +} diff --git a/x/wasm/simulation/operations.go b/x/wasm/simulation/operations.go index 6ecc03df3e..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" @@ -10,6 +9,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" ) @@ -37,7 +37,6 @@ func WeightedOperations( var ( weightMsgStoreCode int weightMsgInstantiateContract int - wasmContractPath string ) simstate.AppParams.GetOrGenerate(simstate.Cdc, OpWeightMsgStoreCode, &weightMsgStoreCode, nil, @@ -51,17 +50,8 @@ func WeightedOperations( weightMsgInstantiateContract = simappparams.DefaultWeightMsgInstantiateContract }, ) - 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" - }, - ) - wasmBz, err := os.ReadFile(wasmContractPath) - if err != nil { - panic(err) - } + wasmBz := testdata.ReflectContractWasm() return simulation.WeightedOperations{ simulation.NewWeightedOperation( diff --git a/x/wasm/simulation/operations_test.go b/x/wasm/simulation/operations_test.go new file mode 100644 index 0000000000..d5e476ed05 --- /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 +}