Skip to content

Commit

Permalink
Move AccountProvider interface into fp-em (#71)
Browse files Browse the repository at this point in the history
* Move account provider interface into fp-em

* Implement AccountProvider interface for EvmSystem

* Revert formatter

* Move NativeSystemAccountProvider to pallet-evm
  • Loading branch information
dmitrylavrenov committed Oct 31, 2023
1 parent 890b0c2 commit d2138c7
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 31 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.

35 changes: 28 additions & 7 deletions frame/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,6 @@ pub mod runner;
mod tests;
pub mod weights;

mod account_provider;
pub use account_provider::{AccountProvider, NativeSystemAccountProvider};

use frame_support::{
dispatch::{DispatchResultWithPostInfo, Pays, PostDispatchInfo},
traits::{
Expand All @@ -81,7 +78,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 @@ -93,9 +90,9 @@ use fp_account::AccountId20;
#[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 @@ -945,3 +942,27 @@ impl<T> OnCreate<T> for Tuple {
)*)
}
}

/// Native system account provider that `frame_system` provides.
pub struct NativeSystemAccountProvider<T>(sp_std::marker::PhantomData<T>);

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)
}

fn inc_account_nonce(who: &Self::AccountId) {
frame_system::Pallet::<T>::inc_account_nonce(&who)
}

fn create_account(who: &Self::AccountId) {
let _ = frame_system::Pallet::<T>::inc_sufficients(&who);
}
fn remove_account(who: &Self::AccountId) {
let _ = frame_system::Pallet::<T>::dec_sufficients(&who);
}
}

4 changes: 4 additions & 0 deletions primitives/account/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ sp-core = { workspace = true }
sp-io = { workspace = true }
sp-runtime = { workspace = true }
sp-std = { workspace = true }
# Frontier
fp-evm = { workspace = true }

[dev-dependencies]

Expand All @@ -39,4 +41,6 @@ std = [
"sp-io/std",
"sp-runtime/std",
"sp-std/std",
# Frontier
"fp-evm/std",
]
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 @@ -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<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;

fn account_nonce(who: &Self::AccountId) -> Self::Index {
frame_system::Pallet::<T>::account_nonce(&who)
}

fn inc_account_nonce(who: &Self::AccountId) {
frame_system::Pallet::<T>::inc_account_nonce(&who)
}

fn create_account(who: &Self::AccountId) {
let _ = frame_system::Pallet::<T>::inc_sufficients(&who);
}
fn remove_account(who: &Self::AccountId) {
let _ = frame_system::Pallet::<T>::dec_sufficients(&who);
}
}
2 changes: 2 additions & 0 deletions primitives/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#![cfg_attr(not(feature = "std"), no_std)]
#![deny(unused_crate_dependencies)]

mod account_provider;
mod precompile;
mod validation;

Expand All @@ -35,6 +36,7 @@ pub use evm::{
};

pub use self::{
account_provider::AccountProvider,
precompile::{
Context, ExitError, ExitRevert, ExitSucceed, LinearCostPrecompile, Precompile,
PrecompileFailure, PrecompileHandle, PrecompileOutput, PrecompileResult, PrecompileSet,
Expand Down

0 comments on commit d2138c7

Please sign in to comment.