diff --git a/frame/evm-system/src/lib.rs b/frame/evm-system/src/lib.rs index 497f1c1e86..d69eb10cbb 100644 --- a/frame/evm-system/src/lib.rs +++ b/frame/evm-system/src/lib.rs @@ -160,16 +160,19 @@ impl Pallet { /// Increment a particular account's nonce by 1. pub fn inc_account_nonce(who: &::AccountId) { - Account::::mutate(who, |a| { - a.nonce += ::Index::one(); - - // Meaning that account is being created. - if a.nonce == ::Index::one() - && a.data == ::AccountData::default() - { - Self::on_created_account(who.clone()); - } + let is_new_account = Account::::mutate_exists(who, |maybe_account| { + let is_new_account = maybe_account.is_none(); + + let account = maybe_account.get_or_insert_default(); + account.nonce += ::Index::one(); + + is_new_account }); + + // Meaning that account is being created. + if is_new_account { + Self::on_created_account(who.clone()); + } } /// Create an account. diff --git a/frame/evm-system/src/tests.rs b/frame/evm-system/src/tests.rs index 2a797e966b..34e18378a9 100644 --- a/frame/evm-system/src/tests.rs +++ b/frame/evm-system/src/tests.rs @@ -113,14 +113,17 @@ fn remove_account_fails() { }); } -/// This test verifies that incrementing account nonce works in the happy path. +/// This test verifies that incrementing account nonce works in the happy path +/// in case a new account should be created. #[test] -fn inc_account_nonce_works() { +fn inc_account_nonce_account_created() { new_test_ext().execute_with_ext(|_| { // Prepare test data. let account_id = H160::from_str("1000000000000000000000000000000000000001").unwrap(); // Check test preconditions. + assert!(!EvmSystem::account_exists(&account_id)); + let nonce_before = EvmSystem::account_nonce(&account_id); // Set block number to enable events. @@ -139,6 +142,7 @@ fn inc_account_nonce_works() { // Assert state changes. assert_eq!(EvmSystem::account_nonce(&account_id), nonce_before + 1); + assert!(EvmSystem::account_exists(&account_id)); System::assert_has_event(RuntimeEvent::EvmSystem(Event::NewAccount { account: account_id, })); @@ -147,12 +151,36 @@ fn inc_account_nonce_works() { EvmSystem::inc_account_nonce(&account_id); // Assert state changes. assert_eq!(EvmSystem::account_nonce(&account_id), nonce_before + 2); + assert!(EvmSystem::account_exists(&account_id)); // Assert mock invocations. on_new_account_ctx.checkpoint(); }); } +/// This test verifies that incrementing account nonce works in the happy path +/// in case an account already exists. +#[test] +fn inc_account_nonce_account_exists() { + new_test_ext().execute_with_ext(|_| { + // Prepare test data. + let account_id = H160::from_str("1000000000000000000000000000000000000001").unwrap(); + >::insert(account_id.clone(), AccountInfo::<_, _>::default()); + + // Check test preconditions. + assert!(EvmSystem::account_exists(&account_id)); + + let nonce_before = EvmSystem::account_nonce(&account_id); + + // Invoke the function under test. + EvmSystem::inc_account_nonce(&account_id); + + // Assert state changes. + assert!(EvmSystem::account_exists(&account_id)); + assert_eq!(EvmSystem::account_nonce(&account_id), nonce_before + 1); + }); +} + /// This test verifies that try_mutate_exists works as expected in case data wasn't providing /// and returned data is `Some`. As a result, a new account has been created. #[test]