@@ -80,15 +80,12 @@ abstract contract RateLimiter is IRateLimiter, IRateLimiterEvents {
80
80
rateLimitDuration = _rateLimitDuration;
81
81
}
82
82
83
- function _setLimit (
84
- TrimmedAmount memory limit ,
85
- RateLimitParams storage rateLimitParams
86
- ) internal {
87
- TrimmedAmount memory oldLimit = rateLimitParams.limit;
83
+ function _setLimit (TrimmedAmount limit , RateLimitParams storage rateLimitParams ) internal {
84
+ TrimmedAmount oldLimit = rateLimitParams.limit;
88
85
if (oldLimit.isNull ()) {
89
86
rateLimitParams.currentCapacity = limit;
90
87
} else {
91
- TrimmedAmount memory currentCapacity = _getCurrentCapacity (rateLimitParams);
88
+ TrimmedAmount currentCapacity = _getCurrentCapacity (rateLimitParams);
92
89
rateLimitParams.currentCapacity =
93
90
_calculateNewCurrentCapacity (limit, oldLimit, currentCapacity);
94
91
}
@@ -97,7 +94,7 @@ abstract contract RateLimiter is IRateLimiter, IRateLimiterEvents {
97
94
rateLimitParams.lastTxTimestamp = uint64 (block .timestamp );
98
95
}
99
96
100
- function _setOutboundLimit (TrimmedAmount memory limit ) internal {
97
+ function _setOutboundLimit (TrimmedAmount limit ) internal {
101
98
_setLimit (limit, _getOutboundLimitParamsStorage ());
102
99
}
103
100
@@ -106,7 +103,7 @@ abstract contract RateLimiter is IRateLimiter, IRateLimiterEvents {
106
103
}
107
104
108
105
function getCurrentOutboundCapacity () public view returns (uint256 ) {
109
- TrimmedAmount memory trimmedCapacity = _getCurrentCapacity (getOutboundLimitParams ());
106
+ TrimmedAmount trimmedCapacity = _getCurrentCapacity (getOutboundLimitParams ());
110
107
uint8 decimals = tokenDecimals ();
111
108
return trimmedCapacity.untrim (decimals);
112
109
}
@@ -119,7 +116,7 @@ abstract contract RateLimiter is IRateLimiter, IRateLimiterEvents {
119
116
return _getOutboundQueueStorage ()[queueSequence];
120
117
}
121
118
122
- function _setInboundLimit (TrimmedAmount memory limit , uint16 chainId_ ) internal {
119
+ function _setInboundLimit (TrimmedAmount limit , uint16 chainId_ ) internal {
123
120
_setLimit (limit, _getInboundLimitParamsStorage ()[chainId_]);
124
121
}
125
122
@@ -128,7 +125,7 @@ abstract contract RateLimiter is IRateLimiter, IRateLimiterEvents {
128
125
}
129
126
130
127
function getCurrentInboundCapacity (uint16 chainId_ ) public view returns (uint256 ) {
131
- TrimmedAmount memory trimmedCapacity = _getCurrentCapacity (getInboundLimitParams (chainId_));
128
+ TrimmedAmount trimmedCapacity = _getCurrentCapacity (getInboundLimitParams (chainId_));
132
129
uint8 decimals = tokenDecimals ();
133
130
return trimmedCapacity.untrim (decimals);
134
131
}
@@ -147,11 +144,12 @@ abstract contract RateLimiter is IRateLimiter, IRateLimiterEvents {
147
144
function _getCurrentCapacity (RateLimitParams memory rateLimitParams )
148
145
internal
149
146
view
150
- returns (TrimmedAmount memory capacity )
147
+ returns (TrimmedAmount capacity )
151
148
{
152
149
// If the rate limit duration is 0 then the rate limiter is skipped
153
150
if (rateLimitDuration == 0 ) {
154
- return TrimmedAmount (type (uint64 ).max, rateLimitParams.currentCapacity.getDecimals ());
151
+ return
152
+ packTrimmedAmount (type (uint64 ).max, rateLimitParams.currentCapacity.getDecimals ());
155
153
}
156
154
157
155
// The capacity and rate limit are expressed as trimmed amounts, i.e.
@@ -167,13 +165,13 @@ abstract contract RateLimiter is IRateLimiter, IRateLimiterEvents {
167
165
// Multiply (limit * timePassed), then divide by the duration.
168
166
// Dividing first has terrible numerical stability --
169
167
// when rateLimitDuration is close to the limit, there is significant rounding error.
170
- // We are safe to multiply first, since these numbers are u64 TrimmedAmount memory types
168
+ // We are safe to multiply first, since these numbers are u64 TrimmedAmount types
171
169
// and we're performing arithmetic on u256 words.
172
170
uint256 calculatedCapacity = rateLimitParams.currentCapacity.getAmount ()
173
171
+ (rateLimitParams.limit.getAmount () * timePassed) / rateLimitDuration;
174
172
175
173
uint256 result = min (calculatedCapacity, rateLimitParams.limit.getAmount ());
176
- return TrimmedAmount (uint64 (result), rateLimitParams.currentCapacity.getDecimals ());
174
+ return packTrimmedAmount (uint64 (result), rateLimitParams.currentCapacity.getDecimals ());
177
175
}
178
176
}
179
177
@@ -185,42 +183,42 @@ abstract contract RateLimiter is IRateLimiter, IRateLimiterEvents {
185
183
* @param currentCapacity The current capacity
186
184
*/
187
185
function _calculateNewCurrentCapacity (
188
- TrimmedAmount memory newLimit ,
189
- TrimmedAmount memory oldLimit ,
190
- TrimmedAmount memory currentCapacity
191
- ) internal pure returns (TrimmedAmount memory newCurrentCapacity ) {
192
- TrimmedAmount memory difference;
193
-
194
- if (oldLimit. gt ( newLimit) ) {
195
- difference = oldLimit. sub ( newLimit) ;
196
- newCurrentCapacity = currentCapacity. gt ( difference)
197
- ? currentCapacity. sub ( difference)
198
- : TrimmedAmount (0 , currentCapacity.decimals );
186
+ TrimmedAmount newLimit ,
187
+ TrimmedAmount oldLimit ,
188
+ TrimmedAmount currentCapacity
189
+ ) internal pure returns (TrimmedAmount newCurrentCapacity ) {
190
+ TrimmedAmount difference;
191
+
192
+ if (oldLimit > newLimit) {
193
+ difference = oldLimit - newLimit;
194
+ newCurrentCapacity = currentCapacity > difference
195
+ ? currentCapacity - difference
196
+ : packTrimmedAmount (0 , currentCapacity.getDecimals () );
199
197
} else {
200
- difference = newLimit. sub ( oldLimit) ;
201
- newCurrentCapacity = currentCapacity. add ( difference) ;
198
+ difference = newLimit - oldLimit;
199
+ newCurrentCapacity = currentCapacity + difference;
202
200
}
203
201
204
- if (newCurrentCapacity. gt ( newLimit) ) {
202
+ if (newCurrentCapacity > newLimit) {
205
203
revert CapacityCannotExceedLimit (newCurrentCapacity, newLimit);
206
204
}
207
205
}
208
206
209
- function _consumeOutboundAmount (TrimmedAmount memory amount ) internal {
207
+ function _consumeOutboundAmount (TrimmedAmount amount ) internal {
210
208
if (rateLimitDuration == 0 ) return ;
211
209
_consumeRateLimitAmount (
212
210
amount, _getCurrentCapacity (getOutboundLimitParams ()), _getOutboundLimitParamsStorage ()
213
211
);
214
212
}
215
213
216
- function _backfillOutboundAmount (TrimmedAmount memory amount ) internal {
214
+ function _backfillOutboundAmount (TrimmedAmount amount ) internal {
217
215
if (rateLimitDuration == 0 ) return ;
218
216
_backfillRateLimitAmount (
219
217
amount, _getCurrentCapacity (getOutboundLimitParams ()), _getOutboundLimitParamsStorage ()
220
218
);
221
219
}
222
220
223
- function _consumeInboundAmount (TrimmedAmount memory amount , uint16 chainId_ ) internal {
221
+ function _consumeInboundAmount (TrimmedAmount amount , uint16 chainId_ ) internal {
224
222
if (rateLimitDuration == 0 ) return ;
225
223
_consumeRateLimitAmount (
226
224
amount,
@@ -229,7 +227,7 @@ abstract contract RateLimiter is IRateLimiter, IRateLimiterEvents {
229
227
);
230
228
}
231
229
232
- function _backfillInboundAmount (TrimmedAmount memory amount , uint16 chainId_ ) internal {
230
+ function _backfillInboundAmount (TrimmedAmount amount , uint16 chainId_ ) internal {
233
231
if (rateLimitDuration == 0 ) return ;
234
232
_backfillRateLimitAmount (
235
233
amount,
@@ -239,37 +237,33 @@ abstract contract RateLimiter is IRateLimiter, IRateLimiterEvents {
239
237
}
240
238
241
239
function _consumeRateLimitAmount (
242
- TrimmedAmount memory amount ,
243
- TrimmedAmount memory capacity ,
240
+ TrimmedAmount amount ,
241
+ TrimmedAmount capacity ,
244
242
RateLimitParams storage rateLimitParams
245
243
) internal {
246
244
rateLimitParams.lastTxTimestamp = uint64 (block .timestamp );
247
- rateLimitParams.currentCapacity = capacity. sub ( amount) ;
245
+ rateLimitParams.currentCapacity = capacity - amount;
248
246
}
249
247
250
248
/// @dev Refills the capacity by the given amount.
251
249
/// This is used to replenish the capacity via backflows.
252
250
function _backfillRateLimitAmount (
253
- TrimmedAmount memory amount ,
254
- TrimmedAmount memory capacity ,
251
+ TrimmedAmount amount ,
252
+ TrimmedAmount capacity ,
255
253
RateLimitParams storage rateLimitParams
256
254
) internal {
257
255
rateLimitParams.lastTxTimestamp = uint64 (block .timestamp );
258
256
rateLimitParams.currentCapacity = capacity.saturatingAdd (amount).min (rateLimitParams.limit);
259
257
}
260
258
261
- function _isOutboundAmountRateLimited (TrimmedAmount memory amount )
262
- internal
263
- view
264
- returns (bool )
265
- {
259
+ function _isOutboundAmountRateLimited (TrimmedAmount amount ) internal view returns (bool ) {
266
260
return rateLimitDuration != 0
267
261
? _isAmountRateLimited (_getCurrentCapacity (getOutboundLimitParams ()), amount)
268
262
: false ;
269
263
}
270
264
271
265
function _isInboundAmountRateLimited (
272
- TrimmedAmount memory amount ,
266
+ TrimmedAmount amount ,
273
267
uint16 chainId_
274
268
) internal view returns (bool ) {
275
269
return rateLimitDuration != 0
@@ -278,15 +272,15 @@ abstract contract RateLimiter is IRateLimiter, IRateLimiterEvents {
278
272
}
279
273
280
274
function _isAmountRateLimited (
281
- TrimmedAmount memory capacity ,
282
- TrimmedAmount memory amount
275
+ TrimmedAmount capacity ,
276
+ TrimmedAmount amount
283
277
) internal pure returns (bool ) {
284
- return capacity. lt ( amount) ;
278
+ return capacity < amount;
285
279
}
286
280
287
281
function _enqueueOutboundTransfer (
288
282
uint64 sequence ,
289
- TrimmedAmount memory amount ,
283
+ TrimmedAmount amount ,
290
284
uint16 recipientChain ,
291
285
bytes32 recipient ,
292
286
address senderAddress ,
@@ -306,7 +300,7 @@ abstract contract RateLimiter is IRateLimiter, IRateLimiterEvents {
306
300
307
301
function _enqueueInboundTransfer (
308
302
bytes32 digest ,
309
- TrimmedAmount memory amount ,
303
+ TrimmedAmount amount ,
310
304
address recipient
311
305
) internal {
312
306
_getInboundQueueStorage ()[digest] = InboundQueuedTransfer ({
0 commit comments