Skip to content

Commit

Permalink
use transient storage for the current hook addr
Browse files Browse the repository at this point in the history
  • Loading branch information
snreynolds committed Nov 17, 2023
1 parent d893478 commit e68a5dc
Show file tree
Hide file tree
Showing 27 changed files with 160 additions and 124 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
188685
186984
2 changes: 1 addition & 1 deletion .forge-snapshots/cached dynamic fee, no hooks.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
142201
142310
2 changes: 1 addition & 1 deletion .forge-snapshots/donate gas with 1 token.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
134065
132186
2 changes: 1 addition & 1 deletion .forge-snapshots/donate gas with 2 tokens.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
182638
180759
2 changes: 1 addition & 1 deletion .forge-snapshots/mint with empty hook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
335320
313472
2 changes: 1 addition & 1 deletion .forge-snapshots/mint with native token.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
198076
196197
2 changes: 1 addition & 1 deletion .forge-snapshots/mint.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
198018
196139
2 changes: 1 addition & 1 deletion .forge-snapshots/poolManager bytecode size.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
25133
25374
1 change: 0 additions & 1 deletion .forge-snapshots/simple swap native.snap

This file was deleted.

2 changes: 1 addition & 1 deletion .forge-snapshots/simple swap with native.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
193042
191151
2 changes: 1 addition & 1 deletion .forge-snapshots/simple swap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
201609
199718
Original file line number Diff line number Diff line change
@@ -1 +1 @@
121638
121747
2 changes: 1 addition & 1 deletion .forge-snapshots/swap against liquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
109127
109236
2 changes: 1 addition & 1 deletion .forge-snapshots/swap burn claim for input.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
128197
128306
1 change: 0 additions & 1 deletion .forge-snapshots/swap mint 1155 as output.snap

This file was deleted.

2 changes: 1 addition & 1 deletion .forge-snapshots/swap mint output as claim.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
211778
209887
1 change: 0 additions & 1 deletion .forge-snapshots/swap with 1155 as input.snap

This file was deleted.

