From e14fcca936dfb18cd9d6ceb8a61522c7996cb07a Mon Sep 17 00:00:00 2001 From: Dmitry Lavrenov Date: Tue, 5 Mar 2024 18:18:29 +0300 Subject: [PATCH 1/9] Introduce account provider interface --- primitives/evm/src/account_provider.rs | 41 ++++++++++++++++++++++++++ primitives/evm/src/lib.rs | 2 ++ 2 files changed, 43 insertions(+) create mode 100644 primitives/evm/src/account_provider.rs diff --git a/primitives/evm/src/account_provider.rs b/primitives/evm/src/account_provider.rs new file mode 100644 index 0000000000..4fbd07530b --- /dev/null +++ b/primitives/evm/src/account_provider.rs @@ -0,0 +1,41 @@ +//! Custom account provider logic. + +use sp_runtime::traits::AtLeast32Bit; + +/// The account provider interface abstraction layer. +/// +/// Expose account related logic that `pallet_evm` required to control accounts existence +/// in the network and their transactions uniqueness. By default, the pallet operates native +/// system accounts records that `frame_system` provides. +/// +/// The interface allow any custom account provider logic to be used instead of +/// just using `frame_system` account provider. The accounts records should store nonce value +/// for each account at least. +pub trait AccountProvider { + /// The account identifier type. + /// + /// Represent the account itself in accounts records. + type AccountId; + /// Account index (aka nonce) type. + /// + /// The number that helps to ensure that each transaction in the network is unique + /// for particular account. + type Index: AtLeast32Bit; + + /// Creates a new account in accounts records. + /// + /// The account associated with new created address EVM. + fn create_account(who: &Self::AccountId); + /// Removes an account from accounts records. + /// + /// The account associated with removed address from EVM. + fn remove_account(who: &Self::AccountId); + /// Return current account nonce value. + /// + /// Used to represent account basic information in EVM format. + fn account_nonce(who: &Self::AccountId) -> Self::Index; + /// Increment a particular account's nonce value. + /// + /// Incremented with each new transaction submitted by the account. + fn inc_account_nonce(who: &Self::AccountId); +} diff --git a/primitives/evm/src/lib.rs b/primitives/evm/src/lib.rs index 1b436f1d05..1f5f6066aa 100644 --- a/primitives/evm/src/lib.rs +++ b/primitives/evm/src/lib.rs @@ -20,6 +20,7 @@ mod precompile; mod validation; +mod account_provider; use frame_support::weights::{constants::WEIGHT_REF_TIME_PER_MILLIS, Weight}; use scale_codec::{Decode, Encode}; @@ -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, From c5fb4ca7fe884bd257c3cfba39f117fe8c559713 Mon Sep 17 00:00:00 2001 From: Dmitry Lavrenov Date: Tue, 5 Mar 2024 18:49:12 +0300 Subject: [PATCH 2/9] Use AccountProvider logic at pallet-evm --- frame/ethereum/src/mock.rs | 1 + frame/evm/precompile/dispatch/src/lib.rs | 4 +- frame/evm/precompile/dispatch/src/mock.rs | 5 +- frame/evm/src/lib.rs | 67 ++++++++++++++++------- frame/evm/src/mock.rs | 5 +- frame/evm/src/runner/stack.rs | 6 +- precompiles/src/precompile_set.rs | 2 +- precompiles/tests-external/lib.rs | 1 + primitives/evm/src/account_provider.rs | 6 +- template/runtime/src/lib.rs | 1 + 10 files changed, 66 insertions(+), 32 deletions(-) diff --git a/frame/ethereum/src/mock.rs b/frame/ethereum/src/mock.rs index 814d5e9551..70f4a20fe7 100644 --- a/frame/ethereum/src/mock.rs +++ b/frame/ethereum/src/mock.rs @@ -158,6 +158,7 @@ parameter_types! { } impl pallet_evm::Config for Test { + type AccountProvider = pallet_evm::NativeSystemAccountProvider; type FeeCalculator = FixedGasPrice; type GasWeightMapping = pallet_evm::FixedGasWeightMapping; type WeightPerGas = WeightPerGas; diff --git a/frame/evm/precompile/dispatch/src/lib.rs b/frame/evm/precompile/dispatch/src/lib.rs index 16fadf7e91..3393064e65 100644 --- a/frame/evm/precompile/dispatch/src/lib.rs +++ b/frame/evm/precompile/dispatch/src/lib.rs @@ -54,8 +54,8 @@ impl Precompile for Dispatch + GetDispatchInfo + Decode, - ::RuntimeOrigin: From>, - DispatchValidator: DispatchValidateT, + ::RuntimeOrigin: From>>, + DispatchValidator: DispatchValidateT, T::RuntimeCall>, DecodeLimit: Get, { fn execute(handle: &mut impl PrecompileHandle) -> PrecompileResult { diff --git a/frame/evm/precompile/dispatch/src/mock.rs b/frame/evm/precompile/dispatch/src/mock.rs index 01a66d779d..26917201e9 100644 --- a/frame/evm/precompile/dispatch/src/mock.rs +++ b/frame/evm/precompile/dispatch/src/mock.rs @@ -134,14 +134,15 @@ parameter_types! { pub SuicideQuickClearLimit: u32 = 0; } impl pallet_evm::Config for Test { + type AccountProvider = pallet_evm::NativeSystemAccountProvider; type FeeCalculator = FixedGasPrice; type GasWeightMapping = pallet_evm::FixedGasWeightMapping; type WeightPerGas = WeightPerGas; type BlockHashMapping = pallet_evm::SubstrateBlockHashMapping; - type CallOrigin = EnsureAddressRoot; + type CallOrigin = EnsureAddressRoot>; - type WithdrawOrigin = EnsureAddressNever; + type WithdrawOrigin = EnsureAddressNever>; type AddressMapping = IdentityAddressMapping; type Currency = Balances; diff --git a/frame/evm/src/lib.rs b/frame/evm/src/lib.rs index 59945a4fd1..bada09f768 100644 --- a/frame/evm/src/lib.rs +++ b/frame/evm/src/lib.rs @@ -98,7 +98,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, IsPrecompileResult, LinearCostPrecompile, Log, Precompile, PrecompileFailure, PrecompileHandle, PrecompileOutput, PrecompileResult, PrecompileSet, TransactionValidationError, Vicinity, }; @@ -121,6 +121,9 @@ pub mod pallet { #[pallet::config] pub trait Config: frame_system::Config { + /// Account info provider. + type AccountProvider: AccountProvider; + /// Calculator for current gas price. type FeeCalculator: FeeCalculator; @@ -136,12 +139,12 @@ pub mod pallet { /// Allow the origin to call on behalf of given address. type CallOrigin: EnsureAddressOrigin; /// Allow the origin to withdraw on behalf of given address. - type WithdrawOrigin: EnsureAddressOrigin; + type WithdrawOrigin: EnsureAddressOrigin>; /// Mapping from address to account id. - type AddressMapping: AddressMapping; + type AddressMapping: AddressMapping>; /// Currency type for withdraw and balance storage. - type Currency: Currency + Inspect; + type Currency: Currency> + Inspect>; /// The overarching event type. type RuntimeEvent: From> + IsType<::RuntimeEvent>; @@ -547,7 +550,7 @@ pub mod pallet { MAX_ACCOUNT_NONCE, UniqueSaturatedInto::::unique_saturated_into(account.nonce), ) { - frame_system::Pallet::::inc_account_nonce(&account_id); + T::AccountProvider::inc_account_nonce(&account_id); } T::Currency::deposit_creating(&account_id, account.balance.unique_saturated_into()); @@ -576,13 +579,16 @@ pub mod pallet { pub type Suicided = StorageMap<_, Blake2_128Concat, H160, (), OptionQuery>; } +/// Utility alias for easy access to the [`AccountProvider::AccountId`] type from a given config. +pub type AccountIdOf = <::AccountProvider as AccountProvider>::AccountId; + /// Type alias for currency balance. pub type BalanceOf = - <::Currency as Currency<::AccountId>>::Balance; + <::Currency as Currency>>::Balance; /// Type alias for negative imbalance during fees type NegativeImbalanceOf = - ::AccountId>>::NegativeImbalance; + >>::NegativeImbalance; #[derive( Debug, @@ -812,7 +818,7 @@ impl Pallet { // In theory, we can always have pre-EIP161 contracts, so we // make sure the account nonce is at least one. let account_id = T::AddressMapping::into_account_id(*address); - frame_system::Pallet::::inc_account_nonce(&account_id); + T::AccountProvider::inc_account_nonce(&account_id); } >::remove(address); @@ -827,7 +833,7 @@ impl Pallet { >::remove(address); let account_id = T::AddressMapping::into_account_id(*address); - let _ = frame_system::Pallet::::dec_sufficients(&account_id); + T::AccountProvider::remove_account(&account_id); } KillStorageResult::SomeRemaining(_) => (), } @@ -850,7 +856,7 @@ impl Pallet { if !>::contains_key(address) { let account_id = T::AddressMapping::into_account_id(address); - let _ = frame_system::Pallet::::inc_sufficients(&account_id); + T::AccountProvider::create_account(&account_id); } // Update metadata. @@ -891,7 +897,7 @@ impl Pallet { pub fn account_basic(address: &H160) -> (Account, frame_support::weights::Weight) { let account_id = T::AddressMapping::into_account_id(*address); - let nonce = frame_system::Pallet::::account_nonce(&account_id); + let nonce = T::AccountProvider::account_nonce(&account_id); // keepalive `true` takes into account ExistentialDeposit as part of what's considered liquid balance. let balance = T::Currency::reducible_balance(&account_id, Preservation::Preserve, Fortitude::Polite); @@ -948,17 +954,17 @@ pub struct EVMCurrencyAdapter(sp_std::marker::PhantomData<(C, OU)>); impl OnChargeEVMTransaction for EVMCurrencyAdapter where T: Config, - C: Currency<::AccountId>, + C: Currency>, C::PositiveImbalance: Imbalance< - ::AccountId>>::Balance, + >>::Balance, Opposite = C::NegativeImbalance, >, C::NegativeImbalance: Imbalance< - ::AccountId>>::Balance, + >>::Balance, Opposite = C::PositiveImbalance, >, OU: OnUnbalanced>, - U256: UniqueSaturatedInto<::AccountId>>::Balance>, + U256: UniqueSaturatedInto<>>::Balance>, { // Kept type as Option to satisfy bound of Default type LiquidityInfo = Option>; @@ -1042,10 +1048,10 @@ where impl OnChargeEVMTransaction for () where T: Config, - ::AccountId>>::PositiveImbalance: - Imbalance<::AccountId>>::Balance, Opposite = ::AccountId>>::NegativeImbalance>, - ::AccountId>>::NegativeImbalance: -Imbalance<::AccountId>>::Balance, Opposite = ::AccountId>>::PositiveImbalance>, + >>::PositiveImbalance: + Imbalance<>>::Balance, Opposite = >>::NegativeImbalance>, + >>::NegativeImbalance: +Imbalance<>>::Balance, Opposite = >>::PositiveImbalance>, U256: UniqueSaturatedInto>, { @@ -1089,3 +1095,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 Nonce = T::Nonce; + + fn account_nonce(who: &Self::AccountId) -> Self::Nonce { + 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/mock.rs b/frame/evm/src/mock.rs index 1249f62620..56e0b4e69f 100644 --- a/frame/evm/src/mock.rs +++ b/frame/evm/src/mock.rs @@ -132,14 +132,15 @@ parameter_types! { pub SuicideQuickClearLimit: u32 = 0; } impl crate::Config for Test { + type AccountProvider = crate::NativeSystemAccountProvider; type FeeCalculator = FixedGasPrice; type GasWeightMapping = crate::FixedGasWeightMapping; type WeightPerGas = WeightPerGas; type BlockHashMapping = crate::SubstrateBlockHashMapping; - type CallOrigin = EnsureAddressRoot; + type CallOrigin = EnsureAddressRoot>; - type WithdrawOrigin = EnsureAddressNever; + type WithdrawOrigin = EnsureAddressNever>; type AddressMapping = IdentityAddressMapping; type Currency = Balances; diff --git a/frame/evm/src/runner/stack.rs b/frame/evm/src/runner/stack.rs index ab4be0b068..5b135ddc01 100644 --- a/frame/evm/src/runner/stack.rs +++ b/frame/evm/src/runner/stack.rs @@ -48,8 +48,8 @@ use fp_evm::{ }; use crate::{ - runner::Runner as RunnerT, AccountCodes, AccountCodesMetadata, AccountStorages, AddressMapping, - BalanceOf, BlockHashMapping, Config, Error, Event, FeeCalculator, OnChargeEVMTransaction, + runner::Runner as RunnerT, AccountCodes, AccountCodesMetadata, AccountProvider, AccountStorages, + AddressMapping, BalanceOf, BlockHashMapping, Config, Error, Event, FeeCalculator, OnChargeEVMTransaction, OnCreate, Pallet, RunnerError, }; @@ -841,7 +841,7 @@ where fn inc_nonce(&mut self, address: H160) -> Result<(), ExitError> { let account_id = T::AddressMapping::into_account_id(address); - frame_system::Pallet::::inc_account_nonce(&account_id); + T::AccountProvider::inc_account_nonce(&account_id); Ok(()) } diff --git a/precompiles/src/precompile_set.rs b/precompiles/src/precompile_set.rs index ebe2d11cfd..d3a7087423 100644 --- a/precompiles/src/precompile_set.rs +++ b/precompiles/src/precompile_set.rs @@ -1088,7 +1088,7 @@ impl PrecompileSetBuilder } /// Return the list of addresses contained in this PrecompileSet. - pub fn used_addresses() -> impl Iterator { + pub fn used_addresses() -> impl Iterator> { Self::new() .inner .used_addresses() diff --git a/precompiles/tests-external/lib.rs b/precompiles/tests-external/lib.rs index 42a7682f91..e6e7e03b2e 100644 --- a/precompiles/tests-external/lib.rs +++ b/precompiles/tests-external/lib.rs @@ -225,6 +225,7 @@ parameter_types! { } impl pallet_evm::Config for Runtime { + type AccountProvider = pallet_evm::NativeSystemAccountProvider; type FeeCalculator = (); type GasWeightMapping = pallet_evm::FixedGasWeightMapping; type WeightPerGas = WeightPerGas; diff --git a/primitives/evm/src/account_provider.rs b/primitives/evm/src/account_provider.rs index 4fbd07530b..545e763429 100644 --- a/primitives/evm/src/account_provider.rs +++ b/primitives/evm/src/account_provider.rs @@ -16,11 +16,11 @@ pub trait AccountProvider { /// /// Represent the account itself in accounts records. type AccountId; - /// Account index (aka nonce) type. + /// Account nonce type. /// /// The number that helps to ensure that each transaction in the network is unique /// for particular account. - type Index: AtLeast32Bit; + type Nonce: AtLeast32Bit; /// Creates a new account in accounts records. /// @@ -33,7 +33,7 @@ pub trait AccountProvider { /// Return current account nonce value. /// /// Used to represent account basic information in EVM format. - fn account_nonce(who: &Self::AccountId) -> Self::Index; + fn account_nonce(who: &Self::AccountId) -> Self::Nonce; /// Increment a particular account's nonce value. /// /// Incremented with each new transaction submitted by the account. diff --git a/template/runtime/src/lib.rs b/template/runtime/src/lib.rs index 9684ebf008..56b45817d1 100644 --- a/template/runtime/src/lib.rs +++ b/template/runtime/src/lib.rs @@ -330,6 +330,7 @@ parameter_types! { } impl pallet_evm::Config for Runtime { + type AccountProvider = pallet_evm::NativeSystemAccountProvider; type FeeCalculator = BaseFee; type GasWeightMapping = pallet_evm::FixedGasWeightMapping; type WeightPerGas = WeightPerGas; From 3278d0b0aa5c4c547d7025be2b680f7ca59d4d2a Mon Sep 17 00:00:00 2001 From: Dmitry Lavrenov Date: Tue, 5 Mar 2024 18:54:35 +0300 Subject: [PATCH 3/9] Remove redundant ower type usage --- frame/evm/precompile/dispatch/src/mock.rs | 4 ++-- frame/evm/src/mock.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/frame/evm/precompile/dispatch/src/mock.rs b/frame/evm/precompile/dispatch/src/mock.rs index 26917201e9..6f0cea64cc 100644 --- a/frame/evm/precompile/dispatch/src/mock.rs +++ b/frame/evm/precompile/dispatch/src/mock.rs @@ -140,9 +140,9 @@ impl pallet_evm::Config for Test { type WeightPerGas = WeightPerGas; type BlockHashMapping = pallet_evm::SubstrateBlockHashMapping; - type CallOrigin = EnsureAddressRoot>; + type CallOrigin = EnsureAddressRoot; - type WithdrawOrigin = EnsureAddressNever>; + type WithdrawOrigin = EnsureAddressNever; type AddressMapping = IdentityAddressMapping; type Currency = Balances; diff --git a/frame/evm/src/mock.rs b/frame/evm/src/mock.rs index 56e0b4e69f..e61f9b4d47 100644 --- a/frame/evm/src/mock.rs +++ b/frame/evm/src/mock.rs @@ -138,9 +138,9 @@ impl crate::Config for Test { type WeightPerGas = WeightPerGas; type BlockHashMapping = crate::SubstrateBlockHashMapping; - type CallOrigin = EnsureAddressRoot>; + type CallOrigin = EnsureAddressRoot; - type WithdrawOrigin = EnsureAddressNever>; + type WithdrawOrigin = EnsureAddressNever; type AddressMapping = IdentityAddressMapping; type Currency = Balances; From 68b23ec614fecb10cbea67601b32cd9d59622cb0 Mon Sep 17 00:00:00 2001 From: Dmitry Lavrenov Date: Tue, 5 Mar 2024 19:42:32 +0300 Subject: [PATCH 4/9] Fix fmt --- frame/evm/src/lib.rs | 47 ++++++++++++++++------------------- frame/evm/src/runner/stack.rs | 6 ++--- primitives/evm/src/lib.rs | 2 +- 3 files changed, 25 insertions(+), 30 deletions(-) diff --git a/frame/evm/src/lib.rs b/frame/evm/src/lib.rs index bada09f768..d0cd2478dc 100644 --- a/frame/evm/src/lib.rs +++ b/frame/evm/src/lib.rs @@ -98,9 +98,10 @@ use sp_std::{cmp::min, collections::btree_map::BTreeMap, vec::Vec}; use fp_account::AccountId20; use fp_evm::GenesisAccount; pub use fp_evm::{ - Account, AccountProvider, CallInfo, CreateInfo, ExecutionInfoV2 as ExecutionInfo, FeeCalculator, - IsPrecompileResult, LinearCostPrecompile, Log, Precompile, PrecompileFailure, PrecompileHandle, - PrecompileOutput, PrecompileResult, PrecompileSet, TransactionValidationError, Vicinity, + Account, AccountProvider, CallInfo, CreateInfo, ExecutionInfoV2 as ExecutionInfo, + FeeCalculator, IsPrecompileResult, LinearCostPrecompile, Log, Precompile, PrecompileFailure, + PrecompileHandle, PrecompileOutput, PrecompileResult, PrecompileSet, + TransactionValidationError, Vicinity, }; pub use self::{ @@ -583,12 +584,10 @@ pub mod pallet { pub type AccountIdOf = <::AccountProvider as AccountProvider>::AccountId; /// Type alias for currency balance. -pub type BalanceOf = - <::Currency as Currency>>::Balance; +pub type BalanceOf = <::Currency as Currency>>::Balance; /// Type alias for negative imbalance during fees -type NegativeImbalanceOf = - >>::NegativeImbalance; +type NegativeImbalanceOf = >>::NegativeImbalance; #[derive( Debug, @@ -955,14 +954,10 @@ impl OnChargeEVMTransaction for EVMCurrencyAdapter where T: Config, C: Currency>, - C::PositiveImbalance: Imbalance< - >>::Balance, - Opposite = C::NegativeImbalance, - >, - C::NegativeImbalance: Imbalance< - >>::Balance, - Opposite = C::PositiveImbalance, - >, + C::PositiveImbalance: + Imbalance<>>::Balance, Opposite = C::NegativeImbalance>, + C::NegativeImbalance: + Imbalance<>>::Balance, Opposite = C::PositiveImbalance>, OU: OnUnbalanced>, U256: UniqueSaturatedInto<>>::Balance>, { @@ -1046,22 +1041,22 @@ where /// Implementation for () does not specify what to do with imbalance impl OnChargeEVMTransaction for () - where +where T: Config, - >>::PositiveImbalance: - Imbalance<>>::Balance, Opposite = >>::NegativeImbalance>, - >>::NegativeImbalance: -Imbalance<>>::Balance, Opposite = >>::PositiveImbalance>, -U256: UniqueSaturatedInto>, - + >>::PositiveImbalance: Imbalance< + >>::Balance, + Opposite = >>::NegativeImbalance, + >, + >>::NegativeImbalance: Imbalance< + >>::Balance, + Opposite = >>::PositiveImbalance, + >, + U256: UniqueSaturatedInto>, { // Kept type as Option to satisfy bound of Default type LiquidityInfo = Option>; - fn withdraw_fee( - who: &H160, - fee: U256, - ) -> Result> { + fn withdraw_fee(who: &H160, fee: U256) -> Result> { EVMCurrencyAdapter::<::Currency, ()>::withdraw_fee(who, fee) } diff --git a/frame/evm/src/runner/stack.rs b/frame/evm/src/runner/stack.rs index 5b135ddc01..8b0a80f4ce 100644 --- a/frame/evm/src/runner/stack.rs +++ b/frame/evm/src/runner/stack.rs @@ -48,9 +48,9 @@ use fp_evm::{ }; use crate::{ - runner::Runner as RunnerT, AccountCodes, AccountCodesMetadata, AccountProvider, AccountStorages, - AddressMapping, BalanceOf, BlockHashMapping, Config, Error, Event, FeeCalculator, OnChargeEVMTransaction, - OnCreate, Pallet, RunnerError, + runner::Runner as RunnerT, AccountCodes, AccountCodesMetadata, AccountProvider, + AccountStorages, AddressMapping, BalanceOf, BlockHashMapping, Config, Error, Event, + FeeCalculator, OnChargeEVMTransaction, OnCreate, Pallet, RunnerError, }; #[cfg(feature = "forbid-evm-reentrancy")] diff --git a/primitives/evm/src/lib.rs b/primitives/evm/src/lib.rs index 1f5f6066aa..411489c076 100644 --- a/primitives/evm/src/lib.rs +++ b/primitives/evm/src/lib.rs @@ -18,9 +18,9 @@ #![cfg_attr(not(feature = "std"), no_std)] #![warn(unused_crate_dependencies)] +mod account_provider; mod precompile; mod validation; -mod account_provider; use frame_support::weights::{constants::WEIGHT_REF_TIME_PER_MILLIS, Weight}; use scale_codec::{Decode, Encode}; From 198623bb80d48c46126d1ada5a2bc1e2efb3d8d2 Mon Sep 17 00:00:00 2001 From: Dmitry Lavrenov Date: Tue, 5 Mar 2024 20:16:51 +0300 Subject: [PATCH 5/9] Fix clippy --- frame/evm/src/lib.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/frame/evm/src/lib.rs b/frame/evm/src/lib.rs index d0cd2478dc..adef13dffa 100644 --- a/frame/evm/src/lib.rs +++ b/frame/evm/src/lib.rs @@ -1099,17 +1099,17 @@ impl AccountProvider for NativeSystemAccountProvider type Nonce = T::Nonce; fn account_nonce(who: &Self::AccountId) -> Self::Nonce { - frame_system::Pallet::::account_nonce(&who) + frame_system::Pallet::::account_nonce(who) } fn inc_account_nonce(who: &Self::AccountId) { - frame_system::Pallet::::inc_account_nonce(&who) + frame_system::Pallet::::inc_account_nonce(who) } fn create_account(who: &Self::AccountId) { - let _ = frame_system::Pallet::::inc_sufficients(&who); + let _ = frame_system::Pallet::::inc_sufficients(who); } fn remove_account(who: &Self::AccountId) { - let _ = frame_system::Pallet::::dec_sufficients(&who); + let _ = frame_system::Pallet::::dec_sufficients(who); } } From 37fdfff8b6a53908e3723af021fc760eaede3b91 Mon Sep 17 00:00:00 2001 From: Dmitry Lavrenov Date: Tue, 10 Sep 2024 12:32:35 +0300 Subject: [PATCH 6/9] Fix clippy --- frame/evm/precompile/storage-cleaner/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frame/evm/precompile/storage-cleaner/src/lib.rs b/frame/evm/precompile/storage-cleaner/src/lib.rs index 67d0f5425c..1660785aa0 100644 --- a/frame/evm/precompile/storage-cleaner/src/lib.rs +++ b/frame/evm/precompile/storage-cleaner/src/lib.rs @@ -202,7 +202,7 @@ where pallet_evm::Suicided::::remove(address); let account_id = Runtime::AddressMapping::into_account_id(address); - let _ = Runtime::AccountProvider::remove_account(&account_id); + Runtime::AccountProvider::remove_account(&account_id); } } From 995a3a738cab65976aeacacd034364840a07aa5a Mon Sep 17 00:00:00 2001 From: MOZGIII Date: Tue, 10 Sep 2024 23:46:58 -0300 Subject: [PATCH 7/9] License at primitives/evm/src/account_provider.rs --- primitives/evm/src/account_provider.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/primitives/evm/src/account_provider.rs b/primitives/evm/src/account_provider.rs index 545e763429..3f4a859c87 100644 --- a/primitives/evm/src/account_provider.rs +++ b/primitives/evm/src/account_provider.rs @@ -1,3 +1,20 @@ +// This file is part of Frontier. + +// Copyright (c) Humanode Core. +// SPDX-License-Identifier: Apache-2.0 + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + //! Custom account provider logic. use sp_runtime::traits::AtLeast32Bit; From 5daaffee040689aa2162485b3d5979e85a38a043 Mon Sep 17 00:00:00 2001 From: MOZGIII Date: Tue, 10 Sep 2024 23:54:45 -0300 Subject: [PATCH 8/9] Rename NativeSystemAccountProvider to FrameSystemAccountProvider --- frame/ethereum/src/mock.rs | 2 +- frame/evm/precompile/dispatch/src/mock.rs | 2 +- frame/evm/precompile/storage-cleaner/src/mock.rs | 2 +- frame/evm/src/lib.rs | 9 ++++++--- frame/evm/src/mock.rs | 2 +- precompiles/tests-external/lib.rs | 2 +- template/runtime/src/lib.rs | 2 +- 7 files changed, 12 insertions(+), 9 deletions(-) diff --git a/frame/ethereum/src/mock.rs b/frame/ethereum/src/mock.rs index 9f006fa017..6da28a4b77 100644 --- a/frame/ethereum/src/mock.rs +++ b/frame/ethereum/src/mock.rs @@ -159,7 +159,7 @@ parameter_types! { } impl pallet_evm::Config for Test { - type AccountProvider = pallet_evm::NativeSystemAccountProvider; + type AccountProvider = pallet_evm::FrameSystemAccountProvider; type FeeCalculator = FixedGasPrice; type GasWeightMapping = pallet_evm::FixedGasWeightMapping; type WeightPerGas = WeightPerGas; diff --git a/frame/evm/precompile/dispatch/src/mock.rs b/frame/evm/precompile/dispatch/src/mock.rs index ca94ea09ff..7a921371a8 100644 --- a/frame/evm/precompile/dispatch/src/mock.rs +++ b/frame/evm/precompile/dispatch/src/mock.rs @@ -136,7 +136,7 @@ parameter_types! { pub SuicideQuickClearLimit: u32 = 0; } impl pallet_evm::Config for Test { - type AccountProvider = pallet_evm::NativeSystemAccountProvider; + type AccountProvider = pallet_evm::FrameSystemAccountProvider; type FeeCalculator = FixedGasPrice; type GasWeightMapping = pallet_evm::FixedGasWeightMapping; type WeightPerGas = WeightPerGas; diff --git a/frame/evm/precompile/storage-cleaner/src/mock.rs b/frame/evm/precompile/storage-cleaner/src/mock.rs index c82c79b928..2fe0c16a6a 100644 --- a/frame/evm/precompile/storage-cleaner/src/mock.rs +++ b/frame/evm/precompile/storage-cleaner/src/mock.rs @@ -124,7 +124,7 @@ parameter_types! { } impl pallet_evm::Config for Runtime { - type AccountProvider = pallet_evm::NativeSystemAccountProvider; + type AccountProvider = pallet_evm::FrameSystemAccountProvider; type FeeCalculator = (); type GasWeightMapping = pallet_evm::FixedGasWeightMapping; type WeightPerGas = WeightPerGas; diff --git a/frame/evm/src/lib.rs b/frame/evm/src/lib.rs index 8210cc30a1..a3332d7061 100644 --- a/frame/evm/src/lib.rs +++ b/frame/evm/src/lib.rs @@ -1175,10 +1175,12 @@ impl OnCreate for Tuple { } } -/// Native system account provider that `frame_system` provides. -pub struct NativeSystemAccountProvider(core::marker::PhantomData); +/// EVM account provider based on the [`frame_system`] accounts. +/// +/// Uses standard Substrate accounts system to hold EVM accounts. +pub struct FrameSystemAccountProvider(core::marker::PhantomData); -impl AccountProvider for NativeSystemAccountProvider { +impl AccountProvider for FrameSystemAccountProvider { type AccountId = T::AccountId; type Nonce = T::Nonce; @@ -1193,6 +1195,7 @@ impl AccountProvider for NativeSystemAccountProvider 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/mock.rs b/frame/evm/src/mock.rs index 101a829aa2..8cb158d4d0 100644 --- a/frame/evm/src/mock.rs +++ b/frame/evm/src/mock.rs @@ -134,7 +134,7 @@ parameter_types! { pub SuicideQuickClearLimit: u32 = 0; } impl crate::Config for Test { - type AccountProvider = crate::NativeSystemAccountProvider; + type AccountProvider = crate::FrameSystemAccountProvider; type FeeCalculator = FixedGasPrice; type GasWeightMapping = crate::FixedGasWeightMapping; type WeightPerGas = WeightPerGas; diff --git a/precompiles/tests-external/lib.rs b/precompiles/tests-external/lib.rs index 68889d6bef..b79fed5cb4 100644 --- a/precompiles/tests-external/lib.rs +++ b/precompiles/tests-external/lib.rs @@ -230,7 +230,7 @@ parameter_types! { } impl pallet_evm::Config for Runtime { - type AccountProvider = pallet_evm::NativeSystemAccountProvider; + type AccountProvider = pallet_evm::FrameSystemAccountProvider; type FeeCalculator = (); type GasWeightMapping = pallet_evm::FixedGasWeightMapping; type WeightPerGas = WeightPerGas; diff --git a/template/runtime/src/lib.rs b/template/runtime/src/lib.rs index cd2c774783..6954605854 100644 --- a/template/runtime/src/lib.rs +++ b/template/runtime/src/lib.rs @@ -348,7 +348,7 @@ parameter_types! { } impl pallet_evm::Config for Runtime { - type AccountProvider = pallet_evm::NativeSystemAccountProvider; + type AccountProvider = pallet_evm::FrameSystemAccountProvider; type FeeCalculator = BaseFee; type GasWeightMapping = pallet_evm::FixedGasWeightMapping; type WeightPerGas = WeightPerGas; From c546f177e6f1f468bd0e6fec183bce480fedb398 Mon Sep 17 00:00:00 2001 From: MOZGIII Date: Tue, 10 Sep 2024 23:57:27 -0300 Subject: [PATCH 9/9] Formatting corrcetions at account_provider.rs --- primitives/evm/src/account_provider.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/primitives/evm/src/account_provider.rs b/primitives/evm/src/account_provider.rs index 3f4a859c87..a9ee7c92a1 100644 --- a/primitives/evm/src/account_provider.rs +++ b/primitives/evm/src/account_provider.rs @@ -33,6 +33,7 @@ pub trait AccountProvider { /// /// Represent the account itself in accounts records. type AccountId; + /// Account nonce type. /// /// The number that helps to ensure that each transaction in the network is unique @@ -43,14 +44,17 @@ pub trait AccountProvider { /// /// The account associated with new created address EVM. fn create_account(who: &Self::AccountId); + /// Removes an account from accounts records. /// /// The account associated with removed address from EVM. fn remove_account(who: &Self::AccountId); + /// Return current account nonce value. /// /// Used to represent account basic information in EVM format. fn account_nonce(who: &Self::AccountId) -> Self::Nonce; + /// Increment a particular account's nonce value. /// /// Incremented with each new transaction submitted by the account.