From 90681abcc9d2c8409af754c7875320eb7743c1a1 Mon Sep 17 00:00:00 2001 From: Alexander Bezobchuk Date: Fri, 29 May 2020 16:07:34 -0400 Subject: [PATCH] Merge PR #532: Fix expiry parsing --- x/pricefeed/alias.go | 1 + x/pricefeed/client/cli/tx.go | 21 ++++++++++++++------- x/pricefeed/client/rest/tx.go | 23 +++++++++++++---------- x/pricefeed/types/msgs.go | 3 +++ 4 files changed, 31 insertions(+), 17 deletions(-) diff --git a/x/pricefeed/alias.go b/x/pricefeed/alias.go index 6a734e06..c3d44a41 100644 --- a/x/pricefeed/alias.go +++ b/x/pricefeed/alias.go @@ -31,6 +31,7 @@ const ( QueryOracles = types.QueryOracles QueryRawPrices = types.QueryRawPrices QueryPrice = types.QueryPrice + MaxExpiry = types.MaxExpiry ) // nolint diff --git a/x/pricefeed/client/cli/tx.go b/x/pricefeed/client/cli/tx.go index 52e8414d..2db366b9 100644 --- a/x/pricefeed/client/cli/tx.go +++ b/x/pricefeed/client/cli/tx.go @@ -3,6 +3,7 @@ package cli import ( "bufio" "fmt" + "strconv" "time" "github.com/spf13/cobra" @@ -41,7 +42,7 @@ func GetTxCmd(storeKey string, cdc *codec.Codec) *cobra.Command { func GetCmdPostPrice(cdc *codec.Codec) *cobra.Command { return &cobra.Command{ Use: "postprice [marketID] [price] [expiry]", - Short: "post the latest price for a particular market", + Short: "post the latest price for a particular market with a given expiry as a UNIX time", Args: cobra.ExactArgs(3), RunE: func(cmd *cobra.Command, args []string) error { inBuf := bufio.NewReader(cmd.InOrStdin()) @@ -52,17 +53,23 @@ func GetCmdPostPrice(cdc *codec.Codec) *cobra.Command { if err != nil { return err } - expiryInt, ok := sdk.NewIntFromString(args[2]) - if !ok { - return fmt.Errorf("invalid expiry - %s", args[2]) + + expiryInt, err := strconv.ParseInt(args[2], 10, 64) + if err != nil { + return fmt.Errorf("invalid expiry %s: %w", args[2], err) } - expiry := tmtime.Canonical(time.Unix(expiryInt.Int64(), 0)) + + if expiryInt > types.MaxExpiry { + return fmt.Errorf("invalid expiry; got %d, max: %d", expiryInt, types.MaxExpiry) + } + + expiry := tmtime.Canonical(time.Unix(expiryInt, 0)) msg := types.NewMsgPostPrice(cliCtx.GetFromAddress(), args[0], price, expiry) - err = msg.ValidateBasic() - if err != nil { + if err = msg.ValidateBasic(); err != nil { return err } + return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg}) }, } diff --git a/x/pricefeed/client/rest/tx.go b/x/pricefeed/client/rest/tx.go index 06c1cda5..3c70f037 100644 --- a/x/pricefeed/client/rest/tx.go +++ b/x/pricefeed/client/rest/tx.go @@ -3,15 +3,14 @@ package rest import ( "fmt" "net/http" + "strconv" "time" - "github.com/gorilla/mux" - "github.com/cosmos/cosmos-sdk/client/context" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/rest" "github.com/cosmos/cosmos-sdk/x/auth/client/utils" - + "github.com/gorilla/mux" tmtime "github.com/tendermint/tendermint/types/time" "github.com/kava-labs/kava/x/pricefeed/types" @@ -48,17 +47,21 @@ func postPriceHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { return } - expiryInt, ok := sdk.NewIntFromString(req.Expiry) - if !ok { - rest.WriteErrorResponse(w, http.StatusBadRequest, "invalid expiry") + expiryInt, err := strconv.ParseInt(req.Expiry, 10, 64) + if err != nil { + rest.WriteErrorResponse(w, http.StatusBadRequest, fmt.Sprintf("invalid expiry %s: %s", req.Expiry, err)) return } - expiry := tmtime.Canonical(time.Unix(expiryInt.Int64(), 0)) - // create the message + if expiryInt > types.MaxExpiry { + rest.WriteErrorResponse(w, http.StatusBadRequest, fmt.Sprintf("invalid expiry; got %d, max: %d", expiryInt, types.MaxExpiry)) + return + } + + expiry := tmtime.Canonical(time.Unix(expiryInt, 0)) + msg := types.NewMsgPostPrice(addr, req.MarketID, price, expiry) - err = msg.ValidateBasic() - if err != nil { + if err = msg.ValidateBasic(); err != nil { rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) return } diff --git a/x/pricefeed/types/msgs.go b/x/pricefeed/types/msgs.go index 77476699..408551b8 100644 --- a/x/pricefeed/types/msgs.go +++ b/x/pricefeed/types/msgs.go @@ -13,6 +13,9 @@ import ( const ( // TypeMsgPostPrice type of PostPrice msg TypeMsgPostPrice = "post_price" + + // MaxExpiry defines the max expiry time defined as UNIX time (9999-12-31 23:59:59 +0000 UTC) + MaxExpiry = 253402300799 ) // ensure Msg interface compliance at compile time