2 changes: 1 addition & 1 deletion .forge-snapshots/swap with dynamic fee.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
187925
186224
2 changes: 1 addition & 1 deletion .forge-snapshots/swap with hooks.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
109106
109215
2 changes: 1 addition & 1 deletion .forge-snapshots/update dynamic fee in before swap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
194515
192814
179 changes: 91 additions & 88 deletions .gas-snapshot

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ fs_permissions = [{ access = "read-write", path = ".forge-snapshots/"}, { access
cancun = true

[profile.default.fuzz]
runs = 1000
runs = 100
seed = "0x4444"

[profile.ci.fuzz]
Expand Down
14 changes: 5 additions & 9 deletions src/PoolManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {Claims} from "./Claims.sol";
import {PoolId, PoolIdLibrary} from "./types/PoolId.sol";
import {BalanceDelta} from "./types/BalanceDelta.sol";
import {Lockers} from "./libraries/Lockers.sol";
import {CurrentHookAddress} from "./libraries/CurrentHookAddress.sol";

/// @notice Holds the state for all pools
contract PoolManager is IPoolManager, Fees, NoDelegateCall, Claims {
Expand Down Expand Up @@ -179,7 +180,7 @@ contract PoolManager is IPoolManager, Fees, NoDelegateCall, Claims {
// todo fix stack too deep :/
address locker = Lockers.getCurrentLocker();
if (msg.sender != locker) {
if (msg.sender != currentHook || !Hooks.shouldAccessLock(IHooks(currentHook))) {
if (msg.sender != CurrentHookAddress.get() || !Hooks.shouldAccessLock(IHooks(CurrentHookAddress.get()))) {
revert LockedBy(locker);
}
}
Expand All @@ -192,7 +193,7 @@ contract PoolManager is IPoolManager, Fees, NoDelegateCall, Claims {
IPoolManager.ModifyPositionParams memory params,
bytes calldata hookData
) external override noDelegateCall onlyByLocker returns (BalanceDelta delta) {
_setCurrentHook(address(key.hooks));
CurrentHookAddress.set(address(key.hooks));

if (key.hooks.shouldCallBeforeModifyPosition()) {
if (
Expand Down Expand Up @@ -252,7 +253,7 @@ contract PoolManager is IPoolManager, Fees, NoDelegateCall, Claims {
onlyByLocker
returns (BalanceDelta delta)
{
_setCurrentHook(address(key.hooks));
CurrentHookAddress.set(address(key.hooks));

if (key.hooks.shouldCallBeforeSwap()) {
if (key.hooks.beforeSwap(msg.sender, key, params, hookData) != IHooks.beforeSwap.selector) {
Expand Down Expand Up @@ -306,7 +307,7 @@ contract PoolManager is IPoolManager, Fees, NoDelegateCall, Claims {
onlyByLocker
returns (BalanceDelta delta)
{
_setCurrentHook(address(key.hooks));
CurrentHookAddress.set(address(key.hooks));

if (key.hooks.shouldCallBeforeDonate()) {
if (key.hooks.beforeDonate(msg.sender, key, amount0, amount1, hookData) != IHooks.beforeDonate.selector) {
Expand Down Expand Up @@ -406,11 +407,6 @@ contract PoolManager is IPoolManager, Fees, NoDelegateCall, Claims {
return Lockers.nonzeroDeltaCount();
}

// TODO: Use transient storage
function _setCurrentHook(address hookAddr) internal {
if (currentHook != hookAddr) currentHook = hookAddr;
}

/// @notice receive native tokens for native pools
receive() external payable {}
}
23 changes: 23 additions & 0 deletions src/libraries/CurrentHookAddress.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

// TODO: Remove this library once the `transient` keyword is supported in solidity.
library CurrentHookAddress {
uint256 constant HOOK_ADDRESS_SLOT = uint256(keccak256("HookAddress")) - 1;

function get() internal view returns (address _address) {
uint256 slot = HOOK_ADDRESS_SLOT;
assembly {
_address := tload(slot)
}
}

function set(address _address) internal {
if (_address != get()) {
uint256 slot = HOOK_ADDRESS_SLOT;
assembly {
tstore(slot, _address)
}
}
}
}
4 changes: 0 additions & 4 deletions src/test/AccessLockHook.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import {CurrencyLibrary, Currency} from "../types/Currency.sol";
import {Hooks} from "../libraries/Hooks.sol";
import {TickMath} from "../libraries/TickMath.sol";

import "forge-std/console2.sol";

contract AccessLockHook is BaseTestHooks {
using CurrencyLibrary for Currency;

Expand Down Expand Up @@ -67,8 +65,6 @@ contract AccessLockHook is BaseTestHooks {
if (action == LockAction.Mint) {
manager.mint(key.currency1, address(this), amount);
} else if (action == LockAction.Take) {
console2.log("TAKE");
console2.log(key.currency1.balanceOf(address(manager)));
manager.take(key.currency1, address(this), amount);
} else if (action == LockAction.Donate) {
manager.donate(key, amount, amount, new bytes(0));
Expand Down
2 changes: 0 additions & 2 deletions test/AccessLock.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import {BalanceDelta} from "../src/types/BalanceDelta.sol";
import {Pool} from "../src/libraries/Pool.sol";
import {TickMath} from "../src/libraries/TickMath.sol";

import "forge-std/console2.sol";

contract AccessLockTest is Test, Deployers {
using Pool for Pool.State;
using CurrencyLibrary for Currency;
Expand Down
23 changes: 23 additions & 0 deletions test/CurrentHookAddress.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.20;

import {Test} from "forge-std/Test.sol";
import {CurrentHookAddress} from "../src/libraries/CurrentHookAddress.sol";

contract CurrentHookAddressTest is Test {
function test_get_currentHookAddress() public {
assertEq(CurrentHookAddress.get(), address(0));
}

function test_set_currentHookAddress() public {
CurrentHookAddress.set(address(1));
assertEq(CurrentHookAddress.get(), address(1));
}

function test_set_currentHookAddressTwice() public {
CurrentHookAddress.set(address(1));
CurrentHookAddress.set(address(2));

assertEq(CurrentHookAddress.get(), address(2));
}
}

0 comments on commit e68a5dc

Please sign in to comment.