Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f1b5244

Browse files
bzneincrodriguezvega
andauthoredApr 4, 2024··
chore: refactor validategrpc into an internal shared package (#6080)
* Refactor validateGRPCRequest * Refactor test * Linter * Remove duplicated import * Linter (again) * Refactor last instance of validateGRPCRequest --------- Co-authored-by: Carlos Rodriguez <[email protected]>
1 parent b465156 commit f1b5244

File tree

5 files changed

+94
-55
lines changed

5 files changed

+94
-55
lines changed
 

‎internal/validate/validate.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package validate
2+
3+
import (
4+
"google.golang.org/grpc/codes"
5+
"google.golang.org/grpc/status"
6+
7+
host "github.com/cosmos/ibc-go/v8/modules/core/24-host"
8+
)
9+
10+
// GRPCRequest validates that the portID and channelID of a gRPC Request are valid identifiers.
11+
func GRPCRequest(portID, channelID string) error {
12+
if err := host.PortIdentifierValidator(portID); err != nil {
13+
return status.Error(codes.InvalidArgument, err.Error())
14+
}
15+
16+
if err := host.ChannelIdentifierValidator(channelID); err != nil {
17+
return status.Error(codes.InvalidArgument, err.Error())
18+
}
19+
20+
return nil
21+
}

‎internal/validate/validate_test.go

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package validate_test
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/require"
8+
9+
"github.com/cosmos/ibc-go/v8/internal/validate"
10+
)
11+
12+
func TestGRPCRequest(t *testing.T) {
13+
const (
14+
validID = "validIdentifier"
15+
invalidID = ""
16+
)
17+
testCases := []struct {
18+
msg string
19+
portID string
20+
channelID string
21+
expPass bool
22+
}{
23+
{
24+
"success",
25+
validID,
26+
validID,
27+
true,
28+
},
29+
{
30+
"invalid portID",
31+
invalidID,
32+
validID,
33+
false,
34+
},
35+
{
36+
"invalid channelID",
37+
validID,
38+
invalidID,
39+
false,
40+
},
41+
}
42+
43+
for _, tc := range testCases {
44+
t.Run(fmt.Sprintf("Case %s", tc.msg), func(t *testing.T) {
45+
err := validate.GRPCRequest(tc.portID, tc.channelID)
46+
if tc.expPass {
47+
require.NoError(t, err, tc.msg)
48+
} else {
49+
require.Error(t, err, tc.msg)
50+
}
51+
})
52+
}
53+
}

‎modules/apps/29-fee/keeper/grpc_query.go

+3-15
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ import (
1212
sdk "github.com/cosmos/cosmos-sdk/types"
1313
"github.com/cosmos/cosmos-sdk/types/query"
1414

15+
"github.com/cosmos/ibc-go/v8/internal/validate"
1516
"github.com/cosmos/ibc-go/v8/modules/apps/29-fee/types"
1617
channeltypes "github.com/cosmos/ibc-go/v8/modules/core/04-channel/types"
17-
host "github.com/cosmos/ibc-go/v8/modules/core/24-host"
1818
)
1919

2020
var _ types.QueryServer = (*Keeper)(nil)
@@ -75,7 +75,7 @@ func (k Keeper) IncentivizedPacketsForChannel(goCtx context.Context, req *types.
7575
return nil, status.Error(codes.InvalidArgument, "empty request")
7676
}
7777

