cosmos-sdk/client/tx/query.go

152 lines
3.6 KiB
Go
Raw Normal View History

package tx
import (
"encoding/hex"
"fmt"
"net/http"
"github.com/tendermint/tendermint/libs/common"
"github.com/gorilla/mux"
"github.com/spf13/cobra"
abci "github.com/tendermint/tendermint/abci/types"
ctypes "github.com/tendermint/tendermint/rpc/core/types"
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/client/utils"
"github.com/cosmos/cosmos-sdk/codec"
2018-03-02 01:24:07 -08:00
sdk "github.com/cosmos/cosmos-sdk/types"
2018-05-23 19:26:54 -07:00
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/spf13/viper"
)
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 {
// find the key to look up the account
hashHexStr := args[0]
2018-08-06 11:11:30 -07:00
cliCtx := context.NewCLIContext().WithCodec(cdc)
output, err := queryTx(cdc, cliCtx, hashHexStr)
2018-04-18 21:49:24 -07:00
if err != nil {
return err
}
2018-08-06 11:11:30 -07:00
fmt.Println(string(output))
2018-04-18 21:49:24 -07:00
return nil
},
}
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().String(client.FlagChainID, "", "Chain ID of Tendermint node")
viper.BindPFlag(client.FlagChainID, cmd.Flags().Lookup(client.FlagChainID))
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
}
func queryTx(cdc *codec.Codec, cliCtx context.CLIContext, hashHexStr string) ([]byte, error) {
hash, err := hex.DecodeString(hashHexStr)
if err != nil {
return nil, err
}
2018-08-06 11:11:30 -07:00
node, err := cliCtx.GetNode()
2018-02-28 15:26:39 -08:00
if err != nil {
return nil, err
}
res, err := node.Tx(hash, !cliCtx.TrustNode)
if err != nil {
return nil, err
}
2018-08-06 11:11:30 -07:00
if !cliCtx.TrustNode {
err := ValidateTxResult(cliCtx, res)
if err != nil {
return nil, err
}
}
2018-04-18 21:49:24 -07:00
info, err := formatTxResult(cdc, res)
if err != nil {
return nil, err
}
if cliCtx.Indent {
return cdc.MarshalJSONIndent(info, "", " ")
}
return cdc.MarshalJSON(info)
}
// ValidateTxResult performs transaction verification
func ValidateTxResult(cliCtx context.CLIContext, res *ctypes.ResultTx) error {
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) (Info, error) {
2018-02-28 17:57:38 -08:00
tx, err := parseTx(cdc, res.Tx)
if err != nil {
return Info{}, err
}
return Info{
Hash: res.Hash,
Height: res.Height,
Tx: tx,
Result: res.TxResult,
2018-08-06 11:11:30 -07:00
}, nil
}
// Info is used to prepare info to display
type Info struct {
Hash common.HexBytes `json:"hash"`
Height int64 `json:"height"`
Tx sdk.Tx `json:"tx"`
Result abci.ResponseDeliverTx `json:"result"`
}
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.UnmarshalBinary(txBytes, &tx)
if err != nil {
return nil, err
}
2018-08-06 11:11:30 -07:00
return tx, nil
}
// REST
2018-04-18 21:49:24 -07:00
// 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 {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
utils.PostProcessResponse(w, cdc, output, cliCtx.Indent)
}
}