2018-02-23 04:53:49 -08:00
|
|
|
package tx
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
2018-02-23 05:17:07 -08:00
|
|
|
"encoding/json"
|
2018-02-23 04:53:49 -08:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
|
2018-02-28 17:57:38 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/client" // XXX: not good
|
2018-02-23 04:53:49 -08:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
abci "github.com/tendermint/abci/types"
|
2018-02-28 17:57:38 -08:00
|
|
|
wire "github.com/tendermint/go-wire"
|
2018-02-23 04:53:49 -08:00
|
|
|
ctypes "github.com/tendermint/tendermint/rpc/core/types"
|
|
|
|
)
|
|
|
|
|
2018-02-28 17:57:38 -08:00
|
|
|
// Get the default command for a tx query
|
|
|
|
func QueryTxCmd(cmdr commander) *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-02-28 17:57:38 -08:00
|
|
|
RunE: cmdr.queryTxCmd,
|
2018-02-23 04:53:49 -08:00
|
|
|
}
|
|
|
|
cmd.Flags().StringP(client.FlagNode, "n", "tcp://localhost:46657", "Node to connect to")
|
|
|
|
// TODO: change this to false when we can
|
|
|
|
cmd.Flags().Bool(client.FlagTrustNode, true, "Don't verify proofs for responses")
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2018-02-28 15:26:39 -08:00
|
|
|
// command to query for a transaction
|
2018-02-28 17:57:38 -08:00
|
|
|
func (c commander) queryTxCmd(cmd *cobra.Command, args []string) error {
|
2018-02-23 04:53:49 -08:00
|
|
|
if len(args) != 1 || len(args[0]) == 0 {
|
|
|
|
return errors.New("You must provide a tx hash")
|
|
|
|
}
|
|
|
|
|
|
|
|
// find the key to look up the account
|
|
|
|
hexStr := args[0]
|
|
|
|
hash, err := hex.DecodeString(hexStr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the node
|
2018-02-28 15:26:39 -08:00
|
|
|
node, err := client.GetNode()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-02-23 04:53:49 -08:00
|
|
|
}
|
|
|
|
prove := !viper.GetBool(client.FlagTrustNode)
|
|
|
|
|
|
|
|
res, err := node.Tx(hash, prove)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-02-28 17:57:38 -08:00
|
|
|
info, err := formatTxResult(c.cdc, res)
|
2018-02-23 04:53:49 -08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-02-23 05:17:07 -08:00
|
|
|
output, err := json.MarshalIndent(info, "", " ")
|
2018-02-23 04:53:49 -08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Println(string(output))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-02-28 17:57:38 -08:00
|
|
|
func formatTxResult(cdc *wire.Codec, res *ctypes.ResultTx) (txInfo, 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 {
|
|
|
|
return txInfo{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
info := txInfo{
|
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-02-23 04:53:49 -08:00
|
|
|
}
|
|
|
|
return info, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// txInfo is used to prepare info to display
|
|
|
|
type txInfo struct {
|
|
|
|
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-02-23 04:53:49 -08:00
|
|
|
var tx sdk.StdTx
|
|
|
|
err := cdc.UnmarshalBinary(txBytes, &tx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return tx, nil
|
|
|
|
}
|