cosmos-sdk/client/tx/query.go

135 lines
3.0 KiB
Go
Raw Normal View History

package tx
import (
"encoding/hex"
"fmt"
"net/http"
"strconv"
"github.com/tendermint/tendermint/libs/common"
"github.com/gorilla/mux"
"github.com/spf13/cobra"
"github.com/spf13/viper"
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"
2018-03-02 01:24:07 -08:00
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire"
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.
2018-04-18 21:49:24 -07:00
func QueryTxCmd(cdc *wire.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]
trustNode := viper.GetBool(client.FlagTrustNode)
2018-08-06 11:11:30 -07:00
cliCtx := context.NewCLIContext().WithCodec(cdc)
output, err := queryTx(cdc, cliCtx, hashHexStr, trustNode)
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")
2018-04-18 21:49:24 -07:00
// TODO: change this to false when we can
cmd.Flags().Bool(client.FlagTrustNode, true, "Don't verify proofs for responses")
return cmd
}
2018-08-06 11:11:30 -07:00
func queryTx(cdc *wire.Codec, cliCtx context.CLIContext, hashHexStr string, trustNode bool) ([]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, !trustNode)
if err != nil {
return nil, err
}
2018-08-06 11:11:30 -07:00
2018-04-18 21:49:24 -07:00
info, err := formatTxResult(cdc, res)
if err != nil {
return nil, err
}
return wire.MarshalJSONIndent(cdc, info)
}
func formatTxResult(cdc *wire.Codec, res *ctypes.ResultTx) (Info, error) {
// TODO: verify the proof if requested
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"`
}
2018-02-28 17:57:38 -08:00
func parseTx(cdc *wire.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
2018-08-06 11:11:30 -07:00
func QueryTxRequestHandlerFn(cdc *wire.Codec, cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
hashHexStr := vars["hash"]
trustNode, err := strconv.ParseBool(r.FormValue("trust_node"))
// trustNode defaults to true
if err != nil {
trustNode = true
}
2018-08-06 11:11:30 -07:00
output, err := queryTx(cdc, cliCtx, hashHexStr, trustNode)
if err != nil {
w.WriteHeader(500)
w.Write([]byte(err.Error()))
return
}
2018-08-06 11:11:30 -07:00
w.Write(output)
}
}