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

chore: add gRPC for querying incentivized packets for a specific channel #983

Merged
Merged
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
37 changes: 37 additions & 0 deletions docs/ibc/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
- [ibc/applications/fee/v1/query.proto](#ibc/applications/fee/v1/query.proto)
- [QueryIncentivizedPacketRequest](#ibc.applications.fee.v1.QueryIncentivizedPacketRequest)
- [QueryIncentivizedPacketResponse](#ibc.applications.fee.v1.QueryIncentivizedPacketResponse)
- [QueryIncentivizedPacketsForChannelRequest](#ibc.applications.fee.v1.QueryIncentivizedPacketsForChannelRequest)
- [QueryIncentivizedPacketsForChannelResponse](#ibc.applications.fee.v1.QueryIncentivizedPacketsForChannelResponse)
- [QueryIncentivizedPacketsRequest](#ibc.applications.fee.v1.QueryIncentivizedPacketsRequest)
- [QueryIncentivizedPacketsResponse](#ibc.applications.fee.v1.QueryIncentivizedPacketsResponse)
- [QueryTotalAckFeesRequest](#ibc.applications.fee.v1.QueryTotalAckFeesRequest)
Expand Down Expand Up @@ -937,6 +939,40 @@ QueryIncentivizedPacketsResponse is the response type for the incentivized packe



<a name="ibc.applications.fee.v1.QueryIncentivizedPacketsForChannelRequest"></a>

### QueryIncentivizedPacketsForChannelRequest
QueryIncentivizedPacketsForChannelRequest is the request type for querying for all incentivized packets
for a specific channel


| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `pagination` | [cosmos.base.query.v1beta1.PageRequest](#cosmos.base.query.v1beta1.PageRequest) | | pagination defines an optional pagination for the request. |
| `port_id` | [string](#string) | | |
| `channel_id` | [string](#string) | | |
| `query_height` | [uint64](#uint64) | | Height to query at |






<a name="ibc.applications.fee.v1.QueryIncentivizedPacketsForChannelResponse"></a>

### QueryIncentivizedPacketsForChannelResponse
QueryIncentivizedPacketsResponse is the response type for the incentivized packets RPC


| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `incentivized_packets` | [IdentifiedPacketFees](#ibc.applications.fee.v1.IdentifiedPacketFees) | repeated | Map of all incentivized_packets |






<a name="ibc.applications.fee.v1.QueryIncentivizedPacketsRequest"></a>

### QueryIncentivizedPacketsRequest
Expand Down Expand Up @@ -1073,6 +1109,7 @@ Query provides defines the gRPC querier service.
| ----------- | ------------ | ------------- | ------------| ------- | -------- |
| `IncentivizedPackets` | [QueryIncentivizedPacketsRequest](#ibc.applications.fee.v1.QueryIncentivizedPacketsRequest) | [QueryIncentivizedPacketsResponse](#ibc.applications.fee.v1.QueryIncentivizedPacketsResponse) | Gets all incentivized packets | GET|/ibc/apps/fee/v1/incentivized_packets|
| `IncentivizedPacket` | [QueryIncentivizedPacketRequest](#ibc.applications.fee.v1.QueryIncentivizedPacketRequest) | [QueryIncentivizedPacketResponse](#ibc.applications.fee.v1.QueryIncentivizedPacketResponse) | Gets the fees expected for submitting the ReceivePacket, AcknowledgementPacket, and TimeoutPacket messages for the given packet | GET|/ibc/apps/fee/v1/incentivized_packet/port/{packet_id.port_id}/channel/{packet_id.channel_id}/sequence/{packet_id.sequence}|
| `IncentivizedPacketsForChannel` | [QueryIncentivizedPacketsForChannelRequest](#ibc.applications.fee.v1.QueryIncentivizedPacketsForChannelRequest) | [QueryIncentivizedPacketsForChannelResponse](#ibc.applications.fee.v1.QueryIncentivizedPacketsForChannelResponse) | Gets all incentivized packets for a specific channel | GET|/ibc/apps/fee/v1/incentivized_packets/{port_id}/{channel_id}|
| `TotalRecvFees` | [QueryTotalRecvFeesRequest](#ibc.applications.fee.v1.QueryTotalRecvFeesRequest) | [QueryTotalRecvFeesResponse](#ibc.applications.fee.v1.QueryTotalRecvFeesResponse) | TotalRecvFees returns the total receive fees for a packet given its identifier | GET|/ibc/apps/fee/v1/total_recv_fees/port/{packet_id.port_id}/channel/{packet_id.channel_id}/sequence/{packet_id.sequence}|
| `TotalAckFees` | [QueryTotalAckFeesRequest](#ibc.applications.fee.v1.QueryTotalAckFeesRequest) | [QueryTotalAckFeesResponse](#ibc.applications.fee.v1.QueryTotalAckFeesResponse) | TotalAckFees returns the total acknowledgement fees for a packet given its identifier | GET|/ibc/apps/fee/v1/total_ack_fees/port/{packet_id.port_id}/channel/{packet_id.channel_id}/sequence/{packet_id.sequence}|
| `TotalTimeoutFees` | [QueryTotalTimeoutFeesRequest](#ibc.applications.fee.v1.QueryTotalTimeoutFeesRequest) | [QueryTotalTimeoutFeesResponse](#ibc.applications.fee.v1.QueryTotalTimeoutFeesResponse) | TotalTimeoutFees returns the total timeout fees for a packet given its identifier | GET|/ibc/apps/fee/v1/total_timeout_fees/port/{packet_id.port_id}/channel/{packet_id.channel_id}/sequence/{packet_id.sequence}|
Expand Down
40 changes: 35 additions & 5 deletions modules/apps/29-fee/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ package keeper
import (
"context"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/query"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"github.com/cosmos/ibc-go/v3/modules/apps/29-fee/types"
)
Expand Down Expand Up @@ -66,12 +65,43 @@ func (k Keeper) IncentivizedPacket(c context.Context, req *types.QueryIncentiviz
}, nil
}

// TotalRecvFees implements the Query/TotalRecvFees gRPC method
func (k Keeper) TotalRecvFees(goCtx context.Context, req *types.QueryTotalRecvFeesRequest) (*types.QueryTotalRecvFeesResponse, error) {
// IncentivizedPacketsForChannel implements the IncentivizedPacketsForChannel gRPC method
func (k Keeper) IncentivizedPacketsForChannel(goCtx context.Context, req *types.QueryIncentivizedPacketsForChannelRequest) (*types.QueryIncentivizedPacketsForChannelResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}

ctx := sdk.UnwrapSDKContext(goCtx).WithBlockHeight(int64(req.QueryHeight))

var packets []*types.IdentifiedPacketFees
keyPrefix := types.KeyFeesInEscrowChannelPrefix(req.PortId, req.ChannelId)
store := prefix.NewStore(ctx.KVStore(k.storeKey), keyPrefix)
_, err := query.Paginate(store, req.Pagination, func(key, value []byte) error {
packetID, err := types.ParseKeyFeesInEscrow(string(keyPrefix) + string(key))
if err != nil {
return err
}
Comment on lines +81 to +83
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

left off a test case because then I'd have to set an invalid key. I could do this, but the correctness of keeper setting shouldn't really be tested by grpc query tests


packetFees := k.MustUnmarshalFees(value)

identifiedPacketFees := types.NewIdentifiedPacketFees(packetID, packetFees.PacketFees)
packets = append(packets, &identifiedPacketFees)

return nil
})

if err != nil {
return nil, status.Error(codes.NotFound, err.Error())
}

return &types.QueryIncentivizedPacketsForChannelResponse{
IncentivizedPackets: packets,
}, nil
}

// TotalRecvFees implements the Query/TotalRecvFees gRPC method
func (k Keeper) TotalRecvFees(goCtx context.Context, req *types.QueryTotalRecvFeesRequest) (*types.QueryTotalRecvFeesResponse, error) {

ctx := sdk.UnwrapSDKContext(goCtx)

feesInEscrow, found := k.GetFeesInEscrow(ctx, req.PacketId)
Expand Down
95 changes: 95 additions & 0 deletions modules/apps/29-fee/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper_test

import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"

Expand Down Expand Up @@ -140,6 +141,100 @@ func (suite *KeeperTestSuite) TestQueryIncentivizedPacket() {
}
}

func (suite *KeeperTestSuite) TestQueryIncentivizedPacketsForChannel() {
var (
req *types.QueryIncentivizedPacketsForChannelRequest
expIdentifiedPacketFees []*types.IdentifiedPacketFees
)

fee := types.Fee{
AckFee: sdk.Coins{sdk.Coin{Denom: sdk.DefaultBondDenom, Amount: sdk.NewInt(100)}},
RecvFee: sdk.Coins{sdk.Coin{Denom: sdk.DefaultBondDenom, Amount: sdk.NewInt(100)}},
TimeoutFee: sdk.Coins{sdk.Coin{Denom: sdk.DefaultBondDenom, Amount: sdk.NewInt(100)}},
}

testCases := []struct {
msg string
malleate func()
expPass bool
}{
{
"empty pagination",
func() {
expIdentifiedPacketFees = nil
req = &types.QueryIncentivizedPacketsForChannelRequest{}
},
true,
},
{
"success",
func() {
req = &types.QueryIncentivizedPacketsForChannelRequest{
Pagination: &query.PageRequest{
Limit: 5,
CountTotal: false,
},
PortId: ibctesting.MockFeePort,
ChannelId: ibctesting.FirstChannelID,
QueryHeight: 0,
}
},
true,
},
{
"no packets for specified channel",
func() {
expIdentifiedPacketFees = nil
req = &types.QueryIncentivizedPacketsForChannelRequest{
Pagination: &query.PageRequest{
Limit: 5,
CountTotal: false,
},
PortId: ibctesting.MockFeePort,
ChannelId: "channel-10",
QueryHeight: 0,
}
},
true,
},
}

for _, tc := range testCases {
suite.Run(fmt.Sprintf("Case %s", tc.msg), func() {
suite.SetupTest() // reset

// setup
refundAcc := suite.chainA.SenderAccount.GetAddress()
packetFee := types.NewPacketFee(fee, refundAcc.String(), nil)
packetFees := types.NewPacketFees([]types.PacketFee{packetFee, packetFee, packetFee})

identifiedFees1 := types.NewIdentifiedPacketFees(channeltypes.NewPacketId(ibctesting.FirstChannelID, ibctesting.MockFeePort, 1), packetFees.PacketFees)
identifiedFees2 := types.NewIdentifiedPacketFees(channeltypes.NewPacketId(ibctesting.FirstChannelID, ibctesting.MockFeePort, 2), packetFees.PacketFees)
identifiedFees3 := types.NewIdentifiedPacketFees(channeltypes.NewPacketId(ibctesting.FirstChannelID, ibctesting.MockFeePort, 3), packetFees.PacketFees)

expIdentifiedPacketFees = append(expIdentifiedPacketFees, &identifiedFees1, &identifiedFees2, &identifiedFees3)

suite.chainA.GetSimApp().IBCFeeKeeper.SetFeeEnabled(suite.chainA.GetContext(), ibctesting.MockFeePort, ibctesting.FirstChannelID)
for _, identifiedPacketFees := range expIdentifiedPacketFees {
suite.chainA.GetSimApp().IBCFeeKeeper.SetFeesInEscrow(suite.chainA.GetContext(), identifiedPacketFees.PacketId, types.NewPacketFees(identifiedPacketFees.PacketFees))
}

tc.malleate()
ctx := sdk.WrapSDKContext(suite.chainA.GetContext())

res, err := suite.queryClient.IncentivizedPacketsForChannel(ctx, req)

if tc.expPass {
suite.Require().NoError(err)
suite.Require().NotNil(res)
suite.Require().Equal(expIdentifiedPacketFees, res.IncentivizedPackets)
} else {
suite.Require().Error(err)
}
})
}
}

func (suite *KeeperTestSuite) TestQueryTotalRecvFees() {
var (
req *types.QueryTotalRecvFeesRequest
Expand Down
1 change: 1 addition & 0 deletions modules/apps/29-fee/types/fee.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"

channeltypes "github.com/cosmos/ibc-go/v3/modules/core/04-channel/types"
)

Expand Down
Loading