Skip to content

Commit

Permalink
chore: refactor validategrpc into an internal shared package (cosmos#…
Browse files Browse the repository at this point in the history
…6080)

* Refactor validateGRPCRequest

* Refactor test

* Linter

* Remove duplicated import

* Linter (again)

* Refactor last instance of validateGRPCRequest

---------

Co-authored-by: Carlos Rodriguez <[email protected]>
  • Loading branch information
bznein and crodriguezvega authored Apr 4, 2024
1 parent b465156 commit f1b5244
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 55 deletions.
21 changes: 21 additions & 0 deletions internal/validate/validate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package validate

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

host "github.com/cosmos/ibc-go/v8/modules/core/24-host"
)

// GRPCRequest validates that the portID and channelID of a gRPC Request are valid identifiers.
func GRPCRequest(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
}
53 changes: 53 additions & 0 deletions internal/validate/validate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package validate_test

import (
"fmt"
"testing"

"github.com/stretchr/testify/require"

"github.com/cosmos/ibc-go/v8/internal/validate"
)

func TestGRPCRequest(t *testing.T) {
const (
validID = "validIdentifier"
invalidID = ""
)
testCases := []struct {
msg string
portID string
channelID string
expPass bool
}{
{
"success",
validID,
validID,
true,
},
{
"invalid portID",
invalidID,
validID,
false,
},
{
"invalid channelID",
validID,
invalidID,
false,
},
}

for _, tc := range testCases {
t.Run(fmt.Sprintf("Case %s", tc.msg), func(t *testing.T) {
err := validate.GRPCRequest(tc.portID, tc.channelID)
if tc.expPass {
require.NoError(t, err, tc.msg)
} else {
require.Error(t, err, tc.msg)
}
})
}
}
18 changes: 3 additions & 15 deletions modules/apps/29-fee/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"

"github.com/cosmos/ibc-go/v8/internal/validate"
"github.com/cosmos/ibc-go/v8/modules/apps/29-fee/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 @@ -75,7 +75,7 @@ func (k Keeper) IncentivizedPacketsForChannel(goCtx context.Context, req *types.
return nil, status.Error(codes.InvalidArgument, "empty request")
}

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

Expand Down Expand Up @@ -270,7 +270,7 @@ func (k Keeper) FeeEnabledChannel(goCtx context.Context, req *types.QueryFeeEnab
return nil, status.Error(codes.InvalidArgument, "empty request")
}

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

Expand All @@ -289,15 +289,3 @@ func (k Keeper) FeeEnabledChannel(goCtx context.Context, req *types.QueryFeeEnab
FeeEnabled: isFeeEnabled,
}, 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
}
16 changes: 2 additions & 14 deletions modules/apps/transfer/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"

"github.com/cosmos/ibc-go/v8/internal/validate"
"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 @@ -121,7 +121,7 @@ func (k Keeper) EscrowAddress(c context.Context, req *types.QueryEscrowAddressRe

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

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

Expand Down Expand Up @@ -156,15 +156,3 @@ 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
}
41 changes: 15 additions & 26 deletions modules/core/04-channel/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"

"github.com/cosmos/ibc-go/v8/internal/validate"
clienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types"
connectiontypes "github.com/cosmos/ibc-go/v8/modules/core/03-connection/types"
"github.com/cosmos/ibc-go/v8/modules/core/04-channel/types"
Expand All @@ -28,7 +29,7 @@ func (k Keeper) Channel(c context.Context, req *types.QueryChannelRequest) (*typ
return nil, status.Error(codes.InvalidArgument, "empty request")
}

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

Expand Down Expand Up @@ -138,7 +139,7 @@ func (k Keeper) ChannelClientState(c context.Context, req *types.QueryChannelCli
return nil, status.Error(codes.InvalidArgument, "empty request")
}

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

Expand All @@ -161,7 +162,7 @@ func (k Keeper) ChannelConsensusState(c context.Context, req *types.QueryChannel
return nil, status.Error(codes.InvalidArgument, "empty request")
}

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

Expand Down Expand Up @@ -207,7 +208,7 @@ func (k Keeper) PacketCommitment(c context.Context, req *types.QueryPacketCommit
return nil, status.Error(codes.InvalidArgument, "empty request")
}

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

Expand Down Expand Up @@ -239,7 +240,7 @@ func (k Keeper) PacketCommitments(c context.Context, req *types.QueryPacketCommi
return nil, status.Error(codes.InvalidArgument, "empty request")
}

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

Expand Down Expand Up @@ -284,7 +285,7 @@ func (k Keeper) PacketReceipt(c context.Context, req *types.QueryPacketReceiptRe
return nil, status.Error(codes.InvalidArgument, "empty request")
}

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

Expand Down Expand Up @@ -312,7 +313,7 @@ func (k Keeper) PacketAcknowledgement(c context.Context, req *types.QueryPacketA
return nil, status.Error(codes.InvalidArgument, "empty request")
}

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

Expand Down Expand Up @@ -343,7 +344,7 @@ func (k Keeper) PacketAcknowledgements(c context.Context, req *types.QueryPacket
return nil, status.Error(codes.InvalidArgument, "empty request")
}

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

Expand Down Expand Up @@ -425,7 +426,7 @@ func (k Keeper) UnreceivedPackets(c context.Context, req *types.QueryUnreceivedP
return nil, status.Error(codes.InvalidArgument, "empty request")
}

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

Expand Down Expand Up @@ -511,7 +512,7 @@ func (k Keeper) UnreceivedAcks(c context.Context, req *types.QueryUnreceivedAcks
return nil, status.Error(codes.InvalidArgument, "empty request")
}

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

Expand Down Expand Up @@ -551,7 +552,7 @@ func (k Keeper) NextSequenceReceive(c context.Context, req *types.QueryNextSeque
return nil, status.Error(codes.InvalidArgument, "empty request")
}

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

Expand Down Expand Up @@ -586,7 +587,7 @@ func (k Keeper) NextSequenceSend(c context.Context, req *types.QueryNextSequence
return nil, status.Error(codes.InvalidArgument, "empty request")
}

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

Expand All @@ -609,7 +610,7 @@ func (k Keeper) UpgradeErrorReceipt(c context.Context, req *types.QueryUpgradeEr
return nil, status.Error(codes.InvalidArgument, "empty request")
}

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

Expand Down Expand Up @@ -640,7 +641,7 @@ func (k Keeper) Upgrade(c context.Context, req *types.QueryUpgradeRequest) (*typ
return nil, status.Error(codes.InvalidArgument, "empty request")
}

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

Expand Down Expand Up @@ -674,15 +675,3 @@ func (k Keeper) ChannelParams(c context.Context, req *types.QueryChannelParamsRe
Params: &params,
}, 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
}

0 comments on commit f1b5244

Please sign in to comment.