Skip to content

Commit 5e627a1

Browse files
author
Rahul Maganti
committed
evm: rebasing changes
1 parent 2df0d82 commit 5e627a1

File tree

3 files changed

+47
-47
lines changed

3 files changed

+47
-47
lines changed

evm/test/NttManager.t.sol

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ contract TestNttManager is Test, IRateLimiterEvents {
7373
// === pure unit tests
7474

7575
// naive implementation of countSetBits to test against
76-
function simpleCount(uint64 n) public returns (uint8) {
76+
function simpleCount(uint64 n) public pure returns (uint8) {
7777
uint8 count;
7878

7979
while (n > 0) {

evm/test/RateLimit.t.sol

+9-10
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,7 @@ contract TestRateLimit is Test, IRateLimiterEvents {
652652
nttManager.getInboundLimitParams(TransceiverHelpersLib.SENDING_CHAIN_ID);
653653
assertEq(
654654
inboundLimitParams.currentCapacity.getAmount(),
655-
inboundLimitParams.limit.sub(transferAmount).getAmount()
655+
(inboundLimitParams.limit - transferAmount).getAmount()
656656
);
657657
assertEq(inboundLimitParams.lastTxTimestamp, receiveTime);
658658
}
@@ -691,7 +691,7 @@ contract TestRateLimit is Test, IRateLimiterEvents {
691691
nttManager.getOutboundLimitParams();
692692
assertEq(
693693
outboundLimitParams.currentCapacity.getAmount(),
694-
outboundLimitParams.limit.sub(transferAmount).getAmount()
694+
(outboundLimitParams.limit - transferAmount).getAmount()
695695
);
696696
assertEq(outboundLimitParams.lastTxTimestamp, sendAgainTime);
697697
}
@@ -744,7 +744,7 @@ contract TestRateLimit is Test, IRateLimiterEvents {
744744

745745
(address user_A, address user_B, DummyToken token, uint8 decimals) = setupToken();
746746

747-
TrimmedAmount memory mintAmount = TrimmedAmount(mintAmt, 8);
747+
TrimmedAmount mintAmount = packTrimmedAmount(mintAmt, 8);
748748
token.mintDummy(address(user_A), mintAmount.untrim(decimals));
749749
nttManager.setOutboundLimit(mintAmount.untrim(decimals));
750750

@@ -758,10 +758,10 @@ contract TestRateLimit is Test, IRateLimiterEvents {
758758

759759
// allow for amounts greater than uint64 to check if [`transfer`] reverts
760760
// on amounts greater than u64 MAX.
761-
TrimmedAmount memory transferAmount = transferAmt.trim(decimals, 8);
761+
TrimmedAmount transferAmount = transferAmt.trim(decimals, 8);
762762

763763
// check error conditions
764-
if (transferAmount.amount == 0) {
764+
if (transferAmount.getAmount() == 0) {
765765
vm.expectRevert();
766766
// transfer tokens from user_A -> user_B via the nttManager
767767
nttManager.transfer(
@@ -775,7 +775,7 @@ contract TestRateLimit is Test, IRateLimiterEvents {
775775
return;
776776
}
777777

778-
if (transferAmount.amount > type(uint64).max) {
778+
if (transferAmount.getAmount() > type(uint64).max) {
779779
bytes4 selector = bytes4(keccak256("AmountTooLarge(uint256)"));
780780
vm.expectRevert(abi.encodeWithSelector(selector, transferAmt));
781781

@@ -799,7 +799,7 @@ contract TestRateLimit is Test, IRateLimiterEvents {
799799

800800
// assert nttManager has 10 tokens and user_A has 10 fewer tokens
801801
assertEq(token.balanceOf(address(nttManager)), transferAmount.untrim(decimals));
802-
assertEq(token.balanceOf(user_A), mintAmount.sub(transferAmount).untrim(decimals));
802+
assertEq(token.balanceOf(user_A), (mintAmount - transferAmount).untrim(decimals));
803803

804804
{
805805
// consumed capacity on the outbound side
@@ -808,7 +808,7 @@ contract TestRateLimit is Test, IRateLimiterEvents {
808808
nttManager.getOutboundLimitParams();
809809
assertEq(
810810
outboundLimitParams.currentCapacity.getAmount(),
811-
outboundLimitParams.limit.sub(transferAmount).getAmount()
811+
(outboundLimitParams.limit - transferAmount).getAmount()
812812
);
813813
assertEq(outboundLimitParams.lastTxTimestamp, initialBlockTimestamp);
814814
}
@@ -969,7 +969,7 @@ contract TestRateLimit is Test, IRateLimiterEvents {
969969
TransceiverStructs.NttManagerMessage memory m;
970970
bytes memory encodedEm;
971971
uint256 inboundLimit = inboundLimitAmt;
972-
TrimmedAmount memory trimmedAmount = TrimmedAmount(uint64(amount), 8);
972+
TrimmedAmount trimmedAmount = packTrimmedAmount(uint64(amount), 8);
973973
{
974974
TransceiverStructs.TransceiverMessage memory em;
975975
(m, em) = TransceiverHelpersLib.attestTransceiversHelper(
@@ -979,7 +979,6 @@ contract TestRateLimit is Test, IRateLimiterEvents {
979979
nttManager,
980980
nttManager,
981981
trimmedAmount,
982-
// TrimmedAmount(50, 8),
983982
inboundLimit.trim(token.decimals(), token.decimals()),
984983
transceivers
985984
);

evm/test/TrimmedAmount.t.sol

+37-36
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,6 @@ contract TrimmingTest is Test {
99
using TrimmedAmountLib for uint256;
1010
using TrimmedAmountLib for TrimmedAmount;
1111

12-
function test_packUnpack(uint64 amount, uint8 decimals) public {
13-
TrimmedAmount trimmed = packTrimmedAmount(amount, decimals);
14-
assertEq(trimmed.getAmount(), amount);
15-
assertEq(trimmed.getDecimals(), decimals);
16-
}
17-
1812
function testTrimmingRoundTrip() public {
1913
uint8 decimals = 18;
2014
uint256 amount = 50 * 10 ** decimals;
@@ -169,6 +163,14 @@ contract TrimmingTest is Test {
169163
assertEq(expectedRoundTrip, amountRoundTrip);
170164
}
171165

166+
// ============= FUZZ TESTS ================== //
167+
168+
function test_packUnpack(uint64 amount, uint8 decimals) public {
169+
TrimmedAmount trimmed = packTrimmedAmount(amount, decimals);
170+
assertEq(trimmed.getAmount(), amount);
171+
assertEq(trimmed.getDecimals(), decimals);
172+
}
173+
172174
function testFuzz_AddOperatorOverload(TrimmedAmount a, TrimmedAmount b) public {
173175
vm.assume(a.getDecimals() == b.getDecimals());
174176

@@ -235,53 +237,50 @@ contract TrimmingTest is Test {
235237
vm.assume(fromDecimals <= 8 && fromDecimals <= toDecimals);
236238

237239
// initialize TrimmedAmount
238-
TrimmedAmount memory trimmedAmount = TrimmedAmount(uint64(amt), fromDecimals);
240+
TrimmedAmount trimmedAmount = packTrimmedAmount(uint64(amt), fromDecimals);
239241

240242
// trimming is left inverse of trimming
241243
uint256 amountUntrimmed = trimmedAmount.untrim(toDecimals);
242-
TrimmedAmount memory amountRoundTrip = amountUntrimmed.trim(toDecimals, fromDecimals);
244+
TrimmedAmount amountRoundTrip = amountUntrimmed.trim(toDecimals, fromDecimals);
243245

244-
assertEq(trimmedAmount.amount, amountRoundTrip.amount);
246+
assertEq(trimmedAmount.getAmount(), amountRoundTrip.getAmount());
245247
}
246248

247-
// FUZZ TESTS
248-
249249
// invariant: forall (TrimmedAmount a, TrimmedAmount b)
250250
// a.saturatingAdd(b).amount <= type(uint64).max
251-
function testFuzz_saturatingAddDoesNotOverflow(
252-
TrimmedAmount memory a,
253-
TrimmedAmount memory b
254-
) public {
255-
vm.assume(a.decimals == b.decimals);
251+
function testFuzz_saturatingAddDoesNotOverflow(TrimmedAmount a, TrimmedAmount b) public {
252+
vm.assume(a.getDecimals() == b.getDecimals());
256253

257-
TrimmedAmount memory c = a.saturatingAdd(b);
254+
TrimmedAmount c = a.saturatingAdd(b);
258255

259256
// decimals should always be the same, else revert
260-
assertEq(c.decimals, a.decimals);
257+
assertEq(c.getDecimals(), a.getDecimals());
261258

262259
// amount should never overflow
263-
assertLe(c.amount, type(uint64).max);
260+
assertLe(c.getAmount(), type(uint64).max);
264261
// amount should never underflow
265-
assertGe(c.amount, 0);
262+
assertGe(c.getAmount(), 0);
266263
}
267264

268265
// NOTE: above the TRIMMED_DECIMALS threshold will always get trimmed to TRIMMED_DECIMALS
269266
// or trimmed to the number of decimals on the recipient chain.
270267
// this tests for inputs with decimals > TRIMMED_DECIMALS
271-
function testFuzz_SubOperatorZeroAboveThreshold(uint256 amt, uint8 decimals) public pure {
268+
function testFuzz_SubOperatorZeroAboveThreshold(uint256 amt, uint8 decimals) public {
272269
decimals = uint8(bound(decimals, 8, 18));
273270
uint256 maxAmt = (type(uint64).max) / (10 ** decimals);
274271
vm.assume(amt < maxAmt);
275272

276273
uint256 amount = amt * (10 ** decimals);
277274
uint256 amountOther = 0;
278-
TrimmedAmount memory trimmedAmount = amount.trim(decimals, 8);
279-
TrimmedAmount memory trimmedAmountOther = amountOther.trim(decimals, 8);
275+
TrimmedAmount trimmedAmount = amount.trim(decimals, 8);
276+
TrimmedAmount trimmedAmountOther = amountOther.trim(decimals, 8);
280277

281-
TrimmedAmount memory trimmedSub = trimmedAmount.sub(trimmedAmountOther);
278+
TrimmedAmount trimmedSub = trimmedAmount - trimmedAmountOther;
282279

283-
TrimmedAmount memory expectedTrimmedSub = TrimmedAmount(uint64(amt * (10 ** 8)), 8);
284-
assert(expectedTrimmedSub.eq(trimmedSub));
280+
TrimmedAmount expectedTrimmedSub = packTrimmedAmount(uint64(amt * (10 ** 8)), 8);
281+
assert(expectedTrimmedSub == trimmedSub);
282+
assertEq(expectedTrimmedSub.getAmount(), trimmedSub.getAmount());
283+
assertEq(expectedTrimmedSub.getDecimals(), trimmedSub.getDecimals());
285284
}
286285

287286
function testFuzz_SubOperatorWillOverflow(
@@ -296,29 +295,31 @@ contract TrimmingTest is Test {
296295

297296
uint256 amountLeft = amtLeft * (10 ** decimals);
298297
uint256 amountRight = amtRight * (10 ** decimals);
299-
TrimmedAmount memory trimmedAmount = amountLeft.trim(decimals, 8);
300-
TrimmedAmount memory trimmedAmountOther = amountRight.trim(decimals, 8);
298+
TrimmedAmount trimmedAmount = amountLeft.trim(decimals, 8);
299+
TrimmedAmount trimmedAmountOther = amountRight.trim(decimals, 8);
301300

302-
vm.expectRevert();
303-
trimmedAmount.sub(trimmedAmountOther);
301+
vm.expectRevert(stdError.arithmeticError);
302+
trimmedAmount - trimmedAmountOther;
304303
}
305304

306305
// NOTE: above the TRIMMED_DECIMALS threshold will always get trimmed to TRIMMED_DECIMALS
307306
// or trimmed to the number of decimals on the recipient chain.
308307
// this tests for inputs with decimals > TRIMMED_DECIMALS
309-
function testFuzz_AddOperatorZeroAboveThreshold(uint256 amt, uint8 decimals) public pure {
308+
function testFuzz_AddOperatorZeroAboveThreshold(uint256 amt, uint8 decimals) public {
310309
decimals = uint8(bound(decimals, 8, 18));
311310
uint256 maxAmt = (type(uint64).max) / (10 ** decimals);
312311
vm.assume(amt < maxAmt);
313312

314313
uint256 amount = amt * (10 ** decimals);
315314
uint256 amountOther = 0;
316-
TrimmedAmount memory trimmedAmount = amount.trim(decimals, 8);
317-
TrimmedAmount memory trimmedAmountOther = amountOther.trim(decimals, 8);
315+
TrimmedAmount trimmedAmount = amount.trim(decimals, 8);
316+
TrimmedAmount trimmedAmountOther = amountOther.trim(decimals, 8);
318317

319-
TrimmedAmount memory trimmedSum = trimmedAmount.add(trimmedAmountOther);
318+
TrimmedAmount trimmedSum = trimmedAmount + trimmedAmountOther;
320319

321-
TrimmedAmount memory expectedTrimmedSum = TrimmedAmount(uint64(amt * (10 ** 8)), 8);
322-
assert(expectedTrimmedSum.eq(trimmedSum));
320+
TrimmedAmount expectedTrimmedSum = packTrimmedAmount(uint64(amt * (10 ** 8)), 8);
321+
assert(expectedTrimmedSum == trimmedSum);
322+
assertEq(expectedTrimmedSum.getAmount(), trimmedSum.getAmount());
323+
assertEq(expectedTrimmedSum.getDecimals(), trimmedSum.getDecimals());
323324
}
324325
}

0 commit comments

Comments
 (0)