コントラクト

classDiagram
    direction LR

    class Ownable {
        <<Interface>>
        +owner() address
        +transferOwnership(address)
    }

    class ERC20 {
        <<Interface>>
        +totalSupply() uint256
        +balanceOf(address) uint256
        +transfer(address, uint256)
    }

    class AllowList {
        <<Wrapper>>
        +setAdmin(address)
        +setEnabled(address)
        +setManager(address)
        +revoke(address)
        +readAllowList(address) uint256
    }

    class ExampleWarp {
        +sendWarpMessage(bytes)
        +validateWarpMessage(...)
        +validateWarpBlockHash(...)
        +validateGetBlockchainID(bytes32)
    }

    class ExampleFeeManager {
        +enableWAGMIFees()
        +enableCChainFees()
        +enableCustomFees(FeeConfig)
        +getCurrentFeeConfig() FeeConfig
        +getFeeConfigLastChangedAt() uint256
    }

    class ExampleDeployerList {
        +deployContract()
    }

    class ExampleTxAllowList {
        +deployContract()
    }

    class ERC20NativeMinter {
        +mint(address, uint256)
        +burn(address, uint256)
        +mintdraw(uint256)
        +deposit()
        +decimals() uint8
    }

    class ExampleRewardManager {
        +currentRewardAddress() address
        +setRewardAddress(address)
        +allowFeeRecipients()
        +disableRewards()
        +areFeeRecipientsAllowed() bool
    }

    Ownable <|-- AllowList
    Ownable <|-- ExampleRewardManager

    AllowList <|-- ERC20NativeMinter
    AllowList <|-- ExampleFeeManager
    AllowList <|-- ExampleDeployerList
    AllowList <|-- ExampleTxAllowList
    
    ERC20 <|-- ERC20NativeMinter
  • Ownable OpenZeppelinの標準的なコントラクト。 コントラクトの所有権(オーナー)を管理する基本的な機能を提供します。
  • ERC20 OpenZeppelinの標準的なトークンコントラクト。 ERC20トークンの基本機能を実装します。
  • AllowList プリコンパイルを操作するためのラッパーコントラクトの基盤。 Ownableを継承し、オーナーのみが権限設定を変更できるようにします。 setAdminやsetEnabledなどの権限管理関数を持ちます。
  • ExampleWarp WarpMessengerプリコンパイルを操作するサンプル。 クロスチェーンメッセージを送信するsendWarpMessageや、受信したメッセージを検証するvalidateWarpMessageなどの関数を持ちます。
  • ExampleFeeManager FeeManagerプリコンパイルを操作するサンプル。 AllowListを継承し、権限を持つユーザーのみがenableWAGMIFeesなどのガス料金設定変更関数を呼び出せます。
  • ExampleDeployerList ContractDeployerAllowListプリコンパイルを操作するサンプル。 AllowListを継承しており、テスト用にdeployContract関数を持ちます。
  • ExampleTxAllowList TxAllowListプリコンパイルを操作するサンプル。 ExampleDeployerListと同様にAllowListを継承し、deployContract関数を持ちます。
  • ERC20NativeMinter ネイティブトークンとERC20トークンを交換するコントラクト。 ERC20のトークン機能と、AllowListのオーナーによるmint/burn権限管理機能を継承しています。
  • ExampleRewardManager RewardManagerプリコンパイルを操作するサンプル。 Ownableを継承し、オーナーのみがsetRewardAddressなどの報酬設定関数を呼び出せます。

プリコンパイル

設定と実行

大きく分けて、設定と実行の2つに分けられ、この2つが連携してプリコンパイルシステム全体を構成しています。

EVM起動時(設定フェーズ)

Subnet-EVMが起動すると、まずmodules.Registererが、Goのinit()機能を通じてシステムに存在する全てのプリコンパイルModuleをグローバルなリストに登録します。 次に、EVMはブロックチェーンの初期設定ファイル(ジェネシス)を読み込みます。 設定ファイル内の各キー(例: "feeManagerConfig")に対応するModuleのConfiguratorを使って、JSON設定をGoの具体的なConfig構造体(例: feemanager.FeeManagerConfig)にデコードします。 デコードされたConfigオブジェクトのVerify()メソッドを呼び出し、設定値が不正でないか検証します。 検証を通過すると、ConfiguratorのConfigure()メソッドが呼び出され、Configに定義された初期設定(例: AllowListの初期メンバー)がEVMのステートに書き込まれます。 この段階で、設定アーキテクチャの役割は完了し、プリコンパイルの初期状態が確定します。

トランザクション実行時(実行フェーズ)

ユーザーがプリコンパイルのアドレス(例: 0x0200...0003 for FeeManager)にトランザクションを送信します。 EVMは、そのアドレスがプリコンパイルに属することを認識し、起動時に登録されたModuleリストから対応するModuleを検索します。 EVMは、見つかったModuleからContractフィールド(実行アーキテクチャで定義された実行ロジック本体)を取得します。 取得したContractのRun()メソッドを呼び出します。 このとき、現在のステート情報などを含むAccessibleStateを引数として渡します。 Run()メソッド(通常はstatefulPrecompileWithFunctionSelectors)は、トランザクションのcalldataから関数セレクタを読み取り、対応するGoの内部関数(例: setFeeConfig)を実行します。 実行関数は、AccessibleStateを通じてEVMのステートを読み書きし、処理結果を返します。

