Skip to content

Commit

Permalink
fix query findings (#796)
Browse files Browse the repository at this point in the history
* fix query findings
  • Loading branch information
skyargos authored Nov 20, 2023
1 parent 0bbdc32 commit 3a7e28a
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 35 deletions.
18 changes: 13 additions & 5 deletions x/bounty/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,19 +171,24 @@ func GetCmdQueryFindings() *cobra.Command {
fmt.Sprintf(`Query for a all paginated findings that match optional filters.
Example:
$ %s query bounty findings
$ %s query bounty findings --program-id 1
$ %s query bounty findings --submitter-address cosmos1skjwj5whet0lpe65qaq4rpq03hjxlwd9nf39lk
$ %s query bounty findings --page=1 --limit=100
`,
version.AppName, version.AppName, version.AppName, version.AppName,
version.AppName, version.AppName, version.AppName,
),
),
RunE: func(cmd *cobra.Command, args []string) error {
// validate that the program-id is an uint
pid, _ := cmd.Flags().GetString(FlagProgramID)
// validate that the program-id
pid, err := cmd.Flags().GetString(FlagProgramID)
if err != nil {
return err
}

submitterAddr, _ := cmd.Flags().GetString(FlagSubmitterAddress)
submitterAddr, err := cmd.Flags().GetString(FlagSubmitterAddress)
if err != nil {
return err
}
if len(submitterAddr) != 0 {
_ = sdk.MustAccAddressFromBech32(submitterAddr)
}
Expand All @@ -207,6 +212,9 @@ $ %s query bounty findings --page=1 --limit=100
req.ProgramId = pid
}

if len(req.ProgramId) == 0 && len(req.SubmitterAddress) == 0 {
return fmt.Errorf("invalid request")
}
res, err := queryClient.Findings(cmd.Context(), req)
if err != nil {
return err
Expand Down
21 changes: 21 additions & 0 deletions x/bounty/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ func (k Keeper) Findings(c context.Context, req *types.QueryFindingsRequest) (*t
var queryFindings types.Findings
ctx := sdk.UnwrapSDKContext(c)

if len(req.ProgramId) == 0 && len(req.SubmitterAddress) == 0 {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}

store := ctx.KVStore(k.storeKey)
programStore := prefix.NewStore(store, types.FindingKey)

Expand All @@ -76,6 +80,23 @@ func (k Keeper) Findings(c context.Context, req *types.QueryFindingsRequest) (*t
if err := k.cdc.Unmarshal(value, &finding); err != nil {
return false, status.Error(codes.Internal, err.Error())
}

switch {
case len(req.ProgramId) != 0 && len(req.SubmitterAddress) != 0:
if finding.ProgramId == req.ProgramId && finding.SubmitterAddress == req.SubmitterAddress {
queryFindings = append(queryFindings, finding)
}
case len(req.ProgramId) != 0:
if finding.ProgramId == req.ProgramId {
queryFindings = append(queryFindings, finding)

}
case len(req.SubmitterAddress) != 0:
if finding.SubmitterAddress == req.SubmitterAddress {
queryFindings = append(queryFindings, finding)
}
}

return true, nil
})
if err != nil {
Expand Down
78 changes: 48 additions & 30 deletions x/bounty/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package keeper_test

import (
"context"
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/google/uuid"

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

"github.com/shentufoundation/shentu/v2/x/bounty/types"
)

Expand Down Expand Up @@ -57,8 +57,8 @@ func (suite *KeeperTestSuite) TestGRPCQueryProgram() {
for _, testCase := range testCases {
suite.Run(fmt.Sprintf("Case %s", testCase.msg), func() {
testCase.malleate()

programRes, err := queryClient.Program(context.Background(), req)
ctx := sdk.WrapSDKContext(suite.ctx)
programRes, err := queryClient.Program(ctx, req)

if testCase.expPass {
suite.Require().NoError(err)
Expand Down Expand Up @@ -100,8 +100,8 @@ func (suite *KeeperTestSuite) TestGRPCQueryPrograms() {
for _, testCase := range testCases {
suite.Run(fmt.Sprintf("Case %s", testCase.msg), func() {
testCase.malleate()

programRes, err := queryClient.Programs(context.Background(), req)
ctx := sdk.WrapSDKContext(suite.ctx)
programRes, err := queryClient.Programs(ctx, req)

if testCase.expPass {
suite.Require().NoError(err)
Expand Down Expand Up @@ -175,8 +175,8 @@ func (suite *KeeperTestSuite) TestGRPCQueryFinding() {
for _, testCase := range testCases {
suite.Run(fmt.Sprintf("Case %s", testCase.msg), func() {
testCase.malleate()

findingRes, err := queryClient.Finding(context.Background(), req)
ctx := sdk.WrapSDKContext(suite.ctx)
findingRes, err := queryClient.Finding(ctx, req)
if testCase.expPass {
suite.Require().NoError(err)
} else {
Expand All @@ -191,50 +191,68 @@ func (suite *KeeperTestSuite) TestGRPCQueryFinding() {
func (suite *KeeperTestSuite) TestGRPCQueryFindings() {
queryClient := suite.queryClient

// create programs
pid := uuid.NewString()
pid, fid := uuid.NewString(), uuid.NewString()
suite.InitCreateProgram(pid)
suite.InitActivateProgram(pid)

var (
req *types.QueryFindingsRequest
)
suite.InitSubmitFinding(pid, fid)
suite.InitSubmitFinding(pid, uuid.NewString())

testCases := []struct {
msg string
malleate func()
expPass bool
msg string
req *types.QueryFindingsRequest
expResultLen int
expPass bool
}{
{
"valid request",
func() {
req = &types.QueryFindingsRequest{ProgramId: pid}
suite.InitSubmitFinding(pid, "1")
},
"invalid request",
&types.QueryFindingsRequest{},
0,
false,
},
{
"valid request => piq and submitter address",
&types.QueryFindingsRequest{ProgramId: pid, SubmitterAddress: suite.whiteHatAddr.String()},
2,
true,
},
{
"valid request with submitter address",
func() {
req = &types.QueryFindingsRequest{ProgramId: pid, SubmitterAddress: suite.address[0].String()}
},
"valid request => piq",
&types.QueryFindingsRequest{ProgramId: pid},
2,
true,
},
{
"valid request => submitter address",
&types.QueryFindingsRequest{SubmitterAddress: suite.whiteHatAddr.String()},
2,
true,
},
{
"valid request => invalid pid",
&types.QueryFindingsRequest{ProgramId: "not exist"},
0,
true,
},
{
"valid request => invalid submitter address",
&types.QueryFindingsRequest{SubmitterAddress: suite.normalAddr.String()},
0,
true,
},
}

for _, testCase := range testCases {
suite.Run(fmt.Sprintf("Case %s", testCase.msg), func() {
testCase.malleate()

findingRes, err := queryClient.Findings(context.Background(), req)
ctx := sdk.WrapSDKContext(suite.ctx)
findingRes, err := queryClient.Findings(ctx, testCase.req)

if testCase.expPass {
suite.Require().NoError(err)
suite.Require().Equal(len(findingRes.Findings), testCase.expResultLen)
} else {
suite.Require().Error(err)
suite.Require().Nil(findingRes)
}
})
}

}

0 comments on commit 3a7e28a

Please sign in to comment.