Skip to content
Snippets Groups Projects
Unverified Commit fb7075d4 authored by paritytech-cmd-bot-polkadot-sdk[bot]'s avatar paritytech-cmd-bot-polkadot-sdk[bot] Committed by GitHub
Browse files

[stable2407] Backport #6506 (#6657)


Backport #6506 into `stable2407` from Dinonard.

See the
[documentation](https://github.com/paritytech/polkadot-sdk/blob/master/docs/BACKPORT.md)
on how to use this bot.

<!--
  # To be used by other automation, do not modify:
  original-pr-number: #${pull_number}
-->

Co-authored-by: default avatarDino Pačandi <3002868+Dinonard@users.noreply.github.com>
Co-authored-by: default avatarEgor_P <egor@parity.io>
parent 22601b23
Branches
No related merge requests found
title: Zero refund check for FungibleAdapter
doc:
- audience: Runtime User
description: |-
`FungibleAdapter` will now check if the _refund amount_ is zero before calling deposit & emitting an event.
Fixes https://github.com/paritytech/polkadot-sdk/issues/6469.
crates:
- name: pallet-transaction-payment
bump: patch
...@@ -121,14 +121,15 @@ where ...@@ -121,14 +121,15 @@ where
if let Some(paid) = already_withdrawn { if let Some(paid) = already_withdrawn {
// Calculate how much refund we should return // Calculate how much refund we should return
let refund_amount = paid.peek().saturating_sub(corrected_fee); let refund_amount = paid.peek().saturating_sub(corrected_fee);
// refund to the the account that paid the fees if it exists. otherwise, don't refind // Refund to the the account that paid the fees if it exists & refund is non-zero.
// anything. // Otherwise, don't refund anything.
let refund_imbalance = if F::total_balance(who) > F::Balance::zero() { let refund_imbalance =
F::deposit(who, refund_amount, Precision::BestEffort) if refund_amount > Zero::zero() && F::total_balance(who) > F::Balance::zero() {
.unwrap_or_else(|_| Debt::<T::AccountId, F>::zero()) F::deposit(who, refund_amount, Precision::BestEffort)
} else { .unwrap_or_else(|_| Debt::<T::AccountId, F>::zero())
Debt::<T::AccountId, F>::zero() } else {
}; Debt::<T::AccountId, F>::zero()
};
// merge the imbalance caused by paying the fees and refunding parts of it again. // merge the imbalance caused by paying the fees and refunding parts of it again.
let adjusted_paid: Credit<T::AccountId, F> = paid let adjusted_paid: Credit<T::AccountId, F> = paid
.offset(refund_imbalance) .offset(refund_imbalance)
......
...@@ -841,3 +841,80 @@ fn genesis_default_works() { ...@@ -841,3 +841,80 @@ fn genesis_default_works() {
assert_eq!(<NextFeeMultiplier<Runtime>>::get(), Multiplier::saturating_from_integer(1)); assert_eq!(<NextFeeMultiplier<Runtime>>::get(), Multiplier::saturating_from_integer(1));
}); });
} }
<<<<<<< HEAD
=======
#[test]
fn no_fee_and_no_weight_for_other_origins() {
ExtBuilder::default().build().execute_with(|| {
let ext = Ext::from(0);
let mut info = CALL.get_dispatch_info();
info.extension_weight = ext.weight(CALL);
// Ensure we test the refund.
assert!(info.extension_weight != Weight::zero());
let len = CALL.encoded_size();
let origin = frame_system::RawOrigin::Root.into();
let (pre, origin) = ext.validate_and_prepare(origin, CALL, &info, len, 0).unwrap();
assert!(origin.as_system_ref().unwrap().is_root());
let pd_res = Ok(());
let mut post_info = frame_support::dispatch::PostDispatchInfo {
actual_weight: Some(info.total_weight()),
pays_fee: Default::default(),
};
<Ext as TransactionExtension<RuntimeCall>>::post_dispatch(
pre,
&info,
&mut post_info,
len,
&pd_res,
)
.unwrap();
assert_eq!(post_info.actual_weight, Some(info.call_weight));
})
}
#[test]
fn fungible_adapter_no_zero_refund_action() {
type FungibleAdapterT = payment::FungibleAdapter<Balances, DealWithFees>;
ExtBuilder::default().balance_factor(10).build().execute_with(|| {
System::set_block_number(10);
let dummy_acc = 1;
let (actual_fee, no_tip) = (10, 0);
let already_paid = <FungibleAdapterT as OnChargeTransaction<Runtime>>::withdraw_fee(
&dummy_acc,
CALL,
&CALL.get_dispatch_info(),
actual_fee,
no_tip,
).expect("Account must have enough funds.");
// Correction action with no expected side effect.
assert!(<FungibleAdapterT as OnChargeTransaction<Runtime>>::correct_and_deposit_fee(
&dummy_acc,
&CALL.get_dispatch_info(),
&default_post_info(),
actual_fee,
no_tip,
already_paid,
).is_ok());
// Ensure no zero amount deposit event is emitted.
let events = System::events();
assert!(!events
.iter()
.any(|record| matches!(record.event, RuntimeEvent::Balances(pallet_balances::Event::Deposit { amount, .. }) if amount.is_zero())),
"No zero amount deposit amount event should be emitted.",
);
});
}
>>>>>>> f520adb (Zero refund check for FungibleAdapter (#6506))
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment