-
Notifications
You must be signed in to change notification settings - Fork 504
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
OZ-L10, SB-L53, SB-L54, SB-M78; Check code length on Notifications, Unsubscribe Minimum Gas Limit #318
OZ-L10, SB-L53, SB-L54, SB-M78; Check code length on Notifications, Unsubscribe Minimum Gas Limit #318
Changes from 14 commits
eca398f
c93fc95
dd948d2
845a1d4
7710b38
732bee4
234596f
a05c0c9
12989e5
e7fe727
de32571
e96d8d3
df2be3b
f23b137
0faaa59
21fa51f
f151630
dc3a623
6caa7cc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
420775 | ||
420753 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
79492 | ||
79470 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
62380 | ||
62358 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
45268 | ||
45246 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,26 +4,27 @@ pragma solidity ^0.8.0; | |
import {ISubscriber} from "../interfaces/ISubscriber.sol"; | ||
import {PositionConfig} from "../libraries/PositionConfig.sol"; | ||
import {PositionConfigId, PositionConfigIdLibrary} from "../libraries/PositionConfigId.sol"; | ||
import {BipsLibrary} from "../libraries/BipsLibrary.sol"; | ||
import {INotifier} from "../interfaces/INotifier.sol"; | ||
import {CustomRevert} from "@uniswap/v4-core/src/libraries/CustomRevert.sol"; | ||
import {BalanceDelta} from "@uniswap/v4-core/src/types/BalanceDelta.sol"; | ||
|
||
/// @notice Notifier is used to opt in to sending updates to external contracts about position modifications or transfers | ||
abstract contract Notifier is INotifier { | ||
using BipsLibrary for uint256; | ||
using CustomRevert for bytes4; | ||
using PositionConfigIdLibrary for PositionConfigId; | ||
|
||
ISubscriber private constant NO_SUBSCRIBER = ISubscriber(address(0)); | ||
|
||
// a percentage of the block.gaslimit denoted in BPS, used as the gas limit for subscriber calls | ||
// 100 bps is 1%, at 30M gas, the limit is 300K | ||
uint256 private constant BLOCK_LIMIT_BPS = 100; | ||
/// @inheritdoc INotifier | ||
uint256 public immutable unsubscribeGasLimit; | ||
|
||
/// @inheritdoc INotifier | ||
mapping(uint256 tokenId => ISubscriber subscriber) public subscriber; | ||
|
||
constructor(uint256 _unsubscribeGasLimit) { | ||
unsubscribeGasLimit = _unsubscribeGasLimit; | ||
} | ||
|
||
modifier onlyIfApproved(address caller, uint256 tokenId) virtual; | ||
modifier onlyValidConfig(uint256 tokenId, PositionConfig calldata config) virtual; | ||
|
||
|
@@ -65,13 +66,17 @@ abstract contract Notifier is INotifier { | |
function _unsubscribe(uint256 tokenId, PositionConfig calldata config) internal { | ||
_positionConfigs(tokenId).setUnsubscribe(); | ||
ISubscriber _subscriber = subscriber[tokenId]; | ||
if (_subscriber == NO_SUBSCRIBER) NotSubscribed.selector.revertWith(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i see that this check makes sense, surprised it wasnt here before There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it may make more sense to check _postionConfigs().hasSubscriber? i guess see if its cheaper in the case where they do have a subscriber? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yep looked like .hasSubscriber() was slightly cheaper! refactored it |
||
|
||
delete subscriber[tokenId]; | ||
|
||
// A gas limit and a try-catch block are used to protect users from a malicious subscriber. | ||
// Users should always be able to unsubscribe, not matter how the subscriber behaves. | ||
uint256 subscriberGasLimit = block.gaslimit.calculatePortion(BLOCK_LIMIT_BPS); | ||
try _subscriber.notifyUnsubscribe{gas: subscriberGasLimit}(tokenId, config) {} catch {} | ||
if (address(_subscriber).code.length > 0) { | ||
// require that the remaining gas is sufficient to notify the subscriber | ||
// otherwise, users can select a gas limit where .notifyUnsubscribe hits OutOfGas yet the transaction/unsubscription | ||
// can still succeed | ||
saucepoint marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (gasleft() < unsubscribeGasLimit) GasLimitTooLow.selector.revertWith(); | ||
try _subscriber.notifyUnsubscribe{gas: unsubscribeGasLimit}(tokenId, config) {} catch {} | ||
} | ||
|
||
emit Unsubscription(tokenId, address(_subscriber)); | ||
} | ||
|
@@ -106,6 +111,7 @@ abstract contract Notifier is INotifier { | |
} | ||
|
||
function _call(address target, bytes memory encodedCall) internal returns (bool success) { | ||
if (target.code.length == 0) NoCodeSubscriber.selector.revertWith(); | ||
assembly ("memory-safe") { | ||
success := call(gas(), target, 0, add(encodedCall, 0x20), mload(encodedCall), 0, 0) | ||
} | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -10,6 +10,9 @@ interface ISubscriber { | |||||
/// @param config details about the position | ||||||
/// @param data additional data passed in by the caller | ||||||
function notifySubscribe(uint256 tokenId, PositionConfig memory config, bytes memory data) external; | ||||||
/// @notice Called when a position unsubscribes from the subscriber | ||||||
/// @dev This call's gas is capped at `unsubscribeGasLimit` (set at deployment) | ||||||
/// @dev Because of EIP-150, solidity may only allocate 63/64 of gasleft() | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i actually think its clearer to leave it as
because this only happens when gasLeft == unsubscriberGasLimit There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i just mean that subscribers should know that they actually cannot take more than 63/64 of unsubscribeGasLimit because thats the lower bound of gas they will receive There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah agree that is the lower bound ! |
||||||
/// @param tokenId the token ID of the position | ||||||
/// @param config details about the position | ||||||
function notifyUnsubscribe(uint256 tokenId, PositionConfig memory config) external; | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: i think there is an extra line added that can be removed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this natspec? fwiw i added this to INotifier