このように、設定アーキテクチャは「起動時に一度だけ、プリコンパイルの初期状態を準備する」ためのものであり、実行アーキテクチャは「トランザクションが来るたびに具体的な処理を実行する」**ためのものです。

設定アーキテクチャ

この図は、各プリコンパイルがブロックチェーンの初期設定ファイルやネットワークアップグレードによって、どのように設定され、検証されるかを示しています。

classDiagram
    direction TB

    %% ===== precompileconfig =====
    class precompileconfig_Config {
        <<Interface>>
        +Key() : string
        +Timestamp() : *uint64
        +Verify(cfg : precompileconfig_ChainConfig)
        +Equal(cfg : precompileconfig_Config) : bool
    }
    class precompileconfig_Upgrade {
        +BlockTimestamp : *uint64
        +Disable : bool
    }
    class precompileconfig_ChainConfig {
        <<Interface>>
        +IsDurango(height : uint64) : bool
    }

    %% ===== allowlist =====
    class allowlist_AllowListConfig {
        +AdminAddresses : []common.Address
        +ManagerAddresses : []common.Address
        +EnabledAddresses : []common.Address
    }

    %% ===== concrete_configs =====
    class concrete_configs_TxAllowListConfig
    class concrete_configs_FeeManagerConfig {
        +InitialFeeConfig : *commontype.FeeConfig
    }
    class concrete_configs_NativeMinterConfig {
        +InitialMint : map[common.Address]*math.HexOrDecimal256
    }
    class concrete_configs_RewardManagerConfig {
        +InitialRewardConfig : *InitialRewardConfig
    }
    class concrete_configs_WarpConfig {
        +QuorumNumerator : uint64
    }

    %% ===== Relationships =====
    precompileconfig_Config <|-- concrete_configs_TxAllowListConfig
    concrete_configs_TxAllowListConfig *-- precompileconfig_Upgrade
    concrete_configs_TxAllowListConfig *-- allowlist_AllowListConfig

    precompileconfig_Config <|-- concrete_configs_FeeManagerConfig
    concrete_configs_FeeManagerConfig *-- precompileconfig_Upgrade
    concrete_configs_FeeManagerConfig *-- allowlist_AllowListConfig

    precompileconfig_Config <|-- concrete_configs_NativeMinterConfig
    concrete_configs_NativeMinterConfig *-- precompileconfig_Upgrade
    concrete_configs_NativeMinterConfig *-- allowlist_AllowListConfig

    precompileconfig_Config <|-- concrete_configs_RewardManagerConfig
    concrete_configs_RewardManagerConfig *-- precompileconfig_Upgrade
    concrete_configs_RewardManagerConfig *-- allowlist_AllowListConfig

    precompileconfig_Config <|-- concrete_configs_WarpConfig
    concrete_configs_WarpConfig *-- precompileconfig_Upgrade

precompileconfig.Config

全てのプリコンパイル設定構造体が実装しなければならないインターフェースです。 EVMが様々な種類のプリコンパイル設定を統一的に扱うためのルールを定義します。

  • Key() genesis.json内のJSONオブジェクトを、どのプリコンパイルの設定か識別するための文字列キーを返します。
  • Timestamp() プリコンパイルが有効化されるブロックのタイムスタンプを返します。
  • Verify() 設定値が論理的に正しいか(例: アドレスの重複がないか、数値が範囲内か)を検証します。 EVM起動時に呼び出され、失敗するとノードは起動しません。

precompileconfig.Upgrade

プリコンパイルの有効化・無効化のタイミングを定義する再利用可能な構造体です。 BlockTimestampフィールドとDisableフラグを持ちます。 ほとんどのConfig実装は、この構造体を自身のフィールドとして埋め込み(Composition *--)、アップグレード機能を取り込みます。

allowlist.AllowListConfig

AllowList機能を持つプリコンパイルが、初期メンバー(AdminAddresses, ManagerAddressesなど)を定義するために利用する、再利用可能な設定構造体です。

concrete_configs内の各Config

各プリコンパイル(TxAllowList, FeeManagerなど)に特化した設定構造体です。 これらは全てprecompileconfig.Configインターフェースを実装(Inheritance <|--)します。 図が示すように、多くはprecompileconfig.Upgradeとallowlist.AllowListConfigを埋め込むことで、これらの共通設定機能を使用しています。 それに加え、FeeManagerConfigがInitialFeeConfigを持つように、各プリコンパイルは自身に固有の初期設定項目をフィールドとして追加で持ちます。

実行アーキテクチャ

この図は、EVMからの呼び出しに応じて、各プリコンパイルがどのように実行されるかを示しています。

