Skip to content
This repository was archived by the owner on Feb 21, 2024. It is now read-only.

Commit 4d4b535

Browse files
gui1117nazar-pc
authored andcommitted
Fix transaction payment fee/tip unbalanceds (paritytech#8860)
* fix and test * fmt
1 parent cdb697c commit 4d4b535

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

frame/transaction-payment/src/lib.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ mod tests {
609609
DispatchClass, DispatchInfo, PostDispatchInfo, GetDispatchInfo, Weight,
610610
WeightToFeePolynomial, WeightToFeeCoefficients, WeightToFeeCoefficient,
611611
},
612-
traits::Currency,
612+
traits::{Currency, OnUnbalanced, Imbalance},
613613
};
614614
use pallet_balances::Call as BalancesCall;
615615
use sp_core::H256;
@@ -718,8 +718,27 @@ mod tests {
718718
}
719719
}
720720

721+
thread_local! {
722+
static TIP_UNBALANCED_AMOUNT: RefCell<u64> = RefCell::new(0);
723+
static FEE_UNBALANCED_AMOUNT: RefCell<u64> = RefCell::new(0);
724+
}
725+
726+
pub struct DealWithFees;
727+
impl OnUnbalanced<pallet_balances::NegativeImbalance<Runtime>> for DealWithFees {
728+
fn on_unbalanceds<B>(
729+
mut fees_then_tips: impl Iterator<Item=pallet_balances::NegativeImbalance<Runtime>>
730+
) {
731+
if let Some(fees) = fees_then_tips.next() {
732+
FEE_UNBALANCED_AMOUNT.with(|a| *a.borrow_mut() += fees.peek());
733+
if let Some(tips) = fees_then_tips.next() {
734+
TIP_UNBALANCED_AMOUNT.with(|a| *a.borrow_mut() += tips.peek());
735+
}
736+
}
737+
}
738+
}
739+
721740
impl Config for Runtime {
722-
type OnChargeTransaction = CurrencyAdapter<Balances, ()>;
741+
type OnChargeTransaction = CurrencyAdapter<Balances, DealWithFees>;
723742
type TransactionByteFee = TransactionByteFee;
724743
type WeightToFee = WeightToFee;
725744
type FeeMultiplierUpdate = ();
@@ -832,6 +851,10 @@ mod tests {
832851
::post_dispatch(pre, &info_from_weight(5), &default_post_info(), len, &Ok(()))
833852
);
834853
assert_eq!(Balances::free_balance(1), 100 - 5 - 5 - 10);
854+
assert_eq!(FEE_UNBALANCED_AMOUNT.with(|a| a.borrow().clone()), 5 + 5 + 10);
855+
assert_eq!(TIP_UNBALANCED_AMOUNT.with(|a| a.borrow().clone()), 0);
856+
857+
FEE_UNBALANCED_AMOUNT.with(|a| *a.borrow_mut() = 0);
835858

836859
let pre = ChargeTransactionPayment::<Runtime>::from(5 /* tipped */)
837860
.pre_dispatch(&2, CALL, &info_from_weight(100), len)
@@ -843,6 +866,8 @@ mod tests {
843866
::post_dispatch(pre, &info_from_weight(100), &post_info_from_weight(50), len, &Ok(()))
844867
);
845868
assert_eq!(Balances::free_balance(2), 200 - 5 - 10 - 50 - 5);
869+
assert_eq!(FEE_UNBALANCED_AMOUNT.with(|a| a.borrow().clone()), 5 + 10 + 50);
870+
assert_eq!(TIP_UNBALANCED_AMOUNT.with(|a| a.borrow().clone()), 5);
846871
});
847872
}
848873

frame/transaction-payment/src/payment.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,15 @@ pub trait OnChargeTransaction<T: Config> {
5050
/// Implements the transaction payment for a module implementing the `Currency`
5151
/// trait (eg. the pallet_balances) using an unbalance handler (implementing
5252
/// `OnUnbalanced`).
53+
///
54+
/// The unbalance handler is given 2 unbalanceds in [`OnUnbalanced::on_unbalanceds`]: fee and
55+
/// then tip.
5356
pub struct CurrencyAdapter<C, OU>(PhantomData<(C, OU)>);
5457

5558
/// Default implementation for a Currency and an OnUnbalanced handler.
59+
///
60+
/// The unbalance handler is given 2 unbalanceds in [`OnUnbalanced::on_unbalanceds`]: fee and
61+
/// then tip.
5662
impl<T, C, OU> OnChargeTransaction<T> for CurrencyAdapter<C, OU>
5763
where
5864
T: Config,
@@ -97,7 +103,7 @@ where
97103
/// Since the predicted fee might have been too high, parts of the fee may
98104
/// be refunded.
99105
///
100-
/// Note: The `fee` already includes the `tip`.
106+
/// Note: The `corrected_fee` already includes the `tip`.
101107
fn correct_and_deposit_fee(
102108
who: &T::AccountId,
103109
_dispatch_info: &DispatchInfoOf<T::Call>,
@@ -120,8 +126,8 @@ where
120126
.same()
121127
.map_err(|_| TransactionValidityError::Invalid(InvalidTransaction::Payment))?;
122128
// Call someone else to handle the imbalance (fee and tip separately)
123-
let imbalances = adjusted_paid.split(tip);
124-
OU::on_unbalanceds(Some(imbalances.0).into_iter().chain(Some(imbalances.1)));
129+
let (tip, fee) = adjusted_paid.split(tip);
130+
OU::on_unbalanceds(Some(fee).into_iter().chain(Some(tip)));
125131
}
126132
Ok(())
127133
}

0 commit comments

Comments
 (0)