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 12, 2023
1 parent 4dff4e4 commit a23c2ab
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 30 deletions.
31 changes: 26 additions & 5 deletions frame/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand All @@ -92,15 +89,15 @@ use frame_support::{
use frame_system::RawOrigin;
use sp_core::{H160, H256, U256};
use sp_runtime::{
traits::{BadOrigin, NumberFor, Saturating, UniqueSaturatedInto, AtLeast32Bit, Zero},
traits::{BadOrigin, NumberFor, Saturating, UniqueSaturatedInto, Zero},
AccountId32, DispatchErrorWithPostInfo,
};
use sp_std::{cmp::min, collections::btree_map::BTreeMap, vec::Vec};
// Frontier
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,
};
Expand Down Expand Up @@ -1070,3 +1067,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 Nonce = T::Nonce;

fn account_nonce(who: &Self::AccountId) -> Self::Nonce {
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: 1 addition & 1 deletion primitives/account/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ std = [
"sp-io/std",
"sp-runtime/std",
"sp-runtime-interface/std",
"sp-std/std",
"sp-std/std"
]
serde = [
"dep:serde",
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 @@ -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 Nonce = <T as frame_system::Config>::Nonce;

fn account_nonce(who: &Self::AccountId) -> Self::Nonce {
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 @@ -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,
Expand Down

0 comments on commit a23c2ab

Please sign in to comment.