概要
ExecutorManagerコントラクトは、ERC7579における Executor モジュールの管理機能を提供します。
Executor モジュールは、UserOperationで指定された処理をコントラクトアカウントの代わりに実行するコントラクトです。
コントラクトアカウントに対して Executor モジュールをインストール・アンインストールし、その設定情報を特定のストレージスロットに保持します。
また、インストール時には ModuleInstalled イベントを発行し、モジュールの初期化処理を呼び出します。
コード
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import {IHook, IExecutor, IModule} from "../interfaces/IERC7579Modules.sol";
import {IERC7579Account} from "../interfaces/IERC7579Account.sol";
import {EXECUTOR_MANAGER_STORAGE_SLOT, MODULE_TYPE_EXECUTOR} from "../types/Constants.sol";
abstract contract ExecutorManager {
struct ExecutorConfig {
IHook hook; // address(1) : hook not required, address(0) : validator not installed
}
struct ExecutorStorage {
mapping(IExecutor => ExecutorConfig) executorConfig;
}
function executorConfig(IExecutor executor) external view returns (ExecutorConfig memory) {
return _executorConfig(executor);
}
function _executorConfig(IExecutor executor) internal view returns (ExecutorConfig storage config) {
ExecutorStorage storage es;
bytes32 slot = EXECUTOR_MANAGER_STORAGE_SLOT;
assembly {
es.slot := slot
}
config = es.executorConfig[executor];
}
function _installExecutor(IExecutor executor, bytes calldata executorData, IHook hook) internal {
_installExecutorWithoutInit(executor, hook);
if (executorData.length == 0) {
(bool success,) = address(executor).call(abi.encodeWithSelector(IModule.onInstall.selector, executorData)); // ignore return value
} else {
executor.onInstall(executorData);
}
}
function _installExecutorWithoutInit(IExecutor executor, IHook hook) internal {
if (address(hook) == address(0)) {
hook = IHook(address(1));
}
ExecutorConfig storage config = _executorConfig(executor);
config.hook = hook;
emit IERC7579Account.ModuleInstalled(MODULE_TYPE_EXECUTOR, address(executor));
}
function _clearExecutorData(IExecutor executor) internal returns (IHook hook) {
ExecutorConfig storage config = _executorConfig(executor);
hook = config.hook;
config.hook = IHook(address(0));
}
}変数・イベント・構造体
EXECUTOR_MANAGER_STORAGE_SLOT
import { EXECUTOR_MANAGER_STORAGE_SLOT } from "../types/Constants.sol";Executor モジュールの設定情報を格納するストレージ領域を一意に特定する定数スロット番号。
コントラクトがアップグレード可能なプロキシ構成で動作するときに、ストレージの位置衝突を避けるための bytes32 定数です。
MODULE_TYPE_EXECUTOR
import { MODULE_TYPE_EXECUTOR } from "../types/Constants.sol";Executor モジュールを示すERC7579のモジュールタイプID。
数値 2 を想定しており、ModuleInstalled イベントの引数に使用されます。
ModuleInstalled
event ModuleInstalled(uint256 moduleTypeId, address module);Executor モジュールがコントラクトアカウントにインストールされた時に発行されるイベント。
installModule 処理の最後で発行されます。
パラメータ
moduleTypeId- インストールされたモジュールのタイプID。
module- インストールされたモジュールコントラクトのアドレス。
ExecutorConfig
struct ExecutorConfig {
IHook hook; // address(1) : hook not required, address(0) : validator not installed
}Executor モジュールごとの連携用フック情報を保持する構造体。
hook フィールドには、前後処理を行う IHook モジュールのアドレスを格納します。
address(1) はフック不要、address(0) は該当モジュール未設定を意味します。
パラメータ
hook- 実行前後チェック用の
IHookモジュールアドレス。
- 実行前後チェック用の
ExecutorStorage
struct ExecutorStorage {
mapping(IExecutor => ExecutorConfig) executorConfig;
}全ての Executor モジュールの設定情報を保持するためのストレージ構造体。
- キーに
IExecutorモジュールアドレス、値にExecutorConfigを格納するマッピングです。 EXECUTOR_MANAGER_STORAGE_SLOTにアセンブリで紐付けられています。
パラメータ
executorConfigIExecutorごとのExecutorConfigを管理するマッピング。
関数
executorConfig
function executorConfig(IExecutor executor) external view returns (ExecutorConfig memory) {
return _executorConfig(executor);
}指定した Executor モジュールの設定情報を取得する関数。
_executorConfig 関数を呼び出してExecutorConfig を返します。
引数
executor- 設定情報を取得したい
IExecutorモジュールのアドレス。
- 設定情報を取得したい
戻り値
ExecutorConfig memory- 指定モジュールのフック設定を含む構造体。
_executorConfig
function _executorConfig(IExecutor executor) internal view returns (ExecutorConfig storage config) {
ExecutorStorage storage es;
bytes32 slot = EXECUTOR_MANAGER_STORAGE_SLOT;
assembly {
es.slot := slot
}
config = es.executorConfig[executor];
}指定モジュールの設定情報を格納したストレージ参照を返す内部関数。
EXECUTOR_MANAGER_STORAGE_SLOT のストレージ位置をアセンブリで指定し、ExecutorStorage にキャストしてから executorConfig マッピングを参照します。
引数
executor- 参照したい
IExecutorモジュールのアドレス。
- 参照したい
戻り値
ExecutorConfig storage- 指定モジュールの永続的なストレージ参照。
_installExecutor
function _installExecutor(IExecutor executor, bytes calldata executorData, IHook hook) internal {
_installExecutorWithoutInit(executor, hook);
if (executorData.length == 0) {
(bool success,) = address(executor).call(abi.encodeWithSelector(IModule.onInstall.selector, executorData)); // ignore return value
} else {
executor.onInstall(executorData);
}
}Executor モジュールをインストールして初期化処理を行う関数。
_installExecutorWithoutInitで設定情報を保存してイベント発行をします。executorDataが空の場合はcallでonInstall関数を呼び出し、それ以外はonInstallを直接呼び出します。
executorData は初期化データであるため、空でない場合は初期化処理の成功が必須です。
しかし、空の場合は初期化処理が失敗しても問題ありません。
そのため、call で呼び出しますが、結果は確認しないようにしています。
onInstall を直接呼び出す場合は処理が失敗すると全体の処理が失敗となります。
引数
executor- インストール対象の
IExecutorモジュールのアドレス。
- インストール対象の
executorDataonInstallに渡す初期化用データ。
hook- 前後処理用の
IHookモジュールのアドレス。
- 前後処理用の
_installExecutorWithoutInit
function _installExecutorWithoutInit(IExecutor executor, IHook hook) internal {
if (address(hook) == address(0)) {
hook = IHook(address(1));
}
ExecutorConfig storage config = _executorConfig(executor);
config.hook = hook;
emit IERC7579Account.ModuleInstalled(MODULE_TYPE_EXECUTOR, address(executor));
}Executor モジュールの設定情報を保存してイベントを発行する関数。
フックアドレスが未設定(address(0))の場合は address(1) に置き換えます。ExecutorStorage に hook を保存し、ModuleInstalled イベントを発行します。
引数
executor- 設定対象の
IExecutorモジュールのアドレス。
- 設定対象の
hook- 前後処理用の
IHookモジュールのアドレス。
- 前後処理用の
_clearExecutorData
function _clearExecutorData(IExecutor executor) internal returns (IHook hook) {
ExecutorConfig storage config = _executorConfig(executor);
hook = config.hook;
config.hook = IHook(address(0));
}Executor モジュールの設定情報をリセットし、リセット前のフックアドレスを返す関数。
指定モジュールの ExecutorConfig.hook を address(0) にリセットします。リセット前のフックアドレスを戻り値として返却することで、後処理に利用できます。
引数
executor- クリア対象の
IExecutorモジュールのアドレス。
- クリア対象の
戻り値
IHook hook- クリア前に設定されていたフックモジュールのアドレス。