-
Notifications
You must be signed in to change notification settings - Fork 751
Expand file tree
/
Copy pathrate_limiting_v2_test.go
More file actions
212 lines (175 loc) · 7.4 KB
/
rate_limiting_v2_test.go
File metadata and controls
212 lines (175 loc) · 7.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
//go:build !test_e2e
package ratelimiting
import (
"context"
"testing"
"time"
"github.com/cosmos/interchaintest/v10/ibc"
"github.com/cosmos/interchaintest/v10/testutil"
testifysuite "github.com/stretchr/testify/suite"
sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/cosmos/ibc-go/e2e/testsuite"
"github.com/cosmos/ibc-go/e2e/testsuite/query"
"github.com/cosmos/ibc-go/e2e/testvalues"
ratelimitingtypes "github.com/cosmos/ibc-go/v10/modules/apps/rate-limiting/types"
transfertypes "github.com/cosmos/ibc-go/v10/modules/apps/transfer/types"
clienttypes "github.com/cosmos/ibc-go/v10/modules/core/02-client/types"
)
type RateLimV2TestSuite struct {
testsuite.E2ETestSuite
}
func TestRateLimitV2Suite(t *testing.T) {
testifysuite.Run(t, new(RateLimV2TestSuite))
}
func (s *RateLimV2TestSuite) SetupSuite() {
s.SetupChains(context.TODO(), 2, nil, func(options *testsuite.ChainOptions) {
options.RelayerCount = 1
})
}
func (s *RateLimV2TestSuite) TestRateLimitV2HappyPath() {
t := s.T()
ctx := context.TODO()
testName := t.Name()
chainA, chainB := s.GetChains()
userA := s.CreateUserOnChainA(ctx, testvalues.StartingTokenAmount)
userB := s.CreateUserOnChainB(ctx, testvalues.StartingTokenAmount)
authority, err := query.ModuleAccountAddress(ctx, govtypes.ModuleName, chainA)
s.Require().NoError(err)
s.Require().NotNil(authority)
s.CreatePaths(ibc.DefaultClientOpts(), s.TransferChannelOptions(), testName)
relayer := s.GetRelayerForTest(testName)
s.StartRelayer(relayer, testName)
chanAB := s.GetChannelBetweenChains(testName, chainA, chainB)
denomA := chainA.Config().Denom
// Test IBC v2 transfer with rate limiting
t.Run("IBC v2 transfer with rate limiting: happy path", func(_ *testing.T) {
// Set up a rate limit for the channel
sendPercentage := int64(10) // 10% of total supply
recvPercentage := int64(10) // 10% of total supply
s.addRateLimit(ctx, chainA, userA, denomA, chanAB.ChannelID, authority.String(), sendPercentage, recvPercentage, 1)
// Verify rate limit was set
rateLimit := s.rateLimit(ctx, chainA, denomA, chanAB.ChannelID)
s.Require().NotNil(rateLimit)
s.Require().Equal(rateLimit.Quota.MaxPercentSend.Int64(), sendPercentage)
s.Require().Equal(rateLimit.Quota.MaxPercentRecv.Int64(), recvPercentage)
// Create IBC v2 transfer using aliasing
transferAmount := testvalues.DefaultTransferAmount(denomA)
// Create IBC v2 transfer message with UseAliasing = true
timeoutTimestamp := uint64(time.Now().Add(time.Hour).UnixNano())
msgTransfer := transfertypes.NewMsgTransferWithEncoding(
chanAB.PortID,
chanAB.ChannelID,
transferAmount,
userA.FormattedAddress(),
userB.FormattedAddress(),
clienttypes.Height{}, // IBC v2 requires timeoutHeight to be zero
timeoutTimestamp,
"", // memo
"proto", // encoding
true, // useAliasing - this makes it IBC v2
)
// Broadcast the IBC v2 transfer
txResp := s.BroadcastMessages(ctx, chainA, userA, msgTransfer)
s.AssertTxSuccess(txResp)
// Verify the transfer was successful by checking balances
s.Require().NoError(testutil.WaitForBlocks(ctx, 2, chainA, chainB))
// Check that the rate limit flow was updated
rateLimitAfter := s.rateLimit(ctx, chainA, denomA, chanAB.ChannelID)
s.Require().NotNil(rateLimitAfter)
s.Require().Equal(transferAmount.Amount.Int64(), rateLimitAfter.Flow.Outflow.Int64())
})
t.Run("IBC v2 transfer exceeds rate limit: should fail", func(_ *testing.T) {
// Reset the rate limit to a very small amount
sendPercentage := int64(1) // 1% of total supply
recvPercentage := int64(1) // 1% of total supply
s.updateRateLimit(ctx, chainA, userA, denomA, chanAB.ChannelID, authority.String(), sendPercentage, recvPercentage)
// Verify rate limit was updated
rateLimit := s.rateLimit(ctx, chainA, denomA, chanAB.ChannelID)
s.Require().NotNil(rateLimit)
s.Require().Equal(rateLimit.Quota.MaxPercentSend.Int64(), sendPercentage)
// Try to send a large amount that should exceed the rate limit
largeAmount := sdk.NewInt64Coin(denomA, 1000000) // Large amount
timeoutTimestamp := uint64(time.Now().Add(time.Hour).UnixNano())
msgTransfer := transfertypes.NewMsgTransferWithEncoding(
chanAB.PortID,
chanAB.ChannelID,
largeAmount,
userA.FormattedAddress(),
userB.FormattedAddress(),
clienttypes.Height{}, // IBC v2 requires timeoutHeight to be zero
timeoutTimestamp,
"", // memo
"proto", // encoding
true, // useAliasing - this makes it IBC v2
)
// This transfer should fail due to rate limiting
txResp := s.BroadcastMessages(ctx, chainA, userA, msgTransfer)
s.AssertTxFailure(txResp, ratelimitingtypes.ErrQuotaExceeded)
})
t.Run("IBC v2 transfer after rate limit reset: should succeed", func(_ *testing.T) {
// Reset the rate limit flow
s.resetRateLimit(ctx, chainA, userA, denomA, chanAB.ChannelID, authority.String())
// Verify flow was reset
rateLimit := s.rateLimit(ctx, chainA, denomA, chanAB.ChannelID)
s.Require().NotNil(rateLimit)
s.Require().Zero(rateLimit.Flow.Outflow.Int64())
// Now the transfer should succeed again
transferAmount := testvalues.DefaultTransferAmount(denomA)
timeoutTimestamp := uint64(time.Now().Add(time.Hour).UnixNano())
msgTransfer := transfertypes.NewMsgTransferWithEncoding(
chanAB.PortID,
chanAB.ChannelID,
transferAmount,
userA.FormattedAddress(),
userB.FormattedAddress(),
clienttypes.Height{}, // IBC v2 requires timeoutHeight to be zero
timeoutTimestamp,
"", // memo
"proto", // encoding
true, // useAliasing - this makes it IBC v2
)
txResp := s.BroadcastMessages(ctx, chainA, userA, msgTransfer)
s.AssertTxSuccess(txResp)
})
}
// Helper methods (reused from the original rate limiting test)
func (s *RateLimV2TestSuite) rateLimit(ctx context.Context, chain ibc.Chain, denom, chanID string) *ratelimitingtypes.RateLimit {
respRateLim, err := query.GRPCQuery[ratelimitingtypes.QueryRateLimitResponse](ctx, chain, &ratelimitingtypes.QueryRateLimitRequest{
Denom: denom,
ChannelOrClientId: chanID,
})
s.Require().NoError(err)
return respRateLim.RateLimit
}
func (s *RateLimV2TestSuite) addRateLimit(ctx context.Context, chain ibc.Chain, user ibc.Wallet, denom, chanID, authority string, sendPercent, recvPercent, duration int64) {
msg := &ratelimitingtypes.MsgAddRateLimit{
Signer: authority,
Denom: denom,
ChannelOrClientId: chanID,
MaxPercentSend: sdkmath.NewInt(sendPercent),
MaxPercentRecv: sdkmath.NewInt(recvPercent),
DurationHours: uint64(duration),
}
s.ExecuteAndPassGovV1Proposal(ctx, msg, chain, user)
}
func (s *RateLimV2TestSuite) resetRateLimit(ctx context.Context, chain ibc.Chain, user ibc.Wallet, denom, chanID, authority string) {
msg := &ratelimitingtypes.MsgResetRateLimit{
Signer: authority,
Denom: denom,
ChannelOrClientId: chanID,
}
s.ExecuteAndPassGovV1Proposal(ctx, msg, chain, user)
}
func (s *RateLimV2TestSuite) updateRateLimit(ctx context.Context, chain ibc.Chain, user ibc.Wallet, denom, chanID, authority string, sendPercent, recvPercent int64) {
msg := &ratelimitingtypes.MsgUpdateRateLimit{
Signer: authority,
Denom: denom,
ChannelOrClientId: chanID,
MaxPercentSend: sdkmath.NewInt(sendPercent),
MaxPercentRecv: sdkmath.NewInt(recvPercent),
DurationHours: 1,
}
s.ExecuteAndPassGovV1Proposal(ctx, msg, chain, user)
}