From ab28f147363942574fe0ef39ef3a48f5cb23268b Mon Sep 17 00:00:00 2001 From: Barry Date: Wed, 12 Apr 2023 14:33:48 +0800 Subject: [PATCH] add ut and query cli for wasm extra propsal (#3093) * add wasm proposal ut * add wasm proposal ut * add query api * update max value * fix ut * fix cli display * optimized code * optimized code * optimized code * optimized code --- x/wasm/client/cli/gov_custom.go | 2 +- x/wasm/client/cli/query.go | 24 ++ x/wasm/client/rest/query.go | 21 ++ x/wasm/keeper/genesis_test.go | 14 +- x/wasm/keeper/legacy_querier.go | 10 + x/wasm/keeper/proposal_integration_test.go | 94 ++++++- x/wasm/keeper/querier_test.go | 4 +- x/wasm/proto/proposal_custom.proto | 4 + x/wasm/types/errors.go | 4 +- x/wasm/types/exported_keepers.go | 1 + x/wasm/types/proposal_custom.go | 15 +- x/wasm/types/proposal_custom.pb.go | 87 ++++-- x/wasm/types/proposal_test.go | 308 +++++++++++++++++++++ 13 files changed, 547 insertions(+), 41 deletions(-) diff --git a/x/wasm/client/cli/gov_custom.go b/x/wasm/client/cli/gov_custom.go index eea13dfce5..e7068ad199 100644 --- a/x/wasm/client/cli/gov_custom.go +++ b/x/wasm/client/cli/gov_custom.go @@ -144,7 +144,7 @@ Where proposal.json contains like these: "title":"modify wasm gas factor", "description":"modify wasm gas factor", "action": "GasFactor", - "extra": "{\"factor\":240000000}", + "extra": "{\"factor\":\"14\"}", "deposit":[ { "denom":"%s", diff --git a/x/wasm/client/cli/query.go b/x/wasm/client/cli/query.go index 3e6b08fb91..e2e518be6e 100644 --- a/x/wasm/client/cli/query.go +++ b/x/wasm/client/cli/query.go @@ -50,6 +50,7 @@ func NewQueryCmd(cdc *codec.CodecProxy, reg codectypes.InterfaceRegistry) *cobra NewCmdLibVersion(cdc, reg), NewCmdListContractBlockedMethod(cdc), NewCmdGetParams(cdc, reg), + NewCmdGetExtraParams(cdc, reg), NewCmdGetAddressWhitelist(cdc, reg), ) @@ -133,6 +134,29 @@ func NewCmdGetParams(m *codec.CodecProxy, reg codectypes.InterfaceRegistry) *cob return cmd } +func NewCmdGetExtraParams(m *codec.CodecProxy, reg codectypes.InterfaceRegistry) *cobra.Command { + cmd := &cobra.Command{ + Use: "extra-params", + Short: "Get wasm extra parameters on the chain", + Long: "Get wasm extra parameters on the chain", + Args: cobra.ExactArgs(0), + RunE: func(cmd *cobra.Command, args []string) error { + clientCtx := clientCtx.NewCLIContext().WithProxy(m).WithInterfaceRegistry(reg) + route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, keeper.QueryExtraParams) + + res, _, err := clientCtx.Query(route) + if err != nil { + return err + } + + var params types.QueryExtraParams + m.GetCdc().MustUnmarshalJSON(res, ¶ms) + return clientCtx.PrintOutput(params) + }, + } + return cmd +} + func NewCmdGetAddressWhitelist(m *codec.CodecProxy, reg codectypes.InterfaceRegistry) *cobra.Command { cmd := &cobra.Command{ Use: "get-address-whitelist", diff --git a/x/wasm/client/rest/query.go b/x/wasm/client/rest/query.go index dc0755bc60..7e59a583bb 100644 --- a/x/wasm/client/rest/query.go +++ b/x/wasm/client/rest/query.go @@ -32,6 +32,7 @@ func registerQueryRoutes(cliCtx clientCtx.CLIContext, r *mux.Router) { r.HandleFunc("/wasm/contract/{contractAddr}/raw/{key}", queryContractStateRawHandlerFn(cliCtx)).Queries("encoding", "{encoding}").Methods("GET") r.HandleFunc("/wasm/contract/{contractAddr}/blocked_methods", queryContractBlockedMethodsHandlerFn(cliCtx)).Methods("GET") r.HandleFunc("/wasm/params", queryParamsHandlerFn(cliCtx)).Methods("GET") + r.HandleFunc("/wasm/extra_params", queryExtraParamsHandlerFn(cliCtx)).Methods("GET") r.HandleFunc("/wasm/whitelist", queryContractWhitelistHandlerFn(cliCtx)).Methods("GET") } @@ -55,6 +56,26 @@ func queryParamsHandlerFn(cliCtx clientCtx.CLIContext) http.HandlerFunc { } } +func queryExtraParamsHandlerFn(cliCtx clientCtx.CLIContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r) + if !ok { + return + } + + route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, keeper.QueryExtraParams) + + res, height, err := cliCtx.Query(route) + if err != nil { + rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + return + } + + cliCtx = cliCtx.WithHeight(height) + rest.PostProcessResponse(w, cliCtx, res) + } +} + func queryContractWhitelistHandlerFn(cliCtx clientCtx.CLIContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r) diff --git a/x/wasm/keeper/genesis_test.go b/x/wasm/keeper/genesis_test.go index 21b551a450..49bc720417 100644 --- a/x/wasm/keeper/genesis_test.go +++ b/x/wasm/keeper/genesis_test.go @@ -458,10 +458,10 @@ func TestImportContractWithCodeHistoryReset(t *testing.T) { "code_id": "1", "code_info": { "code_hash": %q, - "creator": "cosmos1qtu5n0cnhfkjj6l2rq97hmky9fd89gwca9yarx", + "creator": "ex190227rqaps5nplhg2tg8hww7slvvquzy0qa0l0", "instantiate_config": { "permission": "OnlyAddress", - "address": "cosmos1qtu5n0cnhfkjj6l2rq97hmky9fd89gwca9yarx" + "address": "ex190227rqaps5nplhg2tg8hww7slvvquzy0qa0l0" } }, "code_bytes": %q @@ -472,8 +472,8 @@ func TestImportContractWithCodeHistoryReset(t *testing.T) { "contract_address": "0x5A8D648DEE57b2fc90D98DC17fa887159b69638b", "contract_info": { "code_id": "1", - "creator": "cosmos13x849jzd03vne42ynpj25hn8npjecxqrjghd8x", - "admin": "cosmos1h5t8zxmjr30e9dqghtlpl40f2zz5cgey6esxtn", + "creator": "ex1fsfwwvl93qv6r56jpu084hxxzn9zphnyxhske5", + "admin": "ex1s0vrf96rrsknl64jj65lhf89ltwj7lksr7m3r9", "label": "ȀĴnZV芢毤" } } @@ -513,7 +513,7 @@ func TestImportContractWithCodeHistoryReset(t *testing.T) { // verify code info gotCodeInfo := keeper.GetCodeInfo(ctx, 1) require.NotNil(t, gotCodeInfo) - codeCreatorAddr := "cosmos1qtu5n0cnhfkjj6l2rq97hmky9fd89gwca9yarx" + codeCreatorAddr := "ex190227rqaps5nplhg2tg8hww7slvvquzy0qa0l0" expCodeInfo := types.CodeInfo{ CodeHash: wasmCodeHash[:], Creator: codeCreatorAddr, @@ -528,8 +528,8 @@ func TestImportContractWithCodeHistoryReset(t *testing.T) { contractAddr, _ := sdk.WasmAddressFromBech32("0x5A8D648DEE57b2fc90D98DC17fa887159b69638b") gotContractInfo := keeper.GetContractInfo(ctx, contractAddr) require.NotNil(t, gotContractInfo) - contractCreatorAddr := "cosmos13x849jzd03vne42ynpj25hn8npjecxqrjghd8x" - adminAddr := "cosmos1h5t8zxmjr30e9dqghtlpl40f2zz5cgey6esxtn" + contractCreatorAddr := "ex1fsfwwvl93qv6r56jpu084hxxzn9zphnyxhske5" + adminAddr := "ex1s0vrf96rrsknl64jj65lhf89ltwj7lksr7m3r9" expContractInfo := types.ContractInfo{ CodeID: firstCodeID, diff --git a/x/wasm/keeper/legacy_querier.go b/x/wasm/keeper/legacy_querier.go index 07fd61ec98..80d95f3203 100644 --- a/x/wasm/keeper/legacy_querier.go +++ b/x/wasm/keeper/legacy_querier.go @@ -21,6 +21,7 @@ const ( QueryContractHistory = "contract-history" QueryListContractBlockedMethod = "list-contract-blocked-method" QueryParams = "params" + QueryExtraParams = "extra-params" ) const ( @@ -76,6 +77,8 @@ func NewLegacyQuerier(keeper types.ViewKeeper, gasLimit sdk.Gas) sdk.Querier { rsp = queryListContractBlockedMethod(ctx, contractAddr, keeper) case QueryParams: rsp = queryParams(ctx, keeper) + case QueryExtraParams: + rsp = queryExtraParams(ctx, keeper) default: return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "unknown data query endpoint") } @@ -171,3 +174,10 @@ func queryParams(ctx sdk.Context, keeper types.ViewKeeper) *types.Params { params := keeper.GetParams(ctx) return ¶ms } + +func queryExtraParams(ctx sdk.Context, keeper types.ViewKeeper) *types.QueryExtraParams { + params := types.QueryExtraParams{ + GasFactor: strconv.FormatUint(keeper.GetGasFactor(ctx), 10), + } + return ¶ms +} diff --git a/x/wasm/keeper/proposal_integration_test.go b/x/wasm/keeper/proposal_integration_test.go index dee98efa3d..148342e5e2 100644 --- a/x/wasm/keeper/proposal_integration_test.go +++ b/x/wasm/keeper/proposal_integration_test.go @@ -1,4 +1,20 @@ -package keeper +package keeper_test + +import ( + "github.com/okex/exchain/x/wasm" + "github.com/okex/exchain/x/wasm/keeper" + "testing" + "time" + + "github.com/okex/exchain/app" + "github.com/okex/exchain/libs/cosmos-sdk/codec" + sdk "github.com/okex/exchain/libs/cosmos-sdk/types" + abci "github.com/okex/exchain/libs/tendermint/abci/types" + tmtypes "github.com/okex/exchain/libs/tendermint/types" + govtypes "github.com/okex/exchain/x/gov/types" + "github.com/okex/exchain/x/wasm/types" + "github.com/stretchr/testify/suite" +) //import ( // "bytes" @@ -841,3 +857,79 @@ package keeper // }) // } //} + +func (suite *ProposalTestSuite) SetupTest() { + checkTx := false + + suite.app = app.Setup(checkTx) + suite.ctx = suite.app.BaseApp.NewContext(checkTx, abci.Header{Height: 1, ChainID: "ethermint-3", Time: time.Now().UTC()}) + suite.wasmHandler = keeper.NewWasmProposalHandler(&suite.app.WasmKeeper, wasm.NecessaryProposals) + suite.codec = codec.New() +} + +func TestSuite(t *testing.T) { + suite.Run(t, new(ProposalTestSuite)) +} + +type ProposalTestSuite struct { + suite.Suite + + ctx sdk.Context + wasmHandler govtypes.Handler + app *app.OKExChainApp + codec *codec.Codec +} + +func (suite *ProposalTestSuite) TestModifyNextBlockUpdateProposal() { + suite.ctx.SetBlockHeight(1000) + + proposal := types.ExtraProposal{ + Title: types.ActionModifyGasFactor, + Description: "Description", + Action: types.ActionModifyGasFactor, + Extra: "", + } + + govProposal := govtypes.Proposal{ + Content: &proposal, + } + + testCases := []struct { + msg string + extra string + gasFactor uint64 + expectError error + }{ + {"1", "", keeper.DefaultGasMultiplier, types.ErrExtraProposalParams("parse json error, expect like {\"factor\":\"14\"}, but get:")}, + {"1", "{}", keeper.DefaultGasMultiplier, types.ErrExtraProposalParams("parse factor error, decimal string cannot be empty")}, + {"1", "{\"\"}", keeper.DefaultGasMultiplier, types.ErrExtraProposalParams("parse json error, expect like {\"factor\":\"14\"}, but get:{\"\"}")}, + {"1", "{\"df\", \"\"}", keeper.DefaultGasMultiplier, types.ErrExtraProposalParams("parse json error, expect like {\"factor\":\"14\"}, but get:{\"df\", \"\"}")}, + {"1", "{\"factor\":19.7}", keeper.DefaultGasMultiplier, types.ErrExtraProposalParams("parse json error, expect like {\"factor\":\"14\"}, but get:{\"factor\":19.7}")}, + {"1", "{\"factor\":19}", keeper.DefaultGasMultiplier, types.ErrExtraProposalParams("parse json error, expect like {\"factor\":\"14\"}, but get:{\"factor\":19}")}, + {"1", "{\"factor\": \"adfasd\"}", keeper.DefaultGasMultiplier, types.ErrExtraProposalParams("parse factor error, failed to set decimal string: adfasd000000000000000000")}, + {"1", "{\"factor\": \"-1\"}", keeper.DefaultGasMultiplier, types.ErrExtraProposalParams("parse factor error, expect factor positive and 18 precision, but get -1")}, + {"2", "{\"factor\": \"0\"}", keeper.DefaultGasMultiplier, types.ErrExtraProposalParams("parse factor error, expect factor positive and 18 precision, but get 0")}, + {"3", "{\"factor\": \"0.0000000000000000000000001\"}", keeper.DefaultGasMultiplier, types.ErrExtraProposalParams("parse factor error, invalid precision; max: 18, got: 25")}, + {"4", "{\"factor\": \"0.000000000000000001\"}", keeper.DefaultGasMultiplier, types.ErrCodeInvalidGasFactor}, + {"4", "{\"factor\":\"19.7a\"}", keeper.DefaultGasMultiplier, types.ErrExtraProposalParams("parse factor error, failed to set decimal string: 197a0000000000000000")}, + {"4", "{\"factor\":\"a19.7\"}", keeper.DefaultGasMultiplier, types.ErrExtraProposalParams("parse factor error, failed to set decimal string: a19700000000000000000")}, + {"4", "{\"factor\": \"10000000\"}", (uint64(types.MaxGasFactor)) * keeper.BaseGasMultiplier, nil}, + {"4", "{\"factor\":\"19.7\"}", 197 * keeper.BaseGasMultiplier / 10, nil}, + } + + tmtypes.UnittestOnlySetMilestoneEarthHeight(-1) + + for _, tc := range testCases { + suite.Run(tc.msg, func() { + proposal.Extra = tc.extra + govProposal.Content = &proposal + + err := suite.wasmHandler(suite.ctx, &govProposal) + suite.app.BeginBlock(abci.RequestBeginBlock{Header: abci.Header{ChainID: "exchain-67", Height: 1, Time: time.Now()}}) + suite.Require().Equal(tc.expectError, err) + + gasFactor := suite.app.WasmKeeper.GetGasFactor(suite.ctx) + suite.Require().Equal(tc.gasFactor, gasFactor) + }) + } +} diff --git a/x/wasm/keeper/querier_test.go b/x/wasm/keeper/querier_test.go index 8f208f92dd..7257f94545 100644 --- a/x/wasm/keeper/querier_test.go +++ b/x/wasm/keeper/querier_test.go @@ -659,7 +659,7 @@ func TestQueryCodeInfo(t *testing.T) { ctx, keepers := CreateTestInput(t, false, SupportedFeatures) keeper := keepers.WasmKeeper - anyAddress, err := sdk.WasmAddressFromBech32("cosmos100dejzacpanrldpjjwksjm62shqhyss44jf5xz") + anyAddress, err := sdk.WasmAddressFromBech32("ex190227rqaps5nplhg2tg8hww7slvvquzy0qa0l0") require.NoError(t, err) specs := map[string]struct { codeId uint64 @@ -714,7 +714,7 @@ func TestQueryCodeInfoList(t *testing.T) { ctx, keepers := CreateTestInput(t, false, SupportedFeatures) keeper := keepers.WasmKeeper - anyAddress, err := sdk.WasmAddressFromBech32("cosmos100dejzacpanrldpjjwksjm62shqhyss44jf5xz") + anyAddress, err := sdk.WasmAddressFromBech32("ex190227rqaps5nplhg2tg8hww7slvvquzy0qa0l0") require.NoError(t, err) codeInfoWithConfig := func(accessConfig types.AccessConfig) types.CodeInfo { codeInfo := types.CodeInfoFixture(types.WithSHA256CodeHash(wasmCode)) diff --git a/x/wasm/proto/proposal_custom.proto b/x/wasm/proto/proposal_custom.proto index 7157b6625c..7f6709884c 100644 --- a/x/wasm/proto/proposal_custom.proto +++ b/x/wasm/proto/proposal_custom.proto @@ -32,3 +32,7 @@ message ExtraProposal { string action = 3; string extra = 4; } + +message QueryExtraParams { + string gas_factor = 1; +} diff --git a/x/wasm/types/errors.go b/x/wasm/types/errors.go index cab9feca8d..b006995aea 100644 --- a/x/wasm/types/errors.go +++ b/x/wasm/types/errors.go @@ -91,11 +91,11 @@ var ( // ErrExceedMaxQueryStackSize error if max query stack size is exceeded ErrExceedMaxQueryStackSize = sdkErrors.Register(DefaultCodespace, 27, "max query stack size exceeded") - ErrCodeInvalidGasFactor = sdkErrors.Register(ModuleName, 29, "invalid gas factor") + ErrCodeInvalidGasFactor = sdkErrors.Register(DefaultCodespace, 29, "invalid gas factor") ErrHandleExtraProposal = sdkErrors.Register(DefaultCodespace, 30, "handle extra proposal error") ErrUnknownExtraProposalAction = sdkErrors.Register(DefaultCodespace, 31, "extra proposal's action unknown") - ErrProposerMustBeValidator = sdkErrors.Register(ModuleName, 32, "the proposal of proposer must be validator") + ErrProposerMustBeValidator = sdkErrors.Register(DefaultCodespace, 32, "the proposal of proposer must be validator") ) type ErrNoSuchContract struct { diff --git a/x/wasm/types/exported_keepers.go b/x/wasm/types/exported_keepers.go index 460e869375..caffbfafe4 100644 --- a/x/wasm/types/exported_keepers.go +++ b/x/wasm/types/exported_keepers.go @@ -23,6 +23,7 @@ type ViewKeeper interface { IsPinnedCode(ctx sdk.Context, codeID uint64) bool GetContractMethodBlockedList(ctx sdk.Context, contractAddr string) *ContractMethods GetParams(ctx sdk.Context) Params + GetGasFactor(ctx sdk.Context) uint64 } // ContractOpsKeeper contains mutable operations on a contract. diff --git a/x/wasm/types/proposal_custom.go b/x/wasm/types/proposal_custom.go index 04adee26c3..a9213a4a02 100644 --- a/x/wasm/types/proposal_custom.go +++ b/x/wasm/types/proposal_custom.go @@ -10,8 +10,9 @@ import ( ) const ( - maxAddressListLength = 100 - maxMethodListLength = 100 + maxAddressListLength = 100 + maxMethodListLength = 100 + MaxGasFactor int64 = 10000000 ) // ProposalRoute returns the routing key of a parameter change proposal. @@ -223,16 +224,20 @@ func NewActionModifyGasFactor(data string) (sdk.Dec, error) { var param GasFactor err := json.Unmarshal([]byte(data), ¶m) if err != nil { - return sdk.Dec{}, ErrExtraProposalParams("parse json error") + return sdk.Dec{}, ErrExtraProposalParams(fmt.Sprintf("parse json error, expect like {\"factor\":\"14\"}, but get:%s", data)) } result, err := sdk.NewDecFromStr(param.Factor) if err != nil { - return sdk.Dec{}, ErrExtraProposalParams(fmt.Sprintf("parse factor error:%s", param.Factor)) + return sdk.Dec{}, ErrExtraProposalParams(fmt.Sprintf("parse factor error, %s", err.Error())) } if result.IsNil() || result.IsNegative() || result.IsZero() { - return sdk.Dec{}, ErrExtraProposalParams(fmt.Sprintf("parse factor error:%s", param.Factor)) + return sdk.Dec{}, ErrExtraProposalParams(fmt.Sprintf("parse factor error, expect factor positive and 18 precision, but get %s", param.Factor)) + } + + if result.GT(sdk.NewDec(MaxGasFactor)) { + return sdk.Dec{}, ErrExtraProposalParams(fmt.Sprintf("max gas factor:%v, but get:%s", MaxGasFactor, param.Factor)) } return result, nil diff --git a/x/wasm/types/proposal_custom.pb.go b/x/wasm/types/proposal_custom.pb.go index 4461e5031f..0a554a481b 100644 --- a/x/wasm/types/proposal_custom.pb.go +++ b/x/wasm/types/proposal_custom.pb.go @@ -294,12 +294,51 @@ func (m *ExtraProposal) GetExtra() string { return "" } +type QueryExtraParams struct { + GasFactor string `protobuf:"bytes,1,opt,name=gas_factor,json=gasFactor,proto3" json:"gas_factor,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` +} + +func (m *QueryExtraParams) Reset() { *m = QueryExtraParams{} } +func (m *QueryExtraParams) String() string { return proto.CompactTextString(m) } +func (*QueryExtraParams) ProtoMessage() {} +func (*QueryExtraParams) Descriptor() ([]byte, []int) { + return fileDescriptor_dd9d4d6e8a1d82c0, []int{5} +} +func (m *QueryExtraParams) XXX_Unmarshal(b []byte) error { + return xxx_messageInfo_QueryExtraParams.Unmarshal(m, b) +} +func (m *QueryExtraParams) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + return xxx_messageInfo_QueryExtraParams.Marshal(b, m, deterministic) +} +func (m *QueryExtraParams) XXX_Merge(src proto.Message) { + xxx_messageInfo_QueryExtraParams.Merge(m, src) +} +func (m *QueryExtraParams) XXX_Size() int { + return xxx_messageInfo_QueryExtraParams.Size(m) +} +func (m *QueryExtraParams) XXX_DiscardUnknown() { + xxx_messageInfo_QueryExtraParams.DiscardUnknown(m) +} + +var xxx_messageInfo_QueryExtraParams proto.InternalMessageInfo + +func (m *QueryExtraParams) GetGasFactor() string { + if m != nil { + return m.GasFactor + } + return "" +} + func init() { proto.RegisterType((*UpdateDeploymentWhitelistProposal)(nil), "types.UpdateDeploymentWhitelistProposal") proto.RegisterType((*UpdateWASMContractMethodBlockedListProposal)(nil), "types.UpdateWASMContractMethodBlockedListProposal") proto.RegisterType((*ContractMethods)(nil), "types.ContractMethods") proto.RegisterType((*Method)(nil), "types.Method") proto.RegisterType((*ExtraProposal)(nil), "types.ExtraProposal") + proto.RegisterType((*QueryExtraParams)(nil), "types.QueryExtraParams") } func init() { @@ -307,27 +346,29 @@ func init() { } var fileDescriptor_dd9d4d6e8a1d82c0 = []byte{ - // 341 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xac, 0x52, 0x4d, 0x4b, 0xeb, 0x40, - 0x14, 0x25, 0x4d, 0xdb, 0xd7, 0xde, 0x7e, 0xbc, 0xc7, 0xf0, 0x28, 0xc1, 0x55, 0xcc, 0xc6, 0xa0, - 0xd0, 0x42, 0xdd, 0x0b, 0xad, 0x75, 0x67, 0x41, 0x22, 0x52, 0x70, 0xa1, 0x4c, 0x33, 0x03, 0x1d, - 0x4c, 0x32, 0x61, 0xe6, 0x16, 0xdb, 0x5f, 0xe0, 0xef, 0x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c4, 0xd1, - 0x13, 0xfc, 0x3d, 0x2a, 0x27, 0x11, 0x74, 0x53, 0x47, 0x55, 0x39, 0x9d, 0xdb, 0x03, 0x8e, 0x9c, - 0xc1, 0x9f, 0xdc, 0x79, 0xa9, 0x85, 0x7e, 0xdc, 0x19, 0xf7, 0x9c, 0x17, 0xdb, 0x24, 0xd9, 0xa9, - 0xd1, 0x18, 0x9a, 0x96, 0x22, 0x04, 0xea, 0x05, 0xcd, 0x77, 0xe1, 0xcd, 0xbb, 0xda, 0x08, 0xdf, - 0xa0, 0xa2, 0x2e, 0xb5, 0x05, 0xd1, 0x1a, 0x7a, 0x37, 0xd5, 0xe3, 0xc7, 0x8b, 0x1b, 0x40, 0x93, - 0xa6, 0x46, 0xf4, 0x8d, 0xe8, 0xd0, 0xd7, 0xd8, 0xfa, 0xde, 0xd8, 0x69, 0xff, 0xb1, 0xeb, 0xae, - 0xd0, 0x44, 0x59, 0x36, 0xcd, 0xd9, 0x5d, 0x7e, 0x04, 0x00, 0x00, 0xff, 0xff, 0x1c, 0xb6, 0x74, - 0x86, 0x9c, 0x02, 0x00, 0x00, + // 373 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xac, 0x52, 0xdd, 0x6a, 0xdb, 0x30, + 0x14, 0xc6, 0x71, 0x92, 0x25, 0x27, 0x3f, 0x0b, 0x62, 0x04, 0x33, 0x18, 0x78, 0xbe, 0x99, 0xd9, + 0x20, 0x61, 0xd9, 0xfd, 0x20, 0x59, 0xb6, 0xab, 0x05, 0x36, 0x97, 0x12, 0xe8, 0x45, 0x83, 0x62, + 0xab, 0x89, 0xa8, 0x6d, 0x19, 0xe9, 0x98, 0x26, 0x4f, 0xd0, 0xe7, 0xea, 0x9b, 0x15, 0x4b, 0x4a, + 0x9b, 0xa4, 0x97, 0xed, 0x8d, 0xd1, 0xf9, 0x3e, 0xeb, 0x7c, 0x3f, 0x08, 0x82, 0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x77, 0x99, 0xf5, 0x52, 0xf3, 0xdd, 0xb0, 0x33, 0xe9, 0x59, 0x2f, 0x66, + 0x49, 0x74, 0x60, 0x83, 0x09, 0x34, 0x0d, 0x44, 0x08, 0xd4, 0x73, 0x9a, 0x1d, 0xc2, 0xeb, 0x73, + 0xd5, 0x08, 0xdb, 0xa1, 0xa4, 0x36, 0xb5, 0x19, 0x82, 0x12, 0x7a, 0xbf, 0xab, 0xc3, 0xab, 0x8b, + 0x1b, 0x42, 0x93, 0xc6, 0x9a, 0x74, 0x35, 0x69, 0xa7, 0x67, 0xd9, 0xfa, 0xb1, 0xec, 0x77, 0x18, + 0xfc, 0x2f, 0x99, 0xdc, 0x1b, 0x6d, 0x2a, 0x69, 0xa6, 0xc8, 0x27, 0x80, 0x0d, 0x55, 0xab, 0x1b, + 0x1a, 0xa3, 0x38, 0x34, 0xd1, 0xde, 0x50, 0xf5, 0x47, 0x03, 0xb3, 0xfe, 0x55, 0xd7, 0x3e, 0x5c, + 0x9d, 0x7e, 0xdd, 0xd4, 0x2f, 0xf5, 0xc7, 0x63, 0x00, 0x00, 0x00, 0xff, 0xff, 0x0c, 0x0b, 0x5a, + 0x84, 0xcf, 0x02, 0x00, 0x00, } diff --git a/x/wasm/types/proposal_test.go b/x/wasm/types/proposal_test.go index cd67fd017a..376695f531 100644 --- a/x/wasm/types/proposal_test.go +++ b/x/wasm/types/proposal_test.go @@ -3,13 +3,19 @@ package types import ( "bytes" "encoding/json" + "fmt" + "math/rand" "strings" "testing" + "time" sdk "github.com/okex/exchain/libs/cosmos-sdk/types" + "github.com/okex/exchain/libs/cosmos-sdk/x/gov/types" + "github.com/okex/exchain/libs/tendermint/global" govtypes "github.com/okex/exchain/x/gov/types" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "github.com/stretchr/testify/suite" "gopkg.in/yaml.v2" ) @@ -912,3 +918,305 @@ func TestProposalJsonSignBytes(t *testing.T) { }) } } + +type ProposalSuite struct { + suite.Suite +} + +func TestProposalSuite(t *testing.T) { + suite.Run(t, new(ProposalSuite)) +} + +func RandStr(length int) string { + str := "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" + bytes := []byte(str) + result := []byte{} + rand.Seed(time.Now().UnixNano() + int64(rand.Intn(100))) + for i := 0; i < length; i++ { + result = append(result, bytes[rand.Intn(len(bytes))]) + } + return string(result) +} + +func (suite *ProposalSuite) TestNewChangeDistributionTypeProposal() { + testCases := []struct { + name string + title string + description string + action string + extra string + err error + }{ + { + "no proposal title", + "", + "description", + "", + "", + govtypes.ErrInvalidProposalContent("proposal title cannot be blank"), + }, + { + "gt max proposal title length", + RandStr(types.MaxTitleLength + 1), + "description", + "", + "", + govtypes.ErrInvalidProposalContent(fmt.Sprintf("proposal title is longer than max length of %d", govtypes.MaxTitleLength)), + }, + { + "gt max proposal title length", + RandStr(types.MaxTitleLength), + "", + "", + "", + govtypes.ErrInvalidProposalContent("proposal description cannot be blank"), + }, + { + "gt max proposal description length", + RandStr(types.MaxTitleLength), + RandStr(types.MaxDescriptionLength + 1), + "", + "", + govtypes.ErrInvalidProposalContent(fmt.Sprintf("proposal description is longer than max length of %d", govtypes.MaxDescriptionLength)), + }, + { + "no action", + RandStr(types.MaxTitleLength), + RandStr(types.MaxDescriptionLength), + "", + "", + govtypes.ErrInvalidProposalContent("extra proposal's action is required"), + }, + { + "action too large", + RandStr(types.MaxTitleLength), + RandStr(types.MaxDescriptionLength), + RandStr(govtypes.MaxExtraActionLength + 1), + "", + govtypes.ErrInvalidProposalContent("extra proposal's action length is bigger than max length"), + }, + { + "no extra body", + RandStr(types.MaxTitleLength), + RandStr(types.MaxDescriptionLength), + RandStr(govtypes.MaxExtraActionLength), + "", + govtypes.ErrInvalidProposalContent("extra proposal's extra is required"), + }, + { + "extra too large", + RandStr(types.MaxTitleLength), + RandStr(types.MaxDescriptionLength), + RandStr(govtypes.MaxTitleLength), + RandStr(govtypes.MaxExtraBodyLength + 1), + govtypes.ErrInvalidProposalContent("extra proposal's extra body length is bigger than max length"), + }, + { + "unknown action", + RandStr(types.MaxTitleLength), + RandStr(types.MaxDescriptionLength), + RandStr(govtypes.MaxTitleLength), + RandStr(govtypes.MaxExtraBodyLength), + ErrUnknownExtraProposalAction, + }, + { + "ActionModifyGasFactor, parse error json", + RandStr(types.MaxTitleLength), + RandStr(types.MaxDescriptionLength), + ActionModifyGasFactor, + "{dfafdasf}", + ErrExtraProposalParams("parse json error, expect like {\"factor\":\"14\"}, but get:{dfafdasf}"), + }, + { + "ActionModifyGasFactor, action is nil", + RandStr(types.MaxTitleLength), + RandStr(types.MaxDescriptionLength), + "", + "", + govtypes.ErrInvalidProposalContent("extra proposal's action is required"), + }, + { + "ActionModifyGasFactor, extra is nil", + RandStr(types.MaxTitleLength), + RandStr(types.MaxDescriptionLength), + "hello", + "", + govtypes.ErrInvalidProposalContent("extra proposal's extra is required"), + }, + { + "ActionModifyGasFactor, error json", + RandStr(types.MaxTitleLength), + RandStr(types.MaxDescriptionLength), + "hello", + "hello", + ErrUnknownExtraProposalAction, + }, + { + "ActionModifyGasFactor, extra is nil", + RandStr(types.MaxTitleLength), + RandStr(types.MaxDescriptionLength), + ActionModifyGasFactor, + "", + govtypes.ErrInvalidProposalContent("extra proposal's extra is required"), + }, + { + "ActionModifyGasFactor, error json", + RandStr(types.MaxTitleLength), + RandStr(types.MaxDescriptionLength), + ActionModifyGasFactor, + "{}", + ErrExtraProposalParams("parse factor error, decimal string cannot be empty"), + }, + { + "ActionModifyGasFactor, error json", + RandStr(types.MaxTitleLength), + RandStr(types.MaxDescriptionLength), + ActionModifyGasFactor, + "{\"\"}", + ErrExtraProposalParams("parse json error, expect like {\"factor\":\"14\"}, but get:{\"\"}"), + }, + { + "ActionModifyGasFactor, key error", + RandStr(types.MaxTitleLength), + RandStr(types.MaxDescriptionLength), + ActionModifyGasFactor, + "{\"df\": \"\"}", + ErrExtraProposalParams("parse factor error, decimal string cannot be empty"), + }, + { + "ActionModifyGasFactor, value error", + RandStr(types.MaxTitleLength), + RandStr(types.MaxDescriptionLength), + ActionModifyGasFactor, + "{\"factor\":19.7}", + ErrExtraProposalParams("parse json error, expect like {\"factor\":\"14\"}, but get:{\"factor\":19.7}"), + }, + { + "ActionModifyGasFactor, value error", + RandStr(types.MaxTitleLength), + RandStr(types.MaxDescriptionLength), + ActionModifyGasFactor, + "{\"factor\":19}", + ErrExtraProposalParams("parse json error, expect like {\"factor\":\"14\"}, but get:{\"factor\":19}"), + }, + { + "ActionModifyGasFactor, value error", + RandStr(types.MaxTitleLength), + RandStr(types.MaxDescriptionLength), + ActionModifyGasFactor, + "{\"factor\": \"adfasd\"}", + ErrExtraProposalParams("parse factor error, failed to set decimal string: adfasd000000000000000000"), + }, + { + "ActionModifyGasFactor, value -1", + RandStr(types.MaxTitleLength), + RandStr(types.MaxDescriptionLength), + ActionModifyGasFactor, + "{\"factor\": \"-1\"}", + ErrExtraProposalParams("parse factor error, expect factor positive and 18 precision, but get -1"), + }, + { + "ActionModifyGasFactor, value 0", + RandStr(types.MaxTitleLength), + RandStr(types.MaxDescriptionLength), + ActionModifyGasFactor, + "{\"factor\": \"0\"}", + ErrExtraProposalParams("parse factor error, expect factor positive and 18 precision, but get 0"), + }, + { + "ActionModifyGasFactor, value > 18", + RandStr(types.MaxTitleLength), + RandStr(types.MaxDescriptionLength), + ActionModifyGasFactor, + "{\"factor\": \"0.0000000000000000001\"}", + ErrExtraProposalParams("parse factor error, invalid precision; max: 18, got: 19"), + }, + { + "ActionModifyGasFactor, value = 18", + RandStr(types.MaxTitleLength), + RandStr(types.MaxDescriptionLength), + ActionModifyGasFactor, + "{\"factor\": \"0.000000000000000001\"}", + nil, + }, + { + "ActionModifyGasFactor, value ok", + RandStr(types.MaxTitleLength), + RandStr(types.MaxDescriptionLength), + ActionModifyGasFactor, + "{\"factor\": \"10000001\"}", + ErrExtraProposalParams("max gas factor:10000000, but get:10000001"), + }, + { + "ActionModifyGasFactor, value ok", + RandStr(types.MaxTitleLength), + RandStr(types.MaxDescriptionLength), + ActionModifyGasFactor, + "{\"factor\": \"10000000\"}", + nil, + }, + { + "ActionModifyGasFactor, value error", + RandStr(types.MaxTitleLength), + RandStr(types.MaxDescriptionLength), + ActionModifyGasFactor, + "{\"factor\":\"19.7a\"}", + ErrExtraProposalParams("parse factor error, failed to set decimal string: 197a0000000000000000"), + }, + { + "ActionModifyGasFactor, value error", + RandStr(types.MaxTitleLength), + RandStr(types.MaxDescriptionLength), + ActionModifyGasFactor, + "{\"factor\":\"a19.7\"}", + ErrExtraProposalParams("parse factor error, failed to set decimal string: a19700000000000000000"), + }, + { + "ActionModifyGasFactor, value ok", + RandStr(types.MaxTitleLength), + RandStr(types.MaxDescriptionLength), + ActionModifyGasFactor, + "{\"factor\":\"19.7\"}", + nil, + }, + { + "ActionModifyGasFactor, value ok", + RandStr(types.MaxTitleLength), + RandStr(types.MaxDescriptionLength), + ActionModifyGasFactor, + "{\"factor\":\"19.6757657657657567864554354354357\"}", + ErrExtraProposalParams("parse factor error, invalid precision; max: 18, got: 31"), + }, + { + "ActionModifyGasFactor, value ok", + RandStr(types.MaxTitleLength), + RandStr(types.MaxDescriptionLength), + ActionModifyGasFactor, + "{\"factor\":\"19.675765765767\"}", + nil, + }, + } + + for _, tc := range testCases { + suite.Run(tc.name, func() { + global.SetGlobalHeight(100) + proposal := ExtraProposal{ + Title: tc.title, + Description: tc.description, + Action: tc.action, + Extra: tc.extra, + } + + require.Equal(suite.T(), tc.title, proposal.GetTitle()) + require.Equal(suite.T(), tc.description, proposal.GetDescription()) + require.Equal(suite.T(), RouterKey, proposal.ProposalRoute()) + require.Equal(suite.T(), string(ProposalTypeExtra), proposal.ProposalType()) + require.NotPanics(suite.T(), func() { + _ = proposal.String() + }) + + err := proposal.ValidateBasic() + require.Equal(suite.T(), tc.err, err) + }) + } +}