From 577e69ea72615273b178612dac17e78373d49500 Mon Sep 17 00:00:00 2001 From: Liam Aharon Date: Fri, 24 Mar 2023 13:39:13 +0400 Subject: [PATCH 01/37] typo --- frame/support/src/traits/tokens/fungible/regular.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frame/support/src/traits/tokens/fungible/regular.rs b/frame/support/src/traits/tokens/fungible/regular.rs index 574392cac8256..92fb21332ac10 100644 --- a/frame/support/src/traits/tokens/fungible/regular.rs +++ b/frame/support/src/traits/tokens/fungible/regular.rs @@ -122,7 +122,7 @@ impl> Dust { /// **WARNING** /// Do not use this directly unless you want trouble, since it allows you to alter account balances /// without keeping the issuance up to date. It has no safeguards against accidentally creating -/// token imbalances in your system leading to accidental imflation or deflation. It's really just +/// token imbalances in your system leading to accidental inflation or deflation. It's really just /// for the underlying datatype to implement so the user gets the much safer `Balanced` trait to /// use. pub trait Unbalanced: Inspect { From 2d43fbc9dd9c8d8bbe04d42296404f3231db6be5 Mon Sep 17 00:00:00 2001 From: Liam Aharon Date: Sat, 25 Mar 2023 15:16:09 +0400 Subject: [PATCH 02/37] - create test files for each fungile trait - begin implementing tests for the Inspect trait --- .../fungible_conformance_tests/balanced.rs | 4 + .../balanced_hold.rs | 4 + .../handle_imbalance_drop.rs | 4 + .../fungible_conformance_tests/inspect.rs | 137 ++++++++++++++++++ .../inspect_freeze.rs | 4 + .../inspect_hold.rs | 4 + .../tests/fungible_conformance_tests/mod.rs | 18 +++ .../fungible_conformance_tests/mutate.rs | 4 + .../mutate_freeze.rs | 4 + .../fungible_conformance_tests/mutate_hold.rs | 4 + frame/balances/src/tests/mod.rs | 1 + 11 files changed, 188 insertions(+) create mode 100644 frame/balances/src/tests/fungible_conformance_tests/balanced.rs create mode 100644 frame/balances/src/tests/fungible_conformance_tests/balanced_hold.rs create mode 100644 frame/balances/src/tests/fungible_conformance_tests/handle_imbalance_drop.rs create mode 100644 frame/balances/src/tests/fungible_conformance_tests/inspect.rs create mode 100644 frame/balances/src/tests/fungible_conformance_tests/inspect_freeze.rs create mode 100644 frame/balances/src/tests/fungible_conformance_tests/inspect_hold.rs create mode 100644 frame/balances/src/tests/fungible_conformance_tests/mod.rs create mode 100644 frame/balances/src/tests/fungible_conformance_tests/mutate.rs create mode 100644 frame/balances/src/tests/fungible_conformance_tests/mutate_freeze.rs create mode 100644 frame/balances/src/tests/fungible_conformance_tests/mutate_hold.rs diff --git a/frame/balances/src/tests/fungible_conformance_tests/balanced.rs b/frame/balances/src/tests/fungible_conformance_tests/balanced.rs new file mode 100644 index 0000000000000..ca0c68b685df9 --- /dev/null +++ b/frame/balances/src/tests/fungible_conformance_tests/balanced.rs @@ -0,0 +1,4 @@ +#[test] +fn stub() { + assert!(true); +} diff --git a/frame/balances/src/tests/fungible_conformance_tests/balanced_hold.rs b/frame/balances/src/tests/fungible_conformance_tests/balanced_hold.rs new file mode 100644 index 0000000000000..ca0c68b685df9 --- /dev/null +++ b/frame/balances/src/tests/fungible_conformance_tests/balanced_hold.rs @@ -0,0 +1,4 @@ +#[test] +fn stub() { + assert!(true); +} diff --git a/frame/balances/src/tests/fungible_conformance_tests/handle_imbalance_drop.rs b/frame/balances/src/tests/fungible_conformance_tests/handle_imbalance_drop.rs new file mode 100644 index 0000000000000..ca0c68b685df9 --- /dev/null +++ b/frame/balances/src/tests/fungible_conformance_tests/handle_imbalance_drop.rs @@ -0,0 +1,4 @@ +#[test] +fn stub() { + assert!(true); +} diff --git a/frame/balances/src/tests/fungible_conformance_tests/inspect.rs b/frame/balances/src/tests/fungible_conformance_tests/inspect.rs new file mode 100644 index 0000000000000..a3505783d412b --- /dev/null +++ b/frame/balances/src/tests/fungible_conformance_tests/inspect.rs @@ -0,0 +1,137 @@ +// This file is part of Substrate. + +// Copyright (C) 2017-2023 Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: Apache-2.0 + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//! Conformance tests regarding the functionality of the `fungible` Inspect trait. +//! +//! TODO: Decouple these tests from balances and abstract them into a macro so that they can be +//! used by any pallet that implements the `fungible` Inspect trait. + +use super::*; +use frame_support::traits::tokens::{ + Fortitude::{Force, Polite}, + Preservation::{Expendable, Preserve, Protect}, +}; +use fungible::{Inspect, Mutate, MutateFreeze}; + +#[test] +fn total_issuance_works() { + ExtBuilder::default().build_and_execute_with(|| { + assert_eq!(Balances::total_issuance(), 0); + Balances::set_balance(&1, 100); + assert_eq!(Balances::total_issuance(), 100); + Balances::set_balance(&2, 100); + assert_eq!(Balances::total_issuance(), 200); + Balances::set_balance(&2, 101); + assert_eq!(Balances::total_issuance(), 201); + }); +} + +#[test] +fn active_issuance_works() { + ExtBuilder::default().existential_deposit(10).build_and_execute_with(|| { + // fungible docs on active_issuance: + // "The total amount of issuance in the system excluding those which are controlled by + // the system." + // + // TODO: What does it mean for some issuance to be 'controlled by the system'? + // If there is no offical definition, then I think it cannot be tested in a generic way and + // we should remove this test. + assert!(true); + }); +} + +#[test] +fn minimum_balance_works() { + ExtBuilder::default().existential_deposit(5).build_and_execute_with(|| { + assert_eq!(Balances::minimum_balance(), 5); + }); + ExtBuilder::default().existential_deposit(10).build_and_execute_with(|| { + assert_eq!(Balances::minimum_balance(), 10); + }); +} + +#[test] +fn total_balance_works() { + ExtBuilder::default().existential_deposit(10).build_and_execute_with(|| { + assert_eq!(Balances::total_balance(&1), 0); + Balances::set_balance(&1, 100); + assert_eq!(Balances::total_balance(&1), 100); + Balances::set_balance(&2, 100); + assert_eq!(Balances::total_balance(&2), 100); + Balances::set_balance(&2, 101); + assert_eq!(Balances::total_balance(&2), 101); + }); +} + +// TODO: Properly understand Preservation and Fortitude enums, and ensure every permutation is +// thoroughly tested. +#[test] +fn reducible_balance_basic_works() { + ExtBuilder::default().existential_deposit(10).build_and_execute_with(|| { + Balances::set_balance(&1, 100); + assert_eq!(Balances::reducible_balance(&1, Expendable, Polite), 100); + assert_eq!(Balances::reducible_balance(&1, Protect, Polite), 90); + assert_eq!(Balances::reducible_balance(&1, Preserve, Polite), 90); + assert_eq!(Balances::reducible_balance(&1, Expendable, Force), 100); + assert_eq!(Balances::reducible_balance(&1, Protect, Force), 90); + assert_eq!(Balances::reducible_balance(&1, Preserve, Force), 90); + }); +} + +#[test] +fn reducible_balance_other_provide_works() { + ExtBuilder::default().existential_deposit(10).build_and_execute_with(|| { + Balances::set_balance(&1, 100); + System::inc_providers(&1); + assert_eq!(Balances::reducible_balance(&1, Expendable, Polite), 100); + assert_eq!(Balances::reducible_balance(&1, Protect, Polite), 100); + assert_eq!(Balances::reducible_balance(&1, Preserve, Polite), 90); + assert_eq!(Balances::reducible_balance(&1, Expendable, Force), 100); + assert_eq!(Balances::reducible_balance(&1, Protect, Force), 100); + assert_eq!(Balances::reducible_balance(&1, Preserve, Force), 90); + }); +} + +#[test] +fn reducible_balance_frozen_works() { + ExtBuilder::default().existential_deposit(10).build_and_execute_with(|| { + Balances::set_balance(&1, 100); + assert_ok!(Balances::set_freeze(&TestId::Foo, &1, 50)); + assert_eq!(Balances::reducible_balance(&1, Expendable, Polite), 50); + assert_eq!(Balances::reducible_balance(&1, Protect, Polite), 50); + assert_eq!(Balances::reducible_balance(&1, Preserve, Polite), 50); + assert_eq!(Balances::reducible_balance(&1, Expendable, Force), 90); + assert_eq!(Balances::reducible_balance(&1, Protect, Force), 90); + assert_eq!(Balances::reducible_balance(&1, Preserve, Force), 90); + }); +} + +#[test] +fn can_deposit_works() { + ExtBuilder::default().build_and_execute_with(|| { + // TODO + assert!(true); + }); +} + +#[test] +fn can_deposit_withdraw_works() { + ExtBuilder::default().build_and_execute_with(|| { + // TODO + assert!(true); + }); +} diff --git a/frame/balances/src/tests/fungible_conformance_tests/inspect_freeze.rs b/frame/balances/src/tests/fungible_conformance_tests/inspect_freeze.rs new file mode 100644 index 0000000000000..ca0c68b685df9 --- /dev/null +++ b/frame/balances/src/tests/fungible_conformance_tests/inspect_freeze.rs @@ -0,0 +1,4 @@ +#[test] +fn stub() { + assert!(true); +} diff --git a/frame/balances/src/tests/fungible_conformance_tests/inspect_hold.rs b/frame/balances/src/tests/fungible_conformance_tests/inspect_hold.rs new file mode 100644 index 0000000000000..ca0c68b685df9 --- /dev/null +++ b/frame/balances/src/tests/fungible_conformance_tests/inspect_hold.rs @@ -0,0 +1,4 @@ +#[test] +fn stub() { + assert!(true); +} diff --git a/frame/balances/src/tests/fungible_conformance_tests/mod.rs b/frame/balances/src/tests/fungible_conformance_tests/mod.rs new file mode 100644 index 0000000000000..dad5a604653d8 --- /dev/null +++ b/frame/balances/src/tests/fungible_conformance_tests/mod.rs @@ -0,0 +1,18 @@ +/// Temporary module to hold the conformance tests for the Fungible trait, +/// until it is abstracted into pallet-agnostic macro/s and moved outside +/// of the balances module. +/// +/// TODO: Decouple these tests from balances and abstract them into a macro so that they can be +/// used by any pallet that implements any `fungible` traits. +/// +/// Open question: Do we need conformance tests for 'unbalanced' traits? +use super::*; +mod balanced; +mod balanced_hold; +mod handle_imbalance_drop; +mod inspect; +mod inspect_freeze; +mod inspect_hold; +pub mod mutate; +pub mod mutate_freeze; +pub mod mutate_hold; diff --git a/frame/balances/src/tests/fungible_conformance_tests/mutate.rs b/frame/balances/src/tests/fungible_conformance_tests/mutate.rs new file mode 100644 index 0000000000000..ca0c68b685df9 --- /dev/null +++ b/frame/balances/src/tests/fungible_conformance_tests/mutate.rs @@ -0,0 +1,4 @@ +#[test] +fn stub() { + assert!(true); +} diff --git a/frame/balances/src/tests/fungible_conformance_tests/mutate_freeze.rs b/frame/balances/src/tests/fungible_conformance_tests/mutate_freeze.rs new file mode 100644 index 0000000000000..ca0c68b685df9 --- /dev/null +++ b/frame/balances/src/tests/fungible_conformance_tests/mutate_freeze.rs @@ -0,0 +1,4 @@ +#[test] +fn stub() { + assert!(true); +} diff --git a/frame/balances/src/tests/fungible_conformance_tests/mutate_hold.rs b/frame/balances/src/tests/fungible_conformance_tests/mutate_hold.rs new file mode 100644 index 0000000000000..ca0c68b685df9 --- /dev/null +++ b/frame/balances/src/tests/fungible_conformance_tests/mutate_hold.rs @@ -0,0 +1,4 @@ +#[test] +fn stub() { + assert!(true); +} diff --git a/frame/balances/src/tests/mod.rs b/frame/balances/src/tests/mod.rs index c4a8a631cad20..38fca895e3fa9 100644 --- a/frame/balances/src/tests/mod.rs +++ b/frame/balances/src/tests/mod.rs @@ -45,6 +45,7 @@ use sp_runtime::{ mod currency_tests; mod dispatchable_tests; +mod fungible_conformance_tests; mod fungible_tests; mod reentrancy_tests; From 9e3dc4ecdaa44d9031bd971b4edbbf1f06642875 Mon Sep 17 00:00:00 2001 From: Liam Aharon Date: Tue, 28 Mar 2023 16:50:40 +0400 Subject: [PATCH 03/37] wrap inspect tests in a macro --- .../fungible_conformance_tests/inspect.rs | 212 +++++++++--------- 1 file changed, 107 insertions(+), 105 deletions(-) diff --git a/frame/balances/src/tests/fungible_conformance_tests/inspect.rs b/frame/balances/src/tests/fungible_conformance_tests/inspect.rs index a3505783d412b..c0a2deda9295a 100644 --- a/frame/balances/src/tests/fungible_conformance_tests/inspect.rs +++ b/frame/balances/src/tests/fungible_conformance_tests/inspect.rs @@ -20,118 +20,120 @@ //! TODO: Decouple these tests from balances and abstract them into a macro so that they can be //! used by any pallet that implements the `fungible` Inspect trait. -use super::*; -use frame_support::traits::tokens::{ - Fortitude::{Force, Polite}, - Preservation::{Expendable, Preserve, Protect}, -}; -use fungible::{Inspect, Mutate, MutateFreeze}; +macro_rules! fungible_inspect_conformance_tests { + ($ext_builder:ident, $pallet:ident) => { + use super::*; + use frame_support::traits::tokens::{ + Fortitude::{Force, Polite}, + Preservation::{Expendable, Preserve, Protect}, + }; + use fungible::{Inspect, Mutate, MutateFreeze}; -#[test] -fn total_issuance_works() { - ExtBuilder::default().build_and_execute_with(|| { - assert_eq!(Balances::total_issuance(), 0); - Balances::set_balance(&1, 100); - assert_eq!(Balances::total_issuance(), 100); - Balances::set_balance(&2, 100); - assert_eq!(Balances::total_issuance(), 200); - Balances::set_balance(&2, 101); - assert_eq!(Balances::total_issuance(), 201); - }); -} + #[test] + fn total_issuance_works() { + $ext_builder::default().build_and_execute_with(|| { + assert_eq!($pallet::total_issuance(), 0); + $pallet::set_balance(&1, 100); + assert_eq!($pallet::total_issuance(), 100); + $pallet::set_balance(&2, 100); + assert_eq!($pallet::total_issuance(), 200); + $pallet::set_balance(&2, 101); + assert_eq!($pallet::total_issuance(), 201); + }); + } -#[test] -fn active_issuance_works() { - ExtBuilder::default().existential_deposit(10).build_and_execute_with(|| { - // fungible docs on active_issuance: - // "The total amount of issuance in the system excluding those which are controlled by - // the system." - // - // TODO: What does it mean for some issuance to be 'controlled by the system'? - // If there is no offical definition, then I think it cannot be tested in a generic way and - // we should remove this test. - assert!(true); - }); -} + #[test] + fn active_issuance_works() { + $ext_builder::default().existential_deposit(10).build_and_execute_with(|| { + // TODO: Is there an official definition for what it means for issuance to be + // 'controlled by the system'? If there is no offical definition, then I think it + // cannot be tested in a generic way and we should remove this test. + assert!(true); + }); + } -#[test] -fn minimum_balance_works() { - ExtBuilder::default().existential_deposit(5).build_and_execute_with(|| { - assert_eq!(Balances::minimum_balance(), 5); - }); - ExtBuilder::default().existential_deposit(10).build_and_execute_with(|| { - assert_eq!(Balances::minimum_balance(), 10); - }); -} + #[test] + fn minimum_balance_works() { + $ext_builder::default().existential_deposit(5).build_and_execute_with(|| { + assert_eq!($pallet::minimum_balance(), 5); + }); + $ext_builder::default().existential_deposit(10).build_and_execute_with(|| { + assert_eq!($pallet::minimum_balance(), 10); + }); + } -#[test] -fn total_balance_works() { - ExtBuilder::default().existential_deposit(10).build_and_execute_with(|| { - assert_eq!(Balances::total_balance(&1), 0); - Balances::set_balance(&1, 100); - assert_eq!(Balances::total_balance(&1), 100); - Balances::set_balance(&2, 100); - assert_eq!(Balances::total_balance(&2), 100); - Balances::set_balance(&2, 101); - assert_eq!(Balances::total_balance(&2), 101); - }); -} + #[test] + fn total_balance_works() { + $ext_builder::default().existential_deposit(10).build_and_execute_with(|| { + assert_eq!($pallet::total_balance(&1), 0); + $pallet::set_balance(&1, 100); + assert_eq!($pallet::total_balance(&1), 100); + $pallet::set_balance(&2, 100); + assert_eq!($pallet::total_balance(&2), 100); + $pallet::set_balance(&2, 101); + assert_eq!($pallet::total_balance(&2), 101); + }); + } -// TODO: Properly understand Preservation and Fortitude enums, and ensure every permutation is -// thoroughly tested. -#[test] -fn reducible_balance_basic_works() { - ExtBuilder::default().existential_deposit(10).build_and_execute_with(|| { - Balances::set_balance(&1, 100); - assert_eq!(Balances::reducible_balance(&1, Expendable, Polite), 100); - assert_eq!(Balances::reducible_balance(&1, Protect, Polite), 90); - assert_eq!(Balances::reducible_balance(&1, Preserve, Polite), 90); - assert_eq!(Balances::reducible_balance(&1, Expendable, Force), 100); - assert_eq!(Balances::reducible_balance(&1, Protect, Force), 90); - assert_eq!(Balances::reducible_balance(&1, Preserve, Force), 90); - }); -} + // TODO: Properly understand Preservation and Fortitude enums, and ensure every permutation + // is thoroughly tested. + #[test] + fn reducible_balance_basic_works() { + $ext_builder::default().existential_deposit(10).build_and_execute_with(|| { + $pallet::set_balance(&1, 100); + assert_eq!($pallet::reducible_balance(&1, Expendable, Polite), 100); + assert_eq!($pallet::reducible_balance(&1, Protect, Polite), 90); + assert_eq!($pallet::reducible_balance(&1, Preserve, Polite), 90); + assert_eq!($pallet::reducible_balance(&1, Expendable, Force), 100); + assert_eq!($pallet::reducible_balance(&1, Protect, Force), 90); + assert_eq!($pallet::reducible_balance(&1, Preserve, Force), 90); + }); + } -#[test] -fn reducible_balance_other_provide_works() { - ExtBuilder::default().existential_deposit(10).build_and_execute_with(|| { - Balances::set_balance(&1, 100); - System::inc_providers(&1); - assert_eq!(Balances::reducible_balance(&1, Expendable, Polite), 100); - assert_eq!(Balances::reducible_balance(&1, Protect, Polite), 100); - assert_eq!(Balances::reducible_balance(&1, Preserve, Polite), 90); - assert_eq!(Balances::reducible_balance(&1, Expendable, Force), 100); - assert_eq!(Balances::reducible_balance(&1, Protect, Force), 100); - assert_eq!(Balances::reducible_balance(&1, Preserve, Force), 90); - }); -} + #[test] + fn reducible_balance_other_provide_works() { + $ext_builder::default().existential_deposit(10).build_and_execute_with(|| { + $pallet::set_balance(&1, 100); + System::inc_providers(&1); + assert_eq!($pallet::reducible_balance(&1, Expendable, Polite), 100); + assert_eq!($pallet::reducible_balance(&1, Protect, Polite), 100); + assert_eq!($pallet::reducible_balance(&1, Preserve, Polite), 90); + assert_eq!($pallet::reducible_balance(&1, Expendable, Force), 100); + assert_eq!($pallet::reducible_balance(&1, Protect, Force), 100); + assert_eq!($pallet::reducible_balance(&1, Preserve, Force), 90); + }); + } -#[test] -fn reducible_balance_frozen_works() { - ExtBuilder::default().existential_deposit(10).build_and_execute_with(|| { - Balances::set_balance(&1, 100); - assert_ok!(Balances::set_freeze(&TestId::Foo, &1, 50)); - assert_eq!(Balances::reducible_balance(&1, Expendable, Polite), 50); - assert_eq!(Balances::reducible_balance(&1, Protect, Polite), 50); - assert_eq!(Balances::reducible_balance(&1, Preserve, Polite), 50); - assert_eq!(Balances::reducible_balance(&1, Expendable, Force), 90); - assert_eq!(Balances::reducible_balance(&1, Protect, Force), 90); - assert_eq!(Balances::reducible_balance(&1, Preserve, Force), 90); - }); -} + #[test] + fn reducible_balance_frozen_works() { + $ext_builder::default().existential_deposit(10).build_and_execute_with(|| { + $pallet::set_balance(&1, 100); + assert_ok!($pallet::set_freeze(&TestId::Foo, &1, 50)); + assert_eq!($pallet::reducible_balance(&1, Expendable, Polite), 50); + assert_eq!($pallet::reducible_balance(&1, Protect, Polite), 50); + assert_eq!($pallet::reducible_balance(&1, Preserve, Polite), 50); + assert_eq!($pallet::reducible_balance(&1, Expendable, Force), 90); + assert_eq!($pallet::reducible_balance(&1, Protect, Force), 90); + assert_eq!($pallet::reducible_balance(&1, Preserve, Force), 90); + }); + } -#[test] -fn can_deposit_works() { - ExtBuilder::default().build_and_execute_with(|| { - // TODO - assert!(true); - }); -} + #[test] + fn can_deposit_works() { + $ext_builder::default().build_and_execute_with(|| { + // TODO + assert!(true); + }); + } -#[test] -fn can_deposit_withdraw_works() { - ExtBuilder::default().build_and_execute_with(|| { - // TODO - assert!(true); - }); + #[test] + fn can_deposit_withdraw_works() { + $ext_builder::default().build_and_execute_with(|| { + // TODO + assert!(true); + }); + } + }; } + +fungible_inspect_conformance_tests!(ExtBuilder, Balances); From c8b9e808b18a10a0ce7e913a77f1bfa5d8253740 Mon Sep 17 00:00:00 2001 From: Liam Aharon Date: Tue, 28 Mar 2023 16:51:04 +0400 Subject: [PATCH 04/37] first run of mutate tests --- .../fungible_conformance_tests/mutate.rs | 267 +++++++++++++++++- 1 file changed, 264 insertions(+), 3 deletions(-) diff --git a/frame/balances/src/tests/fungible_conformance_tests/mutate.rs b/frame/balances/src/tests/fungible_conformance_tests/mutate.rs index ca0c68b685df9..f3c918081b1a0 100644 --- a/frame/balances/src/tests/fungible_conformance_tests/mutate.rs +++ b/frame/balances/src/tests/fungible_conformance_tests/mutate.rs @@ -1,4 +1,265 @@ -#[test] -fn stub() { - assert!(true); +// This file is part of Substrate. + +// Copyright (C) 2017-2023 Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: Apache-2.0 + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//! Conformance tests regarding the functionality of the `fungible` Mutate trait. +//! +//! TODO: Decouple these tests from balances and abstract them into a macro so that they can be +//! used by any pallet that implements the `fungible` Mutate trait. + +// TODO: is making an assumption about existential deposits valid for the `fungible` trait? +// It is referenced in passing in some comments, but there are no default implementations +// that make use of it. + +macro_rules! fungible_mutate_conformance_tests { + ($ext_builder:ident, $pallet:ident) => { + use super::*; + use frame_support::traits::tokens::{ + Fortitude::{self}, + Precision, + Preservation::{self}, + }; + use fungible::Mutate; + + #[test] + // TODO: Is this test required? We already have a test for `total_issuance` in the inspect + // tests. + fn mint_into_increases_total_issuance() { + $ext_builder::default().build_and_execute_with(|| { + assert_eq!($pallet::total_issuance(), 0); + assert_ok!($pallet::mint_into(&1, 100)); + assert_eq!($pallet::total_issuance(), 100); + assert_ok!($pallet::mint_into(&2, 100)); + assert_eq!($pallet::total_issuance(), 200); + assert_ok!($pallet::mint_into(&2, 1)); + assert_eq!($pallet::total_issuance(), 201); + }); + } + + #[test] + fn mint_into_increases_account_balance() { + $ext_builder::default().build_and_execute_with(|| { + assert_eq!($pallet::free_balance(&1), 0); + assert_ok!($pallet::mint_into(&1, 100)); + assert_eq!($pallet::free_balance(&1), 100); + assert_ok!($pallet::mint_into(&2, 100)); + assert_eq!($pallet::free_balance(&2), 100); + assert_ok!($pallet::mint_into(&2, 1)); + assert_eq!($pallet::free_balance(&2), 101); + }); + } + + #[test] + fn mint_into_callback_works() { + // TODO: What is the best way to test that `done_mint_into` is called with the correct + // parameters? + } + + #[test] + fn mint_into_returns_actual() { + $ext_builder::default().build_and_execute_with(|| { + // TODO: Does this test make too many assumptions about the implementing pallet? + // e.g. what if they have a minimum mint amount of 100? or 1_000_000? What if there + // is a maximum minting amount? These tests would fail, even though the pallet is + // still conforming. + assert_eq!($pallet::mint_into(&1, 1).unwrap(), 1); + assert_eq!($pallet::mint_into(&1, 100_000).unwrap(), 100_000); + assert_eq!($pallet::mint_into(&1, 0).unwrap(), 0); + }); + } + + // TODO: Is this test required? We already have a test for `total_issuance` in the inspect + // tests. + #[test] + fn burn_from_reduces_total_issuance() { + $ext_builder::default().build_and_execute_with(|| { + assert_ok!($pallet::mint_into(&1, 100)); + assert_eq!($pallet::total_issuance(), 100); + assert_ok!($pallet::burn_from(&1, 50, Precision::BestEffort, Fortitude::Polite)); + assert_eq!($pallet::total_issuance(), 50); + assert_ok!($pallet::burn_from(&1, 50, Precision::BestEffort, Fortitude::Polite)); + assert_eq!($pallet::total_issuance(), 0); + }); + } + + #[test] + fn burn_from_reduces_account_balance() { + $ext_builder::default().build_and_execute_with(|| { + assert_ok!($pallet::mint_into(&1, 100)); + assert_eq!($pallet::free_balance(&1), 100); + assert_ok!($pallet::burn_from(&1, 50, Precision::BestEffort, Fortitude::Polite)); + assert_eq!($pallet::free_balance(&1), 50); + assert_ok!($pallet::burn_from(&1, 0, Precision::BestEffort, Fortitude::Polite)); + assert_eq!($pallet::free_balance(&1), 50); + assert_ok!($pallet::burn_from(&1, 50, Precision::BestEffort, Fortitude::Polite)); + assert_eq!($pallet::free_balance(&1), 0); + }); + } + + #[test] + fn burn_from_returns_actual() { + $ext_builder::default().build_and_execute_with(|| { + assert_ok!($pallet::mint_into(&1, 100)); + assert_eq!( + $pallet::burn_from(&1, 1, Precision::BestEffort, Fortitude::Polite).unwrap(), + 1 + ); + assert_eq!( + $pallet::burn_from(&1, 0, Precision::BestEffort, Fortitude::Polite).unwrap(), + 0 + ); + assert_eq!( + $pallet::burn_from(&1, 100_000, Precision::BestEffort, Fortitude::Polite) + .unwrap(), + 99 + ); + }); + } + + #[test] + fn burn_from_precision_best_effort_works() { + $ext_builder::default().existential_deposit(10).build_and_execute_with(|| { + assert_ok!($pallet::mint_into(&1, 100)); + assert_eq!( + $pallet::burn_from(&1, 101, Precision::BestEffort, Fortitude::Polite), + Ok(100) + ); + assert_eq!($pallet::free_balance(&1), 0); + }); + } + + #[test] + fn burn_from_precision_exact_works() { + $ext_builder::default().existential_deposit(10).build_and_execute_with(|| { + assert_ok!($pallet::mint_into(&1, 100)); + assert_noop!( + $pallet::burn_from(&1, 101, Precision::Exact, Fortitude::Polite), + // TODO: Is it making too much of an assumption that the implementing pallet + // should implement the same Dispatch errors as in the default implementation? + DispatchError::Token(TokenError::FundsUnavailable) + ); + }); + } + + // Note regarding the lack of Fortitude tests: I don't think we can test them in a general + // way, because we cannot make assumptions about the inner workings of reducible_balance. + + #[test] + fn burn_from_callback_works() { + // TODO: What is the best way to test that `done_burn_from` is called with the + // correct parameters? + } + + // Note regarding shelve/restore tests: I'm unsure what `suspend`/`resume` refer to or + // how these methods are different to calling mint_into/burn_from. + + #[test] + fn transfer_fails_insufficient_funds() { + $ext_builder::default().existential_deposit(10).build_and_execute_with(|| { + assert_ok!($pallet::mint_into(&1, 100)); + assert_noop!( + <$pallet as fungible::Mutate<_>>::transfer(&1, &2, 101, Preservation::Protect), + DispatchError::Arithmetic(ArithmeticError::Underflow) + ); + }); + } + + #[test] + fn transfer_fails_existential_deposit_requirement() { + $ext_builder::default().existential_deposit(10).build_and_execute_with(|| { + assert_ok!($pallet::mint_into(&1, 100)); + assert_noop!( + <$pallet as fungible::Mutate<_>>::transfer(&1, &2, 5, Preservation::Protect), + DispatchError::Token(TokenError::BelowMinimum) + ); + }); + } + + // TODO: Unclear exactly what the difference is between Protect and Preserve, or if they + // make sense without making assumptions about the implementing pallet. + + #[test] + fn transfer_fails_preservation_protect() { + $ext_builder::default().existential_deposit(10).build_and_execute_with(|| { + assert_ok!($pallet::mint_into(&1, 100)); + assert_noop!( + <$pallet as fungible::Mutate<_>>::transfer(&1, &2, 95, Preservation::Protect), + DispatchError::Token(TokenError::NotExpendable) + ); + }); + } + + #[test] + fn transfer_fails_preservation_preserve() { + $ext_builder::default().existential_deposit(10).build_and_execute_with(|| { + assert_ok!($pallet::mint_into(&1, 100)); + assert_noop!( + <$pallet as fungible::Mutate<_>>::transfer(&1, &2, 95, Preservation::Preserve), + DispatchError::Token(TokenError::NotExpendable) + ); + }); + } + + #[test] + fn transfer_fails_balance_below_minimum() { + $ext_builder::default().existential_deposit(100).build_and_execute_with(|| { + assert_ok!($pallet::mint_into(&1, 1000)); + assert_noop!( + <$pallet as fungible::Mutate<_>>::transfer( + &1, + &2, + 50, + Preservation::Expendable + ), + DispatchError::Token(TokenError::BelowMinimum) + ); + }); + } + + #[test] + fn transfer_to_exisitng_account_updates_balances() { + $ext_builder::default().build_and_execute_with(|| { + assert_ok!($pallet::mint_into(&1, 100)); + assert_ok!($pallet::mint_into(&2, 100)); + assert_ok!(<$pallet as fungible::Mutate<_>>::transfer( + &1, + &2, + 25, + Preservation::Expendable + )); + assert_eq!($pallet::free_balance(&1), 75); + assert_eq!($pallet::free_balance(&2), 125); + }); + } + + #[test] + fn transfer_to_new_account_updates_balances() { + $ext_builder::default().build_and_execute_with(|| { + assert_ok!($pallet::mint_into(&1, 100)); + assert_ok!(<$pallet as fungible::Mutate<_>>::transfer( + &1, + &2, + 25, + Preservation::Expendable + )); + assert_eq!($pallet::free_balance(&1), 75); + assert_eq!($pallet::free_balance(&2), 25); + }); + } + }; } + +fungible_mutate_conformance_tests!(ExtBuilder, Balances); From 9f553fd34e822d023f7cc881173189aa946588af Mon Sep 17 00:00:00 2001 From: Liam Aharon Date: Tue, 28 Mar 2023 17:23:56 +0400 Subject: [PATCH 05/37] move test implementation out of ballances --- .../src/tests/fungible_conformance_tests.rs | 22 +++++++++++++++++++ frame/balances/src/tests/fungible_tests.rs | 2 ++ .../fungible/conformance_tests}/balanced.rs | 0 .../conformance_tests}/balanced_hold.rs | 0 .../handle_imbalance_drop.rs | 0 .../fungible/conformance_tests}/inspect.rs | 12 +++++----- .../conformance_tests}/inspect_freeze.rs | 0 .../conformance_tests}/inspect_hold.rs | 0 .../tokens/fungible/conformance_tests}/mod.rs | 13 +++++------ .../fungible/conformance_tests}/mutate.rs | 6 ++--- .../conformance_tests}/mutate_freeze.rs | 0 .../conformance_tests}/mutate_hold.rs | 0 .../support/src/traits/tokens/fungible/mod.rs | 1 + 13 files changed, 39 insertions(+), 17 deletions(-) create mode 100644 frame/balances/src/tests/fungible_conformance_tests.rs rename frame/{balances/src/tests/fungible_conformance_tests => support/src/traits/tokens/fungible/conformance_tests}/balanced.rs (100%) rename frame/{balances/src/tests/fungible_conformance_tests => support/src/traits/tokens/fungible/conformance_tests}/balanced_hold.rs (100%) rename frame/{balances/src/tests/fungible_conformance_tests => support/src/traits/tokens/fungible/conformance_tests}/handle_imbalance_drop.rs (100%) rename frame/{balances/src/tests/fungible_conformance_tests => support/src/traits/tokens/fungible/conformance_tests}/inspect.rs (92%) rename frame/{balances/src/tests/fungible_conformance_tests => support/src/traits/tokens/fungible/conformance_tests}/inspect_freeze.rs (100%) rename frame/{balances/src/tests/fungible_conformance_tests => support/src/traits/tokens/fungible/conformance_tests}/inspect_hold.rs (100%) rename frame/{balances/src/tests/fungible_conformance_tests => support/src/traits/tokens/fungible/conformance_tests}/mod.rs (77%) rename frame/{balances/src/tests/fungible_conformance_tests => support/src/traits/tokens/fungible/conformance_tests}/mutate.rs (98%) rename frame/{balances/src/tests/fungible_conformance_tests => support/src/traits/tokens/fungible/conformance_tests}/mutate_freeze.rs (100%) rename frame/{balances/src/tests/fungible_conformance_tests => support/src/traits/tokens/fungible/conformance_tests}/mutate_hold.rs (100%) diff --git a/frame/balances/src/tests/fungible_conformance_tests.rs b/frame/balances/src/tests/fungible_conformance_tests.rs new file mode 100644 index 0000000000000..32513913d2c32 --- /dev/null +++ b/frame/balances/src/tests/fungible_conformance_tests.rs @@ -0,0 +1,22 @@ +// This file is part of Substrate. + +// Copyright (C) 2017-2023 Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: Apache-2.0 + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use super::*; +use frame_support::{inspect_conformance_tests, mutate_conformance_tests}; + +inspect_conformance_tests!(ExtBuilder, Balances); +mutate_conformance_tests!(ExtBuilder, Balances); diff --git a/frame/balances/src/tests/fungible_tests.rs b/frame/balances/src/tests/fungible_tests.rs index 128086885391f..0d12dd7ce0b15 100644 --- a/frame/balances/src/tests/fungible_tests.rs +++ b/frame/balances/src/tests/fungible_tests.rs @@ -16,6 +16,8 @@ // limitations under the License. //! Tests regarding the functionality of the `fungible` trait set implementations. +//! +//! TODO: Bundle these tests into the conformance tests & remove them. use super::*; use frame_support::traits::tokens::{ diff --git a/frame/balances/src/tests/fungible_conformance_tests/balanced.rs b/frame/support/src/traits/tokens/fungible/conformance_tests/balanced.rs similarity index 100% rename from frame/balances/src/tests/fungible_conformance_tests/balanced.rs rename to frame/support/src/traits/tokens/fungible/conformance_tests/balanced.rs diff --git a/frame/balances/src/tests/fungible_conformance_tests/balanced_hold.rs b/frame/support/src/traits/tokens/fungible/conformance_tests/balanced_hold.rs similarity index 100% rename from frame/balances/src/tests/fungible_conformance_tests/balanced_hold.rs rename to frame/support/src/traits/tokens/fungible/conformance_tests/balanced_hold.rs diff --git a/frame/balances/src/tests/fungible_conformance_tests/handle_imbalance_drop.rs b/frame/support/src/traits/tokens/fungible/conformance_tests/handle_imbalance_drop.rs similarity index 100% rename from frame/balances/src/tests/fungible_conformance_tests/handle_imbalance_drop.rs rename to frame/support/src/traits/tokens/fungible/conformance_tests/handle_imbalance_drop.rs diff --git a/frame/balances/src/tests/fungible_conformance_tests/inspect.rs b/frame/support/src/traits/tokens/fungible/conformance_tests/inspect.rs similarity index 92% rename from frame/balances/src/tests/fungible_conformance_tests/inspect.rs rename to frame/support/src/traits/tokens/fungible/conformance_tests/inspect.rs index c0a2deda9295a..f73ff28abf61a 100644 --- a/frame/balances/src/tests/fungible_conformance_tests/inspect.rs +++ b/frame/support/src/traits/tokens/fungible/conformance_tests/inspect.rs @@ -15,12 +15,14 @@ // See the License for the specific language governing permissions and // limitations under the License. -//! Conformance tests regarding the functionality of the `fungible` Inspect trait. +//! Conformance tests checking functionality of the `fungible` Inspect trait. //! -//! TODO: Decouple these tests from balances and abstract them into a macro so that they can be -//! used by any pallet that implements the `fungible` Inspect trait. +//! TODO: Feels like these can be just rolled up into Mutable trait tests, cannot test Inspect +//! without Mutate being avaliable, and cannot think of a realistic scenario a pallet would +//! implement Inspect without Mutate. -macro_rules! fungible_inspect_conformance_tests { +#[macro_export] +macro_rules! inspect_conformance_tests { ($ext_builder:ident, $pallet:ident) => { use super::*; use frame_support::traits::tokens::{ @@ -135,5 +137,3 @@ macro_rules! fungible_inspect_conformance_tests { } }; } - -fungible_inspect_conformance_tests!(ExtBuilder, Balances); diff --git a/frame/balances/src/tests/fungible_conformance_tests/inspect_freeze.rs b/frame/support/src/traits/tokens/fungible/conformance_tests/inspect_freeze.rs similarity index 100% rename from frame/balances/src/tests/fungible_conformance_tests/inspect_freeze.rs rename to frame/support/src/traits/tokens/fungible/conformance_tests/inspect_freeze.rs diff --git a/frame/balances/src/tests/fungible_conformance_tests/inspect_hold.rs b/frame/support/src/traits/tokens/fungible/conformance_tests/inspect_hold.rs similarity index 100% rename from frame/balances/src/tests/fungible_conformance_tests/inspect_hold.rs rename to frame/support/src/traits/tokens/fungible/conformance_tests/inspect_hold.rs diff --git a/frame/balances/src/tests/fungible_conformance_tests/mod.rs b/frame/support/src/traits/tokens/fungible/conformance_tests/mod.rs similarity index 77% rename from frame/balances/src/tests/fungible_conformance_tests/mod.rs rename to frame/support/src/traits/tokens/fungible/conformance_tests/mod.rs index dad5a604653d8..e39006e95f548 100644 --- a/frame/balances/src/tests/fungible_conformance_tests/mod.rs +++ b/frame/support/src/traits/tokens/fungible/conformance_tests/mod.rs @@ -6,13 +6,12 @@ /// used by any pallet that implements any `fungible` traits. /// /// Open question: Do we need conformance tests for 'unbalanced' traits? -use super::*; -mod balanced; -mod balanced_hold; -mod handle_imbalance_drop; -mod inspect; -mod inspect_freeze; -mod inspect_hold; +pub mod balanced; +pub mod balanced_hold; +pub mod handle_imbalance_drop; +pub mod inspect; +pub mod inspect_freeze; +pub mod inspect_hold; pub mod mutate; pub mod mutate_freeze; pub mod mutate_hold; diff --git a/frame/balances/src/tests/fungible_conformance_tests/mutate.rs b/frame/support/src/traits/tokens/fungible/conformance_tests/mutate.rs similarity index 98% rename from frame/balances/src/tests/fungible_conformance_tests/mutate.rs rename to frame/support/src/traits/tokens/fungible/conformance_tests/mutate.rs index f3c918081b1a0..2d0810d159860 100644 --- a/frame/balances/src/tests/fungible_conformance_tests/mutate.rs +++ b/frame/support/src/traits/tokens/fungible/conformance_tests/mutate.rs @@ -24,7 +24,8 @@ // It is referenced in passing in some comments, but there are no default implementations // that make use of it. -macro_rules! fungible_mutate_conformance_tests { +#[macro_export] +macro_rules! mutate_conformance_tests { ($ext_builder:ident, $pallet:ident) => { use super::*; use frame_support::traits::tokens::{ @@ -32,7 +33,6 @@ macro_rules! fungible_mutate_conformance_tests { Precision, Preservation::{self}, }; - use fungible::Mutate; #[test] // TODO: Is this test required? We already have a test for `total_issuance` in the inspect @@ -261,5 +261,3 @@ macro_rules! fungible_mutate_conformance_tests { } }; } - -fungible_mutate_conformance_tests!(ExtBuilder, Balances); diff --git a/frame/balances/src/tests/fungible_conformance_tests/mutate_freeze.rs b/frame/support/src/traits/tokens/fungible/conformance_tests/mutate_freeze.rs similarity index 100% rename from frame/balances/src/tests/fungible_conformance_tests/mutate_freeze.rs rename to frame/support/src/traits/tokens/fungible/conformance_tests/mutate_freeze.rs diff --git a/frame/balances/src/tests/fungible_conformance_tests/mutate_hold.rs b/frame/support/src/traits/tokens/fungible/conformance_tests/mutate_hold.rs similarity index 100% rename from frame/balances/src/tests/fungible_conformance_tests/mutate_hold.rs rename to frame/support/src/traits/tokens/fungible/conformance_tests/mutate_hold.rs diff --git a/frame/support/src/traits/tokens/fungible/mod.rs b/frame/support/src/traits/tokens/fungible/mod.rs index 204b85ac29448..8ab63ad366f08 100644 --- a/frame/support/src/traits/tokens/fungible/mod.rs +++ b/frame/support/src/traits/tokens/fungible/mod.rs @@ -38,6 +38,7 @@ //! funds must be removed from an account before it is known precisely what should be done with //! them. +pub mod conformance_tests; pub mod freeze; pub mod hold; mod imbalance; From bdf0020b3e1b8ae22d984e8afd7e681f4c455b9d Mon Sep 17 00:00:00 2001 From: Liam Aharon Date: Fri, 7 Apr 2023 08:49:41 +0400 Subject: [PATCH 06/37] make tests more generic --- .../src/tests/fungible_conformance_tests.rs | 126 ++++- .../conformance_tests/balanced_hold.rs | 4 - .../handle_imbalance_drop.rs | 4 - .../fungible/conformance_tests/inspect.rs | 139 ----- .../conformance_tests/inspect_freeze.rs | 4 - .../conformance_tests/inspect_hold.rs | 4 - .../tokens/fungible/conformance_tests/mod.rs | 10 - .../fungible/conformance_tests/mutate.rs | 490 +++++++++--------- .../conformance_tests/mutate_freeze.rs | 4 - .../fungible/conformance_tests/mutate_hold.rs | 4 - 10 files changed, 368 insertions(+), 421 deletions(-) delete mode 100644 frame/support/src/traits/tokens/fungible/conformance_tests/balanced_hold.rs delete mode 100644 frame/support/src/traits/tokens/fungible/conformance_tests/handle_imbalance_drop.rs delete mode 100644 frame/support/src/traits/tokens/fungible/conformance_tests/inspect.rs delete mode 100644 frame/support/src/traits/tokens/fungible/conformance_tests/inspect_freeze.rs delete mode 100644 frame/support/src/traits/tokens/fungible/conformance_tests/inspect_hold.rs delete mode 100644 frame/support/src/traits/tokens/fungible/conformance_tests/mutate_freeze.rs delete mode 100644 frame/support/src/traits/tokens/fungible/conformance_tests/mutate_hold.rs diff --git a/frame/balances/src/tests/fungible_conformance_tests.rs b/frame/balances/src/tests/fungible_conformance_tests.rs index 32513913d2c32..ca9838fb21e3a 100644 --- a/frame/balances/src/tests/fungible_conformance_tests.rs +++ b/frame/balances/src/tests/fungible_conformance_tests.rs @@ -16,7 +16,127 @@ // limitations under the License. use super::*; -use frame_support::{inspect_conformance_tests, mutate_conformance_tests}; +use frame_support::traits::fungible::conformance_tests; -inspect_conformance_tests!(ExtBuilder, Balances); -mutate_conformance_tests!(ExtBuilder, Balances); +// TODO: Add a macro to the conformance tests to generate all of these tests + +#[test] +fn mint_into_success() { + ExtBuilder::default().build_and_execute_with(|| { + conformance_tests::mutate::mint_into_success::< + Balances, + ::AccountId, + ::Balance, + >(); + }); +} + +#[test] +fn mint_into_overflow() { + ExtBuilder::default().build_and_execute_with(|| { + conformance_tests::mutate::mint_into_overflow::< + Balances, + ::AccountId, + ::Balance, + >(); + }); +} + +#[test] +fn mint_into_done_mint_into() { + ExtBuilder::default().build_and_execute_with(|| { + conformance_tests::mutate::mint_into_done_mint_into::< + Balances, + ::AccountId, + ::Balance, + >(); + }); +} + +#[test] +fn burn_from_exact_success() { + ExtBuilder::default().build_and_execute_with(|| { + conformance_tests::mutate::burn_from_exact_success::< + Balances, + ::AccountId, + ::Balance, + >(); + }); +} + +#[test] +fn burn_from_best_effort_success() { + ExtBuilder::default().build_and_execute_with(|| { + conformance_tests::mutate::burn_from_best_effort_success::< + Balances, + ::AccountId, + ::Balance, + >(); + }); +} + +#[test] +fn burn_from_exact_insufficient_funds() { + ExtBuilder::default().build_and_execute_with(|| { + conformance_tests::mutate::burn_from_exact_insufficient_funds::< + Balances, + ::AccountId, + ::Balance, + >(); + }); +} + +#[test] +fn restore_success() { + ExtBuilder::default().build_and_execute_with(|| { + conformance_tests::mutate::restore_success::< + Balances, + ::AccountId, + ::Balance, + >(); + }); +} + +#[test] +fn restore_overflow() { + ExtBuilder::default().build_and_execute_with(|| { + conformance_tests::mutate::restore_overflow::< + Balances, + ::AccountId, + ::Balance, + >(); + }); +} + +#[test] +fn restore_done_restore() { + ExtBuilder::default().build_and_execute_with(|| { + conformance_tests::mutate::restore_done_restore::< + Balances, + ::AccountId, + ::Balance, + >(); + }); +} + +#[test] +fn shelve_success() { + ExtBuilder::default().build_and_execute_with(|| { + conformance_tests::mutate::shelve_success::< + Balances, + ::AccountId, + ::Balance, + >(); + }); +} + +#[test] +fn shelve_insufficient_funds() { + ExtBuilder::default().build_and_execute_with(|| { + conformance_tests::mutate::shelve_insufficient_funds::< + Balances, + ::AccountId, + ::Balance, + >(); + }); +} diff --git a/frame/support/src/traits/tokens/fungible/conformance_tests/balanced_hold.rs b/frame/support/src/traits/tokens/fungible/conformance_tests/balanced_hold.rs deleted file mode 100644 index ca0c68b685df9..0000000000000 --- a/frame/support/src/traits/tokens/fungible/conformance_tests/balanced_hold.rs +++ /dev/null @@ -1,4 +0,0 @@ -#[test] -fn stub() { - assert!(true); -} diff --git a/frame/support/src/traits/tokens/fungible/conformance_tests/handle_imbalance_drop.rs b/frame/support/src/traits/tokens/fungible/conformance_tests/handle_imbalance_drop.rs deleted file mode 100644 index ca0c68b685df9..0000000000000 --- a/frame/support/src/traits/tokens/fungible/conformance_tests/handle_imbalance_drop.rs +++ /dev/null @@ -1,4 +0,0 @@ -#[test] -fn stub() { - assert!(true); -} diff --git a/frame/support/src/traits/tokens/fungible/conformance_tests/inspect.rs b/frame/support/src/traits/tokens/fungible/conformance_tests/inspect.rs deleted file mode 100644 index f73ff28abf61a..0000000000000 --- a/frame/support/src/traits/tokens/fungible/conformance_tests/inspect.rs +++ /dev/null @@ -1,139 +0,0 @@ -// This file is part of Substrate. - -// Copyright (C) 2017-2023 Parity Technologies (UK) Ltd. -// SPDX-License-Identifier: Apache-2.0 - -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -//! Conformance tests checking functionality of the `fungible` Inspect trait. -//! -//! TODO: Feels like these can be just rolled up into Mutable trait tests, cannot test Inspect -//! without Mutate being avaliable, and cannot think of a realistic scenario a pallet would -//! implement Inspect without Mutate. - -#[macro_export] -macro_rules! inspect_conformance_tests { - ($ext_builder:ident, $pallet:ident) => { - use super::*; - use frame_support::traits::tokens::{ - Fortitude::{Force, Polite}, - Preservation::{Expendable, Preserve, Protect}, - }; - use fungible::{Inspect, Mutate, MutateFreeze}; - - #[test] - fn total_issuance_works() { - $ext_builder::default().build_and_execute_with(|| { - assert_eq!($pallet::total_issuance(), 0); - $pallet::set_balance(&1, 100); - assert_eq!($pallet::total_issuance(), 100); - $pallet::set_balance(&2, 100); - assert_eq!($pallet::total_issuance(), 200); - $pallet::set_balance(&2, 101); - assert_eq!($pallet::total_issuance(), 201); - }); - } - - #[test] - fn active_issuance_works() { - $ext_builder::default().existential_deposit(10).build_and_execute_with(|| { - // TODO: Is there an official definition for what it means for issuance to be - // 'controlled by the system'? If there is no offical definition, then I think it - // cannot be tested in a generic way and we should remove this test. - assert!(true); - }); - } - - #[test] - fn minimum_balance_works() { - $ext_builder::default().existential_deposit(5).build_and_execute_with(|| { - assert_eq!($pallet::minimum_balance(), 5); - }); - $ext_builder::default().existential_deposit(10).build_and_execute_with(|| { - assert_eq!($pallet::minimum_balance(), 10); - }); - } - - #[test] - fn total_balance_works() { - $ext_builder::default().existential_deposit(10).build_and_execute_with(|| { - assert_eq!($pallet::total_balance(&1), 0); - $pallet::set_balance(&1, 100); - assert_eq!($pallet::total_balance(&1), 100); - $pallet::set_balance(&2, 100); - assert_eq!($pallet::total_balance(&2), 100); - $pallet::set_balance(&2, 101); - assert_eq!($pallet::total_balance(&2), 101); - }); - } - - // TODO: Properly understand Preservation and Fortitude enums, and ensure every permutation - // is thoroughly tested. - #[test] - fn reducible_balance_basic_works() { - $ext_builder::default().existential_deposit(10).build_and_execute_with(|| { - $pallet::set_balance(&1, 100); - assert_eq!($pallet::reducible_balance(&1, Expendable, Polite), 100); - assert_eq!($pallet::reducible_balance(&1, Protect, Polite), 90); - assert_eq!($pallet::reducible_balance(&1, Preserve, Polite), 90); - assert_eq!($pallet::reducible_balance(&1, Expendable, Force), 100); - assert_eq!($pallet::reducible_balance(&1, Protect, Force), 90); - assert_eq!($pallet::reducible_balance(&1, Preserve, Force), 90); - }); - } - - #[test] - fn reducible_balance_other_provide_works() { - $ext_builder::default().existential_deposit(10).build_and_execute_with(|| { - $pallet::set_balance(&1, 100); - System::inc_providers(&1); - assert_eq!($pallet::reducible_balance(&1, Expendable, Polite), 100); - assert_eq!($pallet::reducible_balance(&1, Protect, Polite), 100); - assert_eq!($pallet::reducible_balance(&1, Preserve, Polite), 90); - assert_eq!($pallet::reducible_balance(&1, Expendable, Force), 100); - assert_eq!($pallet::reducible_balance(&1, Protect, Force), 100); - assert_eq!($pallet::reducible_balance(&1, Preserve, Force), 90); - }); - } - - #[test] - fn reducible_balance_frozen_works() { - $ext_builder::default().existential_deposit(10).build_and_execute_with(|| { - $pallet::set_balance(&1, 100); - assert_ok!($pallet::set_freeze(&TestId::Foo, &1, 50)); - assert_eq!($pallet::reducible_balance(&1, Expendable, Polite), 50); - assert_eq!($pallet::reducible_balance(&1, Protect, Polite), 50); - assert_eq!($pallet::reducible_balance(&1, Preserve, Polite), 50); - assert_eq!($pallet::reducible_balance(&1, Expendable, Force), 90); - assert_eq!($pallet::reducible_balance(&1, Protect, Force), 90); - assert_eq!($pallet::reducible_balance(&1, Preserve, Force), 90); - }); - } - - #[test] - fn can_deposit_works() { - $ext_builder::default().build_and_execute_with(|| { - // TODO - assert!(true); - }); - } - - #[test] - fn can_deposit_withdraw_works() { - $ext_builder::default().build_and_execute_with(|| { - // TODO - assert!(true); - }); - } - }; -} diff --git a/frame/support/src/traits/tokens/fungible/conformance_tests/inspect_freeze.rs b/frame/support/src/traits/tokens/fungible/conformance_tests/inspect_freeze.rs deleted file mode 100644 index ca0c68b685df9..0000000000000 --- a/frame/support/src/traits/tokens/fungible/conformance_tests/inspect_freeze.rs +++ /dev/null @@ -1,4 +0,0 @@ -#[test] -fn stub() { - assert!(true); -} diff --git a/frame/support/src/traits/tokens/fungible/conformance_tests/inspect_hold.rs b/frame/support/src/traits/tokens/fungible/conformance_tests/inspect_hold.rs deleted file mode 100644 index ca0c68b685df9..0000000000000 --- a/frame/support/src/traits/tokens/fungible/conformance_tests/inspect_hold.rs +++ /dev/null @@ -1,4 +0,0 @@ -#[test] -fn stub() { - assert!(true); -} diff --git a/frame/support/src/traits/tokens/fungible/conformance_tests/mod.rs b/frame/support/src/traits/tokens/fungible/conformance_tests/mod.rs index e39006e95f548..8516b7183a407 100644 --- a/frame/support/src/traits/tokens/fungible/conformance_tests/mod.rs +++ b/frame/support/src/traits/tokens/fungible/conformance_tests/mod.rs @@ -2,16 +2,6 @@ /// until it is abstracted into pallet-agnostic macro/s and moved outside /// of the balances module. /// -/// TODO: Decouple these tests from balances and abstract them into a macro so that they can be -/// used by any pallet that implements any `fungible` traits. -/// /// Open question: Do we need conformance tests for 'unbalanced' traits? pub mod balanced; -pub mod balanced_hold; -pub mod handle_imbalance_drop; -pub mod inspect; -pub mod inspect_freeze; -pub mod inspect_hold; pub mod mutate; -pub mod mutate_freeze; -pub mod mutate_hold; diff --git a/frame/support/src/traits/tokens/fungible/conformance_tests/mutate.rs b/frame/support/src/traits/tokens/fungible/conformance_tests/mutate.rs index 2d0810d159860..4a2be241bf931 100644 --- a/frame/support/src/traits/tokens/fungible/conformance_tests/mutate.rs +++ b/frame/support/src/traits/tokens/fungible/conformance_tests/mutate.rs @@ -15,249 +15,249 @@ // See the License for the specific language governing permissions and // limitations under the License. -//! Conformance tests regarding the functionality of the `fungible` Mutate trait. -//! -//! TODO: Decouple these tests from balances and abstract them into a macro so that they can be -//! used by any pallet that implements the `fungible` Mutate trait. - -// TODO: is making an assumption about existential deposits valid for the `fungible` trait? -// It is referenced in passing in some comments, but there are no default implementations -// that make use of it. - -#[macro_export] -macro_rules! mutate_conformance_tests { - ($ext_builder:ident, $pallet:ident) => { - use super::*; - use frame_support::traits::tokens::{ - Fortitude::{self}, - Precision, - Preservation::{self}, - }; - - #[test] - // TODO: Is this test required? We already have a test for `total_issuance` in the inspect - // tests. - fn mint_into_increases_total_issuance() { - $ext_builder::default().build_and_execute_with(|| { - assert_eq!($pallet::total_issuance(), 0); - assert_ok!($pallet::mint_into(&1, 100)); - assert_eq!($pallet::total_issuance(), 100); - assert_ok!($pallet::mint_into(&2, 100)); - assert_eq!($pallet::total_issuance(), 200); - assert_ok!($pallet::mint_into(&2, 1)); - assert_eq!($pallet::total_issuance(), 201); - }); - } - - #[test] - fn mint_into_increases_account_balance() { - $ext_builder::default().build_and_execute_with(|| { - assert_eq!($pallet::free_balance(&1), 0); - assert_ok!($pallet::mint_into(&1, 100)); - assert_eq!($pallet::free_balance(&1), 100); - assert_ok!($pallet::mint_into(&2, 100)); - assert_eq!($pallet::free_balance(&2), 100); - assert_ok!($pallet::mint_into(&2, 1)); - assert_eq!($pallet::free_balance(&2), 101); - }); - } - - #[test] - fn mint_into_callback_works() { - // TODO: What is the best way to test that `done_mint_into` is called with the correct - // parameters? - } - - #[test] - fn mint_into_returns_actual() { - $ext_builder::default().build_and_execute_with(|| { - // TODO: Does this test make too many assumptions about the implementing pallet? - // e.g. what if they have a minimum mint amount of 100? or 1_000_000? What if there - // is a maximum minting amount? These tests would fail, even though the pallet is - // still conforming. - assert_eq!($pallet::mint_into(&1, 1).unwrap(), 1); - assert_eq!($pallet::mint_into(&1, 100_000).unwrap(), 100_000); - assert_eq!($pallet::mint_into(&1, 0).unwrap(), 0); - }); - } - - // TODO: Is this test required? We already have a test for `total_issuance` in the inspect - // tests. - #[test] - fn burn_from_reduces_total_issuance() { - $ext_builder::default().build_and_execute_with(|| { - assert_ok!($pallet::mint_into(&1, 100)); - assert_eq!($pallet::total_issuance(), 100); - assert_ok!($pallet::burn_from(&1, 50, Precision::BestEffort, Fortitude::Polite)); - assert_eq!($pallet::total_issuance(), 50); - assert_ok!($pallet::burn_from(&1, 50, Precision::BestEffort, Fortitude::Polite)); - assert_eq!($pallet::total_issuance(), 0); - }); - } - - #[test] - fn burn_from_reduces_account_balance() { - $ext_builder::default().build_and_execute_with(|| { - assert_ok!($pallet::mint_into(&1, 100)); - assert_eq!($pallet::free_balance(&1), 100); - assert_ok!($pallet::burn_from(&1, 50, Precision::BestEffort, Fortitude::Polite)); - assert_eq!($pallet::free_balance(&1), 50); - assert_ok!($pallet::burn_from(&1, 0, Precision::BestEffort, Fortitude::Polite)); - assert_eq!($pallet::free_balance(&1), 50); - assert_ok!($pallet::burn_from(&1, 50, Precision::BestEffort, Fortitude::Polite)); - assert_eq!($pallet::free_balance(&1), 0); - }); - } - - #[test] - fn burn_from_returns_actual() { - $ext_builder::default().build_and_execute_with(|| { - assert_ok!($pallet::mint_into(&1, 100)); - assert_eq!( - $pallet::burn_from(&1, 1, Precision::BestEffort, Fortitude::Polite).unwrap(), - 1 - ); - assert_eq!( - $pallet::burn_from(&1, 0, Precision::BestEffort, Fortitude::Polite).unwrap(), - 0 - ); - assert_eq!( - $pallet::burn_from(&1, 100_000, Precision::BestEffort, Fortitude::Polite) - .unwrap(), - 99 - ); - }); - } - - #[test] - fn burn_from_precision_best_effort_works() { - $ext_builder::default().existential_deposit(10).build_and_execute_with(|| { - assert_ok!($pallet::mint_into(&1, 100)); - assert_eq!( - $pallet::burn_from(&1, 101, Precision::BestEffort, Fortitude::Polite), - Ok(100) - ); - assert_eq!($pallet::free_balance(&1), 0); - }); - } - - #[test] - fn burn_from_precision_exact_works() { - $ext_builder::default().existential_deposit(10).build_and_execute_with(|| { - assert_ok!($pallet::mint_into(&1, 100)); - assert_noop!( - $pallet::burn_from(&1, 101, Precision::Exact, Fortitude::Polite), - // TODO: Is it making too much of an assumption that the implementing pallet - // should implement the same Dispatch errors as in the default implementation? - DispatchError::Token(TokenError::FundsUnavailable) - ); - }); - } - - // Note regarding the lack of Fortitude tests: I don't think we can test them in a general - // way, because we cannot make assumptions about the inner workings of reducible_balance. - - #[test] - fn burn_from_callback_works() { - // TODO: What is the best way to test that `done_burn_from` is called with the - // correct parameters? - } - - // Note regarding shelve/restore tests: I'm unsure what `suspend`/`resume` refer to or - // how these methods are different to calling mint_into/burn_from. - - #[test] - fn transfer_fails_insufficient_funds() { - $ext_builder::default().existential_deposit(10).build_and_execute_with(|| { - assert_ok!($pallet::mint_into(&1, 100)); - assert_noop!( - <$pallet as fungible::Mutate<_>>::transfer(&1, &2, 101, Preservation::Protect), - DispatchError::Arithmetic(ArithmeticError::Underflow) - ); - }); - } - - #[test] - fn transfer_fails_existential_deposit_requirement() { - $ext_builder::default().existential_deposit(10).build_and_execute_with(|| { - assert_ok!($pallet::mint_into(&1, 100)); - assert_noop!( - <$pallet as fungible::Mutate<_>>::transfer(&1, &2, 5, Preservation::Protect), - DispatchError::Token(TokenError::BelowMinimum) - ); - }); - } - - // TODO: Unclear exactly what the difference is between Protect and Preserve, or if they - // make sense without making assumptions about the implementing pallet. - - #[test] - fn transfer_fails_preservation_protect() { - $ext_builder::default().existential_deposit(10).build_and_execute_with(|| { - assert_ok!($pallet::mint_into(&1, 100)); - assert_noop!( - <$pallet as fungible::Mutate<_>>::transfer(&1, &2, 95, Preservation::Protect), - DispatchError::Token(TokenError::NotExpendable) - ); - }); - } - - #[test] - fn transfer_fails_preservation_preserve() { - $ext_builder::default().existential_deposit(10).build_and_execute_with(|| { - assert_ok!($pallet::mint_into(&1, 100)); - assert_noop!( - <$pallet as fungible::Mutate<_>>::transfer(&1, &2, 95, Preservation::Preserve), - DispatchError::Token(TokenError::NotExpendable) - ); - }); - } - - #[test] - fn transfer_fails_balance_below_minimum() { - $ext_builder::default().existential_deposit(100).build_and_execute_with(|| { - assert_ok!($pallet::mint_into(&1, 1000)); - assert_noop!( - <$pallet as fungible::Mutate<_>>::transfer( - &1, - &2, - 50, - Preservation::Expendable - ), - DispatchError::Token(TokenError::BelowMinimum) - ); - }); - } - - #[test] - fn transfer_to_exisitng_account_updates_balances() { - $ext_builder::default().build_and_execute_with(|| { - assert_ok!($pallet::mint_into(&1, 100)); - assert_ok!($pallet::mint_into(&2, 100)); - assert_ok!(<$pallet as fungible::Mutate<_>>::transfer( - &1, - &2, - 25, - Preservation::Expendable - )); - assert_eq!($pallet::free_balance(&1), 75); - assert_eq!($pallet::free_balance(&2), 125); - }); - } - - #[test] - fn transfer_to_new_account_updates_balances() { - $ext_builder::default().build_and_execute_with(|| { - assert_ok!($pallet::mint_into(&1, 100)); - assert_ok!(<$pallet as fungible::Mutate<_>>::transfer( - &1, - &2, - 25, - Preservation::Expendable - )); - assert_eq!($pallet::free_balance(&1), 75); - assert_eq!($pallet::free_balance(&2), 25); - }); - } - }; +use crate::traits::{ + fungible::{Inspect, Mutate}, + tokens::{Fortitude, Precision, Preservation}, +}; +use core::fmt::Debug; +use sp_arithmetic::traits::AtLeast8BitUnsigned; + +// +// mint_into tests +// + +pub fn mint_into_success() +where + T: Mutate + Inspect, + AccountId: AtLeast8BitUnsigned, + Balance: AtLeast8BitUnsigned + Debug, +{ + let account_0 = AccountId::from(0); + let account_1 = AccountId::from(1); + + // Sanity check that balances and total issuance start with zero balance + assert_eq!(T::total_balance(&account_0), Balance::zero()); + assert_eq!(T::total_balance(&account_1), Balance::zero()); + assert_eq!(T::total_issuance(), Balance::zero()); + + // Test: Mint an amount into each account + let amount_0 = T::minimum_balance(); + let amount_1 = T::minimum_balance() + 5.into(); + T::mint_into(&account_0, amount_0.clone()).unwrap(); + T::mint_into(&account_1, amount_1.clone()).unwrap(); + + // Verify: Account balances are updated correctly + assert_eq!(T::total_balance(&account_0), amount_0.clone()); + assert_eq!(T::total_balance(&account_1), amount_1.clone()); + + // Verify: Total issuance is updated correctly + assert_eq!(T::total_issuance(), amount_0 + amount_1); +} + +pub fn mint_into_overflow() +where + T: Mutate + Inspect, + AccountId: AtLeast8BitUnsigned, + Balance: AtLeast8BitUnsigned + Debug, +{ + let account = AccountId::from(10); + let amount = Balance::max_value() - 5.into(); + + // Mint just below the maximum balance + T::mint_into(&account, amount.clone()).unwrap(); + + // Test: Try minting beyond the maximum balance value + T::mint_into(&account, 10.into()).unwrap_err(); + + // Verify: The balance did not change + assert_eq!(T::total_balance(&account), amount.clone()); + + // Verify: The total issuance did not change + assert_eq!(T::total_issuance(), amount); +} + +pub fn mint_into_done_mint_into() +where + T: Mutate + Inspect, + AccountId: AtLeast8BitUnsigned, + Balance: AtLeast8BitUnsigned + Debug, +{ + // TODO: How can we test this? + assert!(true); +} + +// +// burn_from tests +// + +pub fn burn_from_exact_success() +where + T: Mutate + Inspect, + AccountId: AtLeast8BitUnsigned, + Balance: AtLeast8BitUnsigned + Debug, +{ + // Setup account + let account = AccountId::from(5); + let initial_balance = Balance::from(100); + T::mint_into(&account, initial_balance.clone()).unwrap(); + + // Test: Burn an exact amount from the account + let amount_to_burn = Balance::from(50); + let precision = Precision::Exact; + let force = Fortitude::Polite; + T::burn_from(&account, amount_to_burn.clone(), precision, force).unwrap(); + + // Verify: The balance and total issuance should be reduced by the burned amount + assert_eq!(T::balance(&account), initial_balance.clone() - amount_to_burn.clone()); + assert_eq!(T::total_issuance(), initial_balance - amount_to_burn); +} + +pub fn burn_from_best_effort_success() +where + T: Mutate + Inspect, + AccountId: AtLeast8BitUnsigned, + Balance: AtLeast8BitUnsigned + Debug, +{ + // Setup account + let account = AccountId::from(5); + let initial_balance = Balance::from(100); + T::mint_into(&account, initial_balance.clone()).unwrap(); + + // Get reducible balance + let force = Fortitude::Polite; + let reducible_balance = T::reducible_balance(&account, Preservation::Expendable, force); + + // Test: Burn a best effort amount from the account that is greater than the reducible balance + let amount_to_burn = initial_balance.clone() + 10.into(); + let precision = Precision::BestEffort; + assert!(amount_to_burn > reducible_balance); + assert!(amount_to_burn > T::balance(&account)); + T::burn_from(&account, amount_to_burn.clone(), precision, force).unwrap(); + + // Verify: The balance and total issuance should be reduced by the reducible_balance + assert_eq!(T::balance(&account), initial_balance.clone() - reducible_balance.clone()); + assert_eq!(T::total_issuance(), initial_balance - reducible_balance); +} + +pub fn burn_from_exact_insufficient_funds() +where + T: Mutate + Inspect, + AccountId: AtLeast8BitUnsigned, + Balance: AtLeast8BitUnsigned + Debug, +{ + // Set up the initial conditions and parameters for the test + let account = AccountId::from(5); + let initial_balance = Balance::from(100); + T::mint_into(&account, initial_balance.clone()).unwrap(); + + // Test: Attempt to burn an amount greater than the account's balance with Exact precision + let amount_to_burn = initial_balance.clone() + 10.into(); + let precision = Precision::Exact; + let force = Fortitude::Polite; + T::burn_from(&account, amount_to_burn, precision, force).unwrap_err(); + + // Verify: The balance and total issuance should remain unchanged + assert_eq!(T::balance(&account), initial_balance); + assert_eq!(T::total_issuance(), initial_balance); +} + +// +// restore tests +// + +pub fn restore_success() +where + T: Mutate + Inspect, + AccountId: AtLeast8BitUnsigned, + Balance: AtLeast8BitUnsigned + Debug, +{ + let account_0 = AccountId::from(0); + let account_1 = AccountId::from(1); + + // Test: Restore an amount into each account + let amount_0 = T::minimum_balance(); + let amount_1 = T::minimum_balance() + 5.into(); + T::restore(&account_0, amount_0.clone()).unwrap(); + T::restore(&account_1, amount_1.clone()).unwrap(); + + // Verify: Account balances are updated correctly + assert_eq!(T::total_balance(&account_0), amount_0.clone()); + assert_eq!(T::total_balance(&account_1), amount_1.clone()); + + // Verify: Total issuance is updated correctly + assert_eq!(T::total_issuance(), amount_0 + amount_1); +} + +pub fn restore_overflow() +where + T: Mutate + Inspect, + AccountId: AtLeast8BitUnsigned, + Balance: AtLeast8BitUnsigned + Debug, +{ + let account = AccountId::from(10); + let amount = Balance::max_value() - 5.into(); + + // Restore just below the maximum balance + T::restore(&account, amount.clone()).unwrap(); + + // Test: Try restoring beyond the maximum balance value + T::restore(&account, 10.into()).unwrap_err(); + + // Verify: The balance and total issuance did not change + assert_eq!(T::total_balance(&account), amount.clone()); + assert_eq!(T::total_issuance(), amount); +} + +pub fn restore_done_restore() +where + T: Mutate + Inspect, + AccountId: AtLeast8BitUnsigned, + Balance: AtLeast8BitUnsigned + Debug, +{ + // TODO: How can we test this? + assert!(true); +} + +// +// shelve tests +// + +pub fn shelve_success() +where + T: Mutate + Inspect, + AccountId: AtLeast8BitUnsigned, + Balance: AtLeast8BitUnsigned + Debug, +{ + // Setup account + let account = AccountId::from(5); + let initial_balance = Balance::from(100); + T::restore(&account, initial_balance.clone()).unwrap(); + + // Test: Shelve an amount from the account + let amount_to_shelve = Balance::from(50); + T::shelve(&account, amount_to_shelve.clone()).unwrap(); + + // Verify: The balance and total issuance should be reduced by the shelved amount + assert_eq!(T::balance(&account), initial_balance.clone() - amount_to_shelve.clone()); + assert_eq!(T::total_issuance(), initial_balance - amount_to_shelve); +} + +pub fn shelve_insufficient_funds() +where + T: Mutate + Inspect, + AccountId: AtLeast8BitUnsigned, + Balance: AtLeast8BitUnsigned + Debug, +{ + // Set up the initial conditions and parameters for the test + let account = AccountId::from(5); + let initial_balance = Balance::from(100); + T::restore(&account, initial_balance.clone()).unwrap(); + + // Test: Attempt to shelve an amount greater than the account's balance with Exact precision + let amount_to_shelve = initial_balance.clone() + 10.into(); + T::shelve(&account, amount_to_shelve).unwrap_err(); + + // Verify: The balance and total issuance should remain unchanged + assert_eq!(T::balance(&account), initial_balance); + assert_eq!(T::total_issuance(), initial_balance); } diff --git a/frame/support/src/traits/tokens/fungible/conformance_tests/mutate_freeze.rs b/frame/support/src/traits/tokens/fungible/conformance_tests/mutate_freeze.rs deleted file mode 100644 index ca0c68b685df9..0000000000000 --- a/frame/support/src/traits/tokens/fungible/conformance_tests/mutate_freeze.rs +++ /dev/null @@ -1,4 +0,0 @@ -#[test] -fn stub() { - assert!(true); -} diff --git a/frame/support/src/traits/tokens/fungible/conformance_tests/mutate_hold.rs b/frame/support/src/traits/tokens/fungible/conformance_tests/mutate_hold.rs deleted file mode 100644 index ca0c68b685df9..0000000000000 --- a/frame/support/src/traits/tokens/fungible/conformance_tests/mutate_hold.rs +++ /dev/null @@ -1,4 +0,0 @@ -#[test] -fn stub() { - assert!(true); -} From c1286cd25f6a19bf5b92f918abbfc3c17e6c08b0 Mon Sep 17 00:00:00 2001 From: Liam Aharon Date: Fri, 7 Apr 2023 09:19:38 +0400 Subject: [PATCH 07/37] transfer tests --- .../src/tests/fungible_conformance_tests.rs | 66 ++++++++ .../fungible/conformance_tests/mutate.rs | 153 +++++++++++++++++- 2 files changed, 212 insertions(+), 7 deletions(-) diff --git a/frame/balances/src/tests/fungible_conformance_tests.rs b/frame/balances/src/tests/fungible_conformance_tests.rs index ca9838fb21e3a..0fe0906af153e 100644 --- a/frame/balances/src/tests/fungible_conformance_tests.rs +++ b/frame/balances/src/tests/fungible_conformance_tests.rs @@ -42,6 +42,17 @@ fn mint_into_overflow() { }); } +#[test] +fn mint_into_below_minimum() { + ExtBuilder::default().build_and_execute_with(|| { + conformance_tests::mutate::mint_into_below_minimum::< + Balances, + ::AccountId, + ::Balance, + >(); + }); +} + #[test] fn mint_into_done_mint_into() { ExtBuilder::default().build_and_execute_with(|| { @@ -108,6 +119,17 @@ fn restore_overflow() { }); } +#[test] +fn restore_below_minimum() { + ExtBuilder::default().build_and_execute_with(|| { + conformance_tests::mutate::restore_below_minimum::< + Balances, + ::AccountId, + ::Balance, + >(); + }); +} + #[test] fn restore_done_restore() { ExtBuilder::default().build_and_execute_with(|| { @@ -140,3 +162,47 @@ fn shelve_insufficient_funds() { >(); }); } + +#[test] +fn shelve_done_shelve() { + ExtBuilder::default().build_and_execute_with(|| { + conformance_tests::mutate::shelve_done_shelve::< + Balances, + ::AccountId, + ::Balance, + >(); + }); +} + +#[test] +fn transfer_success() { + ExtBuilder::default().build_and_execute_with(|| { + conformance_tests::mutate::transfer_success::< + Balances, + ::AccountId, + ::Balance, + >(); + }); +} + +#[test] +fn transfer_expendable() { + ExtBuilder::default().build_and_execute_with(|| { + conformance_tests::mutate::transfer_expendable::< + Balances, + ::AccountId, + ::Balance, + >(); + }); +} + +#[test] +fn transfer_protect_preserve() { + ExtBuilder::default().build_and_execute_with(|| { + conformance_tests::mutate::transfer_protect_preserve::< + Balances, + ::AccountId, + ::Balance, + >(); + }); +} diff --git a/frame/support/src/traits/tokens/fungible/conformance_tests/mutate.rs b/frame/support/src/traits/tokens/fungible/conformance_tests/mutate.rs index 4a2be241bf931..b2e0b6242aac0 100644 --- a/frame/support/src/traits/tokens/fungible/conformance_tests/mutate.rs +++ b/frame/support/src/traits/tokens/fungible/conformance_tests/mutate.rs @@ -23,7 +23,7 @@ use core::fmt::Debug; use sp_arithmetic::traits::AtLeast8BitUnsigned; // -// mint_into tests +// mint_into // pub fn mint_into_success() @@ -66,7 +66,7 @@ where // Mint just below the maximum balance T::mint_into(&account, amount.clone()).unwrap(); - // Test: Try minting beyond the maximum balance value + // Verify: Minting beyond the maximum balance value returns an Err T::mint_into(&account, 10.into()).unwrap_err(); // Verify: The balance did not change @@ -76,6 +76,28 @@ where assert_eq!(T::total_issuance(), amount); } +pub fn mint_into_below_minimum() +where + T: Mutate + Inspect, + AccountId: AtLeast8BitUnsigned, + Balance: AtLeast8BitUnsigned + Debug, +{ + // Skip if there is no minimum balance + if T::minimum_balance() == Balance::zero() { + return + } + + let account = AccountId::from(10); + let amount = T::minimum_balance() - 1.into(); + + // Verify: Minting below the minimum balance returns Err + T::mint_into(&account, amount.clone()).unwrap_err(); + + // Verify: noop + assert_eq!(T::total_balance(&account), Balance::zero()); + assert_eq!(T::total_issuance(), Balance::zero()); +} + pub fn mint_into_done_mint_into() where T: Mutate + Inspect, @@ -87,7 +109,7 @@ where } // -// burn_from tests +// burn_from // pub fn burn_from_exact_success() @@ -150,7 +172,7 @@ where let initial_balance = Balance::from(100); T::mint_into(&account, initial_balance.clone()).unwrap(); - // Test: Attempt to burn an amount greater than the account's balance with Exact precision + // Verify: Burn an amount greater than the account's balance with Exact precision returns Err let amount_to_burn = initial_balance.clone() + 10.into(); let precision = Precision::Exact; let force = Fortitude::Polite; @@ -162,7 +184,7 @@ where } // -// restore tests +// restore // pub fn restore_success() @@ -200,7 +222,7 @@ where // Restore just below the maximum balance T::restore(&account, amount.clone()).unwrap(); - // Test: Try restoring beyond the maximum balance value + // Verify: Restoring beyond the maximum balance returns an Err T::restore(&account, 10.into()).unwrap_err(); // Verify: The balance and total issuance did not change @@ -208,6 +230,28 @@ where assert_eq!(T::total_issuance(), amount); } +pub fn restore_below_minimum() +where + T: Mutate + Inspect, + AccountId: AtLeast8BitUnsigned, + Balance: AtLeast8BitUnsigned + Debug, +{ + // Skip if there is no minimum balance + if T::minimum_balance() == Balance::zero() { + return + } + + let account = AccountId::from(10); + let amount = T::minimum_balance() - 1.into(); + + // Verify: Restoring below the minimum balance returns Err + T::restore(&account, amount.clone()).unwrap_err(); + + // Verify: noop + assert_eq!(T::total_balance(&account), Balance::zero()); + assert_eq!(T::total_issuance(), Balance::zero()); +} + pub fn restore_done_restore() where T: Mutate + Inspect, @@ -253,7 +297,7 @@ where let initial_balance = Balance::from(100); T::restore(&account, initial_balance.clone()).unwrap(); - // Test: Attempt to shelve an amount greater than the account's balance with Exact precision + // Verify: Shelving greater than the balance with Exact precision returns Err let amount_to_shelve = initial_balance.clone() + 10.into(); T::shelve(&account, amount_to_shelve).unwrap_err(); @@ -261,3 +305,98 @@ where assert_eq!(T::balance(&account), initial_balance); assert_eq!(T::total_issuance(), initial_balance); } + +pub fn shelve_done_shelve() +where + T: Mutate + Inspect, + AccountId: AtLeast8BitUnsigned, + Balance: AtLeast8BitUnsigned + Debug, +{ + // TODO: How can we test this? + assert!(true); +} + +// +// transfer +// + +pub fn transfer_success() +where + T: Mutate + Inspect, + AccountId: AtLeast8BitUnsigned, + Balance: AtLeast8BitUnsigned + Debug, +{ + let account_0 = AccountId::from(0); + let account_1 = AccountId::from(1); + let initial_balance = T::minimum_balance() + 10.into(); + T::set_balance(&account_0, initial_balance.clone()); + T::set_balance(&account_1, initial_balance.clone()); + + // Test: Transfer an amount from account_0 to account_1 + let transfer_amount = initial_balance.clone() - 3.into(); + T::transfer(&account_0, &account_1, transfer_amount.clone(), Preservation::Expendable).unwrap(); + + // Verify: Account balances are updated correctly + assert_eq!(T::total_balance(&account_0), initial_balance.clone() - transfer_amount.clone()); + assert_eq!(T::total_balance(&account_1), initial_balance.clone() + transfer_amount.clone()); + + // Verify: Total issuance doesn't change + assert_eq!(T::total_issuance(), initial_balance * 2.into()); +} + +pub fn transfer_expendable() +where + T: Mutate + Inspect, + AccountId: AtLeast8BitUnsigned, + Balance: AtLeast8BitUnsigned + Debug, +{ + let account_0 = AccountId::from(0); + let account_1 = AccountId::from(1); + let initial_balance = T::minimum_balance() + 10.into(); + T::set_balance(&account_0, initial_balance.clone()); + T::set_balance(&account_1, initial_balance.clone()); + + // Test: Transfer entire balance from account_0 to account_1 + let preservation = Preservation::Expendable; + let transfer_amount = initial_balance.clone(); + T::transfer(&account_0, &account_1, transfer_amount.clone(), preservation).unwrap(); + + // Verify: Account balances are updated correctly + assert_eq!(T::total_balance(&account_0), Balance::zero()); + assert_eq!(T::total_balance(&account_1), initial_balance.clone() * 2.into()); + + // Verify: Total issuance doesn't change + assert_eq!(T::total_issuance(), initial_balance * 2.into()); +} + +pub fn transfer_protect_preserve() +where + T: Mutate + Inspect, + AccountId: AtLeast8BitUnsigned, + Balance: AtLeast8BitUnsigned + Debug, +{ + let account_0 = AccountId::from(0); + let account_1 = AccountId::from(1); + let initial_balance = T::minimum_balance() + 10.into(); + T::set_balance(&account_0, initial_balance.clone()); + T::set_balance(&account_1, initial_balance.clone()); + + // Verify: Transfer Protect entire balance from account_0 to account_1 should Err + let preservation = Preservation::Protect; + let transfer_amount = initial_balance.clone(); + T::transfer(&account_0, &account_1, transfer_amount.clone(), preservation).unwrap_err(); + + // Verify: Noop + assert_eq!(T::total_balance(&account_0), initial_balance.clone()); + assert_eq!(T::total_balance(&account_1), initial_balance.clone()); + assert_eq!(T::total_issuance(), initial_balance.clone() * 2.into()); + + // Verify: Transfer Preserve entire balance from account_0 to account_1 should Err + let preservation = Preservation::Preserve; + T::transfer(&account_0, &account_1, transfer_amount.clone(), preservation).unwrap_err(); + + // Verify: Noop + assert_eq!(T::total_balance(&account_0), initial_balance.clone()); + assert_eq!(T::total_balance(&account_1), initial_balance.clone()); + assert_eq!(T::total_issuance(), initial_balance * 2.into()); +} From b1c7b2a9f674fdfc8fa4c82fb212ac0b2c0cccfb Mon Sep 17 00:00:00 2001 From: Liam Aharon Date: Fri, 7 Apr 2023 09:32:56 +0400 Subject: [PATCH 08/37] combine inspect and mutate tests --- .../src/tests/fungible_conformance_tests.rs | 34 +++++----- .../{mutate.rs => inspect_mutate.rs} | 62 ++++++++++++++++--- .../tokens/fungible/conformance_tests/mod.rs | 2 +- 3 files changed, 71 insertions(+), 27 deletions(-) rename frame/support/src/traits/tokens/fungible/conformance_tests/{mutate.rs => inspect_mutate.rs} (80%) diff --git a/frame/balances/src/tests/fungible_conformance_tests.rs b/frame/balances/src/tests/fungible_conformance_tests.rs index 0fe0906af153e..8e067c6b5048b 100644 --- a/frame/balances/src/tests/fungible_conformance_tests.rs +++ b/frame/balances/src/tests/fungible_conformance_tests.rs @@ -23,7 +23,7 @@ use frame_support::traits::fungible::conformance_tests; #[test] fn mint_into_success() { ExtBuilder::default().build_and_execute_with(|| { - conformance_tests::mutate::mint_into_success::< + conformance_tests::inspect_mutate::mint_into_success::< Balances, ::AccountId, ::Balance, @@ -34,7 +34,7 @@ fn mint_into_success() { #[test] fn mint_into_overflow() { ExtBuilder::default().build_and_execute_with(|| { - conformance_tests::mutate::mint_into_overflow::< + conformance_tests::inspect_mutate::mint_into_overflow::< Balances, ::AccountId, ::Balance, @@ -45,7 +45,7 @@ fn mint_into_overflow() { #[test] fn mint_into_below_minimum() { ExtBuilder::default().build_and_execute_with(|| { - conformance_tests::mutate::mint_into_below_minimum::< + conformance_tests::inspect_mutate::mint_into_below_minimum::< Balances, ::AccountId, ::Balance, @@ -56,7 +56,7 @@ fn mint_into_below_minimum() { #[test] fn mint_into_done_mint_into() { ExtBuilder::default().build_and_execute_with(|| { - conformance_tests::mutate::mint_into_done_mint_into::< + conformance_tests::inspect_mutate::mint_into_done_mint_into::< Balances, ::AccountId, ::Balance, @@ -67,7 +67,7 @@ fn mint_into_done_mint_into() { #[test] fn burn_from_exact_success() { ExtBuilder::default().build_and_execute_with(|| { - conformance_tests::mutate::burn_from_exact_success::< + conformance_tests::inspect_mutate::burn_from_exact_success::< Balances, ::AccountId, ::Balance, @@ -78,7 +78,7 @@ fn burn_from_exact_success() { #[test] fn burn_from_best_effort_success() { ExtBuilder::default().build_and_execute_with(|| { - conformance_tests::mutate::burn_from_best_effort_success::< + conformance_tests::inspect_mutate::burn_from_best_effort_success::< Balances, ::AccountId, ::Balance, @@ -89,7 +89,7 @@ fn burn_from_best_effort_success() { #[test] fn burn_from_exact_insufficient_funds() { ExtBuilder::default().build_and_execute_with(|| { - conformance_tests::mutate::burn_from_exact_insufficient_funds::< + conformance_tests::inspect_mutate::burn_from_exact_insufficient_funds::< Balances, ::AccountId, ::Balance, @@ -100,7 +100,7 @@ fn burn_from_exact_insufficient_funds() { #[test] fn restore_success() { ExtBuilder::default().build_and_execute_with(|| { - conformance_tests::mutate::restore_success::< + conformance_tests::inspect_mutate::restore_success::< Balances, ::AccountId, ::Balance, @@ -111,7 +111,7 @@ fn restore_success() { #[test] fn restore_overflow() { ExtBuilder::default().build_and_execute_with(|| { - conformance_tests::mutate::restore_overflow::< + conformance_tests::inspect_mutate::restore_overflow::< Balances, ::AccountId, ::Balance, @@ -122,7 +122,7 @@ fn restore_overflow() { #[test] fn restore_below_minimum() { ExtBuilder::default().build_and_execute_with(|| { - conformance_tests::mutate::restore_below_minimum::< + conformance_tests::inspect_mutate::restore_below_minimum::< Balances, ::AccountId, ::Balance, @@ -133,7 +133,7 @@ fn restore_below_minimum() { #[test] fn restore_done_restore() { ExtBuilder::default().build_and_execute_with(|| { - conformance_tests::mutate::restore_done_restore::< + conformance_tests::inspect_mutate::restore_done_restore::< Balances, ::AccountId, ::Balance, @@ -144,7 +144,7 @@ fn restore_done_restore() { #[test] fn shelve_success() { ExtBuilder::default().build_and_execute_with(|| { - conformance_tests::mutate::shelve_success::< + conformance_tests::inspect_mutate::shelve_success::< Balances, ::AccountId, ::Balance, @@ -155,7 +155,7 @@ fn shelve_success() { #[test] fn shelve_insufficient_funds() { ExtBuilder::default().build_and_execute_with(|| { - conformance_tests::mutate::shelve_insufficient_funds::< + conformance_tests::inspect_mutate::shelve_insufficient_funds::< Balances, ::AccountId, ::Balance, @@ -166,7 +166,7 @@ fn shelve_insufficient_funds() { #[test] fn shelve_done_shelve() { ExtBuilder::default().build_and_execute_with(|| { - conformance_tests::mutate::shelve_done_shelve::< + conformance_tests::inspect_mutate::shelve_done_shelve::< Balances, ::AccountId, ::Balance, @@ -177,7 +177,7 @@ fn shelve_done_shelve() { #[test] fn transfer_success() { ExtBuilder::default().build_and_execute_with(|| { - conformance_tests::mutate::transfer_success::< + conformance_tests::inspect_mutate::transfer_success::< Balances, ::AccountId, ::Balance, @@ -188,7 +188,7 @@ fn transfer_success() { #[test] fn transfer_expendable() { ExtBuilder::default().build_and_execute_with(|| { - conformance_tests::mutate::transfer_expendable::< + conformance_tests::inspect_mutate::transfer_expendable::< Balances, ::AccountId, ::Balance, @@ -199,7 +199,7 @@ fn transfer_expendable() { #[test] fn transfer_protect_preserve() { ExtBuilder::default().build_and_execute_with(|| { - conformance_tests::mutate::transfer_protect_preserve::< + conformance_tests::inspect_mutate::transfer_protect_preserve::< Balances, ::AccountId, ::Balance, diff --git a/frame/support/src/traits/tokens/fungible/conformance_tests/mutate.rs b/frame/support/src/traits/tokens/fungible/conformance_tests/inspect_mutate.rs similarity index 80% rename from frame/support/src/traits/tokens/fungible/conformance_tests/mutate.rs rename to frame/support/src/traits/tokens/fungible/conformance_tests/inspect_mutate.rs index b2e0b6242aac0..9f8383d61f1f6 100644 --- a/frame/support/src/traits/tokens/fungible/conformance_tests/mutate.rs +++ b/frame/support/src/traits/tokens/fungible/conformance_tests/inspect_mutate.rs @@ -38,7 +38,10 @@ where // Sanity check that balances and total issuance start with zero balance assert_eq!(T::total_balance(&account_0), Balance::zero()); assert_eq!(T::total_balance(&account_1), Balance::zero()); + assert_eq!(T::balance(&account_0), Balance::zero()); + assert_eq!(T::balance(&account_1), Balance::zero()); assert_eq!(T::total_issuance(), Balance::zero()); + assert_eq!(T::active_issuance(), Balance::zero()); // Test: Mint an amount into each account let amount_0 = T::minimum_balance(); @@ -49,9 +52,12 @@ where // Verify: Account balances are updated correctly assert_eq!(T::total_balance(&account_0), amount_0.clone()); assert_eq!(T::total_balance(&account_1), amount_1.clone()); + assert_eq!(T::balance(&account_0), amount_0.clone()); + assert_eq!(T::balance(&account_1), amount_1.clone()); // Verify: Total issuance is updated correctly - assert_eq!(T::total_issuance(), amount_0 + amount_1); + assert_eq!(T::total_issuance(), amount_0.clone() + amount_1.clone()); + assert_eq!(T::active_issuance(), amount_0 + amount_1); } pub fn mint_into_overflow() @@ -71,9 +77,11 @@ where // Verify: The balance did not change assert_eq!(T::total_balance(&account), amount.clone()); + assert_eq!(T::balance(&account), amount.clone()); // Verify: The total issuance did not change - assert_eq!(T::total_issuance(), amount); + assert_eq!(T::total_issuance(), amount.clone()); + assert_eq!(T::active_issuance(), amount); } pub fn mint_into_below_minimum() @@ -95,7 +103,9 @@ where // Verify: noop assert_eq!(T::total_balance(&account), Balance::zero()); + assert_eq!(T::balance(&account), Balance::zero()); assert_eq!(T::total_issuance(), Balance::zero()); + assert_eq!(T::active_issuance(), Balance::zero()); } pub fn mint_into_done_mint_into() @@ -131,7 +141,9 @@ where // Verify: The balance and total issuance should be reduced by the burned amount assert_eq!(T::balance(&account), initial_balance.clone() - amount_to_burn.clone()); - assert_eq!(T::total_issuance(), initial_balance - amount_to_burn); + assert_eq!(T::total_balance(&account), initial_balance.clone() - amount_to_burn.clone()); + assert_eq!(T::total_issuance(), initial_balance.clone() - amount_to_burn.clone()); + assert_eq!(T::active_issuance(), initial_balance - amount_to_burn); } pub fn burn_from_best_effort_success() @@ -158,7 +170,9 @@ where // Verify: The balance and total issuance should be reduced by the reducible_balance assert_eq!(T::balance(&account), initial_balance.clone() - reducible_balance.clone()); - assert_eq!(T::total_issuance(), initial_balance - reducible_balance); + assert_eq!(T::total_balance(&account), initial_balance.clone() - reducible_balance.clone()); + assert_eq!(T::total_issuance(), initial_balance.clone() - reducible_balance.clone()); + assert_eq!(T::active_issuance(), initial_balance - reducible_balance); } pub fn burn_from_exact_insufficient_funds() @@ -180,7 +194,9 @@ where // Verify: The balance and total issuance should remain unchanged assert_eq!(T::balance(&account), initial_balance); + assert_eq!(T::total_balance(&account), initial_balance); assert_eq!(T::total_issuance(), initial_balance); + assert_eq!(T::active_issuance(), initial_balance); } // @@ -205,9 +221,12 @@ where // Verify: Account balances are updated correctly assert_eq!(T::total_balance(&account_0), amount_0.clone()); assert_eq!(T::total_balance(&account_1), amount_1.clone()); + assert_eq!(T::balance(&account_0), amount_0.clone()); + assert_eq!(T::balance(&account_1), amount_1.clone()); // Verify: Total issuance is updated correctly - assert_eq!(T::total_issuance(), amount_0 + amount_1); + assert_eq!(T::total_issuance(), amount_0.clone() + amount_1.clone()); + assert_eq!(T::active_issuance(), amount_0 + amount_1); } pub fn restore_overflow() @@ -227,7 +246,9 @@ where // Verify: The balance and total issuance did not change assert_eq!(T::total_balance(&account), amount.clone()); + assert_eq!(T::balance(&account), amount.clone()); assert_eq!(T::total_issuance(), amount); + assert_eq!(T::active_issuance(), amount); } pub fn restore_below_minimum() @@ -249,7 +270,9 @@ where // Verify: noop assert_eq!(T::total_balance(&account), Balance::zero()); + assert_eq!(T::balance(&account), Balance::zero()); assert_eq!(T::total_issuance(), Balance::zero()); + assert_eq!(T::active_issuance(), Balance::zero()); } pub fn restore_done_restore() @@ -283,7 +306,9 @@ where // Verify: The balance and total issuance should be reduced by the shelved amount assert_eq!(T::balance(&account), initial_balance.clone() - amount_to_shelve.clone()); - assert_eq!(T::total_issuance(), initial_balance - amount_to_shelve); + assert_eq!(T::total_balance(&account), initial_balance.clone() - amount_to_shelve.clone()); + assert_eq!(T::total_issuance(), initial_balance.clone() - amount_to_shelve.clone()); + assert_eq!(T::active_issuance(), initial_balance - amount_to_shelve); } pub fn shelve_insufficient_funds() @@ -303,7 +328,9 @@ where // Verify: The balance and total issuance should remain unchanged assert_eq!(T::balance(&account), initial_balance); + assert_eq!(T::total_balance(&account), initial_balance); assert_eq!(T::total_issuance(), initial_balance); + assert_eq!(T::active_issuance(), initial_balance); } pub fn shelve_done_shelve() @@ -339,9 +366,12 @@ where // Verify: Account balances are updated correctly assert_eq!(T::total_balance(&account_0), initial_balance.clone() - transfer_amount.clone()); assert_eq!(T::total_balance(&account_1), initial_balance.clone() + transfer_amount.clone()); + assert_eq!(T::balance(&account_0), initial_balance.clone() - transfer_amount.clone()); + assert_eq!(T::balance(&account_1), initial_balance.clone() + transfer_amount.clone()); // Verify: Total issuance doesn't change - assert_eq!(T::total_issuance(), initial_balance * 2.into()); + assert_eq!(T::total_issuance(), initial_balance.clone() * 2.into()); + assert_eq!(T::active_issuance(), initial_balance * 2.into()); } pub fn transfer_expendable() @@ -364,9 +394,12 @@ where // Verify: Account balances are updated correctly assert_eq!(T::total_balance(&account_0), Balance::zero()); assert_eq!(T::total_balance(&account_1), initial_balance.clone() * 2.into()); + assert_eq!(T::balance(&account_0), Balance::zero()); + assert_eq!(T::balance(&account_1), initial_balance.clone() * 2.into()); // Verify: Total issuance doesn't change - assert_eq!(T::total_issuance(), initial_balance * 2.into()); + assert_eq!(T::total_issuance(), initial_balance.clone() * 2.into()); + assert_eq!(T::active_issuance(), initial_balance * 2.into()); } pub fn transfer_protect_preserve() @@ -375,6 +408,11 @@ where AccountId: AtLeast8BitUnsigned, Balance: AtLeast8BitUnsigned + Debug, { + // This test means nothing if there is no minimum balance + if T::minimum_balance() == Balance::zero() { + return + } + let account_0 = AccountId::from(0); let account_1 = AccountId::from(1); let initial_balance = T::minimum_balance() + 10.into(); @@ -389,7 +427,10 @@ where // Verify: Noop assert_eq!(T::total_balance(&account_0), initial_balance.clone()); assert_eq!(T::total_balance(&account_1), initial_balance.clone()); + assert_eq!(T::balance(&account_0), initial_balance.clone()); + assert_eq!(T::balance(&account_1), initial_balance.clone()); assert_eq!(T::total_issuance(), initial_balance.clone() * 2.into()); + assert_eq!(T::active_issuance(), initial_balance.clone() * 2.into()); // Verify: Transfer Preserve entire balance from account_0 to account_1 should Err let preservation = Preservation::Preserve; @@ -398,5 +439,8 @@ where // Verify: Noop assert_eq!(T::total_balance(&account_0), initial_balance.clone()); assert_eq!(T::total_balance(&account_1), initial_balance.clone()); - assert_eq!(T::total_issuance(), initial_balance * 2.into()); + assert_eq!(T::balance(&account_0), initial_balance.clone()); + assert_eq!(T::balance(&account_1), initial_balance.clone()); + assert_eq!(T::total_issuance(), initial_balance.clone() * 2.into()); + assert_eq!(T::active_issuance(), initial_balance * 2.into()); } diff --git a/frame/support/src/traits/tokens/fungible/conformance_tests/mod.rs b/frame/support/src/traits/tokens/fungible/conformance_tests/mod.rs index 8516b7183a407..eeafd040d7dbc 100644 --- a/frame/support/src/traits/tokens/fungible/conformance_tests/mod.rs +++ b/frame/support/src/traits/tokens/fungible/conformance_tests/mod.rs @@ -4,4 +4,4 @@ /// /// Open question: Do we need conformance tests for 'unbalanced' traits? pub mod balanced; -pub mod mutate; +pub mod inspect_mutate; From 28ae3719600ea558bc7fc32806c7523573ea2559 Mon Sep 17 00:00:00 2001 From: Liam Aharon Date: Fri, 7 Apr 2023 10:04:25 +0400 Subject: [PATCH 09/37] set balance failing tests --- .../src/tests/fungible_conformance_tests.rs | 33 ++++++++++ .../conformance_tests/inspect_mutate.rs | 64 +++++++++++++++++++ 2 files changed, 97 insertions(+) diff --git a/frame/balances/src/tests/fungible_conformance_tests.rs b/frame/balances/src/tests/fungible_conformance_tests.rs index 8e067c6b5048b..534d5d10f345b 100644 --- a/frame/balances/src/tests/fungible_conformance_tests.rs +++ b/frame/balances/src/tests/fungible_conformance_tests.rs @@ -206,3 +206,36 @@ fn transfer_protect_preserve() { >(); }); } + +#[test] +fn transfer_done_transfer() { + ExtBuilder::default().build_and_execute_with(|| { + conformance_tests::inspect_mutate::transfer_done_transfer::< + Balances, + ::AccountId, + ::Balance, + >(); + }); +} + +#[test] +fn set_balance_mint_success() { + ExtBuilder::default().build_and_execute_with(|| { + conformance_tests::inspect_mutate::set_balance_mint_success::< + Balances, + ::AccountId, + ::Balance, + >(); + }); +} + +#[test] +fn set_balance_burn_success() { + ExtBuilder::default().build_and_execute_with(|| { + conformance_tests::inspect_mutate::set_balance_burn_success::< + Balances, + ::AccountId, + ::Balance, + >(); + }); +} diff --git a/frame/support/src/traits/tokens/fungible/conformance_tests/inspect_mutate.rs b/frame/support/src/traits/tokens/fungible/conformance_tests/inspect_mutate.rs index 9f8383d61f1f6..2db343eb7ffa5 100644 --- a/frame/support/src/traits/tokens/fungible/conformance_tests/inspect_mutate.rs +++ b/frame/support/src/traits/tokens/fungible/conformance_tests/inspect_mutate.rs @@ -444,3 +444,67 @@ where assert_eq!(T::total_issuance(), initial_balance.clone() * 2.into()); assert_eq!(T::active_issuance(), initial_balance * 2.into()); } + +pub fn transfer_done_transfer() +where + T: Mutate + Inspect, + AccountId: AtLeast8BitUnsigned, + Balance: AtLeast8BitUnsigned + Debug, +{ + // TODO: How can we test this? + assert!(true); +} + +// +// set_balance +// + +pub fn set_balance_mint_success() +where + T: Mutate + Inspect, + AccountId: AtLeast8BitUnsigned, + Balance: AtLeast8BitUnsigned + Debug, +{ + let account = AccountId::from(10); + let initial_balance = T::minimum_balance() + 10.into(); + T::mint_into(&account, initial_balance.clone()).unwrap(); + + // Test: Increase the account balance with set_balance + let increase_amount: Balance = 5.into(); + let new = T::set_balance(&account, initial_balance.clone() + increase_amount.clone()); + + // Verify: set_balance returned the new balance + let expected_new = initial_balance + increase_amount; + assert_eq!(new, expected_new); + + // Verify: Balance and issuance is updated correctly + assert_eq!(T::total_balance(&account), expected_new.clone()); + assert_eq!(T::balance(&account), expected_new.clone()); + assert_eq!(T::total_issuance(), expected_new.clone()); + assert_eq!(T::active_issuance(), expected_new); +} + +pub fn set_balance_burn_success() +where + T: Mutate + Inspect, + AccountId: AtLeast8BitUnsigned, + Balance: AtLeast8BitUnsigned + Debug, +{ + let account = AccountId::from(10); + let initial_balance = T::minimum_balance() + 10.into(); + T::mint_into(&account, initial_balance.clone()).unwrap(); + + // Test: Increase the account balance with set_balance + let burn_amount: Balance = 5.into(); + let new = T::set_balance(&account, initial_balance.clone() - burn_amount.clone()); + + // Verify: set_balance returned the new balance + let expected_new = initial_balance - burn_amount; + assert_eq!(new, expected_new); + + // Verify: Balance and issuance is updated correctly + assert_eq!(T::total_balance(&account), expected_new.clone()); + assert_eq!(T::balance(&account), expected_new.clone()); + assert_eq!(T::total_issuance(), expected_new.clone()); + assert_eq!(T::active_issuance(), expected_new); +} From bfd008b0f2dfe54c45238e8fa516891f8e04a919 Mon Sep 17 00:00:00 2001 From: Liam Aharon Date: Fri, 7 Apr 2023 11:04:34 +0400 Subject: [PATCH 10/37] can_deposit tests --- .../src/tests/fungible_conformance_tests.rs | 33 +++++++++ .../conformance_tests/inspect_mutate.rs | 68 ++++++++++++++++--- .../src/traits/tokens/fungible/regular.rs | 4 +- 3 files changed, 94 insertions(+), 11 deletions(-) diff --git a/frame/balances/src/tests/fungible_conformance_tests.rs b/frame/balances/src/tests/fungible_conformance_tests.rs index 534d5d10f345b..e183bbe436d2b 100644 --- a/frame/balances/src/tests/fungible_conformance_tests.rs +++ b/frame/balances/src/tests/fungible_conformance_tests.rs @@ -239,3 +239,36 @@ fn set_balance_burn_success() { >(); }); } + +#[test] +fn can_deposit_success() { + ExtBuilder::default().build_and_execute_with(|| { + conformance_tests::inspect_mutate::can_deposit_success::< + Balances, + ::AccountId, + ::Balance, + >(); + }); +} + +#[test] +fn can_deposit_below_minimum() { + ExtBuilder::default().existential_deposit(5).build_and_execute_with(|| { + conformance_tests::inspect_mutate::can_deposit_below_minimum::< + Balances, + ::AccountId, + ::Balance, + >(); + }); +} + +#[test] +fn can_deposit_overflow() { + ExtBuilder::default().build_and_execute_with(|| { + conformance_tests::inspect_mutate::can_deposit_overflow::< + Balances, + ::AccountId, + ::Balance, + >(); + }); +} diff --git a/frame/support/src/traits/tokens/fungible/conformance_tests/inspect_mutate.rs b/frame/support/src/traits/tokens/fungible/conformance_tests/inspect_mutate.rs index 2db343eb7ffa5..115703df72685 100644 --- a/frame/support/src/traits/tokens/fungible/conformance_tests/inspect_mutate.rs +++ b/frame/support/src/traits/tokens/fungible/conformance_tests/inspect_mutate.rs @@ -17,7 +17,7 @@ use crate::traits::{ fungible::{Inspect, Mutate}, - tokens::{Fortitude, Precision, Preservation}, + tokens::{DepositConsequence, Fortitude, Precision, Preservation, Provenance}, }; use core::fmt::Debug; use sp_arithmetic::traits::AtLeast8BitUnsigned; @@ -35,14 +35,6 @@ where let account_0 = AccountId::from(0); let account_1 = AccountId::from(1); - // Sanity check that balances and total issuance start with zero balance - assert_eq!(T::total_balance(&account_0), Balance::zero()); - assert_eq!(T::total_balance(&account_1), Balance::zero()); - assert_eq!(T::balance(&account_0), Balance::zero()); - assert_eq!(T::balance(&account_1), Balance::zero()); - assert_eq!(T::total_issuance(), Balance::zero()); - assert_eq!(T::active_issuance(), Balance::zero()); - // Test: Mint an amount into each account let amount_0 = T::minimum_balance(); let amount_1 = T::minimum_balance() + 5.into(); @@ -508,3 +500,61 @@ where assert_eq!(T::total_issuance(), expected_new.clone()); assert_eq!(T::active_issuance(), expected_new); } + +// +// can_deposit +// + +pub fn can_deposit_success() +where + T: Mutate + Inspect, + AccountId: AtLeast8BitUnsigned, + Balance: AtLeast8BitUnsigned + Debug, +{ + let account = AccountId::from(10); + let initial_balance = T::minimum_balance() + 10.into(); + T::mint_into(&account, initial_balance.clone()).unwrap(); + + // Test: Try deposit a reasonable amount + let ret = T::can_deposit(&account, 5.into(), Provenance::Minted); + + // Verify: Returns success + assert_eq!(ret, DepositConsequence::Success); +} + +pub fn can_deposit_below_minimum() +where + T: Mutate + Inspect, + AccountId: AtLeast8BitUnsigned, + Balance: AtLeast8BitUnsigned + Debug, +{ + // can_deposit always returns Success for amount 0 + if T::minimum_balance() < 1.into() { + return + } + + let account = AccountId::from(10); + + // Test: can_deposit below the minimum + let ret = T::can_deposit(&account, T::minimum_balance() - 1.into(), Provenance::Minted); + + // Verify: Returns success + assert_eq!(ret, DepositConsequence::BelowMinimum); +} + +pub fn can_deposit_overflow() +where + T: Mutate + Inspect, + AccountId: AtLeast8BitUnsigned, + Balance: AtLeast8BitUnsigned + Debug, +{ + let account = AccountId::from(10); + + // Test: Try deposit over the max balance + let initial_balance = Balance::max_value() - 5.into(); + T::mint_into(&account, initial_balance.clone()).unwrap(); + let ret = T::can_deposit(&account, 10.into(), Provenance::Minted); + + // Verify: Returns success + assert_eq!(ret, DepositConsequence::Overflow); +} diff --git a/frame/support/src/traits/tokens/fungible/regular.rs b/frame/support/src/traits/tokens/fungible/regular.rs index 92fb21332ac10..3476549464032 100644 --- a/frame/support/src/traits/tokens/fungible/regular.rs +++ b/frame/support/src/traits/tokens/fungible/regular.rs @@ -327,9 +327,9 @@ pub trait Mutate: Inspect + Unbalanced { fn set_balance(who: &AccountId, amount: Self::Balance) -> Self::Balance { let b = Self::balance(who); if b > amount { - Self::burn_from(who, b - amount, BestEffort, Force).map(|d| amount.saturating_sub(d)) + Self::burn_from(who, b - amount, BestEffort, Force).map(|d| b.saturating_sub(d)) } else { - Self::mint_into(who, amount - b).map(|d| amount.saturating_add(d)) + Self::mint_into(who, amount - b).map(|d| b.saturating_add(d)) } .unwrap_or(b) } From 7a7fab95d17de5a811e2e7624824b14f8240a2e1 Mon Sep 17 00:00:00 2001 From: Liam Aharon Date: Fri, 7 Apr 2023 11:24:35 +0400 Subject: [PATCH 11/37] can_withdraw tests --- .../src/tests/fungible_conformance_tests.rs | 33 +++++++++ .../conformance_tests/inspect_mutate.rs | 67 ++++++++++++++++++- 2 files changed, 98 insertions(+), 2 deletions(-) diff --git a/frame/balances/src/tests/fungible_conformance_tests.rs b/frame/balances/src/tests/fungible_conformance_tests.rs index e183bbe436d2b..ac4f78c5239ae 100644 --- a/frame/balances/src/tests/fungible_conformance_tests.rs +++ b/frame/balances/src/tests/fungible_conformance_tests.rs @@ -272,3 +272,36 @@ fn can_deposit_overflow() { >(); }); } + +#[test] +fn can_withdraw_success() { + ExtBuilder::default().build_and_execute_with(|| { + conformance_tests::inspect_mutate::can_withdraw_success::< + Balances, + ::AccountId, + ::Balance, + >(); + }); +} + +#[test] +fn can_withdraw_reduced_to_zero() { + ExtBuilder::default().build_and_execute_with(|| { + conformance_tests::inspect_mutate::can_withdraw_reduced_to_zero::< + Balances, + ::AccountId, + ::Balance, + >(); + }); +} + +#[test] +fn can_withdraw_balance_low() { + ExtBuilder::default().build_and_execute_with(|| { + conformance_tests::inspect_mutate::can_withdraw_balance_low::< + Balances, + ::AccountId, + ::Balance, + >(); + }); +} diff --git a/frame/support/src/traits/tokens/fungible/conformance_tests/inspect_mutate.rs b/frame/support/src/traits/tokens/fungible/conformance_tests/inspect_mutate.rs index 115703df72685..680923b4504b3 100644 --- a/frame/support/src/traits/tokens/fungible/conformance_tests/inspect_mutate.rs +++ b/frame/support/src/traits/tokens/fungible/conformance_tests/inspect_mutate.rs @@ -17,7 +17,9 @@ use crate::traits::{ fungible::{Inspect, Mutate}, - tokens::{DepositConsequence, Fortitude, Precision, Preservation, Provenance}, + tokens::{ + DepositConsequence, Fortitude, Precision, Preservation, Provenance, WithdrawConsequence, + }, }; use core::fmt::Debug; use sp_arithmetic::traits::AtLeast8BitUnsigned; @@ -515,7 +517,7 @@ where let initial_balance = T::minimum_balance() + 10.into(); T::mint_into(&account, initial_balance.clone()).unwrap(); - // Test: Try deposit a reasonable amount + // Test: can_deposit a reasonable amount let ret = T::can_deposit(&account, 5.into(), Provenance::Minted); // Verify: Returns success @@ -558,3 +560,64 @@ where // Verify: Returns success assert_eq!(ret, DepositConsequence::Overflow); } + +// +// can_withdraw +// + +pub fn can_withdraw_success() +where + T: Mutate + Inspect, + AccountId: AtLeast8BitUnsigned, + Balance: AtLeast8BitUnsigned + Debug, +{ + let account = AccountId::from(10); + let initial_balance = T::minimum_balance() + 10.into(); + T::mint_into(&account, initial_balance.clone()).unwrap(); + + // Test: can_withdraw a reasonable amount + let ret = T::can_withdraw(&account, 5.into()); + + // Verify: Returns success + assert_eq!(ret, WithdrawConsequence::Success); +} + +pub fn can_withdraw_reduced_to_zero() +where + T: Mutate + Inspect, + AccountId: AtLeast8BitUnsigned, + Balance: AtLeast8BitUnsigned + Debug, +{ + if T::minimum_balance() == Balance::zero() { + return + } + + let account = AccountId::from(10); + let initial_balance = T::minimum_balance(); + T::mint_into(&account, initial_balance.clone()).unwrap(); + + // Verify: can_withdraw below the minimum balance returns ReducedToZero + let ret = T::can_withdraw(&account, 1.into()); + assert_eq!(ret, WithdrawConsequence::ReducedToZero(T::minimum_balance() - 1.into())); +} + +pub fn can_withdraw_balance_low() +where + T: Mutate + Inspect, + AccountId: AtLeast8BitUnsigned, + Balance: AtLeast8BitUnsigned + Debug, +{ + if T::minimum_balance() == Balance::zero() { + return + } + + let account = AccountId::from(10); + let other_account = AccountId::from(100); + let initial_balance = T::minimum_balance() + 5.into(); + T::mint_into(&account, initial_balance.clone()).unwrap(); + T::mint_into(&other_account, initial_balance.clone() * 2.into()).unwrap(); + + // Verify: can_withdraw below the account balance returns BalanceLow + let ret = T::can_withdraw(&account, initial_balance + 1.into()); + assert_eq!(ret, WithdrawConsequence::BalanceLow); +} From d30577423eff1ceddb4ba1364fc90bcdfdc9a6d4 Mon Sep 17 00:00:00 2001 From: Liam Aharon Date: Fri, 7 Apr 2023 11:30:22 +0400 Subject: [PATCH 12/37] test reducible_balance --- .../src/tests/fungible_conformance_tests.rs | 22 ++++++++++++ .../conformance_tests/inspect_mutate.rs | 36 +++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/frame/balances/src/tests/fungible_conformance_tests.rs b/frame/balances/src/tests/fungible_conformance_tests.rs index ac4f78c5239ae..7910ac90b31f2 100644 --- a/frame/balances/src/tests/fungible_conformance_tests.rs +++ b/frame/balances/src/tests/fungible_conformance_tests.rs @@ -305,3 +305,25 @@ fn can_withdraw_balance_low() { >(); }); } + +#[test] +fn reducible_balance_expendable() { + ExtBuilder::default().build_and_execute_with(|| { + conformance_tests::inspect_mutate::reducible_balance_expendable::< + Balances, + ::AccountId, + ::Balance, + >(); + }); +} + +#[test] +fn reducible_balance_protect_preserve() { + ExtBuilder::default().build_and_execute_with(|| { + conformance_tests::inspect_mutate::reducible_balance_protect_preserve::< + Balances, + ::AccountId, + ::Balance, + >(); + }); +} diff --git a/frame/support/src/traits/tokens/fungible/conformance_tests/inspect_mutate.rs b/frame/support/src/traits/tokens/fungible/conformance_tests/inspect_mutate.rs index 680923b4504b3..48d6c325ca86c 100644 --- a/frame/support/src/traits/tokens/fungible/conformance_tests/inspect_mutate.rs +++ b/frame/support/src/traits/tokens/fungible/conformance_tests/inspect_mutate.rs @@ -621,3 +621,39 @@ where let ret = T::can_withdraw(&account, initial_balance + 1.into()); assert_eq!(ret, WithdrawConsequence::BalanceLow); } + +// +// reducible_balance +// + +pub fn reducible_balance_expendable() +where + T: Mutate + Inspect, + AccountId: AtLeast8BitUnsigned, + Balance: AtLeast8BitUnsigned + Debug, +{ + let account = AccountId::from(10); + let initial_balance = T::minimum_balance() + 10.into(); + T::mint_into(&account, initial_balance.clone()).unwrap(); + + // Verify: reducible_balance returns the full balance + let ret = T::reducible_balance(&account, Preservation::Expendable, Fortitude::Polite); + assert_eq!(ret, initial_balance); +} + +pub fn reducible_balance_protect_preserve() +where + T: Mutate + Inspect, + AccountId: AtLeast8BitUnsigned, + Balance: AtLeast8BitUnsigned + Debug, +{ + let account = AccountId::from(10); + let initial_balance = T::minimum_balance() + 10.into(); + T::mint_into(&account, initial_balance.clone()).unwrap(); + + // Verify: reducible_balance returns the full balance - min balance + let ret = T::reducible_balance(&account, Preservation::Protect, Fortitude::Polite); + assert_eq!(ret, initial_balance.clone() - T::minimum_balance()); + let ret = T::reducible_balance(&account, Preservation::Preserve, Fortitude::Polite); + assert_eq!(ret, initial_balance - T::minimum_balance()); +} From 7544e0f26ceddda6631f4eb25e2f73fb5f790fa8 Mon Sep 17 00:00:00 2001 From: Liam Aharon Date: Fri, 7 Apr 2023 11:32:38 +0400 Subject: [PATCH 13/37] remove balanced stub --- .../traits/tokens/fungible/conformance_tests/balanced.rs | 4 ---- .../src/traits/tokens/fungible/conformance_tests/mod.rs | 6 ------ 2 files changed, 10 deletions(-) delete mode 100644 frame/support/src/traits/tokens/fungible/conformance_tests/balanced.rs diff --git a/frame/support/src/traits/tokens/fungible/conformance_tests/balanced.rs b/frame/support/src/traits/tokens/fungible/conformance_tests/balanced.rs deleted file mode 100644 index ca0c68b685df9..0000000000000 --- a/frame/support/src/traits/tokens/fungible/conformance_tests/balanced.rs +++ /dev/null @@ -1,4 +0,0 @@ -#[test] -fn stub() { - assert!(true); -} diff --git a/frame/support/src/traits/tokens/fungible/conformance_tests/mod.rs b/frame/support/src/traits/tokens/fungible/conformance_tests/mod.rs index eeafd040d7dbc..88ba56a6fed02 100644 --- a/frame/support/src/traits/tokens/fungible/conformance_tests/mod.rs +++ b/frame/support/src/traits/tokens/fungible/conformance_tests/mod.rs @@ -1,7 +1 @@ -/// Temporary module to hold the conformance tests for the Fungible trait, -/// until it is abstracted into pallet-agnostic macro/s and moved outside -/// of the balances module. -/// -/// Open question: Do we need conformance tests for 'unbalanced' traits? -pub mod balanced; pub mod inspect_mutate; From 509f61ad53171c9430e40c10dea6c7ae1cccbbdb Mon Sep 17 00:00:00 2001 From: Liam Aharon Date: Fri, 7 Apr 2023 12:17:01 +0400 Subject: [PATCH 14/37] revert set_balance return val fix --- frame/support/src/traits/tokens/fungible/regular.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/frame/support/src/traits/tokens/fungible/regular.rs b/frame/support/src/traits/tokens/fungible/regular.rs index 3476549464032..574392cac8256 100644 --- a/frame/support/src/traits/tokens/fungible/regular.rs +++ b/frame/support/src/traits/tokens/fungible/regular.rs @@ -122,7 +122,7 @@ impl> Dust { /// **WARNING** /// Do not use this directly unless you want trouble, since it allows you to alter account balances /// without keeping the issuance up to date. It has no safeguards against accidentally creating -/// token imbalances in your system leading to accidental inflation or deflation. It's really just +/// token imbalances in your system leading to accidental imflation or deflation. It's really just /// for the underlying datatype to implement so the user gets the much safer `Balanced` trait to /// use. pub trait Unbalanced: Inspect { @@ -327,9 +327,9 @@ pub trait Mutate: Inspect + Unbalanced { fn set_balance(who: &AccountId, amount: Self::Balance) -> Self::Balance { let b = Self::balance(who); if b > amount { - Self::burn_from(who, b - amount, BestEffort, Force).map(|d| b.saturating_sub(d)) + Self::burn_from(who, b - amount, BestEffort, Force).map(|d| amount.saturating_sub(d)) } else { - Self::mint_into(who, amount - b).map(|d| b.saturating_add(d)) + Self::mint_into(who, amount - b).map(|d| amount.saturating_add(d)) } .unwrap_or(b) } From d7bf251ab6a1732db86ec5585b7114b564f12146 Mon Sep 17 00:00:00 2001 From: Liam Aharon Date: Fri, 7 Apr 2023 12:17:37 +0400 Subject: [PATCH 15/37] typo --- frame/support/src/traits/tokens/fungible/regular.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frame/support/src/traits/tokens/fungible/regular.rs b/frame/support/src/traits/tokens/fungible/regular.rs index 574392cac8256..92fb21332ac10 100644 --- a/frame/support/src/traits/tokens/fungible/regular.rs +++ b/frame/support/src/traits/tokens/fungible/regular.rs @@ -122,7 +122,7 @@ impl> Dust { /// **WARNING** /// Do not use this directly unless you want trouble, since it allows you to alter account balances /// without keeping the issuance up to date. It has no safeguards against accidentally creating -/// token imbalances in your system leading to accidental imflation or deflation. It's really just +/// token imbalances in your system leading to accidental inflation or deflation. It's really just /// for the underlying datatype to implement so the user gets the much safer `Balanced` trait to /// use. pub trait Unbalanced: Inspect { From 3ebe6e973932a43f8e9f6c11a6dabf6fee6a37b5 Mon Sep 17 00:00:00 2001 From: Liam Aharon Date: Mon, 10 Apr 2023 16:50:07 +0400 Subject: [PATCH 16/37] macro and dust trap tests --- Cargo.lock | 5 +- frame/balances/Cargo.toml | 1 + .../src/tests/fungible_conformance_tests.rs | 557 ++++++++---------- .../conformance_tests/inspect_mutate.rs | 261 +++++--- 4 files changed, 438 insertions(+), 386 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d4185103f8c96..bde01471bd4af 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5744,6 +5744,7 @@ dependencies = [ "log", "pallet-transaction-payment", "parity-scale-codec", + "paste", "scale-info", "sp-core", "sp-io", @@ -7173,9 +7174,9 @@ dependencies = [ [[package]] name = "paste" -version = "1.0.11" +version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d01a5bd0424d00070b0098dd17ebca6f961a959dead1dbcbbbc1d1cd8d3deeba" +checksum = "9f746c4065a8fa3fe23974dd82f15431cc8d40779821001404d10d2e79ca7d79" [[package]] name = "pbkdf2" diff --git a/frame/balances/Cargo.toml b/frame/balances/Cargo.toml index 9646a934df00e..8285910f7c1ce 100644 --- a/frame/balances/Cargo.toml +++ b/frame/balances/Cargo.toml @@ -26,6 +26,7 @@ sp-std = { version = "5.0.0", default-features = false, path = "../../primitives pallet-transaction-payment = { version = "4.0.0-dev", path = "../transaction-payment" } sp-core = { version = "7.0.0", path = "../../primitives/core" } sp-io = { version = "7.0.0", path = "../../primitives/io" } +paste = "1.0.12" [features] default = ["std"] diff --git a/frame/balances/src/tests/fungible_conformance_tests.rs b/frame/balances/src/tests/fungible_conformance_tests.rs index 7910ac90b31f2..d2eb7e19f6064 100644 --- a/frame/balances/src/tests/fungible_conformance_tests.rs +++ b/frame/balances/src/tests/fungible_conformance_tests.rs @@ -16,314 +16,253 @@ // limitations under the License. use super::*; -use frame_support::traits::fungible::conformance_tests; - -// TODO: Add a macro to the conformance tests to generate all of these tests - -#[test] -fn mint_into_success() { - ExtBuilder::default().build_and_execute_with(|| { - conformance_tests::inspect_mutate::mint_into_success::< - Balances, - ::AccountId, - ::Balance, - >(); - }); -} - -#[test] -fn mint_into_overflow() { - ExtBuilder::default().build_and_execute_with(|| { - conformance_tests::inspect_mutate::mint_into_overflow::< - Balances, - ::AccountId, - ::Balance, - >(); - }); -} - -#[test] -fn mint_into_below_minimum() { - ExtBuilder::default().build_and_execute_with(|| { - conformance_tests::inspect_mutate::mint_into_below_minimum::< - Balances, - ::AccountId, - ::Balance, - >(); - }); -} - -#[test] -fn mint_into_done_mint_into() { - ExtBuilder::default().build_and_execute_with(|| { - conformance_tests::inspect_mutate::mint_into_done_mint_into::< - Balances, - ::AccountId, - ::Balance, - >(); - }); -} - -#[test] -fn burn_from_exact_success() { - ExtBuilder::default().build_and_execute_with(|| { - conformance_tests::inspect_mutate::burn_from_exact_success::< - Balances, - ::AccountId, - ::Balance, - >(); - }); -} - -#[test] -fn burn_from_best_effort_success() { - ExtBuilder::default().build_and_execute_with(|| { - conformance_tests::inspect_mutate::burn_from_best_effort_success::< - Balances, - ::AccountId, - ::Balance, - >(); - }); -} - -#[test] -fn burn_from_exact_insufficient_funds() { - ExtBuilder::default().build_and_execute_with(|| { - conformance_tests::inspect_mutate::burn_from_exact_insufficient_funds::< - Balances, - ::AccountId, - ::Balance, - >(); - }); -} - -#[test] -fn restore_success() { - ExtBuilder::default().build_and_execute_with(|| { - conformance_tests::inspect_mutate::restore_success::< - Balances, - ::AccountId, - ::Balance, - >(); - }); -} - -#[test] -fn restore_overflow() { - ExtBuilder::default().build_and_execute_with(|| { - conformance_tests::inspect_mutate::restore_overflow::< - Balances, - ::AccountId, - ::Balance, - >(); - }); -} - -#[test] -fn restore_below_minimum() { - ExtBuilder::default().build_and_execute_with(|| { - conformance_tests::inspect_mutate::restore_below_minimum::< - Balances, - ::AccountId, - ::Balance, - >(); - }); -} - -#[test] -fn restore_done_restore() { - ExtBuilder::default().build_and_execute_with(|| { - conformance_tests::inspect_mutate::restore_done_restore::< - Balances, - ::AccountId, - ::Balance, - >(); - }); -} - -#[test] -fn shelve_success() { - ExtBuilder::default().build_and_execute_with(|| { - conformance_tests::inspect_mutate::shelve_success::< - Balances, - ::AccountId, - ::Balance, - >(); - }); -} - -#[test] -fn shelve_insufficient_funds() { - ExtBuilder::default().build_and_execute_with(|| { - conformance_tests::inspect_mutate::shelve_insufficient_funds::< - Balances, - ::AccountId, - ::Balance, - >(); - }); -} - -#[test] -fn shelve_done_shelve() { - ExtBuilder::default().build_and_execute_with(|| { - conformance_tests::inspect_mutate::shelve_done_shelve::< - Balances, - ::AccountId, - ::Balance, - >(); - }); +use frame_support::traits::fungible::{conformance_tests, Inspect, Mutate}; +use paste::paste; + +macro_rules! run_tests { + ($parent_module:tt :: $child_module:tt, $ext_deposit:expr, $dust_trap:expr, $( $name:ident ),*) => { + $( + paste! { + #[test] + fn [< $name _ext_deposit_ $ext_deposit _dust_trap_ $dust_trap >]() { + let (trap_account, builder) = match $dust_trap { + "on" => { + let trap_account = ::AccountId::from(65174286u64); + let builder = ExtBuilder::default().existential_deposit($ext_deposit).dust_trap(trap_account); + (Some(trap_account), builder) + }, + "off" => { + let builder = ExtBuilder::default().existential_deposit($ext_deposit); + (None, builder) + }, + _ => panic!("dust_trap must be either \"on\" or \"off\"") + }; + builder.build_and_execute_with(|| { + // Initialise the trap account if it is being used + if let Some(trap_account) = trap_account { + Balances::set_balance(&trap_account, Balances::minimum_balance()); + }; + // Run the test + $parent_module::$child_module::$name::< + Balances, + ::AccountId, + ::Balance, + >(trap_account); + }); + } + } + )* + } } -#[test] -fn transfer_success() { - ExtBuilder::default().build_and_execute_with(|| { - conformance_tests::inspect_mutate::transfer_success::< - Balances, - ::AccountId, - ::Balance, - >(); - }); -} - -#[test] -fn transfer_expendable() { - ExtBuilder::default().build_and_execute_with(|| { - conformance_tests::inspect_mutate::transfer_expendable::< - Balances, - ::AccountId, - ::Balance, - >(); - }); -} - -#[test] -fn transfer_protect_preserve() { - ExtBuilder::default().build_and_execute_with(|| { - conformance_tests::inspect_mutate::transfer_protect_preserve::< - Balances, - ::AccountId, - ::Balance, - >(); - }); -} - -#[test] -fn transfer_done_transfer() { - ExtBuilder::default().build_and_execute_with(|| { - conformance_tests::inspect_mutate::transfer_done_transfer::< - Balances, - ::AccountId, - ::Balance, - >(); - }); -} - -#[test] -fn set_balance_mint_success() { - ExtBuilder::default().build_and_execute_with(|| { - conformance_tests::inspect_mutate::set_balance_mint_success::< - Balances, - ::AccountId, - ::Balance, - >(); - }); -} - -#[test] -fn set_balance_burn_success() { - ExtBuilder::default().build_and_execute_with(|| { - conformance_tests::inspect_mutate::set_balance_burn_success::< - Balances, - ::AccountId, - ::Balance, - >(); - }); -} - -#[test] -fn can_deposit_success() { - ExtBuilder::default().build_and_execute_with(|| { - conformance_tests::inspect_mutate::can_deposit_success::< - Balances, - ::AccountId, - ::Balance, - >(); - }); -} - -#[test] -fn can_deposit_below_minimum() { - ExtBuilder::default().existential_deposit(5).build_and_execute_with(|| { - conformance_tests::inspect_mutate::can_deposit_below_minimum::< - Balances, - ::AccountId, - ::Balance, - >(); - }); -} - -#[test] -fn can_deposit_overflow() { - ExtBuilder::default().build_and_execute_with(|| { - conformance_tests::inspect_mutate::can_deposit_overflow::< - Balances, - ::AccountId, - ::Balance, - >(); - }); -} - -#[test] -fn can_withdraw_success() { - ExtBuilder::default().build_and_execute_with(|| { - conformance_tests::inspect_mutate::can_withdraw_success::< - Balances, - ::AccountId, - ::Balance, - >(); - }); -} - -#[test] -fn can_withdraw_reduced_to_zero() { - ExtBuilder::default().build_and_execute_with(|| { - conformance_tests::inspect_mutate::can_withdraw_reduced_to_zero::< - Balances, - ::AccountId, - ::Balance, - >(); - }); -} - -#[test] -fn can_withdraw_balance_low() { - ExtBuilder::default().build_and_execute_with(|| { - conformance_tests::inspect_mutate::can_withdraw_balance_low::< - Balances, - ::AccountId, - ::Balance, - >(); - }); -} - -#[test] -fn reducible_balance_expendable() { - ExtBuilder::default().build_and_execute_with(|| { - conformance_tests::inspect_mutate::reducible_balance_expendable::< - Balances, - ::AccountId, - ::Balance, - >(); - }); -} - -#[test] -fn reducible_balance_protect_preserve() { - ExtBuilder::default().build_and_execute_with(|| { - conformance_tests::inspect_mutate::reducible_balance_protect_preserve::< - Balances, - ::AccountId, - ::Balance, - >(); - }); -} +// run_tests!(conformance_tests::inspect_mutate, 100, "on", mint_into_overflow); + +run_tests!( + conformance_tests::inspect_mutate, + 1, + "off", + mint_into_success, + mint_into_overflow, + mint_into_below_minimum, + mint_into_done_mint_into, + burn_from_exact_success, + burn_from_best_effort_success, + burn_from_exact_insufficient_funds, + restore_success, + restore_overflow, + restore_below_minimum, + restore_done_restore, + shelve_success, + shelve_insufficient_funds, + shelve_done_shelve, + transfer_success, + transfer_expendable_all, + transfer_expendable_dust, + transfer_protect_preserve, + transfer_done_transfer, + set_balance_mint_success, + set_balance_burn_success, + can_deposit_success, + can_deposit_below_minimum, + can_deposit_overflow, + can_withdraw_success, + can_withdraw_reduced_to_zero, + can_withdraw_balance_low, + reducible_balance_expendable, + reducible_balance_protect_preserve +); + +run_tests!( + conformance_tests::inspect_mutate, + 2, + "off", + mint_into_success, + mint_into_overflow, + mint_into_below_minimum, + mint_into_done_mint_into, + burn_from_exact_success, + burn_from_best_effort_success, + burn_from_exact_insufficient_funds, + restore_success, + restore_overflow, + restore_below_minimum, + restore_done_restore, + shelve_success, + shelve_insufficient_funds, + shelve_done_shelve, + transfer_success, + transfer_expendable_all, + transfer_expendable_dust, + transfer_protect_preserve, + transfer_done_transfer, + set_balance_mint_success, + set_balance_burn_success, + can_deposit_success, + can_deposit_below_minimum, + can_deposit_overflow, + can_withdraw_success, + can_withdraw_reduced_to_zero, + can_withdraw_balance_low, + reducible_balance_expendable, + reducible_balance_protect_preserve +); + +run_tests!( + conformance_tests::inspect_mutate, + 100, + "off", + mint_into_success, + mint_into_overflow, + mint_into_below_minimum, + mint_into_done_mint_into, + burn_from_exact_success, + burn_from_best_effort_success, + burn_from_exact_insufficient_funds, + restore_success, + restore_overflow, + restore_below_minimum, + restore_done_restore, + shelve_success, + shelve_insufficient_funds, + shelve_done_shelve, + transfer_success, + transfer_expendable_all, + transfer_expendable_dust, + transfer_protect_preserve, + transfer_done_transfer, + set_balance_mint_success, + set_balance_burn_success, + can_deposit_success, + can_deposit_below_minimum, + can_deposit_overflow, + can_withdraw_success, + can_withdraw_reduced_to_zero, + can_withdraw_balance_low, + reducible_balance_expendable, + reducible_balance_protect_preserve +); + +run_tests!( + conformance_tests::inspect_mutate, + 1, + "on", + mint_into_success, + mint_into_overflow, + mint_into_below_minimum, + mint_into_done_mint_into, + burn_from_exact_success, + burn_from_best_effort_success, + burn_from_exact_insufficient_funds, + restore_success, + restore_overflow, + restore_below_minimum, + restore_done_restore, + shelve_success, + shelve_insufficient_funds, + shelve_done_shelve, + transfer_success, + transfer_expendable_all, + transfer_expendable_dust, + transfer_protect_preserve, + transfer_done_transfer, + set_balance_mint_success, + set_balance_burn_success, + can_deposit_success, + can_deposit_below_minimum, + can_deposit_overflow, + can_withdraw_success, + can_withdraw_reduced_to_zero, + can_withdraw_balance_low, + reducible_balance_expendable, + reducible_balance_protect_preserve +); + +run_tests!( + conformance_tests::inspect_mutate, + 2, + "on", + mint_into_success, + mint_into_overflow, + mint_into_below_minimum, + mint_into_done_mint_into, + burn_from_exact_success, + burn_from_best_effort_success, + burn_from_exact_insufficient_funds, + restore_success, + restore_overflow, + restore_below_minimum, + restore_done_restore, + shelve_success, + shelve_insufficient_funds, + shelve_done_shelve, + transfer_success, + transfer_expendable_all, + transfer_expendable_dust, + transfer_protect_preserve, + transfer_done_transfer, + set_balance_mint_success, + set_balance_burn_success, + can_deposit_success, + can_deposit_below_minimum, + can_deposit_overflow, + can_withdraw_success, + can_withdraw_reduced_to_zero, + can_withdraw_balance_low, + reducible_balance_expendable, + reducible_balance_protect_preserve +); + +run_tests!( + conformance_tests::inspect_mutate, + 100, + "on", + mint_into_success, + mint_into_overflow, + mint_into_below_minimum, + mint_into_done_mint_into, + burn_from_exact_success, + burn_from_best_effort_success, + burn_from_exact_insufficient_funds, + restore_success, + restore_overflow, + restore_below_minimum, + restore_done_restore, + shelve_success, + shelve_insufficient_funds, + shelve_done_shelve, + transfer_success, + transfer_expendable_all, + transfer_expendable_dust, + transfer_protect_preserve, + transfer_done_transfer, + set_balance_mint_success, + set_balance_burn_success, + can_deposit_success, + can_deposit_below_minimum, + can_deposit_overflow, + can_withdraw_success, + can_withdraw_reduced_to_zero, + can_withdraw_balance_low, + reducible_balance_expendable, + reducible_balance_protect_preserve +); diff --git a/frame/support/src/traits/tokens/fungible/conformance_tests/inspect_mutate.rs b/frame/support/src/traits/tokens/fungible/conformance_tests/inspect_mutate.rs index 48d6c325ca86c..37e168520a501 100644 --- a/frame/support/src/traits/tokens/fungible/conformance_tests/inspect_mutate.rs +++ b/frame/support/src/traits/tokens/fungible/conformance_tests/inspect_mutate.rs @@ -28,12 +28,14 @@ use sp_arithmetic::traits::AtLeast8BitUnsigned; // mint_into // -pub fn mint_into_success() +pub fn mint_into_success(_dust_trap: Option) where T: Mutate + Inspect, AccountId: AtLeast8BitUnsigned, Balance: AtLeast8BitUnsigned + Debug, { + let initial_total_issuance = T::total_issuance(); + let initial_active_issuance = T::active_issuance(); let account_0 = AccountId::from(0); let account_1 = AccountId::from(1); @@ -50,18 +52,20 @@ where assert_eq!(T::balance(&account_1), amount_1.clone()); // Verify: Total issuance is updated correctly - assert_eq!(T::total_issuance(), amount_0.clone() + amount_1.clone()); - assert_eq!(T::active_issuance(), amount_0 + amount_1); + assert_eq!(T::total_issuance(), initial_total_issuance + amount_0.clone() + amount_1.clone()); + assert_eq!(T::active_issuance(), initial_active_issuance + amount_0 + amount_1); } -pub fn mint_into_overflow() +pub fn mint_into_overflow(_dust_trap: Option) where T: Mutate + Inspect, AccountId: AtLeast8BitUnsigned, Balance: AtLeast8BitUnsigned + Debug, { + let initial_total_issuance = T::total_issuance(); + let initial_active_issuance = T::active_issuance(); let account = AccountId::from(10); - let amount = Balance::max_value() - 5.into(); + let amount = Balance::max_value() - 5.into() - initial_total_issuance.clone(); // Mint just below the maximum balance T::mint_into(&account, amount.clone()).unwrap(); @@ -74,11 +78,11 @@ where assert_eq!(T::balance(&account), amount.clone()); // Verify: The total issuance did not change - assert_eq!(T::total_issuance(), amount.clone()); - assert_eq!(T::active_issuance(), amount); + assert_eq!(T::total_issuance(), initial_total_issuance + amount.clone()); + assert_eq!(T::active_issuance(), initial_active_issuance + amount); } -pub fn mint_into_below_minimum() +pub fn mint_into_below_minimum(_dust_trap: Option) where T: Mutate + Inspect, AccountId: AtLeast8BitUnsigned, @@ -89,6 +93,8 @@ where return } + let initial_total_issuance = T::total_issuance(); + let initial_active_issuance = T::active_issuance(); let account = AccountId::from(10); let amount = T::minimum_balance() - 1.into(); @@ -98,11 +104,11 @@ where // Verify: noop assert_eq!(T::total_balance(&account), Balance::zero()); assert_eq!(T::balance(&account), Balance::zero()); - assert_eq!(T::total_issuance(), Balance::zero()); - assert_eq!(T::active_issuance(), Balance::zero()); + assert_eq!(T::total_issuance(), initial_total_issuance); + assert_eq!(T::active_issuance(), initial_active_issuance); } -pub fn mint_into_done_mint_into() +pub fn mint_into_done_mint_into(_dust_trap: Option) where T: Mutate + Inspect, AccountId: AtLeast8BitUnsigned, @@ -116,19 +122,22 @@ where // burn_from // -pub fn burn_from_exact_success() +pub fn burn_from_exact_success(_dust_trap: Option) where T: Mutate + Inspect, AccountId: AtLeast8BitUnsigned, Balance: AtLeast8BitUnsigned + Debug, { + let initial_total_issuance = T::total_issuance(); + let initial_active_issuance = T::active_issuance(); + // Setup account let account = AccountId::from(5); - let initial_balance = Balance::from(100); + let initial_balance = T::minimum_balance() + 10.into(); T::mint_into(&account, initial_balance.clone()).unwrap(); // Test: Burn an exact amount from the account - let amount_to_burn = Balance::from(50); + let amount_to_burn = Balance::from(5); let precision = Precision::Exact; let force = Fortitude::Polite; T::burn_from(&account, amount_to_burn.clone(), precision, force).unwrap(); @@ -136,19 +145,25 @@ where // Verify: The balance and total issuance should be reduced by the burned amount assert_eq!(T::balance(&account), initial_balance.clone() - amount_to_burn.clone()); assert_eq!(T::total_balance(&account), initial_balance.clone() - amount_to_burn.clone()); - assert_eq!(T::total_issuance(), initial_balance.clone() - amount_to_burn.clone()); - assert_eq!(T::active_issuance(), initial_balance - amount_to_burn); + assert_eq!( + T::total_issuance(), + initial_total_issuance + initial_balance.clone() - amount_to_burn.clone() + ); + assert_eq!(T::active_issuance(), initial_active_issuance + initial_balance - amount_to_burn); } -pub fn burn_from_best_effort_success() +pub fn burn_from_best_effort_success(_dust_trap: Option) where T: Mutate + Inspect, AccountId: AtLeast8BitUnsigned, Balance: AtLeast8BitUnsigned + Debug, { + let initial_total_issuance = T::total_issuance(); + let initial_active_issuance = T::active_issuance(); + // Setup account let account = AccountId::from(5); - let initial_balance = Balance::from(100); + let initial_balance = T::minimum_balance() + 10.into(); T::mint_into(&account, initial_balance.clone()).unwrap(); // Get reducible balance @@ -156,7 +171,7 @@ where let reducible_balance = T::reducible_balance(&account, Preservation::Expendable, force); // Test: Burn a best effort amount from the account that is greater than the reducible balance - let amount_to_burn = initial_balance.clone() + 10.into(); + let amount_to_burn = reducible_balance.clone() + 5.into(); let precision = Precision::BestEffort; assert!(amount_to_burn > reducible_balance); assert!(amount_to_burn > T::balance(&account)); @@ -165,11 +180,14 @@ where // Verify: The balance and total issuance should be reduced by the reducible_balance assert_eq!(T::balance(&account), initial_balance.clone() - reducible_balance.clone()); assert_eq!(T::total_balance(&account), initial_balance.clone() - reducible_balance.clone()); - assert_eq!(T::total_issuance(), initial_balance.clone() - reducible_balance.clone()); - assert_eq!(T::active_issuance(), initial_balance - reducible_balance); + assert_eq!( + T::total_issuance(), + initial_total_issuance + initial_balance.clone() - reducible_balance.clone() + ); + assert_eq!(T::active_issuance(), initial_active_issuance + initial_balance - reducible_balance); } -pub fn burn_from_exact_insufficient_funds() +pub fn burn_from_exact_insufficient_funds(_dust_trap: Option) where T: Mutate + Inspect, AccountId: AtLeast8BitUnsigned, @@ -177,8 +195,10 @@ where { // Set up the initial conditions and parameters for the test let account = AccountId::from(5); - let initial_balance = Balance::from(100); + let initial_balance = T::minimum_balance() + 10.into(); T::mint_into(&account, initial_balance.clone()).unwrap(); + let initial_total_issuance = T::total_issuance(); + let initial_active_issuance = T::active_issuance(); // Verify: Burn an amount greater than the account's balance with Exact precision returns Err let amount_to_burn = initial_balance.clone() + 10.into(); @@ -189,15 +209,15 @@ where // Verify: The balance and total issuance should remain unchanged assert_eq!(T::balance(&account), initial_balance); assert_eq!(T::total_balance(&account), initial_balance); - assert_eq!(T::total_issuance(), initial_balance); - assert_eq!(T::active_issuance(), initial_balance); + assert_eq!(T::total_issuance(), initial_total_issuance); + assert_eq!(T::active_issuance(), initial_active_issuance); } // // restore // -pub fn restore_success() +pub fn restore_success(_dust_trap: Option) where T: Mutate + Inspect, AccountId: AtLeast8BitUnsigned, @@ -209,6 +229,8 @@ where // Test: Restore an amount into each account let amount_0 = T::minimum_balance(); let amount_1 = T::minimum_balance() + 5.into(); + let initial_total_issuance = T::total_issuance(); + let initial_active_issuance = T::active_issuance(); T::restore(&account_0, amount_0.clone()).unwrap(); T::restore(&account_1, amount_1.clone()).unwrap(); @@ -219,18 +241,20 @@ where assert_eq!(T::balance(&account_1), amount_1.clone()); // Verify: Total issuance is updated correctly - assert_eq!(T::total_issuance(), amount_0.clone() + amount_1.clone()); - assert_eq!(T::active_issuance(), amount_0 + amount_1); + assert_eq!(T::total_issuance(), initial_total_issuance + amount_0.clone() + amount_1.clone()); + assert_eq!(T::active_issuance(), initial_active_issuance + amount_0 + amount_1); } -pub fn restore_overflow() +pub fn restore_overflow(_dust_trap: Option) where T: Mutate + Inspect, AccountId: AtLeast8BitUnsigned, Balance: AtLeast8BitUnsigned + Debug, { + let initial_total_issuance = T::total_issuance(); + let initial_active_issuance = T::active_issuance(); let account = AccountId::from(10); - let amount = Balance::max_value() - 5.into(); + let amount = Balance::max_value() - 5.into() - initial_total_issuance.clone(); // Restore just below the maximum balance T::restore(&account, amount.clone()).unwrap(); @@ -241,11 +265,11 @@ where // Verify: The balance and total issuance did not change assert_eq!(T::total_balance(&account), amount.clone()); assert_eq!(T::balance(&account), amount.clone()); - assert_eq!(T::total_issuance(), amount); - assert_eq!(T::active_issuance(), amount); + assert_eq!(T::total_issuance(), initial_total_issuance + amount.clone()); + assert_eq!(T::active_issuance(), initial_active_issuance + amount); } -pub fn restore_below_minimum() +pub fn restore_below_minimum(_dust_trap: Option) where T: Mutate + Inspect, AccountId: AtLeast8BitUnsigned, @@ -258,6 +282,8 @@ where let account = AccountId::from(10); let amount = T::minimum_balance() - 1.into(); + let initial_total_issuance = T::total_issuance(); + let initial_active_issuance = T::active_issuance(); // Verify: Restoring below the minimum balance returns Err T::restore(&account, amount.clone()).unwrap_err(); @@ -265,11 +291,11 @@ where // Verify: noop assert_eq!(T::total_balance(&account), Balance::zero()); assert_eq!(T::balance(&account), Balance::zero()); - assert_eq!(T::total_issuance(), Balance::zero()); - assert_eq!(T::active_issuance(), Balance::zero()); + assert_eq!(T::total_issuance(), initial_total_issuance); + assert_eq!(T::active_issuance(), initial_active_issuance); } -pub fn restore_done_restore() +pub fn restore_done_restore(_dust_trap: Option) where T: Mutate + Inspect, AccountId: AtLeast8BitUnsigned, @@ -283,37 +309,47 @@ where // shelve tests // -pub fn shelve_success() +pub fn shelve_success(_dust_trap: Option) where T: Mutate + Inspect, AccountId: AtLeast8BitUnsigned, Balance: AtLeast8BitUnsigned + Debug, { + let initial_total_issuance = T::total_issuance(); + let initial_active_issuance = T::active_issuance(); + // Setup account let account = AccountId::from(5); - let initial_balance = Balance::from(100); + let initial_balance = T::minimum_balance() + 10.into(); + T::restore(&account, initial_balance.clone()).unwrap(); // Test: Shelve an amount from the account - let amount_to_shelve = Balance::from(50); + let amount_to_shelve = Balance::from(5); T::shelve(&account, amount_to_shelve.clone()).unwrap(); // Verify: The balance and total issuance should be reduced by the shelved amount assert_eq!(T::balance(&account), initial_balance.clone() - amount_to_shelve.clone()); assert_eq!(T::total_balance(&account), initial_balance.clone() - amount_to_shelve.clone()); - assert_eq!(T::total_issuance(), initial_balance.clone() - amount_to_shelve.clone()); - assert_eq!(T::active_issuance(), initial_balance - amount_to_shelve); + assert_eq!( + T::total_issuance(), + initial_total_issuance + initial_balance.clone() - amount_to_shelve.clone() + ); + assert_eq!(T::active_issuance(), initial_active_issuance + initial_balance - amount_to_shelve); } -pub fn shelve_insufficient_funds() +pub fn shelve_insufficient_funds(_dust_trap: Option) where T: Mutate + Inspect, AccountId: AtLeast8BitUnsigned, Balance: AtLeast8BitUnsigned + Debug, { + let initial_total_issuance = T::total_issuance(); + let initial_active_issuance = T::active_issuance(); + // Set up the initial conditions and parameters for the test let account = AccountId::from(5); - let initial_balance = Balance::from(100); + let initial_balance = T::minimum_balance() + 10.into(); T::restore(&account, initial_balance.clone()).unwrap(); // Verify: Shelving greater than the balance with Exact precision returns Err @@ -323,11 +359,11 @@ where // Verify: The balance and total issuance should remain unchanged assert_eq!(T::balance(&account), initial_balance); assert_eq!(T::total_balance(&account), initial_balance); - assert_eq!(T::total_issuance(), initial_balance); - assert_eq!(T::active_issuance(), initial_balance); + assert_eq!(T::total_issuance(), initial_total_issuance + initial_balance.clone()); + assert_eq!(T::active_issuance(), initial_active_issuance + initial_balance); } -pub fn shelve_done_shelve() +pub fn shelve_done_shelve(_dust_trap: Option) where T: Mutate + Inspect, AccountId: AtLeast8BitUnsigned, @@ -341,12 +377,14 @@ where // transfer // -pub fn transfer_success() +pub fn transfer_success(_dust_trap: Option) where T: Mutate + Inspect, AccountId: AtLeast8BitUnsigned, Balance: AtLeast8BitUnsigned + Debug, { + let initial_total_issuance = T::total_issuance(); + let initial_active_issuance = T::active_issuance(); let account_0 = AccountId::from(0); let account_1 = AccountId::from(1); let initial_balance = T::minimum_balance() + 10.into(); @@ -354,7 +392,7 @@ where T::set_balance(&account_1, initial_balance.clone()); // Test: Transfer an amount from account_0 to account_1 - let transfer_amount = initial_balance.clone() - 3.into(); + let transfer_amount = Balance::from(3); T::transfer(&account_0, &account_1, transfer_amount.clone(), Preservation::Expendable).unwrap(); // Verify: Account balances are updated correctly @@ -364,16 +402,18 @@ where assert_eq!(T::balance(&account_1), initial_balance.clone() + transfer_amount.clone()); // Verify: Total issuance doesn't change - assert_eq!(T::total_issuance(), initial_balance.clone() * 2.into()); - assert_eq!(T::active_issuance(), initial_balance * 2.into()); + assert_eq!(T::total_issuance(), initial_total_issuance + initial_balance.clone() * 2.into()); + assert_eq!(T::active_issuance(), initial_active_issuance + initial_balance * 2.into()); } -pub fn transfer_expendable() +pub fn transfer_expendable_all(_dust_trap: Option) where T: Mutate + Inspect, AccountId: AtLeast8BitUnsigned, Balance: AtLeast8BitUnsigned + Debug, { + let initial_total_issuance = T::total_issuance(); + let initial_active_issuance = T::active_issuance(); let account_0 = AccountId::from(0); let account_1 = AccountId::from(1); let initial_balance = T::minimum_balance() + 10.into(); @@ -392,11 +432,70 @@ where assert_eq!(T::balance(&account_1), initial_balance.clone() * 2.into()); // Verify: Total issuance doesn't change - assert_eq!(T::total_issuance(), initial_balance.clone() * 2.into()); - assert_eq!(T::active_issuance(), initial_balance * 2.into()); + assert_eq!(T::total_issuance(), initial_total_issuance + initial_balance.clone() * 2.into()); + assert_eq!(T::active_issuance(), initial_active_issuance + initial_balance * 2.into()); +} + +pub fn transfer_expendable_dust(dust_trap: Option) +where + T: Mutate + Inspect, + AccountId: AtLeast8BitUnsigned, + Balance: AtLeast8BitUnsigned + Debug, +{ + let account_0 = AccountId::from(10); + let account_1 = AccountId::from(20); + let initial_balance = T::minimum_balance() + 10.into(); + T::set_balance(&account_0, initial_balance.clone()); + T::set_balance(&account_1, initial_balance.clone()); + + let initial_total_issuance = T::total_issuance(); + let initial_active_issuance = T::active_issuance(); + let initial_dust_trap_balance = match dust_trap.clone() { + Some(dust_trap) => T::total_balance(&dust_trap), + None => Balance::zero(), + }; + + // Test: Transfer balance + let preservation = Preservation::Expendable; + let transfer_amount = Balance::from(11); + T::transfer(&account_0, &account_1, transfer_amount.clone(), preservation).unwrap(); + + // Verify: Account balances are updated correctly + assert_eq!(T::total_balance(&account_0), Balance::zero()); + assert_eq!(T::total_balance(&account_1), initial_balance.clone() + transfer_amount.clone()); + assert_eq!(T::balance(&account_0), Balance::zero()); + assert_eq!(T::balance(&account_1), initial_balance.clone() + transfer_amount.clone()); + + match dust_trap { + Some(dust_trap) => { + // Verify: Total issuance and active issuance don't change + assert_eq!(T::total_issuance(), initial_total_issuance.clone()); + assert_eq!(T::active_issuance(), initial_active_issuance.clone()); + // Verify: Dust is collected into dust trap + assert_eq!( + T::total_balance(&dust_trap), + initial_dust_trap_balance.clone() + T::minimum_balance() - 1.into() + ); + assert_eq!( + T::balance(&dust_trap), + initial_dust_trap_balance + T::minimum_balance() - 1.into() + ); + }, + None => { + // Verify: Total issuance and active issuance are reduced by the dust amount + assert_eq!( + T::total_issuance(), + initial_total_issuance - T::minimum_balance() + 1.into() + ); + assert_eq!( + T::active_issuance(), + initial_active_issuance - T::minimum_balance() + 1.into() + ); + }, + } } -pub fn transfer_protect_preserve() +pub fn transfer_protect_preserve(_dust_trap: Option) where T: Mutate + Inspect, AccountId: AtLeast8BitUnsigned, @@ -407,6 +506,8 @@ where return } + let initial_total_issuance = T::total_issuance(); + let initial_active_issuance = T::active_issuance(); let account_0 = AccountId::from(0); let account_1 = AccountId::from(1); let initial_balance = T::minimum_balance() + 10.into(); @@ -423,8 +524,14 @@ where assert_eq!(T::total_balance(&account_1), initial_balance.clone()); assert_eq!(T::balance(&account_0), initial_balance.clone()); assert_eq!(T::balance(&account_1), initial_balance.clone()); - assert_eq!(T::total_issuance(), initial_balance.clone() * 2.into()); - assert_eq!(T::active_issuance(), initial_balance.clone() * 2.into()); + assert_eq!( + T::total_issuance(), + initial_total_issuance.clone() + initial_balance.clone() * 2.into() + ); + assert_eq!( + T::active_issuance(), + initial_active_issuance.clone() + initial_balance.clone() * 2.into() + ); // Verify: Transfer Preserve entire balance from account_0 to account_1 should Err let preservation = Preservation::Preserve; @@ -435,11 +542,11 @@ where assert_eq!(T::total_balance(&account_1), initial_balance.clone()); assert_eq!(T::balance(&account_0), initial_balance.clone()); assert_eq!(T::balance(&account_1), initial_balance.clone()); - assert_eq!(T::total_issuance(), initial_balance.clone() * 2.into()); - assert_eq!(T::active_issuance(), initial_balance * 2.into()); + assert_eq!(T::total_issuance(), initial_total_issuance + initial_balance.clone() * 2.into()); + assert_eq!(T::active_issuance(), initial_active_issuance + initial_balance * 2.into()); } -pub fn transfer_done_transfer() +pub fn transfer_done_transfer(_dust_trap: Option) where T: Mutate + Inspect, AccountId: AtLeast8BitUnsigned, @@ -453,12 +560,14 @@ where // set_balance // -pub fn set_balance_mint_success() +pub fn set_balance_mint_success(_dust_trap: Option) where T: Mutate + Inspect, AccountId: AtLeast8BitUnsigned, Balance: AtLeast8BitUnsigned + Debug, { + let initial_total_issuance = T::total_issuance(); + let initial_active_issuance = T::active_issuance(); let account = AccountId::from(10); let initial_balance = T::minimum_balance() + 10.into(); T::mint_into(&account, initial_balance.clone()).unwrap(); @@ -474,16 +583,18 @@ where // Verify: Balance and issuance is updated correctly assert_eq!(T::total_balance(&account), expected_new.clone()); assert_eq!(T::balance(&account), expected_new.clone()); - assert_eq!(T::total_issuance(), expected_new.clone()); - assert_eq!(T::active_issuance(), expected_new); + assert_eq!(T::total_issuance(), initial_total_issuance + expected_new.clone()); + assert_eq!(T::active_issuance(), initial_active_issuance + expected_new); } -pub fn set_balance_burn_success() +pub fn set_balance_burn_success(_dust_trap: Option) where T: Mutate + Inspect, AccountId: AtLeast8BitUnsigned, Balance: AtLeast8BitUnsigned + Debug, { + let initial_total_issuance = T::total_issuance(); + let initial_active_issuance = T::active_issuance(); let account = AccountId::from(10); let initial_balance = T::minimum_balance() + 10.into(); T::mint_into(&account, initial_balance.clone()).unwrap(); @@ -499,15 +610,15 @@ where // Verify: Balance and issuance is updated correctly assert_eq!(T::total_balance(&account), expected_new.clone()); assert_eq!(T::balance(&account), expected_new.clone()); - assert_eq!(T::total_issuance(), expected_new.clone()); - assert_eq!(T::active_issuance(), expected_new); + assert_eq!(T::total_issuance(), initial_total_issuance + expected_new.clone()); + assert_eq!(T::active_issuance(), initial_active_issuance + expected_new); } // // can_deposit // -pub fn can_deposit_success() +pub fn can_deposit_success(_dust_trap: Option) where T: Mutate + Inspect, AccountId: AtLeast8BitUnsigned, @@ -524,14 +635,14 @@ where assert_eq!(ret, DepositConsequence::Success); } -pub fn can_deposit_below_minimum() +pub fn can_deposit_below_minimum(_dust_trap: Option) where T: Mutate + Inspect, AccountId: AtLeast8BitUnsigned, Balance: AtLeast8BitUnsigned + Debug, { // can_deposit always returns Success for amount 0 - if T::minimum_balance() < 1.into() { + if T::minimum_balance() < 2.into() { return } @@ -544,7 +655,7 @@ where assert_eq!(ret, DepositConsequence::BelowMinimum); } -pub fn can_deposit_overflow() +pub fn can_deposit_overflow(_dust_trap: Option) where T: Mutate + Inspect, AccountId: AtLeast8BitUnsigned, @@ -553,7 +664,7 @@ where let account = AccountId::from(10); // Test: Try deposit over the max balance - let initial_balance = Balance::max_value() - 5.into(); + let initial_balance = Balance::max_value() - 5.into() - T::total_issuance(); T::mint_into(&account, initial_balance.clone()).unwrap(); let ret = T::can_deposit(&account, 10.into(), Provenance::Minted); @@ -565,7 +676,7 @@ where // can_withdraw // -pub fn can_withdraw_success() +pub fn can_withdraw_success(_dust_trap: Option) where T: Mutate + Inspect, AccountId: AtLeast8BitUnsigned, @@ -582,7 +693,7 @@ where assert_eq!(ret, WithdrawConsequence::Success); } -pub fn can_withdraw_reduced_to_zero() +pub fn can_withdraw_reduced_to_zero(_dust_trap: Option) where T: Mutate + Inspect, AccountId: AtLeast8BitUnsigned, @@ -601,7 +712,7 @@ where assert_eq!(ret, WithdrawConsequence::ReducedToZero(T::minimum_balance() - 1.into())); } -pub fn can_withdraw_balance_low() +pub fn can_withdraw_balance_low(_dust_trap: Option) where T: Mutate + Inspect, AccountId: AtLeast8BitUnsigned, @@ -626,7 +737,7 @@ where // reducible_balance // -pub fn reducible_balance_expendable() +pub fn reducible_balance_expendable(_dust_trap: Option) where T: Mutate + Inspect, AccountId: AtLeast8BitUnsigned, @@ -641,7 +752,7 @@ where assert_eq!(ret, initial_balance); } -pub fn reducible_balance_protect_preserve() +pub fn reducible_balance_protect_preserve(_dust_trap: Option) where T: Mutate + Inspect, AccountId: AtLeast8BitUnsigned, From 33f6a6beac630c00c6bf60d171a77af1e1a4e8c2 Mon Sep 17 00:00:00 2001 From: Liam Aharon Date: Mon, 10 Apr 2023 17:08:26 +0400 Subject: [PATCH 17/37] disable test when it doesn't make sense --- .../tokens/fungible/conformance_tests/inspect_mutate.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/frame/support/src/traits/tokens/fungible/conformance_tests/inspect_mutate.rs b/frame/support/src/traits/tokens/fungible/conformance_tests/inspect_mutate.rs index 37e168520a501..225f883c52001 100644 --- a/frame/support/src/traits/tokens/fungible/conformance_tests/inspect_mutate.rs +++ b/frame/support/src/traits/tokens/fungible/conformance_tests/inspect_mutate.rs @@ -442,6 +442,10 @@ where AccountId: AtLeast8BitUnsigned, Balance: AtLeast8BitUnsigned + Debug, { + if T::minimum_balance() == Balance::zero() { + return + } + let account_0 = AccountId::from(10); let account_1 = AccountId::from(20); let initial_balance = T::minimum_balance() + 10.into(); From 13455c7d02abe700716d23b84ed44e44061ff6f6 Mon Sep 17 00:00:00 2001 From: Liam Aharon Date: Mon, 10 Apr 2023 17:08:42 +0400 Subject: [PATCH 18/37] remove debug comment --- frame/balances/src/tests/fungible_conformance_tests.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/frame/balances/src/tests/fungible_conformance_tests.rs b/frame/balances/src/tests/fungible_conformance_tests.rs index d2eb7e19f6064..5c31096277df0 100644 --- a/frame/balances/src/tests/fungible_conformance_tests.rs +++ b/frame/balances/src/tests/fungible_conformance_tests.rs @@ -55,8 +55,6 @@ macro_rules! run_tests { } } -// run_tests!(conformance_tests::inspect_mutate, 100, "on", mint_into_overflow); - run_tests!( conformance_tests::inspect_mutate, 1, From a948896b635cfd3fdbbde2c41b05a53337f522f9 Mon Sep 17 00:00:00 2001 From: Liam Aharon Date: Mon, 10 Apr 2023 17:31:33 +0400 Subject: [PATCH 19/37] reduce macro boilerplate --- .../src/tests/fungible_conformance_tests.rs | 421 ++++++++---------- 1 file changed, 181 insertions(+), 240 deletions(-) diff --git a/frame/balances/src/tests/fungible_conformance_tests.rs b/frame/balances/src/tests/fungible_conformance_tests.rs index 5c31096277df0..167ac57ca3abc 100644 --- a/frame/balances/src/tests/fungible_conformance_tests.rs +++ b/frame/balances/src/tests/fungible_conformance_tests.rs @@ -19,248 +19,189 @@ use super::*; use frame_support::traits::fungible::{conformance_tests, Inspect, Mutate}; use paste::paste; -macro_rules! run_tests { - ($parent_module:tt :: $child_module:tt, $ext_deposit:expr, $dust_trap:expr, $( $name:ident ),*) => { - $( - paste! { - #[test] - fn [< $name _ext_deposit_ $ext_deposit _dust_trap_ $dust_trap >]() { - let (trap_account, builder) = match $dust_trap { - "on" => { - let trap_account = ::AccountId::from(65174286u64); - let builder = ExtBuilder::default().existential_deposit($ext_deposit).dust_trap(trap_account); - (Some(trap_account), builder) - }, - "off" => { - let builder = ExtBuilder::default().existential_deposit($ext_deposit); - (None, builder) - }, - _ => panic!("dust_trap must be either \"on\" or \"off\"") +macro_rules! generate_test { + ($parent_module:tt :: $child_module:tt, $ext_deposit:expr, $dust_trap:expr, $name:ident) => { + paste! { + #[test] + fn [< $name _ext_deposit_ $ext_deposit _dust_trap_ $dust_trap >]() { + let (trap_account, builder) = match $dust_trap { + "on" => { + let trap_account = ::AccountId::from(65174286u64); + let builder = ExtBuilder::default().existential_deposit($ext_deposit).dust_trap(trap_account); + (Some(trap_account), builder) + }, + "off" => { + let builder = ExtBuilder::default().existential_deposit($ext_deposit); + (None, builder) + }, + _ => panic!("dust_trap must be either \"on\" or \"off\"") + }; + builder.build_and_execute_with(|| { + // Initialise the trap account if it is being used + if let Some(trap_account) = trap_account { + Balances::set_balance(&trap_account, Balances::minimum_balance()); }; - builder.build_and_execute_with(|| { - // Initialise the trap account if it is being used - if let Some(trap_account) = trap_account { - Balances::set_balance(&trap_account, Balances::minimum_balance()); - }; - // Run the test - $parent_module::$child_module::$name::< - Balances, - ::AccountId, - ::Balance, - >(trap_account); - }); - } + // Run the test + $parent_module::$child_module::$name::< + Balances, + ::AccountId, + ::Balance, + >(trap_account); + }); } - )* - } + } + } } -run_tests!( - conformance_tests::inspect_mutate, - 1, - "off", - mint_into_success, - mint_into_overflow, - mint_into_below_minimum, - mint_into_done_mint_into, - burn_from_exact_success, - burn_from_best_effort_success, - burn_from_exact_insufficient_funds, - restore_success, - restore_overflow, - restore_below_minimum, - restore_done_restore, - shelve_success, - shelve_insufficient_funds, - shelve_done_shelve, - transfer_success, - transfer_expendable_all, - transfer_expendable_dust, - transfer_protect_preserve, - transfer_done_transfer, - set_balance_mint_success, - set_balance_burn_success, - can_deposit_success, - can_deposit_below_minimum, - can_deposit_overflow, - can_withdraw_success, - can_withdraw_reduced_to_zero, - can_withdraw_balance_low, - reducible_balance_expendable, - reducible_balance_protect_preserve -); - -run_tests!( - conformance_tests::inspect_mutate, - 2, - "off", - mint_into_success, - mint_into_overflow, - mint_into_below_minimum, - mint_into_done_mint_into, - burn_from_exact_success, - burn_from_best_effort_success, - burn_from_exact_insufficient_funds, - restore_success, - restore_overflow, - restore_below_minimum, - restore_done_restore, - shelve_success, - shelve_insufficient_funds, - shelve_done_shelve, - transfer_success, - transfer_expendable_all, - transfer_expendable_dust, - transfer_protect_preserve, - transfer_done_transfer, - set_balance_mint_success, - set_balance_burn_success, - can_deposit_success, - can_deposit_below_minimum, - can_deposit_overflow, - can_withdraw_success, - can_withdraw_reduced_to_zero, - can_withdraw_balance_low, - reducible_balance_expendable, - reducible_balance_protect_preserve -); - -run_tests!( - conformance_tests::inspect_mutate, - 100, - "off", - mint_into_success, - mint_into_overflow, - mint_into_below_minimum, - mint_into_done_mint_into, - burn_from_exact_success, - burn_from_best_effort_success, - burn_from_exact_insufficient_funds, - restore_success, - restore_overflow, - restore_below_minimum, - restore_done_restore, - shelve_success, - shelve_insufficient_funds, - shelve_done_shelve, - transfer_success, - transfer_expendable_all, - transfer_expendable_dust, - transfer_protect_preserve, - transfer_done_transfer, - set_balance_mint_success, - set_balance_burn_success, - can_deposit_success, - can_deposit_below_minimum, - can_deposit_overflow, - can_withdraw_success, - can_withdraw_reduced_to_zero, - can_withdraw_balance_low, - reducible_balance_expendable, - reducible_balance_protect_preserve -); - -run_tests!( - conformance_tests::inspect_mutate, - 1, - "on", - mint_into_success, - mint_into_overflow, - mint_into_below_minimum, - mint_into_done_mint_into, - burn_from_exact_success, - burn_from_best_effort_success, - burn_from_exact_insufficient_funds, - restore_success, - restore_overflow, - restore_below_minimum, - restore_done_restore, - shelve_success, - shelve_insufficient_funds, - shelve_done_shelve, - transfer_success, - transfer_expendable_all, - transfer_expendable_dust, - transfer_protect_preserve, - transfer_done_transfer, - set_balance_mint_success, - set_balance_burn_success, - can_deposit_success, - can_deposit_below_minimum, - can_deposit_overflow, - can_withdraw_success, - can_withdraw_reduced_to_zero, - can_withdraw_balance_low, - reducible_balance_expendable, - reducible_balance_protect_preserve -); - -run_tests!( - conformance_tests::inspect_mutate, - 2, - "on", - mint_into_success, - mint_into_overflow, - mint_into_below_minimum, - mint_into_done_mint_into, - burn_from_exact_success, - burn_from_best_effort_success, - burn_from_exact_insufficient_funds, - restore_success, - restore_overflow, - restore_below_minimum, - restore_done_restore, - shelve_success, - shelve_insufficient_funds, - shelve_done_shelve, - transfer_success, - transfer_expendable_all, - transfer_expendable_dust, - transfer_protect_preserve, - transfer_done_transfer, - set_balance_mint_success, - set_balance_burn_success, - can_deposit_success, - can_deposit_below_minimum, - can_deposit_overflow, - can_withdraw_success, - can_withdraw_reduced_to_zero, - can_withdraw_balance_low, - reducible_balance_expendable, - reducible_balance_protect_preserve -); +macro_rules! run_tests { + ($parent_module:tt :: $child_module:tt, $ext_deposit:expr, $dust_trap:expr) => { + generate_test!($parent_module::$child_module, $ext_deposit, $dust_trap, mint_into_success); + generate_test!($parent_module::$child_module, $ext_deposit, $dust_trap, mint_into_overflow); + generate_test!( + $parent_module::$child_module, + $ext_deposit, + $dust_trap, + mint_into_below_minimum + ); + generate_test!( + $parent_module::$child_module, + $ext_deposit, + $dust_trap, + mint_into_done_mint_into + ); + generate_test!( + $parent_module::$child_module, + $ext_deposit, + $dust_trap, + burn_from_exact_success + ); + generate_test!( + $parent_module::$child_module, + $ext_deposit, + $dust_trap, + burn_from_best_effort_success + ); + generate_test!( + $parent_module::$child_module, + $ext_deposit, + $dust_trap, + burn_from_exact_insufficient_funds + ); + generate_test!($parent_module::$child_module, $ext_deposit, $dust_trap, restore_success); + generate_test!($parent_module::$child_module, $ext_deposit, $dust_trap, restore_overflow); + generate_test!( + $parent_module::$child_module, + $ext_deposit, + $dust_trap, + restore_below_minimum + ); + generate_test!( + $parent_module::$child_module, + $ext_deposit, + $dust_trap, + restore_done_restore + ); + generate_test!($parent_module::$child_module, $ext_deposit, $dust_trap, shelve_success); + generate_test!( + $parent_module::$child_module, + $ext_deposit, + $dust_trap, + shelve_insufficient_funds + ); + generate_test!($parent_module::$child_module, $ext_deposit, $dust_trap, shelve_done_shelve); + generate_test!($parent_module::$child_module, $ext_deposit, $dust_trap, transfer_success); + generate_test!( + $parent_module::$child_module, + $ext_deposit, + $dust_trap, + transfer_expendable_all + ); + generate_test!( + $parent_module::$child_module, + $ext_deposit, + $dust_trap, + transfer_expendable_dust + ); + generate_test!( + $parent_module::$child_module, + $ext_deposit, + $dust_trap, + transfer_protect_preserve + ); + generate_test!( + $parent_module::$child_module, + $ext_deposit, + $dust_trap, + transfer_done_transfer + ); + generate_test!( + $parent_module::$child_module, + $ext_deposit, + $dust_trap, + set_balance_mint_success + ); + generate_test!( + $parent_module::$child_module, + $ext_deposit, + $dust_trap, + set_balance_burn_success + ); + generate_test!( + $parent_module::$child_module, + $ext_deposit, + $dust_trap, + can_deposit_success + ); + generate_test!( + $parent_module::$child_module, + $ext_deposit, + $dust_trap, + can_deposit_below_minimum + ); + generate_test!( + $parent_module::$child_module, + $ext_deposit, + $dust_trap, + can_deposit_overflow + ); + generate_test!( + $parent_module::$child_module, + $ext_deposit, + $dust_trap, + can_withdraw_success + ); + generate_test!( + $parent_module::$child_module, + $ext_deposit, + $dust_trap, + can_withdraw_reduced_to_zero + ); + generate_test!( + $parent_module::$child_module, + $ext_deposit, + $dust_trap, + can_withdraw_balance_low + ); + generate_test!( + $parent_module::$child_module, + $ext_deposit, + $dust_trap, + reducible_balance_expendable + ); + generate_test!( + $parent_module::$child_module, + $ext_deposit, + $dust_trap, + reducible_balance_protect_preserve + ); + }; +} -run_tests!( - conformance_tests::inspect_mutate, - 100, - "on", - mint_into_success, - mint_into_overflow, - mint_into_below_minimum, - mint_into_done_mint_into, - burn_from_exact_success, - burn_from_best_effort_success, - burn_from_exact_insufficient_funds, - restore_success, - restore_overflow, - restore_below_minimum, - restore_done_restore, - shelve_success, - shelve_insufficient_funds, - shelve_done_shelve, - transfer_success, - transfer_expendable_all, - transfer_expendable_dust, - transfer_protect_preserve, - transfer_done_transfer, - set_balance_mint_success, - set_balance_burn_success, - can_deposit_success, - can_deposit_below_minimum, - can_deposit_overflow, - can_withdraw_success, - can_withdraw_reduced_to_zero, - can_withdraw_balance_low, - reducible_balance_expendable, - reducible_balance_protect_preserve -); +run_tests!(conformance_tests::inspect_mutate, 1, "off"); +run_tests!(conformance_tests::inspect_mutate, 1, "on"); +run_tests!(conformance_tests::inspect_mutate, 2, "off"); +run_tests!(conformance_tests::inspect_mutate, 2, "on"); +run_tests!(conformance_tests::inspect_mutate, 5, "off"); +run_tests!(conformance_tests::inspect_mutate, 5, "on"); +run_tests!(conformance_tests::inspect_mutate, 1000, "off"); +run_tests!(conformance_tests::inspect_mutate, 1000, "on"); From c12a3272c13a0a7831ef715c6e9cd715a20900a6 Mon Sep 17 00:00:00 2001 From: Liam Aharon Date: Mon, 10 Apr 2023 17:35:14 +0400 Subject: [PATCH 20/37] improved var naming --- .../src/tests/fungible_conformance_tests.rs | 177 ++++-------------- 1 file changed, 35 insertions(+), 142 deletions(-) diff --git a/frame/balances/src/tests/fungible_conformance_tests.rs b/frame/balances/src/tests/fungible_conformance_tests.rs index 167ac57ca3abc..49e86f47d88cb 100644 --- a/frame/balances/src/tests/fungible_conformance_tests.rs +++ b/frame/balances/src/tests/fungible_conformance_tests.rs @@ -20,7 +20,7 @@ use frame_support::traits::fungible::{conformance_tests, Inspect, Mutate}; use paste::paste; macro_rules! generate_test { - ($parent_module:tt :: $child_module:tt, $ext_deposit:expr, $dust_trap:expr, $name:ident) => { + ($pm:tt :: $cm:tt, $ext_deposit:expr, $dust_trap:expr, $name:ident) => { paste! { #[test] fn [< $name _ext_deposit_ $ext_deposit _dust_trap_ $dust_trap >]() { @@ -42,7 +42,7 @@ macro_rules! generate_test { Balances::set_balance(&trap_account, Balances::minimum_balance()); }; // Run the test - $parent_module::$child_module::$name::< + $pm::$cm::$name::< Balances, ::AccountId, ::Balance, @@ -53,147 +53,40 @@ macro_rules! generate_test { } } +// $pm = parent module +// $cm = child module +// shorthand so each generate_test! call can fit on one line macro_rules! run_tests { - ($parent_module:tt :: $child_module:tt, $ext_deposit:expr, $dust_trap:expr) => { - generate_test!($parent_module::$child_module, $ext_deposit, $dust_trap, mint_into_success); - generate_test!($parent_module::$child_module, $ext_deposit, $dust_trap, mint_into_overflow); - generate_test!( - $parent_module::$child_module, - $ext_deposit, - $dust_trap, - mint_into_below_minimum - ); - generate_test!( - $parent_module::$child_module, - $ext_deposit, - $dust_trap, - mint_into_done_mint_into - ); - generate_test!( - $parent_module::$child_module, - $ext_deposit, - $dust_trap, - burn_from_exact_success - ); - generate_test!( - $parent_module::$child_module, - $ext_deposit, - $dust_trap, - burn_from_best_effort_success - ); - generate_test!( - $parent_module::$child_module, - $ext_deposit, - $dust_trap, - burn_from_exact_insufficient_funds - ); - generate_test!($parent_module::$child_module, $ext_deposit, $dust_trap, restore_success); - generate_test!($parent_module::$child_module, $ext_deposit, $dust_trap, restore_overflow); - generate_test!( - $parent_module::$child_module, - $ext_deposit, - $dust_trap, - restore_below_minimum - ); - generate_test!( - $parent_module::$child_module, - $ext_deposit, - $dust_trap, - restore_done_restore - ); - generate_test!($parent_module::$child_module, $ext_deposit, $dust_trap, shelve_success); - generate_test!( - $parent_module::$child_module, - $ext_deposit, - $dust_trap, - shelve_insufficient_funds - ); - generate_test!($parent_module::$child_module, $ext_deposit, $dust_trap, shelve_done_shelve); - generate_test!($parent_module::$child_module, $ext_deposit, $dust_trap, transfer_success); - generate_test!( - $parent_module::$child_module, - $ext_deposit, - $dust_trap, - transfer_expendable_all - ); - generate_test!( - $parent_module::$child_module, - $ext_deposit, - $dust_trap, - transfer_expendable_dust - ); - generate_test!( - $parent_module::$child_module, - $ext_deposit, - $dust_trap, - transfer_protect_preserve - ); - generate_test!( - $parent_module::$child_module, - $ext_deposit, - $dust_trap, - transfer_done_transfer - ); - generate_test!( - $parent_module::$child_module, - $ext_deposit, - $dust_trap, - set_balance_mint_success - ); - generate_test!( - $parent_module::$child_module, - $ext_deposit, - $dust_trap, - set_balance_burn_success - ); - generate_test!( - $parent_module::$child_module, - $ext_deposit, - $dust_trap, - can_deposit_success - ); - generate_test!( - $parent_module::$child_module, - $ext_deposit, - $dust_trap, - can_deposit_below_minimum - ); - generate_test!( - $parent_module::$child_module, - $ext_deposit, - $dust_trap, - can_deposit_overflow - ); - generate_test!( - $parent_module::$child_module, - $ext_deposit, - $dust_trap, - can_withdraw_success - ); - generate_test!( - $parent_module::$child_module, - $ext_deposit, - $dust_trap, - can_withdraw_reduced_to_zero - ); - generate_test!( - $parent_module::$child_module, - $ext_deposit, - $dust_trap, - can_withdraw_balance_low - ); - generate_test!( - $parent_module::$child_module, - $ext_deposit, - $dust_trap, - reducible_balance_expendable - ); - generate_test!( - $parent_module::$child_module, - $ext_deposit, - $dust_trap, - reducible_balance_protect_preserve - ); + ($pm:tt :: $cm:tt, $ext_deposit:expr, $dust_trap:expr) => { + generate_test!($pm::$cm, $ext_deposit, $dust_trap, mint_into_success); + generate_test!($pm::$cm, $ext_deposit, $dust_trap, mint_into_overflow); + generate_test!($pm::$cm, $ext_deposit, $dust_trap, mint_into_below_minimum); + generate_test!($pm::$cm, $ext_deposit, $dust_trap, mint_into_done_mint_into); + generate_test!($pm::$cm, $ext_deposit, $dust_trap, burn_from_exact_success); + generate_test!($pm::$cm, $ext_deposit, $dust_trap, burn_from_best_effort_success); + generate_test!($pm::$cm, $ext_deposit, $dust_trap, burn_from_exact_insufficient_funds); + generate_test!($pm::$cm, $ext_deposit, $dust_trap, restore_success); + generate_test!($pm::$cm, $ext_deposit, $dust_trap, restore_overflow); + generate_test!($pm::$cm, $ext_deposit, $dust_trap, restore_below_minimum); + generate_test!($pm::$cm, $ext_deposit, $dust_trap, restore_done_restore); + generate_test!($pm::$cm, $ext_deposit, $dust_trap, shelve_success); + generate_test!($pm::$cm, $ext_deposit, $dust_trap, shelve_insufficient_funds); + generate_test!($pm::$cm, $ext_deposit, $dust_trap, shelve_done_shelve); + generate_test!($pm::$cm, $ext_deposit, $dust_trap, transfer_success); + generate_test!($pm::$cm, $ext_deposit, $dust_trap, transfer_expendable_all); + generate_test!($pm::$cm, $ext_deposit, $dust_trap, transfer_expendable_dust); + generate_test!($pm::$cm, $ext_deposit, $dust_trap, transfer_protect_preserve); + generate_test!($pm::$cm, $ext_deposit, $dust_trap, transfer_done_transfer); + generate_test!($pm::$cm, $ext_deposit, $dust_trap, set_balance_mint_success); + generate_test!($pm::$cm, $ext_deposit, $dust_trap, set_balance_burn_success); + generate_test!($pm::$cm, $ext_deposit, $dust_trap, can_deposit_success); + generate_test!($pm::$cm, $ext_deposit, $dust_trap, can_deposit_below_minimum); + generate_test!($pm::$cm, $ext_deposit, $dust_trap, can_deposit_overflow); + generate_test!($pm::$cm, $ext_deposit, $dust_trap, can_withdraw_success); + generate_test!($pm::$cm, $ext_deposit, $dust_trap, can_withdraw_reduced_to_zero); + generate_test!($pm::$cm, $ext_deposit, $dust_trap, can_withdraw_balance_low); + generate_test!($pm::$cm, $ext_deposit, $dust_trap, reducible_balance_expendable); + generate_test!($pm::$cm, $ext_deposit, $dust_trap, reducible_balance_protect_preserve); }; } From 16858567c1351ff138063f988b464bb2520634e4 Mon Sep 17 00:00:00 2001 From: Liam Aharon Date: Mon, 10 Apr 2023 17:36:59 +0400 Subject: [PATCH 21/37] improve variable naming --- .../src/tests/fungible_conformance_tests.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/frame/balances/src/tests/fungible_conformance_tests.rs b/frame/balances/src/tests/fungible_conformance_tests.rs index 49e86f47d88cb..285a9d0c30dc9 100644 --- a/frame/balances/src/tests/fungible_conformance_tests.rs +++ b/frame/balances/src/tests/fungible_conformance_tests.rs @@ -56,7 +56,7 @@ macro_rules! generate_test { // $pm = parent module // $cm = child module // shorthand so each generate_test! call can fit on one line -macro_rules! run_tests { +macro_rules! generate_all_tests { ($pm:tt :: $cm:tt, $ext_deposit:expr, $dust_trap:expr) => { generate_test!($pm::$cm, $ext_deposit, $dust_trap, mint_into_success); generate_test!($pm::$cm, $ext_deposit, $dust_trap, mint_into_overflow); @@ -90,11 +90,11 @@ macro_rules! run_tests { }; } -run_tests!(conformance_tests::inspect_mutate, 1, "off"); -run_tests!(conformance_tests::inspect_mutate, 1, "on"); -run_tests!(conformance_tests::inspect_mutate, 2, "off"); -run_tests!(conformance_tests::inspect_mutate, 2, "on"); -run_tests!(conformance_tests::inspect_mutate, 5, "off"); -run_tests!(conformance_tests::inspect_mutate, 5, "on"); -run_tests!(conformance_tests::inspect_mutate, 1000, "off"); -run_tests!(conformance_tests::inspect_mutate, 1000, "on"); +generate_all_tests!(conformance_tests::inspect_mutate, 1, "off"); +generate_all_tests!(conformance_tests::inspect_mutate, 1, "on"); +generate_all_tests!(conformance_tests::inspect_mutate, 2, "off"); +generate_all_tests!(conformance_tests::inspect_mutate, 2, "on"); +generate_all_tests!(conformance_tests::inspect_mutate, 5, "off"); +generate_all_tests!(conformance_tests::inspect_mutate, 5, "on"); +generate_all_tests!(conformance_tests::inspect_mutate, 1000, "off"); +generate_all_tests!(conformance_tests::inspect_mutate, 1000, "on"); From 1b135097c9cb12daffa3dccdf742d9a4f4ca8e81 Mon Sep 17 00:00:00 2001 From: Liam Aharon Date: Mon, 10 Apr 2023 17:41:04 +0400 Subject: [PATCH 22/37] remove redundant comment --- frame/balances/src/tests/fungible_tests.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/frame/balances/src/tests/fungible_tests.rs b/frame/balances/src/tests/fungible_tests.rs index 0d12dd7ce0b15..128086885391f 100644 --- a/frame/balances/src/tests/fungible_tests.rs +++ b/frame/balances/src/tests/fungible_tests.rs @@ -16,8 +16,6 @@ // limitations under the License. //! Tests regarding the functionality of the `fungible` trait set implementations. -//! -//! TODO: Bundle these tests into the conformance tests & remove them. use super::*; use frame_support::traits::tokens::{ From 884550401dcfe03c5d17060679f7197c38b1d8ad Mon Sep 17 00:00:00 2001 From: Liam Aharon Date: Mon, 10 Apr 2023 17:42:40 +0400 Subject: [PATCH 23/37] remove placeholder tests --- .../conformance_tests/inspect_mutate.rs | 40 ------------------- 1 file changed, 40 deletions(-) diff --git a/frame/support/src/traits/tokens/fungible/conformance_tests/inspect_mutate.rs b/frame/support/src/traits/tokens/fungible/conformance_tests/inspect_mutate.rs index 225f883c52001..a41b302be2a25 100644 --- a/frame/support/src/traits/tokens/fungible/conformance_tests/inspect_mutate.rs +++ b/frame/support/src/traits/tokens/fungible/conformance_tests/inspect_mutate.rs @@ -108,16 +108,6 @@ where assert_eq!(T::active_issuance(), initial_active_issuance); } -pub fn mint_into_done_mint_into(_dust_trap: Option) -where - T: Mutate + Inspect, - AccountId: AtLeast8BitUnsigned, - Balance: AtLeast8BitUnsigned + Debug, -{ - // TODO: How can we test this? - assert!(true); -} - // // burn_from // @@ -295,16 +285,6 @@ where assert_eq!(T::active_issuance(), initial_active_issuance); } -pub fn restore_done_restore(_dust_trap: Option) -where - T: Mutate + Inspect, - AccountId: AtLeast8BitUnsigned, - Balance: AtLeast8BitUnsigned + Debug, -{ - // TODO: How can we test this? - assert!(true); -} - // // shelve tests // @@ -363,16 +343,6 @@ where assert_eq!(T::active_issuance(), initial_active_issuance + initial_balance); } -pub fn shelve_done_shelve(_dust_trap: Option) -where - T: Mutate + Inspect, - AccountId: AtLeast8BitUnsigned, - Balance: AtLeast8BitUnsigned + Debug, -{ - // TODO: How can we test this? - assert!(true); -} - // // transfer // @@ -550,16 +520,6 @@ where assert_eq!(T::active_issuance(), initial_active_issuance + initial_balance * 2.into()); } -pub fn transfer_done_transfer(_dust_trap: Option) -where - T: Mutate + Inspect, - AccountId: AtLeast8BitUnsigned, - Balance: AtLeast8BitUnsigned + Debug, -{ - // TODO: How can we test this? - assert!(true); -} - // // set_balance // From 8b1b707380dc17d149ee507a30fdb821812520d9 Mon Sep 17 00:00:00 2001 From: Liam Aharon Date: Mon, 10 Apr 2023 18:29:21 +0400 Subject: [PATCH 24/37] remove placeholder tests --- frame/balances/src/tests/fungible_conformance_tests.rs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/frame/balances/src/tests/fungible_conformance_tests.rs b/frame/balances/src/tests/fungible_conformance_tests.rs index 285a9d0c30dc9..ecff3aba2cfbc 100644 --- a/frame/balances/src/tests/fungible_conformance_tests.rs +++ b/frame/balances/src/tests/fungible_conformance_tests.rs @@ -61,22 +61,18 @@ macro_rules! generate_all_tests { generate_test!($pm::$cm, $ext_deposit, $dust_trap, mint_into_success); generate_test!($pm::$cm, $ext_deposit, $dust_trap, mint_into_overflow); generate_test!($pm::$cm, $ext_deposit, $dust_trap, mint_into_below_minimum); - generate_test!($pm::$cm, $ext_deposit, $dust_trap, mint_into_done_mint_into); generate_test!($pm::$cm, $ext_deposit, $dust_trap, burn_from_exact_success); generate_test!($pm::$cm, $ext_deposit, $dust_trap, burn_from_best_effort_success); generate_test!($pm::$cm, $ext_deposit, $dust_trap, burn_from_exact_insufficient_funds); generate_test!($pm::$cm, $ext_deposit, $dust_trap, restore_success); generate_test!($pm::$cm, $ext_deposit, $dust_trap, restore_overflow); generate_test!($pm::$cm, $ext_deposit, $dust_trap, restore_below_minimum); - generate_test!($pm::$cm, $ext_deposit, $dust_trap, restore_done_restore); generate_test!($pm::$cm, $ext_deposit, $dust_trap, shelve_success); generate_test!($pm::$cm, $ext_deposit, $dust_trap, shelve_insufficient_funds); - generate_test!($pm::$cm, $ext_deposit, $dust_trap, shelve_done_shelve); generate_test!($pm::$cm, $ext_deposit, $dust_trap, transfer_success); generate_test!($pm::$cm, $ext_deposit, $dust_trap, transfer_expendable_all); generate_test!($pm::$cm, $ext_deposit, $dust_trap, transfer_expendable_dust); generate_test!($pm::$cm, $ext_deposit, $dust_trap, transfer_protect_preserve); - generate_test!($pm::$cm, $ext_deposit, $dust_trap, transfer_done_transfer); generate_test!($pm::$cm, $ext_deposit, $dust_trap, set_balance_mint_success); generate_test!($pm::$cm, $ext_deposit, $dust_trap, set_balance_burn_success); generate_test!($pm::$cm, $ext_deposit, $dust_trap, can_deposit_success); From 95286a86011a9639b775a3946e34e32a097019df Mon Sep 17 00:00:00 2001 From: Liam Aharon Date: Tue, 11 Apr 2023 06:42:35 +0400 Subject: [PATCH 25/37] simplify macro --- .../src/tests/fungible_conformance_tests.rs | 142 +++++++++--------- 1 file changed, 73 insertions(+), 69 deletions(-) diff --git a/frame/balances/src/tests/fungible_conformance_tests.rs b/frame/balances/src/tests/fungible_conformance_tests.rs index ecff3aba2cfbc..d05f5b8a40c6d 100644 --- a/frame/balances/src/tests/fungible_conformance_tests.rs +++ b/frame/balances/src/tests/fungible_conformance_tests.rs @@ -19,78 +19,82 @@ use super::*; use frame_support::traits::fungible::{conformance_tests, Inspect, Mutate}; use paste::paste; -macro_rules! generate_test { - ($pm:tt :: $cm:tt, $ext_deposit:expr, $dust_trap:expr, $name:ident) => { - paste! { - #[test] - fn [< $name _ext_deposit_ $ext_deposit _dust_trap_ $dust_trap >]() { - let (trap_account, builder) = match $dust_trap { - "on" => { - let trap_account = ::AccountId::from(65174286u64); - let builder = ExtBuilder::default().existential_deposit($ext_deposit).dust_trap(trap_account); - (Some(trap_account), builder) - }, - "off" => { - let builder = ExtBuilder::default().existential_deposit($ext_deposit); - (None, builder) - }, - _ => panic!("dust_trap must be either \"on\" or \"off\"") - }; - builder.build_and_execute_with(|| { - // Initialise the trap account if it is being used - if let Some(trap_account) = trap_account { - Balances::set_balance(&trap_account, Balances::minimum_balance()); - }; - // Run the test - $pm::$cm::$name::< - Balances, - ::AccountId, - ::Balance, - >(trap_account); - }); - } - } - } -} - // $pm = parent module // $cm = child module -// shorthand so each generate_test! call can fit on one line -macro_rules! generate_all_tests { +// shorthand so each run_tests! call can fit on one line +macro_rules! run_tests { + ($pm:tt :: $cm:tt, $ext_deposit:expr, $dust_trap:expr, $($name:ident),*) => { + $( + paste! { + #[test] + fn [< $name _ext_deposit_ $ext_deposit _dust_trap_ $dust_trap >]() { + let (trap_account, builder) = match $dust_trap { + "on" => { + let trap_account = ::AccountId::from(65174286u64); + let builder = ExtBuilder::default().existential_deposit($ext_deposit).dust_trap(trap_account); + (Some(trap_account), builder) + }, + "off" => { + let builder = ExtBuilder::default().existential_deposit($ext_deposit); + (None, builder) + }, + _ => panic!("dust_trap must be either \"on\" or \"off\"") + }; + builder.build_and_execute_with(|| { + // Initialise the trap account if it is being used + if let Some(trap_account) = trap_account { + Balances::set_balance(&trap_account, Balances::minimum_balance()); + }; + // Run the test + $pm::$cm::$name::< + Balances, + ::AccountId, + ::Balance, + >(trap_account); + }); + } + } + )* + }; ($pm:tt :: $cm:tt, $ext_deposit:expr, $dust_trap:expr) => { - generate_test!($pm::$cm, $ext_deposit, $dust_trap, mint_into_success); - generate_test!($pm::$cm, $ext_deposit, $dust_trap, mint_into_overflow); - generate_test!($pm::$cm, $ext_deposit, $dust_trap, mint_into_below_minimum); - generate_test!($pm::$cm, $ext_deposit, $dust_trap, burn_from_exact_success); - generate_test!($pm::$cm, $ext_deposit, $dust_trap, burn_from_best_effort_success); - generate_test!($pm::$cm, $ext_deposit, $dust_trap, burn_from_exact_insufficient_funds); - generate_test!($pm::$cm, $ext_deposit, $dust_trap, restore_success); - generate_test!($pm::$cm, $ext_deposit, $dust_trap, restore_overflow); - generate_test!($pm::$cm, $ext_deposit, $dust_trap, restore_below_minimum); - generate_test!($pm::$cm, $ext_deposit, $dust_trap, shelve_success); - generate_test!($pm::$cm, $ext_deposit, $dust_trap, shelve_insufficient_funds); - generate_test!($pm::$cm, $ext_deposit, $dust_trap, transfer_success); - generate_test!($pm::$cm, $ext_deposit, $dust_trap, transfer_expendable_all); - generate_test!($pm::$cm, $ext_deposit, $dust_trap, transfer_expendable_dust); - generate_test!($pm::$cm, $ext_deposit, $dust_trap, transfer_protect_preserve); - generate_test!($pm::$cm, $ext_deposit, $dust_trap, set_balance_mint_success); - generate_test!($pm::$cm, $ext_deposit, $dust_trap, set_balance_burn_success); - generate_test!($pm::$cm, $ext_deposit, $dust_trap, can_deposit_success); - generate_test!($pm::$cm, $ext_deposit, $dust_trap, can_deposit_below_minimum); - generate_test!($pm::$cm, $ext_deposit, $dust_trap, can_deposit_overflow); - generate_test!($pm::$cm, $ext_deposit, $dust_trap, can_withdraw_success); - generate_test!($pm::$cm, $ext_deposit, $dust_trap, can_withdraw_reduced_to_zero); - generate_test!($pm::$cm, $ext_deposit, $dust_trap, can_withdraw_balance_low); - generate_test!($pm::$cm, $ext_deposit, $dust_trap, reducible_balance_expendable); - generate_test!($pm::$cm, $ext_deposit, $dust_trap, reducible_balance_protect_preserve); + run_tests!( + $pm::$cm, + $ext_deposit, + $dust_trap, + mint_into_success, + mint_into_overflow, + mint_into_below_minimum, + burn_from_exact_success, + burn_from_best_effort_success, + burn_from_exact_insufficient_funds, + restore_success, + restore_overflow, + restore_below_minimum, + shelve_success, + shelve_insufficient_funds, + transfer_success, + transfer_expendable_all, + transfer_expendable_dust, + transfer_protect_preserve, + set_balance_mint_success, + set_balance_burn_success, + can_deposit_success, + can_deposit_below_minimum, + can_deposit_overflow, + can_withdraw_success, + can_withdraw_reduced_to_zero, + can_withdraw_balance_low, + reducible_balance_expendable, + reducible_balance_protect_preserve + ); }; } -generate_all_tests!(conformance_tests::inspect_mutate, 1, "off"); -generate_all_tests!(conformance_tests::inspect_mutate, 1, "on"); -generate_all_tests!(conformance_tests::inspect_mutate, 2, "off"); -generate_all_tests!(conformance_tests::inspect_mutate, 2, "on"); -generate_all_tests!(conformance_tests::inspect_mutate, 5, "off"); -generate_all_tests!(conformance_tests::inspect_mutate, 5, "on"); -generate_all_tests!(conformance_tests::inspect_mutate, 1000, "off"); -generate_all_tests!(conformance_tests::inspect_mutate, 1000, "on"); +run_tests!(conformance_tests::inspect_mutate, 1, "off"); +run_tests!(conformance_tests::inspect_mutate, 1, "on"); +run_tests!(conformance_tests::inspect_mutate, 2, "off"); +run_tests!(conformance_tests::inspect_mutate, 2, "on"); +run_tests!(conformance_tests::inspect_mutate, 5, "off"); +run_tests!(conformance_tests::inspect_mutate, 5, "on"); +run_tests!(conformance_tests::inspect_mutate, 1000, "off"); +run_tests!(conformance_tests::inspect_mutate, 1000, "on"); From 682f33f1c6101e35bbd32a7ad4884cc9fb597433 Mon Sep 17 00:00:00 2001 From: Liam Aharon Date: Tue, 11 Apr 2023 15:35:07 +1000 Subject: [PATCH 26/37] Update frame/balances/src/tests/fungible_conformance_tests.rs Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com> --- frame/balances/src/tests/fungible_conformance_tests.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frame/balances/src/tests/fungible_conformance_tests.rs b/frame/balances/src/tests/fungible_conformance_tests.rs index d05f5b8a40c6d..d1abbcf4c51df 100644 --- a/frame/balances/src/tests/fungible_conformance_tests.rs +++ b/frame/balances/src/tests/fungible_conformance_tests.rs @@ -1,6 +1,6 @@ // This file is part of Substrate. -// Copyright (C) 2017-2023 Parity Technologies (UK) Ltd. +// Copyright (C) Parity Technologies (UK) Ltd. // SPDX-License-Identifier: Apache-2.0 // Licensed under the Apache License, Version 2.0 (the "License"); From 090e7bb68d354c9a82ccfee10e4911790def33da Mon Sep 17 00:00:00 2001 From: Liam Aharon Date: Tue, 11 Apr 2023 09:55:05 +0400 Subject: [PATCH 27/37] use Balance from T --- .../src/tests/fungible_conformance_tests.rs | 1 - .../conformance_tests/inspect_mutate.rs | 199 +++++++++--------- 2 files changed, 100 insertions(+), 100 deletions(-) diff --git a/frame/balances/src/tests/fungible_conformance_tests.rs b/frame/balances/src/tests/fungible_conformance_tests.rs index d05f5b8a40c6d..157e84aedb20d 100644 --- a/frame/balances/src/tests/fungible_conformance_tests.rs +++ b/frame/balances/src/tests/fungible_conformance_tests.rs @@ -49,7 +49,6 @@ macro_rules! run_tests { $pm::$cm::$name::< Balances, ::AccountId, - ::Balance, >(trap_account); }); } diff --git a/frame/support/src/traits/tokens/fungible/conformance_tests/inspect_mutate.rs b/frame/support/src/traits/tokens/fungible/conformance_tests/inspect_mutate.rs index a41b302be2a25..46796799a1620 100644 --- a/frame/support/src/traits/tokens/fungible/conformance_tests/inspect_mutate.rs +++ b/frame/support/src/traits/tokens/fungible/conformance_tests/inspect_mutate.rs @@ -23,16 +23,17 @@ use crate::traits::{ }; use core::fmt::Debug; use sp_arithmetic::traits::AtLeast8BitUnsigned; +use sp_runtime::traits::{Bounded, Zero}; // // mint_into // -pub fn mint_into_success(_dust_trap: Option) +pub fn mint_into_success(_dust_trap: Option) where - T: Mutate + Inspect, + T: Mutate, + >::Balance: AtLeast8BitUnsigned + Debug, AccountId: AtLeast8BitUnsigned, - Balance: AtLeast8BitUnsigned + Debug, { let initial_total_issuance = T::total_issuance(); let initial_active_issuance = T::active_issuance(); @@ -56,16 +57,16 @@ where assert_eq!(T::active_issuance(), initial_active_issuance + amount_0 + amount_1); } -pub fn mint_into_overflow(_dust_trap: Option) +pub fn mint_into_overflow(_dust_trap: Option) where - T: Mutate + Inspect, + T: Mutate, + >::Balance: AtLeast8BitUnsigned + Debug, AccountId: AtLeast8BitUnsigned, - Balance: AtLeast8BitUnsigned + Debug, { let initial_total_issuance = T::total_issuance(); let initial_active_issuance = T::active_issuance(); let account = AccountId::from(10); - let amount = Balance::max_value() - 5.into() - initial_total_issuance.clone(); + let amount = T::Balance::max_value() - 5.into() - initial_total_issuance.clone(); // Mint just below the maximum balance T::mint_into(&account, amount.clone()).unwrap(); @@ -82,14 +83,14 @@ where assert_eq!(T::active_issuance(), initial_active_issuance + amount); } -pub fn mint_into_below_minimum(_dust_trap: Option) +pub fn mint_into_below_minimum(_dust_trap: Option) where - T: Mutate + Inspect, + T: Mutate, + >::Balance: AtLeast8BitUnsigned + Debug, AccountId: AtLeast8BitUnsigned, - Balance: AtLeast8BitUnsigned + Debug, { // Skip if there is no minimum balance - if T::minimum_balance() == Balance::zero() { + if T::minimum_balance() == T::Balance::zero() { return } @@ -102,8 +103,8 @@ where T::mint_into(&account, amount.clone()).unwrap_err(); // Verify: noop - assert_eq!(T::total_balance(&account), Balance::zero()); - assert_eq!(T::balance(&account), Balance::zero()); + assert_eq!(T::total_balance(&account), T::Balance::zero()); + assert_eq!(T::balance(&account), T::Balance::zero()); assert_eq!(T::total_issuance(), initial_total_issuance); assert_eq!(T::active_issuance(), initial_active_issuance); } @@ -112,11 +113,11 @@ where // burn_from // -pub fn burn_from_exact_success(_dust_trap: Option) +pub fn burn_from_exact_success(_dust_trap: Option) where - T: Mutate + Inspect, + T: Mutate, + >::Balance: AtLeast8BitUnsigned + Debug, AccountId: AtLeast8BitUnsigned, - Balance: AtLeast8BitUnsigned + Debug, { let initial_total_issuance = T::total_issuance(); let initial_active_issuance = T::active_issuance(); @@ -127,7 +128,7 @@ where T::mint_into(&account, initial_balance.clone()).unwrap(); // Test: Burn an exact amount from the account - let amount_to_burn = Balance::from(5); + let amount_to_burn = T::Balance::from(5); let precision = Precision::Exact; let force = Fortitude::Polite; T::burn_from(&account, amount_to_burn.clone(), precision, force).unwrap(); @@ -142,11 +143,11 @@ where assert_eq!(T::active_issuance(), initial_active_issuance + initial_balance - amount_to_burn); } -pub fn burn_from_best_effort_success(_dust_trap: Option) +pub fn burn_from_best_effort_success(_dust_trap: Option) where - T: Mutate + Inspect, + T: Mutate, + >::Balance: AtLeast8BitUnsigned + Debug, AccountId: AtLeast8BitUnsigned, - Balance: AtLeast8BitUnsigned + Debug, { let initial_total_issuance = T::total_issuance(); let initial_active_issuance = T::active_issuance(); @@ -177,11 +178,11 @@ where assert_eq!(T::active_issuance(), initial_active_issuance + initial_balance - reducible_balance); } -pub fn burn_from_exact_insufficient_funds(_dust_trap: Option) +pub fn burn_from_exact_insufficient_funds(_dust_trap: Option) where - T: Mutate + Inspect, + T: Mutate, + >::Balance: AtLeast8BitUnsigned + Debug, AccountId: AtLeast8BitUnsigned, - Balance: AtLeast8BitUnsigned + Debug, { // Set up the initial conditions and parameters for the test let account = AccountId::from(5); @@ -207,11 +208,11 @@ where // restore // -pub fn restore_success(_dust_trap: Option) +pub fn restore_success(_dust_trap: Option) where - T: Mutate + Inspect, + T: Mutate, + >::Balance: AtLeast8BitUnsigned + Debug, AccountId: AtLeast8BitUnsigned, - Balance: AtLeast8BitUnsigned + Debug, { let account_0 = AccountId::from(0); let account_1 = AccountId::from(1); @@ -235,16 +236,16 @@ where assert_eq!(T::active_issuance(), initial_active_issuance + amount_0 + amount_1); } -pub fn restore_overflow(_dust_trap: Option) +pub fn restore_overflow(_dust_trap: Option) where - T: Mutate + Inspect, + T: Mutate, + >::Balance: AtLeast8BitUnsigned + Debug, AccountId: AtLeast8BitUnsigned, - Balance: AtLeast8BitUnsigned + Debug, { let initial_total_issuance = T::total_issuance(); let initial_active_issuance = T::active_issuance(); let account = AccountId::from(10); - let amount = Balance::max_value() - 5.into() - initial_total_issuance.clone(); + let amount = T::Balance::max_value() - 5.into() - initial_total_issuance.clone(); // Restore just below the maximum balance T::restore(&account, amount.clone()).unwrap(); @@ -259,14 +260,14 @@ where assert_eq!(T::active_issuance(), initial_active_issuance + amount); } -pub fn restore_below_minimum(_dust_trap: Option) +pub fn restore_below_minimum(_dust_trap: Option) where - T: Mutate + Inspect, + T: Mutate, + >::Balance: AtLeast8BitUnsigned + Debug, AccountId: AtLeast8BitUnsigned, - Balance: AtLeast8BitUnsigned + Debug, { // Skip if there is no minimum balance - if T::minimum_balance() == Balance::zero() { + if T::minimum_balance() == T::Balance::zero() { return } @@ -279,8 +280,8 @@ where T::restore(&account, amount.clone()).unwrap_err(); // Verify: noop - assert_eq!(T::total_balance(&account), Balance::zero()); - assert_eq!(T::balance(&account), Balance::zero()); + assert_eq!(T::total_balance(&account), T::Balance::zero()); + assert_eq!(T::balance(&account), T::Balance::zero()); assert_eq!(T::total_issuance(), initial_total_issuance); assert_eq!(T::active_issuance(), initial_active_issuance); } @@ -289,11 +290,11 @@ where // shelve tests // -pub fn shelve_success(_dust_trap: Option) +pub fn shelve_success(_dust_trap: Option) where - T: Mutate + Inspect, + T: Mutate, + >::Balance: AtLeast8BitUnsigned + Debug, AccountId: AtLeast8BitUnsigned, - Balance: AtLeast8BitUnsigned + Debug, { let initial_total_issuance = T::total_issuance(); let initial_active_issuance = T::active_issuance(); @@ -305,7 +306,7 @@ where T::restore(&account, initial_balance.clone()).unwrap(); // Test: Shelve an amount from the account - let amount_to_shelve = Balance::from(5); + let amount_to_shelve = T::Balance::from(5); T::shelve(&account, amount_to_shelve.clone()).unwrap(); // Verify: The balance and total issuance should be reduced by the shelved amount @@ -318,11 +319,11 @@ where assert_eq!(T::active_issuance(), initial_active_issuance + initial_balance - amount_to_shelve); } -pub fn shelve_insufficient_funds(_dust_trap: Option) +pub fn shelve_insufficient_funds(_dust_trap: Option) where - T: Mutate + Inspect, + T: Mutate, + >::Balance: AtLeast8BitUnsigned + Debug, AccountId: AtLeast8BitUnsigned, - Balance: AtLeast8BitUnsigned + Debug, { let initial_total_issuance = T::total_issuance(); let initial_active_issuance = T::active_issuance(); @@ -347,11 +348,11 @@ where // transfer // -pub fn transfer_success(_dust_trap: Option) +pub fn transfer_success(_dust_trap: Option) where - T: Mutate + Inspect, + T: Mutate, + >::Balance: AtLeast8BitUnsigned + Debug, AccountId: AtLeast8BitUnsigned, - Balance: AtLeast8BitUnsigned + Debug, { let initial_total_issuance = T::total_issuance(); let initial_active_issuance = T::active_issuance(); @@ -362,7 +363,7 @@ where T::set_balance(&account_1, initial_balance.clone()); // Test: Transfer an amount from account_0 to account_1 - let transfer_amount = Balance::from(3); + let transfer_amount = T::Balance::from(3); T::transfer(&account_0, &account_1, transfer_amount.clone(), Preservation::Expendable).unwrap(); // Verify: Account balances are updated correctly @@ -376,11 +377,11 @@ where assert_eq!(T::active_issuance(), initial_active_issuance + initial_balance * 2.into()); } -pub fn transfer_expendable_all(_dust_trap: Option) +pub fn transfer_expendable_all(_dust_trap: Option) where - T: Mutate + Inspect, + T: Mutate, + >::Balance: AtLeast8BitUnsigned + Debug, AccountId: AtLeast8BitUnsigned, - Balance: AtLeast8BitUnsigned + Debug, { let initial_total_issuance = T::total_issuance(); let initial_active_issuance = T::active_issuance(); @@ -396,9 +397,9 @@ where T::transfer(&account_0, &account_1, transfer_amount.clone(), preservation).unwrap(); // Verify: Account balances are updated correctly - assert_eq!(T::total_balance(&account_0), Balance::zero()); + assert_eq!(T::total_balance(&account_0), T::Balance::zero()); assert_eq!(T::total_balance(&account_1), initial_balance.clone() * 2.into()); - assert_eq!(T::balance(&account_0), Balance::zero()); + assert_eq!(T::balance(&account_0), T::Balance::zero()); assert_eq!(T::balance(&account_1), initial_balance.clone() * 2.into()); // Verify: Total issuance doesn't change @@ -406,13 +407,13 @@ where assert_eq!(T::active_issuance(), initial_active_issuance + initial_balance * 2.into()); } -pub fn transfer_expendable_dust(dust_trap: Option) +pub fn transfer_expendable_dust(dust_trap: Option) where - T: Mutate + Inspect, + T: Mutate, + >::Balance: AtLeast8BitUnsigned + Debug, AccountId: AtLeast8BitUnsigned, - Balance: AtLeast8BitUnsigned + Debug, { - if T::minimum_balance() == Balance::zero() { + if T::minimum_balance() == T::Balance::zero() { return } @@ -426,18 +427,18 @@ where let initial_active_issuance = T::active_issuance(); let initial_dust_trap_balance = match dust_trap.clone() { Some(dust_trap) => T::total_balance(&dust_trap), - None => Balance::zero(), + None => T::Balance::zero(), }; // Test: Transfer balance let preservation = Preservation::Expendable; - let transfer_amount = Balance::from(11); + let transfer_amount = T::Balance::from(11); T::transfer(&account_0, &account_1, transfer_amount.clone(), preservation).unwrap(); // Verify: Account balances are updated correctly - assert_eq!(T::total_balance(&account_0), Balance::zero()); + assert_eq!(T::total_balance(&account_0), T::Balance::zero()); assert_eq!(T::total_balance(&account_1), initial_balance.clone() + transfer_amount.clone()); - assert_eq!(T::balance(&account_0), Balance::zero()); + assert_eq!(T::balance(&account_0), T::Balance::zero()); assert_eq!(T::balance(&account_1), initial_balance.clone() + transfer_amount.clone()); match dust_trap { @@ -469,14 +470,14 @@ where } } -pub fn transfer_protect_preserve(_dust_trap: Option) +pub fn transfer_protect_preserve(_dust_trap: Option) where - T: Mutate + Inspect, + T: Mutate, + >::Balance: AtLeast8BitUnsigned + Debug, AccountId: AtLeast8BitUnsigned, - Balance: AtLeast8BitUnsigned + Debug, { // This test means nothing if there is no minimum balance - if T::minimum_balance() == Balance::zero() { + if T::minimum_balance() == T::Balance::zero() { return } @@ -524,11 +525,11 @@ where // set_balance // -pub fn set_balance_mint_success(_dust_trap: Option) +pub fn set_balance_mint_success(_dust_trap: Option) where - T: Mutate + Inspect, + T: Mutate, + >::Balance: AtLeast8BitUnsigned + Debug, AccountId: AtLeast8BitUnsigned, - Balance: AtLeast8BitUnsigned + Debug, { let initial_total_issuance = T::total_issuance(); let initial_active_issuance = T::active_issuance(); @@ -537,7 +538,7 @@ where T::mint_into(&account, initial_balance.clone()).unwrap(); // Test: Increase the account balance with set_balance - let increase_amount: Balance = 5.into(); + let increase_amount: T::Balance = 5.into(); let new = T::set_balance(&account, initial_balance.clone() + increase_amount.clone()); // Verify: set_balance returned the new balance @@ -551,11 +552,11 @@ where assert_eq!(T::active_issuance(), initial_active_issuance + expected_new); } -pub fn set_balance_burn_success(_dust_trap: Option) +pub fn set_balance_burn_success(_dust_trap: Option) where - T: Mutate + Inspect, + T: Mutate, + >::Balance: AtLeast8BitUnsigned + Debug, AccountId: AtLeast8BitUnsigned, - Balance: AtLeast8BitUnsigned + Debug, { let initial_total_issuance = T::total_issuance(); let initial_active_issuance = T::active_issuance(); @@ -564,7 +565,7 @@ where T::mint_into(&account, initial_balance.clone()).unwrap(); // Test: Increase the account balance with set_balance - let burn_amount: Balance = 5.into(); + let burn_amount: T::Balance = 5.into(); let new = T::set_balance(&account, initial_balance.clone() - burn_amount.clone()); // Verify: set_balance returned the new balance @@ -582,11 +583,11 @@ where // can_deposit // -pub fn can_deposit_success(_dust_trap: Option) +pub fn can_deposit_success(_dust_trap: Option) where - T: Mutate + Inspect, + T: Mutate, + >::Balance: AtLeast8BitUnsigned + Debug, AccountId: AtLeast8BitUnsigned, - Balance: AtLeast8BitUnsigned + Debug, { let account = AccountId::from(10); let initial_balance = T::minimum_balance() + 10.into(); @@ -599,11 +600,11 @@ where assert_eq!(ret, DepositConsequence::Success); } -pub fn can_deposit_below_minimum(_dust_trap: Option) +pub fn can_deposit_below_minimum(_dust_trap: Option) where - T: Mutate + Inspect, + T: Mutate, + >::Balance: AtLeast8BitUnsigned + Debug, AccountId: AtLeast8BitUnsigned, - Balance: AtLeast8BitUnsigned + Debug, { // can_deposit always returns Success for amount 0 if T::minimum_balance() < 2.into() { @@ -619,16 +620,16 @@ where assert_eq!(ret, DepositConsequence::BelowMinimum); } -pub fn can_deposit_overflow(_dust_trap: Option) +pub fn can_deposit_overflow(_dust_trap: Option) where - T: Mutate + Inspect, + T: Mutate, + >::Balance: AtLeast8BitUnsigned + Debug, AccountId: AtLeast8BitUnsigned, - Balance: AtLeast8BitUnsigned + Debug, { let account = AccountId::from(10); // Test: Try deposit over the max balance - let initial_balance = Balance::max_value() - 5.into() - T::total_issuance(); + let initial_balance = T::Balance::max_value() - 5.into() - T::total_issuance(); T::mint_into(&account, initial_balance.clone()).unwrap(); let ret = T::can_deposit(&account, 10.into(), Provenance::Minted); @@ -640,11 +641,11 @@ where // can_withdraw // -pub fn can_withdraw_success(_dust_trap: Option) +pub fn can_withdraw_success(_dust_trap: Option) where - T: Mutate + Inspect, + T: Mutate, + >::Balance: AtLeast8BitUnsigned + Debug, AccountId: AtLeast8BitUnsigned, - Balance: AtLeast8BitUnsigned + Debug, { let account = AccountId::from(10); let initial_balance = T::minimum_balance() + 10.into(); @@ -657,13 +658,13 @@ where assert_eq!(ret, WithdrawConsequence::Success); } -pub fn can_withdraw_reduced_to_zero(_dust_trap: Option) +pub fn can_withdraw_reduced_to_zero(_dust_trap: Option) where - T: Mutate + Inspect, + T: Mutate, + >::Balance: AtLeast8BitUnsigned + Debug, AccountId: AtLeast8BitUnsigned, - Balance: AtLeast8BitUnsigned + Debug, { - if T::minimum_balance() == Balance::zero() { + if T::minimum_balance() == T::Balance::zero() { return } @@ -676,13 +677,13 @@ where assert_eq!(ret, WithdrawConsequence::ReducedToZero(T::minimum_balance() - 1.into())); } -pub fn can_withdraw_balance_low(_dust_trap: Option) +pub fn can_withdraw_balance_low(_dust_trap: Option) where - T: Mutate + Inspect, + T: Mutate, + >::Balance: AtLeast8BitUnsigned + Debug, AccountId: AtLeast8BitUnsigned, - Balance: AtLeast8BitUnsigned + Debug, { - if T::minimum_balance() == Balance::zero() { + if T::minimum_balance() == T::Balance::zero() { return } @@ -701,11 +702,11 @@ where // reducible_balance // -pub fn reducible_balance_expendable(_dust_trap: Option) +pub fn reducible_balance_expendable(_dust_trap: Option) where - T: Mutate + Inspect, + T: Mutate, + >::Balance: AtLeast8BitUnsigned + Debug, AccountId: AtLeast8BitUnsigned, - Balance: AtLeast8BitUnsigned + Debug, { let account = AccountId::from(10); let initial_balance = T::minimum_balance() + 10.into(); @@ -716,11 +717,11 @@ where assert_eq!(ret, initial_balance); } -pub fn reducible_balance_protect_preserve(_dust_trap: Option) +pub fn reducible_balance_protect_preserve(_dust_trap: Option) where - T: Mutate + Inspect, + T: Mutate, + >::Balance: AtLeast8BitUnsigned + Debug, AccountId: AtLeast8BitUnsigned, - Balance: AtLeast8BitUnsigned + Debug, { let account = AccountId::from(10); let initial_balance = T::minimum_balance() + 10.into(); From dd7ae890c2eb96a673626c29c5a9a8973ce800df Mon Sep 17 00:00:00 2001 From: Liam Aharon Date: Tue, 11 Apr 2023 09:56:11 +0400 Subject: [PATCH 28/37] fix copyright --- .../traits/tokens/fungible/conformance_tests/inspect_mutate.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frame/support/src/traits/tokens/fungible/conformance_tests/inspect_mutate.rs b/frame/support/src/traits/tokens/fungible/conformance_tests/inspect_mutate.rs index 46796799a1620..40108b9fba28d 100644 --- a/frame/support/src/traits/tokens/fungible/conformance_tests/inspect_mutate.rs +++ b/frame/support/src/traits/tokens/fungible/conformance_tests/inspect_mutate.rs @@ -1,6 +1,6 @@ // This file is part of Substrate. -// Copyright (C) 2017-2023 Parity Technologies (UK) Ltd. +// Copyright (C) Parity Technologies (UK) Ltd. // SPDX-License-Identifier: Apache-2.0 // Licensed under the Apache License, Version 2.0 (the "License"); From 880aeb00e8aa48d7409af2d723ff1c1dfff7ab3b Mon Sep 17 00:00:00 2001 From: Liam Aharon Date: Tue, 11 Apr 2023 10:40:39 +0400 Subject: [PATCH 29/37] add test doc comments --- .../conformance_tests/inspect_mutate.rs | 277 +++++++++++++++--- 1 file changed, 241 insertions(+), 36 deletions(-) diff --git a/frame/support/src/traits/tokens/fungible/conformance_tests/inspect_mutate.rs b/frame/support/src/traits/tokens/fungible/conformance_tests/inspect_mutate.rs index 40108b9fba28d..ca5756c70f57f 100644 --- a/frame/support/src/traits/tokens/fungible/conformance_tests/inspect_mutate.rs +++ b/frame/support/src/traits/tokens/fungible/conformance_tests/inspect_mutate.rs @@ -25,10 +25,16 @@ use core::fmt::Debug; use sp_arithmetic::traits::AtLeast8BitUnsigned; use sp_runtime::traits::{Bounded, Zero}; -// -// mint_into -// - +/// Test the `mint_into` function for successful token minting. +/// +/// This test checks the `mint_into` function in the `Mutate` trait implementation for type `T`. +/// It ensures that account balances and total issuance values are updated correctly after minting +/// tokens into two distinct accounts. +/// +/// # Type Parameters +/// +/// - `T`: Implements `Mutate`. +/// - `AccountId`: Account identifier implementing `AtLeast8BitUnsigned`. pub fn mint_into_success(_dust_trap: Option) where T: Mutate, @@ -57,6 +63,15 @@ where assert_eq!(T::active_issuance(), initial_active_issuance + amount_0 + amount_1); } +/// Test the `mint_into` function for overflow prevention. +/// +/// This test ensures that minting tokens beyond the maximum balance value for an account +/// returns an error and does not change the account balance or total issuance values. +/// +/// # Type Parameters +/// +/// - `T`: Implements `Mutate`. +/// - `AccountId`: Account identifier implementing `AtLeast8BitUnsigned`. pub fn mint_into_overflow(_dust_trap: Option) where T: Mutate, @@ -83,6 +98,15 @@ where assert_eq!(T::active_issuance(), initial_active_issuance + amount); } +/// Test the `mint_into` function for handling balances below the minimum value. +/// +/// This test verifies that minting tokens below the minimum balance for an account +/// returns an error and has no impact on the account balance or total issuance values. +/// +/// # Type Parameters +/// +/// - `T`: Implements `Mutate`. +/// - `AccountId`: Account identifier implementing `AtLeast8BitUnsigned`. pub fn mint_into_below_minimum(_dust_trap: Option) where T: Mutate, @@ -109,10 +133,15 @@ where assert_eq!(T::active_issuance(), initial_active_issuance); } -// -// burn_from -// - +/// Test the `burn_from` function for successfully burning an exact amount of tokens. +/// +/// This test checks that the `burn_from` function with `Precision::Exact` correctly +/// reduces the account balance and total issuance values by the burned amount. +/// +/// # Type Parameters +/// +/// - `T`: Implements `Mutate` for `AccountId`. +/// - `AccountId`: Account identifier implementing `AtLeast8BitUnsigned`. pub fn burn_from_exact_success(_dust_trap: Option) where T: Mutate, @@ -143,6 +172,16 @@ where assert_eq!(T::active_issuance(), initial_active_issuance + initial_balance - amount_to_burn); } +/// Test the `burn_from` function for successfully burning tokens with a best-effort approach. +/// +/// This test verifies that the `burn_from` function with `Precision::BestEffort` correctly +/// reduces the account balance and total issuance values by the reducible balance when +/// attempting to burn an amount greater than the reducible balance. +/// +/// # Type Parameters +/// +/// - `T`: Implements `Mutate` for `AccountId`. +/// - `AccountId`: Account identifier implementing `AtLeast8BitUnsigned`. pub fn burn_from_best_effort_success(_dust_trap: Option) where T: Mutate, @@ -178,6 +217,16 @@ where assert_eq!(T::active_issuance(), initial_active_issuance + initial_balance - reducible_balance); } +/// Test the `burn_from` function for handling insufficient funds with `Precision::Exact`. +/// +/// This test verifies that burning an amount greater than the account's balance with +/// `Precision::Exact` returns an error and does not change the account balance or total issuance +/// values. +/// +/// # Type Parameters +/// +/// - `T`: Implements `Mutate`. +/// - `AccountId`: Account identifier implementing `AtLeast8BitUnsigned`. pub fn burn_from_exact_insufficient_funds(_dust_trap: Option) where T: Mutate, @@ -204,10 +253,15 @@ where assert_eq!(T::active_issuance(), initial_active_issuance); } -// -// restore -// - +/// Test the `restore` function for successful restoration. +/// +/// This test verifies that restoring an amount into each account updates their balances and the +/// total issuance values correctly. +/// +/// # Type Parameters +/// +/// - `T`: Implements `Mutate`. +/// - `AccountId`: Account identifier implementing `AtLeast8BitUnsigned`. pub fn restore_success(_dust_trap: Option) where T: Mutate, @@ -236,6 +290,15 @@ where assert_eq!(T::active_issuance(), initial_active_issuance + amount_0 + amount_1); } +/// Test the `restore` function for handling balance overflow. +/// +/// This test verifies that restoring an amount beyond the maximum balance returns an error and +/// does not change the account balance or total issuance values. +/// +/// # Type Parameters +/// +/// - `T`: Implements `Mutate`. +/// - `AccountId`: Account identifier implementing `AtLeast8BitUnsigned`. pub fn restore_overflow(_dust_trap: Option) where T: Mutate, @@ -260,6 +323,15 @@ where assert_eq!(T::active_issuance(), initial_active_issuance + amount); } +/// Test the `restore` function for handling restoration below the minimum balance. +/// +/// This test verifies that restoring an amount below the minimum balance returns an error and +/// does not change the account balance or total issuance values. +/// +/// # Type Parameters +/// +/// - `T`: Implements `Mutate`. +/// - `AccountId`: Account identifier implementing `AtLeast8BitUnsigned`. pub fn restore_below_minimum(_dust_trap: Option) where T: Mutate, @@ -286,10 +358,15 @@ where assert_eq!(T::active_issuance(), initial_active_issuance); } -// -// shelve tests -// - +/// Test the `shelve` function for successful shelving. +/// +/// This test verifies that shelving an amount from an account reduces the account balance and +/// total issuance values by the shelved amount. +/// +/// # Type Parameters +/// +/// - `T`: Implements `Mutate`. +/// - `AccountId`: Account identifier implementing `AtLeast8BitUnsigned`. pub fn shelve_success(_dust_trap: Option) where T: Mutate, @@ -319,6 +396,15 @@ where assert_eq!(T::active_issuance(), initial_active_issuance + initial_balance - amount_to_shelve); } +/// Test the `shelve` function for handling insufficient funds. +/// +/// This test verifies that attempting to shelve an amount greater than the account's balance +/// returns an error and does not change the account balance or total issuance values. +/// +/// # Type Parameters +/// +/// - `T`: Implements `Mutate`. +/// - `AccountId`: Account identifier implementing `AtLeast8BitUnsigned`. pub fn shelve_insufficient_funds(_dust_trap: Option) where T: Mutate, @@ -344,10 +430,16 @@ where assert_eq!(T::active_issuance(), initial_active_issuance + initial_balance); } -// -// transfer -// - +/// Test the `transfer` function for a successful transfer. +/// +/// This test verifies that transferring an amount between two accounts with +/// `Preservation::Expendable` updates the account balances and maintains the total issuance and +/// active issuance values. +/// +/// # Type Parameters +/// +/// - `T`: Implements `Mutate`. +/// - `AccountId`: Account identifier implementing `AtLeast8BitUnsigned`. pub fn transfer_success(_dust_trap: Option) where T: Mutate, @@ -377,6 +469,17 @@ where assert_eq!(T::active_issuance(), initial_active_issuance + initial_balance * 2.into()); } +/// Test the `transfer` function with `Preservation::Expendable` for transferring the entire +/// balance. +/// +/// This test verifies that transferring the entire balance from one account to another with +/// `Preservation::Expendable` updates the account balances and maintains the total issuance and +/// active issuance values. +/// +/// # Type Parameters +/// +/// - `T`: Implements `Mutate`. +/// - `AccountId`: Account identifier implementing `AtLeast8BitUnsigned`. pub fn transfer_expendable_all(_dust_trap: Option) where T: Mutate, @@ -407,6 +510,23 @@ where assert_eq!(T::active_issuance(), initial_active_issuance + initial_balance * 2.into()); } +/// Test the transfer function with Preservation::Expendable for transferring amounts that leaves +/// an account with less than the minimum balance. +/// +/// This test verifies that when transferring an amount using Preservation::Expendable and an +/// account will be left with less than the minimum balance, the account balances are updated, dust +/// is collected properly depending on whether a dust_trap exists, and the total issuance and active +/// issuance values remain consistent. +/// +/// # Parameters +/// +/// - dust_trap: An optional account identifier to which dust will be collected. If None, dust will +/// be removed from the total and active issuance. +/// +/// # Type Parameters +/// +/// - T: Implements Mutate. +/// - AccountId: Account identifier implementing AtLeast8BitUnsigned. pub fn transfer_expendable_dust(dust_trap: Option) where T: Mutate, @@ -470,6 +590,17 @@ where } } +/// Test the `transfer` function with `Preservation::Protect` and `Preservation::Preserve` for +/// transferring the entire balance. +/// +/// This test verifies that attempting to transfer the entire balance with `Preservation::Protect` +/// or `Preservation::Preserve` returns an error, and the account balances, total issuance, and +/// active issuance values remain unchanged. +/// +/// # Type Parameters +/// +/// - `T`: Implements `Mutate`. +/// - `AccountId`: Account identifier implementing `AtLeast8BitUnsigned`. pub fn transfer_protect_preserve(_dust_trap: Option) where T: Mutate, @@ -521,10 +652,15 @@ where assert_eq!(T::active_issuance(), initial_active_issuance + initial_balance * 2.into()); } -// -// set_balance -// - +/// Test the set_balance function for successful minting. +/// +/// This test verifies that minting a balance using set_balance updates the account balance, total +/// issuance, and active issuance correctly. +/// +/// # Type Parameters +/// +/// - T: Implements Mutate. +/// - AccountId: Account identifier implementing AtLeast8BitUnsigned. pub fn set_balance_mint_success(_dust_trap: Option) where T: Mutate, @@ -552,6 +688,15 @@ where assert_eq!(T::active_issuance(), initial_active_issuance + expected_new); } +/// Test the set_balance function for successful burning. +/// +/// This test verifies that burning a balance using set_balance updates the account balance, total +/// issuance, and active issuance correctly. +/// +/// # Type Parameters +/// +/// - T: Implements Mutate. +/// - AccountId: Account identifier implementing AtLeast8BitUnsigned. pub fn set_balance_burn_success(_dust_trap: Option) where T: Mutate, @@ -579,10 +724,15 @@ where assert_eq!(T::active_issuance(), initial_active_issuance + expected_new); } -// -// can_deposit -// - +/// Test the can_deposit function for returning a success value. +/// +/// This test verifies that the can_deposit function returns DepositConsequence::Success when +/// depositing a reasonable amount. +/// +/// # Type Parameters +/// +/// - T: Implements Mutate. +/// - AccountId: Account identifier implementing AtLeast8BitUnsigned. pub fn can_deposit_success(_dust_trap: Option) where T: Mutate, @@ -600,6 +750,15 @@ where assert_eq!(ret, DepositConsequence::Success); } +/// Test the can_deposit function for returning a minimum balance error. +/// +/// This test verifies that the can_deposit function returns DepositConsequence::BelowMinimum when +/// depositing below the minimum balance. +/// +/// # Type Parameters +/// +/// - T: Implements Mutate. +/// - AccountId: Account identifier implementing AtLeast8BitUnsigned. pub fn can_deposit_below_minimum(_dust_trap: Option) where T: Mutate, @@ -620,6 +779,15 @@ where assert_eq!(ret, DepositConsequence::BelowMinimum); } +/// Test the can_deposit function for returning an overflow error. +/// +/// This test verifies that the can_deposit function returns DepositConsequence::Overflow when +/// depositing an amount that would cause an overflow. +/// +/// # Type Parameters +/// +/// - T: Implements Mutate. +/// - AccountId: Account identifier implementing AtLeast8BitUnsigned. pub fn can_deposit_overflow(_dust_trap: Option) where T: Mutate, @@ -637,10 +805,15 @@ where assert_eq!(ret, DepositConsequence::Overflow); } -// -// can_withdraw -// - +/// Test the can_withdraw function for returning a success value. +/// +/// This test verifies that the can_withdraw function returns WithdrawConsequence::Success when +/// withdrawing a reasonable amount. +/// +/// # Type Parameters +/// +/// - T: Implements Mutate. +/// - AccountId: Account identifier implementing AtLeast8BitUnsigned. pub fn can_withdraw_success(_dust_trap: Option) where T: Mutate, @@ -658,6 +831,15 @@ where assert_eq!(ret, WithdrawConsequence::Success); } +/// Test the can_withdraw function for withdrawal resulting in a reduced balance of zero. +/// +/// This test verifies that the can_withdraw function returns WithdrawConsequence::ReducedToZero +/// when withdrawing an amount that would reduce the account balance below the minimum balance. +/// +/// # Type Parameters +/// +/// - T: Implements Mutate. +/// - AccountId: Account identifier implementing AtLeast8BitUnsigned. pub fn can_withdraw_reduced_to_zero(_dust_trap: Option) where T: Mutate, @@ -677,6 +859,15 @@ where assert_eq!(ret, WithdrawConsequence::ReducedToZero(T::minimum_balance() - 1.into())); } +/// Test the can_withdraw function for returning a low balance error. +/// +/// This test verifies that the can_withdraw function returns WithdrawConsequence::BalanceLow when +/// withdrawing an amount that would result in an account balance below the current balance. +/// +/// # Type Parameters +/// +/// - T: Implements Mutate. +/// - AccountId: Account identifier implementing AtLeast8BitUnsigned. pub fn can_withdraw_balance_low(_dust_trap: Option) where T: Mutate, @@ -698,10 +889,15 @@ where assert_eq!(ret, WithdrawConsequence::BalanceLow); } -// -// reducible_balance -// - +/// Test the reducible_balance function with Preservation::Expendable. +/// +/// This test verifies that the reducible_balance function returns the full account balance when +/// using Preservation::Expendable. +/// +/// # Type Parameters +/// +/// - T: Implements Mutate. +/// - AccountId: Account identifier implementing AtLeast8BitUnsigned. pub fn reducible_balance_expendable(_dust_trap: Option) where T: Mutate, @@ -717,6 +913,15 @@ where assert_eq!(ret, initial_balance); } +/// Test the reducible_balance function with Preservation::Protect and Preservation::Preserve. +/// +/// This test verifies that the reducible_balance function returns the account balance minus the +/// minimum balance when using either Preservation::Protect or Preservation::Preserve. +/// +/// # Type Parameters +/// +/// - T: Implements Mutate. +/// - AccountId: Account identifier implementing AtLeast8BitUnsigned. pub fn reducible_balance_protect_preserve(_dust_trap: Option) where T: Mutate, From 8d8ffa962dd3e08ae024a76498eedd6732b567a0 Mon Sep 17 00:00:00 2001 From: Liam Aharon Date: Tue, 11 Apr 2023 10:42:19 +0400 Subject: [PATCH 30/37] improve test naming --- frame/balances/src/tests/fungible_conformance_tests.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frame/balances/src/tests/fungible_conformance_tests.rs b/frame/balances/src/tests/fungible_conformance_tests.rs index 1e63451d8ca21..0def3fedf4818 100644 --- a/frame/balances/src/tests/fungible_conformance_tests.rs +++ b/frame/balances/src/tests/fungible_conformance_tests.rs @@ -27,7 +27,7 @@ macro_rules! run_tests { $( paste! { #[test] - fn [< $name _ext_deposit_ $ext_deposit _dust_trap_ $dust_trap >]() { + fn [< $name _existential_deposit_ $ext_deposit _dust_trap_ $dust_trap >]() { let (trap_account, builder) = match $dust_trap { "on" => { let trap_account = ::AccountId::from(65174286u64); From 704f06e6f90c9557e42a8da30440c116f0e39881 Mon Sep 17 00:00:00 2001 From: Liam Aharon Date: Wed, 12 Apr 2023 09:10:01 +0400 Subject: [PATCH 31/37] clippy --- .../conformance_tests/inspect_mutate.rs | 225 ++++++++---------- 1 file changed, 105 insertions(+), 120 deletions(-) diff --git a/frame/support/src/traits/tokens/fungible/conformance_tests/inspect_mutate.rs b/frame/support/src/traits/tokens/fungible/conformance_tests/inspect_mutate.rs index ca5756c70f57f..f48cc0c8aacfc 100644 --- a/frame/support/src/traits/tokens/fungible/conformance_tests/inspect_mutate.rs +++ b/frame/support/src/traits/tokens/fungible/conformance_tests/inspect_mutate.rs @@ -49,17 +49,17 @@ where // Test: Mint an amount into each account let amount_0 = T::minimum_balance(); let amount_1 = T::minimum_balance() + 5.into(); - T::mint_into(&account_0, amount_0.clone()).unwrap(); - T::mint_into(&account_1, amount_1.clone()).unwrap(); + T::mint_into(&account_0, amount_0).unwrap(); + T::mint_into(&account_1, amount_1).unwrap(); // Verify: Account balances are updated correctly - assert_eq!(T::total_balance(&account_0), amount_0.clone()); - assert_eq!(T::total_balance(&account_1), amount_1.clone()); - assert_eq!(T::balance(&account_0), amount_0.clone()); - assert_eq!(T::balance(&account_1), amount_1.clone()); + assert_eq!(T::total_balance(&account_0), amount_0); + assert_eq!(T::total_balance(&account_1), amount_1); + assert_eq!(T::balance(&account_0), amount_0); + assert_eq!(T::balance(&account_1), amount_1); // Verify: Total issuance is updated correctly - assert_eq!(T::total_issuance(), initial_total_issuance + amount_0.clone() + amount_1.clone()); + assert_eq!(T::total_issuance(), initial_total_issuance + amount_0 + amount_1); assert_eq!(T::active_issuance(), initial_active_issuance + amount_0 + amount_1); } @@ -81,20 +81,20 @@ where let initial_total_issuance = T::total_issuance(); let initial_active_issuance = T::active_issuance(); let account = AccountId::from(10); - let amount = T::Balance::max_value() - 5.into() - initial_total_issuance.clone(); + let amount = T::Balance::max_value() - 5.into() - initial_total_issuance; // Mint just below the maximum balance - T::mint_into(&account, amount.clone()).unwrap(); + T::mint_into(&account, amount).unwrap(); // Verify: Minting beyond the maximum balance value returns an Err T::mint_into(&account, 10.into()).unwrap_err(); // Verify: The balance did not change - assert_eq!(T::total_balance(&account), amount.clone()); - assert_eq!(T::balance(&account), amount.clone()); + assert_eq!(T::total_balance(&account), amount); + assert_eq!(T::balance(&account), amount); // Verify: The total issuance did not change - assert_eq!(T::total_issuance(), initial_total_issuance + amount.clone()); + assert_eq!(T::total_issuance(), initial_total_issuance + amount); assert_eq!(T::active_issuance(), initial_active_issuance + amount); } @@ -124,7 +124,7 @@ where let amount = T::minimum_balance() - 1.into(); // Verify: Minting below the minimum balance returns Err - T::mint_into(&account, amount.clone()).unwrap_err(); + T::mint_into(&account, amount).unwrap_err(); // Verify: noop assert_eq!(T::total_balance(&account), T::Balance::zero()); @@ -154,21 +154,18 @@ where // Setup account let account = AccountId::from(5); let initial_balance = T::minimum_balance() + 10.into(); - T::mint_into(&account, initial_balance.clone()).unwrap(); + T::mint_into(&account, initial_balance).unwrap(); // Test: Burn an exact amount from the account let amount_to_burn = T::Balance::from(5); let precision = Precision::Exact; let force = Fortitude::Polite; - T::burn_from(&account, amount_to_burn.clone(), precision, force).unwrap(); + T::burn_from(&account, amount_to_burn, precision, force).unwrap(); // Verify: The balance and total issuance should be reduced by the burned amount - assert_eq!(T::balance(&account), initial_balance.clone() - amount_to_burn.clone()); - assert_eq!(T::total_balance(&account), initial_balance.clone() - amount_to_burn.clone()); - assert_eq!( - T::total_issuance(), - initial_total_issuance + initial_balance.clone() - amount_to_burn.clone() - ); + assert_eq!(T::balance(&account), initial_balance - amount_to_burn); + assert_eq!(T::total_balance(&account), initial_balance - amount_to_burn); + assert_eq!(T::total_issuance(), initial_total_issuance + initial_balance - amount_to_burn); assert_eq!(T::active_issuance(), initial_active_issuance + initial_balance - amount_to_burn); } @@ -194,26 +191,23 @@ where // Setup account let account = AccountId::from(5); let initial_balance = T::minimum_balance() + 10.into(); - T::mint_into(&account, initial_balance.clone()).unwrap(); + T::mint_into(&account, initial_balance).unwrap(); // Get reducible balance let force = Fortitude::Polite; let reducible_balance = T::reducible_balance(&account, Preservation::Expendable, force); // Test: Burn a best effort amount from the account that is greater than the reducible balance - let amount_to_burn = reducible_balance.clone() + 5.into(); + let amount_to_burn = reducible_balance + 5.into(); let precision = Precision::BestEffort; assert!(amount_to_burn > reducible_balance); assert!(amount_to_burn > T::balance(&account)); - T::burn_from(&account, amount_to_burn.clone(), precision, force).unwrap(); + T::burn_from(&account, amount_to_burn, precision, force).unwrap(); // Verify: The balance and total issuance should be reduced by the reducible_balance - assert_eq!(T::balance(&account), initial_balance.clone() - reducible_balance.clone()); - assert_eq!(T::total_balance(&account), initial_balance.clone() - reducible_balance.clone()); - assert_eq!( - T::total_issuance(), - initial_total_issuance + initial_balance.clone() - reducible_balance.clone() - ); + assert_eq!(T::balance(&account), initial_balance - reducible_balance); + assert_eq!(T::total_balance(&account), initial_balance - reducible_balance); + assert_eq!(T::total_issuance(), initial_total_issuance + initial_balance - reducible_balance); assert_eq!(T::active_issuance(), initial_active_issuance + initial_balance - reducible_balance); } @@ -236,12 +230,12 @@ where // Set up the initial conditions and parameters for the test let account = AccountId::from(5); let initial_balance = T::minimum_balance() + 10.into(); - T::mint_into(&account, initial_balance.clone()).unwrap(); + T::mint_into(&account, initial_balance).unwrap(); let initial_total_issuance = T::total_issuance(); let initial_active_issuance = T::active_issuance(); // Verify: Burn an amount greater than the account's balance with Exact precision returns Err - let amount_to_burn = initial_balance.clone() + 10.into(); + let amount_to_burn = initial_balance + 10.into(); let precision = Precision::Exact; let force = Fortitude::Polite; T::burn_from(&account, amount_to_burn, precision, force).unwrap_err(); @@ -276,17 +270,17 @@ where let amount_1 = T::minimum_balance() + 5.into(); let initial_total_issuance = T::total_issuance(); let initial_active_issuance = T::active_issuance(); - T::restore(&account_0, amount_0.clone()).unwrap(); - T::restore(&account_1, amount_1.clone()).unwrap(); + T::restore(&account_0, amount_0).unwrap(); + T::restore(&account_1, amount_1).unwrap(); // Verify: Account balances are updated correctly - assert_eq!(T::total_balance(&account_0), amount_0.clone()); - assert_eq!(T::total_balance(&account_1), amount_1.clone()); - assert_eq!(T::balance(&account_0), amount_0.clone()); - assert_eq!(T::balance(&account_1), amount_1.clone()); + assert_eq!(T::total_balance(&account_0), amount_0); + assert_eq!(T::total_balance(&account_1), amount_1); + assert_eq!(T::balance(&account_0), amount_0); + assert_eq!(T::balance(&account_1), amount_1); // Verify: Total issuance is updated correctly - assert_eq!(T::total_issuance(), initial_total_issuance + amount_0.clone() + amount_1.clone()); + assert_eq!(T::total_issuance(), initial_total_issuance + amount_0 + amount_1); assert_eq!(T::active_issuance(), initial_active_issuance + amount_0 + amount_1); } @@ -308,18 +302,18 @@ where let initial_total_issuance = T::total_issuance(); let initial_active_issuance = T::active_issuance(); let account = AccountId::from(10); - let amount = T::Balance::max_value() - 5.into() - initial_total_issuance.clone(); + let amount = T::Balance::max_value() - 5.into() - initial_total_issuance; // Restore just below the maximum balance - T::restore(&account, amount.clone()).unwrap(); + T::restore(&account, amount).unwrap(); // Verify: Restoring beyond the maximum balance returns an Err T::restore(&account, 10.into()).unwrap_err(); // Verify: The balance and total issuance did not change - assert_eq!(T::total_balance(&account), amount.clone()); - assert_eq!(T::balance(&account), amount.clone()); - assert_eq!(T::total_issuance(), initial_total_issuance + amount.clone()); + assert_eq!(T::total_balance(&account), amount); + assert_eq!(T::balance(&account), amount); + assert_eq!(T::total_issuance(), initial_total_issuance + amount); assert_eq!(T::active_issuance(), initial_active_issuance + amount); } @@ -349,7 +343,7 @@ where let initial_active_issuance = T::active_issuance(); // Verify: Restoring below the minimum balance returns Err - T::restore(&account, amount.clone()).unwrap_err(); + T::restore(&account, amount).unwrap_err(); // Verify: noop assert_eq!(T::total_balance(&account), T::Balance::zero()); @@ -380,19 +374,16 @@ where let account = AccountId::from(5); let initial_balance = T::minimum_balance() + 10.into(); - T::restore(&account, initial_balance.clone()).unwrap(); + T::restore(&account, initial_balance).unwrap(); // Test: Shelve an amount from the account let amount_to_shelve = T::Balance::from(5); - T::shelve(&account, amount_to_shelve.clone()).unwrap(); + T::shelve(&account, amount_to_shelve).unwrap(); // Verify: The balance and total issuance should be reduced by the shelved amount - assert_eq!(T::balance(&account), initial_balance.clone() - amount_to_shelve.clone()); - assert_eq!(T::total_balance(&account), initial_balance.clone() - amount_to_shelve.clone()); - assert_eq!( - T::total_issuance(), - initial_total_issuance + initial_balance.clone() - amount_to_shelve.clone() - ); + assert_eq!(T::balance(&account), initial_balance - amount_to_shelve); + assert_eq!(T::total_balance(&account), initial_balance - amount_to_shelve); + assert_eq!(T::total_issuance(), initial_total_issuance + initial_balance - amount_to_shelve); assert_eq!(T::active_issuance(), initial_active_issuance + initial_balance - amount_to_shelve); } @@ -417,16 +408,16 @@ where // Set up the initial conditions and parameters for the test let account = AccountId::from(5); let initial_balance = T::minimum_balance() + 10.into(); - T::restore(&account, initial_balance.clone()).unwrap(); + T::restore(&account, initial_balance).unwrap(); // Verify: Shelving greater than the balance with Exact precision returns Err - let amount_to_shelve = initial_balance.clone() + 10.into(); + let amount_to_shelve = initial_balance + 10.into(); T::shelve(&account, amount_to_shelve).unwrap_err(); // Verify: The balance and total issuance should remain unchanged assert_eq!(T::balance(&account), initial_balance); assert_eq!(T::total_balance(&account), initial_balance); - assert_eq!(T::total_issuance(), initial_total_issuance + initial_balance.clone()); + assert_eq!(T::total_issuance(), initial_total_issuance + initial_balance); assert_eq!(T::active_issuance(), initial_active_issuance + initial_balance); } @@ -451,21 +442,21 @@ where let account_0 = AccountId::from(0); let account_1 = AccountId::from(1); let initial_balance = T::minimum_balance() + 10.into(); - T::set_balance(&account_0, initial_balance.clone()); - T::set_balance(&account_1, initial_balance.clone()); + T::set_balance(&account_0, initial_balance); + T::set_balance(&account_1, initial_balance); // Test: Transfer an amount from account_0 to account_1 let transfer_amount = T::Balance::from(3); - T::transfer(&account_0, &account_1, transfer_amount.clone(), Preservation::Expendable).unwrap(); + T::transfer(&account_0, &account_1, transfer_amount, Preservation::Expendable).unwrap(); // Verify: Account balances are updated correctly - assert_eq!(T::total_balance(&account_0), initial_balance.clone() - transfer_amount.clone()); - assert_eq!(T::total_balance(&account_1), initial_balance.clone() + transfer_amount.clone()); - assert_eq!(T::balance(&account_0), initial_balance.clone() - transfer_amount.clone()); - assert_eq!(T::balance(&account_1), initial_balance.clone() + transfer_amount.clone()); + assert_eq!(T::total_balance(&account_0), initial_balance - transfer_amount); + assert_eq!(T::total_balance(&account_1), initial_balance + transfer_amount); + assert_eq!(T::balance(&account_0), initial_balance - transfer_amount); + assert_eq!(T::balance(&account_1), initial_balance + transfer_amount); // Verify: Total issuance doesn't change - assert_eq!(T::total_issuance(), initial_total_issuance + initial_balance.clone() * 2.into()); + assert_eq!(T::total_issuance(), initial_total_issuance + initial_balance * 2.into()); assert_eq!(T::active_issuance(), initial_active_issuance + initial_balance * 2.into()); } @@ -491,22 +482,22 @@ where let account_0 = AccountId::from(0); let account_1 = AccountId::from(1); let initial_balance = T::minimum_balance() + 10.into(); - T::set_balance(&account_0, initial_balance.clone()); - T::set_balance(&account_1, initial_balance.clone()); + T::set_balance(&account_0, initial_balance); + T::set_balance(&account_1, initial_balance); // Test: Transfer entire balance from account_0 to account_1 let preservation = Preservation::Expendable; - let transfer_amount = initial_balance.clone(); - T::transfer(&account_0, &account_1, transfer_amount.clone(), preservation).unwrap(); + let transfer_amount = initial_balance; + T::transfer(&account_0, &account_1, transfer_amount, preservation).unwrap(); // Verify: Account balances are updated correctly assert_eq!(T::total_balance(&account_0), T::Balance::zero()); - assert_eq!(T::total_balance(&account_1), initial_balance.clone() * 2.into()); + assert_eq!(T::total_balance(&account_1), initial_balance * 2.into()); assert_eq!(T::balance(&account_0), T::Balance::zero()); - assert_eq!(T::balance(&account_1), initial_balance.clone() * 2.into()); + assert_eq!(T::balance(&account_1), initial_balance * 2.into()); // Verify: Total issuance doesn't change - assert_eq!(T::total_issuance(), initial_total_issuance + initial_balance.clone() * 2.into()); + assert_eq!(T::total_issuance(), initial_total_issuance + initial_balance * 2.into()); assert_eq!(T::active_issuance(), initial_active_issuance + initial_balance * 2.into()); } @@ -540,8 +531,8 @@ where let account_0 = AccountId::from(10); let account_1 = AccountId::from(20); let initial_balance = T::minimum_balance() + 10.into(); - T::set_balance(&account_0, initial_balance.clone()); - T::set_balance(&account_1, initial_balance.clone()); + T::set_balance(&account_0, initial_balance); + T::set_balance(&account_1, initial_balance); let initial_total_issuance = T::total_issuance(); let initial_active_issuance = T::active_issuance(); @@ -553,23 +544,23 @@ where // Test: Transfer balance let preservation = Preservation::Expendable; let transfer_amount = T::Balance::from(11); - T::transfer(&account_0, &account_1, transfer_amount.clone(), preservation).unwrap(); + T::transfer(&account_0, &account_1, transfer_amount, preservation).unwrap(); // Verify: Account balances are updated correctly assert_eq!(T::total_balance(&account_0), T::Balance::zero()); - assert_eq!(T::total_balance(&account_1), initial_balance.clone() + transfer_amount.clone()); + assert_eq!(T::total_balance(&account_1), initial_balance + transfer_amount); assert_eq!(T::balance(&account_0), T::Balance::zero()); - assert_eq!(T::balance(&account_1), initial_balance.clone() + transfer_amount.clone()); + assert_eq!(T::balance(&account_1), initial_balance + transfer_amount); match dust_trap { Some(dust_trap) => { // Verify: Total issuance and active issuance don't change - assert_eq!(T::total_issuance(), initial_total_issuance.clone()); - assert_eq!(T::active_issuance(), initial_active_issuance.clone()); + assert_eq!(T::total_issuance(), initial_total_issuance); + assert_eq!(T::active_issuance(), initial_active_issuance); // Verify: Dust is collected into dust trap assert_eq!( T::total_balance(&dust_trap), - initial_dust_trap_balance.clone() + T::minimum_balance() - 1.into() + initial_dust_trap_balance + T::minimum_balance() - 1.into() ); assert_eq!( T::balance(&dust_trap), @@ -617,38 +608,32 @@ where let account_0 = AccountId::from(0); let account_1 = AccountId::from(1); let initial_balance = T::minimum_balance() + 10.into(); - T::set_balance(&account_0, initial_balance.clone()); - T::set_balance(&account_1, initial_balance.clone()); + T::set_balance(&account_0, initial_balance); + T::set_balance(&account_1, initial_balance); // Verify: Transfer Protect entire balance from account_0 to account_1 should Err let preservation = Preservation::Protect; - let transfer_amount = initial_balance.clone(); - T::transfer(&account_0, &account_1, transfer_amount.clone(), preservation).unwrap_err(); + let transfer_amount = initial_balance; + T::transfer(&account_0, &account_1, transfer_amount, preservation).unwrap_err(); // Verify: Noop - assert_eq!(T::total_balance(&account_0), initial_balance.clone()); - assert_eq!(T::total_balance(&account_1), initial_balance.clone()); - assert_eq!(T::balance(&account_0), initial_balance.clone()); - assert_eq!(T::balance(&account_1), initial_balance.clone()); - assert_eq!( - T::total_issuance(), - initial_total_issuance.clone() + initial_balance.clone() * 2.into() - ); - assert_eq!( - T::active_issuance(), - initial_active_issuance.clone() + initial_balance.clone() * 2.into() - ); + assert_eq!(T::total_balance(&account_0), initial_balance); + assert_eq!(T::total_balance(&account_1), initial_balance); + assert_eq!(T::balance(&account_0), initial_balance); + assert_eq!(T::balance(&account_1), initial_balance); + assert_eq!(T::total_issuance(), initial_total_issuance + initial_balance * 2.into()); + assert_eq!(T::active_issuance(), initial_active_issuance + initial_balance * 2.into()); // Verify: Transfer Preserve entire balance from account_0 to account_1 should Err let preservation = Preservation::Preserve; - T::transfer(&account_0, &account_1, transfer_amount.clone(), preservation).unwrap_err(); + T::transfer(&account_0, &account_1, transfer_amount, preservation).unwrap_err(); // Verify: Noop - assert_eq!(T::total_balance(&account_0), initial_balance.clone()); - assert_eq!(T::total_balance(&account_1), initial_balance.clone()); - assert_eq!(T::balance(&account_0), initial_balance.clone()); - assert_eq!(T::balance(&account_1), initial_balance.clone()); - assert_eq!(T::total_issuance(), initial_total_issuance + initial_balance.clone() * 2.into()); + assert_eq!(T::total_balance(&account_0), initial_balance); + assert_eq!(T::total_balance(&account_1), initial_balance); + assert_eq!(T::balance(&account_0), initial_balance); + assert_eq!(T::balance(&account_1), initial_balance); + assert_eq!(T::total_issuance(), initial_total_issuance + initial_balance * 2.into()); assert_eq!(T::active_issuance(), initial_active_issuance + initial_balance * 2.into()); } @@ -671,20 +656,20 @@ where let initial_active_issuance = T::active_issuance(); let account = AccountId::from(10); let initial_balance = T::minimum_balance() + 10.into(); - T::mint_into(&account, initial_balance.clone()).unwrap(); + T::mint_into(&account, initial_balance).unwrap(); // Test: Increase the account balance with set_balance let increase_amount: T::Balance = 5.into(); - let new = T::set_balance(&account, initial_balance.clone() + increase_amount.clone()); + let new = T::set_balance(&account, initial_balance + increase_amount); // Verify: set_balance returned the new balance let expected_new = initial_balance + increase_amount; assert_eq!(new, expected_new); // Verify: Balance and issuance is updated correctly - assert_eq!(T::total_balance(&account), expected_new.clone()); - assert_eq!(T::balance(&account), expected_new.clone()); - assert_eq!(T::total_issuance(), initial_total_issuance + expected_new.clone()); + assert_eq!(T::total_balance(&account), expected_new); + assert_eq!(T::balance(&account), expected_new); + assert_eq!(T::total_issuance(), initial_total_issuance + expected_new); assert_eq!(T::active_issuance(), initial_active_issuance + expected_new); } @@ -707,20 +692,20 @@ where let initial_active_issuance = T::active_issuance(); let account = AccountId::from(10); let initial_balance = T::minimum_balance() + 10.into(); - T::mint_into(&account, initial_balance.clone()).unwrap(); + T::mint_into(&account, initial_balance).unwrap(); // Test: Increase the account balance with set_balance let burn_amount: T::Balance = 5.into(); - let new = T::set_balance(&account, initial_balance.clone() - burn_amount.clone()); + let new = T::set_balance(&account, initial_balance - burn_amount); // Verify: set_balance returned the new balance let expected_new = initial_balance - burn_amount; assert_eq!(new, expected_new); // Verify: Balance and issuance is updated correctly - assert_eq!(T::total_balance(&account), expected_new.clone()); - assert_eq!(T::balance(&account), expected_new.clone()); - assert_eq!(T::total_issuance(), initial_total_issuance + expected_new.clone()); + assert_eq!(T::total_balance(&account), expected_new); + assert_eq!(T::balance(&account), expected_new); + assert_eq!(T::total_issuance(), initial_total_issuance + expected_new); assert_eq!(T::active_issuance(), initial_active_issuance + expected_new); } @@ -741,7 +726,7 @@ where { let account = AccountId::from(10); let initial_balance = T::minimum_balance() + 10.into(); - T::mint_into(&account, initial_balance.clone()).unwrap(); + T::mint_into(&account, initial_balance).unwrap(); // Test: can_deposit a reasonable amount let ret = T::can_deposit(&account, 5.into(), Provenance::Minted); @@ -798,7 +783,7 @@ where // Test: Try deposit over the max balance let initial_balance = T::Balance::max_value() - 5.into() - T::total_issuance(); - T::mint_into(&account, initial_balance.clone()).unwrap(); + T::mint_into(&account, initial_balance).unwrap(); let ret = T::can_deposit(&account, 10.into(), Provenance::Minted); // Verify: Returns success @@ -822,7 +807,7 @@ where { let account = AccountId::from(10); let initial_balance = T::minimum_balance() + 10.into(); - T::mint_into(&account, initial_balance.clone()).unwrap(); + T::mint_into(&account, initial_balance).unwrap(); // Test: can_withdraw a reasonable amount let ret = T::can_withdraw(&account, 5.into()); @@ -852,7 +837,7 @@ where let account = AccountId::from(10); let initial_balance = T::minimum_balance(); - T::mint_into(&account, initial_balance.clone()).unwrap(); + T::mint_into(&account, initial_balance).unwrap(); // Verify: can_withdraw below the minimum balance returns ReducedToZero let ret = T::can_withdraw(&account, 1.into()); @@ -881,8 +866,8 @@ where let account = AccountId::from(10); let other_account = AccountId::from(100); let initial_balance = T::minimum_balance() + 5.into(); - T::mint_into(&account, initial_balance.clone()).unwrap(); - T::mint_into(&other_account, initial_balance.clone() * 2.into()).unwrap(); + T::mint_into(&account, initial_balance).unwrap(); + T::mint_into(&other_account, initial_balance * 2.into()).unwrap(); // Verify: can_withdraw below the account balance returns BalanceLow let ret = T::can_withdraw(&account, initial_balance + 1.into()); @@ -906,7 +891,7 @@ where { let account = AccountId::from(10); let initial_balance = T::minimum_balance() + 10.into(); - T::mint_into(&account, initial_balance.clone()).unwrap(); + T::mint_into(&account, initial_balance).unwrap(); // Verify: reducible_balance returns the full balance let ret = T::reducible_balance(&account, Preservation::Expendable, Fortitude::Polite); @@ -930,11 +915,11 @@ where { let account = AccountId::from(10); let initial_balance = T::minimum_balance() + 10.into(); - T::mint_into(&account, initial_balance.clone()).unwrap(); + T::mint_into(&account, initial_balance).unwrap(); // Verify: reducible_balance returns the full balance - min balance let ret = T::reducible_balance(&account, Preservation::Protect, Fortitude::Polite); - assert_eq!(ret, initial_balance.clone() - T::minimum_balance()); + assert_eq!(ret, initial_balance - T::minimum_balance()); let ret = T::reducible_balance(&account, Preservation::Preserve, Fortitude::Polite); assert_eq!(ret, initial_balance - T::minimum_balance()); } From e8b1323424159152171ffb28bb25284bcb2fa25e Mon Sep 17 00:00:00 2001 From: Liam Aharon Date: Wed, 12 Apr 2023 09:19:08 +0400 Subject: [PATCH 32/37] fix rustdoc errors --- .../conformance_tests/inspect_mutate.rs | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/frame/support/src/traits/tokens/fungible/conformance_tests/inspect_mutate.rs b/frame/support/src/traits/tokens/fungible/conformance_tests/inspect_mutate.rs index f48cc0c8aacfc..af1ebb1615e0f 100644 --- a/frame/support/src/traits/tokens/fungible/conformance_tests/inspect_mutate.rs +++ b/frame/support/src/traits/tokens/fungible/conformance_tests/inspect_mutate.rs @@ -33,8 +33,10 @@ use sp_runtime::traits::{Bounded, Zero}; /// /// # Type Parameters /// +/// ```ignore /// - `T`: Implements `Mutate`. /// - `AccountId`: Account identifier implementing `AtLeast8BitUnsigned`. +/// ``` pub fn mint_into_success(_dust_trap: Option) where T: Mutate, @@ -70,8 +72,10 @@ where /// /// # Type Parameters /// +/// ```ignore /// - `T`: Implements `Mutate`. /// - `AccountId`: Account identifier implementing `AtLeast8BitUnsigned`. +/// ``` pub fn mint_into_overflow(_dust_trap: Option) where T: Mutate, @@ -105,8 +109,10 @@ where /// /// # Type Parameters /// +/// ```ignore /// - `T`: Implements `Mutate`. /// - `AccountId`: Account identifier implementing `AtLeast8BitUnsigned`. +/// ``` pub fn mint_into_below_minimum(_dust_trap: Option) where T: Mutate, @@ -140,8 +146,10 @@ where /// /// # Type Parameters /// +/// ```ignore /// - `T`: Implements `Mutate` for `AccountId`. /// - `AccountId`: Account identifier implementing `AtLeast8BitUnsigned`. +/// ``` pub fn burn_from_exact_success(_dust_trap: Option) where T: Mutate, @@ -177,8 +185,10 @@ where /// /// # Type Parameters /// +/// ```ignore /// - `T`: Implements `Mutate` for `AccountId`. /// - `AccountId`: Account identifier implementing `AtLeast8BitUnsigned`. +/// ``` pub fn burn_from_best_effort_success(_dust_trap: Option) where T: Mutate, @@ -219,8 +229,10 @@ where /// /// # Type Parameters /// +/// ```ignore /// - `T`: Implements `Mutate`. /// - `AccountId`: Account identifier implementing `AtLeast8BitUnsigned`. +/// ``` pub fn burn_from_exact_insufficient_funds(_dust_trap: Option) where T: Mutate, @@ -254,8 +266,10 @@ where /// /// # Type Parameters /// +/// ```ignore /// - `T`: Implements `Mutate`. /// - `AccountId`: Account identifier implementing `AtLeast8BitUnsigned`. +/// ``` pub fn restore_success(_dust_trap: Option) where T: Mutate, @@ -291,8 +305,10 @@ where /// /// # Type Parameters /// +/// ```ignore /// - `T`: Implements `Mutate`. /// - `AccountId`: Account identifier implementing `AtLeast8BitUnsigned`. +/// ``` pub fn restore_overflow(_dust_trap: Option) where T: Mutate, @@ -324,8 +340,10 @@ where /// /// # Type Parameters /// +/// ```ignore /// - `T`: Implements `Mutate`. /// - `AccountId`: Account identifier implementing `AtLeast8BitUnsigned`. +/// ``` pub fn restore_below_minimum(_dust_trap: Option) where T: Mutate, @@ -359,8 +377,10 @@ where /// /// # Type Parameters /// +/// ```ignore /// - `T`: Implements `Mutate`. /// - `AccountId`: Account identifier implementing `AtLeast8BitUnsigned`. +/// ``` pub fn shelve_success(_dust_trap: Option) where T: Mutate, @@ -394,8 +414,10 @@ where /// /// # Type Parameters /// +/// ```ignore /// - `T`: Implements `Mutate`. /// - `AccountId`: Account identifier implementing `AtLeast8BitUnsigned`. +/// ``` pub fn shelve_insufficient_funds(_dust_trap: Option) where T: Mutate, @@ -429,8 +451,10 @@ where /// /// # Type Parameters /// +/// ```ignore /// - `T`: Implements `Mutate`. /// - `AccountId`: Account identifier implementing `AtLeast8BitUnsigned`. +/// ``` pub fn transfer_success(_dust_trap: Option) where T: Mutate, @@ -469,8 +493,10 @@ where /// /// # Type Parameters /// +/// ```ignore /// - `T`: Implements `Mutate`. /// - `AccountId`: Account identifier implementing `AtLeast8BitUnsigned`. +/// ``` pub fn transfer_expendable_all(_dust_trap: Option) where T: Mutate, @@ -516,8 +542,10 @@ where /// /// # Type Parameters /// +/// ```ignore /// - T: Implements Mutate. /// - AccountId: Account identifier implementing AtLeast8BitUnsigned. +/// ``` pub fn transfer_expendable_dust(dust_trap: Option) where T: Mutate, @@ -590,8 +618,10 @@ where /// /// # Type Parameters /// +/// ```ignore /// - `T`: Implements `Mutate`. /// - `AccountId`: Account identifier implementing `AtLeast8BitUnsigned`. +/// ``` pub fn transfer_protect_preserve(_dust_trap: Option) where T: Mutate, @@ -644,8 +674,10 @@ where /// /// # Type Parameters /// +/// ```ignore /// - T: Implements Mutate. /// - AccountId: Account identifier implementing AtLeast8BitUnsigned. +/// ``` pub fn set_balance_mint_success(_dust_trap: Option) where T: Mutate, @@ -680,8 +712,10 @@ where /// /// # Type Parameters /// +/// ```ignore /// - T: Implements Mutate. /// - AccountId: Account identifier implementing AtLeast8BitUnsigned. +/// ``` pub fn set_balance_burn_success(_dust_trap: Option) where T: Mutate, @@ -716,8 +750,10 @@ where /// /// # Type Parameters /// +/// ```ignore /// - T: Implements Mutate. /// - AccountId: Account identifier implementing AtLeast8BitUnsigned. +/// ``` pub fn can_deposit_success(_dust_trap: Option) where T: Mutate, @@ -742,8 +778,10 @@ where /// /// # Type Parameters /// +/// ```ignore /// - T: Implements Mutate. /// - AccountId: Account identifier implementing AtLeast8BitUnsigned. +/// ``` pub fn can_deposit_below_minimum(_dust_trap: Option) where T: Mutate, @@ -771,8 +809,10 @@ where /// /// # Type Parameters /// +/// ```ignore /// - T: Implements Mutate. /// - AccountId: Account identifier implementing AtLeast8BitUnsigned. +/// ``` pub fn can_deposit_overflow(_dust_trap: Option) where T: Mutate, @@ -797,8 +837,10 @@ where /// /// # Type Parameters /// +/// ```ignore /// - T: Implements Mutate. /// - AccountId: Account identifier implementing AtLeast8BitUnsigned. +/// ``` pub fn can_withdraw_success(_dust_trap: Option) where T: Mutate, @@ -823,8 +865,10 @@ where /// /// # Type Parameters /// +/// ```ignore /// - T: Implements Mutate. /// - AccountId: Account identifier implementing AtLeast8BitUnsigned. +/// ``` pub fn can_withdraw_reduced_to_zero(_dust_trap: Option) where T: Mutate, @@ -851,8 +895,10 @@ where /// /// # Type Parameters /// +/// ```ignore /// - T: Implements Mutate. /// - AccountId: Account identifier implementing AtLeast8BitUnsigned. +/// ``` pub fn can_withdraw_balance_low(_dust_trap: Option) where T: Mutate, @@ -881,8 +927,10 @@ where /// /// # Type Parameters /// +/// ```ignore /// - T: Implements Mutate. /// - AccountId: Account identifier implementing AtLeast8BitUnsigned. +/// ``` pub fn reducible_balance_expendable(_dust_trap: Option) where T: Mutate, @@ -905,8 +953,10 @@ where /// /// # Type Parameters /// +/// ```ignore /// - T: Implements Mutate. /// - AccountId: Account identifier implementing AtLeast8BitUnsigned. +/// ``` pub fn reducible_balance_protect_preserve(_dust_trap: Option) where T: Mutate, From f7baa52a3013e76cf01a89c85820bb794e4043c4 Mon Sep 17 00:00:00 2001 From: Liam Aharon Date: Wed, 12 Apr 2023 09:36:48 +0400 Subject: [PATCH 33/37] fix rustdoc --- .../conformance_tests/inspect_mutate.rs | 50 +++++++++---------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/frame/support/src/traits/tokens/fungible/conformance_tests/inspect_mutate.rs b/frame/support/src/traits/tokens/fungible/conformance_tests/inspect_mutate.rs index af1ebb1615e0f..732742cca9b54 100644 --- a/frame/support/src/traits/tokens/fungible/conformance_tests/inspect_mutate.rs +++ b/frame/support/src/traits/tokens/fungible/conformance_tests/inspect_mutate.rs @@ -33,7 +33,7 @@ use sp_runtime::traits::{Bounded, Zero}; /// /// # Type Parameters /// -/// ```ignore +/// ```text /// - `T`: Implements `Mutate`. /// - `AccountId`: Account identifier implementing `AtLeast8BitUnsigned`. /// ``` @@ -72,7 +72,7 @@ where /// /// # Type Parameters /// -/// ```ignore +/// ```text /// - `T`: Implements `Mutate`. /// - `AccountId`: Account identifier implementing `AtLeast8BitUnsigned`. /// ``` @@ -109,7 +109,7 @@ where /// /// # Type Parameters /// -/// ```ignore +/// ```text /// - `T`: Implements `Mutate`. /// - `AccountId`: Account identifier implementing `AtLeast8BitUnsigned`. /// ``` @@ -146,7 +146,7 @@ where /// /// # Type Parameters /// -/// ```ignore +/// ```text /// - `T`: Implements `Mutate` for `AccountId`. /// - `AccountId`: Account identifier implementing `AtLeast8BitUnsigned`. /// ``` @@ -185,7 +185,7 @@ where /// /// # Type Parameters /// -/// ```ignore +/// ```text /// - `T`: Implements `Mutate` for `AccountId`. /// - `AccountId`: Account identifier implementing `AtLeast8BitUnsigned`. /// ``` @@ -229,7 +229,7 @@ where /// /// # Type Parameters /// -/// ```ignore +/// ```text /// - `T`: Implements `Mutate`. /// - `AccountId`: Account identifier implementing `AtLeast8BitUnsigned`. /// ``` @@ -266,7 +266,7 @@ where /// /// # Type Parameters /// -/// ```ignore +/// ```text /// - `T`: Implements `Mutate`. /// - `AccountId`: Account identifier implementing `AtLeast8BitUnsigned`. /// ``` @@ -305,7 +305,7 @@ where /// /// # Type Parameters /// -/// ```ignore +/// ```text /// - `T`: Implements `Mutate`. /// - `AccountId`: Account identifier implementing `AtLeast8BitUnsigned`. /// ``` @@ -340,7 +340,7 @@ where /// /// # Type Parameters /// -/// ```ignore +/// ```text /// - `T`: Implements `Mutate`. /// - `AccountId`: Account identifier implementing `AtLeast8BitUnsigned`. /// ``` @@ -377,7 +377,7 @@ where /// /// # Type Parameters /// -/// ```ignore +/// ```text /// - `T`: Implements `Mutate`. /// - `AccountId`: Account identifier implementing `AtLeast8BitUnsigned`. /// ``` @@ -414,7 +414,7 @@ where /// /// # Type Parameters /// -/// ```ignore +/// ```text /// - `T`: Implements `Mutate`. /// - `AccountId`: Account identifier implementing `AtLeast8BitUnsigned`. /// ``` @@ -451,7 +451,7 @@ where /// /// # Type Parameters /// -/// ```ignore +/// ```text /// - `T`: Implements `Mutate`. /// - `AccountId`: Account identifier implementing `AtLeast8BitUnsigned`. /// ``` @@ -493,7 +493,7 @@ where /// /// # Type Parameters /// -/// ```ignore +/// ```text /// - `T`: Implements `Mutate`. /// - `AccountId`: Account identifier implementing `AtLeast8BitUnsigned`. /// ``` @@ -542,7 +542,7 @@ where /// /// # Type Parameters /// -/// ```ignore +/// ```text /// - T: Implements Mutate. /// - AccountId: Account identifier implementing AtLeast8BitUnsigned. /// ``` @@ -618,7 +618,7 @@ where /// /// # Type Parameters /// -/// ```ignore +/// ```text /// - `T`: Implements `Mutate`. /// - `AccountId`: Account identifier implementing `AtLeast8BitUnsigned`. /// ``` @@ -674,7 +674,7 @@ where /// /// # Type Parameters /// -/// ```ignore +/// ```text /// - T: Implements Mutate. /// - AccountId: Account identifier implementing AtLeast8BitUnsigned. /// ``` @@ -712,7 +712,7 @@ where /// /// # Type Parameters /// -/// ```ignore +/// ```text /// - T: Implements Mutate. /// - AccountId: Account identifier implementing AtLeast8BitUnsigned. /// ``` @@ -750,7 +750,7 @@ where /// /// # Type Parameters /// -/// ```ignore +/// ```text /// - T: Implements Mutate. /// - AccountId: Account identifier implementing AtLeast8BitUnsigned. /// ``` @@ -778,7 +778,7 @@ where /// /// # Type Parameters /// -/// ```ignore +/// ```text /// - T: Implements Mutate. /// - AccountId: Account identifier implementing AtLeast8BitUnsigned. /// ``` @@ -809,7 +809,7 @@ where /// /// # Type Parameters /// -/// ```ignore +/// ```text /// - T: Implements Mutate. /// - AccountId: Account identifier implementing AtLeast8BitUnsigned. /// ``` @@ -837,7 +837,7 @@ where /// /// # Type Parameters /// -/// ```ignore +/// ```text /// - T: Implements Mutate. /// - AccountId: Account identifier implementing AtLeast8BitUnsigned. /// ``` @@ -865,7 +865,7 @@ where /// /// # Type Parameters /// -/// ```ignore +/// ```text /// - T: Implements Mutate. /// - AccountId: Account identifier implementing AtLeast8BitUnsigned. /// ``` @@ -895,7 +895,7 @@ where /// /// # Type Parameters /// -/// ```ignore +/// ```text /// - T: Implements Mutate. /// - AccountId: Account identifier implementing AtLeast8BitUnsigned. /// ``` @@ -927,7 +927,7 @@ where /// /// # Type Parameters /// -/// ```ignore +/// ```text /// - T: Implements Mutate. /// - AccountId: Account identifier implementing AtLeast8BitUnsigned. /// ``` @@ -953,7 +953,7 @@ where /// /// # Type Parameters /// -/// ```ignore +/// ```text /// - T: Implements Mutate. /// - AccountId: Account identifier implementing AtLeast8BitUnsigned. /// ``` From 137a8dcfc30e1bfd5a52afc566ced817669e3d42 Mon Sep 17 00:00:00 2001 From: Liam Aharon Date: Tue, 18 Apr 2023 18:36:35 +0400 Subject: [PATCH 34/37] improve macro --- .../src/tests/fungible_conformance_tests.rs | 53 +++++++++---------- 1 file changed, 24 insertions(+), 29 deletions(-) diff --git a/frame/balances/src/tests/fungible_conformance_tests.rs b/frame/balances/src/tests/fungible_conformance_tests.rs index 0def3fedf4818..3f9388043caef 100644 --- a/frame/balances/src/tests/fungible_conformance_tests.rs +++ b/frame/balances/src/tests/fungible_conformance_tests.rs @@ -23,43 +23,42 @@ use paste::paste; // $cm = child module // shorthand so each run_tests! call can fit on one line macro_rules! run_tests { - ($pm:tt :: $cm:tt, $ext_deposit:expr, $dust_trap:expr, $($name:ident),*) => { + ($pm:tt :: $cm:tt, $ext_deposit:expr, $($name:ident),*) => { $( paste! { #[test] - fn [< $name _existential_deposit_ $ext_deposit _dust_trap_ $dust_trap >]() { - let (trap_account, builder) = match $dust_trap { - "on" => { - let trap_account = ::AccountId::from(65174286u64); - let builder = ExtBuilder::default().existential_deposit($ext_deposit).dust_trap(trap_account); - (Some(trap_account), builder) - }, - "off" => { - let builder = ExtBuilder::default().existential_deposit($ext_deposit); - (None, builder) - }, - _ => panic!("dust_trap must be either \"on\" or \"off\"") - }; + fn [< $name _existential_deposit_ $ext_deposit _dust_trap_on >]() { + let trap_account = ::AccountId::from(65174286u64); + let builder = ExtBuilder::default().existential_deposit($ext_deposit).dust_trap(trap_account); builder.build_and_execute_with(|| { - // Initialise the trap account if it is being used - if let Some(trap_account) = trap_account { - Balances::set_balance(&trap_account, Balances::minimum_balance()); - }; + // Initialise the trap account + Balances::set_balance(&trap_account, Balances::minimum_balance()); // Run the test $pm::$cm::$name::< Balances, ::AccountId, - >(trap_account); + >(Some(trap_account)); + }); + } + + #[test] + fn [< $name _existential_deposit_ $ext_deposit _dust_trap_off >]() { + let builder = ExtBuilder::default().existential_deposit($ext_deposit); + builder.build_and_execute_with(|| { + // Run the test + $pm::$cm::$name::< + Balances, + ::AccountId, + >(None); }); } } )* }; - ($pm:tt :: $cm:tt, $ext_deposit:expr, $dust_trap:expr) => { + ($pm:tt :: $cm:tt, $ext_deposit:expr) => { run_tests!( $pm::$cm, $ext_deposit, - $dust_trap, mint_into_success, mint_into_overflow, mint_into_below_minimum, @@ -89,11 +88,7 @@ macro_rules! run_tests { }; } -run_tests!(conformance_tests::inspect_mutate, 1, "off"); -run_tests!(conformance_tests::inspect_mutate, 1, "on"); -run_tests!(conformance_tests::inspect_mutate, 2, "off"); -run_tests!(conformance_tests::inspect_mutate, 2, "on"); -run_tests!(conformance_tests::inspect_mutate, 5, "off"); -run_tests!(conformance_tests::inspect_mutate, 5, "on"); -run_tests!(conformance_tests::inspect_mutate, 1000, "off"); -run_tests!(conformance_tests::inspect_mutate, 1000, "on"); +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); From 39c00bb0f50eb44877ea85384465130458d51deb Mon Sep 17 00:00:00 2001 From: Liam Aharon Date: Tue, 18 Apr 2023 18:49:42 +0400 Subject: [PATCH 35/37] improve variable naming --- .../src/tests/fungible_conformance_tests.rs | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/frame/balances/src/tests/fungible_conformance_tests.rs b/frame/balances/src/tests/fungible_conformance_tests.rs index 3f9388043caef..f8f071400baeb 100644 --- a/frame/balances/src/tests/fungible_conformance_tests.rs +++ b/frame/balances/src/tests/fungible_conformance_tests.rs @@ -19,11 +19,8 @@ use super::*; use frame_support::traits::fungible::{conformance_tests, Inspect, Mutate}; use paste::paste; -// $pm = parent module -// $cm = child module -// shorthand so each run_tests! call can fit on one line macro_rules! run_tests { - ($pm:tt :: $cm:tt, $ext_deposit:expr, $($name:ident),*) => { + ($parent_module:tt :: $child_module:tt, $ext_deposit:expr, $($name:ident),*) => { $( paste! { #[test] @@ -34,7 +31,7 @@ macro_rules! run_tests { // Initialise the trap account Balances::set_balance(&trap_account, Balances::minimum_balance()); // Run the test - $pm::$cm::$name::< + $parent_module::$child_module::$name::< Balances, ::AccountId, >(Some(trap_account)); @@ -46,7 +43,7 @@ macro_rules! run_tests { let builder = ExtBuilder::default().existential_deposit($ext_deposit); builder.build_and_execute_with(|| { // Run the test - $pm::$cm::$name::< + $parent_module::$child_module::$name::< Balances, ::AccountId, >(None); @@ -55,9 +52,9 @@ macro_rules! run_tests { } )* }; - ($pm:tt :: $cm:tt, $ext_deposit:expr) => { + ($parent_module:tt :: $child_module:tt, $ext_deposit:expr) => { run_tests!( - $pm::$cm, + $parent_module::$child_module, $ext_deposit, mint_into_success, mint_into_overflow, From 00d494b87f6a701fcb3555d6040152d5292196a5 Mon Sep 17 00:00:00 2001 From: Liam Aharon Date: Tue, 18 Apr 2023 20:33:39 +0400 Subject: [PATCH 36/37] remove redundant comment --- frame/balances/src/tests/fungible_conformance_tests.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/frame/balances/src/tests/fungible_conformance_tests.rs b/frame/balances/src/tests/fungible_conformance_tests.rs index f8f071400baeb..b37cf8f4bfabd 100644 --- a/frame/balances/src/tests/fungible_conformance_tests.rs +++ b/frame/balances/src/tests/fungible_conformance_tests.rs @@ -28,9 +28,7 @@ macro_rules! run_tests { let trap_account = ::AccountId::from(65174286u64); let builder = ExtBuilder::default().existential_deposit($ext_deposit).dust_trap(trap_account); builder.build_and_execute_with(|| { - // Initialise the trap account Balances::set_balance(&trap_account, Balances::minimum_balance()); - // Run the test $parent_module::$child_module::$name::< Balances, ::AccountId, @@ -42,7 +40,6 @@ macro_rules! run_tests { fn [< $name _existential_deposit_ $ext_deposit _dust_trap_off >]() { let builder = ExtBuilder::default().existential_deposit($ext_deposit); builder.build_and_execute_with(|| { - // Run the test $parent_module::$child_module::$name::< Balances, ::AccountId, From f265ff10830dfa93e591e77bf4b69bb8a17835ca Mon Sep 17 00:00:00 2001 From: Liam Aharon Date: Thu, 4 May 2023 18:22:03 +0400 Subject: [PATCH 37/37] use path --- frame/balances/src/tests/fungible_conformance_tests.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/frame/balances/src/tests/fungible_conformance_tests.rs b/frame/balances/src/tests/fungible_conformance_tests.rs index b37cf8f4bfabd..6262aa04dc088 100644 --- a/frame/balances/src/tests/fungible_conformance_tests.rs +++ b/frame/balances/src/tests/fungible_conformance_tests.rs @@ -20,7 +20,7 @@ use frame_support::traits::fungible::{conformance_tests, Inspect, Mutate}; use paste::paste; macro_rules! run_tests { - ($parent_module:tt :: $child_module:tt, $ext_deposit:expr, $($name:ident),*) => { + ($path:path, $ext_deposit:expr, $($name:ident),*) => { $( paste! { #[test] @@ -29,7 +29,7 @@ macro_rules! run_tests { 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()); - $parent_module::$child_module::$name::< + $path::$name::< Balances, ::AccountId, >(Some(trap_account)); @@ -40,7 +40,7 @@ macro_rules! run_tests { fn [< $name _existential_deposit_ $ext_deposit _dust_trap_off >]() { let builder = ExtBuilder::default().existential_deposit($ext_deposit); builder.build_and_execute_with(|| { - $parent_module::$child_module::$name::< + $path::$name::< Balances, ::AccountId, >(None); @@ -49,9 +49,9 @@ macro_rules! run_tests { } )* }; - ($parent_module:tt :: $child_module:tt, $ext_deposit:expr) => { + ($path:path, $ext_deposit:expr) => { run_tests!( - $parent_module::$child_module, + $path, $ext_deposit, mint_into_success, mint_into_overflow,