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
+7 -23
View File
@@ -4,7 +4,6 @@ import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
@@ -17,11 +16,6 @@ import (
authtxb "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder"
)
const (
queryArgDryRun = "simulate"
queryArgGenerateOnly = "generate_only"
)
//----------------------------------------
// Basic HTTP utilities
@@ -39,18 +33,6 @@ func WriteSimulationResponse(w http.ResponseWriter, gas uint64) {
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.
func ParseInt64OrReturnBadRequest(w http.ResponseWriter, s string) (n int64, ok bool) {
var err error
@@ -113,8 +95,6 @@ func WriteGenerateStdTxResponse(w http.ResponseWriter, txBldr authtxb.TxBuilder,
return
}
func urlQueryHasArg(url *url.URL, arg string) bool { return url.Query().Get(arg) == "true" }
//----------------------------------------
// Building / Sending utilities
@@ -128,6 +108,8 @@ type BaseReq struct {
Sequence uint64 `json:"sequence"`
Gas string `json:"gas"`
GasAdjustment string `json:"gas_adjustment"`
GenerateOnly bool `json:"generate_only"`
Simulate bool `json:"simulate"`
}
// Sanitize performs basic sanitization on a BaseReq object.
@@ -140,6 +122,8 @@ func (br BaseReq) Sanitize() BaseReq {
GasAdjustment: strings.TrimSpace(br.GasAdjustment),
AccountNumber: br.AccountNumber,
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,
}
if HasDryRunArg(r) || txBldr.SimulateGas {
if baseReq.Simulate || txBldr.SimulateGas {
newBldr, err := EnrichCtxWithGas(txBldr, cliCtx, baseReq.Name, msgs)
if err != nil {
WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
if HasDryRunArg(r) {
if baseReq.Simulate {
WriteSimulationResponse(w, newBldr.Gas)
return
}
@@ -238,7 +222,7 @@ func CompleteAndBroadcastTxREST(w http.ResponseWriter, r *http.Request, cliCtx c
txBldr = newBldr
}
if HasGenerateOnlyArg(r) {
if baseReq.GenerateOnly {
WriteGenerateStdTxResponse(w, txBldr, msgs)
return
}