Skip to content

Commit

Permalink
balanced and unbalanced conformance tests
Browse files Browse the repository at this point in the history
  • Loading branch information
liamaharon committed Aug 30, 2023
1 parent b4ee6f2 commit e5c876d
Show file tree
Hide file tree
Showing 6 changed files with 1,461 additions and 52 deletions.
5 changes: 4 additions & 1 deletion substrate/frame/balances/src/impl_fungible.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,10 @@ impl<T: Config<I>, I: 'static> fungible::Unbalanced<T::AccountId> for Pallet<T,
}

fn deactivate(amount: Self::Balance) {
InactiveIssuance::<T, I>::mutate(|b| b.saturating_accrue(amount));
InactiveIssuance::<T, I>::mutate(|b| {
// InactiveIssuance cannot be greater than TotalIssuance.
*b = b.saturating_add(amount).min(TotalIssuance::<T, I>::get());
});
}

fn reactivate(amount: Self::Balance) {
Expand Down
81 changes: 67 additions & 14 deletions substrate/frame/balances/src/tests/fungible_conformance_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,30 @@ use super::*;
use frame_support::traits::fungible::{conformance_tests, Inspect, Mutate};
use paste::paste;

macro_rules! run_tests {
($path:path, $ext_deposit:expr, $($name:ident),*) => {
macro_rules! generate_tests {
// Handle a conformance test that requires special testing with and without a dust trap.
(dust_trap_variation, $base_path:path, $scope:expr, $trait:ident, $ext_deposit:expr, $($test_name:ident),*) => {
$(
paste! {
#[test]
fn [< $name _existential_deposit_ $ext_deposit _dust_trap_on >]() {
fn [<$trait _ $scope _ $test_name _existential_deposit_ $ext_deposit _dust_trap_on >]() {
// Some random trap account.
let trap_account = <Test as frame_system::Config>::AccountId::from(65174286u64);
let builder = ExtBuilder::default().existential_deposit($ext_deposit).dust_trap(trap_account);
builder.build_and_execute_with(|| {
Balances::set_balance(&trap_account, Balances::minimum_balance());
$path::$name::<
$base_path::$scope::$trait::$test_name::<
Balances,
<Test as frame_system::Config>::AccountId,
>(Some(trap_account));
});
}

#[test]
fn [< $name _existential_deposit_ $ext_deposit _dust_trap_off >]() {
fn [< $trait _ $scope _ $test_name _existential_deposit_ $ext_deposit _dust_trap_off >]() {
let builder = ExtBuilder::default().existential_deposit($ext_deposit);
builder.build_and_execute_with(|| {
$path::$name::<
$base_path::$scope::$trait::$test_name::<
Balances,
<Test as frame_system::Config>::AccountId,
>(None);
Expand All @@ -49,9 +51,37 @@ macro_rules! run_tests {
}
)*
};
($path:path, $ext_deposit:expr) => {
run_tests!(
$path,
// Regular conformance test
($base_path:path, $scope:expr, $trait:ident, $ext_deposit:expr, $($test_name:ident),*) => {
$(
paste! {
#[test]
fn [< $trait _ $scope _ $test_name _existential_deposit_ $ext_deposit>]() {
let builder = ExtBuilder::default().existential_deposit($ext_deposit);
builder.build_and_execute_with(|| {
$base_path::$scope::$trait::$test_name::<
Balances,
<Test as frame_system::Config>::AccountId,
>();
});
}
}
)*
};
($base_path:path, $ext_deposit:expr) => {
// regular::mutate
generate_tests!(
dust_trap_variation,
$base_path,
regular,
mutate,
$ext_deposit,
transfer_expendable_dust
);
generate_tests!(
$base_path,
regular,
mutate,
$ext_deposit,
mint_into_success,
mint_into_overflow,
Expand All @@ -66,7 +96,6 @@ macro_rules! run_tests {
shelve_insufficient_funds,
transfer_success,
transfer_expendable_all,
transfer_expendable_dust,
transfer_protect_preserve,
set_balance_mint_success,
set_balance_burn_success,
Expand All @@ -79,10 +108,34 @@ macro_rules! run_tests {
reducible_balance_expendable,
reducible_balance_protect_preserve
);
// regular::unbalanced
generate_tests!(
$base_path,
regular,
unbalanced,
$ext_deposit,
write_balance,
decrease_balance_expendable,
decrease_balance_preserve,
increase_balance,
set_total_issuance,
deactivate_and_reactivate
);
// regular::balanced
generate_tests!(
$base_path,
regular,
balanced,
$ext_deposit,
issue_and_resolve_credit,
rescind_and_settle_debt,
deposit,
withdraw,
pair
);
};
}

run_tests!(conformance_tests::inspect_mutate, 1);
run_tests!(conformance_tests::inspect_mutate, 2);
run_tests!(conformance_tests::inspect_mutate, 5);
run_tests!(conformance_tests::inspect_mutate, 1000);
generate_tests!(conformance_tests, 1);
generate_tests!(conformance_tests, 5);
generate_tests!(conformance_tests, 1000);
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pub mod inspect_mutate;
pub mod regular;
Loading

0 comments on commit e5c876d

Please sign in to comment.