diff --git a/frame/evm-system/src/lib.rs b/frame/evm-system/src/lib.rs index c79c3b553a..73c7cfad37 100644 --- a/frame/evm-system/src/lib.rs +++ b/frame/evm-system/src/lib.rs @@ -8,7 +8,7 @@ use frame_support::traits::StoredMap; use scale_codec::{Decode, Encode, FullCodec, MaxEncodedLen}; use scale_info::TypeInfo; -use sp_runtime::{traits::One, DispatchError, DispatchResult, RuntimeDebug}; +use sp_runtime::{traits::One, DispatchError, RuntimeDebug}; #[cfg(test)] mod mock; @@ -115,6 +115,26 @@ pub mod pallet { } } +/// Some resultant status relevant to account creation. +#[derive(Eq, PartialEq, RuntimeDebug)] +pub enum AccountCreationStatus { + /// Account was created. + Created, + /// Account already existed. + Existed, +} + +/// Some resultant status relevant to account removal. +#[derive(Eq, PartialEq, RuntimeDebug)] +pub enum AccountRemovalStatus { + /// Account was destroyed. + Reaped, + /// Account still exists. + Exists, + /// Account doesn't exist. + NotExists, +} + impl Pallet { /// Check the account existence. pub fn account_exists(who: &::AccountId) -> bool { @@ -144,30 +164,29 @@ impl Pallet { } /// Create an account. - pub fn create_account(who: &::AccountId) -> DispatchResult { + pub fn create_account(who: &::AccountId) -> AccountCreationStatus { if Self::account_exists(who) { - return Err(Error::::AccountAlreadyExist.into()); + return AccountCreationStatus::Existed; } Account::::insert(who.clone(), AccountInfo::<_, _>::default()); Self::on_created_account(who.clone()); - Ok(()) + AccountCreationStatus::Created } /// Remove an account. - pub fn remove_account(who: &::AccountId) -> DispatchResult { + pub fn remove_account(who: &::AccountId) -> AccountRemovalStatus { if !Self::account_exists(who) { - return Err(Error::::AccountNotExist.into()); + return AccountRemovalStatus::NotExists; } if Account::::get(who).data != ::AccountData::default() { - return Err(Error::::AccountDataNotEmpty.into()); + return AccountRemovalStatus::Exists; } Account::::remove(who); Self::on_killed_account(who.clone()); - - Ok(()) + AccountRemovalStatus::Reaped } } @@ -180,13 +199,10 @@ impl StoredMap<::AccountId, ::AccountData> k: &::AccountId, f: impl FnOnce(&mut Option<::AccountData>) -> Result, ) -> Result { - let account = Account::::get(k); - let was_providing = account.data != ::AccountData::default(); - - let mut maybe_account_data = if was_providing { - Some(account.data) + let (mut maybe_account_data, was_providing) = if Self::account_exists(k) { + (Some(Account::::get(k).data), true) } else { - None + (None, false) }; let result = f(&mut maybe_account_data)?; diff --git a/frame/evm-system/src/tests.rs b/frame/evm-system/src/tests.rs index b175d5e6c0..ae755fbe0d 100644 --- a/frame/evm-system/src/tests.rs +++ b/frame/evm-system/src/tests.rs @@ -2,7 +2,7 @@ use sp_std::str::FromStr; -use frame_support::{assert_noop, assert_ok}; +use frame_support::assert_noop; use mockall::predicate; use sp_core::H160; @@ -30,7 +30,10 @@ fn create_account_works() { .return_const(()); // Invoke the function under test. - assert_ok!(EvmSystem::create_account(&account_id)); + assert_eq!( + EvmSystem::create_account(&account_id), + AccountCreationStatus::Created + ); // Assert state changes. assert!(EvmSystem::account_exists(&account_id)); @@ -52,9 +55,9 @@ fn create_account_fails() { >::insert(account_id.clone(), AccountInfo::<_, _>::default()); // Invoke the function under test. - assert_noop!( + assert_eq!( EvmSystem::create_account(&account_id), - Error::::AccountAlreadyExist + AccountCreationStatus::Existed ); }); } @@ -79,7 +82,10 @@ fn remove_account_works() { .return_const(()); // Invoke the function under test. - assert_ok!(EvmSystem::remove_account(&account_id)); + assert_eq!( + EvmSystem::remove_account(&account_id), + AccountRemovalStatus::Reaped + ); // Assert state changes. assert!(!EvmSystem::account_exists(&account_id)); @@ -100,9 +106,9 @@ fn remove_account_fails() { let account_id = H160::from_str("1000000000000000000000000000000000000001").unwrap(); // Invoke the function under test. - assert_noop!( + assert_eq!( EvmSystem::remove_account(&account_id), - Error::::AccountNotExist + AccountRemovalStatus::NotExists ); }); } @@ -205,9 +211,7 @@ fn try_mutate_exists_account_removed() { new_test_ext().execute_with_ext(|_| { // Prepare test data. let account_id = H160::from_str("1000000000000000000000000000000000000001").unwrap(); - let nonce = 1; - let data = 1; - >::insert(account_id.clone(), AccountInfo { nonce, data }); + >::insert(account_id.clone(), AccountInfo { nonce: 1, data: 0 }); // Check test preconditions. assert!(EvmSystem::account_exists(&account_id));