2018-02-23 04:53:49 -08:00
|
|
|
package tx
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
|
|
|
|
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/wire"
|
2018-02-23 04:53:49 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
flagTags = "tag"
|
|
|
|
flagAny = "any"
|
|
|
|
)
|
|
|
|
|
2018-02-28 17:57:38 -08:00
|
|
|
// default client command to search through tagged transactions
|
|
|
|
func SearchTxCmd(cmdr commander) *cobra.Command {
|
2018-02-23 04:53:49 -08:00
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "txs",
|
|
|
|
Short: "Search for all transactions that match the given tags",
|
2018-02-28 17:57:38 -08:00
|
|
|
RunE: cmdr.searchTxCmd,
|
2018-02-23 04:53:49 -08:00
|
|
|
}
|
|
|
|
cmd.Flags().StringP(client.FlagNode, "n", "tcp://localhost:46657", "Node to connect to")
|
2018-02-28 17:57:38 -08:00
|
|
|
// TODO: change this to false once proofs built in
|
2018-02-23 04:53:49 -08:00
|
|
|
cmd.Flags().Bool(client.FlagTrustNode, true, "Don't verify proofs for responses")
|
|
|
|
cmd.Flags().StringSlice(flagTags, nil, "Tags that must match (may provide multiple)")
|
|
|
|
cmd.Flags().Bool(flagAny, false, "Return transactions that match ANY tag, rather than ALL")
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2018-02-28 17:57:38 -08:00
|
|
|
func (c commander) searchTxCmd(cmd *cobra.Command, args []string) error {
|
2018-02-23 04:53:49 -08:00
|
|
|
tags := viper.GetStringSlice(flagTags)
|
|
|
|
if len(tags) == 0 {
|
|
|
|
return errors.New("Must declare at least one tag to search")
|
|
|
|
}
|
|
|
|
// XXX: implement ANY
|
|
|
|
query := strings.Join(tags, " AND ")
|
|
|
|
|
|
|
|
// 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.TxSearch(query, prove)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-02-28 17:57:38 -08:00
|
|
|
info, err := formatTxResults(c.cdc, res)
|
2018-02-23 04:53:49 -08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-02-28 17:57:38 -08:00
|
|
|
output, err := c.cdc.MarshalJSON(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 formatTxResults(cdc *wire.Codec, res []*ctypes.ResultTx) ([]txInfo, error) {
|
2018-02-23 04:53:49 -08:00
|
|
|
var err error
|
|
|
|
out := make([]txInfo, len(res))
|
|
|
|
for i := range res {
|
2018-02-28 17:57:38 -08:00
|
|
|
out[i], err = formatTxResult(cdc, res[i])
|
2018-02-23 04:53:49 -08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|