Merge PR #532: Fix expiry parsing

This commit is contained in:
Alexander Bezobchuk 2020-05-29 16:07:34 -04:00 committed by GitHub
parent 5302976109
commit 90681abcc9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 17 deletions

View File

@ -31,6 +31,7 @@ const (
QueryOracles = types.QueryOracles QueryOracles = types.QueryOracles
QueryRawPrices = types.QueryRawPrices QueryRawPrices = types.QueryRawPrices
QueryPrice = types.QueryPrice QueryPrice = types.QueryPrice
MaxExpiry = types.MaxExpiry
) )
// nolint // nolint

View File

@ -3,6 +3,7 @@ package cli
import ( import (
"bufio" "bufio"
"fmt" "fmt"
"strconv"
"time" "time"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -41,7 +42,7 @@ func GetTxCmd(storeKey string, cdc *codec.Codec) *cobra.Command {
func GetCmdPostPrice(cdc *codec.Codec) *cobra.Command { func GetCmdPostPrice(cdc *codec.Codec) *cobra.Command {
return &cobra.Command{ return &cobra.Command{
Use: "postprice [marketID] [price] [expiry]", 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), Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
inBuf := bufio.NewReader(cmd.InOrStdin()) inBuf := bufio.NewReader(cmd.InOrStdin())
@ -52,17 +53,23 @@ func GetCmdPostPrice(cdc *codec.Codec) *cobra.Command {
if err != nil { if err != nil {
return err return err
} }
expiryInt, ok := sdk.NewIntFromString(args[2])
if !ok { expiryInt, err := strconv.ParseInt(args[2], 10, 64)
return fmt.Errorf("invalid expiry - %s", args[2]) 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) msg := types.NewMsgPostPrice(cliCtx.GetFromAddress(), args[0], price, expiry)
err = msg.ValidateBasic() if err = msg.ValidateBasic(); err != nil {
if err != nil {
return err return err
} }
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg}) return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
}, },
} }

View File

@ -3,15 +3,14 @@ package rest
import ( import (
"fmt" "fmt"
"net/http" "net/http"
"strconv"
"time" "time"
"github.com/gorilla/mux"
"github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/client/context"
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/rest" "github.com/cosmos/cosmos-sdk/types/rest"
"github.com/cosmos/cosmos-sdk/x/auth/client/utils" "github.com/cosmos/cosmos-sdk/x/auth/client/utils"
"github.com/gorilla/mux"
tmtime "github.com/tendermint/tendermint/types/time" tmtime "github.com/tendermint/tendermint/types/time"
"github.com/kava-labs/kava/x/pricefeed/types" "github.com/kava-labs/kava/x/pricefeed/types"
@ -48,17 +47,21 @@ func postPriceHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return return
} }
expiryInt, ok := sdk.NewIntFromString(req.Expiry) expiryInt, err := strconv.ParseInt(req.Expiry, 10, 64)
if !ok { if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, "invalid expiry") rest.WriteErrorResponse(w, http.StatusBadRequest, fmt.Sprintf("invalid expiry %s: %s", req.Expiry, err))
return 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) msg := types.NewMsgPostPrice(addr, req.MarketID, price, expiry)
err = msg.ValidateBasic() if err = msg.ValidateBasic(); err != nil {
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return return
} }

View File

@ -13,6 +13,9 @@ import (
const ( const (
// TypeMsgPostPrice type of PostPrice msg // TypeMsgPostPrice type of PostPrice msg
TypeMsgPostPrice = "post_price" 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 // ensure Msg interface compliance at compile time