-
Notifications
You must be signed in to change notification settings - Fork 8
Delegate anything #103
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
Closed
Closed
Delegate anything #103
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| pragma solidity ^0.8.27; | ||
|
|
||
| import { IDelegatedExtension } from "../modules/interfaces/IDelegatedExtension.sol"; | ||
| import { LibBytes } from "../utils/LibBytes.sol"; | ||
| import { LibOptim } from "../utils/LibOptim.sol"; | ||
|
|
||
| /// @title DelegateAnything | ||
| /// @author Michael Standen | ||
| /// @notice Helper for delegating calls to any contract | ||
| contract DelegateAnything is IDelegatedExtension { | ||
|
|
||
| address internal immutable _SELF = address(this); | ||
|
|
||
| /// @notice Error thrown when not called via delegatecall | ||
| error NotDelegateCall(); | ||
|
|
||
| /// @notice Error thrown when a delegate call fails | ||
| error DelegateCallFailed(bytes returnData); | ||
|
|
||
| /// @inheritdoc IDelegatedExtension | ||
| function handleSequenceDelegateCall( | ||
| bytes32, | ||
| uint256, | ||
| uint256, | ||
| uint256, | ||
| uint256, | ||
| bytes calldata data | ||
| ) external { | ||
| if (address(this) == _SELF) { | ||
| revert NotDelegateCall(); | ||
| } | ||
|
|
||
| (address to, uint256 pointer) = LibBytes.readAddress(data, 0); | ||
|
|
||
| bool success; | ||
| (success) = LibOptim.delegatecall(to, gasleft(), data[pointer:]); | ||
| if (!success) { | ||
| revert DelegateCallFailed(LibOptim.returnData()); | ||
| } | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| pragma solidity ^0.8.27; | ||
|
|
||
| import { DelegateAnything } from "../../src/helpers/DelegateAnything.sol"; | ||
| import { AdvTest } from "../utils/TestUtils.sol"; | ||
|
|
||
| contract Target { | ||
|
|
||
| function setStorageSlot( | ||
| bytes32 slot, | ||
| uint256 value | ||
| ) external { | ||
| assembly { | ||
| sstore(slot, value) | ||
| } | ||
| } | ||
|
|
||
| function revertCall() external pure { | ||
| revert("Revert message"); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| contract DelegateCallWrapper { | ||
|
|
||
| DelegateAnything public delegateAnything; | ||
|
|
||
| constructor( | ||
| DelegateAnything _delegateAnything | ||
| ) { | ||
| delegateAnything = _delegateAnything; | ||
| } | ||
|
|
||
| function delegateCall( | ||
| bytes32 opHash, | ||
| uint256 startingGas, | ||
| uint256 index, | ||
| uint256 numCalls, | ||
| uint256 space, | ||
| bytes calldata data | ||
| ) external { | ||
| (bool success, bytes memory returnData) = address(delegateAnything) | ||
| .delegatecall( | ||
| abi.encodeWithSelector( | ||
| DelegateAnything.handleSequenceDelegateCall.selector, opHash, startingGas, index, numCalls, space, data | ||
| ) | ||
| ); | ||
| if (!success) { | ||
| assembly { | ||
| revert(add(returnData, 0x20), mload(returnData)) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
| contract DelegateAnythingTest is AdvTest { | ||
|
|
||
| DelegateAnything public delegateAnything; | ||
| DelegateCallWrapper public wrapper; | ||
| Target public target; | ||
|
|
||
| // Known random hash for storage slot | ||
| bytes32 constant STORAGE_SLOT = keccak256("DelegateAnything.test.storage.slot"); | ||
|
|
||
| function setUp() public { | ||
| delegateAnything = new DelegateAnything(); | ||
| wrapper = new DelegateCallWrapper(delegateAnything); | ||
| target = new Target(); | ||
| } | ||
|
|
||
| function test_directCallReverts() external { | ||
| bytes memory inner = abi.encodeWithSelector(Target.setStorageSlot.selector, STORAGE_SLOT, 42); | ||
| bytes memory data = abi.encodePacked(address(target), inner); | ||
|
|
||
| vm.expectRevert(DelegateAnything.NotDelegateCall.selector); | ||
| delegateAnything.handleSequenceDelegateCall(bytes32(0), 0, 0, 0, 0, data); | ||
| } | ||
|
|
||
| function test_successfulDelegateCall() external { | ||
| uint256 expectedValue = 42; | ||
| bytes32 slot = STORAGE_SLOT; | ||
| bytes memory inner = abi.encodeWithSelector(Target.setStorageSlot.selector, slot, expectedValue); | ||
| bytes memory data = abi.encodePacked(address(target), inner); | ||
|
|
||
| // Call via delegatecall through wrapper | ||
| wrapper.delegateCall(bytes32(0), 0, 0, 0, 0, data); | ||
|
|
||
| // Validate storage slot on wrapper contract (since delegatecall executes in wrapper's context) | ||
| uint256 storedValue = uint256(vm.load(address(wrapper), slot)); | ||
| assertEq(storedValue, expectedValue); | ||
| } | ||
|
|
||
| function test_failedDelegateCall() external { | ||
| bytes memory inner = abi.encodeWithSelector(Target.revertCall.selector); | ||
| bytes memory data = abi.encodePacked(address(target), inner); | ||
|
|
||
| vm.expectRevert( | ||
| abi.encodeWithSelector( | ||
| DelegateAnything.DelegateCallFailed.selector, abi.encodeWithSignature("Error(string)", "Revert message") | ||
| ) | ||
| ); | ||
| wrapper.delegateCall(bytes32(0), 0, 0, 0, 0, data); | ||
| } | ||
|
|
||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
we could simplify it a bit, I don't think this is needed