Skip to content

Commit

Permalink
Query Deposit Records by Host Zone (#631)
Browse files Browse the repository at this point in the history
  • Loading branch information
sampocs authored Mar 8, 2023
1 parent b7371d7 commit 9b17edf
Show file tree
Hide file tree
Showing 7 changed files with 665 additions and 70 deletions.
14 changes: 13 additions & 1 deletion proto/stride/records/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,13 @@ service Query {
returns (QueryAllDepositRecordResponse) {
option (google.api.http).get = "/Stride-Labs/stride/records/deposit_record";
}
// this line is used by starport scaffolding # 2

// Queries a list of DepositRecord items for a given host zone
rpc DepositRecordByHost(QueryDepositRecordByHostRequest)
returns (QueryDepositRecordByHostResponse) {
option (google.api.http).get = "/Stride-Labs/stride/records/"
"deposit_record_by_host_zone/{host_zone_id}";
}
}

// QueryParamsRequest is request type for the Query/Params RPC method.
Expand All @@ -92,6 +98,12 @@ message QueryAllDepositRecordResponse {
cosmos.base.query.v1beta1.PageResponse pagination = 2;
}

message QueryDepositRecordByHostRequest { string host_zone_id = 1; }

message QueryDepositRecordByHostResponse {
repeated DepositRecord deposit_record = 1 [ (gogoproto.nullable) = false ];
}

message QueryGetUserRedemptionRecordRequest { string id = 1; }

message QueryGetUserRedemptionRecordResponse {
Expand Down
1 change: 1 addition & 0 deletions x/records/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func GetQueryCmd(queryRoute string) *cobra.Command {
cmd.AddCommand(CmdShowEpochUnbondingRecord())
cmd.AddCommand(CmdListDepositRecord())
cmd.AddCommand(CmdShowDepositRecord())
cmd.AddCommand(CmdListDepositRecordByHost())
// this line is used by starport scaffolding # 1

return cmd
Expand Down
27 changes: 27 additions & 0 deletions x/records/client/cli/query_deposit_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,30 @@ func CmdShowDepositRecord() *cobra.Command {

return cmd
}

func CmdListDepositRecordByHost() *cobra.Command {
cmd := &cobra.Command{
Use: "deposit-records-by-host [host]",
Short: "list all depositRecords for a given host zone",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
hostZoneId := args[0]

clientCtx := client.GetClientContextFromCmd(cmd)
queryClient := types.NewQueryClient(clientCtx)

res, err := queryClient.DepositRecordByHost(context.Background(), &types.QueryDepositRecordByHostRequest{
HostZoneId: hostZoneId,
})
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}
17 changes: 17 additions & 0 deletions x/records/keeper/grpc_query_deposit_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,20 @@ func (k Keeper) DepositRecord(c context.Context, req *types.QueryGetDepositRecor

return &types.QueryGetDepositRecordResponse{DepositRecord: depositRecord}, nil
}

func (k Keeper) DepositRecordByHost(c context.Context, req *types.QueryDepositRecordByHostRequest) (*types.QueryDepositRecordByHostResponse, error) {
ctx := sdk.UnwrapSDKContext(c)

if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}

var depositRecordsForHost []types.DepositRecord
for _, depositRecord := range k.GetAllDepositRecord(ctx) {
if depositRecord.HostZoneId == req.HostZoneId {
depositRecordsForHost = append(depositRecordsForHost, depositRecord)
}
}

return &types.QueryDepositRecordByHostResponse{DepositRecord: depositRecordsForHost}, nil
}
43 changes: 43 additions & 0 deletions x/records/keeper/grpc_query_deposit_record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,46 @@ func TestDepositRecordQueryPaginated(t *testing.T) {
require.ErrorIs(t, err, status.Error(codes.InvalidArgument, "invalid request"))
})
}

func (s *KeeperTestSuite) TestQueryDepositRecordByHost() {
wctx := sdk.WrapSDKContext(s.Ctx)

// Store deposit records across two hosts
hostChain1 := "chain-1"
hostChain2 := "chain-2"

hostDepositRecords1 := []types.DepositRecord{
{HostZoneId: hostChain1, Id: 1, Amount: sdk.NewInt(1)},
{HostZoneId: hostChain1, Id: 2, Amount: sdk.NewInt(2)},
{HostZoneId: hostChain1, Id: 3, Amount: sdk.NewInt(3)},
}
hostDepositRecords2 := []types.DepositRecord{
{HostZoneId: hostChain2, Id: 4, Amount: sdk.NewInt(4)},
{HostZoneId: hostChain2, Id: 5, Amount: sdk.NewInt(5)},
{HostZoneId: hostChain2, Id: 6, Amount: sdk.NewInt(6)},
}

for _, depositRecord := range append(hostDepositRecords1, hostDepositRecords2...) {
s.App.RecordsKeeper.SetDepositRecord(s.Ctx, depositRecord)
}

// Fetch each list through a host zone id query
actualHostDepositRecords1, err := s.App.RecordsKeeper.DepositRecordByHost(wctx, &types.QueryDepositRecordByHostRequest{
HostZoneId: hostChain1,
})
s.Require().NoError(err, "no error expected when querying by host %s", hostChain1)
s.Require().ElementsMatch(hostDepositRecords1, actualHostDepositRecords1.DepositRecord, "deposit records for %s", hostChain1)

actualHostDepositRecords2, err := s.App.RecordsKeeper.DepositRecordByHost(wctx, &types.QueryDepositRecordByHostRequest{
HostZoneId: hostChain2,
})
s.Require().NoError(err, "no error expected when querying by host %s", hostChain2)
s.Require().ElementsMatch(hostDepositRecords2, actualHostDepositRecords2.DepositRecord, "deposit records for %s", hostChain2)

// Finally, fetch a non-existent chain-id and it should return an empty list
fakeHostDepositRecords, err := s.App.RecordsKeeper.DepositRecordByHost(wctx, &types.QueryDepositRecordByHostRequest{
HostZoneId: "fake_host",
})
s.Require().NoError(err, "no error expected when querying by host %s", hostChain1)
s.Require().Len(fakeHostDepositRecords.DepositRecord, 0)
}
Loading

0 comments on commit 9b17edf

Please sign in to comment.