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