Skip to content

Commit 8ea75e9

Browse files
Rahul MagantiRahulMaganti47
Rahul Maganti
authored andcommitted
evm: modify trimmed amt struct -> uint72
1 parent 86bbf12 commit 8ea75e9

10 files changed

+242
-214
lines changed

evm/src/NttManager/NttManager.sol

+8-8
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ contract NttManager is INttManager, NttManagerState {
138138
if (nativeTokenTransfer.toChain != chainId) {
139139
revert InvalidTargetChain(nativeTokenTransfer.toChain, chainId);
140140
}
141-
TrimmedAmount memory nativeTransferAmount =
141+
TrimmedAmount nativeTransferAmount =
142142
(nativeTokenTransfer.amount.untrim(tokenDecimals_)).trim(tokenDecimals_, tokenDecimals_);
143143

144144
address transferRecipient = fromWormholeFormat(nativeTokenTransfer.to);
@@ -296,8 +296,8 @@ contract NttManager is INttManager, NttManagerState {
296296
}
297297

298298
// trim amount after burning to ensure transfer amount matches (amount - fee)
299-
TrimmedAmount memory trimmedAmount = _trimTransferAmount(amount, recipientChain);
300-
TrimmedAmount memory internalAmount = trimmedAmount.shift(tokenDecimals_);
299+
TrimmedAmount trimmedAmount = _trimTransferAmount(amount, recipientChain);
300+
TrimmedAmount internalAmount = trimmedAmount.shift(tokenDecimals_);
301301

302302
// get the sequence for this transfer
303303
uint64 sequence = _useMessageSequence();
@@ -345,7 +345,7 @@ contract NttManager is INttManager, NttManagerState {
345345

346346
function _transfer(
347347
uint64 sequence,
348-
TrimmedAmount memory amount,
348+
TrimmedAmount amount,
349349
uint16 recipientChain,
350350
bytes32 recipient,
351351
address sender,
@@ -394,7 +394,7 @@ contract NttManager is INttManager, NttManagerState {
394394
);
395395

396396
// push it on the stack again to avoid a stack too deep error
397-
TrimmedAmount memory amt = amount;
397+
TrimmedAmount amt = amount;
398398
uint16 destinationChain = recipientChain;
399399

400400
emit TransferSent(
@@ -408,7 +408,7 @@ contract NttManager is INttManager, NttManagerState {
408408
function _mintOrUnlockToRecipient(
409409
bytes32 digest,
410410
address recipient,
411-
TrimmedAmount memory amount
411+
TrimmedAmount amount
412412
) internal {
413413
// calculate proper amount of tokens to unlock/mint to recipient
414414
// untrim the amount
@@ -447,14 +447,14 @@ contract NttManager is INttManager, NttManagerState {
447447
function _trimTransferAmount(
448448
uint256 amount,
449449
uint16 toChain
450-
) internal view returns (TrimmedAmount memory) {
450+
) internal view returns (TrimmedAmount) {
451451
uint8 toDecimals = _getPeersStorage()[toChain].tokenDecimals;
452452

453453
if (toDecimals == 0) {
454454
revert InvalidPeerDecimals();
455455
}
456456

457-
TrimmedAmount memory trimmedAmount;
457+
TrimmedAmount trimmedAmount;
458458
{
459459
trimmedAmount = amount.trim(tokenDecimals_, toDecimals);
460460
// don't deposit dust that can not be bridged due to the decimal shift

evm/src/libraries/RateLimiter.sol

+42-48
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,12 @@ abstract contract RateLimiter is IRateLimiter, IRateLimiterEvents {
8080
rateLimitDuration = _rateLimitDuration;
8181
}
8282

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;
8885
if (oldLimit.isNull()) {
8986
rateLimitParams.currentCapacity = limit;
9087
} else {
91-
TrimmedAmount memory currentCapacity = _getCurrentCapacity(rateLimitParams);
88+
TrimmedAmount currentCapacity = _getCurrentCapacity(rateLimitParams);
9289
rateLimitParams.currentCapacity =
9390
_calculateNewCurrentCapacity(limit, oldLimit, currentCapacity);
9491
}
@@ -97,7 +94,7 @@ abstract contract RateLimiter is IRateLimiter, IRateLimiterEvents {
9794
rateLimitParams.lastTxTimestamp = uint64(block.timestamp);
9895
}
9996

100-
function _setOutboundLimit(TrimmedAmount memory limit) internal {
97+
function _setOutboundLimit(TrimmedAmount limit) internal {
10198
_setLimit(limit, _getOutboundLimitParamsStorage());
10299
}
103100

@@ -106,7 +103,7 @@ abstract contract RateLimiter is IRateLimiter, IRateLimiterEvents {
106103
}
107104

108105
function getCurrentOutboundCapacity() public view returns (uint256) {
109-
TrimmedAmount memory trimmedCapacity = _getCurrentCapacity(getOutboundLimitParams());
106+
TrimmedAmount trimmedCapacity = _getCurrentCapacity(getOutboundLimitParams());
110107
uint8 decimals = tokenDecimals();
111108
return trimmedCapacity.untrim(decimals);
112109
}
@@ -119,7 +116,7 @@ abstract contract RateLimiter is IRateLimiter, IRateLimiterEvents {
119116
return _getOutboundQueueStorage()[queueSequence];
120117
}
121118

122-
function _setInboundLimit(TrimmedAmount memory limit, uint16 chainId_) internal {
119+
function _setInboundLimit(TrimmedAmount limit, uint16 chainId_) internal {
123120
_setLimit(limit, _getInboundLimitParamsStorage()[chainId_]);
124121
}
125122

@@ -128,7 +125,7 @@ abstract contract RateLimiter is IRateLimiter, IRateLimiterEvents {
128125
}
129126

130127
function getCurrentInboundCapacity(uint16 chainId_) public view returns (uint256) {
131-
TrimmedAmount memory trimmedCapacity = _getCurrentCapacity(getInboundLimitParams(chainId_));
128+
TrimmedAmount trimmedCapacity = _getCurrentCapacity(getInboundLimitParams(chainId_));
132129
uint8 decimals = tokenDecimals();
133130
return trimmedCapacity.untrim(decimals);
134131
}
@@ -147,11 +144,12 @@ abstract contract RateLimiter is IRateLimiter, IRateLimiterEvents {
147144
function _getCurrentCapacity(RateLimitParams memory rateLimitParams)
148145
internal
149146
view
150-
returns (TrimmedAmount memory capacity)
147+
returns (TrimmedAmount capacity)
151148
{
152149
// If the rate limit duration is 0 then the rate limiter is skipped
153150
if (rateLimitDuration == 0) {
154-
return TrimmedAmount(type(uint64).max, rateLimitParams.currentCapacity.getDecimals());
151+
return
152+
packTrimmedAmount(type(uint64).max, rateLimitParams.currentCapacity.getDecimals());
155153
}
156154

157155
// The capacity and rate limit are expressed as trimmed amounts, i.e.
@@ -167,13 +165,13 @@ abstract contract RateLimiter is IRateLimiter, IRateLimiterEvents {
167165
// Multiply (limit * timePassed), then divide by the duration.
168166
// Dividing first has terrible numerical stability --
169167
// 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
171169
// and we're performing arithmetic on u256 words.
172170
uint256 calculatedCapacity = rateLimitParams.currentCapacity.getAmount()
173171
+ (rateLimitParams.limit.getAmount() * timePassed) / rateLimitDuration;
174172

175173
uint256 result = min(calculatedCapacity, rateLimitParams.limit.getAmount());
176-
return TrimmedAmount(uint64(result), rateLimitParams.currentCapacity.getDecimals());
174+
return packTrimmedAmount(uint64(result), rateLimitParams.currentCapacity.getDecimals());
177175
}
178176
}
179177

@@ -185,42 +183,42 @@ abstract contract RateLimiter is IRateLimiter, IRateLimiterEvents {
185183
* @param currentCapacity The current capacity
186184
*/
187185
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());
199197
} else {
200-
difference = newLimit.sub(oldLimit);
201-
newCurrentCapacity = currentCapacity.add(difference);
198+
difference = newLimit - oldLimit;
199+
newCurrentCapacity = currentCapacity + difference;
202200
}
203201

204-
if (newCurrentCapacity.gt(newLimit)) {
202+
if (newCurrentCapacity > newLimit) {
205203
revert CapacityCannotExceedLimit(newCurrentCapacity, newLimit);
206204
}
207205
}
208206

209-
function _consumeOutboundAmount(TrimmedAmount memory amount) internal {
207+
function _consumeOutboundAmount(TrimmedAmount amount) internal {
210208
if (rateLimitDuration == 0) return;
211209
_consumeRateLimitAmount(
212210
amount, _getCurrentCapacity(getOutboundLimitParams()), _getOutboundLimitParamsStorage()
213211
);
214212
}
215213

216-
function _backfillOutboundAmount(TrimmedAmount memory amount) internal {
214+
function _backfillOutboundAmount(TrimmedAmount amount) internal {
217215
if (rateLimitDuration == 0) return;
218216
_backfillRateLimitAmount(
219217
amount, _getCurrentCapacity(getOutboundLimitParams()), _getOutboundLimitParamsStorage()
220218
);
221219
}
222220

223-
function _consumeInboundAmount(TrimmedAmount memory amount, uint16 chainId_) internal {
221+
function _consumeInboundAmount(TrimmedAmount amount, uint16 chainId_) internal {
224222
if (rateLimitDuration == 0) return;
225223
_consumeRateLimitAmount(
226224
amount,
@@ -229,7 +227,7 @@ abstract contract RateLimiter is IRateLimiter, IRateLimiterEvents {
229227
);
230228
}
231229

232-
function _backfillInboundAmount(TrimmedAmount memory amount, uint16 chainId_) internal {
230+
function _backfillInboundAmount(TrimmedAmount amount, uint16 chainId_) internal {
233231
if (rateLimitDuration == 0) return;
234232
_backfillRateLimitAmount(
235233
amount,
@@ -239,37 +237,33 @@ abstract contract RateLimiter is IRateLimiter, IRateLimiterEvents {
239237
}
240238

241239
function _consumeRateLimitAmount(
242-
TrimmedAmount memory amount,
243-
TrimmedAmount memory capacity,
240+
TrimmedAmount amount,
241+
TrimmedAmount capacity,
244242
RateLimitParams storage rateLimitParams
245243
) internal {
246244
rateLimitParams.lastTxTimestamp = uint64(block.timestamp);
247-
rateLimitParams.currentCapacity = capacity.sub(amount);
245+
rateLimitParams.currentCapacity = capacity - amount;
248246
}
249247

250248
/// @dev Refills the capacity by the given amount.
251249
/// This is used to replenish the capacity via backflows.
252250
function _backfillRateLimitAmount(
253-
TrimmedAmount memory amount,
254-
TrimmedAmount memory capacity,
251+
TrimmedAmount amount,
252+
TrimmedAmount capacity,
255253
RateLimitParams storage rateLimitParams
256254
) internal {
257255
rateLimitParams.lastTxTimestamp = uint64(block.timestamp);
258256
rateLimitParams.currentCapacity = capacity.saturatingAdd(amount).min(rateLimitParams.limit);
259257
}
260258

261-
function _isOutboundAmountRateLimited(TrimmedAmount memory amount)
262-
internal
263-
view
264-
returns (bool)
265-
{
259+
function _isOutboundAmountRateLimited(TrimmedAmount amount) internal view returns (bool) {
266260
return rateLimitDuration != 0
267261
? _isAmountRateLimited(_getCurrentCapacity(getOutboundLimitParams()), amount)
268262
: false;
269263
}
270264

271265
function _isInboundAmountRateLimited(
272-
TrimmedAmount memory amount,
266+
TrimmedAmount amount,
273267
uint16 chainId_
274268
) internal view returns (bool) {
275269
return rateLimitDuration != 0
@@ -278,15 +272,15 @@ abstract contract RateLimiter is IRateLimiter, IRateLimiterEvents {
278272
}
279273

280274
function _isAmountRateLimited(
281-
TrimmedAmount memory capacity,
282-
TrimmedAmount memory amount
275+
TrimmedAmount capacity,
276+
TrimmedAmount amount
283277
) internal pure returns (bool) {
284-
return capacity.lt(amount);
278+
return capacity < amount;
285279
}
286280

287281
function _enqueueOutboundTransfer(
288282
uint64 sequence,
289-
TrimmedAmount memory amount,
283+
TrimmedAmount amount,
290284
uint16 recipientChain,
291285
bytes32 recipient,
292286
address senderAddress,
@@ -306,7 +300,7 @@ abstract contract RateLimiter is IRateLimiter, IRateLimiterEvents {
306300

307301
function _enqueueInboundTransfer(
308302
bytes32 digest,
309-
TrimmedAmount memory amount,
303+
TrimmedAmount amount,
310304
address recipient
311305
) internal {
312306
_getInboundQueueStorage()[digest] = InboundQueuedTransfer({

evm/src/libraries/TransceiverStructs.sol

+2-2
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ library TransceiverStructs {
101101
pure
102102
returns (bytes memory encoded)
103103
{
104-
TrimmedAmount memory transferAmount = m.amount;
104+
TrimmedAmount transferAmount = m.amount;
105105
return abi.encodePacked(
106106
NTT_PREFIX,
107107
transferAmount.getDecimals(),
@@ -131,7 +131,7 @@ library TransceiverStructs {
131131
(numDecimals, offset) = encoded.asUint8Unchecked(offset);
132132
uint64 amount;
133133
(amount, offset) = encoded.asUint64Unchecked(offset);
134-
nativeTokenTransfer.amount = TrimmedAmount(amount, numDecimals);
134+
nativeTokenTransfer.amount = packTrimmedAmount(amount, numDecimals);
135135

136136
(nativeTokenTransfer.sourceToken, offset) = encoded.asBytes32Unchecked(offset);
137137
(nativeTokenTransfer.to, offset) = encoded.asBytes32Unchecked(offset);

0 commit comments

Comments
 (0)