classDiagram
    direction TB

    %% ===== contract =====
    class contract_StatefulPrecompiledContract {
        <<Interface>>
        +Run(state, caller, addr, input, gas, readOnly) : any
    }

    %% ===== modules =====
    class modules_Module {
        +ConfigKey : string
        +Address : common.Address
        +Contract : contract_StatefulPrecompiledContract
        +Configurator : Configurator
    }
    class modules_Registerer {
        +RegisterModule(m : modules_Module) : void
    }

    %% ===== concrete_precompiles =====
    class concrete_precompiles_AllowListPrecompile {
        +Run(...) : any
        #createAllowListRoleSetter(...) : any
        #createReadAllowList(...) : any
    }
    class concrete_precompiles_TxAllowListPrecompile
    class concrete_precompiles_FeeManagerPrecompile {
        +Run(...) : any
        #setFeeConfig(...) : void
        #getFeeConfig(...) : any
    }
    class concrete_precompiles_NativeMinterPrecompile {
        +Run(...) : any
        #mintNativeCoin(...) : any
    }
    class concrete_precompiles_RewardManagerPrecompile {
        +Run(...) : any
        #setRewardAddress(...) : void
        #allowFeeRecipients() : any
    }
    class concrete_precompiles_WarpPrecompile {
        +Run(...) : any
        #sendWarpMessage(...) : any
        #getVerifiedWarpMessage(...) : any
    }

    %% ===== Relationships =====
    contract_StatefulPrecompiledContract <|-- concrete_precompiles_AllowListPrecompile
    contract_StatefulPrecompiledContract <|-- concrete_precompiles_TxAllowListPrecompile
    contract_StatefulPrecompiledContract <|-- concrete_precompiles_FeeManagerPrecompile
    contract_StatefulPrecompiledContract <|-- concrete_precompiles_NativeMinterPrecompile
    contract_StatefulPrecompiledContract <|-- concrete_precompiles_RewardManagerPrecompile
    contract_StatefulPrecompiledContract <|-- concrete_precompiles_WarpPrecompile

    concrete_precompiles_TxAllowListPrecompile ..> concrete_precompiles_AllowListPrecompile : "<< uses factory >>"

    modules_Module *-- contract_StatefulPrecompiledContract : contains
    modules_Registerer o-- modules_Module : manages

contract.StatefulPrecompiledContract

全てのプリコンパイルが実装するインターフェース。 EVMがプリコンパイルを呼び出すための単一のエントリーポイントRunメソッドを定義します。

modules.Module / modules.Registerer

Moduleは、個々のプリコンパイルの実行ロジック本体 (Contractフィールド) を含むコンテナです。 Registererは、EVM起動時に全てのModuleを一元的に登録し、アドレスとContractの対応関係を管理します。

concrete_precompiles内の各Precompile

これらは全てStatefulPrecompiledContractインターフェースを実装するプリコンパイルの実行ロジックです。 Runメソッドは通常、contract.statefulPrecompileWithFunctionSelectorsによって実装され、calldataの関数セレクタに応じて、setFeeConfig, mintNativeCoinなどの内部実行関数(図では保護されたメソッド#で表現)に処理をディスパッチします。

  • AllowListPrecompile 汎用的なロールベースのアクセス制御(ホワイトリスト)機能を提供する再利用可能なプリコンパイル実装の基盤です。 他のプリコンパイルは、このロジックを直接再利用することで、独自のアクセス制御を簡単に実装できます。

  • TxAllowListPrecompile Subnet上でトランザクションを発行できるアドレスを管理するプリコンパイルです。 この実装はallowlistが提供するファクトリ関数を呼び出すことで、AllowListPrecompileのロジックをそのまま再利用しています。

  • FeeManagerPrecompile ネットワーク全体のガス料金パラメータ(EIP1559のベースフィーなど)を動的に変更するプリコンパイルです。 allowlistパッケージが提供するヘルパー関数(例: GetAllowListStatus)を内部で呼び出して「誰が料金設定を変更できるか」というアクセス制御を行いつつ、ガス料金設定という固有のコア機能を実装しています。

  • NativeMinterPrecompile Subnetのネイティブトークンを新たにMint(発行)する機能を持つプリコンパイルです。 allowlistのヘルパー関数を利用して「誰がMintできるか」を制御しつつ、ネイティブトークンの供給量を増やすという独自のロジックを持っています。

  • RewardManagerPrecompile トランザクション手数料の分配方法(リワード)を管理するプリコンパイルです。 allowlistのヘルパー関数で「誰が報酬設定を変更できるか」を制御しながら、手数料の分配先を特定アドレスに指定したり、バリデーターに分配したりする固有の機能を実装しています。

  • WarpPrecompile 異なるSubnet間で非同期のクロスチェーンメッセージを安全に送受信するための、Avalanche Warp Messaging (AWM) を実装したプリコンパイルです。 その性質上、特定の管理者によるアクセス制御は不要なためallowlistには依存せず、BLSマルチシグネチャの検証など、独自の高度なロジックを実装しています。