Skip to content

Commit

Permalink
[Part 1 of 2] feat: minor tweaks (#30)
Browse files Browse the repository at this point in the history
* chore: minor gas opt safeCast

* test: update test

* gas: updated forge gas
  • Loading branch information
ChefMist committed May 8, 2024
1 parent ee06ebc commit a9301b8
Show file tree
Hide file tree
Showing 15 changed files with 105 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
401866
401422
Original file line number Diff line number Diff line change
@@ -1 +1 @@
182854
182410
Original file line number Diff line number Diff line change
@@ -1 +1 @@
247993
247549
Original file line number Diff line number Diff line change
@@ -1 +1 @@
129601
128995
Original file line number Diff line number Diff line change
@@ -1 +1 @@
24940941
24940739
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1899
1697
2 changes: 1 addition & 1 deletion .forge-snapshots/LiquidityMathTest#addDeltaNegtive.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
306
116
2 changes: 1 addition & 1 deletion .forge-snapshots/LiquidityMathTest#addDeltaPositive.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
212
113
2 changes: 1 addition & 1 deletion .forge-snapshots/TickTest#update.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
134926
134819
13 changes: 8 additions & 5 deletions src/pool-cl/libraries/LiquidityMath.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ library LiquidityMath {
/// @param y The delta by which liquidity should be changed
/// @return z The liquidity delta
function addDelta(uint128 x, int128 y) internal pure returns (uint128 z) {
// after 0.8.0 overflow is checked by default
if (y < 0) {
z = x - uint128(-y);
} else {
z = x + uint128(y);
assembly {
z := add(x, y)

if shr(128, z) {
// store 0x93dafdf1, error SafeCastOverflow at memory 0 address and revert from pointer 28, to byte 32
mstore(0x0, 0x93dafdf1)
revert(0x1c, 0x04)
}
}
}
}
87 changes: 79 additions & 8 deletions test/libraries/BalanceDelta.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {Test} from "forge-std/Test.sol";
import {BalanceDelta, toBalanceDelta} from "../../src/types/BalanceDelta.sol";

contract TestBalanceDelta is Test {
function testToBalanceDelta() public {
function test_toBalanceDelta() public {
BalanceDelta balanceDelta = toBalanceDelta(0, 0);
assertEq(balanceDelta.amount0(), 0);
assertEq(balanceDelta.amount1(), 0);
Expand All @@ -27,17 +27,52 @@ contract TestBalanceDelta is Test {
assertEq(balanceDelta.amount1(), type(int128).min);
}

function testToBalanceDelta(int128 x, int128 y) public {
function test_fuzz_toBalanceDelta(int128 x, int128 y) public {
BalanceDelta balanceDelta = toBalanceDelta(x, y);
int256 expectedBD = int256(uint256(bytes32(abi.encodePacked(x, y))));
assertEq(BalanceDelta.unwrap(balanceDelta), expectedBD);
}

function test_fuzz_amount0_amount1(int128 x, int128 y) public {
BalanceDelta balanceDelta = toBalanceDelta(x, y);
assertEq(balanceDelta.amount0(), x);
assertEq(balanceDelta.amount1(), y);
}

function testAdd(int128 a, int128 b, int128 c, int128 d) public {
function test_add() public {
BalanceDelta balanceDelta = toBalanceDelta(0, 0) + toBalanceDelta(0, 0);
assertEq(balanceDelta.amount0(), 0);
assertEq(balanceDelta.amount1(), 0);

balanceDelta = toBalanceDelta(-1000, 1000) + toBalanceDelta(1000, -1000);
assertEq(balanceDelta.amount0(), 0);
assertEq(balanceDelta.amount1(), 0);

balanceDelta =
toBalanceDelta(type(int128).min, type(int128).max) + toBalanceDelta(type(int128).max, type(int128).min);
assertEq(balanceDelta.amount0(), -1);
assertEq(balanceDelta.amount1(), -1);

balanceDelta = toBalanceDelta(type(int128).max / 2 + 1, type(int128).max / 2 + 1)
+ toBalanceDelta(type(int128).max / 2, type(int128).max / 2);
assertEq(balanceDelta.amount0(), type(int128).max);
assertEq(balanceDelta.amount1(), type(int128).max);
}

function test_add_revertsOnOverflow() public {
// should revert because type(int128).max + 1 is not possible
vm.expectRevert();
toBalanceDelta(type(int128).max, 0) + toBalanceDelta(1, 0);

vm.expectRevert();
toBalanceDelta(0, type(int128).max) + toBalanceDelta(0, 1);
}

function test_fuzz_add(int128 a, int128 b, int128 c, int128 d) public {
int256 ac = int256(a) + c;
int256 bd = int256(b) + d;

// make sure the addition doesn't overflow
// if the addition overflows it should revert
if (ac != int128(ac) || bd != int128(bd)) {
vm.expectRevert();
}
Expand All @@ -47,16 +82,52 @@ contract TestBalanceDelta is Test {
assertEq(balanceDelta.amount1(), bd);
}

function testSub(int128 a, int128 b, int128 c, int128 d) public {
function test_sub() public {
BalanceDelta balanceDelta = toBalanceDelta(0, 0) - toBalanceDelta(0, 0);
assertEq(balanceDelta.amount0(), 0);
assertEq(balanceDelta.amount1(), 0);

balanceDelta = toBalanceDelta(-1000, 1000) - toBalanceDelta(1000, -1000);
assertEq(balanceDelta.amount0(), -2000);
assertEq(balanceDelta.amount1(), 2000);

balanceDelta =
toBalanceDelta(-1000, -1000) - toBalanceDelta(-(type(int128).min + 1000), -(type(int128).min + 1000));
assertEq(balanceDelta.amount0(), type(int128).min);
assertEq(balanceDelta.amount1(), type(int128).min);

balanceDelta = toBalanceDelta(type(int128).min / 2, type(int128).min / 2)
- toBalanceDelta(-(type(int128).min / 2), -(type(int128).min / 2));
assertEq(balanceDelta.amount0(), type(int128).min);
assertEq(balanceDelta.amount1(), type(int128).min);
}

function test_sub_revertsOnUnderflow() public {
// should revert because type(int128).min - 1 is not possible
vm.expectRevert();
toBalanceDelta(type(int128).min, 0) - toBalanceDelta(1, 0);

vm.expectRevert();
toBalanceDelta(0, type(int128).min) - toBalanceDelta(0, 1);
}

function test_fuzz_sub(int128 a, int128 b, int128 c, int128 d) public {
int256 ac = int256(a) - c;
int256 bd = int256(b) - d;

// make sure the subtraction doesn't underflow
vm.assume(ac == int128(ac));
vm.assume(bd == int128(bd));
// if the subtraction underflows it should revert
if (ac != int128(ac) || bd != int128(bd)) {
vm.expectRevert();
}

BalanceDelta balanceDelta = toBalanceDelta(a, b) - toBalanceDelta(c, d);
assertEq(balanceDelta.amount0(), ac);
assertEq(balanceDelta.amount1(), bd);
}

function test_fuzz_eq(int128 a, int128 b, int128 c, int128 d) public {
bool isEqual = (toBalanceDelta(a, b) == toBalanceDelta(c, d));
if (a == c && b == d) assertTrue(isEqual);
else assertFalse(isEqual);
}
}
3 changes: 2 additions & 1 deletion test/pool-cl/CLPoolManager.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {ProtocolFeeControllerTest} from "./helpers/ProtocolFeeControllerTest.sol
import {IProtocolFeeController} from "../../src/interfaces/IProtocolFeeController.sol";
import {CLFeeManagerHook} from "./helpers/CLFeeManagerHook.sol";
import {CLNoOpTestHook} from "./helpers/CLNoOpTestHook.sol";
import {SafeCast} from "../../src/libraries/SafeCast.sol";

contract CLPoolManagerTest is Test, Deployers, TokenFixture, GasSnapshot {
using PoolIdLibrary for PoolKey;
Expand Down Expand Up @@ -984,7 +985,7 @@ contract CLPoolManagerTest is Test, Deployers, TokenFixture, GasSnapshot {
IERC20(Currency.unwrap(currency0)).approve(address(router), 1e30 ether);
IERC20(Currency.unwrap(currency1)).approve(address(router), 1e30 ether);

vm.expectRevert(stdError.arithmeticError);
vm.expectRevert(SafeCast.SafeCastOverflow.selector);
router.modifyPosition(
key, ICLPoolManager.ModifyLiquidityParams({tickLower: 46000, tickUpper: 46050, liquidityDelta: -1}), ""
);
Expand Down
2 changes: 1 addition & 1 deletion test/pool-cl/libraries/CLPool.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ contract PoolTest is Test {
} else if (params.tickUpper > TickMath.MAX_TICK) {
vm.expectRevert(abi.encodeWithSelector(Tick.TickUpperOutOfBounds.selector, params.tickUpper));
} else if (params.liquidityDelta < 0) {
vm.expectRevert(abi.encodeWithSignature("Panic(uint256)", 0x11));
vm.expectRevert(SafeCast.SafeCastOverflow.selector);
} else if (params.liquidityDelta == 0) {
vm.expectRevert(CLPosition.CannotUpdateEmptyPosition.selector);
} else if (params.liquidityDelta > int128(Tick.tickSpacingToMaxLiquidityPerTick(params.tickSpacing))) {
Expand Down
3 changes: 2 additions & 1 deletion test/pool-cl/libraries/CLPosition.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {Test} from "forge-std/Test.sol";
import {CLPosition} from "../../../src/pool-cl/libraries/CLPosition.sol";
import {CLPool} from "../../../src/pool-cl/libraries/CLPool.sol";
import {FixedPoint128} from "../../../src/pool-cl/libraries/FixedPoint128.sol";
import {SafeCast} from "../../../src/libraries/SafeCast.sol";

contract CLPositionTest is Test, GasSnapshot {
using CLPosition for mapping(bytes32 => CLPosition.Info);
Expand All @@ -31,7 +32,7 @@ contract CLPositionTest is Test, GasSnapshot {
if (liquidityDelta == 0) {
vm.expectRevert(CLPosition.CannotUpdateEmptyPosition.selector);
} else if (liquidityDelta < 0) {
vm.expectRevert(stdError.arithmeticError);
vm.expectRevert(SafeCast.SafeCastOverflow.selector);
}
(uint256 feesOwed0, uint256 feesOwed1) = info.update(liquidityDelta, feeGrowthInside0X128, feeGrowthInside1X128);

Expand Down
7 changes: 4 additions & 3 deletions test/pool-cl/libraries/LiquidityMath.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pragma solidity ^0.8.24;
import {GasSnapshot} from "forge-gas-snapshot/GasSnapshot.sol";
import {stdError} from "forge-std/StdError.sol";
import {Test} from "forge-std/Test.sol";
import {SafeCast} from "../../../src/libraries/SafeCast.sol";
import {LiquidityMath} from "../../../src/pool-cl/libraries/LiquidityMath.sol";

contract LiquidityMathTest is Test, GasSnapshot {
Expand All @@ -22,16 +23,16 @@ contract LiquidityMathTest is Test, GasSnapshot {
}

function testAddDeltaOverflow() public {
vm.expectRevert(stdError.arithmeticError);
vm.expectRevert(SafeCast.SafeCastOverflow.selector);
LiquidityMath.addDelta(2 ** 128 - 15, 15);
}

function testAddDeltaUnderflow() public {
// underflow
vm.expectRevert(stdError.arithmeticError);
vm.expectRevert(SafeCast.SafeCastOverflow.selector);
LiquidityMath.addDelta(0, -1);

vm.expectRevert(stdError.arithmeticError);
vm.expectRevert(SafeCast.SafeCastOverflow.selector);
LiquidityMath.addDelta(3, -4);
}
}

0 comments on commit a9301b8

Please sign in to comment.