@@ -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
@@ -132,7 +129,7 @@ abstract contract RateLimiter is IRateLimiter, IRateLimiterEvents {
132
129
}
133
130
134
131
function getCurrentInboundCapacity (uint16 chainId_ ) public view returns (uint256 ) {
135
- TrimmedAmount memory trimmedCapacity = _getCurrentCapacity (_getInboundLimitParams (chainId_));
132
+ TrimmedAmount trimmedCapacity = _getCurrentCapacity (_getInboundLimitParams (chainId_));
136
133
uint8 decimals = tokenDecimals ();
137
134
return trimmedCapacity.untrim (decimals);
138
135
}
@@ -151,11 +148,12 @@ abstract contract RateLimiter is IRateLimiter, IRateLimiterEvents {
151
148
function _getCurrentCapacity (RateLimitParams memory rateLimitParams )
152
149
internal
153
150
view
154
- returns (TrimmedAmount memory capacity )
151
+ returns (TrimmedAmount capacity )
155
152
{
156
153
// If the rate limit duration is 0 then the rate limiter is skipped
157
154
if (rateLimitDuration == 0 ) {
158
- return TrimmedAmount (type (uint64 ).max, rateLimitParams.currentCapacity.getDecimals ());
155
+ return
156
+ packTrimmedAmount (type (uint64 ).max, rateLimitParams.currentCapacity.getDecimals ());
159
157
}
160
158
161
159
// The capacity and rate limit are expressed as trimmed amounts, i.e.
@@ -171,13 +169,13 @@ abstract contract RateLimiter is IRateLimiter, IRateLimiterEvents {
171
169
// Multiply (limit * timePassed), then divide by the duration.
172
170
// Dividing first has terrible numerical stability --
173
171
// when rateLimitDuration is close to the limit, there is significant rounding error.
174
- // We are safe to multiply first, since these numbers are u64 TrimmedAmount memory types
172
+ // We are safe to multiply first, since these numbers are u64 TrimmedAmount types
175
173
// and we're performing arithmetic on u256 words.
176
174
uint256 calculatedCapacity = rateLimitParams.currentCapacity.getAmount ()
177
175
+ (rateLimitParams.limit.getAmount () * timePassed) / rateLimitDuration;
178
176
179
177
uint256 result = min (calculatedCapacity, rateLimitParams.limit.getAmount ());
180
- return TrimmedAmount (uint64 (result), rateLimitParams.currentCapacity.getDecimals ());
178
+ return packTrimmedAmount (uint64 (result), rateLimitParams.currentCapacity.getDecimals ());
181
179
}
182
180
}
183
181
@@ -189,42 +187,42 @@ abstract contract RateLimiter is IRateLimiter, IRateLimiterEvents {
189
187
* @param currentCapacity The current capacity
190
188
*/
191
189
function _calculateNewCurrentCapacity (
192
- TrimmedAmount memory newLimit ,
193
- TrimmedAmount memory oldLimit ,
194
- TrimmedAmount memory currentCapacity
195
- ) internal pure returns (TrimmedAmount memory newCurrentCapacity ) {
196
- TrimmedAmount memory difference;
197
-
198
- if (oldLimit. gt ( newLimit) ) {
199
- difference = oldLimit. sub ( newLimit) ;
200
- newCurrentCapacity = currentCapacity. gt ( difference)
201
- ? currentCapacity. sub ( difference)
202
- : TrimmedAmount (0 , currentCapacity.decimals );
190
+ TrimmedAmount newLimit ,
191
+ TrimmedAmount oldLimit ,
192
+ TrimmedAmount currentCapacity
193
+ ) internal pure returns (TrimmedAmount newCurrentCapacity ) {
194
+ TrimmedAmount difference;
195
+
196
+ if (oldLimit > newLimit) {
197
+ difference = oldLimit - newLimit;
198
+ newCurrentCapacity = currentCapacity > difference
199
+ ? currentCapacity - difference
200
+ : packTrimmedAmount (0 , currentCapacity.getDecimals () );
203
201
} else {
204
- difference = newLimit. sub ( oldLimit) ;
205
- newCurrentCapacity = currentCapacity. add ( difference) ;
202
+ difference = newLimit - oldLimit;
203
+ newCurrentCapacity = currentCapacity + difference;
206
204
}
207
205
208
- if (newCurrentCapacity. gt ( newLimit) ) {
206
+ if (newCurrentCapacity > newLimit) {
209
207
revert CapacityCannotExceedLimit (newCurrentCapacity, newLimit);
210
208
}
211
209
}
212
210
213
- function _consumeOutboundAmount (TrimmedAmount memory amount ) internal {
211
+ function _consumeOutboundAmount (TrimmedAmount amount ) internal {
214
212
if (rateLimitDuration == 0 ) return ;
215
213
_consumeRateLimitAmount (
216
214
amount, _getCurrentCapacity (_getOutboundLimitParams ()), _getOutboundLimitParamsStorage ()
217
215
);
218
216
}
219
217
220
- function _backfillOutboundAmount (TrimmedAmount memory amount ) internal {
218
+ function _backfillOutboundAmount (TrimmedAmount amount ) internal {
221
219
if (rateLimitDuration == 0 ) return ;
222
220
_backfillRateLimitAmount (
223
221
amount, _getCurrentCapacity (_getOutboundLimitParams ()), _getOutboundLimitParamsStorage ()
224
222
);
225
223
}
226
224
227
- function _consumeInboundAmount (TrimmedAmount memory amount , uint16 chainId_ ) internal {
225
+ function _consumeInboundAmount (TrimmedAmount amount , uint16 chainId_ ) internal {
228
226
if (rateLimitDuration == 0 ) return ;
229
227
_consumeRateLimitAmount (
230
228
amount,
@@ -233,7 +231,7 @@ abstract contract RateLimiter is IRateLimiter, IRateLimiterEvents {
233
231
);
234
232
}
235
233
236
- function _backfillInboundAmount (TrimmedAmount memory amount , uint16 chainId_ ) internal {
234
+ function _backfillInboundAmount (TrimmedAmount amount , uint16 chainId_ ) internal {
237
235
if (rateLimitDuration == 0 ) return ;
238
236
_backfillRateLimitAmount (
239
237
amount,
@@ -243,37 +241,33 @@ abstract contract RateLimiter is IRateLimiter, IRateLimiterEvents {
243
241
}
244
242
245
243
function _consumeRateLimitAmount (
246
- TrimmedAmount memory amount ,
247
- TrimmedAmount memory capacity ,
244
+ TrimmedAmount amount ,
245
+ TrimmedAmount capacity ,
248
246
RateLimitParams storage rateLimitParams
249
247
) internal {
250
248
rateLimitParams.lastTxTimestamp = uint64 (block .timestamp );
251
- rateLimitParams.currentCapacity = capacity. sub ( amount) ;
249
+ rateLimitParams.currentCapacity = capacity - amount;
252
250
}
253
251
254
252
/// @dev Refills the capacity by the given amount.
255
253
/// This is used to replenish the capacity via backflows.
256
254
function _backfillRateLimitAmount (
257
- TrimmedAmount memory amount ,
258
- TrimmedAmount memory capacity ,
255
+ TrimmedAmount amount ,
256
+ TrimmedAmount capacity ,
259
257
RateLimitParams storage rateLimitParams
260
258
) internal {
261
259
rateLimitParams.lastTxTimestamp = uint64 (block .timestamp );
262
260
rateLimitParams.currentCapacity = capacity.saturatingAdd (amount).min (rateLimitParams.limit);
263
261
}
264
262
265
- function _isOutboundAmountRateLimited (TrimmedAmount memory amount )
266
- internal
267
- view
268
- returns (bool )
269
- {
263
+ function _isOutboundAmountRateLimited (TrimmedAmount amount ) internal view returns (bool ) {
270
264
return rateLimitDuration != 0
271
265
? _isAmountRateLimited (_getCurrentCapacity (_getOutboundLimitParams ()), amount)
272
266
: false ;
273
267
}
274
268
275
269
function _isInboundAmountRateLimited (
276
- TrimmedAmount memory amount ,
270
+ TrimmedAmount amount ,
277
271
uint16 chainId_
278
272
) internal view returns (bool ) {
279
273
return rateLimitDuration != 0
@@ -282,15 +276,15 @@ abstract contract RateLimiter is IRateLimiter, IRateLimiterEvents {
282
276
}
283
277
284
278
function _isAmountRateLimited (
285
- TrimmedAmount memory capacity ,
286
- TrimmedAmount memory amount
279
+ TrimmedAmount capacity ,
280
+ TrimmedAmount amount
287
281
) internal pure returns (bool ) {
288
- return capacity. lt ( amount) ;
282
+ return capacity < amount;
289
283
}
290
284
291
285
function _enqueueOutboundTransfer (
292
286
uint64 sequence ,
293
- TrimmedAmount memory amount ,
287
+ TrimmedAmount amount ,
294
288
uint16 recipientChain ,
295
289
bytes32 recipient ,
296
290
address senderAddress ,
@@ -310,7 +304,7 @@ abstract contract RateLimiter is IRateLimiter, IRateLimiterEvents {
310
304
311
305
function _enqueueInboundTransfer (
312
306
bytes32 digest ,
313
- TrimmedAmount memory amount ,
307
+ TrimmedAmount amount ,
314
308
address recipient
315
309
) internal {
316
310
_getInboundQueueStorage ()[digest] = InboundQueuedTransfer ({
0 commit comments