Skip to content

Commit

Permalink
Apply formatting for pallet-evm-system (#66)
Browse files Browse the repository at this point in the history
Apply formatting for evm-system
  • Loading branch information
dmitrylavrenov authored Jun 5, 2023
1 parent ad51810 commit 0374a78
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 56 deletions.
14 changes: 7 additions & 7 deletions frame/evm-system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
#![cfg_attr(not(feature = "std"), no_std)]

use frame_support::traits::StoredMap;
use sp_runtime::{traits::One, RuntimeDebug, DispatchResult, DispatchError};
use scale_codec::{Encode, Decode, MaxEncodedLen, FullCodec};
use scale_codec::{Decode, Encode, FullCodec, MaxEncodedLen};
use scale_info::TypeInfo;
use sp_runtime::{traits::One, DispatchError, DispatchResult, RuntimeDebug};

#[cfg(test)]
mod mock;
Expand All @@ -46,7 +46,7 @@ pub struct AccountInfo<Index, AccountData> {
pub mod pallet {
use super::*;
use frame_support::pallet_prelude::*;
use sp_runtime::traits::{MaybeDisplay, AtLeast32Bit};
use sp_runtime::traits::{AtLeast32Bit, MaybeDisplay};
use sp_std::fmt::Debug;

#[pallet::pallet]
Expand Down Expand Up @@ -113,13 +113,13 @@ pub mod pallet {
KilledAccount { account: <T as Config>::AccountId },
}

#[pallet::error]
pub enum Error<T> {
#[pallet::error]
pub enum Error<T> {
/// The account already exists in case creating it.
AccountAlreadyExist,
AccountAlreadyExist,
/// The account doesn't exist in case removing it.
AccountNotExist,
}
}
}

impl<T: Config> Pallet<T> {
Expand Down
64 changes: 31 additions & 33 deletions frame/evm-system/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@

//! Test mock for unit tests.
use frame_support::{
traits::{ConstU32, ConstU64},
};
use frame_support::traits::{ConstU32, ConstU64};
use mockall::mock;
use sp_core::{H160, H256};
use sp_runtime::{
generic,
traits::{BlakeTwo256, IdentityLookup}, BuildStorage,
traits::{BlakeTwo256, IdentityLookup},
BuildStorage,
};
use sp_std::{boxed::Box, prelude::*};

Expand Down Expand Up @@ -101,42 +100,41 @@ impl pallet_evm_system::Config for Test {
/// Build test externalities from the custom genesis.
/// Using this call requires manual assertions on the genesis init logic.
pub fn new_test_ext() -> sp_io::TestExternalities {
// Build genesis.
let config = GenesisConfig {
..Default::default()
};
let storage = config.build_storage().unwrap();

// Make test externalities from the storage.
storage.into()
// Build genesis.
let config = GenesisConfig {
..Default::default()
};
let storage = config.build_storage().unwrap();

// Make test externalities from the storage.
storage.into()
}

pub fn runtime_lock() -> std::sync::MutexGuard<'static, ()> {
static MOCK_RUNTIME_MUTEX: std::sync::Mutex<()> = std::sync::Mutex::new(());

// Ignore the poisoning for the tests that panic.
// We only care about concurrency here, not about the poisoning.
match MOCK_RUNTIME_MUTEX.lock() {
Ok(guard) => guard,
Err(poisoned) => poisoned.into_inner(),
}
static MOCK_RUNTIME_MUTEX: std::sync::Mutex<()> = std::sync::Mutex::new(());

// Ignore the poisoning for the tests that panic.
// We only care about concurrency here, not about the poisoning.
match MOCK_RUNTIME_MUTEX.lock() {
Ok(guard) => guard,
Err(poisoned) => poisoned.into_inner(),
}
}

pub trait TestExternalitiesExt {
fn execute_with_ext<R, E>(&mut self, execute: E) -> R
where
E: for<'e> FnOnce(&'e ()) -> R;
fn execute_with_ext<R, E>(&mut self, execute: E) -> R
where
E: for<'e> FnOnce(&'e ()) -> R;
}

impl TestExternalitiesExt for frame_support::sp_io::TestExternalities {
fn execute_with_ext<R, E>(&mut self, execute: E) -> R
where
E: for<'e> FnOnce(&'e ()) -> R,
{
let guard = runtime_lock();
let result = self.execute_with(|| execute(&guard));
drop(guard);
result
}
fn execute_with_ext<R, E>(&mut self, execute: E) -> R
where
E: for<'e> FnOnce(&'e ()) -> R,
{
let guard = runtime_lock();
let result = self.execute_with(|| execute(&guard));
drop(guard);
result
}
}

