cosmos-sdk/client/tx/query.go

141 lines
3.4 KiB
Go
Raw Normal View History

package tx
import (
"encoding/hex"
"fmt"
"net/http"
2019-02-15 08:00:41 -08:00
"strings"
"github.com/gorilla/mux"
"github.com/spf13/cobra"
ctypes "github.com/tendermint/tendermint/rpc/core/types"
2018-03-02 01:24:07 -08:00
2018-12-10 06:27:25 -08:00
"github.com/spf13/viper"
2018-03-02 01:24:07 -08:00
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/codec"
2018-03-02 01:24:07 -08:00
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/rest"
2018-05-23 19:26:54 -07:00
"github.com/cosmos/cosmos-sdk/x/auth"
)
2018-08-06 11:11:30 -07:00
// QueryTxCmd implements the default command for a tx query.
func QueryTxCmd(cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
2018-02-28 17:57:38 -08:00
Use: "tx [hash]",
Short: "Matches this txhash over all committed blocks",
2018-04-23 08:47:39 -07:00
Args: cobra.ExactArgs(1),
2018-04-18 21:49:24 -07:00
RunE: func(cmd *cobra.Command, args []string) error {
2018-08-06 11:11:30 -07:00
cliCtx := context.NewCLIContext().WithCodec(cdc)
2019-02-15 08:00:41 -08:00
output, err := queryTx(cdc, cliCtx, args[0])
2018-04-18 21:49:24 -07:00
if err != nil {
return err
}
2019-02-15 08:00:41 -08:00
if output.Empty() {
return fmt.Errorf("No transaction found with hash %s", args[0])
}
return cliCtx.PrintOutput(output)
2018-04-18 21:49:24 -07:00
},
}
2018-04-18 21:49:24 -07:00
cmd.Flags().StringP(client.FlagNode, "n", "tcp://localhost:26657", "Node to connect to")
viper.BindPFlag(client.FlagNode, cmd.Flags().Lookup(client.FlagNode))
cmd.Flags().Bool(client.FlagTrustNode, false, "Trust connected full node (don't verify proofs for responses)")
viper.BindPFlag(client.FlagTrustNode, cmd.Flags().Lookup(client.FlagTrustNode))
return cmd
}
2019-02-15 08:00:41 -08:00
func queryTx(cdc *codec.Codec, cliCtx context.CLIContext, hashHexStr string) (out sdk.TxResponse, err error) {
hash, err := hex.DecodeString(hashHexStr)
if err != nil {
2019-02-15 08:00:41 -08:00
return out, err
}
2018-08-06 11:11:30 -07:00
node, err := cliCtx.GetNode()
2018-02-28 15:26:39 -08:00
if err != nil {
2019-02-15 08:00:41 -08:00
return out, err
}
res, err := node.Tx(hash, !cliCtx.TrustNode)
if err != nil {
2019-02-15 08:00:41 -08:00
return out, err
}
2018-08-06 11:11:30 -07:00
if !cliCtx.TrustNode {
2019-02-15 08:00:41 -08:00
if err = ValidateTxResult(cliCtx, res); err != nil {
return out, err
}
}
2019-02-15 08:00:41 -08:00
if out, err = formatTxResult(cdc, res); err != nil {
return out, err
}
2019-02-15 08:00:41 -08:00
return out, nil
}
// ValidateTxResult performs transaction verification
func ValidateTxResult(cliCtx context.CLIContext, res *ctypes.ResultTx) error {
2019-02-15 08:00:41 -08:00
if !cliCtx.TrustNode {
check, err := cliCtx.Verify(res.Height)
if err != nil {
return err
}
err = res.Proof.Validate(check.Header.DataHash)
if err != nil {
return err
}
}
return nil
}
func formatTxResult(cdc *codec.Codec, res *ctypes.ResultTx) (sdk.TxResponse, error) {
2018-02-28 17:57:38 -08:00
tx, err := parseTx(cdc, res.Tx)
if err != nil {
return sdk.TxResponse{}, err
}
return sdk.NewResponseResultTx(res, tx), nil
}
func parseTx(cdc *codec.Codec, txBytes []byte) (sdk.Tx, error) {
2018-05-23 19:26:54 -07:00
var tx auth.StdTx
2018-08-06 11:11:30 -07:00
err := cdc.UnmarshalBinaryLengthPrefixed(txBytes, &tx)
if err != nil {
return nil, err
}
2018-08-06 11:11:30 -07:00
return tx, nil
}
// REST
2019-02-15 08:00:41 -08:00
// QueryTxRequestHandlerFn transaction query REST handler
func QueryTxRequestHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
hashHexStr := vars["hash"]
output, err := queryTx(cdc, cliCtx, hashHexStr)
if err != nil {
2019-02-15 08:00:41 -08:00
if strings.Contains(err.Error(), hashHexStr) {
rest.WriteErrorResponse(w, http.StatusNotFound, err.Error())
return
}
2019-02-04 07:48:26 -08:00
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
2019-02-15 08:00:41 -08:00
if output.Empty() {
rest.WriteErrorResponse(w, http.StatusNotFound, fmt.Sprintf("no transaction found with hash %s", hashHexStr))
}
2019-02-04 07:48:26 -08:00
rest.PostProcessResponse(w, cdc, output, cliCtx.Indent)
}
}