78-
if err := validategRPCRequest(req.PortId, req.ChannelId); err != nil {
78+
if err := validate.GRPCRequest(req.PortId, req.ChannelId); err != nil {
7979
return nil, err
8080
}
8181

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

273-
if err := validategRPCRequest(req.PortId, req.ChannelId); err != nil {
273+
if err := validate.GRPCRequest(req.PortId, req.ChannelId); err != nil {
274274
return nil, err
275275
}
276276

@@ -289,15 +289,3 @@ func (k Keeper) FeeEnabledChannel(goCtx context.Context, req *types.QueryFeeEnab
289289
FeeEnabled: isFeeEnabled,
290290
}, nil
291291
}
292-
293-
func validategRPCRequest(portID, channelID string) error {
294-
if err := host.PortIdentifierValidator(portID); err != nil {
295-
return status.Error(codes.InvalidArgument, err.Error())
296-
}
297-
298-
if err := host.ChannelIdentifierValidator(channelID); err != nil {
299-
return status.Error(codes.InvalidArgument, err.Error())
300-
}
301-
302-
return nil
303-
}

‎modules/apps/transfer/keeper/grpc_query.go

+2-14
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ import (
1414
sdk "github.com/cosmos/cosmos-sdk/types"
1515
"github.com/cosmos/cosmos-sdk/types/query"
1616

17+
"github.com/cosmos/ibc-go/v8/internal/validate"
1718
"github.com/cosmos/ibc-go/v8/modules/apps/transfer/types"
1819
channeltypes "github.com/cosmos/ibc-go/v8/modules/core/04-channel/types"
19-
host "github.com/cosmos/ibc-go/v8/modules/core/24-host"
2020
)
2121

2222
var _ types.QueryServer = (*Keeper)(nil)
@@ -121,7 +121,7 @@ func (k Keeper) EscrowAddress(c context.Context, req *types.QueryEscrowAddressRe
121121

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

124-
if err := validategRPCRequest(req.PortId, req.ChannelId); err != nil {
124+
if err := validate.GRPCRequest(req.PortId, req.ChannelId); err != nil {
125125
return nil, err
126126
}
127127

@@ -156,15 +156,3 @@ func (k Keeper) TotalEscrowForDenom(c context.Context, req *types.QueryTotalEscr
156156
Amount: amount,
157157
}, nil
158158
}
159-
160-
func validategRPCRequest(portID, channelID string) error {
161-
if err := host.PortIdentifierValidator(portID); err != nil {
162-
return status.Error(codes.InvalidArgument, err.Error())
163-
}
164-
165-
if err := host.ChannelIdentifierValidator(channelID); err != nil {
166-
return status.Error(codes.InvalidArgument, err.Error())
167-
}
168-
169-
return nil
170-
}

‎modules/core/04-channel/keeper/grpc_query.go

+15-26
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
sdk "github.com/cosmos/cosmos-sdk/types"
1515
"github.com/cosmos/cosmos-sdk/types/query"
1616

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

31-
if err := validategRPCRequest(req.PortId, req.ChannelId); err != nil {
32+
if err := validate.GRPCRequest(req.PortId, req.ChannelId); err != nil {
3233
return nil, err
3334
}
3435

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

141-
if err := validategRPCRequest(req.PortId, req.ChannelId); err != nil {
142+
if err := validate.GRPCRequest(req.PortId, req.ChannelId); err != nil {
142143
return nil, err
143144
}
144145

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

164-
if err := validategRPCRequest(req.PortId, req.ChannelId); err != nil {
165+
if err := validate.GRPCRequest(req.PortId, req.ChannelId); err != nil {
165166
return nil, err
166167
}
167168

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

210-
if err := validategRPCRequest(req.PortId, req.ChannelId); err != nil {
211+
if err := validate.GRPCRequest(req.PortId, req.ChannelId); err != nil {
211212
return nil, err
212213
}
213214

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

242-
if err := validategRPCRequest(req.PortId, req.ChannelId); err != nil {
243+
if err := validate.GRPCRequest(req.PortId, req.ChannelId); err != nil {
243244
return nil, err
244245
}
245246

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

287-
if err := validategRPCRequest(req.PortId, req.ChannelId); err != nil {
288+
if err := validate.GRPCRequest(req.PortId, req.ChannelId); err != nil {
288289
return nil, err
289290
}
290291

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

315-
if err := validategRPCRequest(req.PortId, req.ChannelId); err != nil {
316+
if err := validate.GRPCRequest(req.PortId, req.ChannelId); err != nil {
316317
return nil, err
317318
}
318319

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

346-
if err := validategRPCRequest(req.PortId, req.ChannelId); err != nil {
347+
if err := validate.GRPCRequest(req.PortId, req.ChannelId); err != nil {
347348
return nil, err
348349
}
349350

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

428-
if err := validategRPCRequest(req.PortId, req.ChannelId); err != nil {
429+
if err := validate.GRPCRequest(req.PortId, req.ChannelId); err != nil {
429430
return nil, err
430431
}
431432

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

514-
if err := validategRPCRequest(req.PortId, req.ChannelId); err != nil {
515+
if err := validate.GRPCRequest(req.PortId, req.ChannelId); err != nil {
515516
return nil, err
516517
}
517518

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

554-
if err := validategRPCRequest(req.PortId, req.ChannelId); err != nil {
555+
if err := validate.GRPCRequest(req.PortId, req.ChannelId); err != nil {
555556
return nil, err
556557
}
557558

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

589-
if err := validategRPCRequest(req.PortId, req.ChannelId); err != nil {
590+
if err := validate.GRPCRequest(req.PortId, req.ChannelId); err != nil {
590591
return nil, err
591592
}
592593

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

612-
if err := validategRPCRequest(req.PortId, req.ChannelId); err != nil {
613+
if err := validate.GRPCRequest(req.PortId, req.ChannelId); err != nil {
613614
return nil, err
614615
}
615616

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

643-
if err := validategRPCRequest(req.PortId, req.ChannelId); err != nil {
644+
if err := validate.GRPCRequest(req.PortId, req.ChannelId); err != nil {
644645
return nil, err
645646
}
646647

@@ -674,15 +675,3 @@ func (k Keeper) ChannelParams(c context.Context, req *types.QueryChannelParamsRe
674675
Params: &params,
675676
}, nil
676677
}
677-
678-
func validategRPCRequest(portID, channelID string) error {
679-
if err := host.PortIdentifierValidator(portID); err != nil {
680-
return status.Error(codes.InvalidArgument, err.Error())
681-
}
682-
683-
if err := host.ChannelIdentifierValidator(channelID); err != nil {
684-
return status.Error(codes.InvalidArgument, err.Error())
685-
}
686-
687-
return nil
688-
}

0 commit comments

Comments
 (0)
Please sign in to comment.