38 changes: 22 additions & 16 deletions frame/evm-system/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use core::str::FromStr;

use frame_support::{assert_ok, assert_noop};
use frame_support::{assert_noop, assert_ok};
use mockall::predicate;
use sp_core::H160;

Expand All @@ -11,7 +11,7 @@ use crate::{mock::*, *};
/// This test verifies that creating account works in the happy path.
#[test]
fn create_account_works() {
new_test_ext().execute_with_ext(|_| {
new_test_ext().execute_with_ext(|_| {
// Prepare test data.
let account_id = H160::from_str("1000000000000000000000000000000000000001").unwrap();

Expand All @@ -26,17 +26,17 @@ fn create_account_works() {
on_new_account_ctx
.expect()
.once()
.with(
predicate::eq(account_id),
)
.with(predicate::eq(account_id))
.return_const(());

// Invoke the function under test.
assert_ok!(EvmSystem::create_account(&account_id));

// Assert state changes.
assert!(EvmSystem::account_exists(&account_id));
System::assert_has_event(RuntimeEvent::EvmSystem(Event::NewAccount { account: account_id } ));
System::assert_has_event(RuntimeEvent::EvmSystem(Event::NewAccount {
account: account_id,
}));

// Assert mock invocations.
on_new_account_ctx.checkpoint();
Expand All @@ -46,20 +46,23 @@ fn create_account_works() {
/// This test verifies that creating account fails when the account already exists.
#[test]
fn create_account_fails() {
new_test_ext().execute_with(|| {
new_test_ext().execute_with(|| {
// Prepare test data.
let account_id = H160::from_str("1000000000000000000000000000000000000001").unwrap();
<FullAccount<Test>>::insert(account_id.clone(), AccountInfo::<_, _>::default());

// Invoke the function under test.
assert_noop!(EvmSystem::create_account(&account_id), Error::<Test>::AccountAlreadyExist);
assert_noop!(
EvmSystem::create_account(&account_id),
Error::<Test>::AccountAlreadyExist
);
});
}

/// This test verifies that removing account works in the happy path.
#[test]
fn remove_account_works() {
new_test_ext().execute_with(|| {
new_test_ext().execute_with(|| {
// Prepare test data.
let account_id = H160::from_str("1000000000000000000000000000000000000001").unwrap();
<FullAccount<Test>>::insert(account_id.clone(), AccountInfo::<_, _>::default());
Expand All @@ -72,17 +75,17 @@ fn remove_account_works() {
on_killed_account_ctx
.expect()
.once()
.with(
predicate::eq(account_id),
)
.with(predicate::eq(account_id))
.return_const(());

// Invoke the function under test.
assert_ok!(EvmSystem::remove_account(&account_id));

// Assert state changes.
assert!(!EvmSystem::account_exists(&account_id));
System::assert_has_event(RuntimeEvent::EvmSystem(Event::KilledAccount { account: account_id } ));
System::assert_has_event(RuntimeEvent::EvmSystem(Event::KilledAccount {
account: account_id,
}));

// Assert mock invocations.
on_killed_account_ctx.checkpoint();
Expand All @@ -92,19 +95,22 @@ fn remove_account_works() {
/// This test verifies that removing account fails when the account doesn't exist.
#[test]
fn remove_account_fails() {
new_test_ext().execute_with(|| {
new_test_ext().execute_with(|| {
// Prepare test data.
let account_id = H160::from_str("1000000000000000000000000000000000000001").unwrap();

// Invoke the function under test.
assert_noop!(EvmSystem::remove_account(&account_id), Error::<Test>::AccountNotExist);
assert_noop!(
EvmSystem::remove_account(&account_id),
Error::<Test>::AccountNotExist
);
});
}

/// This test verifies that incrementing account nonce works in the happy path.
#[test]
fn inc_account_nonce_works() {
new_test_ext().execute_with(|| {
new_test_ext().execute_with(|| {
// Prepare test data.
let account_id = H160::from_str("1000000000000000000000000000000000000001").unwrap();

Expand Down

0 comments on commit 0374a78

Please sign in to comment.