Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Use can_hold instead of can_reserve in fn hold #12004

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions frame/balances/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1189,12 +1189,15 @@ impl<T: Config<I>, I: 'static> fungible::MutateHold<T::AccountId> for Pallet<T,
if amount.is_zero() {
return Ok(())
}
ensure!(Self::can_reserve(who, amount), Error::<T, I>::InsufficientBalance);
Self::mutate_account(who, |a| {
a.free -= amount;
a.reserved += amount;
})?;
Ok(())
ensure!(
<Self as fungible::InspectHold<T::AccountId>>::can_hold(who, amount),
Copy link
Contributor Author

@KiChjang KiChjang Sep 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, after writing unit tests, I found out that there is a difference between can_reserve and can_hold: the latter ensures that the reserved/held amount doesn't make the free balance go below the existential deposit. Should we still continue using can_hold here then? Or should we change can_hold to match the semantics of can_reserve?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we change can_hold what that mess up any existing code using can_hold / what are the tradeoffs?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want people to be able to use fungible traits instead of Currency in the future. See #8285 and #8453. We currently use Currency traits everywhere, and we'd like to change that.

Error::<T, I>::InsufficientBalance,
);
Self::try_mutate_account(who, |a, _| -> DispatchResult {
a.free = a.free.checked_sub(&amount).ok_or(Error::<T, I>::InsufficientBalance)?;
a.reserved = a.reserved.checked_add(&amount).ok_or(ArithmeticError::Overflow)?;
Ok(())
})
}
fn release(
who: &T::AccountId,
Expand Down
142 changes: 142 additions & 0 deletions frame/balances/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ macro_rules! decl_tests {
use frame_support::{
assert_noop, assert_storage_noop, assert_ok, assert_err,
traits::{
fungible::{self, BalancedHold, Inspect, InspectHold, MutateHold},
LockableCurrency, LockIdentifier, WithdrawReasons,
Currency, ReservableCurrency, ExistenceRequirement::AllowDeath
}
Expand Down Expand Up @@ -393,6 +394,23 @@ macro_rules! decl_tests {
});
}

#[test]
fn holding_fungible_should_work() {
<$ext_builder>::default().build().execute_with(|| {
let _ = <Balances as fungible::Mutate<_>>::mint_into(&1, 111);

assert_eq!(Balances::balance(&1), 111);
assert_eq!(Balances::reducible_balance(&1, false), 111);
assert_eq!(Balances::balance_on_hold(&1), 0);

assert_ok!(Balances::hold(&1, 69));

assert_eq!(Balances::balance(&1), 111);
assert_eq!(Balances::reducible_balance(&1, false), 42);
assert_eq!(Balances::balance_on_hold(&1), 69);
});
}

#[test]
fn balance_transfer_when_reserved_should_not_work() {
<$ext_builder>::default().build().execute_with(|| {
Expand All @@ -404,6 +422,18 @@ macro_rules! decl_tests {
);
});
}

#[test]
fn balance_transfer_when_held_should_not_work() {
<$ext_builder>::default().build().execute_with(|| {
let _ = <Balances as fungible::Mutate<_>>::mint_into(&1, 111);
assert_ok!(Balances::hold(&1, 69));
assert_noop!(
<Balances as fungible::Transfer<_>>::transfer(&1, &2, 69, false),
Error::<$test, _>::InsufficientBalance,
);
});
}

#[test]
fn deducting_balance_should_work() {
Expand Down Expand Up @@ -437,6 +467,18 @@ macro_rules! decl_tests {
});
}

#[test]
fn slashing_fungible_should_work() {
<$ext_builder>::default().build().execute_with(|| {
let _ = <Balances as fungible::Mutate<_>>::mint_into(&1, 111);
assert_ok!(Balances::hold(&1, 69));
assert_eq!(<Balances as fungible::Mutate<_>>::slash(&1, 69), Ok(42));
assert_eq!(Balances::reducible_balance(&1, false), 0);
assert_eq!(Balances::balance_on_hold(&1), 69);
assert_eq!(Balances::total_issuance(), 69);
});
}

