From da85216fa68e81c86c790dcf118c114616ab22c7 Mon Sep 17 00:00:00 2001 From: Dmitry Lavrenov <39522748+dmitrylavrenov@users.noreply.github.com> Date: Sun, 18 Jun 2023 11:34:00 +0300 Subject: [PATCH] Move `AccountProvider` interface into `fp-em` (#71) * Move account provider interface into fp-em * Implement AccountProvider interface for EvmSystem * Revert formatter * Move NativeSystemAccountProvider to pallet-evm --- Cargo.lock | 1 + frame/evm-system/Cargo.toml | 4 +++ frame/evm-system/src/lib.rs | 21 +++++++++++++ frame/evm/src/lib.rs | 30 +++++++++++++++---- .../evm/src/account_provider.rs | 25 +--------------- primitives/evm/src/lib.rs | 2 ++ 6 files changed, 54 insertions(+), 29 deletions(-) rename {frame => primitives}/evm/src/account_provider.rs (66%) diff --git a/Cargo.lock b/Cargo.lock index 68ce56f7a9..4d497d964b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5586,6 +5586,7 @@ dependencies = [ name = "pallet-evm-system" version = "1.0.0-dev" dependencies = [ + "fp-evm", "frame-support", "frame-system", "log", diff --git a/frame/evm-system/Cargo.toml b/frame/evm-system/Cargo.toml index 235931bd08..0327d9f3bc 100644 --- a/frame/evm-system/Cargo.toml +++ b/frame/evm-system/Cargo.toml @@ -19,6 +19,8 @@ frame-support = { workspace = true } frame-system = { workspace = true } sp-runtime = { workspace = true } sp-std = { workspace = true } +# Frontier +fp-evm = { workspace = true } [dev-dependencies] mockall = "0.11" @@ -36,6 +38,8 @@ std = [ "frame-system/std", "sp-runtime/std", "sp-std/std", + # Frontier + "fp-evm/std", ] try-runtime = [ "frame-support/try-runtime", diff --git a/frame/evm-system/src/lib.rs b/frame/evm-system/src/lib.rs index ad38522983..ad2a6b18d0 100644 --- a/frame/evm-system/src/lib.rs +++ b/frame/evm-system/src/lib.rs @@ -211,6 +211,27 @@ impl StoredMap<::AccountId, ::AccountData> } } +impl fp_evm::AccountProvider for Pallet { + type AccountId = ::AccountId; + type Index = ::Index; + + fn create_account(who: &Self::AccountId) { + let _ = Self::create_account(who); + } + + fn remove_account(who: &Self::AccountId) { + let _ = Self::remove_account(who); + } + + fn account_nonce(who: &Self::AccountId) -> Self::Index { + Self::account_nonce(who) + } + + fn inc_account_nonce(who: &Self::AccountId) { + Self::inc_account_nonce(who); + } +} + /// Interface to handle account creation. pub trait OnNewAccount { /// A new account `who` has been registered. diff --git a/frame/evm/src/lib.rs b/frame/evm/src/lib.rs index 7d288c6a1b..066d0f9fb3 100644 --- a/frame/evm/src/lib.rs +++ b/frame/evm/src/lib.rs @@ -65,9 +65,6 @@ pub mod runner; mod tests; pub mod weights; -mod account_provider; -pub use account_provider::{AccountProvider, NativeSystemAccountProvider}; - pub use evm::{ Config as EvmConfig, Context, ExitError, ExitFatal, ExitReason, ExitRevert, ExitSucceed, }; @@ -90,7 +87,7 @@ use frame_support::{ use frame_system::RawOrigin; use sp_core::{Decode, Encode, Hasher, H160, H256, U256}; use sp_runtime::{ - traits::{BadOrigin, Saturating, UniqueSaturatedInto, AtLeast32Bit, Zero}, + traits::{BadOrigin, Saturating, UniqueSaturatedInto, Zero}, AccountId32, DispatchErrorWithPostInfo, }; use sp_std::{cmp::min, collections::btree_map::BTreeMap, vec::Vec}; @@ -98,7 +95,7 @@ use sp_std::{cmp::min, collections::btree_map::BTreeMap, vec::Vec}; use fp_account::AccountId20; use fp_evm::GenesisAccount; pub use fp_evm::{ - Account, CallInfo, CreateInfo, ExecutionInfoV2 as ExecutionInfo, FeeCalculator, + Account, AccountProvider, CallInfo, CreateInfo, ExecutionInfoV2 as ExecutionInfo, FeeCalculator, InvalidEvmTransactionError, IsPrecompileResult, LinearCostPrecompile, Log, Precompile, PrecompileFailure, PrecompileHandle, PrecompileOutput, PrecompileResult, PrecompileSet, Vicinity, @@ -1053,3 +1050,26 @@ impl OnCreate for Tuple { )*) } } + +/// Native system account provider that `frame_system` provides. +pub struct NativeSystemAccountProvider(sp_std::marker::PhantomData); + +impl AccountProvider for NativeSystemAccountProvider { + type AccountId = T::AccountId; + type Index = T::Index; + + fn account_nonce(who: &Self::AccountId) -> Self::Index { + frame_system::Pallet::::account_nonce(&who) + } + + fn inc_account_nonce(who: &Self::AccountId) { + frame_system::Pallet::::inc_account_nonce(&who) + } + + fn create_account(who: &Self::AccountId) { + let _ = frame_system::Pallet::::inc_sufficients(&who); + } + fn remove_account(who: &Self::AccountId) { + let _ = frame_system::Pallet::::dec_sufficients(&who); + } +} diff --git a/frame/evm/src/account_provider.rs b/primitives/evm/src/account_provider.rs similarity index 66% rename from frame/evm/src/account_provider.rs rename to primitives/evm/src/account_provider.rs index 69484222cf..4fbd07530b 100644 --- a/frame/evm/src/account_provider.rs +++ b/primitives/evm/src/account_provider.rs @@ -1,6 +1,6 @@ //! Custom account provider logic. -use super::*; +use sp_runtime::traits::AtLeast32Bit; /// The account provider interface abstraction layer. /// @@ -39,26 +39,3 @@ pub trait AccountProvider { /// Incremented with each new transaction submitted by the account. fn inc_account_nonce(who: &Self::AccountId); } - -/// Native system account provider that `frame_system` provides. -pub struct NativeSystemAccountProvider(sp_std::marker::PhantomData); - -impl AccountProvider for NativeSystemAccountProvider { - type AccountId = ::AccountId; - type Index = ::Index; - - fn account_nonce(who: &Self::AccountId) -> Self::Index { - frame_system::Pallet::::account_nonce(&who) - } - - fn inc_account_nonce(who: &Self::AccountId) { - frame_system::Pallet::::inc_account_nonce(&who) - } - - fn create_account(who: &Self::AccountId) { - let _ = frame_system::Pallet::::inc_sufficients(&who); - } - fn remove_account(who: &Self::AccountId) { - let _ = frame_system::Pallet::::dec_sufficients(&who); - } -} diff --git a/primitives/evm/src/lib.rs b/primitives/evm/src/lib.rs index 4a4902ba27..f1a4643bb6 100644 --- a/primitives/evm/src/lib.rs +++ b/primitives/evm/src/lib.rs @@ -18,6 +18,7 @@ #![cfg_attr(not(feature = "std"), no_std)] #![deny(unused_crate_dependencies)] +mod account_provider; mod precompile; mod validation; @@ -36,6 +37,7 @@ pub use evm::{ }; pub use self::{ + account_provider::AccountProvider, precompile::{ Context, ExitError, ExitRevert, ExitSucceed, IsPrecompileResult, LinearCostPrecompile, Precompile, PrecompileFailure, PrecompileHandle, PrecompileOutput, PrecompileResult,