Skip to content

Commit

Permalink
Fix removing account logic at pallet-evm-system (#111) (#116)
Browse files Browse the repository at this point in the history
* Add assert_total_issuance_invariant helper

* Add evm_system_removing_account_non_zero_balance test

* Fix removing account logic

* Undo redundant changes

* Redesign fix usage

* Undo try_mutate_exists_account_removed changes

* Rename Exists account removal status into Remains

* Improve docs

* Apply renaming at tests

* Use assert_storage_noop

* Remove rusttoolchain
  • Loading branch information
dmitrylavrenov committed Apr 19, 2024
1 parent 392e0fb commit d6d2437
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 29 deletions.
52 changes: 40 additions & 12 deletions frame/evm-balances/src/tests/currency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use frame_support::{assert_noop, assert_ok, traits::Currency};
use sp_runtime::TokenError;

use crate::{mock::*, *};
use crate::{mock::*, tests::assert_total_issuance_invariant, *};

#[test]
fn total_issuance_works() {
Expand Down Expand Up @@ -66,6 +66,8 @@ fn deactivate_reactivate_works() {
EvmBalances::reactivate(40);
// Assert state changes.
assert_eq!(<InactiveIssuance<Test>>::get(), 60);

assert_total_issuance_invariant();
});
}

Expand All @@ -90,6 +92,8 @@ fn burn_works() {
assert_eq!(EvmBalances::total_issuance(), 2 * INIT_BALANCE - 100);
drop(imbalance);
assert_eq!(EvmBalances::total_issuance(), 2 * INIT_BALANCE);

assert_total_issuance_invariant();
});
}

Expand All @@ -106,6 +110,8 @@ fn issue_works() {
assert_eq!(EvmBalances::total_issuance(), 2 * INIT_BALANCE + 100);
drop(imbalance);
assert_eq!(EvmBalances::total_issuance(), 2 * INIT_BALANCE);

assert_total_issuance_invariant();
});
}

Expand Down Expand Up @@ -142,6 +148,8 @@ fn transfer_works() {
to: bob(),
amount: transfered_amount,
}));

assert_total_issuance_invariant();
});
}

Expand Down Expand Up @@ -182,6 +190,8 @@ fn transfer_works_full_balance() {
System::assert_has_event(RuntimeEvent::EvmSystem(
pallet_evm_system::Event::KilledAccount { account: alice() },
));

assert_total_issuance_invariant();
});
}

Expand Down Expand Up @@ -256,6 +266,8 @@ fn slash_works() {
who: alice(),
amount: slashed_amount,
}));

assert_total_issuance_invariant();
});
}

Expand Down Expand Up @@ -286,6 +298,8 @@ fn slash_works_full_balance() {
System::assert_has_event(RuntimeEvent::EvmSystem(
pallet_evm_system::Event::KilledAccount { account: alice() },
));

assert_total_issuance_invariant();
});
}

Expand All @@ -301,10 +315,8 @@ fn deposit_into_existing_works() {
System::set_block_number(1);

// Invoke the function under test.
assert_ok!(EvmBalances::deposit_into_existing(
&alice(),
deposited_amount
));
let imbalance = EvmBalances::deposit_into_existing(&alice(), deposited_amount).unwrap();
drop(imbalance);

// Assert state changes.
assert_eq!(
Expand All @@ -315,6 +327,8 @@ fn deposit_into_existing_works() {
who: alice(),
amount: deposited_amount,
}));

assert_total_issuance_invariant();
});
}

Expand Down Expand Up @@ -376,6 +390,8 @@ fn deposit_creating_works() {
System::assert_has_event(RuntimeEvent::EvmSystem(
pallet_evm_system::Event::NewAccount { account: charlie },
));

assert_total_issuance_invariant();
});
}

Expand All @@ -391,12 +407,14 @@ fn withdraw_works() {
System::set_block_number(1);

// Invoke the function under test.
assert_ok!(EvmBalances::withdraw(
let imbalance = EvmBalances::withdraw(
&alice(),
withdrawed_amount,
WithdrawReasons::FEE,
ExistenceRequirement::KeepAlive
));
ExistenceRequirement::KeepAlive,
)
.unwrap();
drop(imbalance);

// Assert state changes.
assert_eq!(
Expand All @@ -407,6 +425,8 @@ fn withdraw_works() {
who: alice(),
amount: withdrawed_amount,
}));

assert_total_issuance_invariant();
});
}

Expand All @@ -422,12 +442,14 @@ fn withdraw_works_full_balance() {
System::set_block_number(1);

// Invoke the function under test.
assert_ok!(EvmBalances::withdraw(
let imbalance = EvmBalances::withdraw(
&alice(),
withdrawed_amount,
WithdrawReasons::TRANSFER,
ExistenceRequirement::AllowDeath
));
WithdrawReasons::FEE,
ExistenceRequirement::AllowDeath,
)
.unwrap();
drop(imbalance);

// Assert state changes.
assert_eq!(
Expand All @@ -442,6 +464,8 @@ fn withdraw_works_full_balance() {
System::assert_has_event(RuntimeEvent::EvmSystem(
pallet_evm_system::Event::KilledAccount { account: alice() },
));

assert_total_issuance_invariant();
});
}

