Skip to content

Account for reserved balance in ensure_can_withdraw function #8108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
11 changes: 11 additions & 0 deletions prdoc/pr_8108.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
title: "[pallet_balances] Account for reserved balance in `ensure_can_withdraw` function"

doc:
- audience: [Runtime Dev, Runtime User]
description: |-
The function `ensure_can_withdraw` now ensures the `new free balance` is higher
or equal to the `usable balance`.

crates:
- name: pallet-balances
bump: patch
20 changes: 18 additions & 2 deletions substrate/frame/balances/src/impl_currency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,13 +315,29 @@ where
fn ensure_can_withdraw(
who: &T::AccountId,
amount: T::Balance,
_reasons: WithdrawReasons,
reasons: WithdrawReasons,
new_balance: T::Balance,
) -> DispatchResult {
if amount.is_zero() {
return Ok(())
}
ensure!(new_balance >= Self::account(who).frozen, Error::<T, I>::LiquidityRestrictions);
let account = Self::account(who);

// Account for the new reserved amount only if withdrawing for a reserve.
let updated_reserved = if matches!(reasons, WithdrawReasons::RESERVE) {
account.reserved.saturating_add(amount)
} else {
account.reserved
};
Comment on lines +326 to +331
Copy link
Contributor

@gui1117 gui1117 Apr 23, 2025

Choose a reason for hiding this comment

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

I don't think this is a correct assumption of the withdraw reason API.

WithdrawReasons::RESERVE says:

In order to reserve some funds for a later return or repatriation.

But it doesn't say the reserved amount will be on the same account.

So I think in this function we shouldn't add the reserved amount.
Then in can_reserve and reserve we can be more precise so we can create a new function which does add the amount to the reserved amount.

Then after this I will approve the PR.


// Frozen balance applies to total. Anything on hold therefore gets discounted from the
// limit given by the freezes
let untouchable = account
.frozen
.saturating_sub(updated_reserved)
.max(T::ExistentialDeposit::get());

ensure!(new_balance >= untouchable, Error::<T, I>::LiquidityRestrictions);
Ok(())
}

Expand Down
47 changes: 46 additions & 1 deletion substrate/frame/balances/src/tests/currency_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,48 @@ fn lock_should_work_reserve() {
});
}

#[test]
fn reserve_should_work_for_usable_balance() {
frame_support::__private::sp_tracing::init_for_tests();
ExtBuilder::default()
.existential_deposit(1)
.monied(true)
.build_and_execute_with(|| {
// Check balances
let account = Balances::account(&1);
assert_eq!(account.free, 10);
assert_eq!(account.frozen, 0);
assert_eq!(account.reserved, 0);

Balances::set_lock(ID_1, &1, 9, WithdrawReasons::RESERVE);

let account = Balances::account(&1);
assert_eq!(account.free, 10);
assert_eq!(account.frozen, 9);
assert_eq!(account.reserved, 0);

assert_ok!(Balances::reserve(&1, 5));

let account = Balances::account(&1);
assert_eq!(account.free, 5);
assert_eq!(account.frozen, 9);
assert_eq!(account.reserved, 5);

assert_ok!(Balances::reserve(&1, 4));

let account = Balances::account(&1);
assert_eq!(account.free, 1);
assert_eq!(account.frozen, 9);
assert_eq!(account.reserved, 9);

// Check usable balance
// usable_balance = free - max(frozen - reserved, ExistentialDeposit)
// 0 = 1 - max(9 - 9, 1)
let usable_balance = Balances::usable_balance(&1);
assert_eq!(usable_balance, 0);
});
}

#[test]
fn lock_should_work_tx_fee() {
ExtBuilder::default()
Expand Down Expand Up @@ -754,7 +796,10 @@ fn existential_deposit_respected_when_reserving() {
assert_eq!(Balances::reserved_balance(1), 1);

// Cannot reserve any more of the free balance.
assert_noop!(Balances::reserve(&1, 1), DispatchError::ConsumerRemaining);
// free = 100
// reserved = 1
// existential deposit = 100
assert_noop!(Balances::reserve(&1, 1), Error::<Test>::LiquidityRestrictions);
});
}

Expand Down
2 changes: 1 addition & 1 deletion substrate/frame/staking/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ fn staking_should_work() {
}
);
// e.g. it cannot reserve more than 500 that it has free from the total 2000
assert_noop!(Balances::reserve(&3, 501), DispatchError::ConsumerRemaining);
assert_noop!(Balances::reserve(&3, 501), BalancesError::<Test, _>::LiquidityRestrictions);
assert_ok!(Balances::reserve(&3, 409));
});
}
Expand Down
Loading