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

Commit 0e3001a

Browse files
authored
Tip payment is a different withdraw reason. (#3937)
* Tip payment is a different withdraw reason. * Bump runtime version. * Test fix. * Fix lock type
1 parent 5c505d1 commit 0e3001a

File tree

11 files changed

+49
-40
lines changed

11 files changed

+49
-40
lines changed

core/primitives/src/sr25519.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,6 @@ impl std::fmt::Display for Public {
129129
}
130130
}
131131

132-
#[cfg(not(feature = "std"))]
133-
use core as std;
134-
135132
impl rstd::fmt::Debug for Public {
136133
#[cfg(feature = "std")]
137134
fn fmt(&self, f: &mut rstd::fmt::Formatter) -> rstd::fmt::Result {

srml/balances/src/lib.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -824,13 +824,13 @@ where
824824
fn ensure_can_withdraw(
825825
who: &T::AccountId,
826826
_amount: T::Balance,
827-
reason: WithdrawReason,
827+
reasons: WithdrawReasons,
828828
new_balance: T::Balance,
829829
) -> Result {
830-
match reason {
831-
WithdrawReason::Reserve | WithdrawReason::Transfer if Self::vesting_balance(who) > new_balance =>
832-
return Err("vesting balance too high to send value"),
833-
_ => {}
830+
if reasons.intersects(WithdrawReason::Reserve | WithdrawReason::Transfer)
831+
&& Self::vesting_balance(who) > new_balance
832+
{
833+
return Err("vesting balance too high to send value");
834834
}
835835
let locks = Self::locks(who);
836836
if locks.is_empty() {
@@ -842,7 +842,7 @@ where
842842
.all(|l|
843843
now >= l.until
844844
|| new_balance >= l.amount
845-
|| !l.reasons.contains(reason)
845+
|| !l.reasons.intersects(reasons)
846846
)
847847
{
848848
Ok(())
@@ -868,7 +868,7 @@ where
868868
if would_create && value < T::ExistentialDeposit::get() {
869869
return Err("value too low to create account");
870870
}
871-
Self::ensure_can_withdraw(transactor, value, WithdrawReason::Transfer, new_from_balance)?;
871+
Self::ensure_can_withdraw(transactor, value, WithdrawReason::Transfer.into(), new_from_balance)?;
872872

873873
// NOTE: total stake being stored in the same type means that this could never overflow
874874
// but better to be safe than sorry.
@@ -893,7 +893,7 @@ where
893893
fn withdraw(
894894
who: &T::AccountId,
895895
value: Self::Balance,
896-
reason: WithdrawReason,
896+
reasons: WithdrawReasons,
897897
liveness: ExistenceRequirement,
898898
) -> result::Result<Self::NegativeImbalance, &'static str> {
899899
let old_balance = Self::free_balance(who);
@@ -907,7 +907,7 @@ where
907907
{
908908
return Err("payment would kill account")
909909
}
910-
Self::ensure_can_withdraw(who, value, reason, new_balance)?;
910+
Self::ensure_can_withdraw(who, value, reasons, new_balance)?;
911911
Self::set_free_balance(who, new_balance);
912912
Ok(NegativeImbalance::new(value))
913913
} else {
@@ -1014,7 +1014,7 @@ where
10141014
Self::free_balance(who)
10151015
.checked_sub(&value)
10161016
.map_or(false, |new_balance|
1017-
Self::ensure_can_withdraw(who, value, WithdrawReason::Reserve, new_balance).is_ok()
1017+
Self::ensure_can_withdraw(who, value, WithdrawReason::Reserve.into(), new_balance).is_ok()
10181018
)
10191019
}
10201020

@@ -1028,7 +1028,7 @@ where
10281028
return Err("not enough free funds")
10291029
}
10301030
let new_balance = b - value;
1031-
Self::ensure_can_withdraw(who, value, WithdrawReason::Reserve, new_balance)?;
1031+
Self::ensure_can_withdraw(who, value, WithdrawReason::Reserve.into(), new_balance)?;
10321032
Self::set_reserved_balance(who, Self::reserved_balance(who) + value);
10331033
Self::set_free_balance(who, new_balance);
10341034
Ok(())

srml/contracts/src/exec.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ fn transfer<'a, T: Trait, V: Vm<T>, L: Loader<T>>(
642642
if would_create && value < ctx.config.existential_deposit {
643643
return Err("value too low to create account");
644644
}
645-
T::Currency::ensure_can_withdraw(transactor, value, WithdrawReason::Transfer, new_from_balance)?;
645+
T::Currency::ensure_can_withdraw(transactor, value, WithdrawReason::Transfer.into(), new_from_balance)?;
646646

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

srml/contracts/src/gas.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ pub fn buy_gas<T: Trait>(
215215
let imbalance = T::Currency::withdraw(
216216
transactor,
217217
cost,
218-
WithdrawReason::Fee,
218+
WithdrawReason::Fee.into(),
219219
ExistenceRequirement::KeepAlive
220220
)?;
221221

srml/contracts/src/rent.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ fn try_evict_or_and_pay_rent<T: Trait>(
119119
let can_withdraw_rent = T::Currency::ensure_can_withdraw(
120120
account,
121121
dues_limited,
122-
WithdrawReason::Fee,
122+
WithdrawReason::Fee.into(),
123123
balance.saturating_sub(dues_limited),
124124
)
125125
.is_ok();
@@ -129,7 +129,7 @@ fn try_evict_or_and_pay_rent<T: Trait>(
129129
let imbalance = T::Currency::withdraw(
130130
account,
131131
dues_limited,
132-
WithdrawReason::Fee,
132+
WithdrawReason::Fee.into(),
133133
ExistenceRequirement::KeepAlive,
134134
)
135135
.expect(

srml/elections-phragmen/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ use srml_support::{
8383
decl_storage, decl_event, ensure, decl_module, dispatch,
8484
traits::{
8585
Currency, Get, LockableCurrency, LockIdentifier, ReservableCurrency, WithdrawReasons,
86-
ChangeMembers, OnUnbalanced,
86+
ChangeMembers, OnUnbalanced, WithdrawReason
8787
}
8888
};
8989
use system::{self, ensure_signed, ensure_root};
@@ -215,7 +215,7 @@ decl_module! {
215215
&who,
216216
locked_balance,
217217
T::BlockNumber::max_value(),
218-
WithdrawReasons::all(),
218+
WithdrawReasons::except(WithdrawReason::TransactionPayment),
219219
);
220220
<StakeOf<T>>::insert(&who, locked_balance);
221221
<VotesOf<T>>::insert(&who, votes);

srml/elections/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,7 @@ impl<T: Trait> Module<T> {
807807
let imbalance = T::Currency::withdraw(
808808
&who,
809809
T::VotingFee::get(),
810-
WithdrawReason::Fee,
810+
WithdrawReason::Fee.into(),
811811
ExistenceRequirement::KeepAlive,
812812
)?;
813813
T::BadVoterIndex::on_unbalanced(imbalance);

srml/generic-asset/src/lib.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@
128128
//! T::Currency::withdraw(
129129
//! transactor,
130130
//! amount,
131-
//! WithdrawReason::TransactionPayment,
131+
//! WithdrawReason::TransactionPayment.into(),
132132
//! ExistenceRequirement::KeepAlive,
133133
//! )?;
134134
//! // ...
@@ -563,11 +563,16 @@ impl<T: Trait> Module<T> {
563563

564564
/// Transfer some liquid free balance from one account to another.
565565
/// This will not emit the `Transferred` event.
566-
pub fn make_transfer(asset_id: &T::AssetId, from: &T::AccountId, to: &T::AccountId, amount: T::Balance) -> Result {
566+
pub fn make_transfer(
567+
asset_id: &T::AssetId,
568+
from: &T::AccountId,
569+
to: &T::AccountId,
570+
amount: T::Balance
571+
) -> Result {
567572
let new_balance = Self::free_balance(asset_id, from)
568573
.checked_sub(&amount)
569574
.ok_or_else(|| "balance too low to send amount")?;
570-
Self::ensure_can_withdraw(asset_id, from, amount, WithdrawReason::Transfer, new_balance)?;
575+
Self::ensure_can_withdraw(asset_id, from, amount, WithdrawReason::Transfer.into(), new_balance)?;
571576

572577
if from != to {
573578
<FreeBalance<T>>::mutate(asset_id, from, |balance| *balance -= amount);
@@ -734,7 +739,7 @@ impl<T: Trait> Module<T> {
734739
asset_id: &T::AssetId,
735740
who: &T::AccountId,
736741
_amount: T::Balance,
737-
reason: WithdrawReason,
742+
reasons: WithdrawReasons,
738743
new_balance: T::Balance,
739744
) -> Result {
740745
if asset_id != &Self::staking_asset_id() {
@@ -748,7 +753,7 @@ impl<T: Trait> Module<T> {
748753
let now = <system::Module<T>>::block_number();
749754
if Self::locks(who)
750755
.into_iter()
751-
.all(|l| now >= l.until || new_balance >= l.amount || !l.reasons.contains(reason))
756+
.all(|l| now >= l.until || new_balance >= l.amount || !l.reasons.intersects(reasons))
752757
{
753758
Ok(())
754759
} else {
@@ -1098,22 +1103,22 @@ where
10981103
fn ensure_can_withdraw(
10991104
who: &T::AccountId,
11001105
amount: Self::Balance,
1101-
reason: WithdrawReason,
1106+
reasons: WithdrawReasons,
11021107
new_balance: Self::Balance,
11031108
) -> Result {
1104-
<Module<T>>::ensure_can_withdraw(&U::asset_id(), who, amount, reason, new_balance)
1109+
<Module<T>>::ensure_can_withdraw(&U::asset_id(), who, amount, reasons, new_balance)
11051110
}
11061111

11071112
fn withdraw(
11081113
who: &T::AccountId,
11091114
value: Self::Balance,
1110-
reason: WithdrawReason,
1115+
reasons: WithdrawReasons,
11111116
_: ExistenceRequirement, // no existential deposit policy for generic asset
11121117
) -> result::Result<Self::NegativeImbalance, &'static str> {
11131118
let new_balance = Self::free_balance(who)
11141119
.checked_sub(&value)
11151120
.ok_or_else(|| "account has too few funds")?;
1116-
Self::ensure_can_withdraw(who, value, reason, new_balance)?;
1121+
Self::ensure_can_withdraw(who, value, reasons, new_balance)?;
11171122
<Module<T>>::set_free_balance(&U::asset_id(), who, new_balance);
11181123
Ok(NegativeImbalance::new(value))
11191124
}
@@ -1197,7 +1202,7 @@ where
11971202
.checked_sub(&value)
11981203
.map_or(false, |new_balance|
11991204
<Module<T>>::ensure_can_withdraw(
1200-
&U::asset_id(), who, value, WithdrawReason::Reserve, new_balance
1205+
&U::asset_id(), who, value, WithdrawReason::Reserve.into(), new_balance
12011206
).is_ok()
12021207
)
12031208
}

srml/support/src/traits.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ pub trait Currency<AccountId> {
381381
fn ensure_can_withdraw(
382382
who: &AccountId,
383383
_amount: Self::Balance,
384-
reason: WithdrawReason,
384+
reasons: WithdrawReasons,
385385
new_balance: Self::Balance,
386386
) -> result::Result<(), &'static str>;
387387

@@ -459,19 +459,19 @@ pub trait Currency<AccountId> {
459459
fn withdraw(
460460
who: &AccountId,
461461
value: Self::Balance,
462-
reason: WithdrawReason,
462+
reasons: WithdrawReasons,
463463
liveness: ExistenceRequirement,
464464
) -> result::Result<Self::NegativeImbalance, &'static str>;
465465

466466
/// Similar to withdraw, only accepts a `PositiveImbalance` and returns nothing on success.
467467
fn settle(
468468
who: &AccountId,
469469
value: Self::PositiveImbalance,
470-
reason: WithdrawReason,
470+
reasons: WithdrawReasons,
471471
liveness: ExistenceRequirement,
472472
) -> result::Result<(), Self::PositiveImbalance> {
473473
let v = value.peek();
474-
match Self::withdraw(who, v, reason, liveness) {
474+
match Self::withdraw(who, v, reasons, liveness) {
475475
Ok(opposite) => Ok(drop(value.offset(opposite))),
476476
_ => Err(value),
477477
}
@@ -614,6 +614,8 @@ bitmask! {
614614
Reserve = 0b00000100,
615615
/// In order to pay some other (higher-level) fees.
616616
Fee = 0b00001000,
617+
/// In order to tip a validator for transaction inclusion.
618+
Tip = 0b00010000,
617619
}
618620
}
619621

@@ -630,7 +632,7 @@ impl WithdrawReasons {
630632
/// # use srml_support::traits::{WithdrawReason, WithdrawReasons};
631633
/// # fn main() {
632634
/// assert_eq!(
633-
/// WithdrawReason::Fee | WithdrawReason::Transfer | WithdrawReason::Reserve,
635+
/// WithdrawReason::Fee | WithdrawReason::Transfer | WithdrawReason::Reserve | WithdrawReason::Tip,
634636
/// WithdrawReasons::except(WithdrawReason::TransactionPayment),
635637
/// );
636638
/// # }

srml/transaction-payment/src/lib.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,16 @@ impl<T: Trait + Send + Sync> SignedExtension for ChargeTransactionPayment<T>
171171
len: usize,
172172
) -> TransactionValidity {
173173
// pay any fees.
174-
let fee = Self::compute_fee(len, info, self.0);
174+
let tip = self.0;
175+
let fee = Self::compute_fee(len, info, tip);
175176
let imbalance = match T::Currency::withdraw(
176177
who,
177178
fee,
178-
WithdrawReason::TransactionPayment,
179+
if tip.is_zero() {
180+
WithdrawReason::TransactionPayment.into()
181+
} else {
182+
WithdrawReason::TransactionPayment | WithdrawReason::Tip
183+
},
179184
ExistenceRequirement::KeepAlive,
180185
) {
181186
Ok(imbalance) => imbalance,

0 commit comments

Comments
 (0)