-
Notifications
You must be signed in to change notification settings - Fork 30
feat: [v0.8-develop] Simplify hook declaration and storage #51
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
Changes from all commits
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 |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ pragma solidity ^0.8.25; | |
| import {EnumerableSet} from "@openzeppelin/contracts/utils/structs/EnumerableSet.sol"; | ||
|
|
||
| import {IPlugin} from "../interfaces/IPlugin.sol"; | ||
| import {ExecutionHook} from "../interfaces/IAccountLoupe.sol"; | ||
| import {FunctionReference} from "../interfaces/IPluginManager.sol"; | ||
|
|
||
| // bytes = keccak256("ERC6900.UpgradeableModularAccount.Storage") | ||
|
|
@@ -44,10 +45,7 @@ struct SelectorData { | |
| // The pre validation hooks for this function selector. | ||
| EnumerableSet.Bytes32Set preValidationHooks; | ||
| // The execution hooks for this function selector. | ||
| EnumerableSet.Bytes32Set preHooks; | ||
| // bytes21 key = pre hook function reference | ||
| mapping(FunctionReference => FunctionReference) associatedPostHooks; | ||
| EnumerableSet.Bytes32Set postOnlyHooks; | ||
| EnumerableSet.Bytes32Set executionHooks; | ||
| } | ||
|
|
||
| struct AccountStorage { | ||
|
|
@@ -79,6 +77,34 @@ function getPermittedCallKey(address addr, bytes4 selector) pure returns (bytes2 | |
|
|
||
| using EnumerableSet for EnumerableSet.Bytes32Set; | ||
|
|
||
| function toSetValue(FunctionReference functionReference) pure returns (bytes32) { | ||
| return bytes32(FunctionReference.unwrap(functionReference)); | ||
| } | ||
|
|
||
| function toFunctionReference(bytes32 setValue) pure returns (FunctionReference) { | ||
| return FunctionReference.wrap(bytes21(setValue)); | ||
| } | ||
|
|
||
| // ExecutionHook layout: | ||
| // 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF______________________ Hook Function Reference | ||
| // 0x__________________________________________AA____________________ is pre hook | ||
| // 0x____________________________________________BB__________________ is post hook | ||
|
|
||
| function toSetValue(ExecutionHook memory executionHook) pure returns (bytes32) { | ||
| return bytes32(FunctionReference.unwrap(executionHook.hookFunction)) | ||
| | bytes32(executionHook.isPreHook ? uint256(1) << 80 : 0) | ||
| | bytes32(executionHook.isPostHook ? uint256(1) << 72 : 0); | ||
| } | ||
|
|
||
| function toExecutionHook(bytes32 setValue) | ||
| pure | ||
| returns (FunctionReference hookFunction, bool isPreHook, bool isPostHook) | ||
| { | ||
| hookFunction = FunctionReference.wrap(bytes21(setValue)); | ||
| isPreHook = (uint256(setValue) >> 80) & 0xFF == 1; | ||
| isPostHook = (uint256(setValue) >> 72) & 0xFF == 1; | ||
|
Comment on lines
+104
to
+105
Collaborator
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. Curious why
Contributor
Author
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 used |
||
| } | ||
|
|
||
| /// @dev Helper function to get all elements of a set into memory. | ||
| function toFunctionReferenceArray(EnumerableSet.Bytes32Set storage set) | ||
| view | ||
|
|
||
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.
Perhaps we could precede the hook function with the type identifier or implement a sorting byte. This way, in storage, all prehooks could be organized before posthooks, allowing the loop to terminate early.
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.
I agree this would be useful in practice. Currently though, the spec doesn't mandate any ordering for hooks, so can we address this problem in a different PR, for this issue? erc6900/resources#5
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.
👍 thanks!