# ExecutorManager.sol

> ExecutorManagerコントラクトは、ERC7579における Executor モジュールの管理機能を提供します。

- URL: https://cardene.dev/books/kernel/201f0a
- 本: Kernel (https://cardene.dev/books/kernel.md)
- 章: 5 / 13
- 前の章: https://cardene.dev/books/kernel/274e29.md
- 次の章: https://cardene.dev/books/kernel/ddc9f9.md
- 公開日: 2025-06-21
- 更新日: 2025-06-21
- トピック: Blockchain
- タグ: ブロックチェーン, solidity, ethereum, smartcontract

## 概要

**ExecutorManager**コントラクトは、**ERC7579**における `Executor` モジュールの管理機能を提供します。
`Executor` モジュールは、UserOperationで指定された処理をコントラクトアカウントの代わりに実行するコントラクトです。
コントラクトアカウントに対して `Executor` モジュールをインストール・アンインストールし、その設定情報を特定のストレージスロットに保持します。
また、インストール時には `ModuleInstalled` イベントを発行し、モジュールの初期化処理を呼び出します。

## コード

https://github.com/zerodevapp/kernel/blob/dev/src/core/ExecutorManager.sol

```solidity
// 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`

```solidity
import { EXECUTOR_MANAGER_STORAGE_SLOT } from "../types/Constants.sol";
```

Executor モジュールの設定情報を格納するストレージ領域を一意に特定する定数スロット番号。
コントラクトがアップグレード可能なプロキシ構成で動作するときに、ストレージの位置衝突を避けるための `bytes32` 定数です。

### `MODULE_TYPE_EXECUTOR`

```solidity
import { MODULE_TYPE_EXECUTOR } from "../types/Constants.sol";
```

`Executor` モジュールを示す**ERC7579**のモジュールタイプID。
数値 `2` を想定しており、`ModuleInstalled` イベントの引数に使用されます。

### `ModuleInstalled`

```solidity
event ModuleInstalled(uint256 moduleTypeId, address module);
```

`Executor` モジュールがコントラクトアカウントにインストールされた時に発行されるイベント。
`installModule` 処理の最後で発行されます。

**パラメータ**

* `moduleTypeId`
    * インストールされたモジュールのタイプID。
* `module`
    * インストールされたモジュールコントラクトのアドレス。

### `ExecutorConfig`

```solidity
struct ExecutorConfig {
    IHook hook; // address(1) : hook not required, address(0) : validator not installed
}
```

Executor モジュールごとの連携用フック情報を保持する構造体。
`hook` フィールドには、前後処理を行う `IHook` モジュールのアドレスを格納します。
`address(1)` はフック不要、`address(0)` は該当モジュール未設定を意味します。

**パラメータ**

* `hook`
    * 実行前後チェック用の `IHook` モジュールアドレス。

### `ExecutorStorage`

```solidity
struct ExecutorStorage {
    mapping(IExecutor => ExecutorConfig) executorConfig;
}
```

全ての `Executor` モジュールの設定情報を保持するためのストレージ構造体。
* キーに `IExecutor` モジュールアドレス、値に `ExecutorConfig` を格納するマッピングです。
* `EXECUTOR_MANAGER_STORAGE_SLOT` にアセンブリで紐付けられています。

**パラメータ**

* `executorConfig`
    * `IExecutor` ごとの `ExecutorConfig` を管理するマッピング。

## 関数

### `executorConfig`

```solidity
function executorConfig(IExecutor executor) external view returns (ExecutorConfig memory) {
    return _executorConfig(executor);
}
```

指定した `Executor` モジュールの設定情報を取得する関数。
`_executorConfig` 関数を呼び出して`ExecutorConfig` を返します。

**引数**

* `executor`
    * 設定情報を取得したい `IExecutor` モジュールのアドレス。

**戻り値**

* `ExecutorConfig memory`
    * 指定モジュールのフック設定を含む構造体。

### `_executorConfig`

```solidity
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`

```solidity
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` モジュールをインストールして初期化処理を行う関数。
1. `_installExecutorWithoutInit` で設定情報を保存してイベント発行をします。
2. `executorData` が空の場合は `call` で `onInstall`関数を呼び出し、それ以外は `onInstall` を直接呼び出します。

:::message
`executorData` は初期化データであるため、空でない場合は初期化処理の成功が必須です。
しかし、空の場合は初期化処理が失敗しても問題ありません。
そのため、`call` で呼び出しますが、結果は確認しないようにしています。
`onInstall` を直接呼び出す場合は処理が失敗すると全体の処理が失敗となります。
:::

**引数**

* `executor`
    * インストール対象の `IExecutor` モジュールのアドレス。
* `executorData`
    * `onInstall` に渡す初期化用データ。
* `hook`
    * 前後処理用の `IHook` モジュールのアドレス。

### `_installExecutorWithoutInit`

```solidity
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`

```solidity
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`
    * クリア前に設定されていたフックモジュールのアドレス。
