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 9 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 @@ -47,6 +47,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)

Expand Down Expand Up @@ -953,6 +955,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 @@ -999,6 +1035,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}|

<!-- end services -->

Expand Down
45 changes: 42 additions & 3 deletions modules/apps/29-fee/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ package keeper

import (
"context"

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

"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"
channeltypes "github.com/cosmos/ibc-go/v3/modules/core/04-channel/types"
)

var _ types.QueryServer = Keeper{}
Expand Down Expand Up @@ -63,3 +64,41 @@ func (k Keeper) IncentivizedPacket(c context.Context, req *types.QueryIncentiviz
IncentivizedPacket: &fee,
}, nil
}

// IncentivizedPacketsForChannel implements the IncentivizedPacketsForChannel gRPC method
func (k Keeper) IncentivizedPacketsForChannel(c context.Context, req *types.QueryIncentivizedPacketsForChannelRequest) (*types.QueryIncentivizedPacketsForChannelResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

no test case because the tests will panic on a nil request


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

var packets []*types.IdentifiedPacketFees
store := prefix.NewStore(ctx.KVStore(k.storeKey), []byte(string(types.KeyFeesInEscrowChannelPrefix(req.PortId, req.ChannelId))+"/"))
_, err := query.Paginate(store, req.Pagination, func(key, value []byte) error {
// the key returned only includes the sequence
seq, err := strconv.ParseUint(string(key), 10, 64)
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


packetID := channeltypes.NewPacketId(req.ChannelId, req.PortId, seq)
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
}
96 changes: 95 additions & 1 deletion modules/apps/29-fee/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
ibctesting "github.com/cosmos/ibc-go/v3/testing"
)

func (suite *KeeperTestSuite) TestQueryIncentivizedPacketI() {
func (suite *KeeperTestSuite) TestQueryIncentivizedPacket() {

var (
req *types.QueryIncentivizedPacketRequest
Expand Down Expand Up @@ -157,3 +157,97 @@ func (suite *KeeperTestSuite) TestQueryIncentivizedPackets() {
})
}
}

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)
}
})
}
}
10 changes: 10 additions & 0 deletions modules/apps/29-fee/types/fee.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ 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"
)

// NewPacketFee creates and returns a new PacketFee struct including the incentivization fees, refund addres and relayers
Expand Down Expand Up @@ -80,3 +82,11 @@ func (fee Fee) Validate() error {

return nil
}

// NewIdentifiedPacketFees returns a IdentifiedPacketFees struct
func NewIdentifiedPacketFees(packetID channeltypes.PacketId, packetFees []PacketFee) IdentifiedPacketFees {
return IdentifiedPacketFees{
PacketId: packetID,
PacketFees: packetFees,
}
}
Loading