Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move AccountProvider interface into fp-em #71

Merged
merged 4 commits into from
Jun 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
35 changes: 28 additions & 7 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 @@ -919,3 +916,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);
}
}

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 @@ -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,
precompile::{
Context, ExitError, ExitRevert, ExitSucceed, LinearCostPrecompile, Precompile,
PrecompileFailure, PrecompileHandle, PrecompileOutput, PrecompileResult, PrecompileSet,
Expand Down