From fa008549862581b2c6838d6c9418d14564a93655 Mon Sep 17 00:00:00 2001 From: Hanjun Kim Date: Thu, 2 Jan 2025 16:19:59 +0900 Subject: [PATCH 1/3] fix: filter unbonding delegations by requested target id --- x/restaking/keeper/grpc_query.go | 12 +- x/restaking/keeper/grpc_query_test.go | 188 ++++++++++++++++++++++++++ 2 files changed, 197 insertions(+), 3 deletions(-) diff --git a/x/restaking/keeper/grpc_query.go b/x/restaking/keeper/grpc_query.go index 4ec5a028..02579f74 100644 --- a/x/restaking/keeper/grpc_query.go +++ b/x/restaking/keeper/grpc_query.go @@ -255,7 +255,9 @@ func (k Querier) PoolUnbondingDelegations(ctx context.Context, req *types.QueryP if err != nil { return err } - unbondingDelegations = append(unbondingDelegations, unbond) + if unbond.TargetID == req.PoolId { + unbondingDelegations = append(unbondingDelegations, unbond) + } return nil }) if err != nil { @@ -394,7 +396,9 @@ func (k Querier) OperatorUnbondingDelegations(ctx context.Context, req *types.Qu if err != nil { return err } - unbondingDelegations = append(unbondingDelegations, unbond) + if unbond.TargetID == req.OperatorId { + unbondingDelegations = append(unbondingDelegations, unbond) + } return nil }) if err != nil { @@ -533,7 +537,9 @@ func (k Querier) ServiceUnbondingDelegations(ctx context.Context, req *types.Que if err != nil { return err } - unbondingDelegations = append(unbondingDelegations, unbond) + if unbond.TargetID == req.ServiceId { + unbondingDelegations = append(unbondingDelegations, unbond) + } return nil }) if err != nil { diff --git a/x/restaking/keeper/grpc_query_test.go b/x/restaking/keeper/grpc_query_test.go index 68b2ed2e..a0df5dde 100644 --- a/x/restaking/keeper/grpc_query_test.go +++ b/x/restaking/keeper/grpc_query_test.go @@ -581,6 +581,64 @@ func (suite *KeeperTestSuite) TestQuerier_PoolUnbondingDelegations() { ), }, }, + { + name: "query with pool id returns data properly", + store: func(ctx sdk.Context) { + pool1 := poolstypes.Pool{ + ID: 1, + Denom: "umilk", + Address: poolstypes.GetPoolAddress(1).String(), + Tokens: sdkmath.NewInt(150), + DelegatorShares: sdkmath.LegacyNewDec(150), + } + err := suite.pk.SavePool(ctx, pool1) + suite.Require().NoError(err) + pool2 := poolstypes.Pool{ + ID: 2, + Denom: "utia", + Address: poolstypes.GetPoolAddress(2).String(), + Tokens: sdkmath.NewInt(150), + DelegatorShares: sdkmath.LegacyNewDec(150), + } + err = suite.pk.SavePool(ctx, pool2) + suite.Require().NoError(err) + + _, err = suite.k.SetUnbondingDelegationEntry(ctx, + types.UndelegationData{ + Target: pool1, + Delegator: "cosmos13t6y2nnugtshwuy0zkrq287a95lyy8vzleaxmd", + BuildUnbondingDelegation: types.NewPoolUnbondingDelegation, + }, + 10, + time.Date(2024, 1, 1, 12, 0, 0, 0, time.UTC), + sdk.NewCoins(sdk.NewCoin("umilk", sdkmath.NewInt(100))), + ) + suite.Require().NoError(err) + _, err = suite.k.SetUnbondingDelegationEntry(ctx, + types.UndelegationData{ + Target: pool2, + Delegator: "cosmos13t6y2nnugtshwuy0zkrq287a95lyy8vzleaxmd", + BuildUnbondingDelegation: types.NewPoolUnbondingDelegation, + }, + 15, + time.Date(2024, 1, 1, 12, 0, 0, 0, time.UTC), + sdk.NewCoins(sdk.NewCoin("utia", sdkmath.NewInt(50))), + ) + suite.Require().NoError(err) + }, + request: types.NewQueryPoolUnbondingDelegationsRequest(2, nil), + shouldErr: false, + expUnbondingDelegations: []types.UnbondingDelegation{ + types.NewPoolUnbondingDelegation( + "cosmos13t6y2nnugtshwuy0zkrq287a95lyy8vzleaxmd", + 2, + 15, + time.Date(2024, 1, 1, 12, 0, 0, 0, time.UTC), + sdk.NewCoins(sdk.NewCoin("utia", sdkmath.NewInt(50))), + 2, + ), + }, + }, } for _, tc := range testCases { @@ -1056,6 +1114,71 @@ func (suite *KeeperTestSuite) TestQuerier_OperatorUnbondingDelegations() { ), }, }, + { + name: "query with operator id returns data properly", + store: func(ctx sdk.Context) { + operator1 := operatorstypes.Operator{ + ID: 1, + Address: operatorstypes.GetOperatorAddress(1).String(), + Tokens: sdk.NewCoins( + sdk.NewCoin("umilk", sdkmath.NewInt(150)), + ), + DelegatorShares: sdk.NewDecCoins( + sdk.NewDecCoinFromDec("operators/1/umilk", sdkmath.LegacyNewDec(150)), + ), + } + err := suite.ok.SaveOperator(ctx, operator1) + suite.Require().NoError(err) + operator2 := operatorstypes.Operator{ + ID: 2, + Address: operatorstypes.GetOperatorAddress(2).String(), + Tokens: sdk.NewCoins( + sdk.NewCoin("umilk", sdkmath.NewInt(150)), + ), + DelegatorShares: sdk.NewDecCoins( + sdk.NewDecCoinFromDec("operators/2/umilk", sdkmath.LegacyNewDec(150)), + ), + } + err = suite.ok.SaveOperator(ctx, operator2) + suite.Require().NoError(err) + + _, err = suite.k.SetUnbondingDelegationEntry(ctx, + types.UndelegationData{ + Target: operator1, + Delegator: "cosmos13t6y2nnugtshwuy0zkrq287a95lyy8vzleaxmd", + BuildUnbondingDelegation: types.NewOperatorUnbondingDelegation, + }, + 10, + time.Date(2024, 1, 1, 12, 0, 0, 0, time.UTC), + sdk.NewCoins(sdk.NewCoin("umilk", sdkmath.NewInt(100))), + ) + suite.Require().NoError(err) + + _, err = suite.k.SetUnbondingDelegationEntry(ctx, + types.UndelegationData{ + Target: operator2, + Delegator: "cosmos1d03wa9qd8flfjtvldndw5csv94tvg5hzfcmcgn", + BuildUnbondingDelegation: types.NewOperatorUnbondingDelegation, + }, + 20, + time.Date(2024, 1, 2, 12, 0, 0, 0, time.UTC), + sdk.NewCoins(sdk.NewCoin("umilk", sdkmath.NewInt(50))), + ) + suite.Require().NoError(err) + }, + request: types.NewQueryOperatorUnbondingDelegationsRequest(2, nil), + shouldErr: false, + expUnbondingDelegations: []types.UnbondingDelegation{ + types.NewOperatorUnbondingDelegation( + "cosmos1d03wa9qd8flfjtvldndw5csv94tvg5hzfcmcgn", + 2, + 20, + time.Date(2024, 1, 2, 12, 0, 0, 0, time.UTC), + sdk.NewCoins(sdk.NewCoin("umilk", sdkmath.NewInt(50))), + 2, + ), + }, + }, } for _, tc := range testCases { @@ -1535,6 +1658,71 @@ func (suite *KeeperTestSuite) TestQuerier_ServiceUnbondingDelegations() { ), }, }, + { + name: "query with service id returns data properly", + store: func(ctx sdk.Context) { + service1 := servicestypes.Service{ + ID: 1, + Address: servicestypes.GetServiceAddress(1).String(), + Tokens: sdk.NewCoins( + sdk.NewCoin("umilk", sdkmath.NewInt(150)), + ), + DelegatorShares: sdk.NewDecCoins( + sdk.NewDecCoinFromDec("services/1/umilk", sdkmath.LegacyNewDec(150)), + ), + } + err := suite.sk.SaveService(ctx, service1) + suite.Require().NoError(err) + service2 := servicestypes.Service{ + ID: 2, + Address: servicestypes.GetServiceAddress(2).String(), + Tokens: sdk.NewCoins( + sdk.NewCoin("umilk", sdkmath.NewInt(150)), + ), + DelegatorShares: sdk.NewDecCoins( + sdk.NewDecCoinFromDec("services/2/umilk", sdkmath.LegacyNewDec(150)), + ), + } + err = suite.sk.SaveService(ctx, service2) + suite.Require().NoError(err) + + _, err = suite.k.SetUnbondingDelegationEntry(ctx, + types.UndelegationData{ + Target: service1, + Delegator: "cosmos13t6y2nnugtshwuy0zkrq287a95lyy8vzleaxmd", + BuildUnbondingDelegation: types.NewServiceUnbondingDelegation, + }, + 10, + time.Date(2024, 1, 1, 12, 0, 0, 0, time.UTC), + sdk.NewCoins(sdk.NewCoin("umilk", sdkmath.NewInt(100))), + ) + suite.Require().NoError(err) + + _, err = suite.k.SetUnbondingDelegationEntry(ctx, + types.UndelegationData{ + Target: service2, + Delegator: "cosmos1d03wa9qd8flfjtvldndw5csv94tvg5hzfcmcgn", + BuildUnbondingDelegation: types.NewServiceUnbondingDelegation, + }, + 20, + time.Date(2024, 1, 2, 12, 0, 0, 0, time.UTC), + sdk.NewCoins(sdk.NewCoin("umilk", sdkmath.NewInt(50))), + ) + suite.Require().NoError(err) + }, + request: types.NewQueryServiceUnbondingDelegationsRequest(2, nil), + shouldErr: false, + expUnbondingDelegations: []types.UnbondingDelegation{ + types.NewServiceUnbondingDelegation( + "cosmos1d03wa9qd8flfjtvldndw5csv94tvg5hzfcmcgn", + 2, + 20, + time.Date(2024, 1, 2, 12, 0, 0, 0, time.UTC), + sdk.NewCoins(sdk.NewCoin("umilk", sdkmath.NewInt(50))), + 2, + ), + }, + }, } for _, tc := range testCases { From 7ca2b0787a3826df33f4fba44bc86d02a45b4b20 Mon Sep 17 00:00:00 2001 From: Hanjun Kim Date: Thu, 2 Jan 2025 22:05:23 +0900 Subject: [PATCH 2/3] refactor: use GenericFilteredPaginate --- x/restaking/keeper/grpc_query.go | 66 +++++++++++++++++--------------- 1 file changed, 36 insertions(+), 30 deletions(-) diff --git a/x/restaking/keeper/grpc_query.go b/x/restaking/keeper/grpc_query.go index 02579f74..cab562a7 100644 --- a/x/restaking/keeper/grpc_query.go +++ b/x/restaking/keeper/grpc_query.go @@ -249,23 +249,25 @@ func (k Querier) PoolUnbondingDelegations(ctx context.Context, req *types.QueryP delegationsStore := prefix.NewStore(runtime.KVStoreAdapter(store), types.PoolUnbondingDelegationPrefix) // Query the pool unbonding delegations for the given pool id - var unbondingDelegations []types.UnbondingDelegation - pageRes, err := query.Paginate(delegationsStore, req.Pagination, func(key []byte, value []byte) error { - unbond, err := types.UnmarshalUnbondingDelegation(k.cdc, value) - if err != nil { - return err - } - if unbond.TargetID == req.PoolId { - unbondingDelegations = append(unbondingDelegations, unbond) + unbondingDelegations, pageRes, err := query.GenericFilteredPaginate(k.cdc, delegationsStore, req.Pagination, func(_ []byte, unbond *types.UnbondingDelegation) (*types.UnbondingDelegation, error) { + if unbond.TargetID != req.PoolId { + return nil, nil } - return nil + return unbond, nil + }, func() *types.UnbondingDelegation { + return &types.UnbondingDelegation{} }) if err != nil { return nil, status.Error(codes.Internal, err.Error()) } + unbondingDels := make([]types.UnbondingDelegation, len(unbondingDelegations)) + for i, unbond := range unbondingDelegations { + unbondingDels[i] = *unbond + } + return &types.QueryPoolUnbondingDelegationsResponse{ - UnbondingDelegations: unbondingDelegations, + UnbondingDelegations: unbondingDels, Pagination: pageRes, }, nil } @@ -390,23 +392,25 @@ func (k Querier) OperatorUnbondingDelegations(ctx context.Context, req *types.Qu delegationsStore := prefix.NewStore(runtime.KVStoreAdapter(store), types.OperatorUnbondingDelegationPrefix) // Query the operator unbonding delegations for the given pool id - var unbondingDelegations []types.UnbondingDelegation - pageRes, err := query.Paginate(delegationsStore, req.Pagination, func(key []byte, value []byte) error { - unbond, err := types.UnmarshalUnbondingDelegation(k.cdc, value) - if err != nil { - return err - } - if unbond.TargetID == req.OperatorId { - unbondingDelegations = append(unbondingDelegations, unbond) + unbondingDelegations, pageRes, err := query.GenericFilteredPaginate(k.cdc, delegationsStore, req.Pagination, func(_ []byte, unbond *types.UnbondingDelegation) (*types.UnbondingDelegation, error) { + if unbond.TargetID != req.OperatorId { + return nil, nil } - return nil + return unbond, nil + }, func() *types.UnbondingDelegation { + return &types.UnbondingDelegation{} }) if err != nil { return nil, status.Error(codes.Internal, err.Error()) } + unbondingDels := make([]types.UnbondingDelegation, len(unbondingDelegations)) + for i, unbond := range unbondingDelegations { + unbondingDels[i] = *unbond + } + return &types.QueryOperatorUnbondingDelegationsResponse{ - UnbondingDelegations: unbondingDelegations, + UnbondingDelegations: unbondingDels, Pagination: pageRes, }, nil } @@ -531,23 +535,25 @@ func (k Querier) ServiceUnbondingDelegations(ctx context.Context, req *types.Que delegationsStore := prefix.NewStore(runtime.KVStoreAdapter(store), types.ServiceUnbondingDelegationPrefix) // Query the service unbonding delegations for the given pool id - var unbondingDelegations []types.UnbondingDelegation - pageRes, err := query.Paginate(delegationsStore, req.Pagination, func(key []byte, value []byte) error { - unbond, err := types.UnmarshalUnbondingDelegation(k.cdc, value) - if err != nil { - return err - } - if unbond.TargetID == req.ServiceId { - unbondingDelegations = append(unbondingDelegations, unbond) + unbondingDelegations, pageRes, err := query.GenericFilteredPaginate(k.cdc, delegationsStore, req.Pagination, func(_ []byte, unbond *types.UnbondingDelegation) (*types.UnbondingDelegation, error) { + if unbond.TargetID != req.ServiceId { + return nil, nil } - return nil + return unbond, nil + }, func() *types.UnbondingDelegation { + return &types.UnbondingDelegation{} }) if err != nil { return nil, status.Error(codes.Internal, err.Error()) } + unbondingDels := make([]types.UnbondingDelegation, len(unbondingDelegations)) + for i, unbond := range unbondingDelegations { + unbondingDels[i] = *unbond + } + return &types.QueryServiceUnbondingDelegationsResponse{ - UnbondingDelegations: unbondingDelegations, + UnbondingDelegations: unbondingDels, Pagination: pageRes, }, nil } From 0585b57d9de9da86303e85da54122a702ac835ac Mon Sep 17 00:00:00 2001 From: Hanjun Kim Date: Thu, 2 Jan 2025 22:32:20 +0900 Subject: [PATCH 3/3] chore: fix typos --- x/restaking/keeper/grpc_query.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/x/restaking/keeper/grpc_query.go b/x/restaking/keeper/grpc_query.go index cab562a7..f3c2571c 100644 --- a/x/restaking/keeper/grpc_query.go +++ b/x/restaking/keeper/grpc_query.go @@ -391,7 +391,7 @@ func (k Querier) OperatorUnbondingDelegations(ctx context.Context, req *types.Qu store := k.storeService.OpenKVStore(ctx) delegationsStore := prefix.NewStore(runtime.KVStoreAdapter(store), types.OperatorUnbondingDelegationPrefix) - // Query the operator unbonding delegations for the given pool id + // Query the operator unbonding delegations for the given operator id unbondingDelegations, pageRes, err := query.GenericFilteredPaginate(k.cdc, delegationsStore, req.Pagination, func(_ []byte, unbond *types.UnbondingDelegation) (*types.UnbondingDelegation, error) { if unbond.TargetID != req.OperatorId { return nil, nil @@ -534,7 +534,7 @@ func (k Querier) ServiceUnbondingDelegations(ctx context.Context, req *types.Que store := k.storeService.OpenKVStore(ctx) delegationsStore := prefix.NewStore(runtime.KVStoreAdapter(store), types.ServiceUnbondingDelegationPrefix) - // Query the service unbonding delegations for the given pool id + // Query the service unbonding delegations for the given service id unbondingDelegations, pageRes, err := query.GenericFilteredPaginate(k.cdc, delegationsStore, req.Pagination, func(_ []byte, unbond *types.UnbondingDelegation) (*types.UnbondingDelegation, error) { if unbond.TargetID != req.ServiceId { return nil, nil