2018-02-23 04:53:49 -08:00
|
|
|
package tx
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
2018-10-03 08:48:23 -07:00
|
|
|
"net/http"
|
2019-02-15 08:00:41 -08:00
|
|
|
"strings"
|
2018-06-21 10:21:58 -07:00
|
|
|
|
2018-02-28 23:16:54 -08:00
|
|
|
"github.com/gorilla/mux"
|
2018-02-23 04:53:49 -08:00
|
|
|
"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"
|
2018-04-02 05:05:23 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/context"
|
2018-09-13 11:17:32 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
2018-03-02 01:24:07 -08:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2019-02-14 08:53:36 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/types/rest"
|
2018-05-23 19:26:54 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth"
|
2018-02-23 04:53:49 -08:00
|
|
|
)
|
|
|
|
|
2018-08-06 11:11:30 -07:00
|
|
|
// QueryTxCmd implements the default command for a tx query.
|
2018-09-13 11:17:32 -07:00
|
|
|
func QueryTxCmd(cdc *codec.Codec) *cobra.Command {
|
2018-02-23 04:53:49 -08:00
|
|
|
cmd := &cobra.Command{
|
2018-02-28 17:57:38 -08:00
|
|
|
Use: "tx [hash]",
|
2018-02-23 04:53:49 -08:00
|
|
|
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-02-23 04:53:49 -08:00
|
|
|
}
|
2018-04-18 21:49:24 -07:00
|
|
|
|
2018-06-13 15:13:51 -07:00
|
|
|
cmd.Flags().StringP(client.FlagNode, "n", "tcp://localhost:26657", "Node to connect to")
|
2018-09-25 14:36:42 -07:00
|
|
|
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))
|
2018-02-23 04:53:49 -08:00
|
|
|
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) {
|
2018-02-28 23:16:54 -08:00
|
|
|
hash, err := hex.DecodeString(hashHexStr)
|
2018-02-23 04:53:49 -08:00
|
|
|
if err != nil {
|
2019-02-15 08:00:41 -08:00
|
|
|
return out, err
|
2018-02-23 04:53:49 -08:00
|
|
|
}
|
|
|
|
|
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
|
2018-02-23 04:53:49 -08:00
|
|
|
}
|
|
|
|
|
2018-09-14 11:41:21 -07:00
|
|
|
res, err := node.Tx(hash, !cliCtx.TrustNode)
|
2018-02-23 04:53:49 -08:00
|
|
|
if err != nil {
|
2019-02-15 08:00:41 -08:00
|
|
|
return out, err
|
2018-02-23 04:53:49 -08:00
|
|
|
}
|
2018-08-06 11:11:30 -07:00
|
|
|
|
2018-09-14 11:41:21 -07:00
|
|
|
if !cliCtx.TrustNode {
|
2019-02-15 08:00:41 -08:00
|
|
|
if err = ValidateTxResult(cliCtx, res); err != nil {
|
|
|
|
return out, err
|
2018-09-14 11:41:21 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-15 08:00:41 -08:00
|
|
|
if out, err = formatTxResult(cdc, res); err != nil {
|
|
|
|
return out, err
|
2018-02-23 04:53:49 -08:00
|
|
|
}
|
|
|
|
|
2019-02-15 08:00:41 -08:00
|
|
|
return out, nil
|
2018-02-23 04:53:49 -08:00
|
|
|
}
|
|
|
|
|
2018-09-14 11:41:21 -07:00
|
|
|
// 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
|
|
|
|
}
|
2018-09-14 11:41:21 -07:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-04 18:20:56 -08:00
|
|
|
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)
|
2018-02-23 04:53:49 -08:00
|
|
|
if err != nil {
|
2019-02-04 18:20:56 -08:00
|
|
|
return sdk.TxResponse{}, err
|
2018-02-23 04:53:49 -08:00
|
|
|
}
|
|
|
|
|
2019-02-04 18:20:56 -08:00
|
|
|
return sdk.NewResponseResultTx(res, tx), nil
|
2018-02-23 04:53:49 -08:00
|
|
|
}
|
|
|
|
|
2018-09-13 11:17:32 -07:00
|
|
|
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
|
|
|
|
2018-11-04 18:28:38 -08:00
|
|
|
err := cdc.UnmarshalBinaryLengthPrefixed(txBytes, &tx)
|
2018-02-23 04:53:49 -08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-08-06 11:11:30 -07:00
|
|
|
|
2018-02-23 04:53:49 -08:00
|
|
|
return tx, nil
|
|
|
|
}
|
2018-02-28 23:16:54 -08:00
|
|
|
|
|
|
|
// REST
|
|
|
|
|
2019-02-15 08:00:41 -08:00
|
|
|
// QueryTxRequestHandlerFn transaction query REST handler
|
2018-09-13 11:17:32 -07:00
|
|
|
func QueryTxRequestHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc {
|
2018-02-28 23:16:54 -08:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
hashHexStr := vars["hash"]
|
|
|
|
|
2018-09-14 11:41:21 -07:00
|
|
|
output, err := queryTx(cdc, cliCtx, hashHexStr)
|
2018-02-28 23:16:54 -08:00
|
|
|
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())
|
2018-02-28 23:16:54 -08:00
|
|
|
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)
|
2018-02-28 23:16:54 -08:00
|
|
|
}
|
|
|
|
}
|