Skip to content
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
21 changes: 12 additions & 9 deletions frame/evm-system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,16 +160,19 @@ impl<T: Config> Pallet<T> {

/// Increment a particular account's nonce by 1.
pub fn inc_account_nonce(who: &<T as Config>::AccountId) {
Account::<T>::mutate(who, |a| {
a.nonce += <T as Config>::Index::one();

// Meaning that account is being created.
if a.nonce == <T as Config>::Index::one()
&& a.data == <T as Config>::AccountData::default()
{
Self::on_created_account(who.clone());
}
let is_new_account = Account::<T>::mutate_exists(who, |maybe_account| {
let is_new_account = maybe_account.is_none();

let account = maybe_account.get_or_insert_default();
account.nonce += <T as Config>::Index::one();

is_new_account
});

// Meaning that account is being created.
if is_new_account {
Self::on_created_account(who.clone());
}
}

/// Create an account.
Expand Down
32 changes: 30 additions & 2 deletions frame/evm-system/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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,
}));
Expand All @@ -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();
<Account<Test>>::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]
Expand Down