Skip to content

Commit

Permalink
chore: Add gRPC validation for EscrowAddress (#6059)
Browse files Browse the repository at this point in the history
* Add check that Channel exists for EscrowAddress

* Changed import of errors

* Fix compilation errors

* Fixed import

* Fix broken test

* Add test that checks for channelNotFound error

* Added HasChannel to ChannelKeeper interface and used it in gRPC validation

* Fix test and add back test for ChannelNotFound

* Change import alias.

* Added test case for invalid channelID

* Trim whitespace

* Update modules/apps/transfer/keeper/grpc_query.go

Condense check for existence of channel

Co-authored-by: Damian Nolan <damiannolan@gmail.com>

* Update modules/apps/transfer/keeper/grpc_query.go

Co-authored-by: Damian Nolan <damiannolan@gmail.com>

* Added tests for empty portID and empty channelID

---------

Co-authored-by: Damian Nolan <damiannolan@gmail.com>
  • Loading branch information
bznein and damiannolan authored Mar 27, 2024
1 parent 2555a7c commit e7c74b0
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
28 changes: 27 additions & 1 deletion modules/apps/transfer/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
"github.com/cosmos/cosmos-sdk/types/query"

"github.com/cosmos/ibc-go/v8/modules/apps/transfer/types"
channeltypes "github.com/cosmos/ibc-go/v8/modules/core/04-channel/types"
host "github.com/cosmos/ibc-go/v8/modules/core/24-host"
)

var _ types.QueryServer = (*Keeper)(nil)
Expand Down Expand Up @@ -112,13 +114,25 @@ func (k Keeper) DenomHash(c context.Context, req *types.QueryDenomHashRequest) (
}

// EscrowAddress implements the EscrowAddress gRPC method
func (Keeper) EscrowAddress(c context.Context, req *types.QueryEscrowAddressRequest) (*types.QueryEscrowAddressResponse, error) {
func (k Keeper) EscrowAddress(c context.Context, req *types.QueryEscrowAddressRequest) (*types.QueryEscrowAddressResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}

addr := types.GetEscrowAddress(req.PortId, req.ChannelId)

if err := validategRPCRequest(req.PortId, req.ChannelId); err != nil {
return nil, err
}

ctx := sdk.UnwrapSDKContext(c)
if !k.channelKeeper.HasChannel(ctx, req.PortId, req.ChannelId) {
return nil, status.Error(
codes.NotFound,
errorsmod.Wrapf(channeltypes.ErrChannelNotFound, "port ID (%s) channel ID (%s)", req.PortId, req.ChannelId).Error(),
)
}

return &types.QueryEscrowAddressResponse{
EscrowAddress: addr.String(),
}, nil
Expand All @@ -142,3 +156,15 @@ func (k Keeper) TotalEscrowForDenom(c context.Context, req *types.QueryTotalEscr
Amount: amount,
}, nil
}

func validategRPCRequest(portID, channelID string) error {
if err := host.PortIdentifierValidator(portID); err != nil {
return status.Error(codes.InvalidArgument, err.Error())
}

if err := host.ChannelIdentifierValidator(channelID); err != nil {
return status.Error(codes.InvalidArgument, err.Error())
}

return nil
}
32 changes: 32 additions & 0 deletions modules/apps/transfer/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,12 +245,44 @@ func (suite *KeeperTestSuite) TestEscrowAddress() {
},
true,
},
{
"failure - channel not found",
func() {
req = &types.QueryEscrowAddressRequest{
PortId: ibctesting.InvalidID,
ChannelId: ibctesting.FirstChannelID,
}
},
false,
},
{
"failure - empty channelID",
func() {
req = &types.QueryEscrowAddressRequest{
PortId: ibctesting.TransferPort,
ChannelId: "",
}
},
false,
},
{
"failure - empty portID",
func() {
req = &types.QueryEscrowAddressRequest{
PortId: "",
ChannelId: ibctesting.FirstChannelID,
}
},
false,
},
}

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

tc.malleate()
ctx := suite.chainA.GetContext()
Expand Down
1 change: 1 addition & 0 deletions modules/apps/transfer/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type ChannelKeeper interface {
GetChannel(ctx sdk.Context, srcPort, srcChan string) (channel channeltypes.Channel, found bool)
GetNextSequenceSend(ctx sdk.Context, portID, channelID string) (uint64, bool)
GetAllChannelsWithPortPrefix(ctx sdk.Context, portPrefix string) []channeltypes.IdentifiedChannel
HasChannel(ctx sdk.Context, portID, channelID string) bool
}

// ClientKeeper defines the expected IBC client keeper
Expand Down

0 comments on commit e7c74b0

Please sign in to comment.