Skip to content

Commit

Permalink
fix: filter unbonding delegations by requested target id (#230)
Browse files Browse the repository at this point in the history
## Description

Closes: #XXXX

<!-- Add a description of the changes that this PR introduces and the
files that
are the most critical to review. -->

---

### Author Checklist

*All items are required. Please add a note to the item if the item is
not applicable and
please add links to any relevant follow up issues.*

I have...

- [ ] included the correct [type
prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json)
in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [ ] targeted the correct branch (see [PR
Targeting](https://github.com/milkyway-labs/milkyway/blob/master/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building
modules](https://docs.cosmos.network/v0.44/building-modules/intro.html)
- [ ] included the necessary unit and integration
[tests](https://github.com/milkyway-labs/milkyway/blob/master/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go
code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable
and please add
your handle next to the items reviewed if you only reviewed selected
items.*

I have...

- [ ] confirmed the correct [type
prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json)
in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
  • Loading branch information
hallazzang authored Jan 15, 2025
1 parent a3ddf40 commit 5f3f13d
Show file tree
Hide file tree
Showing 2 changed files with 226 additions and 26 deletions.
64 changes: 38 additions & 26 deletions x/restaking/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,21 +255,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 @@ -399,22 +403,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 @@ -543,22 +551,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

0 comments on commit 5f3f13d

Please sign in to comment.