#[test]
fn withdrawing_balance_should_work() {
<$ext_builder>::default().build().execute_with(|| {
Expand All @@ -462,6 +504,18 @@ macro_rules! decl_tests {
});
}

#[test]
fn slashing_incomplete_fungible_should_work() {
<$ext_builder>::default().build().execute_with(|| {
let _ = <Balances as fungible::Mutate<_>>::mint_into(&1, 42);
assert_ok!(Balances::hold(&1, 21));
assert_eq!(<Balances as fungible::Mutate<_>>::slash(&1, 69), Ok(21));
assert_eq!(Balances::reducible_balance(&1, false), 0);
assert_eq!(Balances::balance_on_hold(&1), 21);
assert_eq!(Balances::total_issuance(), 21);
});
}

#[test]
fn unreserving_balance_should_work() {
<$ext_builder>::default().build().execute_with(|| {
Expand All @@ -473,6 +527,17 @@ macro_rules! decl_tests {
});
}

#[test]
fn releasing_fungible_should_work() {
<$ext_builder>::default().build().execute_with(|| {
let _ = <Balances as fungible::Mutate<_>>::mint_into(&1, 111);
assert_ok!(Balances::hold(&1, 111));
assert_eq!(Balances::release(&1, 42, false), Ok(42));
assert_eq!(Balances::balance_on_hold(&1), 69);
assert_eq!(Balances::reducible_balance(&1, false), 42);
});
}

#[test]
fn slashing_reserved_balance_should_work() {
<$ext_builder>::default().build().execute_with(|| {
Expand All @@ -485,6 +550,18 @@ macro_rules! decl_tests {
});
}

#[test]
fn slashing_held_fungible_should_work() {
<$ext_builder>::default().build().execute_with(|| {
let _ = <Balances as fungible::Mutate<_>>::mint_into(&1, 111);
assert_ok!(Balances::hold(&1, 111));
assert_eq!(Balances::slash_held(&1, 42).1, 0);
assert_eq!(Balances::balance_on_hold(&1), 69);
assert_eq!(Balances::reducible_balance(&1, false), 0);
assert_eq!(Balances::total_issuance(), 69);
});
}

#[test]
fn slashing_incomplete_reserved_balance_should_work() {
<$ext_builder>::default().build().execute_with(|| {
Expand All @@ -497,6 +574,18 @@ macro_rules! decl_tests {
});
}

#[test]
fn slashing_incomplete_held_fungible_should_work() {
<$ext_builder>::default().build().execute_with(|| {
let _ = <Balances as fungible::Mutate<_>>::mint_into(&1, 111);
assert_ok!(Balances::hold(&1, 42));
assert_eq!(Balances::slash_held(&1, 69).1, 0);
assert_eq!(Balances::reducible_balance(&1, false), 69);
assert_eq!(Balances::balance_on_hold(&1), 0);
assert_eq!(Balances::total_issuance(), 69);
});
}

#[test]
fn repatriating_reserved_balance_should_work() {
<$ext_builder>::default().build().execute_with(|| {
Expand Down Expand Up @@ -528,6 +617,20 @@ macro_rules! decl_tests {
});
}

#[test]
fn transferring_held_fungible_should_work() {
<$ext_builder>::default().build().execute_with(|| {
let _ = <Balances as fungible::Mutate<_>>::mint_into(&1, 110);
let _ = <Balances as fungible::Mutate<_>>::mint_into(&2, 1);
assert_ok!(Balances::hold(&1, 110));
assert_ok!(Balances::transfer_held(&1, &2, 41, false, true), 41);
assert_eq!(Balances::balance_on_hold(&1), 69);
assert_eq!(Balances::reducible_balance(&1, false), 0);
assert_eq!(Balances::balance_on_hold(&2), 41);
assert_eq!(Balances::reducible_balance(&2, false), 1);
});
}

#[test]
fn transferring_reserved_balance_to_yourself_should_work() {
<$ext_builder>::default().build().execute_with(|| {
Expand All @@ -544,6 +647,22 @@ macro_rules! decl_tests {
});
}

#[test]
fn transferring_held_fungible_to_yourself_should_work() {
<$ext_builder>::default().build().execute_with(|| {
let _ = <Balances as fungible::Mutate<_>>::mint_into(&1, 110);
assert_ok!(Balances::hold(&1, 50));
assert_ok!(Balances::transfer_held(&1, &1, 50, true, false), 50);
assert_eq!(Balances::reducible_balance(&1, false), 110);
assert_eq!(Balances::balance_on_hold(&1), 0);

assert_ok!(Balances::hold(&1, 50));
assert_ok!(Balances::transfer_held(&1, &1, 60, true, false), 50);
assert_eq!(Balances::reducible_balance(&1, false), 110);
assert_eq!(Balances::balance_on_hold(&1), 0);
});
}

#[test]
fn transferring_reserved_balance_to_nonexistent_should_fail() {
<$ext_builder>::default().build().execute_with(|| {
Expand All @@ -553,6 +672,15 @@ macro_rules! decl_tests {
});
}

#[test]
fn transferring_held_fungible_to_nonexistent_should_fail() {
<$ext_builder>::default().build().execute_with(|| {
let _ = <Balances as fungible::Mutate<_>>::mint_into(&1, 111);
assert_ok!(Balances::hold(&1, 111));
assert_noop!(Balances::transfer_held(&1, &2, 42, false, false), Error::<$test, _>::DeadAccount);
});
}

#[test]
fn transferring_incomplete_reserved_balance_should_work() {
<$ext_builder>::default().build().execute_with(|| {
Expand All @@ -567,6 +695,20 @@ macro_rules! decl_tests {
});
}

#[test]
fn transferring_incomplete_held_fungible_should_work() {
<$ext_builder>::default().build().execute_with(|| {
let _ = <Balances as fungible::Mutate<_>>::mint_into(&1, 110);
let _ = <Balances as fungible::Mutate<_>>::mint_into(&2, 1);
assert_ok!(Balances::hold(&1, 41));
assert_ok!(Balances::transfer_held(&1, &2, 69, true, false), 41);
assert_eq!(Balances::balance_on_hold(&1), 0);
assert_eq!(Balances::reducible_balance(&1, false), 69);
assert_eq!(Balances::balance_on_hold(&2), 0);
assert_eq!(Balances::reducible_balance(&2, false), 42);
});
}

#[test]
fn transferring_too_high_value_should_not_panic() {
<$ext_builder>::default().build().execute_with(|| {
Expand Down