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: filter unbonding delegations by requested target id #230

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
64 changes: 38 additions & 26 deletions x/restaking/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,21 +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
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
}
unbondingDelegations = append(unbondingDelegations, unbond)
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
}
Expand Down Expand Up @@ -387,22 +391,26 @@ 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
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
// 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
}
unbondingDelegations = append(unbondingDelegations, unbond)
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
}
Expand Down Expand Up @@ -526,22 +534,26 @@ 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
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
// 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
}
unbondingDelegations = append(unbondingDelegations, unbond)
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
}
Expand Down
188 changes: 188 additions & 0 deletions x/restaking/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
Loading