Skip to content

Commit

Permalink
feat: gas optimization and fix dynamic fee fuzz (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChefMist authored May 31, 2024
1 parent 90136dc commit 79e15e2
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
137614
137531
Original file line number Diff line number Diff line change
@@ -1 +1 @@
29721
32155
Original file line number Diff line number Diff line change
@@ -1 +1 @@
60038
59963
Original file line number Diff line number Diff line change
@@ -1 +1 @@
32328
31820
4 changes: 2 additions & 2 deletions src/libraries/math/ParametersHelper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ library ParametersHelper {
bitmap = params.decodeUint16(OFFSET_HOOK);
}

function checkUnusedBitsAllZero(bytes32 params, uint256 mostSignificantUsedBitOffset) internal pure {
if ((uint256(params) >> (mostSignificantUsedBitOffset + 1)) != 0) {
function checkUnusedBitsAllZero(bytes32 params, uint256 mostSignificantUnUsedBitOffset) internal pure {
if ((uint256(params) >> (mostSignificantUnUsedBitOffset)) != 0) {
revert UnusedBitsNonZero();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/pool-bin/BinPoolManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ contract BinPoolManager is IBinPoolManager, ProtocolFees, Extsload {
if (key.currency0 >= key.currency1) revert CurrenciesInitializedOutOfOrder();

ParametersHelper.checkUnusedBitsAllZero(
key.parameters, BinPoolParametersHelper.OFFSET_MOST_SIGNIFICANT_USED_BITS
key.parameters, BinPoolParametersHelper.OFFSET_MOST_SIGNIFICANT_UNUSED_BITS
);
Hooks.validateHookConfig(key);
BinHooks.validatePermissionsConflict(key);
Expand Down
5 changes: 3 additions & 2 deletions src/pool-bin/libraries/BinPoolParametersHelper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@ import {Encoded} from "../../libraries/math/Encoded.sol";
/// The parameters are stored in a single bytes32 variable in the following format:
/// [0 - 16[: reserve for hooks
/// [16 - 31[: binStep (16 bits)
/// [32 - 256[: unused
library BinPoolParametersHelper {
using Encoded for bytes32;

uint256 internal constant OFFSET_BIN_STEP = 16;
uint256 internal constant OFFSET_MOST_SIGNIFICANT_USED_BITS = 31;
uint256 internal constant OFFSET_MOST_SIGNIFICANT_UNUSED_BITS = 32;

/// @dev Get binstep from the encoded pair parameters
/// @param params The encoded pair parameters, as follows:
/// [0 - 15[: bitmap for hooks registration
/// [16 - 31[: binSteps (16 bits)
/// [32 - 256[: other parameters
/// [32 - 256[: unused
/// @return binStep The binStep
function getBinStep(bytes32 params) internal pure returns (uint16 binStep) {
binStep = params.decodeUint16(OFFSET_BIN_STEP);
Expand Down
2 changes: 1 addition & 1 deletion src/pool-cl/CLPoolManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ contract CLPoolManager is ICLPoolManager, ProtocolFees, Extsload {
if (key.currency0 >= key.currency1) revert CurrenciesInitializedOutOfOrder();

ParametersHelper.checkUnusedBitsAllZero(
key.parameters, CLPoolParametersHelper.OFFSET_MOST_SIGNIFICANT_USED_BITS
key.parameters, CLPoolParametersHelper.OFFSET_MOST_SIGNIFICANT_UNUSED_BITS
);
Hooks.validateHookConfig(key);
CLHooks.validatePermissionsConflict(key);
Expand Down
5 changes: 3 additions & 2 deletions src/pool-cl/libraries/CLPoolParametersHelper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,20 @@ import {Encoded} from "../../libraries/math/Encoded.sol";
*
* [0 - 15[: reserve for hooks
* [16 - 39[: tickSpacing (24 bits)
* [40 - 256[: unused
*/
library CLPoolParametersHelper {
using Encoded for bytes32;

uint256 internal constant OFFSET_TICK_SPACING = 16;
uint256 internal constant OFFSET_MOST_SIGNIFICANT_USED_BITS = 39;
uint256 internal constant OFFSET_MOST_SIGNIFICANT_UNUSED_BITS = 40;

/**
* @dev Get tickSpacing from the encoded pair parameters
* @param params The encoded pair parameters, as follows:
* [0 - 16[: hooks registration bitmaps
* [16 - 39[: tickSpacing (24 bits)
* [40 - 256[: other parameters
* [40 - 256[: unused
* @return tickSpacing The tickSpacing
*/
function getTickSpacing(bytes32 params) internal pure returns (int24 tickSpacing) {
Expand Down
18 changes: 10 additions & 8 deletions test/pool-bin/BinPoolManager.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ contract BinPoolManagerTest is Test, GasSnapshot, BinTestHelper {
}

function test_FuzzInitializePoolUnusedBits(uint256 randomOneBitOffset) external {
randomOneBitOffset = bound(randomOneBitOffset, BinPoolParametersHelper.OFFSET_MOST_SIGNIFICANT_USED_BITS, 255);
randomOneBitOffset = bound(randomOneBitOffset, BinPoolParametersHelper.OFFSET_MOST_SIGNIFICANT_UNUSED_BITS, 255);

uint16 bitMap = 0x0008; // after mint call
MockBinHooks mockHooks = new MockBinHooks();
Expand All @@ -174,10 +174,7 @@ contract BinPoolManagerTest is Test, GasSnapshot, BinTestHelper {
parameters: bytes32(uint256(bitMap) | (1 << randomOneBitOffset)).setBinStep(1)
});

// just a bigger binStep if equals to BinPoolParametersHelper.OFFSET_MOST_SIGNIFICANT_USED_BITS
if (randomOneBitOffset != BinPoolParametersHelper.OFFSET_MOST_SIGNIFICANT_USED_BITS) {
vm.expectRevert(abi.encodeWithSelector(ParametersHelper.UnusedBitsNonZero.selector));
}
vm.expectRevert(abi.encodeWithSelector(ParametersHelper.UnusedBitsNonZero.selector));
poolManager.initialize(key, activeId, new bytes(0));
}

Expand Down Expand Up @@ -978,10 +975,15 @@ contract BinPoolManagerTest is Test, GasSnapshot, BinTestHelper {
vm.expectEmit();
emit DynamicLPFeeUpdated(key.toId(), _lpFee);

snapStart("BinPoolManagerTest#testFuzzUpdateDynamicLPFee");
vm.prank(address(binFeeManagerHook));
poolManager.updateDynamicLPFee(key, _lpFee);
snapEnd();
if (_lpFee != 0) {
// temp fix to only record gas if _lpFee !=0. todo use snapLastCall to make this part of code easier to read
snapStart("BinPoolManagerTest#testFuzzUpdateDynamicLPFee");
poolManager.updateDynamicLPFee(key, _lpFee);
snapEnd();
} else {
poolManager.updateDynamicLPFee(key, _lpFee);
}

(,, uint24 swapFee) = poolManager.getSlot0(key.toId());
assertEq(swapFee, _lpFee);
Expand Down
13 changes: 9 additions & 4 deletions test/pool-cl/CLPoolManager.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2829,10 +2829,15 @@ contract CLPoolManagerTest is Test, NoIsolate, Deployers, TokenFixture, GasSnaps
vm.expectEmit();
emit DynamicLPFeeUpdated(key.toId(), _swapFee);

snapStart("CLPoolManagerTest#testFuzzUpdateDynamicLPFee");
vm.prank(address(clFeeManagerHook));
poolManager.updateDynamicLPFee(key, _swapFee);
snapEnd();
if (_swapFee != 0) {
// temp fix to only record gas if _swapFee !=0. todo use snapLastCall to make this part of code easier to read
snapStart("CLPoolManagerTest#testFuzzUpdateDynamicLPFee");
poolManager.updateDynamicLPFee(key, _swapFee);
snapEnd();
} else {
poolManager.updateDynamicLPFee(key, _swapFee);
}

(,,, uint24 swapFee) = poolManager.getSlot0(key.toId());
assertEq(swapFee, _swapFee);
Expand Down Expand Up @@ -2968,7 +2973,7 @@ contract CLPoolManagerTest is Test, NoIsolate, Deployers, TokenFixture, GasSnaps

function checkUnusedBitsAllZero(bytes32 params) internal pure returns (bool) {
return
(uint256(params) & (type(uint256).max << CLPoolParametersHelper.OFFSET_MOST_SIGNIFICANT_USED_BITS + 1)) == 0;
(uint256(params) & (type(uint256).max << CLPoolParametersHelper.OFFSET_MOST_SIGNIFICANT_UNUSED_BITS)) == 0;
}

function _validateHookConfig(PoolKey memory poolKey) internal view returns (bool) {
Expand Down

0 comments on commit 79e15e2

Please sign in to comment.