Skip to content

Commit 62042ec

Browse files
mconcatDeshErBojhaa
authored andcommitted
cleanup
1 parent b47e4ae commit 62042ec

File tree

4 files changed

+210
-84
lines changed

4 files changed

+210
-84
lines changed
Lines changed: 155 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,155 @@
11
package cli
22

33
import (
4+
"context"
5+
"fmt"
6+
"strings"
7+
8+
"github.com/cosmos/ibc-go/v10/modules/apps/rate-limiting/types"
49
"github.com/spf13/cobra"
510

611
"github.com/cosmos/cosmos-sdk/client"
12+
"github.com/cosmos/cosmos-sdk/client/flags"
13+
"github.com/cosmos/cosmos-sdk/version"
14+
)
715

8-
"github.com/cosmos/ibc-go/v10/modules/apps/rate-limiting/types"
16+
const (
17+
FlagDenom = "denom"
918
)
1019

11-
// GetQueryCmd returns the CLI query commands for the rate-limiting module
20+
// GetQueryCmd returns the cli query commands for this module.
1221
func GetQueryCmd() *cobra.Command {
13-
queryCmd := &cobra.Command{
22+
// Group ratelimit queries under a subcommand
23+
cmd := &cobra.Command{
1424
Use: types.ModuleName,
15-
Short: "Querying commands for the rate-limiting module",
25+
Short: fmt.Sprintf("Querying commands for the %s module", types.ModuleName),
1626
DisableFlagParsing: true,
1727
SuggestionsMinimumDistance: 2,
1828
RunE: client.ValidateCmd,
1929
}
2030

21-
// Add query commands here when defined
22-
// Example:
23-
// queryCmd.AddCommand(
24-
// GetCmdQueryRateLimit(),
25-
// GetCmdQueryParams(),
26-
// )
27-
28-
return queryCmd
31+
cmd.AddCommand(
32+
GetCmdQueryRateLimit(),
33+
GetCmdQueryAllRateLimits(),
34+
GetCmdQueryRateLimitsByChainId(),
35+
GetCmdQueryAllBlacklistedDenoms(), // Add Blacklisted Denoms query
36+
GetCmdQueryAllWhitelistedAddresses(), // Add Whitelisted Addresses query
37+
// TODO: Add GetCmdQueryParams if needed
38+
)
39+
return cmd
2940
}
3041

31-
// Example query command structure to be implemented
32-
/*
33-
// GetCmdQueryRateLimit returns the command to query a rate limit
42+
// GetCmdQueryRateLimit implements a command to query rate limits by channel-id or client-id and denom
3443
func GetCmdQueryRateLimit() *cobra.Command {
3544
cmd := &cobra.Command{
36-
Use: "rate-limit [channel-id] [denom]",
37-
Short: "Query a rate limit for a specific channel and denomination",
38-
Args: cobra.ExactArgs(2),
45+
Use: "rate-limit [channel-or-client-id]",
46+
Short: "Query rate limits by channel-id/client-id and denom",
47+
Long: strings.TrimSpace(
48+
fmt.Sprintf(`Query rate limits by channel-id/client-id and denom.
49+
If the denom flag is omitted, all rate limits for the given channel-id/client-id are returned.
50+
51+
Example:
52+
$ %s query %s rate-limit [channel-or-client-id]
53+
$ %s query %s rate-limit [channel-or-client-id] --denom=[denom]
54+
`,
55+
version.AppName, types.ModuleName, version.AppName, types.ModuleName,
56+
),
57+
),
58+
Args: cobra.ExactArgs(1),
3959
RunE: func(cmd *cobra.Command, args []string) error {
60+
channelOrClientId := args[0]
61+
denom, err := cmd.Flags().GetString(FlagDenom)
62+
if err != nil {
63+
return err
64+
}
65+
4066
clientCtx, err := client.GetClientQueryContext(cmd)
4167
if err != nil {
4268
return err
4369
}
4470
queryClient := types.NewQueryClient(clientCtx)
4571

46-
channelID := args[0]
47-
denom := args[1]
72+
if denom == "" {
73+
// Query all rate limits for the channel/client ID if denom is not specified
74+
req := &types.QueryRateLimitsByChannelOrClientIdRequest{
75+
ChannelOrClientId: channelOrClientId,
76+
}
77+
res, err := queryClient.RateLimitsByChannelOrClientId(context.Background(), req)
78+
if err != nil {
79+
return err
80+
}
81+
// Use PrintProto for slice types as PrintObjectLegacy might not work well
82+
return clientCtx.PrintProto(res)
83+
}
4884

85+
// Query specific rate limit if denom is provided
4986
req := &types.QueryRateLimitRequest{
50-
ChannelId: channelID,
51-
Denom: denom,
87+
Denom: denom,
88+
ChannelOrClientId: channelOrClientId,
89+
}
90+
res, err := queryClient.RateLimit(context.Background(), req)
91+
if err != nil {
92+
return err
93+
}
94+
95+
return clientCtx.PrintProto(res.RateLimit)
96+
},
97+
}
98+
99+
cmd.Flags().String(FlagDenom, "", "The denom identifying a specific rate limit")
100+
flags.AddQueryFlagsToCmd(cmd)
101+
102+
return cmd
103+
}
104+
105+
// GetCmdQueryAllRateLimits return all available rate limits.
106+
func GetCmdQueryAllRateLimits() *cobra.Command {
107+
cmd := &cobra.Command{
108+
Use: "list-rate-limits",
109+
Short: "Query all rate limits",
110+
Args: cobra.NoArgs,
111+
RunE: func(cmd *cobra.Command, args []string) error {
112+
clientCtx, err := client.GetClientQueryContext(cmd)
113+
if err != nil {
114+
return err
115+
}
116+
queryClient := types.NewQueryClient(clientCtx)
117+
118+
req := &types.QueryAllRateLimitsRequest{}
119+
res, err := queryClient.AllRateLimits(context.Background(), req)
120+
if err != nil {
121+
return err
122+
}
123+
124+
return clientCtx.PrintProto(res)
125+
},
126+
}
127+
128+
flags.AddQueryFlagsToCmd(cmd)
129+
130+
return cmd
131+
}
132+
133+
// GetCmdQueryRateLimitsByChainId return all rate limits that exist between this chain
134+
// and the specified ChainId
135+
func GetCmdQueryRateLimitsByChainId() *cobra.Command {
136+
cmd := &cobra.Command{
137+
Use: "rate-limits-by-chain [chain-id]",
138+
Short: "Query all rate limits associated with the channels/clients connecting to the given ChainID",
139+
Args: cobra.ExactArgs(1),
140+
RunE: func(cmd *cobra.Command, args []string) error {
141+
chainId := args[0]
142+
143+
clientCtx, err := client.GetClientQueryContext(cmd)
144+
if err != nil {
145+
return err
52146
}
147+
queryClient := types.NewQueryClient(clientCtx)
53148

54-
res, err := queryClient.RateLimit(cmd.Context(), req)
149+
req := &types.QueryRateLimitsByChainIdRequest{
150+
ChainId: chainId,
151+
}
152+
res, err := queryClient.RateLimitsByChainId(context.Background(), req)
55153
if err != nil {
56154
return err
57155
}
@@ -61,14 +159,15 @@ func GetCmdQueryRateLimit() *cobra.Command {
61159
}
62160

63161
flags.AddQueryFlagsToCmd(cmd)
162+
64163
return cmd
65164
}
66165

67-
// GetCmdQueryParams returns the command to query the module parameters
68-
func GetCmdQueryParams() *cobra.Command {
166+
// GetCmdQueryAllBlacklistedDenoms returns the command to query all blacklisted denoms
167+
func GetCmdQueryAllBlacklistedDenoms() *cobra.Command {
69168
cmd := &cobra.Command{
70-
Use: "params",
71-
Short: "Query the current rate-limiting module parameters",
169+
Use: "list-blacklisted-denoms",
170+
Short: "Query all blacklisted denoms",
72171
Args: cobra.NoArgs,
73172
RunE: func(cmd *cobra.Command, args []string) error {
74173
clientCtx, err := client.GetClientQueryContext(cmd)
@@ -77,7 +176,8 @@ func GetCmdQueryParams() *cobra.Command {
77176
}
78177
queryClient := types.NewQueryClient(clientCtx)
79178

80-
res, err := queryClient.Params(cmd.Context(), &types.QueryParamsRequest{})
179+
req := &types.QueryAllBlacklistedDenomsRequest{}
180+
res, err := queryClient.AllBlacklistedDenoms(context.Background(), req)
81181
if err != nil {
82182
return err
83183
}
@@ -89,4 +189,30 @@ func GetCmdQueryParams() *cobra.Command {
89189
flags.AddQueryFlagsToCmd(cmd)
90190
return cmd
91191
}
92-
*/
192+
193+
// GetCmdQueryAllWhitelistedAddresses returns the command to query all whitelisted address pairs
194+
func GetCmdQueryAllWhitelistedAddresses() *cobra.Command {
195+
cmd := &cobra.Command{
196+
Use: "list-whitelisted-addresses",
197+
Short: "Query all whitelisted address pairs",
198+
Args: cobra.NoArgs,
199+
RunE: func(cmd *cobra.Command, args []string) error {
200+
clientCtx, err := client.GetClientQueryContext(cmd)
201+
if err != nil {
202+
return err
203+
}
204+
queryClient := types.NewQueryClient(clientCtx)
205+
206+
req := &types.QueryAllWhitelistedAddressesRequest{}
207+
res, err := queryClient.AllWhitelistedAddresses(context.Background(), req)
208+
if err != nil {
209+
return err
210+
}
211+
212+
return clientCtx.PrintProto(res)
213+
},
214+
}
215+
216+
flags.AddQueryFlagsToCmd(cmd)
217+
return cmd
218+
}

modules/apps/rate-limiting/ibc_middleware.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ func (im IBCMiddleware) OnChanOpenInit(
6565
connectionHops []string,
6666
portID string,
6767
channelID string,
68-
// chanCap *channeltypes.Capability, // Removed: Not part of porttypes.Middleware interface
6968
counterparty channeltypes.Counterparty,
7069
version string,
7170
) (string, error) {
Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,55 @@
11
package keeper_test
22

3-
// // Helper function to check if an element is in an array
4-
// func isInArray(element string, arr []string) bool {
5-
// for _, e := range arr {
6-
// if e == element {
7-
// return true
8-
// }
9-
// }
10-
// return false
11-
// }
12-
13-
// func (s *KeeperTestSuite) TestDenomBlacklist() {
14-
// allDenoms := []string{"denom1", "denom2", "denom3", "denom4"}
15-
// denomsToBlacklist := []string{"denom1", "denom3"}
16-
17-
// // No denoms are currently blacklisted
18-
// for _, denom := range allDenoms {
19-
// isBlacklisted := s.App.RatelimitKeeper.IsDenomBlacklisted(s.Ctx, denom)
20-
// s.Require().False(isBlacklisted, "%s should not be blacklisted yet", denom)
21-
// }
22-
23-
// // Blacklist two denoms
24-
// for _, denom := range denomsToBlacklist {
25-
// s.App.RatelimitKeeper.AddDenomToBlacklist(s.Ctx, denom)
26-
// }
27-
28-
// // Confirm half the list was blacklisted and the others were not
29-
// for _, denom := range allDenoms {
30-
// isBlacklisted := s.App.RatelimitKeeper.IsDenomBlacklisted(s.Ctx, denom)
31-
32-
// if isInArray(denom, denomsToBlacklist) {
33-
// s.Require().True(isBlacklisted, "%s should have been blacklisted", denom)
34-
// } else {
35-
// s.Require().False(isBlacklisted, "%s should not have been blacklisted", denom)
36-
// }
37-
// }
38-
// actualBlacklistedDenoms := s.App.RatelimitKeeper.GetAllBlacklistedDenoms(s.Ctx)
39-
// s.Require().Len(actualBlacklistedDenoms, len(denomsToBlacklist), "number of blacklisted denoms")
40-
// s.Require().ElementsMatch(denomsToBlacklist, actualBlacklistedDenoms, "list of blacklisted denoms")
41-
42-
// // Finally, remove denoms from blacklist and confirm they were removed
43-
// for _, denom := range denomsToBlacklist {
44-
// s.App.RatelimitKeeper.RemoveDenomFromBlacklist(s.Ctx, denom)
45-
// }
46-
// for _, denom := range allDenoms {
47-
// isBlacklisted := s.App.RatelimitKeeper.IsDenomBlacklisted(s.Ctx, denom)
48-
49-
// if isInArray(denom, denomsToBlacklist) {
50-
// s.Require().False(isBlacklisted, "%s should have been removed from the blacklist", denom)
51-
// } else {
52-
// s.Require().False(isBlacklisted, "%s should never have been blacklisted", denom)
53-
// }
54-
// }
55-
// }
3+
// Helper function to check if an element is in an array
4+
func isInArray(element string, arr []string) bool {
5+
for _, e := range arr {
6+
if e == element {
7+
return true
8+
}
9+
}
10+
return false
11+
}
12+
13+
func (s *KeeperTestSuite) TestDenomBlacklist() {
14+
allDenoms := []string{"denom1", "denom2", "denom3", "denom4"}
15+
denomsToBlacklist := []string{"denom1", "denom3"}
16+
17+
// No denoms are currently blacklisted
18+
for _, denom := range allDenoms {
19+
isBlacklisted := s.chainA.GetSimApp().RateLimitKeeper.IsDenomBlacklisted(s.chainA.GetContext(), denom)
20+
s.Require().False(isBlacklisted, "%s should not be blacklisted yet", denom)
21+
}
22+
23+
// Blacklist two denoms
24+
for _, denom := range denomsToBlacklist {
25+
s.chainA.GetSimApp().RateLimitKeeper.AddDenomToBlacklist(s.chainA.GetContext(), denom)
26+
}
27+
28+
// Confirm half the list was blacklisted and the others were not
29+
for _, denom := range allDenoms {
30+
isBlacklisted := s.chainA.GetSimApp().RateLimitKeeper.IsDenomBlacklisted(s.chainA.GetContext(), denom)
31+
32+
if isInArray(denom, denomsToBlacklist) {
33+
s.Require().True(isBlacklisted, "%s should have been blacklisted", denom)
34+
} else {
35+
s.Require().False(isBlacklisted, "%s should not have been blacklisted", denom)
36+
}
37+
}
38+
actualBlacklistedDenoms := s.chainA.GetSimApp().RateLimitKeeper.GetAllBlacklistedDenoms(s.chainA.GetContext())
39+
s.Require().Len(actualBlacklistedDenoms, len(denomsToBlacklist), "number of blacklisted denoms")
40+
s.Require().ElementsMatch(denomsToBlacklist, actualBlacklistedDenoms, "list of blacklisted denoms")
41+
42+
// Finally, remove denoms from blacklist and confirm they were removed
43+
for _, denom := range denomsToBlacklist {
44+
s.chainA.GetSimApp().RateLimitKeeper.RemoveDenomFromBlacklist(s.chainA.GetContext(), denom)
45+
}
46+
for _, denom := range allDenoms {
47+
isBlacklisted := s.chainA.GetSimApp().RateLimitKeeper.IsDenomBlacklisted(s.chainA.GetContext(), denom)
48+
49+
if isInArray(denom, denomsToBlacklist) {
50+
s.Require().False(isBlacklisted, "%s should have been removed from the blacklist", denom)
51+
} else {
52+
s.Require().False(isBlacklisted, "%s should never have been blacklisted", denom)
53+
}
54+
}
55+
}

modules/apps/rate-limiting/keeper/keeper_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package keeper_test
22

33
import (
4+
// "fmt"
45
"testing"
56

67
testifysuite "github.com/stretchr/testify/suite"
@@ -337,4 +338,4 @@ func (suite *KeeperTestSuite) TestNewKeeper() {
337338
// suite.chainA.GetSimApp().ICAHostKeeper.WithICS4Wrapper(nil)
338339
// ics4Wrapper = suite.chainA.GetSimApp().ICAHostKeeper.GetICS4Wrapper()
339340
// suite.Require().Nil(ics4Wrapper)
340-
// }
341+
// }

0 commit comments

Comments
 (0)