如何为 Substrate 运行时实现 EVM Trait?

Afr*_*Afr 3 rust ethereum evm parity-io substrate

在向运行时添加模块之后,我尝试为Dothereum 运行时实现Parity Substrate 特征。paint-evm

EVM模块特征定义如下:

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;
}
Run Code Online (Sandbox Code Playgroud)

然而,这里添加模块的教程有点含糊,鼓励人们:

“.. 如果事情没有意义,请探索 [..] 模块的源代码 ..”

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

impl evm::Trait for Runtime {
    type FeeCalculator = ();
    type ConvertAccountId = ();
    type Currency = Balances; 
    type Event = Event;
    type Precompiles = ();
}
Run Code Online (Sandbox Code Playgroud)

这里有什么类型的人FeeCalculator以及ConvertAccountId期望什么类型的人?

Jos*_*rff 5

由于 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)
}
Run Code Online (Sandbox Code Playgroud)

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