From dbe91ade6ab7b8606ee3a6d4ced9ca084495b71a Mon Sep 17 00:00:00 2001 From: Dmitry Lavrenov Date: Thu, 28 Mar 2024 19:05:47 +0300 Subject: [PATCH] --wip-- --- frame/evm-balances/src/lib.rs | 11 ++++++++ frame/evm-system/src/lib.rs | 42 +++++++++++++++++++++---------- frame/evm-system/src/tests.rs | 47 ++++++++++++++++++++--------------- rust-toolchain.toml | 5 ++++ 4 files changed, 72 insertions(+), 33 deletions(-) create mode 100644 rust-toolchain.toml diff --git a/frame/evm-balances/src/lib.rs b/frame/evm-balances/src/lib.rs index 7b77b4871d..07b10b5433 100644 --- a/frame/evm-balances/src/lib.rs +++ b/frame/evm-balances/src/lib.rs @@ -229,7 +229,13 @@ impl, I: 'static> Pallet { let result = T::AccountStore::try_mutate_exists(who, |maybe_account| { let is_new = maybe_account.is_none(); let mut account = maybe_account.take().unwrap_or_default(); + println!("\nBefore F"); + println!("Account {:?}", who); + println!("Data {:?}", account); f(&mut account, is_new).map(move |result| { + println!("After F"); + println!("Account {:?}", who); + println!("Data {:?}\n", account); let maybe_endowed = if is_new { Some(account.free) } else { None }; let maybe_account_maybe_dust = Self::post_mutation(who, account); *maybe_account = maybe_account_maybe_dust.0; @@ -434,11 +440,16 @@ where Self::try_mutate_account_with_dust( transactor, |from_account, _| -> DispatchResult { + println!("To acccount {:?} {:?}", dest, to_account); + println!("From acccount {:?} {:?}", transactor, from_account); from_account.free = from_account .free .checked_sub(&value) .ok_or(Error::::InsufficientBalance)?; + println!("To acccount {:?} {:?}", dest, to_account); + println!("From acccount {:?} {:?}", transactor, from_account); + to_account.free = to_account .free .checked_add(&value) diff --git a/frame/evm-system/src/lib.rs b/frame/evm-system/src/lib.rs index 73baa3cbdb..ecacda3e76 100644 --- a/frame/evm-system/src/lib.rs +++ b/frame/evm-system/src/lib.rs @@ -117,6 +117,24 @@ 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, +} + impl Pallet { /// Check the account existence. pub fn account_exists(who: &::AccountId) -> bool { @@ -146,30 +164,31 @@ 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, + ) -> Result { if !Self::account_exists(who) { return Err(Error::::AccountNotExist.into()); } if Account::::get(who).data != ::AccountData::default() { - return Err(Error::::AccountDataNotEmpty.into()); + return Ok(AccountRemovalStatus::Exists); } Account::::remove(who); Self::on_killed_account(who.clone()); - - Ok(()) + Ok(AccountRemovalStatus::Reaped) } } @@ -182,13 +201,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 28fc971842..1b16fbdce1 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_ok, assert_noop}; +use frame_support::{assert_noop, assert_ok}; use mockall::predicate; use sp_core::H160; @@ -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(); @@ -26,17 +26,20 @@ 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_eq!( + EvmSystem::create_account(&account_id), + AccountCreationStatus::Created + ); // 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(); @@ -46,20 +49,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(); >::insert(account_id.clone(), AccountInfo::<_, _>::default()); // Invoke the function under test. - assert_noop!(EvmSystem::create_account(&account_id), Error::::AccountAlreadyExist); + assert_eq!( + EvmSystem::create_account(&account_id), + AccountCreationStatus::Existed + ); }); } /// 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(); >::insert(account_id.clone(), AccountInfo::<_, _>::default()); @@ -72,9 +78,7 @@ 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. @@ -82,7 +86,9 @@ fn remove_account_works() { // 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(); @@ -92,19 +98,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::::AccountNotExist); + assert_noop!( + EvmSystem::remove_account(&account_id), + Error::::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(); @@ -199,9 +208,7 @@ fn try_mutate_exists_account_removed() { new_test_ext().execute_with(|| { // 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)); diff --git a/rust-toolchain.toml b/rust-toolchain.toml new file mode 100644 index 0000000000..bf4676697e --- /dev/null +++ b/rust-toolchain.toml @@ -0,0 +1,5 @@ +[toolchain] +channel = "nightly-2023-06-08" +components = ["rustfmt", "clippy"] +targets = ["wasm32-unknown-unknown"] +profile = "minimal"