client/utils/ reorganization (#3487)

This commit is contained in:
Alessio Treglia 2019-02-04 07:48:26 -08:00 committed by Jack Zampolin
parent 1ae7cee4bf
commit c04c69648e
24 changed files with 404 additions and 377 deletions

View File

@ -17,6 +17,7 @@ BREAKING CHANGES
* Gaia
* SDK
* [\#3487](https://github.com/cosmos/cosmos-sdk/pull/3487) Move HTTP/REST utilities out of client/utils into a new dedicated client/rest package.
* Tendermint

View File

@ -14,13 +14,13 @@ import (
ctypes "github.com/tendermint/tendermint/rpc/core/types"
client "github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/rest"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/client/utils"
"github.com/cosmos/cosmos-sdk/cmd/gaia/app"
"github.com/cosmos/cosmos-sdk/crypto/keys/mintkey"
tests "github.com/cosmos/cosmos-sdk/tests"
"github.com/cosmos/cosmos-sdk/tests"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/version"
"github.com/cosmos/cosmos-sdk/x/auth"
@ -212,7 +212,7 @@ func TestCoinSend(t *testing.T) {
res, body, _ = doTransferWithGas(t, port, seed, name1, memo, pw, addr, "10000", 1.0, true, false, fees)
require.Equal(t, http.StatusOK, res.StatusCode, body)
var gasEstResp utils.GasEstimateResponse
var gasEstResp rest.GasEstimateResponse
require.Nil(t, cdc.UnmarshalJSON([]byte(body), &gasEstResp))
require.NotZero(t, gasEstResp.GasEstimate)
@ -288,7 +288,7 @@ func TestCoinSendGenerateSignAndBroadcast(t *testing.T) {
res, body, _ := doTransferWithGas(t, port, seed, name1, memo, "", addr, client.GasFlagAuto, 1, true, false, fees)
require.Equal(t, http.StatusOK, res.StatusCode, body)
var gasEstResp utils.GasEstimateResponse
var gasEstResp rest.GasEstimateResponse
require.Nil(t, cdc.UnmarshalJSON([]byte(body), &gasEstResp))
require.NotZero(t, gasEstResp.GasEstimate)
@ -315,7 +315,7 @@ func TestCoinSendGenerateSignAndBroadcast(t *testing.T) {
payload := authrest.SignBody{
Tx: msg,
BaseReq: utils.NewBaseReq(
BaseReq: rest.NewBaseReq(
name1, pw, "", viper.GetString(client.FlagChainID), "", "",
accnum, sequence, nil, nil, false, false,
),

View File

@ -26,9 +26,9 @@ import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/keys"
"github.com/cosmos/cosmos-sdk/client/rest"
"github.com/cosmos/cosmos-sdk/client/rpc"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/client/utils"
gapp "github.com/cosmos/cosmos-sdk/cmd/gaia/app"
"github.com/cosmos/cosmos-sdk/codec"
crkeys "github.com/cosmos/cosmos-sdk/crypto/keys"
@ -646,7 +646,7 @@ func doSign(t *testing.T, port, name, password, chainID string, accnum, sequence
var signedMsg auth.StdTx
payload := authrest.SignBody{
Tx: msg,
BaseReq: utils.NewBaseReq(
BaseReq: rest.NewBaseReq(
name, password, "", chainID, "", "", accnum, sequence, nil, nil, false, false,
),
}
@ -712,7 +712,7 @@ func doTransferWithGas(
from = addr.String()
}
baseReq := utils.NewBaseReq(
baseReq := rest.NewBaseReq(
from, password, memo, chainID, gas,
fmt.Sprintf("%f", gasAdjustment), accnum, sequence, fees, nil,
generateOnly, simulate,
@ -746,7 +746,7 @@ func doTransferWithGasAccAuto(
receiveAddr = sdk.AccAddress(receiveInfo.GetPubKey().Address())
chainID := viper.GetString(client.FlagChainID)
baseReq := utils.NewBaseReq(
baseReq := rest.NewBaseReq(
from, password, memo, chainID, gas,
fmt.Sprintf("%f", gasAdjustment), 0, 0, fees, nil, generateOnly, simulate,
)
@ -764,8 +764,8 @@ func doTransferWithGasAccAuto(
}
type sendReq struct {
Amount sdk.Coins `json:"amount"`
BaseReq utils.BaseReq `json:"base_req"`
Amount sdk.Coins `json:"amount"`
BaseReq rest.BaseReq `json:"base_req"`
}
// ----------------------------------------------------------------------
@ -779,7 +779,7 @@ func doDelegate(t *testing.T, port, name, password string,
accnum := acc.GetAccountNumber()
sequence := acc.GetSequence()
chainID := viper.GetString(client.FlagChainID)
baseReq := utils.NewBaseReq(name, password, "", chainID, "", "", accnum, sequence, fees, nil, false, false)
baseReq := rest.NewBaseReq(name, password, "", chainID, "", "", accnum, sequence, fees, nil, false, false)
msg := msgDelegationsInput{
BaseReq: baseReq,
DelegatorAddr: delAddr,
@ -799,7 +799,7 @@ func doDelegate(t *testing.T, port, name, password string,
}
type msgDelegationsInput struct {
BaseReq utils.BaseReq `json:"base_req"`
BaseReq rest.BaseReq `json:"base_req"`
DelegatorAddr sdk.AccAddress `json:"delegator_addr"` // in bech32
ValidatorAddr sdk.ValAddress `json:"validator_addr"` // in bech32
Delegation sdk.Coin `json:"delegation"`
@ -813,7 +813,7 @@ func doUndelegate(t *testing.T, port, name, password string,
accnum := acc.GetAccountNumber()
sequence := acc.GetSequence()
chainID := viper.GetString(client.FlagChainID)
baseReq := utils.NewBaseReq(name, password, "", chainID, "", "", accnum, sequence, fees, nil, false, false)
baseReq := rest.NewBaseReq(name, password, "", chainID, "", "", accnum, sequence, fees, nil, false, false)
msg := msgUndelegateInput{
BaseReq: baseReq,
DelegatorAddr: delAddr,
@ -834,7 +834,7 @@ func doUndelegate(t *testing.T, port, name, password string,
}
type msgUndelegateInput struct {
BaseReq utils.BaseReq `json:"base_req"`
BaseReq rest.BaseReq `json:"base_req"`
DelegatorAddr sdk.AccAddress `json:"delegator_addr"` // in bech32
ValidatorAddr sdk.ValAddress `json:"validator_addr"` // in bech32
SharesAmount sdk.Dec `json:"shares"`
@ -849,7 +849,7 @@ func doBeginRedelegation(t *testing.T, port, name, password string,
sequence := acc.GetSequence()
chainID := viper.GetString(client.FlagChainID)
baseReq := utils.NewBaseReq(name, password, "", chainID, "", "", accnum, sequence, fees, nil, false, false)
baseReq := rest.NewBaseReq(name, password, "", chainID, "", "", accnum, sequence, fees, nil, false, false)
msg := msgBeginRedelegateInput{
BaseReq: baseReq,
@ -872,7 +872,7 @@ func doBeginRedelegation(t *testing.T, port, name, password string,
}
type msgBeginRedelegateInput struct {
BaseReq utils.BaseReq `json:"base_req"`
BaseReq rest.BaseReq `json:"base_req"`
DelegatorAddr sdk.AccAddress `json:"delegator_addr"` // in bech32
ValidatorSrcAddr sdk.ValAddress `json:"validator_src_addr"` // in bech32
ValidatorDstAddr sdk.ValAddress `json:"validator_dst_addr"` // in bech32
@ -1080,7 +1080,7 @@ func doSubmitProposal(t *testing.T, port, seed, name, password string, proposerA
accnum := acc.GetAccountNumber()
sequence := acc.GetSequence()
chainID := viper.GetString(client.FlagChainID)
baseReq := utils.NewBaseReq(name, password, "", chainID, "", "", accnum, sequence, fees, nil, false, false)
baseReq := rest.NewBaseReq(name, password, "", chainID, "", "", accnum, sequence, fees, nil, false, false)
pr := postProposalReq{
Title: "Test",
@ -1106,7 +1106,7 @@ func doSubmitProposal(t *testing.T, port, seed, name, password string, proposerA
}
type postProposalReq struct {
BaseReq utils.BaseReq `json:"base_req"`
BaseReq rest.BaseReq `json:"base_req"`
Title string `json:"title"` // Title of the proposal
Description string `json:"description"` // Description of the proposal
ProposalType string `json:"proposal_type"` // Type of proposal. Initial set {PlainTextProposal, SoftwareUpgradeProposal}
@ -1176,7 +1176,7 @@ func doDeposit(t *testing.T, port, seed, name, password string, proposerAddr sdk
accnum := acc.GetAccountNumber()
sequence := acc.GetSequence()
chainID := viper.GetString(client.FlagChainID)
baseReq := utils.NewBaseReq(name, password, "", chainID, "", "", accnum, sequence, fees, nil, false, false)
baseReq := rest.NewBaseReq(name, password, "", chainID, "", "", accnum, sequence, fees, nil, false, false)
dr := depositReq{
Depositor: proposerAddr,
@ -1198,7 +1198,7 @@ func doDeposit(t *testing.T, port, seed, name, password string, proposerAddr sdk
}
type depositReq struct {
BaseReq utils.BaseReq `json:"base_req"`
BaseReq rest.BaseReq `json:"base_req"`
Depositor sdk.AccAddress `json:"depositor"` // Address of the depositor
Amount sdk.Coins `json:"amount"` // Coins to add to the proposal's deposit
}
@ -1230,7 +1230,7 @@ func doVote(t *testing.T, port, seed, name, password string, proposerAddr sdk.Ac
accnum := acc.GetAccountNumber()
sequence := acc.GetSequence()
chainID := viper.GetString(client.FlagChainID)
baseReq := utils.NewBaseReq(name, password, "", chainID, "", "", accnum, sequence, fees, nil, false, false)
baseReq := rest.NewBaseReq(name, password, "", chainID, "", "", accnum, sequence, fees, nil, false, false)
vr := voteReq{
Voter: proposerAddr,
@ -1252,7 +1252,7 @@ func doVote(t *testing.T, port, seed, name, password string, proposerAddr sdk.Ac
}
type voteReq struct {
BaseReq utils.BaseReq `json:"base_req"`
BaseReq rest.BaseReq `json:"base_req"`
Voter sdk.AccAddress `json:"voter"` // address of the voter
Option string `json:"option"` // option from OptionSet chosen by the voter
}
@ -1362,7 +1362,7 @@ func getSigningInfo(t *testing.T, port string, validatorPubKey string) slashing.
func doUnjail(t *testing.T, port, seed, name, password string,
valAddr sdk.ValAddress, fees sdk.Coins) (resultTx ctypes.ResultBroadcastTxCommit) {
chainID := viper.GetString(client.FlagChainID)
baseReq := utils.NewBaseReq(name, password, "", chainID, "", "", 1, 1, fees, nil, false, false)
baseReq := rest.NewBaseReq(name, password, "", chainID, "", "", 1, 1, fees, nil, false, false)
ur := unjailReq{
BaseReq: baseReq,
@ -1380,5 +1380,5 @@ func doUnjail(t *testing.T, port, seed, name, password string,
}
type unjailReq struct {
BaseReq utils.BaseReq `json:"base_req"`
BaseReq rest.BaseReq `json:"base_req"`
}

View File

@ -1,14 +1,13 @@
package utils
package rest
import (
"fmt"
"io/ioutil"
"net/http"
"strconv"
"strings"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/utils"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/crypto/keys/keyerror"
sdk "github.com/cosmos/cosmos-sdk/types"
@ -16,11 +15,6 @@ import (
authtxb "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder"
)
// GasEstimateResponse defines a response definition for tx gas estimation.
type GasEstimateResponse struct {
GasEstimate uint64 `json:"gas_estimate"`
}
//-----------------------------------------------------------------------------
// Basic HTTP utilities
@ -105,116 +99,6 @@ func ParseFloat64OrReturnBadRequest(w http.ResponseWriter, s string, defaultIfEm
//-----------------------------------------------------------------------------
// Building / Sending utilities
// BaseReq defines a structure that can be embedded in other request structures
// that all share common "base" fields.
type BaseReq struct {
From string `json:"from"`
Password string `json:"password"`
Memo string `json:"memo"`
ChainID string `json:"chain_id"`
AccountNumber uint64 `json:"account_number"`
Sequence uint64 `json:"sequence"`
Fees sdk.Coins `json:"fees"`
GasPrices sdk.DecCoins `json:"gas_prices"`
Gas string `json:"gas"`
GasAdjustment string `json:"gas_adjustment"`
GenerateOnly bool `json:"generate_only"`
Simulate bool `json:"simulate"`
}
// NewBaseReq creates a new basic request instance and sanitizes its values
func NewBaseReq(
from, password, memo, chainID string, gas, gasAdjustment string,
accNumber, seq uint64, fees sdk.Coins, gasPrices sdk.DecCoins, genOnly, simulate bool,
) BaseReq {
return BaseReq{
From: strings.TrimSpace(from),
Password: password,
Memo: strings.TrimSpace(memo),
ChainID: strings.TrimSpace(chainID),
Fees: fees,
GasPrices: gasPrices,
Gas: strings.TrimSpace(gas),
GasAdjustment: strings.TrimSpace(gasAdjustment),
AccountNumber: accNumber,
Sequence: seq,
GenerateOnly: genOnly,
Simulate: simulate,
}
}
// Sanitize performs basic sanitization on a BaseReq object.
func (br BaseReq) Sanitize() BaseReq {
return NewBaseReq(
br.From, br.Password, br.Memo, br.ChainID, br.Gas, br.GasAdjustment,
br.AccountNumber, br.Sequence, br.Fees, br.GasPrices, br.GenerateOnly, br.Simulate,
)
}
// ValidateBasic performs basic validation of a BaseReq. If custom validation
// logic is needed, the implementing request handler should perform those
// checks manually.
func (br BaseReq) ValidateBasic(w http.ResponseWriter) bool {
if !br.GenerateOnly && !br.Simulate {
switch {
case len(br.Password) == 0:
WriteErrorResponse(w, http.StatusUnauthorized, "password required but not specified")
return false
case len(br.ChainID) == 0:
WriteErrorResponse(w, http.StatusUnauthorized, "chain-id required but not specified")
return false
case !br.Fees.IsZero() && !br.GasPrices.IsZero():
// both fees and gas prices were provided
WriteErrorResponse(w, http.StatusBadRequest, "cannot provide both fees and gas prices")
return false
case !br.Fees.IsValid() && !br.GasPrices.IsValid():
// neither fees or gas prices were provided
WriteErrorResponse(w, http.StatusPaymentRequired, "invalid fees or gas prices provided")
return false
}
}
if len(br.From) == 0 {
WriteErrorResponse(w, http.StatusUnauthorized, "name or address required but not specified")
return false
}
return true
}
/*
ReadRESTReq is a simple convenience wrapper that reads the body and
unmarshals to the req interface.
Usage:
type SomeReq struct {
BaseReq `json:"base_req"`
CustomField string `json:"custom_field"`
}
req := new(SomeReq)
err := ReadRESTReq(w, r, cdc, req)
*/
func ReadRESTReq(w http.ResponseWriter, r *http.Request, cdc *codec.Codec, req interface{}) error {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return err
}
err = cdc.UnmarshalJSON(body, req)
if err != nil {
WriteErrorResponse(w, http.StatusBadRequest, fmt.Sprintf("failed to decode JSON payload: %s", err))
return err
}
return nil
}
// CompleteAndBroadcastTxREST implements a utility function that facilitates
// sending a series of messages in a signed tx. In addition, it will handle
// tx gas simulation and estimation.
@ -245,12 +129,12 @@ func CompleteAndBroadcastTxREST(
cliCtx = cliCtx.WithFromName(fromName).WithFromAddress(fromAddress)
txBldr := authtxb.NewTxBuilder(
GetTxEncoder(cdc), baseReq.AccountNumber,
utils.GetTxEncoder(cdc), baseReq.AccountNumber,
baseReq.Sequence, gas, gasAdj, baseReq.Simulate,
baseReq.ChainID, baseReq.Memo, baseReq.Fees, baseReq.GasPrices,
)
txBldr, err = prepareTxBuilder(txBldr, cliCtx)
txBldr, err = utils.PrepareTxBuilder(txBldr, cliCtx)
if err != nil {
WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
@ -262,7 +146,7 @@ func CompleteAndBroadcastTxREST(
return
}
txBldr, err = EnrichWithGas(txBldr, cliCtx, msgs)
txBldr, err = utils.EnrichWithGas(txBldr, cliCtx, msgs)
if err != nil {
WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
@ -336,7 +220,7 @@ func WriteGenerateStdTxResponse(
}
txBldr := authtxb.NewTxBuilder(
GetTxEncoder(cdc), br.AccountNumber, br.Sequence, gas, gasAdj,
utils.GetTxEncoder(cdc), br.AccountNumber, br.Sequence, gas, gasAdj,
br.Simulate, br.ChainID, br.Memo, br.Fees, br.GasPrices,
)
@ -346,7 +230,7 @@ func WriteGenerateStdTxResponse(
return
}
txBldr, err = EnrichWithGas(txBldr, cliCtx, msgs)
txBldr, err = utils.EnrichWithGas(txBldr, cliCtx, msgs)
if err != nil {
WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return

126
client/rest/types.go Normal file
View File

@ -0,0 +1,126 @@
package rest
import (
"fmt"
"io/ioutil"
"net/http"
"strings"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// GasEstimateResponse defines a response definition for tx gas estimation.
type GasEstimateResponse struct {
GasEstimate uint64 `json:"gas_estimate"`
}
// BaseReq defines a structure that can be embedded in other request structures
// that all share common "base" fields.
type BaseReq struct {
From string `json:"from"`
Password string `json:"password"`
Memo string `json:"memo"`
ChainID string `json:"chain_id"`
AccountNumber uint64 `json:"account_number"`
Sequence uint64 `json:"sequence"`
Fees sdk.Coins `json:"fees"`
GasPrices sdk.DecCoins `json:"gas_prices"`
Gas string `json:"gas"`
GasAdjustment string `json:"gas_adjustment"`
GenerateOnly bool `json:"generate_only"`
Simulate bool `json:"simulate"`
}
// NewBaseReq creates a new basic request instance and sanitizes its values
func NewBaseReq(
from, password, memo, chainID string, gas, gasAdjustment string,
accNumber, seq uint64, fees sdk.Coins, gasPrices sdk.DecCoins, genOnly, simulate bool,
) BaseReq {
return BaseReq{
From: strings.TrimSpace(from),
Password: password,
Memo: strings.TrimSpace(memo),
ChainID: strings.TrimSpace(chainID),
Fees: fees,
GasPrices: gasPrices,
Gas: strings.TrimSpace(gas),
GasAdjustment: strings.TrimSpace(gasAdjustment),
AccountNumber: accNumber,
Sequence: seq,
GenerateOnly: genOnly,
Simulate: simulate,
}
}
// Sanitize performs basic sanitization on a BaseReq object.
func (br BaseReq) Sanitize() BaseReq {
return NewBaseReq(
br.From, br.Password, br.Memo, br.ChainID, br.Gas, br.GasAdjustment,
br.AccountNumber, br.Sequence, br.Fees, br.GasPrices, br.GenerateOnly, br.Simulate,
)
}
// ValidateBasic performs basic validation of a BaseReq. If custom validation
// logic is needed, the implementing request handler should perform those
// checks manually.
func (br BaseReq) ValidateBasic(w http.ResponseWriter) bool {
if !br.GenerateOnly && !br.Simulate {
switch {
case len(br.Password) == 0:
WriteErrorResponse(w, http.StatusUnauthorized, "password required but not specified")
return false
case len(br.ChainID) == 0:
WriteErrorResponse(w, http.StatusUnauthorized, "chain-id required but not specified")
return false
case !br.Fees.IsZero() && !br.GasPrices.IsZero():
// both fees and gas prices were provided
WriteErrorResponse(w, http.StatusBadRequest, "cannot provide both fees and gas prices")
return false
case !br.Fees.IsValid() && !br.GasPrices.IsValid():
// neither fees or gas prices were provided
WriteErrorResponse(w, http.StatusPaymentRequired, "invalid fees or gas prices provided")
return false
}
}
if len(br.From) == 0 {
WriteErrorResponse(w, http.StatusUnauthorized, "name or address required but not specified")
return false
}
return true
}
/*
ReadRESTReq is a simple convenience wrapper that reads the body and
unmarshals to the req interface.
Usage:
type SomeReq struct {
BaseReq `json:"base_req"`
CustomField string `json:"custom_field"`
}
req := new(SomeReq)
err := ReadRESTReq(w, r, cdc, req)
*/
func ReadRESTReq(w http.ResponseWriter, r *http.Request, cdc *codec.Codec, req interface{}) error {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return err
}
err = cdc.UnmarshalJSON(body, req)
if err != nil {
WriteErrorResponse(w, http.StatusBadRequest, fmt.Sprintf("failed to decode JSON payload: %s", err))
return err
}
return nil
}

View File

@ -5,6 +5,8 @@ import (
"net/http"
"strconv"
"github.com/cosmos/cosmos-sdk/client/rest"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/context"
@ -12,8 +14,6 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
tmliteProxy "github.com/tendermint/tendermint/lite/proxy"
"github.com/cosmos/cosmos-sdk/client/utils"
)
//BlockCommand returns the verified block data for a given heights
@ -131,7 +131,7 @@ func BlockRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
w.Write([]byte(err.Error()))
return
}
utils.PostProcessResponse(w, cdc, output, cliCtx.Indent)
rest.PostProcessResponse(w, cdc, output, cliCtx.Indent)
}
}
@ -150,6 +150,6 @@ func LatestBlockRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
w.Write([]byte(err.Error()))
return
}
utils.PostProcessResponse(w, cdc, output, cliCtx.Indent)
rest.PostProcessResponse(w, cdc, output, cliCtx.Indent)
}
}

View File

@ -4,10 +4,11 @@ import (
"fmt"
"net/http"
"github.com/cosmos/cosmos-sdk/client/rest"
"github.com/gorilla/mux"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/utils"
"github.com/cosmos/cosmos-sdk/version"
)
@ -34,7 +35,7 @@ func NodeVersionRequestHandler(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
version, err := cliCtx.Query("/app/version", nil)
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

View File

@ -5,6 +5,8 @@ import (
"net/http"
"strconv"
"github.com/cosmos/cosmos-sdk/client/rest"
"github.com/spf13/cobra"
"github.com/spf13/viper"
@ -12,7 +14,6 @@ import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/utils"
)
// StatusCommand returns the status of the network
@ -77,7 +78,7 @@ func NodeInfoRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
}
nodeInfo := status.NodeInfo
utils.PostProcessResponse(w, cdc, nodeInfo, cliCtx.Indent)
rest.PostProcessResponse(w, cdc, nodeInfo, cliCtx.Indent)
}
}

View File

@ -6,6 +6,8 @@ import (
"net/http"
"strconv"
"github.com/cosmos/cosmos-sdk/client/rest"
"github.com/gorilla/mux"
"github.com/spf13/cobra"
@ -14,7 +16,6 @@ import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/utils"
sdk "github.com/cosmos/cosmos-sdk/types"
)
@ -157,7 +158,7 @@ func ValidatorSetRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
w.Write([]byte(err.Error()))
return
}
utils.PostProcessResponse(w, cdc, output, cliCtx.Indent)
rest.PostProcessResponse(w, cdc, output, cliCtx.Indent)
}
}
@ -177,6 +178,6 @@ func LatestValidatorSetRequestHandlerFn(cliCtx context.CLIContext) http.HandlerF
w.Write([]byte(err.Error()))
return
}
utils.PostProcessResponse(w, cdc, output, cliCtx.Indent)
rest.PostProcessResponse(w, cdc, output, cliCtx.Indent)
}
}

View File

@ -3,10 +3,11 @@ package tx
import (
"net/http"
"github.com/cosmos/cosmos-sdk/client/rest"
"io/ioutil"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/utils"
"github.com/cosmos/cosmos-sdk/codec"
)
@ -32,12 +33,12 @@ func BroadcastTxRequest(cliCtx context.CLIContext, cdc *codec.Codec) http.Handle
var m BroadcastBody
body, err := ioutil.ReadAll(r.Body)
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
err = cdc.UnmarshalJSON(body, &m)
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
var res interface{}
@ -49,13 +50,13 @@ func BroadcastTxRequest(cliCtx context.CLIContext, cdc *codec.Codec) http.Handle
case flagAsync:
res, err = cliCtx.BroadcastTxAsync(m.TxBytes)
default:
utils.WriteErrorResponse(w, http.StatusInternalServerError, "unsupported return type. supported types: block, sync, async")
rest.WriteErrorResponse(w, http.StatusInternalServerError, "unsupported return type. supported types: block, sync, async")
return
}
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
utils.PostProcessResponse(w, cdc, res, cliCtx.Indent)
rest.PostProcessResponse(w, cdc, res, cliCtx.Indent)
}
}

View File

@ -5,6 +5,8 @@ import (
"fmt"
"net/http"
"github.com/cosmos/cosmos-sdk/client/rest"
"github.com/tendermint/tendermint/libs/common"
"github.com/gorilla/mux"
@ -16,7 +18,6 @@ import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/utils"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
@ -142,9 +143,9 @@ func QueryTxRequestHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.H
output, err := queryTx(cdc, cliCtx, hashHexStr)
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
utils.PostProcessResponse(w, cdc, output, cliCtx.Indent)
rest.PostProcessResponse(w, cdc, output, cliCtx.Indent)
}
}

View File

@ -8,9 +8,10 @@ import (
"strconv"
"strings"
"github.com/cosmos/cosmos-sdk/client/rest"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/utils"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
@ -175,28 +176,28 @@ func SearchTxRequestHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.
var txs []Info
err := r.ParseForm()
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, sdk.AppendMsgToErr("could not parse query parameters", err.Error()))
rest.WriteErrorResponse(w, http.StatusBadRequest, sdk.AppendMsgToErr("could not parse query parameters", err.Error()))
return
}
if len(r.Form) == 0 {
utils.PostProcessResponse(w, cdc, txs, cliCtx.Indent)
rest.PostProcessResponse(w, cdc, txs, cliCtx.Indent)
return
}
tags, page, limit, err = parseHTTPArgs(r)
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
txs, err = SearchTxs(cliCtx, cdc, tags, page, limit)
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
utils.PostProcessResponse(w, cdc, txs, cliCtx.Indent)
rest.PostProcessResponse(w, cdc, txs, cliCtx.Indent)
}
}

View File

@ -27,7 +27,7 @@ import (
//
// NOTE: Also see CompleteAndBroadcastTxREST.
func CompleteAndBroadcastTxCLI(txBldr authtxb.TxBuilder, cliCtx context.CLIContext, msgs []sdk.Msg) error {
txBldr, err := prepareTxBuilder(txBldr, cliCtx)
txBldr, err := PrepareTxBuilder(txBldr, cliCtx)
if err != nil {
return err
}
@ -227,7 +227,8 @@ func parseQueryResponse(cdc *amino.Codec, rawRes []byte) (uint64, error) {
return simulationResult.GasUsed, nil
}
func prepareTxBuilder(txBldr authtxb.TxBuilder, cliCtx context.CLIContext) (authtxb.TxBuilder, error) {
// PrepareTxBuilder populates a TxBuilder in preparation for the build of a Tx.
func PrepareTxBuilder(txBldr authtxb.TxBuilder, cliCtx context.CLIContext) (authtxb.TxBuilder, error) {
if err := cliCtx.EnsureAccountExists(); err != nil {
return txBldr, err
}
@ -259,7 +260,7 @@ func prepareTxBuilder(txBldr authtxb.TxBuilder, cliCtx context.CLIContext) (auth
// buildUnsignedStdTx builds a StdTx as per the parameters passed in the
// contexts. Gas is automatically estimated if gas wanted is set to 0.
func buildUnsignedStdTx(txBldr authtxb.TxBuilder, cliCtx context.CLIContext, msgs []sdk.Msg) (stdTx auth.StdTx, err error) {
txBldr, err = prepareTxBuilder(txBldr, cliCtx)
txBldr, err = PrepareTxBuilder(txBldr, cliCtx)
if err != nil {
return
}

View File

@ -4,7 +4,7 @@ import (
"net/http"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/utils"
"github.com/cosmos/cosmos-sdk/client/rest"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
@ -39,13 +39,13 @@ func QueryAccountRequestHandlerFn(
addr, err := sdk.AccAddressFromBech32(bech32addr)
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
res, err := cliCtx.QueryStore(auth.AddressStoreKey(addr), storeName)
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
@ -58,11 +58,11 @@ func QueryAccountRequestHandlerFn(
// decode the value
account, err := decoder(res)
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
utils.PostProcessResponse(w, cdc, account, cliCtx.Indent)
rest.PostProcessResponse(w, cdc, account, cliCtx.Indent)
}
}
@ -78,13 +78,13 @@ func QueryBalancesRequestHandlerFn(
addr, err := sdk.AccAddressFromBech32(bech32addr)
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
res, err := cliCtx.QueryStore(auth.AddressStoreKey(addr), storeName)
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
@ -97,10 +97,10 @@ func QueryBalancesRequestHandlerFn(
// decode the value
account, err := decoder(res)
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
utils.PostProcessResponse(w, cdc, account.GetCoins(), cliCtx.Indent)
rest.PostProcessResponse(w, cdc, account.GetCoins(), cliCtx.Indent)
}
}

View File

@ -4,6 +4,7 @@ import (
"net/http"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/rest"
"github.com/cosmos/cosmos-sdk/client/utils"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/crypto/keys/keyerror"
@ -14,9 +15,9 @@ import (
// SignBody defines the properties of a sign request's body.
type SignBody struct {
Tx auth.StdTx `json:"tx"`
AppendSig bool `json:"append_sig"`
BaseReq utils.BaseReq `json:"base_req"`
Tx auth.StdTx `json:"tx"`
AppendSig bool `json:"append_sig"`
BaseReq rest.BaseReq `json:"base_req"`
}
// nolint: unparam
@ -25,8 +26,8 @@ func SignTxRequestHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.Ha
return func(w http.ResponseWriter, r *http.Request) {
var m SignBody
if err := utils.ReadRESTReq(w, r, cdc, &m); err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
if err := rest.ReadRESTReq(w, r, cdc, &m); err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
@ -37,14 +38,14 @@ func SignTxRequestHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.Ha
// validate tx
// discard error if it's CodeNoSignatures as the tx comes with no signatures
if err := m.Tx.ValidateBasic(); err != nil && err.Code() != sdk.CodeNoSignatures {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
// derive the from account address and name from the Keybase
fromAddress, fromName, err := context.GetFromFields(m.BaseReq.From)
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
@ -65,16 +66,16 @@ func SignTxRequestHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.Ha
signedTx, err := txBldr.SignStdTx(cliCtx.GetFromName(), m.BaseReq.Password, m.Tx, m.AppendSig)
if keyerror.IsErrKeyNotFound(err) {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
} else if keyerror.IsErrWrongPassword(err) {
utils.WriteErrorResponse(w, http.StatusUnauthorized, err.Error())
rest.WriteErrorResponse(w, http.StatusUnauthorized, err.Error())
return
} else if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
utils.PostProcessResponse(w, cdc, signedTx, cliCtx.Indent)
rest.PostProcessResponse(w, cdc, signedTx, cliCtx.Indent)
}
}

View File

@ -5,7 +5,7 @@ import (
"net/http"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/utils"
"github.com/cosmos/cosmos-sdk/client/rest"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/x/auth"
)
@ -24,29 +24,29 @@ func BroadcastTxRequestHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) ht
txBytes, err := cliCtx.Codec.MarshalBinaryLengthPrefixed(m.Tx)
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
res, err := cliCtx.BroadcastTx(txBytes)
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
utils.PostProcessResponse(w, cdc, res, cliCtx.Indent)
rest.PostProcessResponse(w, cdc, res, cliCtx.Indent)
}
}
func unmarshalBodyOrReturnBadRequest(cliCtx context.CLIContext, w http.ResponseWriter, r *http.Request, m *broadcastBody) bool {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return false
}
err = cliCtx.Codec.UnmarshalJSON(body, m)
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return false
}
return true

View File

@ -3,8 +3,9 @@ package rest
import (
"net/http"
"github.com/cosmos/cosmos-sdk/client/rest"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/utils"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/crypto/keys"
sdk "github.com/cosmos/cosmos-sdk/types"
@ -21,8 +22,8 @@ func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *codec.Codec,
}
type sendReq struct {
BaseReq utils.BaseReq `json:"base_req"`
Amount sdk.Coins `json:"amount"`
BaseReq rest.BaseReq `json:"base_req"`
Amount sdk.Coins `json:"amount"`
}
var msgCdc = codec.New()
@ -39,12 +40,12 @@ func SendRequestHandlerFn(cdc *codec.Codec, kb keys.Keybase, cliCtx context.CLIC
toAddr, err := sdk.AccAddressFromBech32(bech32Addr)
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
var req sendReq
err = utils.ReadRESTReq(w, r, cdc, &req)
err = rest.ReadRESTReq(w, r, cdc, &req)
if err != nil {
return
}
@ -59,25 +60,25 @@ func SendRequestHandlerFn(cdc *codec.Codec, kb keys.Keybase, cliCtx context.CLIC
// address.
fromAddr, err := sdk.AccAddressFromBech32(req.BaseReq.From)
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
msg := bankclient.CreateMsg(fromAddr, toAddr, req.Amount)
utils.WriteGenerateStdTxResponse(w, cdc, cliCtx, req.BaseReq, []sdk.Msg{msg})
rest.WriteGenerateStdTxResponse(w, cdc, cliCtx, req.BaseReq, []sdk.Msg{msg})
return
}
// derive the from account address and name from the Keybase
fromAddress, fromName, err := context.GetFromFields(req.BaseReq.From)
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
cliCtx = cliCtx.WithFromName(fromName).WithFromAddress(fromAddress)
msg := bankclient.CreateMsg(cliCtx.GetFromAddress(), toAddr, req.Amount)
utils.CompleteAndBroadcastTxREST(w, r, cliCtx, req.BaseReq, []sdk.Msg{msg}, cdc)
rest.CompleteAndBroadcastTxREST(w, r, cliCtx, req.BaseReq, []sdk.Msg{msg}, cdc)
}
}

View File

@ -4,8 +4,9 @@ import (
"fmt"
"net/http"
"github.com/cosmos/cosmos-sdk/client/rest"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/utils"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/gov"
@ -53,7 +54,7 @@ func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *codec.Codec)
}
type postProposalReq struct {
BaseReq utils.BaseReq `json:"base_req"`
BaseReq rest.BaseReq `json:"base_req"`
Title string `json:"title"` // Title of the proposal
Description string `json:"description"` // Description of the proposal
ProposalType string `json:"proposal_type"` // Type of proposal. Initial set {PlainTextProposal, SoftwareUpgradeProposal}
@ -62,13 +63,13 @@ type postProposalReq struct {
}
type depositReq struct {
BaseReq utils.BaseReq `json:"base_req"`
BaseReq rest.BaseReq `json:"base_req"`
Depositor sdk.AccAddress `json:"depositor"` // Address of the depositor
Amount sdk.Coins `json:"amount"` // Coins to add to the proposal's deposit
}
type voteReq struct {
BaseReq utils.BaseReq `json:"base_req"`
BaseReq rest.BaseReq `json:"base_req"`
Voter sdk.AccAddress `json:"voter"` // address of the voter
Option string `json:"option"` // option from OptionSet chosen by the voter
}
@ -76,9 +77,9 @@ type voteReq struct {
func postProposalHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req postProposalReq
err := utils.ReadRESTReq(w, r, cdc, &req)
err := rest.ReadRESTReq(w, r, cdc, &req)
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
@ -89,7 +90,7 @@ func postProposalHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.Han
proposalType, err := gov.ProposalTypeFromString(govClientUtils.NormalizeProposalType(req.ProposalType))
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
@ -97,16 +98,16 @@ func postProposalHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.Han
msg := gov.NewMsgSubmitProposal(req.Title, req.Description, proposalType, req.Proposer, req.InitialDeposit)
err = msg.ValidateBasic()
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
if req.BaseReq.GenerateOnly {
utils.WriteGenerateStdTxResponse(w, cdc, cliCtx, req.BaseReq, []sdk.Msg{msg})
rest.WriteGenerateStdTxResponse(w, cdc, cliCtx, req.BaseReq, []sdk.Msg{msg})
return
}
utils.CompleteAndBroadcastTxREST(w, r, cliCtx, req.BaseReq, []sdk.Msg{msg}, cdc)
rest.CompleteAndBroadcastTxREST(w, r, cliCtx, req.BaseReq, []sdk.Msg{msg}, cdc)
}
}
@ -117,17 +118,17 @@ func depositHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerF
if len(strProposalID) == 0 {
err := errors.New("proposalId required but not specified")
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
proposalID, ok := utils.ParseUint64OrReturnBadRequest(w, strProposalID)
proposalID, ok := rest.ParseUint64OrReturnBadRequest(w, strProposalID)
if !ok {
return
}
var req depositReq
err := utils.ReadRESTReq(w, r, cdc, &req)
err := rest.ReadRESTReq(w, r, cdc, &req)
if err != nil {
return
}
@ -141,16 +142,16 @@ func depositHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerF
msg := gov.NewMsgDeposit(req.Depositor, proposalID, req.Amount)
err = msg.ValidateBasic()
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
if req.BaseReq.GenerateOnly {
utils.WriteGenerateStdTxResponse(w, cdc, cliCtx, req.BaseReq, []sdk.Msg{msg})
rest.WriteGenerateStdTxResponse(w, cdc, cliCtx, req.BaseReq, []sdk.Msg{msg})
return
}
utils.CompleteAndBroadcastTxREST(w, r, cliCtx, req.BaseReq, []sdk.Msg{msg}, cdc)
rest.CompleteAndBroadcastTxREST(w, r, cliCtx, req.BaseReq, []sdk.Msg{msg}, cdc)
}
}
@ -161,17 +162,17 @@ func voteHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc
if len(strProposalID) == 0 {
err := errors.New("proposalId required but not specified")
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
proposalID, ok := utils.ParseUint64OrReturnBadRequest(w, strProposalID)
proposalID, ok := rest.ParseUint64OrReturnBadRequest(w, strProposalID)
if !ok {
return
}
var req voteReq
err := utils.ReadRESTReq(w, r, cdc, &req)
err := rest.ReadRESTReq(w, r, cdc, &req)
if err != nil {
return
}
@ -183,7 +184,7 @@ func voteHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc
voteOption, err := gov.VoteOptionFromString(govClientUtils.NormalizeVoteOption(req.Option))
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
@ -191,16 +192,16 @@ func voteHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc
msg := gov.NewMsgVote(req.Voter, proposalID, voteOption)
err = msg.ValidateBasic()
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
if req.BaseReq.GenerateOnly {
utils.WriteGenerateStdTxResponse(w, cdc, cliCtx, req.BaseReq, []sdk.Msg{msg})
rest.WriteGenerateStdTxResponse(w, cdc, cliCtx, req.BaseReq, []sdk.Msg{msg})
return
}
utils.CompleteAndBroadcastTxREST(w, r, cliCtx, req.BaseReq, []sdk.Msg{msg}, cdc)
rest.CompleteAndBroadcastTxREST(w, r, cliCtx, req.BaseReq, []sdk.Msg{msg}, cdc)
}
}
@ -211,11 +212,11 @@ func queryParamsHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.Hand
res, err := cliCtx.QueryWithData(fmt.Sprintf("custom/gov/%s/%s", gov.QueryParams, paramType), nil)
if err != nil {
utils.WriteErrorResponse(w, http.StatusNotFound, err.Error())
rest.WriteErrorResponse(w, http.StatusNotFound, err.Error())
return
}
utils.PostProcessResponse(w, cdc, res, cliCtx.Indent)
rest.PostProcessResponse(w, cdc, res, cliCtx.Indent)
}
}
@ -226,11 +227,11 @@ func queryProposalHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.Ha
if len(strProposalID) == 0 {
err := errors.New("proposalId required but not specified")
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
proposalID, ok := utils.ParseUint64OrReturnBadRequest(w, strProposalID)
proposalID, ok := rest.ParseUint64OrReturnBadRequest(w, strProposalID)
if !ok {
return
}
@ -239,17 +240,17 @@ func queryProposalHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.Ha
bz, err := cdc.MarshalJSON(params)
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
res, err := cliCtx.QueryWithData("custom/gov/proposal", bz)
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
utils.PostProcessResponse(w, cdc, res, cliCtx.Indent)
rest.PostProcessResponse(w, cdc, res, cliCtx.Indent)
}
}
@ -258,7 +259,7 @@ func queryDepositsHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.Ha
vars := mux.Vars(r)
strProposalID := vars[RestProposalID]
proposalID, ok := utils.ParseUint64OrReturnBadRequest(w, strProposalID)
proposalID, ok := rest.ParseUint64OrReturnBadRequest(w, strProposalID)
if !ok {
return
}
@ -267,19 +268,19 @@ func queryDepositsHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.Ha
bz, err := cdc.MarshalJSON(params)
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
res, err := cliCtx.QueryWithData("custom/gov/proposal", bz)
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
var proposal gov.Proposal
if err := cdc.UnmarshalJSON(res, &proposal); err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
@ -293,11 +294,11 @@ func queryDepositsHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.Ha
}
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
utils.PostProcessResponse(w, cdc, res, cliCtx.Indent)
rest.PostProcessResponse(w, cdc, res, cliCtx.Indent)
}
}
@ -306,18 +307,18 @@ func queryProposerHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.Ha
vars := mux.Vars(r)
strProposalID := vars[RestProposalID]
proposalID, ok := utils.ParseUint64OrReturnBadRequest(w, strProposalID)
proposalID, ok := rest.ParseUint64OrReturnBadRequest(w, strProposalID)
if !ok {
return
}
res, err := gcutils.QueryProposerByTxQuery(cdc, cliCtx, proposalID)
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
utils.PostProcessResponse(w, cdc, res, cliCtx.Indent)
rest.PostProcessResponse(w, cdc, res, cliCtx.Indent)
}
}
@ -329,24 +330,24 @@ func queryDepositHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.Han
if len(strProposalID) == 0 {
err := errors.New("proposalId required but not specified")
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
proposalID, ok := utils.ParseUint64OrReturnBadRequest(w, strProposalID)
proposalID, ok := rest.ParseUint64OrReturnBadRequest(w, strProposalID)
if !ok {
return
}
if len(bechDepositorAddr) == 0 {
err := errors.New("depositor address required but not specified")
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
depositorAddr, err := sdk.AccAddressFromBech32(bechDepositorAddr)
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
@ -354,13 +355,13 @@ func queryDepositHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.Han
bz, err := cdc.MarshalJSON(params)
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
res, err := cliCtx.QueryWithData("custom/gov/deposit", bz)
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
@ -373,25 +374,25 @@ func queryDepositHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.Han
if deposit.Empty() {
bz, err := cdc.MarshalJSON(gov.NewQueryProposalParams(proposalID))
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
res, err = cliCtx.QueryWithData("custom/gov/proposal", bz)
if err != nil || len(res) == 0 {
err := fmt.Errorf("proposalID %d does not exist", proposalID)
utils.WriteErrorResponse(w, http.StatusNotFound, err.Error())
rest.WriteErrorResponse(w, http.StatusNotFound, err.Error())
return
}
res, err = gcutils.QueryDepositByTxQuery(cdc, cliCtx, params)
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
}
utils.PostProcessResponse(w, cdc, res, cliCtx.Indent)
rest.PostProcessResponse(w, cdc, res, cliCtx.Indent)
}
}
@ -403,24 +404,24 @@ func queryVoteHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.Handle
if len(strProposalID) == 0 {
err := errors.New("proposalId required but not specified")
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
proposalID, ok := utils.ParseUint64OrReturnBadRequest(w, strProposalID)
proposalID, ok := rest.ParseUint64OrReturnBadRequest(w, strProposalID)
if !ok {
return
}
if len(bechVoterAddr) == 0 {
err := errors.New("voter address required but not specified")
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
voterAddr, err := sdk.AccAddressFromBech32(bechVoterAddr)
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
@ -428,13 +429,13 @@ func queryVoteHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.Handle
bz, err := cdc.MarshalJSON(params)
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
res, err := cliCtx.QueryWithData("custom/gov/vote", bz)
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
@ -447,25 +448,25 @@ func queryVoteHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.Handle
if vote.Empty() {
bz, err := cdc.MarshalJSON(gov.NewQueryProposalParams(proposalID))
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
res, err = cliCtx.QueryWithData("custom/gov/proposal", bz)
if err != nil || len(res) == 0 {
err := fmt.Errorf("proposalID %d does not exist", proposalID)
utils.WriteErrorResponse(w, http.StatusNotFound, err.Error())
rest.WriteErrorResponse(w, http.StatusNotFound, err.Error())
return
}
res, err = gcutils.QueryVoteByTxQuery(cdc, cliCtx, params)
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
}
utils.PostProcessResponse(w, cdc, res, cliCtx.Indent)
rest.PostProcessResponse(w, cdc, res, cliCtx.Indent)
}
}
@ -477,11 +478,11 @@ func queryVotesOnProposalHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext)
if len(strProposalID) == 0 {
err := errors.New("proposalId required but not specified")
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
proposalID, ok := utils.ParseUint64OrReturnBadRequest(w, strProposalID)
proposalID, ok := rest.ParseUint64OrReturnBadRequest(w, strProposalID)
if !ok {
return
}
@ -490,19 +491,19 @@ func queryVotesOnProposalHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext)
bz, err := cdc.MarshalJSON(params)
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
res, err := cliCtx.QueryWithData("custom/gov/proposal", bz)
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
var proposal gov.Proposal
if err := cdc.UnmarshalJSON(res, &proposal); err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
@ -516,11 +517,11 @@ func queryVotesOnProposalHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext)
}
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
utils.PostProcessResponse(w, cdc, res, cliCtx.Indent)
rest.PostProcessResponse(w, cdc, res, cliCtx.Indent)
}
}
@ -537,7 +538,7 @@ func queryProposalsWithParameterFn(cdc *codec.Codec, cliCtx context.CLIContext)
if len(bechVoterAddr) != 0 {
voterAddr, err := sdk.AccAddressFromBech32(bechVoterAddr)
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
params.Voter = voterAddr
@ -546,7 +547,7 @@ func queryProposalsWithParameterFn(cdc *codec.Codec, cliCtx context.CLIContext)
if len(bechDepositorAddr) != 0 {
depositorAddr, err := sdk.AccAddressFromBech32(bechDepositorAddr)
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
params.Depositor = depositorAddr
@ -555,13 +556,13 @@ func queryProposalsWithParameterFn(cdc *codec.Codec, cliCtx context.CLIContext)
if len(strProposalStatus) != 0 {
proposalStatus, err := gov.ProposalStatusFromString(govClientUtils.NormalizeProposalStatus(strProposalStatus))
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
params.ProposalStatus = proposalStatus
}
if len(strNumLimit) != 0 {
numLimit, ok := utils.ParseUint64OrReturnBadRequest(w, strNumLimit)
numLimit, ok := rest.ParseUint64OrReturnBadRequest(w, strNumLimit)
if !ok {
return
}
@ -570,17 +571,17 @@ func queryProposalsWithParameterFn(cdc *codec.Codec, cliCtx context.CLIContext)
bz, err := cdc.MarshalJSON(params)
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
res, err := cliCtx.QueryWithData("custom/gov/proposals", bz)
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
utils.PostProcessResponse(w, cdc, res, cliCtx.Indent)
rest.PostProcessResponse(w, cdc, res, cliCtx.Indent)
}
}
@ -592,11 +593,11 @@ func queryTallyOnProposalHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext)
if len(strProposalID) == 0 {
err := errors.New("proposalId required but not specified")
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
proposalID, ok := utils.ParseUint64OrReturnBadRequest(w, strProposalID)
proposalID, ok := rest.ParseUint64OrReturnBadRequest(w, strProposalID)
if !ok {
return
}
@ -605,16 +606,16 @@ func queryTallyOnProposalHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext)
bz, err := cdc.MarshalJSON(params)
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
res, err := cliCtx.QueryWithData("custom/gov/tally", bz)
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
utils.PostProcessResponse(w, cdc, res, cliCtx.Indent)
rest.PostProcessResponse(w, cdc, res, cliCtx.Indent)
}
}

View File

@ -3,8 +3,9 @@ package rest
import (
"net/http"
"github.com/cosmos/cosmos-sdk/client/rest"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/utils"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/crypto/keys"
sdk "github.com/cosmos/cosmos-sdk/types"
@ -19,8 +20,8 @@ func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *codec.Codec,
}
type transferReq struct {
BaseReq utils.BaseReq `json:"base_req"`
Amount sdk.Coins `json:"amount"`
BaseReq rest.BaseReq `json:"base_req"`
Amount sdk.Coins `json:"amount"`
}
// TransferRequestHandler - http request handler to transfer coins to a address
@ -33,12 +34,12 @@ func TransferRequestHandlerFn(cdc *codec.Codec, kb keys.Keybase, cliCtx context.
to, err := sdk.AccAddressFromBech32(bech32Addr)
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
var req transferReq
err = utils.ReadRESTReq(w, r, cdc, &req)
err = rest.ReadRESTReq(w, r, cdc, &req)
if err != nil {
return
}
@ -55,7 +56,7 @@ func TransferRequestHandlerFn(cdc *codec.Codec, kb keys.Keybase, cliCtx context.
// address.
addr, err := sdk.AccAddressFromBech32(req.BaseReq.From)
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
@ -66,10 +67,10 @@ func TransferRequestHandlerFn(cdc *codec.Codec, kb keys.Keybase, cliCtx context.
msg := ibc.IBCTransferMsg{IBCPacket: packet}
if req.BaseReq.GenerateOnly {
utils.WriteGenerateStdTxResponse(w, cdc, cliCtx, req.BaseReq, []sdk.Msg{msg})
rest.WriteGenerateStdTxResponse(w, cdc, cliCtx, req.BaseReq, []sdk.Msg{msg})
return
}
utils.CompleteAndBroadcastTxREST(w, r, cliCtx, req.BaseReq, []sdk.Msg{msg}, cdc)
rest.CompleteAndBroadcastTxREST(w, r, cliCtx, req.BaseReq, []sdk.Msg{msg}, cdc)
}
}

View File

@ -4,10 +4,11 @@ import (
"fmt"
"net/http"
"github.com/cosmos/cosmos-sdk/client/rest"
"github.com/gorilla/mux"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/utils"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/slashing"
@ -33,7 +34,7 @@ func signingInfoHandlerFn(cliCtx context.CLIContext, storeName string, cdc *code
pk, err := sdk.GetConsPubKeyBech32(vars["validatorPubKey"])
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
@ -41,7 +42,7 @@ func signingInfoHandlerFn(cliCtx context.CLIContext, storeName string, cdc *code
res, err := cliCtx.QueryStore(key, storeName)
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
@ -54,11 +55,11 @@ func signingInfoHandlerFn(cliCtx context.CLIContext, storeName string, cdc *code
err = cdc.UnmarshalBinaryLengthPrefixed(res, &signingInfo)
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
utils.PostProcessResponse(w, cdc, signingInfo, cliCtx.Indent)
rest.PostProcessResponse(w, cdc, signingInfo, cliCtx.Indent)
}
}
@ -68,10 +69,10 @@ func queryParamsHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.Hand
res, err := cliCtx.QueryWithData(route, nil)
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
utils.PostProcessResponse(w, cdc, res, cliCtx.Indent)
rest.PostProcessResponse(w, cdc, res, cliCtx.Indent)
}
}

View File

@ -4,8 +4,9 @@ import (
"bytes"
"net/http"
"github.com/cosmos/cosmos-sdk/client/rest"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/utils"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/crypto/keys"
sdk "github.com/cosmos/cosmos-sdk/types"
@ -23,7 +24,7 @@ func registerTxRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *codec.Codec
// Unjail TX body
type UnjailReq struct {
BaseReq utils.BaseReq `json:"base_req"`
BaseReq rest.BaseReq `json:"base_req"`
}
func unjailRequestHandlerFn(cdc *codec.Codec, kb keys.Keybase, cliCtx context.CLIContext) http.HandlerFunc {
@ -33,7 +34,7 @@ func unjailRequestHandlerFn(cdc *codec.Codec, kb keys.Keybase, cliCtx context.CL
bech32validator := vars["validatorAddr"]
var req UnjailReq
err := utils.ReadRESTReq(w, r, cdc, &req)
err := rest.ReadRESTReq(w, r, cdc, &req)
if err != nil {
return
}
@ -45,36 +46,36 @@ func unjailRequestHandlerFn(cdc *codec.Codec, kb keys.Keybase, cliCtx context.CL
valAddr, err := sdk.ValAddressFromBech32(bech32validator)
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
msg := slashing.NewMsgUnjail(valAddr)
err = msg.ValidateBasic()
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
if req.BaseReq.GenerateOnly {
utils.WriteGenerateStdTxResponse(w, cdc, cliCtx, req.BaseReq, []sdk.Msg{msg})
rest.WriteGenerateStdTxResponse(w, cdc, cliCtx, req.BaseReq, []sdk.Msg{msg})
return
}
// derive the from account address and name from the Keybase
fromAddress, fromName, err := context.GetFromFields(req.BaseReq.From)
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
cliCtx = cliCtx.WithFromName(fromName).WithFromAddress(fromAddress)
if !bytes.Equal(cliCtx.GetFromAddress(), valAddr) {
utils.WriteErrorResponse(w, http.StatusUnauthorized, "must use own validator address")
rest.WriteErrorResponse(w, http.StatusUnauthorized, "must use own validator address")
return
}
utils.CompleteAndBroadcastTxREST(w, r, cliCtx, req.BaseReq, []sdk.Msg{msg}, cdc)
rest.CompleteAndBroadcastTxREST(w, r, cliCtx, req.BaseReq, []sdk.Msg{msg}, cdc)
}
}

View File

@ -4,9 +4,10 @@ import (
"net/http"
"strings"
"github.com/cosmos/cosmos-sdk/client/rest"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/client/utils"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/staking"
@ -122,13 +123,13 @@ func delegatorTxsHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.Han
_, err := sdk.AccAddressFromBech32(delegatorAddr)
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
node, err := cliCtx.GetNode()
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
@ -170,17 +171,17 @@ func delegatorTxsHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.Han
for _, action := range actions {
foundTxs, errQuery := queryTxs(node, cliCtx, cdc, action, delegatorAddr)
if errQuery != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
}
txs = append(txs, foundTxs...)
}
res, err := cdc.MarshalJSON(txs)
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
utils.PostProcessResponse(w, cdc, res, cliCtx.Indent)
rest.PostProcessResponse(w, cdc, res, cliCtx.Indent)
}
}
@ -201,7 +202,7 @@ func redelegationsHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.Ha
if len(bechDelegatorAddr) != 0 {
delegatorAddr, err := sdk.AccAddressFromBech32(bechDelegatorAddr)
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
params.DelegatorAddr = delegatorAddr
@ -210,7 +211,7 @@ func redelegationsHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.Ha
if len(bechSrcValidatorAddr) != 0 {
srcValidatorAddr, err := sdk.ValAddressFromBech32(bechSrcValidatorAddr)
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
params.SrcValidatorAddr = srcValidatorAddr
@ -219,7 +220,7 @@ func redelegationsHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.Ha
if len(bechDstValidatorAddr) != 0 {
dstValidatorAddr, err := sdk.ValAddressFromBech32(bechDstValidatorAddr)
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
params.DstValidatorAddr = dstValidatorAddr
@ -227,16 +228,16 @@ func redelegationsHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.Ha
bz, err := cdc.MarshalJSON(params)
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
res, err := cliCtx.QueryWithData("custom/staking/redelegations", bz)
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
utils.PostProcessResponse(w, cdc, res, cliCtx.Indent)
rest.PostProcessResponse(w, cdc, res, cliCtx.Indent)
}
}
@ -260,10 +261,10 @@ func validatorsHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.Handl
return func(w http.ResponseWriter, r *http.Request) {
res, err := cliCtx.QueryWithData("custom/staking/validators", nil)
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
utils.PostProcessResponse(w, cdc, res, cliCtx.Indent)
rest.PostProcessResponse(w, cdc, res, cliCtx.Indent)
}
}
@ -287,10 +288,10 @@ func poolHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.HandlerFunc
return func(w http.ResponseWriter, r *http.Request) {
res, err := cliCtx.QueryWithData("custom/staking/pool", nil)
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
utils.PostProcessResponse(w, cdc, res, cliCtx.Indent)
rest.PostProcessResponse(w, cdc, res, cliCtx.Indent)
}
}
@ -299,9 +300,9 @@ func paramsHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.HandlerFu
return func(w http.ResponseWriter, r *http.Request) {
res, err := cliCtx.QueryWithData("custom/staking/parameters", nil)
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
utils.PostProcessResponse(w, cdc, res, cliCtx.Indent)
rest.PostProcessResponse(w, cdc, res, cliCtx.Indent)
}
}

View File

@ -4,8 +4,9 @@ import (
"bytes"
"net/http"
"github.com/cosmos/cosmos-sdk/client/rest"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/utils"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/crypto/keys"
sdk "github.com/cosmos/cosmos-sdk/types"
@ -31,14 +32,14 @@ func registerTxRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *codec.Codec
type (
msgDelegationsInput struct {
BaseReq utils.BaseReq `json:"base_req"`
BaseReq rest.BaseReq `json:"base_req"`
DelegatorAddr sdk.AccAddress `json:"delegator_addr"` // in bech32
ValidatorAddr sdk.ValAddress `json:"validator_addr"` // in bech32
Delegation sdk.Coin `json:"delegation"`
}
msgBeginRedelegateInput struct {
BaseReq utils.BaseReq `json:"base_req"`
BaseReq rest.BaseReq `json:"base_req"`
DelegatorAddr sdk.AccAddress `json:"delegator_addr"` // in bech32
ValidatorSrcAddr sdk.ValAddress `json:"validator_src_addr"` // in bech32
ValidatorDstAddr sdk.ValAddress `json:"validator_dst_addr"` // in bech32
@ -46,7 +47,7 @@ type (
}
msgUndelegateInput struct {
BaseReq utils.BaseReq `json:"base_req"`
BaseReq rest.BaseReq `json:"base_req"`
DelegatorAddr sdk.AccAddress `json:"delegator_addr"` // in bech32
ValidatorAddr sdk.ValAddress `json:"validator_addr"` // in bech32
SharesAmount sdk.Dec `json:"shares"`
@ -57,9 +58,9 @@ func postDelegationsHandlerFn(cdc *codec.Codec, kb keys.Keybase, cliCtx context.
return func(w http.ResponseWriter, r *http.Request) {
var req msgDelegationsInput
err := utils.ReadRESTReq(w, r, cdc, &req)
err := rest.ReadRESTReq(w, r, cdc, &req)
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
@ -71,30 +72,30 @@ func postDelegationsHandlerFn(cdc *codec.Codec, kb keys.Keybase, cliCtx context.
msg := staking.NewMsgDelegate(req.DelegatorAddr, req.ValidatorAddr, req.Delegation)
err = msg.ValidateBasic()
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
if req.BaseReq.GenerateOnly {
utils.WriteGenerateStdTxResponse(w, cdc, cliCtx, req.BaseReq, []sdk.Msg{msg})
rest.WriteGenerateStdTxResponse(w, cdc, cliCtx, req.BaseReq, []sdk.Msg{msg})
return
}
// derive the from account address and name from the Keybase
fromAddress, fromName, err := context.GetFromFields(req.BaseReq.From)
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
cliCtx = cliCtx.WithFromName(fromName).WithFromAddress(fromAddress)
if !bytes.Equal(cliCtx.GetFromAddress(), req.DelegatorAddr) {
utils.WriteErrorResponse(w, http.StatusUnauthorized, "must use own delegator address")
rest.WriteErrorResponse(w, http.StatusUnauthorized, "must use own delegator address")
return
}
utils.CompleteAndBroadcastTxREST(w, r, cliCtx, req.BaseReq, []sdk.Msg{msg}, cdc)
rest.CompleteAndBroadcastTxREST(w, r, cliCtx, req.BaseReq, []sdk.Msg{msg}, cdc)
}
}
@ -102,9 +103,9 @@ func postRedelegationsHandlerFn(cdc *codec.Codec, kb keys.Keybase, cliCtx contex
return func(w http.ResponseWriter, r *http.Request) {
var req msgBeginRedelegateInput
err := utils.ReadRESTReq(w, r, cdc, &req)
err := rest.ReadRESTReq(w, r, cdc, &req)
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
@ -116,30 +117,30 @@ func postRedelegationsHandlerFn(cdc *codec.Codec, kb keys.Keybase, cliCtx contex
msg := staking.NewMsgBeginRedelegate(req.DelegatorAddr, req.ValidatorSrcAddr, req.ValidatorDstAddr, req.SharesAmount)
err = msg.ValidateBasic()
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
if req.BaseReq.GenerateOnly {
utils.WriteGenerateStdTxResponse(w, cdc, cliCtx, req.BaseReq, []sdk.Msg{msg})
rest.WriteGenerateStdTxResponse(w, cdc, cliCtx, req.BaseReq, []sdk.Msg{msg})
return
}
// derive the from account address and name from the Keybase
fromAddress, fromName, err := context.GetFromFields(req.BaseReq.From)
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
cliCtx = cliCtx.WithFromName(fromName).WithFromAddress(fromAddress)
if !bytes.Equal(cliCtx.GetFromAddress(), req.DelegatorAddr) {
utils.WriteErrorResponse(w, http.StatusUnauthorized, "must use own delegator address")
rest.WriteErrorResponse(w, http.StatusUnauthorized, "must use own delegator address")
return
}
utils.CompleteAndBroadcastTxREST(w, r, cliCtx, req.BaseReq, []sdk.Msg{msg}, cdc)
rest.CompleteAndBroadcastTxREST(w, r, cliCtx, req.BaseReq, []sdk.Msg{msg}, cdc)
}
}
@ -147,9 +148,9 @@ func postUnbondingDelegationsHandlerFn(cdc *codec.Codec, kb keys.Keybase, cliCtx
return func(w http.ResponseWriter, r *http.Request) {
var req msgUndelegateInput
err := utils.ReadRESTReq(w, r, cdc, &req)
err := rest.ReadRESTReq(w, r, cdc, &req)
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
@ -161,29 +162,29 @@ func postUnbondingDelegationsHandlerFn(cdc *codec.Codec, kb keys.Keybase, cliCtx
msg := staking.NewMsgUndelegate(req.DelegatorAddr, req.ValidatorAddr, req.SharesAmount)
err = msg.ValidateBasic()
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
if req.BaseReq.GenerateOnly {
utils.WriteGenerateStdTxResponse(w, cdc, cliCtx, req.BaseReq, []sdk.Msg{msg})
rest.WriteGenerateStdTxResponse(w, cdc, cliCtx, req.BaseReq, []sdk.Msg{msg})
return
}
// derive the from account address and name from the Keybase
fromAddress, fromName, err := context.GetFromFields(req.BaseReq.From)
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
cliCtx = cliCtx.WithFromName(fromName).WithFromAddress(fromAddress)
if !bytes.Equal(cliCtx.GetFromAddress(), req.DelegatorAddr) {
utils.WriteErrorResponse(w, http.StatusUnauthorized, "must use own delegator address")
rest.WriteErrorResponse(w, http.StatusUnauthorized, "must use own delegator address")
return
}
utils.CompleteAndBroadcastTxREST(w, r, cliCtx, req.BaseReq, []sdk.Msg{msg}, cdc)
rest.CompleteAndBroadcastTxREST(w, r, cliCtx, req.BaseReq, []sdk.Msg{msg}, cdc)
}
}

View File

@ -4,11 +4,12 @@ import (
"fmt"
"net/http"
"github.com/cosmos/cosmos-sdk/client/rest"
"github.com/gorilla/mux"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/client/utils"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/staking"
@ -59,17 +60,17 @@ func queryRedelegations(cliCtx context.CLIContext, cdc *codec.Codec, endpoint st
delegatorAddr, err := sdk.AccAddressFromBech32(bech32delegator)
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
srcValidatorAddr, err := sdk.ValAddressFromBech32(bech32srcValidator)
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
dstValidatorAddr, err := sdk.ValAddressFromBech32(bech32dstValidator)
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
@ -81,16 +82,16 @@ func queryRedelegations(cliCtx context.CLIContext, cdc *codec.Codec, endpoint st
bz, err := cdc.MarshalJSON(params)
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
res, err := cliCtx.QueryWithData(endpoint, bz)
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
utils.PostProcessResponse(w, cdc, res, cliCtx.Indent)
rest.PostProcessResponse(w, cdc, res, cliCtx.Indent)
}
}
@ -103,7 +104,7 @@ func queryBonds(cliCtx context.CLIContext, cdc *codec.Codec, endpoint string) ht
delegatorAddr, err := sdk.AccAddressFromBech32(bech32delegator)
validatorAddr, err := sdk.ValAddressFromBech32(bech32validator)
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
@ -111,16 +112,16 @@ func queryBonds(cliCtx context.CLIContext, cdc *codec.Codec, endpoint string) ht
bz, err := cdc.MarshalJSON(params)
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
res, err := cliCtx.QueryWithData(endpoint, bz)
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
utils.PostProcessResponse(w, cdc, res, cliCtx.Indent)
rest.PostProcessResponse(w, cdc, res, cliCtx.Indent)
}
}
@ -131,7 +132,7 @@ func queryDelegator(cliCtx context.CLIContext, cdc *codec.Codec, endpoint string
delegatorAddr, err := sdk.AccAddressFromBech32(bech32delegator)
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
@ -139,16 +140,16 @@ func queryDelegator(cliCtx context.CLIContext, cdc *codec.Codec, endpoint string
bz, err := cdc.MarshalJSON(params)
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
res, err := cliCtx.QueryWithData(endpoint, bz)
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
utils.PostProcessResponse(w, cdc, res, cliCtx.Indent)
rest.PostProcessResponse(w, cdc, res, cliCtx.Indent)
}
}
@ -159,7 +160,7 @@ func queryValidator(cliCtx context.CLIContext, cdc *codec.Codec, endpoint string
validatorAddr, err := sdk.ValAddressFromBech32(bech32validatorAddr)
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
@ -167,15 +168,15 @@ func queryValidator(cliCtx context.CLIContext, cdc *codec.Codec, endpoint string
bz, err := cdc.MarshalJSON(params)
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
res, err := cliCtx.QueryWithData(endpoint, bz)
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
utils.PostProcessResponse(w, cdc, res, cliCtx.Indent)
rest.PostProcessResponse(w, cdc, res, cliCtx.Indent)
}
}