cosmos-sdk/x/evidence/client/cli/query.go
Likhita Polavarapu 2bb114acec
fix: Evidence API does not decode the hash properly (#13740)
## Description

Closes: #13444 

This PR fixes the x/evidence api query which is not decoding the evidence hash properly because of its type ([]byte). We fix it with the following steps:
1. Adds a new proto field `hash` of type `string`
2. Make the old `evidence_hash` field deprecated
3. Updates query handler 

---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed 
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
2022-11-04 10:08:44 +00:00

91 lines
2.3 KiB
Go

package cli
import (
"context"
"fmt"
"strings"
"github.com/spf13/cobra"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/cosmos/cosmos-sdk/version"
"github.com/cosmos/cosmos-sdk/x/evidence/types"
)
// GetQueryCmd returns the CLI command with all evidence module query commands
// mounted.
func GetQueryCmd() *cobra.Command {
cmd := &cobra.Command{
Use: types.ModuleName,
Short: "Query for evidence by hash or for all (paginated) submitted evidence",
Long: strings.TrimSpace(
fmt.Sprintf(`Query for specific submitted evidence by hash or query for all (paginated) evidence:
Example:
$ %s query %s DF0C23E8634E480F84B9D5674A7CDC9816466DEC28A3358F73260F68D28D7660
$ %s query %s --page=2 --limit=50
`,
version.AppName, types.ModuleName, version.AppName, types.ModuleName,
),
),
Args: cobra.MaximumNArgs(1),
SuggestionsMinimumDistance: 2,
RunE: QueryEvidenceCmd(),
}
flags.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, "evidence")
return cmd
}
// QueryEvidenceCmd returns the command handler for evidence querying. Evidence
// can be queried for by hash or paginated evidence can be returned.
func QueryEvidenceCmd() func(*cobra.Command, []string) error {
return func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
if len(args) > 0 {
return queryEvidence(clientCtx, args[0])
}
pageReq, err := client.ReadPageRequest(cmd.Flags())
if err != nil {
return err
}
return queryAllEvidence(clientCtx, pageReq)
}
}
func queryEvidence(clientCtx client.Context, hash string) error {
queryClient := types.NewQueryClient(clientCtx)
params := &types.QueryEvidenceRequest{Hash: hash}
res, err := queryClient.Evidence(context.Background(), params)
if err != nil {
return err
}
return clientCtx.PrintProto(res.Evidence)
}
func queryAllEvidence(clientCtx client.Context, pageReq *query.PageRequest) error {
queryClient := types.NewQueryClient(clientCtx)
params := &types.QueryAllEvidenceRequest{
Pagination: pageReq,
}
res, err := queryClient.AllEvidence(context.Background(), params)
if err != nil {
return err
}
return clientCtx.PrintProto(res)
}