Skip to content

Commit

Permalink
feat: run duplicate pre-hooks just once
Browse files Browse the repository at this point in the history
  • Loading branch information
jaypaik committed Jan 23, 2024
1 parent 981bee8 commit 1869bd4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 36 deletions.
45 changes: 21 additions & 24 deletions src/account/AccountLoupe.sol
Original file line number Diff line number Diff line change
Expand Up @@ -44,30 +44,7 @@ abstract contract AccountLoupe is IAccountLoupe {

/// @inheritdoc IAccountLoupe
function getExecutionHooks(bytes4 selector) external view returns (ExecutionHooks[] memory execHooks) {
execHooks = _getHooks(getAccountStorage().selectorData[selector].executionHooks);
}

/// @inheritdoc IAccountLoupe
function getPreValidationHooks(bytes4 selector)
external
view
returns (
FunctionReference[] memory preUserOpValidationHooks,
FunctionReference[] memory preRuntimeValidationHooks
)
{
preUserOpValidationHooks =
toFunctionReferenceArray(getAccountStorage().selectorData[selector].preUserOpValidationHooks);
preRuntimeValidationHooks =
toFunctionReferenceArray(getAccountStorage().selectorData[selector].preRuntimeValidationHooks);
}

/// @inheritdoc IAccountLoupe
function getInstalledPlugins() external view returns (address[] memory pluginAddresses) {
pluginAddresses = getAccountStorage().plugins.values();
}

function _getHooks(HookGroup storage hooks) internal view returns (ExecutionHooks[] memory execHooks) {
HookGroup storage hooks = getAccountStorage().selectorData[selector].executionHooks;
uint256 preExecHooksLength = hooks.preHooks.length();
uint256 postOnlyExecHooksLength = hooks.postOnlyHooks.length();
uint256 maxExecHooksLength = postOnlyExecHooksLength;
Expand Down Expand Up @@ -129,4 +106,24 @@ abstract contract AccountLoupe is IAccountLoupe {
mstore(execHooks, actualExecHooksLength)
}
}

/// @inheritdoc IAccountLoupe
function getPreValidationHooks(bytes4 selector)
external
view
returns (
FunctionReference[] memory preUserOpValidationHooks,
FunctionReference[] memory preRuntimeValidationHooks
)
{
preUserOpValidationHooks =
toFunctionReferenceArray(getAccountStorage().selectorData[selector].preUserOpValidationHooks);
preRuntimeValidationHooks =
toFunctionReferenceArray(getAccountStorage().selectorData[selector].preRuntimeValidationHooks);
}

/// @inheritdoc IAccountLoupe
function getInstalledPlugins() external view returns (address[] memory pluginAddresses) {
pluginAddresses = getAccountStorage().plugins.values();
}
}
15 changes: 3 additions & 12 deletions src/account/UpgradeableModularAccount.sol
Original file line number Diff line number Diff line change
Expand Up @@ -470,14 +470,6 @@ contract UpgradeableModularAccount is
returns (PostExecToRun[] memory postHooksToRun)
{
HookGroup storage hooks = getAccountStorage().selectorData[selector].executionHooks;

return _doPreHooks(hooks, data);
}

function _doPreHooks(HookGroup storage hooks, bytes calldata data)
internal
returns (PostExecToRun[] memory postHooksToRun)
{
uint256 preExecHooksLength = hooks.preHooks.length();
uint256 postOnlyHooksLength = hooks.postOnlyHooks.length();
uint256 maxPostExecHooksLength = postOnlyHooksLength;
Expand Down Expand Up @@ -517,21 +509,20 @@ contract UpgradeableModularAccount is
revert AlwaysDenyRule();
}

bytes memory preExecHookReturnData = _runPreExecHook(preExecHook, data);

uint256 associatedPostExecHooksLength = hooks.associatedPostHooks[preExecHook].length();
if (associatedPostExecHooksLength > 0) {
for (uint256 j = 0; j < associatedPostExecHooksLength;) {
(key,) = hooks.associatedPostHooks[preExecHook].at(j);
postHooksToRun[actualPostHooksToRunLength].postExecHook = _toFunctionReference(key);
postHooksToRun[actualPostHooksToRunLength].preExecHookReturnData =
_runPreExecHook(preExecHook, data);
postHooksToRun[actualPostHooksToRunLength].preExecHookReturnData = preExecHookReturnData;

unchecked {
++actualPostHooksToRunLength;
++j;
}
}
} else {
_runPreExecHook(preExecHook, data);
}

unchecked {
Expand Down

0 comments on commit 1869bd4

Please sign in to comment.