Skip to content

Commit 971cdd8

Browse files
author
Rahul Maganti
committed
evm: modify trimmed amt struct -> uint72
1 parent 1470fc6 commit 971cdd8

10 files changed

+283
-248
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

+47-51
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

@@ -132,7 +129,7 @@ abstract contract RateLimiter is IRateLimiter, IRateLimiterEvents {
132129
}
133130

134131
function getCurrentInboundCapacity(uint16 chainId_) public view returns (uint256) {
135-
TrimmedAmount memory trimmedCapacity = _getCurrentCapacity(_getInboundLimitParams(chainId_));
132+
TrimmedAmount trimmedCapacity = _getCurrentCapacity(_getInboundLimitParams(chainId_));
136133
uint8 decimals = tokenDecimals();
137134
return trimmedCapacity.untrim(decimals);
138135
}
@@ -151,11 +148,13 @@ abstract contract RateLimiter is IRateLimiter, IRateLimiterEvents {
151148
function _getCurrentCapacity(RateLimitParams memory rateLimitParams)
152149
internal
153150
view
154-
returns (TrimmedAmount memory capacity)
151+
returns (TrimmedAmount capacity)
155152
{
156153
// If the rate limit duration is 0 then the rate limiter is skipped
157154
if (rateLimitDuration == 0) {
158-
return TrimmedAmount(type(uint64).max, rateLimitParams.currentCapacity.getDecimals());
155+
return packTrimmedAmount(
156+
type(uint64).max, rateLimitParams.currentCapacity.unpackDecimals()
157+
);
159158
}
160159

161160
// The capacity and rate limit are expressed as trimmed amounts, i.e.
@@ -171,13 +170,14 @@ abstract contract RateLimiter is IRateLimiter, IRateLimiterEvents {
171170
// Multiply (limit * timePassed), then divide by the duration.
172171
// Dividing first has terrible numerical stability --
173172
// 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
175174
// 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;
178177

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());
181181
}
182182
}
183183

@@ -189,42 +189,42 @@ abstract contract RateLimiter is IRateLimiter, IRateLimiterEvents {
189189
* @param currentCapacity The current capacity
190190
*/
191191
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());
203203
} else {
204-
difference = newLimit.sub(oldLimit);
205-
newCurrentCapacity = currentCapacity.add(difference);
204+
difference = newLimit - oldLimit;
205+
newCurrentCapacity = currentCapacity + difference;
206206
}
207207

208-
if (newCurrentCapacity.gt(newLimit)) {
208+
if (newCurrentCapacity > newLimit) {
209209
revert CapacityCannotExceedLimit(newCurrentCapacity, newLimit);
210210
}
211211
}
212212

213-
function _consumeOutboundAmount(TrimmedAmount memory amount) internal {
213+
function _consumeOutboundAmount(TrimmedAmount amount) internal {
214214
if (rateLimitDuration == 0) return;
215215
_consumeRateLimitAmount(
216216
amount, _getCurrentCapacity(_getOutboundLimitParams()), _getOutboundLimitParamsStorage()
217217
);
218218
}
219219

220-
function _backfillOutboundAmount(TrimmedAmount memory amount) internal {
220+
function _backfillOutboundAmount(TrimmedAmount amount) internal {
221221
if (rateLimitDuration == 0) return;
222222
_backfillRateLimitAmount(
223223
amount, _getCurrentCapacity(_getOutboundLimitParams()), _getOutboundLimitParamsStorage()
224224
);
225225
}
226226

227-
function _consumeInboundAmount(TrimmedAmount memory amount, uint16 chainId_) internal {
227+
function _consumeInboundAmount(TrimmedAmount amount, uint16 chainId_) internal {
228228
if (rateLimitDuration == 0) return;
229229
_consumeRateLimitAmount(
230230
amount,
@@ -233,7 +233,7 @@ abstract contract RateLimiter is IRateLimiter, IRateLimiterEvents {
233233
);
234234
}
235235

236-
function _backfillInboundAmount(TrimmedAmount memory amount, uint16 chainId_) internal {
236+
function _backfillInboundAmount(TrimmedAmount amount, uint16 chainId_) internal {
237237
if (rateLimitDuration == 0) return;
238238
_backfillRateLimitAmount(
239239
amount,
@@ -243,37 +243,33 @@ abstract contract RateLimiter is IRateLimiter, IRateLimiterEvents {
243243
}
244244

245245
function _consumeRateLimitAmount(
246-
TrimmedAmount memory amount,
247-
TrimmedAmount memory capacity,
246+
TrimmedAmount amount,
247+
TrimmedAmount capacity,
248248
RateLimitParams storage rateLimitParams
249249
) internal {
250250
rateLimitParams.lastTxTimestamp = uint64(block.timestamp);
251-
rateLimitParams.currentCapacity = capacity.sub(amount);
251+
rateLimitParams.currentCapacity = capacity - amount;
252252
}
253253

254254
/// @dev Refills the capacity by the given amount.
255255
/// This is used to replenish the capacity via backflows.
256256
function _backfillRateLimitAmount(
257-
TrimmedAmount memory amount,
258-
TrimmedAmount memory capacity,
257+
TrimmedAmount amount,
258+
TrimmedAmount capacity,
259259
RateLimitParams storage rateLimitParams
260260
) internal {
261261
rateLimitParams.lastTxTimestamp = uint64(block.timestamp);
262262
rateLimitParams.currentCapacity = capacity.saturatingAdd(amount).min(rateLimitParams.limit);
263263
}
264264

265-
function _isOutboundAmountRateLimited(TrimmedAmount memory amount)
266-
internal
267-
view
268-
returns (bool)
269-
{
265+
function _isOutboundAmountRateLimited(TrimmedAmount amount) internal view returns (bool) {
270266
return rateLimitDuration != 0
271267
? _isAmountRateLimited(_getCurrentCapacity(_getOutboundLimitParams()), amount)
272268
: false;
273269
}
274270

275271
function _isInboundAmountRateLimited(
276-
TrimmedAmount memory amount,
272+
TrimmedAmount amount,
277273
uint16 chainId_
278274
) internal view returns (bool) {
279275
return rateLimitDuration != 0
@@ -282,15 +278,15 @@ abstract contract RateLimiter is IRateLimiter, IRateLimiterEvents {
282278
}
283279

284280
function _isAmountRateLimited(
285-
TrimmedAmount memory capacity,
286-
TrimmedAmount memory amount
281+
TrimmedAmount capacity,
282+
TrimmedAmount amount
287283
) internal pure returns (bool) {
288-
return capacity.lt(amount);
284+
return capacity < amount;
289285
}
290286

291287
function _enqueueOutboundTransfer(
292288
uint64 sequence,
293-
TrimmedAmount memory amount,
289+
TrimmedAmount amount,
294290
uint16 recipientChain,
295291
bytes32 recipient,
296292
address senderAddress,
@@ -310,7 +306,7 @@ abstract contract RateLimiter is IRateLimiter, IRateLimiterEvents {
310306

311307
function _enqueueInboundTransfer(
312308
bytes32 digest,
313-
TrimmedAmount memory amount,
309+
TrimmedAmount amount,
314310
address recipient
315311
) internal {
316312
_getInboundQueueStorage()[digest] = InboundQueuedTransfer({

evm/src/libraries/TransceiverStructs.sol

+4-4
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,11 @@ 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,
107-
transferAmount.getDecimals(),
108-
transferAmount.getAmount(),
107+
transferAmount.unpackDecimals(),
108+
transferAmount.unpackAmount(),
109109
m.sourceToken,
110110
m.to,
111111
m.toChain
@@ -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)