Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Evidence API does not decode the hash properly #13740

Merged
merged 12 commits into from
Nov 4, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
202 changes: 140 additions & 62 deletions api/cosmos/evidence/v1beta1/query.pulsar.go

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions proto/cosmos/evidence/v1beta1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ option go_package = "github.com/cosmos/cosmos-sdk/x/evidence/types";
service Query {
// Evidence queries evidence based on evidence hash.
rpc Evidence(QueryEvidenceRequest) returns (QueryEvidenceResponse) {
option (google.api.http).get = "/cosmos/evidence/v1beta1/evidence/{evidence_hash}";
option (google.api.http).get = "/cosmos/evidence/v1beta1/evidence/{hash}";
}

// AllEvidence queries all evidence.
Expand All @@ -23,8 +23,12 @@ service Query {

// QueryEvidenceRequest is the request type for the Query/Evidence RPC method.
message QueryEvidenceRequest {
// Deprecated: Prefer to use hash instead.
// evidence_hash defines the hash of the requested evidence.
likhita-809 marked this conversation as resolved.
Show resolved Hide resolved
bytes evidence_hash = 1 [(gogoproto.casttype) = "github.com/tendermint/tendermint/libs/bytes.HexBytes"];
bytes evidence_hash = 1 [deprecated = true, (gogoproto.casttype) = "github.com/tendermint/tendermint/libs/bytes.HexBytes"];

// hash defines the evidence hash of the requested evidence.
string hash = 2;
}

// QueryEvidenceResponse is the response type for the Query/Evidence RPC method.
Expand Down
8 changes: 1 addition & 7 deletions x/evidence/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cli

import (
"context"
"encoding/hex"
"fmt"
"strings"

Expand Down Expand Up @@ -64,14 +63,9 @@ func QueryEvidenceCmd() func(*cobra.Command, []string) error {
}

func queryEvidence(clientCtx client.Context, hash string) error {
decodedHash, err := hex.DecodeString(hash)
if err != nil {
return fmt.Errorf("invalid evidence hash: %w", err)
}

queryClient := types.NewQueryClient(clientCtx)

params := &types.QueryEvidenceRequest{EvidenceHash: decodedHash}
params := &types.QueryEvidenceRequest{Hash: hash}
res, err := queryClient.Evidence(context.Background(), params)
if err != nil {
return err
Expand Down
15 changes: 11 additions & 4 deletions x/evidence/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package keeper

import (
"context"
"encoding/hex"
"fmt"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand All @@ -24,15 +26,20 @@ func (k Keeper) Evidence(c context.Context, req *types.QueryEvidenceRequest) (*t
return nil, status.Errorf(codes.InvalidArgument, "empty request")
}

if req.EvidenceHash == nil {
return nil, status.Errorf(codes.InvalidArgument, "invalid hash")
if req.Hash == "" {
return nil, status.Errorf(codes.InvalidArgument, "invalid request\nhash is empty")
likhita-809 marked this conversation as resolved.
Show resolved Hide resolved
}

ctx := sdk.UnwrapSDKContext(c)

evidence, _ := k.GetEvidence(ctx, req.EvidenceHash)
decodedHash, err := hex.DecodeString(req.Hash)
if err != nil {
return nil, fmt.Errorf("invalid evidence hash: %w", err)
}

evidence, _ := k.GetEvidence(ctx, decodedHash)
if evidence == nil {
return nil, status.Errorf(codes.NotFound, "evidence %s not found", req.EvidenceHash)
return nil, status.Errorf(codes.NotFound, "evidence %s not found", req.Hash)
}

msg, ok := evidence.(proto.Message)
Expand Down
6 changes: 2 additions & 4 deletions x/evidence/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/cosmos/cosmos-sdk/x/evidence/exported"
"github.com/cosmos/cosmos-sdk/x/evidence/types"

tmbytes "github.com/tendermint/tendermint/libs/bytes"
)

func (suite *KeeperTestSuite) TestQueryEvidence() {
Expand All @@ -34,7 +32,7 @@ func (suite *KeeperTestSuite) TestQueryEvidence() {
{
"invalid request with empty evidence hash",
func() {
req = &types.QueryEvidenceRequest{EvidenceHash: tmbytes.HexBytes{}}
req = &types.QueryEvidenceRequest{Hash: ""}
},
false,
func(res *types.QueryEvidenceResponse) {},
Expand All @@ -44,7 +42,7 @@ func (suite *KeeperTestSuite) TestQueryEvidence() {
func() {
numEvidence := 100
evidence = suite.populateEvidence(suite.ctx, numEvidence)
req = types.NewQueryEvidenceRequest(evidence[0].Hash())
req = types.NewQueryEvidenceRequest(evidence[0].Hash().String())
},
true,
func(res *types.QueryEvidenceResponse) {
Expand Down
2 changes: 1 addition & 1 deletion x/evidence/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package keeper
import (
"fmt"

storetypes "github.com/cosmos/cosmos-sdk/store/types"
tmbytes "github.com/tendermint/tendermint/libs/bytes"
"github.com/tendermint/tendermint/libs/log"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/store/prefix"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/evidence/exported"
Expand Down
6 changes: 2 additions & 4 deletions x/evidence/types/querier.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package types

import (
tmbytes "github.com/tendermint/tendermint/libs/bytes"

query "github.com/cosmos/cosmos-sdk/types/query"
)

Expand All @@ -13,8 +11,8 @@ const (
)

// NewQueryEvidenceRequest creates a new instance of QueryEvidenceRequest.
func NewQueryEvidenceRequest(hash tmbytes.HexBytes) *QueryEvidenceRequest {
return &QueryEvidenceRequest{EvidenceHash: hash}
func NewQueryEvidenceRequest(hash string) *QueryEvidenceRequest {
return &QueryEvidenceRequest{Hash: hash}
}

// NewQueryAllEvidenceRequest creates a new instance of QueryAllEvidenceRequest.
Expand Down
119 changes: 87 additions & 32 deletions x/evidence/types/query.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 27 additions & 9 deletions x/evidence/types/query.pb.gw.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.