Expand Down Expand Up @@ -502,6 +526,8 @@ fn make_free_balance_be_works() {

// Assert state changes.
assert_eq!(EvmBalances::total_balance(&charlie), made_free_balance);

assert_total_issuance_invariant();
});
}

Expand All @@ -528,6 +554,8 @@ fn evm_system_account_should_be_reaped() {
System::assert_has_event(RuntimeEvent::EvmSystem(
pallet_evm_system::Event::KilledAccount { account: bob() },
));

assert_total_issuance_invariant();
});
}

Expand Down
34 changes: 33 additions & 1 deletion frame/evm-balances/src/tests/fungible.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use frame_support::{
};
use sp_runtime::TokenError;

use crate::{mock::*, *};
use crate::{mock::*, tests::assert_total_issuance_invariant, *};

#[test]
fn total_issuance_works() {
Expand Down Expand Up @@ -353,6 +353,8 @@ fn deactivate_reactivate_works() {
EvmBalances::reactivate(40);
// Assert state changes.
assert_eq!(<InactiveIssuance<Test>>::get(), 60);

assert_total_issuance_invariant();
});
}

Expand Down Expand Up @@ -384,6 +386,8 @@ fn mint_into_works() {
who: alice(),
amount: minted_balance,
}));

assert_total_issuance_invariant();
});
}

Expand Down Expand Up @@ -436,6 +440,8 @@ fn burn_from_works() {
who: alice(),
amount: burned_balance,
}));

assert_total_issuance_invariant();
});
}

Expand Down Expand Up @@ -470,6 +476,8 @@ fn burn_from_works_best_effort() {
System::assert_has_event(RuntimeEvent::EvmSystem(
pallet_evm_system::Event::KilledAccount { account: alice() },
));

assert_total_issuance_invariant();
});
}

Expand Down Expand Up @@ -504,6 +512,8 @@ fn burn_from_works_exact_full_balance() {
System::assert_has_event(RuntimeEvent::EvmSystem(
pallet_evm_system::Event::KilledAccount { account: alice() },
));

assert_total_issuance_invariant();
});
}

Expand Down Expand Up @@ -560,6 +570,8 @@ fn shelve_works() {
who: alice(),
amount: shelved_balance,
}));

assert_total_issuance_invariant();
});
}

Expand Down Expand Up @@ -589,6 +601,8 @@ fn shelve_works_exact_full_balance() {
System::assert_has_event(RuntimeEvent::EvmSystem(
pallet_evm_system::Event::KilledAccount { account: alice() },
));

assert_total_issuance_invariant();
});
}

Expand Down Expand Up @@ -640,6 +654,8 @@ fn restore_works() {
who: alice(),
amount: restored_balance,
}));

assert_total_issuance_invariant();
});
}

Expand Down Expand Up @@ -692,6 +708,8 @@ fn transfer_works() {
to: bob(),
amount: transfered_amount,
}));

assert_total_issuance_invariant();
});
}

Expand Down Expand Up @@ -732,6 +750,8 @@ fn transfer_works_full_balance() {
System::assert_has_event(RuntimeEvent::EvmSystem(
pallet_evm_system::Event::KilledAccount { account: alice() },
));

assert_total_issuance_invariant();
});
}

Expand Down Expand Up @@ -815,6 +835,8 @@ fn rescind_works() {
System::assert_has_event(RuntimeEvent::EvmBalances(Event::Rescinded {
amount: rescinded_balance,
}));

assert_total_issuance_invariant();
});
}

Expand Down Expand Up @@ -842,6 +864,8 @@ fn issue_works() {
System::assert_has_event(RuntimeEvent::EvmBalances(Event::Issued {
amount: issued_balance,
}));

assert_total_issuance_invariant();
});
}

Expand Down Expand Up @@ -872,6 +896,8 @@ fn deposit_flow_works() {

let _ = EvmBalances::settle(&bob(), debt, Preservation::Expendable);
assert_eq!(EvmBalances::total_issuance(), 2 * INIT_BALANCE);

assert_total_issuance_invariant();
});
}

Expand Down Expand Up @@ -904,6 +930,8 @@ fn deposit_works_new_account() {
System::assert_has_event(RuntimeEvent::EvmSystem(
pallet_evm_system::Event::NewAccount { account: charlie },
));

assert_total_issuance_invariant();
});
}

Expand Down Expand Up @@ -940,6 +968,8 @@ fn withdraw_works() {
assert_eq!(EvmBalances::total_issuance(), 2 * INIT_BALANCE);
let _ = EvmBalances::resolve(&bob(), credit);
assert_eq!(EvmBalances::total_issuance(), 2 * INIT_BALANCE);

assert_total_issuance_invariant();
});
}

Expand Down Expand Up @@ -980,5 +1010,7 @@ fn withdraw_works_full_balance() {
assert_eq!(EvmBalances::total_issuance(), 2 * INIT_BALANCE);
let _ = EvmBalances::resolve(&bob(), credit);
assert_eq!(EvmBalances::total_issuance(), 2 * INIT_BALANCE);

assert_total_issuance_invariant();
});
}
Loading

0 comments on commit d6d2437

Please sign in to comment.