diff --git a/x/bounty/client/cli/query.go b/x/bounty/client/cli/query.go index a0324243e..abd7e3d9e 100644 --- a/x/bounty/client/cli/query.go +++ b/x/bounty/client/cli/query.go @@ -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) } @@ -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 diff --git a/x/bounty/keeper/grpc_query.go b/x/bounty/keeper/grpc_query.go index f2af226ba..47e14098d 100644 --- a/x/bounty/keeper/grpc_query.go +++ b/x/bounty/keeper/grpc_query.go @@ -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) @@ -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 { diff --git a/x/bounty/keeper/grpc_query_test.go b/x/bounty/keeper/grpc_query_test.go index bfd59d6d4..126e65a1a 100644 --- a/x/bounty/keeper/grpc_query_test.go +++ b/x/bounty/keeper/grpc_query_test.go @@ -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" ) @@ -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) @@ -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) @@ -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 { @@ -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) } }) } - }