Skip to content

Commit

Permalink
Move account provider interface into fp-em
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitrylavrenov committed Jun 16, 2023
1 parent c2efe7c commit 9c1e007
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 26 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion frame/ethereum/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ impl AddressMapping<AccountId32> for HashedAddressMapping {
}

impl pallet_evm::Config for Test {
type AccountProvider = pallet_evm::NativeSystemAccountProvider<Self>;
type AccountProvider = fp_evm::NativeSystemAccountProvider<Self>;
type FeeCalculator = FixedGasPrice;
type GasWeightMapping = pallet_evm::FixedGasWeightMapping<Self>;
type WeightPerGas = WeightPerGas;
Expand Down
8 changes: 5 additions & 3 deletions frame/evm/precompile/dispatch/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self>;
type AccountProvider = fp_evm::NativeSystemAccountProvider<Self>;
type FeeCalculator = FixedGasPrice;
type GasWeightMapping = pallet_evm::FixedGasWeightMapping<Self>;
type WeightPerGas = WeightPerGas;

type BlockHashMapping = pallet_evm::SubstrateBlockHashMapping<Self>;
type CallOrigin = EnsureAddressRoot<<Self::AccountProvider as pallet_evm::AccountProvider>::AccountId>;
type CallOrigin =
EnsureAddressRoot<<Self::AccountProvider as pallet_evm::AccountProvider>::AccountId>;

type WithdrawOrigin = EnsureAddressNever<<Self::AccountProvider as pallet_evm::AccountProvider>::AccountId>;
type WithdrawOrigin =
EnsureAddressNever<<Self::AccountProvider as pallet_evm::AccountProvider>::AccountId>;
type AddressMapping = IdentityAddressMapping;
type Currency = Balances;

Expand Down
33 changes: 19 additions & 14 deletions frame/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -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};
Expand All @@ -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::{
Expand Down Expand Up @@ -131,12 +128,16 @@ pub mod pallet {
/// Allow the origin to call on behalf of given address.
type CallOrigin: EnsureAddressOrigin<Self::RuntimeOrigin>;
/// Allow the origin to withdraw on behalf of given address.
type WithdrawOrigin: EnsureAddressOrigin<Self::RuntimeOrigin, Success = <Self::AccountProvider as AccountProvider>::AccountId>;
type WithdrawOrigin: EnsureAddressOrigin<
Self::RuntimeOrigin,
Success = <Self::AccountProvider as AccountProvider>::AccountId,
>;

/// Mapping from address to account id.
type AddressMapping: AddressMapping<<Self::AccountProvider as AccountProvider>::AccountId>;
/// Currency type for withdraw and balance storage.
type Currency: Currency<<Self::AccountProvider as AccountProvider>::AccountId> + Inspect<<Self::AccountProvider as AccountProvider>::AccountId>;
type Currency: Currency<<Self::AccountProvider as AccountProvider>::AccountId>
+ Inspect<<Self::AccountProvider as AccountProvider>::AccountId>;

/// The overarching event type.
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
Expand Down Expand Up @@ -517,12 +518,14 @@ pub mod pallet {
}

/// Type alias for currency balance.
pub type BalanceOf<T> =
<<T as Config>::Currency as Currency<<<T as Config>::AccountProvider as AccountProvider>::AccountId>>::Balance;
pub type BalanceOf<T> = <<T as Config>::Currency as Currency<
<<T as Config>::AccountProvider as AccountProvider>::AccountId,
>>::Balance;

/// Type alias for negative imbalance during fees
type NegativeImbalanceOf<C, T> =
<C as Currency<<<T as Config>::AccountProvider as AccountProvider>::AccountId>>::NegativeImbalance;
type NegativeImbalanceOf<C, T> = <C as Currency<
<<T as Config>::AccountProvider as AccountProvider>::AccountId,
>>::NegativeImbalance;

pub trait EnsureAddressOrigin<OuterOrigin> {
/// Success return type.
Expand Down Expand Up @@ -788,7 +791,9 @@ where
Opposite = C::PositiveImbalance,
>,
OU: OnUnbalanced<NegativeImbalanceOf<C, T>>,
U256: UniqueSaturatedInto<<C as Currency<<<T as Config>::AccountProvider as AccountProvider>::AccountId>>::Balance>,
U256: UniqueSaturatedInto<
<C as Currency<<<T as Config>::AccountProvider as AccountProvider>::AccountId>>::Balance,
>,
{
// Kept type as Option to satisfy bound of Default
type LiquidityInfo = Option<NegativeImbalanceOf<C, T>>;
Expand Down
8 changes: 5 additions & 3 deletions frame/evm/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,17 @@ parameter_types! {
pub MockPrecompiles: MockPrecompileSet = MockPrecompileSet;
}
impl crate::Config for Test {
type AccountProvider = crate::NativeSystemAccountProvider<Self>;
type AccountProvider = fp_evm::NativeSystemAccountProvider<Self>;
type FeeCalculator = FixedGasPrice;
type GasWeightMapping = crate::FixedGasWeightMapping<Self>;
type WeightPerGas = WeightPerGas;

type BlockHashMapping = crate::SubstrateBlockHashMapping<Self>;
type CallOrigin = EnsureAddressRoot<<Self::AccountProvider as crate::AccountProvider>::AccountId>;
type CallOrigin =
EnsureAddressRoot<<Self::AccountProvider as crate::AccountProvider>::AccountId>;

type WithdrawOrigin = EnsureAddressNever<<Self::AccountProvider as crate::AccountProvider>::AccountId>;
type WithdrawOrigin =
EnsureAddressNever<<Self::AccountProvider as crate::AccountProvider>::AccountId>;
type AddressMapping = IdentityAddressMapping;
type Currency = Balances;

Expand Down
2 changes: 2 additions & 0 deletions primitives/evm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -29,6 +30,7 @@ std = [
"scale-codec/std",
# Substrate
"frame-support/std",
"frame-system/std",
"sp-core/std",
"sp-runtime/std",
"sp-std/std",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Custom account provider logic.
use super::*;
use sp_runtime::traits::AtLeast32Bit;

/// The account provider interface abstraction layer.
///
Expand Down Expand Up @@ -43,9 +43,9 @@ pub trait AccountProvider {
/// Native system account provider that `frame_system` provides.
pub struct NativeSystemAccountProvider<T>(sp_std::marker::PhantomData<T>);

impl<T: Config> AccountProvider for NativeSystemAccountProvider<T> {
type AccountId = <T as frame_system::Config>::AccountId;
type Index = <T as frame_system::Config>::Index;
impl<T: frame_system::Config> AccountProvider for NativeSystemAccountProvider<T> {
type AccountId = T::AccountId;
type Index = T::Index;

fn account_nonce(who: &Self::AccountId) -> Self::Index {
frame_system::Pallet::<T>::account_nonce(&who)
Expand Down
2 changes: 2 additions & 0 deletions primitives/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#![cfg_attr(not(feature = "std"), no_std)]

mod account_provider;
mod precompile;
mod validation;

Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion template/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ parameter_types! {
}

impl pallet_evm::Config for Runtime {
type AccountProvider = pallet_evm::NativeSystemAccountProvider<Self>;
type AccountProvider = fp_evm::NativeSystemAccountProvider<Self>;
type FeeCalculator = BaseFee;
type GasWeightMapping = pallet_evm::FixedGasWeightMapping<Self>;
type WeightPerGas = WeightPerGas;
Expand Down

0 comments on commit 9c1e007

Please sign in to comment.