Skip to content

Commit

Permalink
Handle generate_only query parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
alessio committed Aug 28, 2018
1 parent efba64c commit 1144625
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 5 deletions.
31 changes: 26 additions & 5 deletions client/lcd/lcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,12 +265,29 @@ func TestCoinSend(t *testing.T) {
require.Equal(t, int64(1), mycoins.Amount.Int64())

// test failure with too little gas
res, body, _ = doSendWithGas(t, port, seed, name, password, addr, 100)
res, body, _ = doSendWithGas(t, port, seed, name, password, addr, 100, false)
require.Equal(t, http.StatusInternalServerError, res.StatusCode, body)

// test success with just enough gas
res, body, _ = doSendWithGas(t, port, seed, name, password, addr, 3000)
res, body, _ = doSendWithGas(t, port, seed, name, password, addr, 3000, false)
}

func TestCoinSendGenerateOnly(t *testing.T) {
name, password := "test", "1234567890"
addr, seed := CreateAddr(t, "test", password, GetKeyBase(t))
cleanup, _, port := InitializeTestLCD(t, 1, []sdk.AccAddress{addr})
defer cleanup()
// create TX
res, body, _ := doSendWithGas(t, port, seed, name, password, addr, 0, true)
t.Log(body)
//tests.WaitForHeight(resultTx.Height+1, port)
require.Equal(t, http.StatusOK, res.StatusCode, body)
var msg auth.StdSignMsg
require.Nil(t, cdc.UnmarshalJSON([]byte(body), &msg))
require.Equal(t, msg.Sequence, int64(0))
require.Equal(t, len(msg.Msgs), 1)
require.Equal(t, msg.Msgs[0].Type(), "bank")
require.Equal(t, msg.Msgs[0].GetSigners(), []sdk.AccAddress{addr})
}

func TestIBCTransfer(t *testing.T) {
Expand Down Expand Up @@ -720,7 +737,7 @@ func getAccount(t *testing.T, port string, addr sdk.AccAddress) auth.Account {
return acc
}

func doSendWithGas(t *testing.T, port, seed, name, password string, addr sdk.AccAddress, gas int64) (res *http.Response, body string, receiveAddr sdk.AccAddress) {
func doSendWithGas(t *testing.T, port, seed, name, password string, addr sdk.AccAddress, gas int64, generateOnly bool) (res *http.Response, body string, receiveAddr sdk.AccAddress) {

// create receive address
kb := client.MockKeyBase()
Expand Down Expand Up @@ -754,12 +771,16 @@ func doSendWithGas(t *testing.T, port, seed, name, password string, addr sdk.Acc
"chain_id":"%s"
}`, gasStr, name, password, accnum, sequence, coinbz, chainID))

res, body = Request(t, port, "POST", fmt.Sprintf("/accounts/%s/send", receiveAddr), jsonStr)
url := fmt.Sprintf("/accounts/%s/send", receiveAddr)
if generateOnly {
url += "?generate-only=true"
}
res, body = Request(t, port, "POST", url, jsonStr)
return
}

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)
require.Equal(t, http.StatusOK, res.StatusCode, body)

err := cdc.UnmarshalJSON([]byte(body), &resultTx)
Expand Down
21 changes: 21 additions & 0 deletions client/utils/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ package utils

import (
"net/http"
"net/url"

"github.com/cosmos/cosmos-sdk/client"
sdk "github.com/cosmos/cosmos-sdk/types"
authctx "github.com/cosmos/cosmos-sdk/x/auth/client/context"
)

// WriteErrorResponse prepares and writes a HTTP error
Expand All @@ -10,3 +15,19 @@ func WriteErrorResponse(w *http.ResponseWriter, status int, msg string) {
(*w).WriteHeader(status)
(*w).Write([]byte(msg))
}

// GenerateAndMarshallStdSignMsgJSON builds a StdSignMsg for the given
// messages and returns its JSON representation.
func GenerateAndMarshallStdSignMsgJSON(txCtx authctx.TxContext, msgs []sdk.Msg) (output []byte, err error) {
stdMsg, err := txCtx.Build(msgs)
if err != nil {
return
}
return txCtx.Codec.MarshalJSON(stdMsg)
}

// HasGenerateOnlyArg returns whether a URL's query "generate-only" parameter is set to "true".
func HasGenerateOnlyArg(url *url.URL) bool {
value := url.Query().Get(client.FlagGenerateOnly)
return value == "true"
}
10 changes: 10 additions & 0 deletions x/bank/client/rest/sendtx.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ func SendRequestHandlerFn(cdc *wire.Codec, kb keys.Keybase, cliCtx context.CLICo
Sequence: m.Sequence,
}

if utils.HasGenerateOnlyArg(r.URL) {
output, err := utils.GenerateAndMarshallStdSignMsgJSON(txCtx, []sdk.Msg{msg})
if err != nil {
utils.WriteErrorResponse(&w, http.StatusInternalServerError, err.Error())
return
}
w.Write(output)
return
}

if m.Gas == 0 {
newCtx, err := utils.EnrichCtxWithGas(txCtx, cliCtx, m.LocalAccountName, m.Password, []sdk.Msg{msg})
if err != nil {
Expand Down

0 comments on commit 1144625

Please sign in to comment.