2019-06-15 05:34:11 -07:00
|
|
|
package rest
|
|
|
|
|
|
|
|
import (
|
2020-12-03 15:33:32 -08:00
|
|
|
"fmt"
|
2019-06-15 05:34:11 -07:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
|
2020-12-09 03:05:12 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
2020-12-03 15:33:32 -08:00
|
|
|
clientrest "github.com/cosmos/cosmos-sdk/client/rest"
|
2020-07-30 07:45:18 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/tx"
|
2020-12-09 03:05:12 -08:00
|
|
|
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
|
2019-06-15 05:34:11 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/types/rest"
|
2020-09-22 07:35:18 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth/legacy/legacytx"
|
2019-06-15 05:34:11 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// BroadcastReq defines a tx broadcasting request.
|
|
|
|
type BroadcastReq struct {
|
2020-09-22 07:35:18 -07:00
|
|
|
Tx legacytx.StdTx `json:"tx" yaml:"tx"`
|
|
|
|
Mode string `json:"mode" yaml:"mode"`
|
2019-06-15 05:34:11 -07:00
|
|
|
}
|
|
|
|
|
2020-12-09 03:05:12 -08:00
|
|
|
var _ codectypes.UnpackInterfacesMessage = BroadcastReq{}
|
|
|
|
|
|
|
|
// UnpackInterfaces implements the UnpackInterfacesMessage interface.
|
|
|
|
func (m BroadcastReq) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
|
|
|
|
return m.Tx.UnpackInterfaces(unpacker)
|
|
|
|
}
|
|
|
|
|
2019-06-15 05:34:11 -07:00
|
|
|
// BroadcastTxRequest implements a tx broadcasting handler that is responsible
|
|
|
|
// for broadcasting a valid and signed tx to a full node. The tx can be
|
|
|
|
// broadcasted via a sync|async|block mechanism.
|
2020-06-01 05:46:03 -07:00
|
|
|
func BroadcastTxRequest(clientCtx client.Context) http.HandlerFunc {
|
2019-06-15 05:34:11 -07:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var req BroadcastReq
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
2020-04-01 00:50:22 -07:00
|
|
|
if rest.CheckBadRequestError(w, err) {
|
2019-06-15 05:34:11 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-07-30 07:45:18 -07:00
|
|
|
// NOTE: amino is used intentionally here, don't migrate it!
|
2020-12-03 15:33:32 -08:00
|
|
|
err = clientCtx.LegacyAmino.UnmarshalJSON(body, &req)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("this transaction cannot be broadcasted via legacy REST endpoints, because it does not support"+
|
|
|
|
" Amino serialization. Please either use CLI, gRPC, gRPC-gateway, or directly query the Tendermint RPC"+
|
|
|
|
" endpoint to broadcast this transaction. The new REST endpoint (via gRPC-gateway) is POST /cosmos/tx/v1beta1/txs."+
|
|
|
|
" Please also see the REST endpoints migration guide at %s for more info", clientrest.DeprecationURL)
|
|
|
|
if rest.CheckBadRequestError(w, err) {
|
|
|
|
return
|
|
|
|
}
|
2019-06-15 05:34:11 -07:00
|
|
|
}
|
|
|
|
|
2020-07-30 07:45:18 -07:00
|
|
|
txBytes, err := tx.ConvertAndEncodeStdTx(clientCtx.TxConfig, req.Tx)
|
2020-04-01 00:50:22 -07:00
|
|
|
if rest.CheckInternalServerError(w, err) {
|
2019-06-15 05:34:11 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-01 05:46:03 -07:00
|
|
|
clientCtx = clientCtx.WithBroadcastMode(req.Mode)
|
2019-06-15 05:34:11 -07:00
|
|
|
|
2020-06-01 05:46:03 -07:00
|
|
|
res, err := clientCtx.BroadcastTx(txBytes)
|
2020-04-01 00:50:22 -07:00
|
|
|
if rest.CheckInternalServerError(w, err) {
|
2019-06-15 05:34:11 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-01 05:46:03 -07:00
|
|
|
rest.PostProcessResponseBare(w, clientCtx, res)
|
2019-06-15 05:34:11 -07:00
|
|
|
}
|
|
|
|
}
|