2018-02-23 04:53:49 -08:00
|
|
|
package tx
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-10-03 08:48:23 -07:00
|
|
|
"net/http"
|
2019-02-15 08:00:41 -08:00
|
|
|
"strings"
|
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"
|
2019-04-02 18:09:37 -07:00
|
|
|
"github.com/tendermint/tendermint/types"
|
2018-03-02 01:24:07 -08:00
|
|
|
|
2018-12-10 06:27:25 -08:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
|
2018-04-02 05:05:23 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/context"
|
2019-05-28 01:44:04 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/flags"
|
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"
|
2019-02-14 08:53:36 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/types/rest"
|
2018-02-23 04:53:49 -08:00
|
|
|
)
|
|
|
|
|
2019-04-02 18:09:37 -07:00
|
|
|
const (
|
|
|
|
flagTags = "tags"
|
|
|
|
flagPage = "page"
|
|
|
|
flagLimit = "limit"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// CLI
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// SearchTxCmd returns a command to search through tagged transactions.
|
|
|
|
func SearchTxCmd(cdc *codec.Codec) *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "txs",
|
|
|
|
Short: "Search for paginated transactions that match a set of tags",
|
|
|
|
Long: strings.TrimSpace(`
|
|
|
|
Search for transactions that match the exact given tags where results are paginated.
|
|
|
|
|
|
|
|
Example:
|
2019-05-18 01:42:24 -07:00
|
|
|
$ <appcli> query txs --tags '<tag1>:<value1>&<tag2>:<value2>' --page 1 --limit 30
|
2019-04-02 18:09:37 -07:00
|
|
|
`),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
tagsStr := viper.GetString(flagTags)
|
|
|
|
tagsStr = strings.Trim(tagsStr, "'")
|
|
|
|
|
|
|
|
var tags []string
|
|
|
|
if strings.Contains(tagsStr, "&") {
|
|
|
|
tags = strings.Split(tagsStr, "&")
|
|
|
|
} else {
|
|
|
|
tags = append(tags, tagsStr)
|
|
|
|
}
|
|
|
|
|
|
|
|
var tmTags []string
|
|
|
|
for _, tag := range tags {
|
|
|
|
if !strings.Contains(tag, ":") {
|
|
|
|
return fmt.Errorf("%s should be of the format <key>:<value>", tagsStr)
|
|
|
|
} else if strings.Count(tag, ":") > 1 {
|
|
|
|
return fmt.Errorf("%s should only contain one <key>:<value> pair", tagsStr)
|
|
|
|
}
|
|
|
|
|
|
|
|
keyValue := strings.Split(tag, ":")
|
|
|
|
if keyValue[0] == types.TxHeightKey {
|
|
|
|
tag = fmt.Sprintf("%s=%s", keyValue[0], keyValue[1])
|
|
|
|
} else {
|
|
|
|
tag = fmt.Sprintf("%s='%s'", keyValue[0], keyValue[1])
|
|
|
|
}
|
|
|
|
tmTags = append(tmTags, tag)
|
|
|
|
}
|
|
|
|
|
|
|
|
page := viper.GetInt(flagPage)
|
|
|
|
limit := viper.GetInt(flagLimit)
|
|
|
|
|
|
|
|
cliCtx := context.NewCLIContext().WithCodec(cdc)
|
|
|
|
txs, err := SearchTxs(cliCtx, cdc, tmTags, page, limit)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var output []byte
|
|
|
|
if cliCtx.Indent {
|
|
|
|
output, err = cdc.MarshalJSONIndent(txs, "", " ")
|
|
|
|
} else {
|
|
|
|
output, err = cdc.MarshalJSON(txs)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println(string(output))
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-05-28 01:44:04 -07:00
|
|
|
cmd.Flags().StringP(flags.FlagNode, "n", "tcp://localhost:26657", "Node to connect to")
|
|
|
|
viper.BindPFlag(flags.FlagNode, cmd.Flags().Lookup(flags.FlagNode))
|
|
|
|
cmd.Flags().Bool(flags.FlagTrustNode, false, "Trust connected full node (don't verify proofs for responses)")
|
|
|
|
viper.BindPFlag(flags.FlagTrustNode, cmd.Flags().Lookup(flags.FlagTrustNode))
|
2019-04-02 18:09:37 -07:00
|
|
|
|
|
|
|
cmd.Flags().String(flagTags, "", "tag:value list of tags that must match")
|
|
|
|
cmd.Flags().Uint32(flagPage, rest.DefaultPage, "Query a specific page of paginated results")
|
|
|
|
cmd.Flags().Uint32(flagLimit, rest.DefaultLimit, "Query number of transactions results per page returned")
|
|
|
|
cmd.MarkFlagRequired(flagTags)
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
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]",
|
2019-04-02 18:09:37 -07:00
|
|
|
Short: "Find a transaction by hash in a committed block.",
|
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 {
|
2018-08-06 11:11:30 -07:00
|
|
|
cliCtx := context.NewCLIContext().WithCodec(cdc)
|
|
|
|
|
2019-02-15 08:00:41 -08:00
|
|
|
output, err := queryTx(cdc, cliCtx, args[0])
|
2018-04-18 21:49:24 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-02-15 08:00:41 -08:00
|
|
|
if output.Empty() {
|
|
|
|
return fmt.Errorf("No transaction found with hash %s", args[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
return cliCtx.PrintOutput(output)
|
2018-04-18 21:49:24 -07:00
|
|
|
},
|
2018-02-23 04:53:49 -08:00
|
|
|
}
|
2018-04-18 21:49:24 -07:00
|
|
|
|
2019-05-28 01:44:04 -07:00
|
|
|
cmd.Flags().StringP(flags.FlagNode, "n", "tcp://localhost:26657", "Node to connect to")
|
|
|
|
viper.BindPFlag(flags.FlagNode, cmd.Flags().Lookup(flags.FlagNode))
|
|
|
|
cmd.Flags().Bool(flags.FlagTrustNode, false, "Trust connected full node (don't verify proofs for responses)")
|
|
|
|
viper.BindPFlag(flags.FlagTrustNode, cmd.Flags().Lookup(flags.FlagTrustNode))
|
2019-04-02 18:09:37 -07:00
|
|
|
|
2018-02-23 04:53:49 -08:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2019-04-02 18:09:37 -07:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// REST
|
|
|
|
// ----------------------------------------------------------------------------
|
2018-02-23 04:53:49 -08:00
|
|
|
|
2019-04-02 18:09:37 -07:00
|
|
|
// QueryTxsByTagsRequestHandlerFn implements a REST handler that searches for
|
|
|
|
// transactions by tags.
|
|
|
|
func QueryTxsByTagsRequestHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var (
|
|
|
|
tags []string
|
|
|
|
txs []sdk.TxResponse
|
|
|
|
page, limit int
|
|
|
|
)
|
2018-08-06 11:11:30 -07:00
|
|
|
|
2019-04-02 18:09:37 -07:00
|
|
|
err := r.ParseForm()
|
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest,
|
|
|
|
sdk.AppendMsgToErr("could not parse query parameters", err.Error()))
|
|
|
|
return
|
2018-09-14 11:41:21 -07:00
|
|
|
}
|
|
|
|
|
2019-04-02 18:09:37 -07:00
|
|
|
if len(r.Form) == 0 {
|
|
|
|
rest.PostProcessResponse(w, cdc, txs, cliCtx.Indent)
|
|
|
|
return
|
|
|
|
}
|
2018-02-23 04:53:49 -08:00
|
|
|
|
2019-04-02 18:09:37 -07:00
|
|
|
tags, page, limit, err = rest.ParseHTTPArgs(r)
|
2019-02-15 08:00:41 -08:00
|
|
|
if err != nil {
|
2019-04-02 18:09:37 -07:00
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
|
|
return
|
2019-02-15 08:00:41 -08:00
|
|
|
}
|
2019-04-02 18:09:37 -07:00
|
|
|
|
2019-05-04 04:09:03 -07:00
|
|
|
searchResult, err := SearchTxs(cliCtx, cdc, tags, page, limit)
|
2019-02-15 08:00:41 -08:00
|
|
|
if err != nil {
|
2019-04-02 18:09:37 -07:00
|
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
2019-02-15 08:00:41 -08:00
|
|
|
}
|
2018-09-14 11:41:21 -07:00
|
|
|
|
2019-05-04 04:09:03 -07:00
|
|
|
rest.PostProcessResponse(w, cdc, searchResult, cliCtx.Indent)
|
2018-02-23 04:53:49 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-02 18:09:37 -07:00
|
|
|
// QueryTxRequestHandlerFn implements a REST handler that queries a transaction
|
|
|
|
// by hash in a committed block.
|
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"]
|
|
|
|
|
2018-09-14 11:41:21 -07:00
|
|
|
output, err := queryTx(cdc, cliCtx, hashHexStr)
|
2018-02-28 23:16:54 -08:00
|
|
|
if err != nil {
|
2019-02-15 08:00:41 -08:00
|
|
|
if strings.Contains(err.Error(), hashHexStr) {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusNotFound, err.Error())
|
|
|
|
return
|
|
|
|
}
|
2019-02-04 07:48:26 -08:00
|
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
2018-02-28 23:16:54 -08:00
|
|
|
return
|
|
|
|
}
|
2019-02-15 08:00:41 -08:00
|
|
|
|
|
|
|
if output.Empty() {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusNotFound, fmt.Sprintf("no transaction found with hash %s", hashHexStr))
|
|
|
|
}
|
|
|
|
|
2019-02-04 07:48:26 -08:00
|
|
|
rest.PostProcessResponse(w, cdc, output, cliCtx.Indent)
|
2018-02-28 23:16:54 -08:00
|
|
|
}
|
|
|
|
}
|