Move generate_only and simulate to POST body in REST txs

Closes: #3056
This commit is contained in:
Alessio Treglia 2018-12-10 14:26:34 +00:00
parent 945803d586
commit ac0a7c0a1d
3 changed files with 26 additions and 38 deletions

View File

@ -279,29 +279,29 @@ func TestCoinSend(t *testing.T) {
require.Equal(t, int64(1), mycoins.Amount.Int64()) require.Equal(t, int64(1), mycoins.Amount.Int64())
// test failure with too little gas // test failure with too little gas
res, body, _ = doSendWithGas(t, port, seed, name, password, addr, "100", 0, "") res, body, _ = doSendWithGas(t, port, seed, name, password, addr, "100", 0, false, false)
require.Equal(t, http.StatusInternalServerError, res.StatusCode, body) require.Equal(t, http.StatusInternalServerError, res.StatusCode, body)
// test failure with negative gas // test failure with negative gas
res, body, _ = doSendWithGas(t, port, seed, name, password, addr, "-200", 0, "") res, body, _ = doSendWithGas(t, port, seed, name, password, addr, "-200", 0, false, false)
require.Equal(t, http.StatusBadRequest, res.StatusCode, body) require.Equal(t, http.StatusBadRequest, res.StatusCode, body)
// test failure with 0 gas // test failure with 0 gas
res, body, _ = doSendWithGas(t, port, seed, name, password, addr, "0", 0, "") res, body, _ = doSendWithGas(t, port, seed, name, password, addr, "0", 0, false, false)
require.Equal(t, http.StatusInternalServerError, res.StatusCode, body) require.Equal(t, http.StatusInternalServerError, res.StatusCode, body)
// test failure with wrong adjustment // test failure with wrong adjustment
res, body, _ = doSendWithGas(t, port, seed, name, password, addr, "simulate", 0.1, "") res, body, _ = doSendWithGas(t, port, seed, name, password, addr, "simulate", 0.1, false, false)
require.Equal(t, http.StatusInternalServerError, res.StatusCode, body) require.Equal(t, http.StatusInternalServerError, res.StatusCode, body)
// run simulation and test success with estimated gas // run simulation and test success with estimated gas
res, body, _ = doSendWithGas(t, port, seed, name, password, addr, "", 0, "?simulate=true") res, body, _ = doSendWithGas(t, port, seed, name, password, addr, "", 0, true, false)
require.Equal(t, http.StatusOK, res.StatusCode, body) require.Equal(t, http.StatusOK, res.StatusCode, body)
var responseBody struct { var responseBody struct {
GasEstimate int64 `json:"gas_estimate"` GasEstimate int64 `json:"gas_estimate"`
} }
require.Nil(t, json.Unmarshal([]byte(body), &responseBody)) require.Nil(t, json.Unmarshal([]byte(body), &responseBody))
res, body, _ = doSendWithGas(t, port, seed, name, password, addr, fmt.Sprintf("%v", responseBody.GasEstimate), 0, "") res, body, _ = doSendWithGas(t, port, seed, name, password, addr, fmt.Sprintf("%v", responseBody.GasEstimate), 0, false, false)
require.Equal(t, http.StatusOK, res.StatusCode, body) require.Equal(t, http.StatusOK, res.StatusCode, body)
} }
@ -342,7 +342,7 @@ func TestCoinSendGenerateSignAndBroadcast(t *testing.T) {
acc := getAccount(t, port, addr) acc := getAccount(t, port, addr)
// generate TX // generate TX
res, body, _ := doSendWithGas(t, port, seed, name, password, addr, "simulate", 0, "?generate_only=true") res, body, _ := doSendWithGas(t, port, seed, name, password, addr, "simulate", 0, false, true)
require.Equal(t, http.StatusOK, res.StatusCode, body) require.Equal(t, http.StatusOK, res.StatusCode, body)
var msg auth.StdTx var msg auth.StdTx
require.Nil(t, cdc.UnmarshalJSON([]byte(body), &msg)) require.Nil(t, cdc.UnmarshalJSON([]byte(body), &msg))
@ -897,7 +897,9 @@ func getAccount(t *testing.T, port string, addr sdk.AccAddress) auth.Account {
return acc return acc
} }
func doSendWithGas(t *testing.T, port, seed, name, password string, addr sdk.AccAddress, gas string, gasAdjustment float64, queryStr string) (res *http.Response, body string, receiveAddr sdk.AccAddress) { func doSendWithGas(t *testing.T, port, seed, name, password string, addr sdk.AccAddress, gas string,
gasAdjustment float64, simulate, generateOnly bool) (
res *http.Response, body string, receiveAddr sdk.AccAddress) {
// create receive address // create receive address
kb := client.MockKeyBase() kb := client.MockKeyBase()
@ -935,11 +937,13 @@ func doSendWithGas(t *testing.T, port, seed, name, password string, addr sdk.Acc
"password": "%s", "password": "%s",
"chain_id": "%s", "chain_id": "%s",
"account_number":"%d", "account_number":"%d",
"sequence":"%d" "sequence": "%d",
"simulate": %v,
"generate_only": %v
} }
}`, coinbz, gasStr, gasAdjustmentStr, name, password, chainID, accnum, sequence)) }`, coinbz, gasStr, gasAdjustmentStr, name, password, chainID, accnum, sequence, simulate, generateOnly))
res, body = Request(t, port, "POST", fmt.Sprintf("/bank/accounts/%s/transfers%v", receiveAddr, queryStr), jsonStr) res, body = Request(t, port, "POST", fmt.Sprintf("/bank/accounts/%s/transfers", receiveAddr), jsonStr)
return return
} }
@ -958,7 +962,7 @@ func doRecoverKey(t *testing.T, port, recoverName, recoverPassword, seed string)
} }
func doSend(t *testing.T, port, seed, name, password string, addr sdk.AccAddress) (receiveAddr sdk.AccAddress, resultTx ctypes.ResultBroadcastTxCommit) { func doSend(t *testing.T, port, seed, name, password string, addr sdk.AccAddress) (receiveAddr sdk.AccAddress, resultTx ctypes.ResultBroadcastTxCommit) {
res, body, receiveAddr := doSendWithGas(t, port, seed, name, password, addr, "", 0, "") res, body, receiveAddr := doSendWithGas(t, port, seed, name, password, addr, "", 0, false, false)
require.Equal(t, http.StatusOK, res.StatusCode, body) require.Equal(t, http.StatusOK, res.StatusCode, body)
err := cdc.UnmarshalJSON([]byte(body), &resultTx) err := cdc.UnmarshalJSON([]byte(body), &resultTx)

View File

@ -4,7 +4,6 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/url"
"strconv" "strconv"
"strings" "strings"
@ -17,11 +16,6 @@ import (
authtxb "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder" authtxb "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder"
) )
const (
queryArgDryRun = "simulate"
queryArgGenerateOnly = "generate_only"
)
//---------------------------------------- //----------------------------------------
// Basic HTTP utilities // Basic HTTP utilities
@ -39,18 +33,6 @@ func WriteSimulationResponse(w http.ResponseWriter, gas uint64) {
w.Write([]byte(fmt.Sprintf(`{"gas_estimate":%v}`, gas))) w.Write([]byte(fmt.Sprintf(`{"gas_estimate":%v}`, gas)))
} }
// HasDryRunArg returns true if the request's URL query contains the dry run
// argument and its value is set to "true".
func HasDryRunArg(r *http.Request) bool {
return urlQueryHasArg(r.URL, queryArgDryRun)
}
// HasGenerateOnlyArg returns whether a URL's query "generate-only" parameter
// is set to "true".
func HasGenerateOnlyArg(r *http.Request) bool {
return urlQueryHasArg(r.URL, queryArgGenerateOnly)
}
// ParseInt64OrReturnBadRequest converts s to a int64 value. // ParseInt64OrReturnBadRequest converts s to a int64 value.
func ParseInt64OrReturnBadRequest(w http.ResponseWriter, s string) (n int64, ok bool) { func ParseInt64OrReturnBadRequest(w http.ResponseWriter, s string) (n int64, ok bool) {
var err error var err error
@ -113,8 +95,6 @@ func WriteGenerateStdTxResponse(w http.ResponseWriter, txBldr authtxb.TxBuilder,
return return
} }
func urlQueryHasArg(url *url.URL, arg string) bool { return url.Query().Get(arg) == "true" }
//---------------------------------------- //----------------------------------------
// Building / Sending utilities // Building / Sending utilities
@ -128,6 +108,8 @@ type BaseReq struct {
Sequence uint64 `json:"sequence"` Sequence uint64 `json:"sequence"`
Gas string `json:"gas"` Gas string `json:"gas"`
GasAdjustment string `json:"gas_adjustment"` GasAdjustment string `json:"gas_adjustment"`
GenerateOnly bool `json:"generate_only"`
Simulate bool `json:"simulate"`
} }
// Sanitize performs basic sanitization on a BaseReq object. // Sanitize performs basic sanitization on a BaseReq object.
@ -140,6 +122,8 @@ func (br BaseReq) Sanitize() BaseReq {
GasAdjustment: strings.TrimSpace(br.GasAdjustment), GasAdjustment: strings.TrimSpace(br.GasAdjustment),
AccountNumber: br.AccountNumber, AccountNumber: br.AccountNumber,
Sequence: br.Sequence, Sequence: br.Sequence,
GenerateOnly: br.GenerateOnly,
Simulate: br.Simulate,
} }
} }
@ -223,14 +207,14 @@ func CompleteAndBroadcastTxREST(w http.ResponseWriter, r *http.Request, cliCtx c
Sequence: baseReq.Sequence, Sequence: baseReq.Sequence,
} }
if HasDryRunArg(r) || txBldr.SimulateGas { if baseReq.Simulate || txBldr.SimulateGas {
newBldr, err := EnrichCtxWithGas(txBldr, cliCtx, baseReq.Name, msgs) newBldr, err := EnrichCtxWithGas(txBldr, cliCtx, baseReq.Name, msgs)
if err != nil { if err != nil {
WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return return
} }
if HasDryRunArg(r) { if baseReq.Simulate {
WriteSimulationResponse(w, newBldr.Gas) WriteSimulationResponse(w, newBldr.Gas)
return return
} }
@ -238,7 +222,7 @@ func CompleteAndBroadcastTxREST(w http.ResponseWriter, r *http.Request, cliCtx c
txBldr = newBldr txBldr = newBldr
} }
if HasGenerateOnlyArg(r) { if baseReq.GenerateOnly {
WriteGenerateStdTxResponse(w, txBldr, msgs) WriteGenerateStdTxResponse(w, txBldr, msgs)
return return
} }

View File

@ -219,14 +219,14 @@ func delegationsRequestHandlerFn(cdc *codec.Codec, kb keys.Keybase, cliCtx conte
baseReq.Sequence++ baseReq.Sequence++
if utils.HasDryRunArg(r) || txBldr.SimulateGas { if baseReq.Simulate || txBldr.SimulateGas {
newBldr, err := utils.EnrichCtxWithGas(txBldr, cliCtx, baseReq.Name, []sdk.Msg{msg}) newBldr, err := utils.EnrichCtxWithGas(txBldr, cliCtx, baseReq.Name, []sdk.Msg{msg})
if err != nil { if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return return
} }
if utils.HasDryRunArg(r) { if baseReq.Simulate {
utils.WriteSimulationResponse(w, newBldr.Gas) utils.WriteSimulationResponse(w, newBldr.Gas)
return return
} }
@ -234,7 +234,7 @@ func delegationsRequestHandlerFn(cdc *codec.Codec, kb keys.Keybase, cliCtx conte
txBldr = newBldr txBldr = newBldr
} }
if utils.HasGenerateOnlyArg(r) { if baseReq.GenerateOnly {
utils.WriteGenerateStdTxResponse(w, txBldr, []sdk.Msg{msg}) utils.WriteGenerateStdTxResponse(w, txBldr, []sdk.Msg{msg})
return return
} }