From 868b0fd43251f8561cf0a5fae924d902cf7545d8 Mon Sep 17 00:00:00 2001 From: wangjj9219 <183318287@qq.com> Date: Tue, 20 Jul 2021 06:37:53 +0800 Subject: [PATCH] fix check when allow death (#559) --- tokens/src/lib.rs | 4 ++-- tokens/src/tests.rs | 48 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/tokens/src/lib.rs b/tokens/src/lib.rs index b37052a..65ff4bd 100644 --- a/tokens/src/lib.rs +++ b/tokens/src/lib.rs @@ -597,7 +597,7 @@ impl<T: Config> Pallet<T> { Self::ensure_can_withdraw(currency_id, from, amount)?; let allow_death = existence_requirement == ExistenceRequirement::AllowDeath; - let allow_death = allow_death && !frame_system::Pallet::<T>::is_provider_required(from); + let allow_death = allow_death && frame_system::Pallet::<T>::can_dec_provider(from); // if from_account does not allow death and non_zero total is below existential // deposit, would return an error. ensure!(allow_death || from_account.total() >= ed, Error::<T>::KeepAlive); @@ -1285,7 +1285,7 @@ where let ed = T::ExistentialDeposits::get(¤cy_id); let allow_death = liveness == ExistenceRequirement::AllowDeath; - let allow_death = allow_death && !frame_system::Pallet::<T>::is_provider_required(who); + let allow_death = allow_death && frame_system::Pallet::<T>::can_dec_provider(who); ensure!(allow_death || account.total() >= ed, Error::<T>::KeepAlive); Ok(()) diff --git a/tokens/src/tests.rs b/tokens/src/tests.rs index e021a05..16590f1 100644 --- a/tokens/src/tests.rs +++ b/tokens/src/tests.rs @@ -340,6 +340,26 @@ fn transfer_all_should_work() { }); } +#[test] +fn transfer_failed_when_allow_death_but_cannot_dec_provider() { + ExtBuilder::default() + .one_hundred_for_alice_n_bob() + .build() + .execute_with(|| { + assert_eq!(System::can_dec_provider(&ALICE), true); + assert_ok!(System::inc_consumers(&ALICE)); + assert_eq!(System::can_dec_provider(&ALICE), false); + assert_noop!( + Tokens::transfer(Some(ALICE).into(), BOB, DOT, 100), + Error::<Runtime>::KeepAlive + ); + + assert_ok!(Tokens::deposit(BTC, &ALICE, 100)); + assert_eq!(System::can_dec_provider(&ALICE), true); + assert_ok!(Tokens::transfer(Some(ALICE).into(), BOB, DOT, 100)); + }); +} + #[test] fn deposit_should_work() { ExtBuilder::default() @@ -593,6 +613,34 @@ fn currency_adapter_ensure_currency_adapter_should_work() { }); } +#[test] +fn currency_adapter_withdraw_failed_when_allow_death_but_cannot_dec_provider() { + ExtBuilder::default() + .one_hundred_for_alice_n_bob() + .build() + .execute_with(|| { + assert_eq!(System::can_dec_provider(&ALICE), true); + assert_ok!(System::inc_consumers(&ALICE)); + assert_eq!(System::can_dec_provider(&ALICE), false); + assert!(TreasuryCurrencyAdapter::withdraw( + &ALICE, + 100, + WithdrawReasons::TRANSFER, + ExistenceRequirement::AllowDeath + ) + .is_err()); + assert_ok!(Tokens::deposit(BTC, &ALICE, 100)); + assert_eq!(System::can_dec_provider(&ALICE), true); + assert!(TreasuryCurrencyAdapter::withdraw( + &ALICE, + 100, + WithdrawReasons::TRANSFER, + ExistenceRequirement::AllowDeath + ) + .is_ok()); + }); +} + #[test] fn currency_adapter_burn_must_work() { ExtBuilder::default() -- GitLab