mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-21 20:21:01 +00:00
Deprecate Currency; introduce holds and freezing into fungible traits (#12951)
* First reworking of fungibles API * New API and docs * More fungible::* API improvements * New ref-counting logic for old API * Missing files * Fixes * Use the new transfer logic * Use fungibles for the dispatchables * Use shelve/restore names * Locking works with total balance. * repotting and removal * Separate Holds from Reserves * Introduce freezes * Missing files * Tests for freezing * Fix hold+freeze combo * More tests * Fee-free dispatchable for upgrading accounts * Benchmarks and a few fixes * Another test * Docs and refactor to avoid blanket impls * Repot * Fit out ItemOf fully * Add events to Balanced traits * Introduced events into Hold traits * Fix Assets pallet tests * Assets benchmarks pass * Missing files and fixes * Fixes * Fixes * Benchmarks fixes * Fix balance benchmarks * Formatting * Expose fungible sub modules * Move NIS to fungible API * Fix broken impl and add test * Fix tests * API for `transfer_and_hold` * Use composite APIs * Formatting * Upgraded event * Fixes * Fixes * Fixes * Fixes * Repot tests and some fixed * Fix some bits * Fix dust tests * Rename `set_balance` - `Balances::set_balance` becomes `Balances::force_set_balance` - `Unbalanced::set_balance` becomes `Unbalances::write_balance` * becomes * Move dust handling to fungibles API * Formatting * Fixes and more refactoring * Fixes * Fixes * Fixes * Fixes * Fixes * Fixes * Fixes * Fixes * Fixes * Use reducible_balance for better correctness on fees * Reducing hold to zero should remove entry. * Add test * Docs * Update frame/support/src/traits/tokens/fungibles/hold.rs Co-authored-by: Muharem Ismailov <ismailov.m.h@gmail.com> * Update frame/support/src/traits/tokens/fungibles/regular.rs Co-authored-by: Muharem Ismailov <ismailov.m.h@gmail.com> * Update frame/support/src/traits/tokens/fungible/hold.rs Co-authored-by: Muharem Ismailov <ismailov.m.h@gmail.com> * Update frame/support/src/traits/tokens/fungible/regular.rs Co-authored-by: Muharem Ismailov <ismailov.m.h@gmail.com> * Docs * Docs * Docs * Fix NIS benchmarks * Doc comment * Remove post_mutation * Fix some tests * Fix some grumbles * Enumify bool args to fungible(s) functions * Fix up assets and balances * Formatting * Fix contracts * Fix tests & benchmarks build * Typify minted boolean arg * Typify on_hold boolean arg; renames * Fix numerous tests * Fix dependency issue * Privatize dangerous API mutate_account * Fix contracts (@alext - please check this commit) * Remove println * Fix tests for contracts * Fix broken rename * Fix broken rename * Fix broken rename * Docs * Update frame/support/src/traits/tokens/fungible/hold.rs Co-authored-by: Anthony Alaribe <anthonyalaribe@gmail.com> * remove from_ref_time * Update frame/executive/src/lib.rs Co-authored-by: Anthony Alaribe <anthonyalaribe@gmail.com> * Update frame/executive/src/lib.rs Co-authored-by: Anthony Alaribe <anthonyalaribe@gmail.com> * Reenable test * Update frame/support/src/traits/tokens/fungibles/hold.rs Co-authored-by: Anthony Alaribe <anthonyalaribe@gmail.com> * Update frame/support/src/traits/tokens/fungible/hold.rs Co-authored-by: Anthony Alaribe <anthonyalaribe@gmail.com> * Update frame/support/src/traits/tokens/fungible/hold.rs Co-authored-by: Anthony Alaribe <anthonyalaribe@gmail.com> * Update frame/support/src/traits/tokens/fungible/hold.rs Co-authored-by: Anthony Alaribe <anthonyalaribe@gmail.com> * Update frame/support/src/traits/tokens/currency.rs Co-authored-by: Anthony Alaribe <anthonyalaribe@gmail.com> * Update frame/lottery/src/tests.rs Co-authored-by: Anthony Alaribe <anthonyalaribe@gmail.com> * Update frame/support/src/traits/tokens/fungible/mod.rs Co-authored-by: Anthony Alaribe <anthonyalaribe@gmail.com> * Update frame/support/src/traits/tokens/fungible/regular.rs Co-authored-by: Anthony Alaribe <anthonyalaribe@gmail.com> * Update frame/support/src/traits/tokens/fungibles/freeze.rs Co-authored-by: Anthony Alaribe <anthonyalaribe@gmail.com> * Update frame/support/src/traits/tokens/fungible/regular.rs Co-authored-by: Anthony Alaribe <anthonyalaribe@gmail.com> * Update frame/support/src/traits/tokens/fungibles/hold.rs Co-authored-by: Anthony Alaribe <anthonyalaribe@gmail.com> * Update frame/support/src/traits/tokens/fungibles/hold.rs Co-authored-by: Anthony Alaribe <anthonyalaribe@gmail.com> * Update frame/support/src/traits/tokens/fungibles/hold.rs Co-authored-by: Anthony Alaribe <anthonyalaribe@gmail.com> * Rename UnwantedRemoval to UnwantedAccountRemoval * Docs * Formatting * Update frame/balances/src/lib.rs Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Update primitives/runtime/src/lib.rs Co-authored-by: Keith Yeung <kungfukeith11@gmail.com> * handle_raw_dust oes nothing * Formatting * Fixes * Grumble * Fixes * Add test * Add test * Tests for reducible_balance * Fixes * Fix Salary * Fixes * Disable broken test * Disable nicely * Fixes * Fixes * Fixes * Rename some events * Fix nomination pools breakage * Add compatibility stub for transfer tx * Reinstate a safely compatible version of Balances set_balance * Fixes * Grumble * Update frame/nis/src/lib.rs Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com> * ".git/.scripts/commands/bench/bench.sh" pallet dev pallet_balances * disable flakey tests * Update frame/balances/src/lib.rs Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com> * Grumbles * Grumble --------- Co-authored-by: Muharem Ismailov <ismailov.m.h@gmail.com> Co-authored-by: Alexander Theißen <alex.theissen@me.com> Co-authored-by: Anthony Alaribe <anthonyalaribe@gmail.com> Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> Co-authored-by: Keith Yeung <kungfukeith11@gmail.com> Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com> Co-authored-by: command-bot <>
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,224 @@
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Copyright (C) 2017-2022 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.
|
||||
|
||||
//! Tests regarding the functionality of the dispatchables/extrinsics.
|
||||
|
||||
use super::*;
|
||||
use frame_support::traits::tokens::Preservation::Expendable;
|
||||
use fungible::{hold::Mutate as HoldMutate, Inspect, Mutate};
|
||||
|
||||
#[test]
|
||||
fn default_indexing_on_new_accounts_should_not_work2() {
|
||||
ExtBuilder::default()
|
||||
.existential_deposit(10)
|
||||
.monied(true)
|
||||
.build_and_execute_with(|| {
|
||||
// account 5 should not exist
|
||||
// ext_deposit is 10, value is 9, not satisfies for ext_deposit
|
||||
assert_noop!(
|
||||
Balances::transfer_allow_death(Some(1).into(), 5, 9),
|
||||
TokenError::BelowMinimum,
|
||||
);
|
||||
assert_eq!(Balances::free_balance(1), 100);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dust_account_removal_should_work() {
|
||||
ExtBuilder::default()
|
||||
.existential_deposit(100)
|
||||
.monied(true)
|
||||
.build_and_execute_with(|| {
|
||||
System::inc_account_nonce(&2);
|
||||
assert_eq!(System::account_nonce(&2), 1);
|
||||
assert_eq!(Balances::total_balance(&2), 2000);
|
||||
// index 1 (account 2) becomes zombie
|
||||
assert_ok!(Balances::transfer_allow_death(Some(2).into(), 5, 1901));
|
||||
assert_eq!(Balances::total_balance(&2), 0);
|
||||
assert_eq!(Balances::total_balance(&5), 1901);
|
||||
assert_eq!(System::account_nonce(&2), 0);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn balance_transfer_works() {
|
||||
ExtBuilder::default().build_and_execute_with(|| {
|
||||
let _ = Balances::mint_into(&1, 111);
|
||||
assert_ok!(Balances::transfer_allow_death(Some(1).into(), 2, 69));
|
||||
assert_eq!(Balances::total_balance(&1), 42);
|
||||
assert_eq!(Balances::total_balance(&2), 69);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn force_transfer_works() {
|
||||
ExtBuilder::default().build_and_execute_with(|| {
|
||||
let _ = Balances::mint_into(&1, 111);
|
||||
assert_noop!(Balances::force_transfer(Some(2).into(), 1, 2, 69), BadOrigin,);
|
||||
assert_ok!(Balances::force_transfer(RawOrigin::Root.into(), 1, 2, 69));
|
||||
assert_eq!(Balances::total_balance(&1), 42);
|
||||
assert_eq!(Balances::total_balance(&2), 69);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn balance_transfer_when_on_hold_should_not_work() {
|
||||
ExtBuilder::default().build_and_execute_with(|| {
|
||||
let _ = Balances::mint_into(&1, 111);
|
||||
assert_ok!(Balances::hold(&TestId::Foo, &1, 69));
|
||||
assert_noop!(
|
||||
Balances::transfer_allow_death(Some(1).into(), 2, 69),
|
||||
TokenError::FundsUnavailable,
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn transfer_keep_alive_works() {
|
||||
ExtBuilder::default().existential_deposit(1).build_and_execute_with(|| {
|
||||
let _ = Balances::mint_into(&1, 100);
|
||||
assert_noop!(
|
||||
Balances::transfer_keep_alive(Some(1).into(), 2, 100),
|
||||
TokenError::NotExpendable
|
||||
);
|
||||
assert_eq!(Balances::total_balance(&1), 100);
|
||||
assert_eq!(Balances::total_balance(&2), 0);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn transfer_keep_alive_all_free_succeed() {
|
||||
ExtBuilder::default().existential_deposit(100).build_and_execute_with(|| {
|
||||
assert_ok!(Balances::force_set_balance(RuntimeOrigin::root(), 1, 300));
|
||||
assert_ok!(Balances::hold(&TestId::Foo, &1, 100));
|
||||
assert_ok!(Balances::transfer_keep_alive(Some(1).into(), 2, 100));
|
||||
assert_eq!(Balances::total_balance(&1), 200);
|
||||
assert_eq!(Balances::total_balance(&2), 100);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn transfer_all_works_1() {
|
||||
ExtBuilder::default().existential_deposit(100).build().execute_with(|| {
|
||||
// setup
|
||||
assert_ok!(Balances::force_set_balance(RuntimeOrigin::root(), 1, 200));
|
||||
assert_ok!(Balances::force_set_balance(RuntimeOrigin::root(), 2, 0));
|
||||
// transfer all and allow death
|
||||
assert_ok!(Balances::transfer_all(Some(1).into(), 2, false));
|
||||
assert_eq!(Balances::total_balance(&1), 0);
|
||||
assert_eq!(Balances::total_balance(&2), 200);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn transfer_all_works_2() {
|
||||
ExtBuilder::default().existential_deposit(100).build().execute_with(|| {
|
||||
// setup
|
||||
assert_ok!(Balances::force_set_balance(RuntimeOrigin::root(), 1, 200));
|
||||
assert_ok!(Balances::force_set_balance(RuntimeOrigin::root(), 2, 0));
|
||||
// transfer all and keep alive
|
||||
assert_ok!(Balances::transfer_all(Some(1).into(), 2, true));
|
||||
assert_eq!(Balances::total_balance(&1), 100);
|
||||
assert_eq!(Balances::total_balance(&2), 100);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn transfer_all_works_3() {
|
||||
ExtBuilder::default().existential_deposit(100).build().execute_with(|| {
|
||||
// setup
|
||||
assert_ok!(Balances::force_set_balance(RuntimeOrigin::root(), 1, 210));
|
||||
assert_ok!(Balances::hold(&TestId::Foo, &1, 10));
|
||||
assert_ok!(Balances::force_set_balance(RuntimeOrigin::root(), 2, 0));
|
||||
// transfer all and allow death w/ reserved
|
||||
assert_ok!(Balances::transfer_all(Some(1).into(), 2, false));
|
||||
assert_eq!(Balances::total_balance(&1), 110);
|
||||
assert_eq!(Balances::total_balance(&2), 100);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn transfer_all_works_4() {
|
||||
ExtBuilder::default().existential_deposit(100).build().execute_with(|| {
|
||||
// setup
|
||||
assert_ok!(Balances::force_set_balance(RuntimeOrigin::root(), 1, 210));
|
||||
assert_ok!(Balances::hold(&TestId::Foo, &1, 10));
|
||||
assert_ok!(Balances::force_set_balance(RuntimeOrigin::root(), 2, 0));
|
||||
// transfer all and keep alive w/ reserved
|
||||
assert_ok!(Balances::transfer_all(Some(1).into(), 2, true));
|
||||
assert_eq!(Balances::total_balance(&1), 110);
|
||||
assert_eq!(Balances::total_balance(&2), 100);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn set_balance_handles_killing_account() {
|
||||
ExtBuilder::default().build_and_execute_with(|| {
|
||||
let _ = Balances::mint_into(&1, 111);
|
||||
assert_ok!(frame_system::Pallet::<Test>::inc_consumers(&1));
|
||||
assert_noop!(
|
||||
Balances::force_set_balance(RuntimeOrigin::root(), 1, 0),
|
||||
DispatchError::ConsumerRemaining,
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn set_balance_handles_total_issuance() {
|
||||
ExtBuilder::default().build_and_execute_with(|| {
|
||||
let old_total_issuance = Balances::total_issuance();
|
||||
assert_ok!(Balances::force_set_balance(RuntimeOrigin::root(), 1337, 69));
|
||||
assert_eq!(Balances::total_issuance(), old_total_issuance + 69);
|
||||
assert_eq!(Balances::total_balance(&1337), 69);
|
||||
assert_eq!(Balances::free_balance(&1337), 69);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn upgrade_accounts_should_work() {
|
||||
ExtBuilder::default()
|
||||
.existential_deposit(1)
|
||||
.monied(true)
|
||||
.build_and_execute_with(|| {
|
||||
System::inc_providers(&7);
|
||||
assert_ok!(<Test as Config>::AccountStore::try_mutate_exists(
|
||||
&7,
|
||||
|a| -> DispatchResult {
|
||||
*a = Some(AccountData {
|
||||
free: 5,
|
||||
reserved: 5,
|
||||
frozen: Zero::zero(),
|
||||
flags: crate::types::ExtraFlags::old_logic(),
|
||||
});
|
||||
Ok(())
|
||||
}
|
||||
));
|
||||
assert!(!Balances::account(&7).flags.is_new_logic());
|
||||
assert_eq!(System::providers(&7), 1);
|
||||
assert_eq!(System::consumers(&7), 0);
|
||||
assert_ok!(Balances::upgrade_accounts(Some(1).into(), vec![7]));
|
||||
assert!(Balances::account(&7).flags.is_new_logic());
|
||||
assert_eq!(System::providers(&7), 1);
|
||||
assert_eq!(System::consumers(&7), 1);
|
||||
|
||||
<Balances as frame_support::traits::ReservableCurrency<_>>::unreserve(&7, 5);
|
||||
assert_ok!(<Balances as fungible::Mutate<_>>::transfer(&7, &1, 10, Expendable));
|
||||
assert_eq!(Balances::total_balance(&7), 0);
|
||||
assert_eq!(System::providers(&7), 0);
|
||||
assert_eq!(System::consumers(&7), 0);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,399 @@
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Copyright (C) 2017-2022 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.
|
||||
|
||||
//! Tests regarding the functionality of the `fungible` trait set implementations.
|
||||
|
||||
use super::*;
|
||||
use frame_support::traits::tokens::{
|
||||
Fortitude::{Force, Polite},
|
||||
Precision::{BestEffort, Exact},
|
||||
Preservation::{Expendable, Preserve, Protect},
|
||||
Restriction::Free,
|
||||
};
|
||||
use fungible::{Inspect, InspectFreeze, InspectHold, Mutate, MutateFreeze, MutateHold, Unbalanced};
|
||||
|
||||
#[test]
|
||||
fn inspect_trait_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 inspect_trait_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 inspect_trait_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 unbalanced_trait_set_balance_works() {
|
||||
ExtBuilder::default().build_and_execute_with(|| {
|
||||
assert_eq!(<Balances as fungible::Inspect<_>>::balance(&1337), 0);
|
||||
assert_ok!(Balances::write_balance(&1337, 100));
|
||||
assert_eq!(<Balances as fungible::Inspect<_>>::balance(&1337), 100);
|
||||
|
||||
assert_ok!(<Balances as fungible::MutateHold<_>>::hold(&TestId::Foo, &1337, 60));
|
||||
assert_eq!(<Balances as fungible::Inspect<_>>::balance(&1337), 40);
|
||||
assert_eq!(<Balances as fungible::InspectHold<_>>::total_balance_on_hold(&1337), 60);
|
||||
assert_eq!(
|
||||
<Balances as fungible::InspectHold<_>>::balance_on_hold(&TestId::Foo, &1337),
|
||||
60
|
||||
);
|
||||
|
||||
assert_noop!(Balances::write_balance(&1337, 0), Error::<Test>::InsufficientBalance);
|
||||
|
||||
assert_ok!(Balances::write_balance(&1337, 1));
|
||||
assert_eq!(<Balances as fungible::Inspect<_>>::balance(&1337), 1);
|
||||
assert_eq!(
|
||||
<Balances as fungible::InspectHold<_>>::balance_on_hold(&TestId::Foo, &1337),
|
||||
60
|
||||
);
|
||||
|
||||
assert_ok!(<Balances as fungible::MutateHold<_>>::release(&TestId::Foo, &1337, 60, Exact));
|
||||
assert_eq!(<Balances as fungible::InspectHold<_>>::balance_on_hold(&TestId::Foo, &1337), 0);
|
||||
assert_eq!(<Balances as fungible::InspectHold<_>>::total_balance_on_hold(&1337), 0);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unbalanced_trait_set_total_issuance_works() {
|
||||
ExtBuilder::default().build_and_execute_with(|| {
|
||||
assert_eq!(<Balances as fungible::Inspect<_>>::total_issuance(), 0);
|
||||
Balances::set_total_issuance(100);
|
||||
assert_eq!(<Balances as fungible::Inspect<_>>::total_issuance(), 100);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unbalanced_trait_decrease_balance_simple_works() {
|
||||
ExtBuilder::default().build_and_execute_with(|| {
|
||||
// An Account that starts at 100
|
||||
assert_ok!(Balances::write_balance(&1337, 100));
|
||||
assert_eq!(<Balances as fungible::Inspect<_>>::balance(&1337), 100);
|
||||
// and reserves 50
|
||||
assert_ok!(<Balances as fungible::MutateHold<_>>::hold(&TestId::Foo, &1337, 50));
|
||||
assert_eq!(<Balances as fungible::Inspect<_>>::balance(&1337), 50);
|
||||
// and is decreased by 20
|
||||
assert_ok!(Balances::decrease_balance(&1337, 20, Exact, Expendable, Polite));
|
||||
assert_eq!(<Balances as fungible::Inspect<_>>::balance(&1337), 30);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unbalanced_trait_decrease_balance_works_1() {
|
||||
ExtBuilder::default().build_and_execute_with(|| {
|
||||
assert_ok!(Balances::write_balance(&1337, 100));
|
||||
assert_eq!(<Balances as fungible::Inspect<_>>::balance(&1337), 100);
|
||||
|
||||
assert_noop!(
|
||||
Balances::decrease_balance(&1337, 101, Exact, Expendable, Polite),
|
||||
TokenError::FundsUnavailable
|
||||
);
|
||||
assert_eq!(Balances::decrease_balance(&1337, 100, Exact, Expendable, Polite), Ok(100));
|
||||
assert_eq!(<Balances as fungible::Inspect<_>>::balance(&1337), 0);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unbalanced_trait_decrease_balance_works_2() {
|
||||
ExtBuilder::default().build_and_execute_with(|| {
|
||||
// free: 40, reserved: 60
|
||||
assert_ok!(Balances::write_balance(&1337, 100));
|
||||
assert_ok!(Balances::hold(&TestId::Foo, &1337, 60));
|
||||
assert_eq!(<Balances as fungible::Inspect<_>>::balance(&1337), 40);
|
||||
assert_eq!(Balances::total_balance_on_hold(&1337), 60);
|
||||
assert_noop!(
|
||||
Balances::decrease_balance(&1337, 40, Exact, Expendable, Polite),
|
||||
Error::<Test>::InsufficientBalance
|
||||
);
|
||||
assert_eq!(Balances::decrease_balance(&1337, 39, Exact, Expendable, Polite), Ok(39));
|
||||
assert_eq!(<Balances as fungible::Inspect<_>>::balance(&1337), 1);
|
||||
assert_eq!(Balances::total_balance_on_hold(&1337), 60);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unbalanced_trait_decrease_balance_at_most_works_1() {
|
||||
ExtBuilder::default().build_and_execute_with(|| {
|
||||
assert_ok!(Balances::write_balance(&1337, 100));
|
||||
assert_eq!(<Balances as fungible::Inspect<_>>::balance(&1337), 100);
|
||||
|
||||
assert_eq!(Balances::decrease_balance(&1337, 101, BestEffort, Expendable, Polite), Ok(100));
|
||||
assert_eq!(<Balances as fungible::Inspect<_>>::balance(&1337), 0);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unbalanced_trait_decrease_balance_at_most_works_2() {
|
||||
ExtBuilder::default().build_and_execute_with(|| {
|
||||
assert_ok!(Balances::write_balance(&1337, 99));
|
||||
assert_eq!(Balances::decrease_balance(&1337, 99, BestEffort, Expendable, Polite), Ok(99));
|
||||
assert_eq!(<Balances as fungible::Inspect<_>>::balance(&1337), 0);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unbalanced_trait_decrease_balance_at_most_works_3() {
|
||||
ExtBuilder::default().build_and_execute_with(|| {
|
||||
// free: 40, reserved: 60
|
||||
assert_ok!(Balances::write_balance(&1337, 100));
|
||||
assert_ok!(Balances::hold(&TestId::Foo, &1337, 60));
|
||||
assert_eq!(Balances::free_balance(1337), 40);
|
||||
assert_eq!(Balances::total_balance_on_hold(&1337), 60);
|
||||
assert_eq!(Balances::decrease_balance(&1337, 0, BestEffort, Expendable, Polite), Ok(0));
|
||||
assert_eq!(Balances::free_balance(1337), 40);
|
||||
assert_eq!(Balances::total_balance_on_hold(&1337), 60);
|
||||
assert_eq!(Balances::decrease_balance(&1337, 10, BestEffort, Expendable, Polite), Ok(10));
|
||||
assert_eq!(Balances::free_balance(1337), 30);
|
||||
assert_eq!(Balances::decrease_balance(&1337, 200, BestEffort, Expendable, Polite), Ok(29));
|
||||
assert_eq!(<Balances as fungible::Inspect<_>>::balance(&1337), 1);
|
||||
assert_eq!(Balances::free_balance(1337), 1);
|
||||
assert_eq!(Balances::total_balance_on_hold(&1337), 60);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unbalanced_trait_increase_balance_works() {
|
||||
ExtBuilder::default().build_and_execute_with(|| {
|
||||
assert_noop!(Balances::increase_balance(&1337, 0, Exact), TokenError::BelowMinimum);
|
||||
assert_eq!(Balances::increase_balance(&1337, 1, Exact), Ok(1));
|
||||
assert_noop!(Balances::increase_balance(&1337, u64::MAX, Exact), ArithmeticError::Overflow);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unbalanced_trait_increase_balance_at_most_works() {
|
||||
ExtBuilder::default().build_and_execute_with(|| {
|
||||
assert_eq!(Balances::increase_balance(&1337, 0, BestEffort), Ok(0));
|
||||
assert_eq!(Balances::increase_balance(&1337, 1, BestEffort), Ok(1));
|
||||
assert_eq!(Balances::increase_balance(&1337, u64::MAX, BestEffort), Ok(u64::MAX - 1));
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn freezing_and_holds_should_overlap() {
|
||||
ExtBuilder::default()
|
||||
.existential_deposit(1)
|
||||
.monied(true)
|
||||
.build_and_execute_with(|| {
|
||||
assert_ok!(Balances::set_freeze(&TestId::Foo, &1, 10));
|
||||
assert_ok!(Balances::hold(&TestId::Foo, &1, 9));
|
||||
assert_eq!(Balances::account(&1).free, 1);
|
||||
assert_eq!(System::consumers(&1), 1);
|
||||
assert_eq!(Balances::account(&1).free, 1);
|
||||
assert_eq!(Balances::account(&1).frozen, 10);
|
||||
assert_eq!(Balances::account(&1).reserved, 9);
|
||||
assert_eq!(Balances::total_balance_on_hold(&1), 9);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn frozen_hold_balance_cannot_be_moved_without_force() {
|
||||
ExtBuilder::default()
|
||||
.existential_deposit(1)
|
||||
.monied(true)
|
||||
.build_and_execute_with(|| {
|
||||
assert_ok!(Balances::set_freeze(&TestId::Foo, &1, 10));
|
||||
assert_ok!(Balances::hold(&TestId::Foo, &1, 9));
|
||||
assert_eq!(Balances::reducible_total_balance_on_hold(&1, Force), 9);
|
||||
assert_eq!(Balances::reducible_total_balance_on_hold(&1, Polite), 0);
|
||||
let e = TokenError::Frozen;
|
||||
assert_noop!(
|
||||
Balances::transfer_on_hold(&TestId::Foo, &1, &2, 1, Exact, Free, Polite),
|
||||
e
|
||||
);
|
||||
assert_ok!(Balances::transfer_on_hold(&TestId::Foo, &1, &2, 1, Exact, Free, Force));
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn frozen_hold_balance_best_effort_transfer_works() {
|
||||
ExtBuilder::default()
|
||||
.existential_deposit(1)
|
||||
.monied(true)
|
||||
.build_and_execute_with(|| {
|
||||
assert_ok!(Balances::set_freeze(&TestId::Foo, &1, 5));
|
||||
assert_ok!(Balances::hold(&TestId::Foo, &1, 9));
|
||||
assert_eq!(Balances::reducible_total_balance_on_hold(&1, Force), 9);
|
||||
assert_eq!(Balances::reducible_total_balance_on_hold(&1, Polite), 5);
|
||||
assert_ok!(Balances::transfer_on_hold(
|
||||
&TestId::Foo,
|
||||
&1,
|
||||
&2,
|
||||
10,
|
||||
BestEffort,
|
||||
Free,
|
||||
Polite
|
||||
));
|
||||
assert_eq!(Balances::total_balance(&1), 5);
|
||||
assert_eq!(Balances::total_balance(&2), 25);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn partial_freezing_should_work() {
|
||||
ExtBuilder::default()
|
||||
.existential_deposit(1)
|
||||
.monied(true)
|
||||
.build_and_execute_with(|| {
|
||||
assert_ok!(Balances::set_freeze(&TestId::Foo, &1, 5));
|
||||
assert_eq!(System::consumers(&1), 1);
|
||||
assert_ok!(<Balances as fungible::Mutate<_>>::transfer(&1, &2, 5, Expendable));
|
||||
assert_noop!(
|
||||
<Balances as fungible::Mutate<_>>::transfer(&1, &2, 1, Expendable),
|
||||
TokenError::Frozen
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn thaw_should_work() {
|
||||
ExtBuilder::default()
|
||||
.existential_deposit(1)
|
||||
.monied(true)
|
||||
.build_and_execute_with(|| {
|
||||
assert_ok!(Balances::set_freeze(&TestId::Foo, &1, u64::MAX));
|
||||
assert_ok!(Balances::thaw(&TestId::Foo, &1));
|
||||
assert_eq!(System::consumers(&1), 0);
|
||||
assert_eq!(Balances::balance_frozen(&TestId::Foo, &1), 0);
|
||||
assert_eq!(Balances::account(&1).frozen, 0);
|
||||
assert_ok!(<Balances as fungible::Mutate<_>>::transfer(&1, &2, 10, Expendable));
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn set_freeze_zero_should_work() {
|
||||
ExtBuilder::default()
|
||||
.existential_deposit(1)
|
||||
.monied(true)
|
||||
.build_and_execute_with(|| {
|
||||
assert_ok!(Balances::set_freeze(&TestId::Foo, &1, u64::MAX));
|
||||
assert_ok!(Balances::set_freeze(&TestId::Foo, &1, 0));
|
||||
assert_eq!(System::consumers(&1), 0);
|
||||
assert_eq!(Balances::balance_frozen(&TestId::Foo, &1), 0);
|
||||
assert_eq!(Balances::account(&1).frozen, 0);
|
||||
assert_ok!(<Balances as fungible::Mutate<_>>::transfer(&1, &2, 10, Expendable));
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn set_freeze_should_work() {
|
||||
ExtBuilder::default()
|
||||
.existential_deposit(1)
|
||||
.monied(true)
|
||||
.build_and_execute_with(|| {
|
||||
assert_ok!(Balances::set_freeze(&TestId::Foo, &1, u64::MAX));
|
||||
assert_ok!(Balances::set_freeze(&TestId::Foo, &1, 5));
|
||||
assert_ok!(<Balances as fungible::Mutate<_>>::transfer(&1, &2, 5, Expendable));
|
||||
assert_noop!(
|
||||
<Balances as fungible::Mutate<_>>::transfer(&1, &2, 1, Expendable),
|
||||
TokenError::Frozen
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn extend_freeze_should_work() {
|
||||
ExtBuilder::default()
|
||||
.existential_deposit(1)
|
||||
.monied(true)
|
||||
.build_and_execute_with(|| {
|
||||
assert_ok!(Balances::set_freeze(&TestId::Foo, &1, 5));
|
||||
assert_ok!(Balances::extend_freeze(&TestId::Foo, &1, 10));
|
||||
assert_eq!(Balances::account(&1).frozen, 10);
|
||||
assert_eq!(Balances::balance_frozen(&TestId::Foo, &1), 10);
|
||||
assert_noop!(
|
||||
<Balances as fungible::Mutate<_>>::transfer(&1, &2, 1, Expendable),
|
||||
TokenError::Frozen
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn double_freezing_should_work() {
|
||||
ExtBuilder::default()
|
||||
.existential_deposit(1)
|
||||
.monied(true)
|
||||
.build_and_execute_with(|| {
|
||||
assert_ok!(Balances::set_freeze(&TestId::Foo, &1, 5));
|
||||
assert_ok!(Balances::set_freeze(&TestId::Bar, &1, 5));
|
||||
assert_eq!(System::consumers(&1), 1);
|
||||
assert_ok!(<Balances as fungible::Mutate<_>>::transfer(&1, &2, 5, Expendable));
|
||||
assert_noop!(
|
||||
<Balances as fungible::Mutate<_>>::transfer(&1, &2, 1, Expendable),
|
||||
TokenError::Frozen
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn can_hold_entire_balance_when_second_provider() {
|
||||
ExtBuilder::default()
|
||||
.existential_deposit(1)
|
||||
.monied(false)
|
||||
.build_and_execute_with(|| {
|
||||
<Balances as fungible::Mutate<_>>::set_balance(&1, 100);
|
||||
assert_noop!(Balances::hold(&TestId::Foo, &1, 100), TokenError::FundsUnavailable);
|
||||
System::inc_providers(&1);
|
||||
assert_eq!(System::providers(&1), 2);
|
||||
assert_ok!(Balances::hold(&TestId::Foo, &1, 100));
|
||||
assert_eq!(System::providers(&1), 1);
|
||||
assert_noop!(System::dec_providers(&1), DispatchError::ConsumerRemaining);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unholding_frees_hold_slot() {
|
||||
ExtBuilder::default()
|
||||
.existential_deposit(1)
|
||||
.monied(false)
|
||||
.build_and_execute_with(|| {
|
||||
<Balances as fungible::Mutate<_>>::set_balance(&1, 100);
|
||||
assert_ok!(Balances::hold(&TestId::Foo, &1, 10));
|
||||
assert_ok!(Balances::hold(&TestId::Bar, &1, 10));
|
||||
assert_ok!(Balances::release(&TestId::Foo, &1, 10, Exact));
|
||||
assert_ok!(Balances::hold(&TestId::Baz, &1, 10));
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,296 @@
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Copyright (C) 2018-2022 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.
|
||||
|
||||
//! Tests.
|
||||
|
||||
#![cfg(test)]
|
||||
|
||||
use crate::{self as pallet_balances, AccountData, Config, CreditOf, Error, Pallet};
|
||||
use codec::{Decode, Encode, MaxEncodedLen};
|
||||
use frame_support::{
|
||||
assert_err, assert_noop, assert_ok, assert_storage_noop,
|
||||
dispatch::DispatchInfo,
|
||||
parameter_types,
|
||||
traits::{
|
||||
tokens::fungible, ConstU32, ConstU64, ConstU8, Imbalance as ImbalanceT, OnUnbalanced,
|
||||
StorageMapShim, StoredMap,
|
||||
},
|
||||
weights::{IdentityFee, Weight},
|
||||
RuntimeDebug,
|
||||
};
|
||||
use frame_system::{self as system, RawOrigin};
|
||||
use pallet_transaction_payment::{ChargeTransactionPayment, CurrencyAdapter, Multiplier};
|
||||
use scale_info::TypeInfo;
|
||||
use sp_core::H256;
|
||||
use sp_io;
|
||||
use sp_runtime::{
|
||||
testing::Header,
|
||||
traits::{BadOrigin, IdentityLookup, SignedExtension, Zero},
|
||||
ArithmeticError, DispatchError, DispatchResult, FixedPointNumber, TokenError,
|
||||
};
|
||||
|
||||
mod currency_tests;
|
||||
mod dispatchable_tests;
|
||||
mod fungible_tests;
|
||||
mod reentrancy_tests;
|
||||
|
||||
type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
|
||||
type Block = frame_system::mocking::MockBlock<Test>;
|
||||
|
||||
#[derive(
|
||||
Encode,
|
||||
Decode,
|
||||
Copy,
|
||||
Clone,
|
||||
Eq,
|
||||
PartialEq,
|
||||
Ord,
|
||||
PartialOrd,
|
||||
MaxEncodedLen,
|
||||
TypeInfo,
|
||||
RuntimeDebug,
|
||||
)]
|
||||
pub enum TestId {
|
||||
Foo,
|
||||
Bar,
|
||||
Baz,
|
||||
}
|
||||
|
||||
frame_support::construct_runtime!(
|
||||
pub struct Test where
|
||||
Block = Block,
|
||||
NodeBlock = Block,
|
||||
UncheckedExtrinsic = UncheckedExtrinsic,
|
||||
{
|
||||
System: frame_system::{Pallet, Call, Config, Storage, Event<T>},
|
||||
Balances: pallet_balances::{Pallet, Call, Storage, Config<T>, Event<T>},
|
||||
TransactionPayment: pallet_transaction_payment::{Pallet, Storage, Event<T>},
|
||||
}
|
||||
);
|
||||
|
||||
parameter_types! {
|
||||
pub BlockWeights: frame_system::limits::BlockWeights =
|
||||
frame_system::limits::BlockWeights::simple_max(
|
||||
frame_support::weights::Weight::from_parts(1024, u64::MAX),
|
||||
);
|
||||
pub static ExistentialDeposit: u64 = 0;
|
||||
}
|
||||
impl frame_system::Config for Test {
|
||||
type BaseCallFilter = frame_support::traits::Everything;
|
||||
type BlockWeights = BlockWeights;
|
||||
type BlockLength = ();
|
||||
type DbWeight = ();
|
||||
type RuntimeOrigin = RuntimeOrigin;
|
||||
type Index = u64;
|
||||
type BlockNumber = u64;
|
||||
type RuntimeCall = RuntimeCall;
|
||||
type Hash = H256;
|
||||
type Hashing = ::sp_runtime::traits::BlakeTwo256;
|
||||
type AccountId = u64;
|
||||
type Lookup = IdentityLookup<Self::AccountId>;
|
||||
type Header = Header;
|
||||
type RuntimeEvent = RuntimeEvent;
|
||||
type BlockHashCount = ConstU64<250>;
|
||||
type Version = ();
|
||||
type PalletInfo = PalletInfo;
|
||||
type AccountData = super::AccountData<u64>;
|
||||
type OnNewAccount = ();
|
||||
type OnKilledAccount = ();
|
||||
type SystemWeightInfo = ();
|
||||
type SS58Prefix = ();
|
||||
type OnSetCode = ();
|
||||
type MaxConsumers = frame_support::traits::ConstU32<16>;
|
||||
}
|
||||
|
||||
impl pallet_transaction_payment::Config for Test {
|
||||
type RuntimeEvent = RuntimeEvent;
|
||||
type OnChargeTransaction = CurrencyAdapter<Pallet<Test>, ()>;
|
||||
type OperationalFeeMultiplier = ConstU8<5>;
|
||||
type WeightToFee = IdentityFee<u64>;
|
||||
type LengthToFee = IdentityFee<u64>;
|
||||
type FeeMultiplierUpdate = ();
|
||||
}
|
||||
|
||||
impl Config for Test {
|
||||
type Balance = u64;
|
||||
type DustRemoval = DustTrap;
|
||||
type RuntimeEvent = RuntimeEvent;
|
||||
type ExistentialDeposit = ExistentialDeposit;
|
||||
type AccountStore = TestAccountStore;
|
||||
type MaxLocks = ConstU32<50>;
|
||||
type MaxReserves = ConstU32<2>;
|
||||
type ReserveIdentifier = TestId;
|
||||
type WeightInfo = ();
|
||||
type HoldIdentifier = TestId;
|
||||
type FreezeIdentifier = TestId;
|
||||
type MaxFreezes = ConstU32<2>;
|
||||
type MaxHolds = ConstU32<2>;
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ExtBuilder {
|
||||
existential_deposit: u64,
|
||||
monied: bool,
|
||||
dust_trap: Option<u64>,
|
||||
}
|
||||
impl Default for ExtBuilder {
|
||||
fn default() -> Self {
|
||||
Self { existential_deposit: 1, monied: false, dust_trap: None }
|
||||
}
|
||||
}
|
||||
impl ExtBuilder {
|
||||
pub fn existential_deposit(mut self, existential_deposit: u64) -> Self {
|
||||
self.existential_deposit = existential_deposit;
|
||||
self
|
||||
}
|
||||
pub fn monied(mut self, monied: bool) -> Self {
|
||||
self.monied = monied;
|
||||
if self.existential_deposit == 0 {
|
||||
self.existential_deposit = 1;
|
||||
}
|
||||
self
|
||||
}
|
||||
pub fn dust_trap(mut self, account: u64) -> Self {
|
||||
self.dust_trap = Some(account);
|
||||
self
|
||||
}
|
||||
pub fn set_associated_consts(&self) {
|
||||
DUST_TRAP_TARGET.with(|v| v.replace(self.dust_trap));
|
||||
EXISTENTIAL_DEPOSIT.with(|v| v.replace(self.existential_deposit));
|
||||
}
|
||||
pub fn build(self) -> sp_io::TestExternalities {
|
||||
self.set_associated_consts();
|
||||
let mut t = frame_system::GenesisConfig::default().build_storage::<Test>().unwrap();
|
||||
pallet_balances::GenesisConfig::<Test> {
|
||||
balances: if self.monied {
|
||||
vec![
|
||||
(1, 10 * self.existential_deposit),
|
||||
(2, 20 * self.existential_deposit),
|
||||
(3, 30 * self.existential_deposit),
|
||||
(4, 40 * self.existential_deposit),
|
||||
(12, 10 * self.existential_deposit),
|
||||
]
|
||||
} else {
|
||||
vec![]
|
||||
},
|
||||
}
|
||||
.assimilate_storage(&mut t)
|
||||
.unwrap();
|
||||
|
||||
let mut ext = sp_io::TestExternalities::new(t);
|
||||
ext.execute_with(|| System::set_block_number(1));
|
||||
ext
|
||||
}
|
||||
pub fn build_and_execute_with(self, f: impl Fn()) {
|
||||
let other = self.clone();
|
||||
UseSystem::set(false);
|
||||
other.build().execute_with(|| f());
|
||||
UseSystem::set(true);
|
||||
self.build().execute_with(|| f());
|
||||
}
|
||||
}
|
||||
|
||||
parameter_types! {
|
||||
static DustTrapTarget: Option<u64> = None;
|
||||
}
|
||||
|
||||
pub struct DustTrap;
|
||||
|
||||
impl OnUnbalanced<CreditOf<Test, ()>> for DustTrap {
|
||||
fn on_nonzero_unbalanced(amount: CreditOf<Test, ()>) {
|
||||
match DustTrapTarget::get() {
|
||||
None => drop(amount),
|
||||
Some(a) => {
|
||||
let result = <Balances as fungible::Balanced<_>>::resolve(&a, amount);
|
||||
debug_assert!(result.is_ok());
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
parameter_types! {
|
||||
pub static UseSystem: bool = false;
|
||||
}
|
||||
|
||||
type BalancesAccountStore = StorageMapShim<super::Account<Test>, u64, super::AccountData<u64>>;
|
||||
type SystemAccountStore = frame_system::Pallet<Test>;
|
||||
|
||||
pub struct TestAccountStore;
|
||||
impl StoredMap<u64, super::AccountData<u64>> for TestAccountStore {
|
||||
fn get(k: &u64) -> super::AccountData<u64> {
|
||||
if UseSystem::get() {
|
||||
<SystemAccountStore as StoredMap<_, _>>::get(k)
|
||||
} else {
|
||||
<BalancesAccountStore as StoredMap<_, _>>::get(k)
|
||||
}
|
||||
}
|
||||
fn try_mutate_exists<R, E: From<DispatchError>>(
|
||||
k: &u64,
|
||||
f: impl FnOnce(&mut Option<super::AccountData<u64>>) -> Result<R, E>,
|
||||
) -> Result<R, E> {
|
||||
if UseSystem::get() {
|
||||
<SystemAccountStore as StoredMap<_, _>>::try_mutate_exists(k, f)
|
||||
} else {
|
||||
<BalancesAccountStore as StoredMap<_, _>>::try_mutate_exists(k, f)
|
||||
}
|
||||
}
|
||||
fn mutate<R>(
|
||||
k: &u64,
|
||||
f: impl FnOnce(&mut super::AccountData<u64>) -> R,
|
||||
) -> Result<R, DispatchError> {
|
||||
if UseSystem::get() {
|
||||
<SystemAccountStore as StoredMap<_, _>>::mutate(k, f)
|
||||
} else {
|
||||
<BalancesAccountStore as StoredMap<_, _>>::mutate(k, f)
|
||||
}
|
||||
}
|
||||
fn mutate_exists<R>(
|
||||
k: &u64,
|
||||
f: impl FnOnce(&mut Option<super::AccountData<u64>>) -> R,
|
||||
) -> Result<R, DispatchError> {
|
||||
if UseSystem::get() {
|
||||
<SystemAccountStore as StoredMap<_, _>>::mutate_exists(k, f)
|
||||
} else {
|
||||
<BalancesAccountStore as StoredMap<_, _>>::mutate_exists(k, f)
|
||||
}
|
||||
}
|
||||
fn insert(k: &u64, t: super::AccountData<u64>) -> Result<(), DispatchError> {
|
||||
if UseSystem::get() {
|
||||
<SystemAccountStore as StoredMap<_, _>>::insert(k, t)
|
||||
} else {
|
||||
<BalancesAccountStore as StoredMap<_, _>>::insert(k, t)
|
||||
}
|
||||
}
|
||||
fn remove(k: &u64) -> Result<(), DispatchError> {
|
||||
if UseSystem::get() {
|
||||
<SystemAccountStore as StoredMap<_, _>>::remove(k)
|
||||
} else {
|
||||
<BalancesAccountStore as StoredMap<_, _>>::remove(k)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn events() -> Vec<RuntimeEvent> {
|
||||
let evt = System::events().into_iter().map(|evt| evt.event).collect::<Vec<_>>();
|
||||
System::reset_events();
|
||||
evt
|
||||
}
|
||||
|
||||
/// create a transaction info struct from weight. Handy to avoid building the whole struct.
|
||||
pub fn info_from_weight(w: Weight) -> DispatchInfo {
|
||||
DispatchInfo { weight: w, ..Default::default() }
|
||||
}
|
||||
@@ -0,0 +1,195 @@
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Copyright (C) 2017-2022 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.
|
||||
|
||||
//! Tests regarding the reentrancy functionality.
|
||||
|
||||
use super::*;
|
||||
use frame_support::traits::tokens::{
|
||||
Fortitude::Force,
|
||||
Precision::BestEffort,
|
||||
Preservation::{Expendable, Protect},
|
||||
};
|
||||
use fungible::Balanced;
|
||||
|
||||
#[test]
|
||||
fn transfer_dust_removal_tst1_should_work() {
|
||||
ExtBuilder::default()
|
||||
.existential_deposit(100)
|
||||
.dust_trap(1)
|
||||
.build_and_execute_with(|| {
|
||||
// Verification of reentrancy in dust removal
|
||||
assert_ok!(Balances::force_set_balance(RawOrigin::Root.into(), 1, 1000));
|
||||
assert_ok!(Balances::force_set_balance(RawOrigin::Root.into(), 2, 500));
|
||||
|
||||
// In this transaction, account 2 free balance
|
||||
// drops below existential balance
|
||||
// and dust balance is removed from account 2
|
||||
assert_ok!(Balances::transfer_allow_death(RawOrigin::Signed(2).into(), 3, 450));
|
||||
|
||||
// As expected dust balance is removed.
|
||||
assert_eq!(Balances::free_balance(&2), 0);
|
||||
|
||||
// As expected beneficiary account 3
|
||||
// received the transfered fund.
|
||||
assert_eq!(Balances::free_balance(&3), 450);
|
||||
|
||||
// Dust balance is deposited to account 1
|
||||
// during the process of dust removal.
|
||||
assert_eq!(Balances::free_balance(&1), 1050);
|
||||
|
||||
// Verify the events
|
||||
assert_eq!(System::events().len(), 12);
|
||||
|
||||
System::assert_has_event(RuntimeEvent::Balances(crate::Event::Transfer {
|
||||
from: 2,
|
||||
to: 3,
|
||||
amount: 450,
|
||||
}));
|
||||
System::assert_has_event(RuntimeEvent::Balances(crate::Event::DustLost {
|
||||
account: 2,
|
||||
amount: 50,
|
||||
}));
|
||||
System::assert_has_event(RuntimeEvent::Balances(crate::Event::Deposit {
|
||||
who: 1,
|
||||
amount: 50,
|
||||
}));
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn transfer_dust_removal_tst2_should_work() {
|
||||
ExtBuilder::default()
|
||||
.existential_deposit(100)
|
||||
.dust_trap(1)
|
||||
.build_and_execute_with(|| {
|
||||
// Verification of reentrancy in dust removal
|
||||
assert_ok!(Balances::force_set_balance(RawOrigin::Root.into(), 1, 1000));
|
||||
assert_ok!(Balances::force_set_balance(RawOrigin::Root.into(), 2, 500));
|
||||
|
||||
// In this transaction, account 2 free balance
|
||||
// drops below existential balance
|
||||
// and dust balance is removed from account 2
|
||||
assert_ok!(Balances::transfer_allow_death(RawOrigin::Signed(2).into(), 1, 450));
|
||||
|
||||
// As expected dust balance is removed.
|
||||
assert_eq!(Balances::free_balance(&2), 0);
|
||||
|
||||
// Dust balance is deposited to account 1
|
||||
// during the process of dust removal.
|
||||
assert_eq!(Balances::free_balance(&1), 1500);
|
||||
|
||||
// Verify the events
|
||||
assert_eq!(System::events().len(), 10);
|
||||
|
||||
System::assert_has_event(RuntimeEvent::Balances(crate::Event::Transfer {
|
||||
from: 2,
|
||||
to: 1,
|
||||
amount: 450,
|
||||
}));
|
||||
System::assert_has_event(RuntimeEvent::Balances(crate::Event::DustLost {
|
||||
account: 2,
|
||||
amount: 50,
|
||||
}));
|
||||
System::assert_has_event(RuntimeEvent::Balances(crate::Event::Deposit {
|
||||
who: 1,
|
||||
amount: 50,
|
||||
}));
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn repatriating_reserved_balance_dust_removal_should_work() {
|
||||
ExtBuilder::default()
|
||||
.existential_deposit(100)
|
||||
.dust_trap(1)
|
||||
.build_and_execute_with(|| {
|
||||
// Verification of reentrancy in dust removal
|
||||
assert_ok!(Balances::force_set_balance(RawOrigin::Root.into(), 1, 1000));
|
||||
assert_ok!(Balances::force_set_balance(RawOrigin::Root.into(), 2, 500));
|
||||
|
||||
// Reserve a value on account 2,
|
||||
// Such that free balance is lower than
|
||||
// Exestintial deposit.
|
||||
assert_ok!(Balances::transfer_allow_death(RuntimeOrigin::signed(2), 1, 450));
|
||||
|
||||
// Since free balance of account 2 is lower than
|
||||
// existential deposit, dust amount is
|
||||
// removed from the account 2
|
||||
assert_eq!(Balances::reserved_balance(2), 0);
|
||||
assert_eq!(Balances::free_balance(2), 0);
|
||||
|
||||
// account 1 is credited with reserved amount
|
||||
// together with dust balance during dust
|
||||
// removal.
|
||||
assert_eq!(Balances::reserved_balance(1), 0);
|
||||
assert_eq!(Balances::free_balance(1), 1500);
|
||||
|
||||
// Verify the events
|
||||
assert_eq!(System::events().len(), 10);
|
||||
|
||||
System::assert_has_event(RuntimeEvent::Balances(crate::Event::Transfer {
|
||||
from: 2,
|
||||
to: 1,
|
||||
amount: 450,
|
||||
}));
|
||||
System::assert_has_event(RuntimeEvent::Balances(crate::Event::DustLost {
|
||||
account: 2,
|
||||
amount: 50,
|
||||
}));
|
||||
System::assert_has_event(RuntimeEvent::Balances(crate::Event::Deposit {
|
||||
who: 1,
|
||||
amount: 50,
|
||||
}));
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn emit_events_with_no_existential_deposit_suicide_with_dust() {
|
||||
ExtBuilder::default().existential_deposit(2).build_and_execute_with(|| {
|
||||
assert_ok!(Balances::force_set_balance(RawOrigin::Root.into(), 1, 100));
|
||||
|
||||
assert_eq!(
|
||||
events(),
|
||||
[
|
||||
RuntimeEvent::System(system::Event::NewAccount { account: 1 }),
|
||||
RuntimeEvent::Balances(crate::Event::Endowed { account: 1, free_balance: 100 }),
|
||||
RuntimeEvent::Balances(crate::Event::BalanceSet { who: 1, free: 100 }),
|
||||
]
|
||||
);
|
||||
|
||||
let res = Balances::withdraw(&1, 98, BestEffort, Protect, Force);
|
||||
assert_eq!(res.unwrap().peek(), 98);
|
||||
|
||||
// no events
|
||||
assert_eq!(
|
||||
events(),
|
||||
[RuntimeEvent::Balances(crate::Event::Withdraw { who: 1, amount: 98 })]
|
||||
);
|
||||
|
||||
let res = Balances::withdraw(&1, 1, BestEffort, Expendable, Force);
|
||||
assert_eq!(res.unwrap().peek(), 1);
|
||||
|
||||
assert_eq!(
|
||||
events(),
|
||||
[
|
||||
RuntimeEvent::System(system::Event::KilledAccount { account: 1 }),
|
||||
RuntimeEvent::Balances(crate::Event::DustLost { account: 1, amount: 1 }),
|
||||
RuntimeEvent::Balances(crate::Event::Withdraw { who: 1, amount: 1 })
|
||||
]
|
||||
);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user