From 9c1e00780a411b2afacc4f5be4fdc9895b61f285 Mon Sep 17 00:00:00 2001 From: Dmitry Lavrenov Date: Fri, 16 Jun 2023 14:20:33 +0300 Subject: [PATCH 1/4] Move account provider interface into fp-em --- Cargo.lock | 1 + frame/ethereum/src/mock.rs | 2 +- frame/evm/precompile/dispatch/src/mock.rs | 8 +++-- frame/evm/src/lib.rs | 33 +++++++++++-------- frame/evm/src/mock.rs | 8 +++-- primitives/evm/Cargo.toml | 2 ++ .../evm/src/account_provider.rs | 8 ++--- primitives/evm/src/lib.rs | 2 ++ template/runtime/src/lib.rs | 2 +- 9 files changed, 40 insertions(+), 26 deletions(-) rename {frame => primitives}/evm/src/account_provider.rs (91%) diff --git a/Cargo.lock b/Cargo.lock index 87cb0af0b2..94cb19fa07 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2171,6 +2171,7 @@ version = "3.0.0-dev" dependencies = [ "evm", "frame-support", + "frame-system", "parity-scale-codec", "serde", "sp-core", diff --git a/frame/ethereum/src/mock.rs b/frame/ethereum/src/mock.rs index 9614fb8a0c..9b313f2799 100644 --- a/frame/ethereum/src/mock.rs +++ b/frame/ethereum/src/mock.rs @@ -152,7 +152,7 @@ impl AddressMapping for HashedAddressMapping { } impl pallet_evm::Config for Test { - type AccountProvider = pallet_evm::NativeSystemAccountProvider; + type AccountProvider = fp_evm::NativeSystemAccountProvider; 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 7163d8ea2d..2c6c0346a7 100644 --- a/frame/evm/precompile/dispatch/src/mock.rs +++ b/frame/evm/precompile/dispatch/src/mock.rs @@ -139,15 +139,17 @@ parameter_types! { pub WeightPerGas: Weight = Weight::from_ref_time(20_000); } impl pallet_evm::Config for Test { - type AccountProvider = pallet_evm::NativeSystemAccountProvider; + type AccountProvider = fp_evm::NativeSystemAccountProvider; type FeeCalculator = FixedGasPrice; type GasWeightMapping = pallet_evm::FixedGasWeightMapping; type WeightPerGas = WeightPerGas; type BlockHashMapping = pallet_evm::SubstrateBlockHashMapping; - type CallOrigin = EnsureAddressRoot<::AccountId>; + type CallOrigin = + EnsureAddressRoot<::AccountId>; - type WithdrawOrigin = EnsureAddressNever<::AccountId>; + type WithdrawOrigin = + EnsureAddressNever<::AccountId>; type AddressMapping = IdentityAddressMapping; type Currency = Balances; diff --git a/frame/evm/src/lib.rs b/frame/evm/src/lib.rs index 76bfb498a2..56dc414a60 100644 --- a/frame/evm/src/lib.rs +++ b/frame/evm/src/lib.rs @@ -64,9 +64,6 @@ pub mod runner; #[cfg(test)] mod tests; -mod account_provider; -pub use account_provider::{AccountProvider, NativeSystemAccountProvider}; - use frame_support::{ dispatch::{DispatchResultWithPostInfo, Pays, PostDispatchInfo}, traits::{ @@ -79,7 +76,7 @@ use frame_system::RawOrigin; use impl_trait_for_tuples::impl_for_tuples; use sp_core::{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, vec::Vec}; @@ -90,9 +87,9 @@ pub use evm::{ #[cfg(feature = "std")] use fp_evm::GenesisAccount; pub use fp_evm::{ - Account, CallInfo, CreateInfo, ExecutionInfo, FeeCalculator, InvalidEvmTransactionError, - LinearCostPrecompile, Log, Precompile, PrecompileFailure, PrecompileHandle, PrecompileOutput, - PrecompileResult, PrecompileSet, Vicinity, + Account, AccountProvider, CallInfo, CreateInfo, ExecutionInfo, FeeCalculator, + InvalidEvmTransactionError, LinearCostPrecompile, Log, Precompile, PrecompileFailure, + PrecompileHandle, PrecompileOutput, PrecompileResult, PrecompileSet, Vicinity, }; pub use self::{ @@ -131,12 +128,16 @@ 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::AccountId>; + type WithdrawOrigin: EnsureAddressOrigin< + Self::RuntimeOrigin, + Success = ::AccountId, + >; /// Mapping from address to account id. type AddressMapping: AddressMapping<::AccountId>; /// Currency type for withdraw and balance storage. - type Currency: Currency<::AccountId> + Inspect<::AccountId>; + type Currency: Currency<::AccountId> + + Inspect<::AccountId>; /// The overarching event type. type RuntimeEvent: From> + IsType<::RuntimeEvent>; @@ -517,12 +518,14 @@ pub mod pallet { } /// Type alias for currency balance. -pub type BalanceOf = - <::Currency as Currency<<::AccountProvider as AccountProvider>::AccountId>>::Balance; +pub type BalanceOf = <::Currency as Currency< + <::AccountProvider as AccountProvider>::AccountId, +>>::Balance; /// Type alias for negative imbalance during fees -type NegativeImbalanceOf = - ::AccountProvider as AccountProvider>::AccountId>>::NegativeImbalance; +type NegativeImbalanceOf = ::AccountProvider as AccountProvider>::AccountId, +>>::NegativeImbalance; pub trait EnsureAddressOrigin { /// Success return type. @@ -788,7 +791,9 @@ where Opposite = C::PositiveImbalance, >, OU: OnUnbalanced>, - U256: UniqueSaturatedInto<::AccountProvider as AccountProvider>::AccountId>>::Balance>, + U256: UniqueSaturatedInto< + ::AccountProvider as AccountProvider>::AccountId>>::Balance, + >, { // Kept type as Option to satisfy bound of Default type LiquidityInfo = Option>; diff --git a/frame/evm/src/mock.rs b/frame/evm/src/mock.rs index ad886616db..e6e5d82264 100644 --- a/frame/evm/src/mock.rs +++ b/frame/evm/src/mock.rs @@ -132,15 +132,17 @@ parameter_types! { pub MockPrecompiles: MockPrecompileSet = MockPrecompileSet; } impl crate::Config for Test { - type AccountProvider = crate::NativeSystemAccountProvider; + type AccountProvider = fp_evm::NativeSystemAccountProvider; type FeeCalculator = FixedGasPrice; type GasWeightMapping = crate::FixedGasWeightMapping; type WeightPerGas = WeightPerGas; type BlockHashMapping = crate::SubstrateBlockHashMapping; - type CallOrigin = EnsureAddressRoot<::AccountId>; + type CallOrigin = + EnsureAddressRoot<::AccountId>; - type WithdrawOrigin = EnsureAddressNever<::AccountId>; + type WithdrawOrigin = + EnsureAddressNever<::AccountId>; type AddressMapping = IdentityAddressMapping; type Currency = Balances; diff --git a/primitives/evm/Cargo.toml b/primitives/evm/Cargo.toml index 02e1e43e0e..ddb4e3767d 100644 --- a/primitives/evm/Cargo.toml +++ b/primitives/evm/Cargo.toml @@ -16,6 +16,7 @@ scale-codec = { package = "parity-scale-codec", workspace = true } serde = { workspace = true, optional = true } # Substrate frame-support = { workspace = true } +frame-system = { workspace = true } sp-core = { workspace = true } sp-runtime = { workspace = true } sp-std = { workspace = true } @@ -29,6 +30,7 @@ std = [ "scale-codec/std", # Substrate "frame-support/std", + "frame-system/std", "sp-core/std", "sp-runtime/std", "sp-std/std", diff --git a/frame/evm/src/account_provider.rs b/primitives/evm/src/account_provider.rs similarity index 91% rename from frame/evm/src/account_provider.rs rename to primitives/evm/src/account_provider.rs index 69484222cf..7be007f32c 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. /// @@ -43,9 +43,9 @@ pub trait AccountProvider { /// 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; +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) diff --git a/primitives/evm/src/lib.rs b/primitives/evm/src/lib.rs index 43e0929cda..4b241b1da0 100644 --- a/primitives/evm/src/lib.rs +++ b/primitives/evm/src/lib.rs @@ -17,6 +17,7 @@ #![cfg_attr(not(feature = "std"), no_std)] +mod account_provider; mod precompile; mod validation; @@ -34,6 +35,7 @@ pub use evm::{ }; pub use self::{ + account_provider::{AccountProvider, NativeSystemAccountProvider}, precompile::{ Context, ExitError, ExitRevert, ExitSucceed, LinearCostPrecompile, Precompile, PrecompileFailure, PrecompileHandle, PrecompileOutput, PrecompileResult, PrecompileSet, diff --git a/template/runtime/src/lib.rs b/template/runtime/src/lib.rs index 2316298271..29a0f2d509 100644 --- a/template/runtime/src/lib.rs +++ b/template/runtime/src/lib.rs @@ -326,7 +326,7 @@ parameter_types! { } impl pallet_evm::Config for Runtime { - type AccountProvider = pallet_evm::NativeSystemAccountProvider; + type AccountProvider = fp_evm::NativeSystemAccountProvider; type FeeCalculator = BaseFee; type GasWeightMapping = pallet_evm::FixedGasWeightMapping; type WeightPerGas = WeightPerGas; From 39109b1287f20d60d41d55755a1b3da2d92a787a Mon Sep 17 00:00:00 2001 From: Dmitry Lavrenov Date: Fri, 16 Jun 2023 16:29:10 +0300 Subject: [PATCH 2/4] Implement AccountProvider interface for EvmSystem --- Cargo.lock | 1 + frame/evm-system/Cargo.toml | 4 ++++ frame/evm-system/src/lib.rs | 21 +++++++++++++++++++++ 3 files changed, 26 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 94cb19fa07..c40a43263d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4934,6 +4934,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. From 708652856a04170be517bb8304d7e2f5f716fb5e Mon Sep 17 00:00:00 2001 From: Dmitry Lavrenov Date: Sun, 18 Jun 2023 11:12:26 +0300 Subject: [PATCH 3/4] Revert formatter --- frame/evm/precompile/dispatch/src/mock.rs | 6 ++---- frame/evm/src/lib.rs | 22 +++++++--------------- frame/evm/src/mock.rs | 6 ++---- 3 files changed, 11 insertions(+), 23 deletions(-) diff --git a/frame/evm/precompile/dispatch/src/mock.rs b/frame/evm/precompile/dispatch/src/mock.rs index 2c6c0346a7..d5202e906c 100644 --- a/frame/evm/precompile/dispatch/src/mock.rs +++ b/frame/evm/precompile/dispatch/src/mock.rs @@ -145,11 +145,9 @@ impl pallet_evm::Config for Test { type WeightPerGas = WeightPerGas; type BlockHashMapping = pallet_evm::SubstrateBlockHashMapping; - type CallOrigin = - EnsureAddressRoot<::AccountId>; + type CallOrigin = EnsureAddressRoot<::AccountId>; - type WithdrawOrigin = - EnsureAddressNever<::AccountId>; + type WithdrawOrigin = EnsureAddressNever<::AccountId>; type AddressMapping = IdentityAddressMapping; type Currency = Balances; diff --git a/frame/evm/src/lib.rs b/frame/evm/src/lib.rs index 56dc414a60..f1d95fcda3 100644 --- a/frame/evm/src/lib.rs +++ b/frame/evm/src/lib.rs @@ -128,16 +128,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< - Self::RuntimeOrigin, - Success = ::AccountId, - >; + type WithdrawOrigin: EnsureAddressOrigin::AccountId>; /// Mapping from address to account id. type AddressMapping: AddressMapping<::AccountId>; /// Currency type for withdraw and balance storage. - type Currency: Currency<::AccountId> - + Inspect<::AccountId>; + type Currency: Currency<::AccountId> + Inspect<::AccountId>; /// The overarching event type. type RuntimeEvent: From> + IsType<::RuntimeEvent>; @@ -518,14 +514,12 @@ pub mod pallet { } /// Type alias for currency balance. -pub type BalanceOf = <::Currency as Currency< - <::AccountProvider as AccountProvider>::AccountId, ->>::Balance; +pub type BalanceOf = + <::Currency as Currency<<::AccountProvider as AccountProvider>::AccountId>>::Balance; /// Type alias for negative imbalance during fees -type NegativeImbalanceOf = ::AccountProvider as AccountProvider>::AccountId, ->>::NegativeImbalance; +type NegativeImbalanceOf = + ::AccountProvider as AccountProvider>::AccountId>>::NegativeImbalance; pub trait EnsureAddressOrigin { /// Success return type. @@ -791,9 +785,7 @@ where Opposite = C::PositiveImbalance, >, OU: OnUnbalanced>, - U256: UniqueSaturatedInto< - ::AccountProvider as AccountProvider>::AccountId>>::Balance, - >, + U256: UniqueSaturatedInto<::AccountProvider as AccountProvider>::AccountId>>::Balance>, { // Kept type as Option to satisfy bound of Default type LiquidityInfo = Option>; diff --git a/frame/evm/src/mock.rs b/frame/evm/src/mock.rs index e6e5d82264..31261c5d77 100644 --- a/frame/evm/src/mock.rs +++ b/frame/evm/src/mock.rs @@ -138,11 +138,9 @@ impl crate::Config for Test { type WeightPerGas = WeightPerGas; type BlockHashMapping = crate::SubstrateBlockHashMapping; - type CallOrigin = - EnsureAddressRoot<::AccountId>; + type CallOrigin = EnsureAddressRoot<::AccountId>; - type WithdrawOrigin = - EnsureAddressNever<::AccountId>; + type WithdrawOrigin = EnsureAddressNever<::AccountId>; type AddressMapping = IdentityAddressMapping; type Currency = Balances; From db3c4afa9d31d41be46dc03825912df67d11aad5 Mon Sep 17 00:00:00 2001 From: Dmitry Lavrenov Date: Sun, 18 Jun 2023 11:21:56 +0300 Subject: [PATCH 4/4] Move NativeSystemAccountProvider to pallet-evm --- Cargo.lock | 1 - frame/ethereum/src/mock.rs | 2 +- frame/evm/precompile/dispatch/src/mock.rs | 2 +- frame/evm/src/lib.rs | 24 +++++++++++++++++++++++ frame/evm/src/mock.rs | 2 +- primitives/evm/Cargo.toml | 2 -- primitives/evm/src/account_provider.rs | 23 ---------------------- primitives/evm/src/lib.rs | 2 +- template/runtime/src/lib.rs | 2 +- 9 files changed, 29 insertions(+), 31 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c40a43263d..0de6bf9e5d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2171,7 +2171,6 @@ version = "3.0.0-dev" dependencies = [ "evm", "frame-support", - "frame-system", "parity-scale-codec", "serde", "sp-core", diff --git a/frame/ethereum/src/mock.rs b/frame/ethereum/src/mock.rs index 9b313f2799..9614fb8a0c 100644 --- a/frame/ethereum/src/mock.rs +++ b/frame/ethereum/src/mock.rs @@ -152,7 +152,7 @@ impl AddressMapping for HashedAddressMapping { } impl pallet_evm::Config for Test { - type AccountProvider = fp_evm::NativeSystemAccountProvider; + type AccountProvider = pallet_evm::NativeSystemAccountProvider; 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 d5202e906c..7163d8ea2d 100644 --- a/frame/evm/precompile/dispatch/src/mock.rs +++ b/frame/evm/precompile/dispatch/src/mock.rs @@ -139,7 +139,7 @@ parameter_types! { pub WeightPerGas: Weight = Weight::from_ref_time(20_000); } impl pallet_evm::Config for Test { - type AccountProvider = fp_evm::NativeSystemAccountProvider; + type AccountProvider = pallet_evm::NativeSystemAccountProvider; type FeeCalculator = FixedGasPrice; type GasWeightMapping = pallet_evm::FixedGasWeightMapping; type WeightPerGas = WeightPerGas; diff --git a/frame/evm/src/lib.rs b/frame/evm/src/lib.rs index f1d95fcda3..19b2cea2d2 100644 --- a/frame/evm/src/lib.rs +++ b/frame/evm/src/lib.rs @@ -916,3 +916,27 @@ 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/mock.rs b/frame/evm/src/mock.rs index 31261c5d77..ad886616db 100644 --- a/frame/evm/src/mock.rs +++ b/frame/evm/src/mock.rs @@ -132,7 +132,7 @@ parameter_types! { pub MockPrecompiles: MockPrecompileSet = MockPrecompileSet; } impl crate::Config for Test { - type AccountProvider = fp_evm::NativeSystemAccountProvider; + type AccountProvider = crate::NativeSystemAccountProvider; type FeeCalculator = FixedGasPrice; type GasWeightMapping = crate::FixedGasWeightMapping; type WeightPerGas = WeightPerGas; diff --git a/primitives/evm/Cargo.toml b/primitives/evm/Cargo.toml index ddb4e3767d..02e1e43e0e 100644 --- a/primitives/evm/Cargo.toml +++ b/primitives/evm/Cargo.toml @@ -16,7 +16,6 @@ scale-codec = { package = "parity-scale-codec", workspace = true } serde = { workspace = true, optional = true } # Substrate frame-support = { workspace = true } -frame-system = { workspace = true } sp-core = { workspace = true } sp-runtime = { workspace = true } sp-std = { workspace = true } @@ -30,7 +29,6 @@ std = [ "scale-codec/std", # Substrate "frame-support/std", - "frame-system/std", "sp-core/std", "sp-runtime/std", "sp-std/std", diff --git a/primitives/evm/src/account_provider.rs b/primitives/evm/src/account_provider.rs index 7be007f32c..4fbd07530b 100644 --- a/primitives/evm/src/account_provider.rs +++ b/primitives/evm/src/account_provider.rs @@ -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 = 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/primitives/evm/src/lib.rs b/primitives/evm/src/lib.rs index 4b241b1da0..60b320d1fc 100644 --- a/primitives/evm/src/lib.rs +++ b/primitives/evm/src/lib.rs @@ -35,7 +35,7 @@ pub use evm::{ }; pub use self::{ - account_provider::{AccountProvider, NativeSystemAccountProvider}, + account_provider::AccountProvider, precompile::{ Context, ExitError, ExitRevert, ExitSucceed, LinearCostPrecompile, Precompile, PrecompileFailure, PrecompileHandle, PrecompileOutput, PrecompileResult, PrecompileSet, diff --git a/template/runtime/src/lib.rs b/template/runtime/src/lib.rs index 29a0f2d509..2316298271 100644 --- a/template/runtime/src/lib.rs +++ b/template/runtime/src/lib.rs @@ -326,7 +326,7 @@ parameter_types! { } impl pallet_evm::Config for Runtime { - type AccountProvider = fp_evm::NativeSystemAccountProvider; + type AccountProvider = pallet_evm::NativeSystemAccountProvider; type FeeCalculator = BaseFee; type GasWeightMapping = pallet_evm::FixedGasWeightMapping; type WeightPerGas = WeightPerGas;