Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Closed
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
15 changes: 8 additions & 7 deletions core/test-runtime/wasm/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
2 changes: 1 addition & 1 deletion node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
impl_name: create_runtime_str!("substrate-node"),
authoring_version: 10,
spec_version: 34,
impl_version: 35,
impl_version: 36,
apis: RUNTIME_API_VERSIONS,
};

Expand Down
15 changes: 8 additions & 7 deletions node/runtime/wasm/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
28 changes: 16 additions & 12 deletions srml/balances/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,11 @@ impl<T: Trait> Module<T> {
if would_create && value < Self::existential_deposit() {
return Err("value too low to create account");
}
Self::ensure_account_can_withdraw(transactor, value, WithdrawReason::Transfer, new_from_balance)?;
Self::ensure_account_can_withdraw(
transactor,
WithdrawReason::Transfer | WithdrawReason::TransactionPayment,
Copy link
Member

Choose a reason for hiding this comment

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

This is not a transaction payment. Transaction payments happen specifically for inclusion of a transaction in the chain.

new_from_balance,
)?;

// NOTE: total stake being stored in the same type means that this could never overflow
// but better to be safe than sorry.
Expand Down Expand Up @@ -370,26 +374,26 @@ impl<T: Trait> Module<T> {

/// Returns `Ok` iff the account is able to make a withdrawal of the given amount
/// for the given reason.
///
///
/// `Err(...)` with the reason why not otherwise.
pub fn ensure_account_can_withdraw(
who: &T::AccountId,
_amount: T::Balance,
reason: WithdrawReason,
reasons: impl Into<WithdrawReasons>,
Copy link
Contributor

Choose a reason for hiding this comment

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

I never encounter this syntax before. Can I know where I can read more about this syntax?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

in parameter as far as I know it is same as if I have a generic in function T: Into<WithdrawReasons> and then reasons: T I don't have that much material maybe try to google impl trait

Copy link
Member

Choose a reason for hiding this comment

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

Incorrect - there should be just one WithdrawReason for any single withdraw,

Copy link
Contributor

Choose a reason for hiding this comment

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

Let's say I want to create a runtime module that allow user to reserve and transfer in a single runtime call. How can do it? Call ensure_account_can_withdraw twice with different reasons before all the withdraws will not give the correct result.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

actually calling

ensure_account_can_withdraw(who, Reserve | Transfer, new_free_balance)

is equivalent to

ensure_account_can_withdraw(who, Transfer, new_free_balance)
ensure_account_can_withdraw(who, Reserve, new_free_balance)

new_balance: T::Balance,
) -> Result {
match reason {
WithdrawReason::Reserve | WithdrawReason::Transfer if Self::vesting_balance(who) > new_balance =>
return Err("vesting balance too high to send value"),
_ => {}
let reasons = reasons.into();
if reasons.intersects(WithdrawReason::Reserve | WithdrawReason::Transfer)
&& Self::vesting_balance(who) > new_balance
{
return Err("vesting balance too high to send value");
}
let locks = Self::locks(who);
if locks.is_empty() {
return Ok(())
}
let now = <system::Module<T>>::block_number();
if Self::locks(who).into_iter()
.all(|l| now >= l.until || new_balance >= l.amount || !l.reasons.contains(reason))
.all(|l| now >= l.until || new_balance >= l.amount || !l.reasons.intersects(reasons))
{
Ok(())
} else {
Expand All @@ -416,7 +420,7 @@ where
Self::free_balance(who)
.checked_sub(&value)
.map_or(false, |new_balance|
Self::ensure_account_can_withdraw(who, value, WithdrawReason::Reserve, new_balance).is_ok()
Self::ensure_account_can_withdraw(who, WithdrawReason::Reserve, new_balance).is_ok()
)
}

Expand Down Expand Up @@ -467,7 +471,7 @@ where
return Err("not enough free funds")
}
let new_balance = b - value;
Self::ensure_account_can_withdraw(who, value, WithdrawReason::Reserve, new_balance)?;
Self::ensure_account_can_withdraw(who, WithdrawReason::Reserve, new_balance)?;
Self::set_reserved_balance(who, Self::reserved_balance(who) + value);
Self::set_free_balance(who, new_balance);
Ok(())
Expand Down Expand Up @@ -602,7 +606,7 @@ impl<T: Trait> TransferAsset<T::AccountId> for Module<T> {
let b = Self::free_balance(who);
ensure!(b >= value, "account has too few funds");
let new_balance = b - value;
Self::ensure_account_can_withdraw(who, value, reason, new_balance)?;
Self::ensure_account_can_withdraw(who, reason, new_balance)?;
Self::set_free_balance(who, new_balance);
Self::decrease_total_stake_by(value);
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion srml/contract/src/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ fn transfer<'a, T: Trait, V: Vm<T>, L: Loader<T>>(
if would_create && value < ctx.config.existential_deposit {
return Err("value too low to create account");
}
<balances::Module<T>>::ensure_account_can_withdraw(transactor, value, WithdrawReason::Transfer, new_from_balance)?;
<balances::Module<T>>::ensure_account_can_withdraw(transactor, WithdrawReason::Transfer, new_from_balance)?;

let new_to_balance = match to_balance.checked_add(&value) {
Some(b) => b,
Expand Down