Skip to content

Commit

Permalink
--wip--
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitrylavrenov committed Mar 28, 2024
1 parent 05c4abc commit dbe91ad
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 33 deletions.
11 changes: 11 additions & 0 deletions frame/evm-balances/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,13 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
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;
Expand Down Expand Up @@ -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::<T, I>::InsufficientBalance)?;

println!("To acccount {:?} {:?}", dest, to_account);
println!("From acccount {:?} {:?}", transactor, from_account);

to_account.free = to_account
.free
.checked_add(&value)
Expand Down
42 changes: 29 additions & 13 deletions frame/evm-system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T: Config> Pallet<T> {
/// Check the account existence.
pub fn account_exists(who: &<T as Config>::AccountId) -> bool {
Expand Down Expand Up @@ -146,30 +164,31 @@ impl<T: Config> Pallet<T> {
}

/// Create an account.
pub fn create_account(who: &<T as Config>::AccountId) -> DispatchResult {
pub fn create_account(who: &<T as Config>::AccountId) -> AccountCreationStatus {
if Self::account_exists(who) {
return Err(Error::<T>::AccountAlreadyExist.into());
return AccountCreationStatus::Existed;
}

Account::<T>::insert(who.clone(), AccountInfo::<_, _>::default());
Self::on_created_account(who.clone());
Ok(())
AccountCreationStatus::Created
}

/// Remove an account.
pub fn remove_account(who: &<T as Config>::AccountId) -> DispatchResult {
pub fn remove_account(
who: &<T as Config>::AccountId,
) -> Result<AccountRemovalStatus, DispatchError> {
if !Self::account_exists(who) {
return Err(Error::<T>::AccountNotExist.into());
}

if Account::<T>::get(who).data != <T as Config>::AccountData::default() {
return Err(Error::<T>::AccountDataNotEmpty.into());
return Ok(AccountRemovalStatus::Exists);
}

Account::<T>::remove(who);
Self::on_killed_account(who.clone());

Ok(())
Ok(AccountRemovalStatus::Reaped)
}
}

Expand All @@ -182,13 +201,10 @@ impl<T: Config> StoredMap<<T as Config>::AccountId, <T as Config>::AccountData>
k: &<T as Config>::AccountId,
f: impl FnOnce(&mut Option<<T as Config>::AccountData>) -> Result<R, E>,
) -> Result<R, E> {
let account = Account::<T>::get(k);
let was_providing = account.data != <T as Config>::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::<T>::get(k).data), true)
} else {
None
(None, false)
};

let result = f(&mut maybe_account_data)?;
Expand Down
47 changes: 27 additions & 20 deletions frame/evm-system/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

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,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();
Expand All @@ -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();
<Account<Test>>::insert(account_id.clone(), AccountInfo::<_, _>::default());

// Invoke the function under test.
assert_noop!(EvmSystem::create_account(&account_id), Error::<Test>::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();
<Account<Test>>::insert(account_id.clone(), AccountInfo::<_, _>::default());
Expand All @@ -72,17 +78,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 +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::<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 Expand Up @@ -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;
<Account<Test>>::insert(account_id.clone(), AccountInfo { nonce, data });
<Account<Test>>::insert(account_id.clone(), AccountInfo { nonce: 1, data: 0 });

// Check test preconditions.
assert!(EvmSystem::account_exists(&account_id));
Expand Down
5 changes: 5 additions & 0 deletions rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[toolchain]
channel = "nightly-2023-06-08"
components = ["rustfmt", "clippy"]
targets = ["wasm32-unknown-unknown"]
profile = "minimal"

0 comments on commit dbe91ad

Please sign in to comment.