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 3f3e919 commit da85216
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 29 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.

4 changes: 4 additions & 0 deletions frame/evm-system/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -36,6 +38,8 @@ std = [
"frame-system/std",
"sp-runtime/std",
"sp-std/std",
# Frontier
"fp-evm/std",
]
try-runtime = [
"frame-support/try-runtime",
Expand Down
21 changes: 21 additions & 0 deletions frame/evm-system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,27 @@ impl<T: Config> StoredMap<<T as Config>::AccountId, <T as Config>::AccountData>
}
}

impl<T: Config> fp_evm::AccountProvider for Pallet<T> {
type AccountId = <T as Config>::AccountId;
type Index = <T as Config>::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<AccountId> {
/// A new account `who` has been registered.
Expand Down
30 changes: 25 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 @@ -90,15 +87,15 @@ use frame_support::{
use frame_system::RawOrigin;
use sp_core::{Decode, Encode, 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, 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,
InvalidEvmTransactionError, IsPrecompileResult, LinearCostPrecompile, Log, Precompile,
PrecompileFailure, PrecompileHandle, PrecompileOutput, PrecompileResult, PrecompileSet,
Vicinity,
Expand Down Expand Up @@ -1053,3 +1050,26 @@ 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);
}
}
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 @@ -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 da85216

Please sign in to comment.