2018-02-23 04:53:49 -08:00
|
|
|
package tx
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
2018-02-28 23:16:54 -08:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
2018-02-23 04:53:49 -08:00
|
|
|
|
2018-07-02 13:34:06 -07:00
|
|
|
"github.com/tendermint/tendermint/libs/common"
|
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"
|
|
|
|
"github.com/spf13/viper"
|
2018-06-28 17:54:47 -07:00
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
2018-02-23 04:53:49 -08:00
|
|
|
ctypes "github.com/tendermint/tendermint/rpc/core/types"
|
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"
|
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 {
|
|
|
|
// 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-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-04-18 21:49:24 -07:00
|
|
|
|
2018-02-23 04:53:49 -08:00
|
|
|
// TODO: change this to false when we can
|
|
|
|
cmd.Flags().Bool(client.FlagTrustNode, true, "Don't verify proofs for responses")
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2018-09-13 11:17:32 -07:00
|
|
|
func queryTx(cdc *codec.Codec, cliCtx context.CLIContext, hashHexStr string, trustNode bool) ([]byte, 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 {
|
2018-02-28 23:16:54 -08:00
|
|
|
return nil, 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 {
|
2018-02-28 23:16:54 -08:00
|
|
|
return nil, err
|
2018-02-23 04:53:49 -08:00
|
|
|
}
|
|
|
|
|
2018-02-28 23:16:54 -08:00
|
|
|
res, err := node.Tx(hash, !trustNode)
|
2018-02-23 04:53:49 -08:00
|
|
|
if err != nil {
|
2018-02-28 23:16:54 -08:00
|
|
|
return nil, err
|
2018-02-23 04:53:49 -08:00
|
|
|
}
|
2018-08-06 11:11:30 -07:00
|
|
|
|
2018-04-18 21:49:24 -07:00
|
|
|
info, err := formatTxResult(cdc, res)
|
2018-02-23 04:53:49 -08:00
|
|
|
if err != nil {
|
2018-02-28 23:16:54 -08:00
|
|
|
return nil, err
|
2018-02-23 04:53:49 -08:00
|
|
|
}
|
|
|
|
|
2018-09-13 11:17:32 -07:00
|
|
|
return codec.MarshalJSONIndent(cdc, info)
|
2018-02-23 04:53:49 -08:00
|
|
|
}
|
|
|
|
|
2018-09-13 11:17:32 -07:00
|
|
|
func formatTxResult(cdc *codec.Codec, res *ctypes.ResultTx) (Info, error) {
|
2018-02-23 04:53:49 -08:00
|
|
|
// TODO: verify the proof if requested
|
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 {
|
2018-08-08 03:38:39 -07:00
|
|
|
return Info{}, err
|
2018-02-23 04:53:49 -08:00
|
|
|
}
|
|
|
|
|
2018-08-08 03:38:39 -07:00
|
|
|
return Info{
|
2018-06-21 10:21:58 -07:00
|
|
|
Hash: res.Hash,
|
2018-02-23 05:17:07 -08:00
|
|
|
Height: res.Height,
|
2018-02-23 04:53:49 -08:00
|
|
|
Tx: tx,
|
2018-02-23 05:17:07 -08:00
|
|
|
Result: res.TxResult,
|
2018-08-06 11:11:30 -07:00
|
|
|
}, nil
|
2018-02-23 04:53:49 -08:00
|
|
|
}
|
|
|
|
|
2018-08-08 03:38:39 -07:00
|
|
|
// Info is used to prepare info to display
|
|
|
|
type Info struct {
|
2018-06-21 10:21:58 -07:00
|
|
|
Hash common.HexBytes `json:"hash"`
|
2018-02-23 04:53:49 -08:00
|
|
|
Height int64 `json:"height"`
|
|
|
|
Tx sdk.Tx `json:"tx"`
|
|
|
|
Result abci.ResponseDeliverTx `json:"result"`
|
|
|
|
}
|
|
|
|
|
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-02-23 04:53:49 -08:00
|
|
|
err := cdc.UnmarshalBinary(txBytes, &tx)
|
|
|
|
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
|
|
|
|
|
2018-04-18 21:49:24 -07:00
|
|
|
// 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"]
|
|
|
|
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)
|
2018-02-28 23:16:54 -08:00
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(500)
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
2018-08-06 11:11:30 -07:00
|
|
|
|
2018-02-28 23:16:54 -08:00
|
|
|
w.Write(output)
|
|
|
|
}
|
|
|
|
}
|