如何为 Substrate Runtime 实现 EVM Trait?

How to implement the EVM Trait for a Substrate Runtime?

正在关注 adding a module to your runtime, I'm trying to implement the Parity Substrate paint-evm trait for the Dothereum Runtime

EVM module trait定义如下:

pub trait Trait: Trait + Trait {
    type FeeCalculator: FeeCalculator;
    type ConvertAccountId: ConvertAccountId<Self::AccountId>;
    type Currency: Currency<Self::AccountId>;
    type Event: From<Event> + Into<Self::Event>;
    type Precompiles: Precompiles;
}

不过,此处的 添加模块 教程有点含糊,建议您:

".. explore the source code of the [..] module if things don't make sense .."

虽然 EVM 模块代码看起来并不太复杂,但我无法理解如何为我的运行时实现 EVM 特性:

impl evm::Trait for Runtime {
    type FeeCalculator = ();
    type ConvertAccountId = ();
    type Currency = Balances; 
    type Event = Event;
    type Precompiles = ();
}

FeeCalculatorConvertAccountId 需要什么类型?

因为 pallet-evm 没有为您需要的类型提供默认实现,您需要自己创建它们。

use paint_evm::{FeeCalculator, ConvertAccountId};
use primitives::{U256, H160};

pub struct FixedGasPrice;

impl FeeCalculator for FixedGasPrice {
    fn gas_price() -> U256 {
        // Gas price is always one token per gas.
        1.into()
    }
}

pub struct TruncatedAccountId;

impl<AccountId> ConvertAccountId<AccountId> for TruncatedAccountId {
    fn convert_account_id(account_id: &AccountId) -> H160 {
        //TODO just truncate the fist several bits and return the resulting H160
        // Or maybe hashing is easier to figure out
        unimplemented!();
    }
}

impl paint_evm::Trait for Runtime {
    type FeeCalculator = FixedGasPrice;
    type ConvertAccountId = TruncatedAccountId;
    type Currency = Balances;
    type Event = Event;
    type Precompiles = (); // We can use () here because paint_evm provides an
                           // `impl Precompiles for ()``
                           // block that always returns none (line 75)
}

随着我对自己的了解越来越多,我期待着改进这个答案。