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

Commit

Permalink
Add Event to Pallet Transaction Payment (paritytech#11618)
Browse files Browse the repository at this point in the history
* add Event to Pallet Transaction Payment

* Fix tests in Pallet Balance

* Fix tests in Pallet Balance/Executive/Asset-tx-payment.

* Fix

* fmt

* Fix

* Fix

* Update Cargo.lock

* Fix tests in executor

* Update frame/transaction-payment/src/lib.rs

Co-authored-by: joe petrowski <[email protected]>

* update the name of the event and fmt.

Co-authored-by: Shawn Tabrizi <[email protected]>
Co-authored-by: joe petrowski <[email protected]>
  • Loading branch information
3 people authored and DaviRain-Su committed Aug 23, 2022
1 parent 409439a commit 1df1e58
Show file tree
Hide file tree
Showing 14 changed files with 77 additions and 24 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions bin/node-template/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ impl pallet_balances::Config for Runtime {
}

impl pallet_transaction_payment::Config for Runtime {
type Event = Event;
type OnChargeTransaction = CurrencyAdapter<Balances, ()>;
type OperationalFeeMultiplier = ConstU8<5>;
type WeightToFee = IdentityFee<Balance>;
Expand Down
7 changes: 4 additions & 3 deletions bin/node/bench/src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,10 @@ impl core::Benchmark for ImportBenchmark {
.inspect_state(|| {
match self.block_type {
BlockType::RandomTransfersKeepAlive => {
// should be 7 per signed extrinsic + 1 per unsigned
// should be 8 per signed extrinsic + 1 per unsigned
// we have 1 unsigned and the rest are signed in the block
// those 7 events per signed are:
// those 8 events per signed are:
// - transaction paid for the transaction payment
// - withdraw (Balances::Withdraw) for charging the transaction fee
// - new account (System::NewAccount) as we always transfer fund to
// non-existent account
Expand All @@ -148,7 +149,7 @@ impl core::Benchmark for ImportBenchmark {
// - extrinsic success
assert_eq!(
node_runtime::System::events().len(),
(self.block.extrinsics.len() - 1) * 7 + 1,
(self.block.extrinsics.len() - 1) * 8 + 1,
);
},
BlockType::Noop => {
Expand Down
1 change: 1 addition & 0 deletions bin/node/executor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pallet-contracts = { version = "4.0.0-dev", path = "../../../frame/contracts" }
pallet-im-online = { version = "4.0.0-dev", path = "../../../frame/im-online" }
pallet-timestamp = { version = "4.0.0-dev", path = "../../../frame/timestamp" }
pallet-treasury = { version = "4.0.0-dev", path = "../../../frame/treasury" }
pallet-transaction-payment = { version = "4.0.0-dev", path = "../../../frame/transaction-payment" }
sp-application-crypto = { version = "6.0.0", path = "../../../primitives/application-crypto" }
sp-consensus-babe = { version = "0.10.0-dev", path = "../../../primitives/consensus/babe" }
sp-externalities = { version = "0.12.0", path = "../../../primitives/externalities" }
Expand Down
33 changes: 33 additions & 0 deletions bin/node/executor/tests/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,17 @@ fn full_native_block_import_works() {
event: Event::Treasury(pallet_treasury::Event::Deposit { value: fees * 8 / 10 }),
topics: vec![],
},
EventRecord {
phase: Phase::ApplyExtrinsic(1),
event: Event::TransactionPayment(
pallet_transaction_payment::Event::TransactionFeePaid {
who: alice().into(),
actual_fee: fees,
tip: 0,
},
),
topics: vec![],
},
EventRecord {
phase: Phase::ApplyExtrinsic(1),
event: Event::System(frame_system::Event::ExtrinsicSuccess {
Expand Down Expand Up @@ -488,6 +499,17 @@ fn full_native_block_import_works() {
event: Event::Treasury(pallet_treasury::Event::Deposit { value: fees * 8 / 10 }),
topics: vec![],
},
EventRecord {
phase: Phase::ApplyExtrinsic(1),
event: Event::TransactionPayment(
pallet_transaction_payment::Event::TransactionFeePaid {
who: bob().into(),
actual_fee: fees,
tip: 0,
},
),
topics: vec![],
},
EventRecord {
phase: Phase::ApplyExtrinsic(1),
event: Event::System(frame_system::Event::ExtrinsicSuccess {
Expand Down Expand Up @@ -525,6 +547,17 @@ fn full_native_block_import_works() {
event: Event::Treasury(pallet_treasury::Event::Deposit { value: fees * 8 / 10 }),
topics: vec![],
},
EventRecord {
phase: Phase::ApplyExtrinsic(2),
event: Event::TransactionPayment(
pallet_transaction_payment::Event::TransactionFeePaid {
who: alice().into(),
actual_fee: fees,
tip: 0,
},
),
topics: vec![],
},
EventRecord {
phase: Phase::ApplyExtrinsic(2),
event: Event::System(frame_system::Event::ExtrinsicSuccess {
Expand Down
1 change: 1 addition & 0 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,7 @@ parameter_types! {
}

impl pallet_transaction_payment::Config for Runtime {
type Event = Event;
type OnChargeTransaction = CurrencyAdapter<Balances, DealWithFees>;
type OperationalFeeMultiplier = OperationalFeeMultiplier;
type WeightToFee = IdentityFee<Balance>;
Expand Down
5 changes: 3 additions & 2 deletions client/db/src/parity_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ impl<H: Clone + AsRef<[u8]>> Database<H> for DbAdapter {
}
return None
},
Change::Reference(col, key) =>
Change::Reference(col, key) => {
if ref_counted_column(col) {
// FIXME accessing value is not strictly needed, optimize this in parity-db.
let value = <Self as Database<H>>::get(self, col, key.as_ref());
Expand All @@ -116,7 +116,8 @@ impl<H: Clone + AsRef<[u8]>> Database<H> for DbAdapter {
not_ref_counted_column.push(col);
}
return None
},
}
},
Change::Release(col, key) =>
if ref_counted_column(col) {
(col as u8, key.as_ref().to_vec(), None)
Expand Down
1 change: 1 addition & 0 deletions frame/balances/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ mod tests;
mod benchmarking;
mod tests_composite;
mod tests_local;
#[cfg(test)]
mod tests_reentrancy;
pub mod weights;

Expand Down
3 changes: 2 additions & 1 deletion frame/balances/src/tests_composite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ frame_support::construct_runtime!(
{
System: frame_system::{Pallet, Call, Config, Storage, Event<T>},
Balances: pallet_balances::{Pallet, Call, Storage, Config<T>, Event<T>},
TransactionPayment: pallet_transaction_payment::{Pallet, Storage},
TransactionPayment: pallet_transaction_payment::{Pallet, Storage, Event<T>},
}
);

Expand Down Expand Up @@ -77,6 +77,7 @@ impl frame_system::Config for Test {
}

impl pallet_transaction_payment::Config for Test {
type Event = Event;
type OnChargeTransaction = CurrencyAdapter<Pallet<Test>, ()>;
type OperationalFeeMultiplier = ConstU8<5>;
type WeightToFee = IdentityFee<u64>;
Expand Down
3 changes: 2 additions & 1 deletion frame/balances/src/tests_local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ frame_support::construct_runtime!(
{
System: frame_system::{Pallet, Call, Config, Storage, Event<T>},
Balances: pallet_balances::{Pallet, Call, Storage, Config<T>, Event<T>},
TransactionPayment: pallet_transaction_payment::{Pallet, Storage},
TransactionPayment: pallet_transaction_payment::{Pallet, Storage, Event<T>},
}
);

Expand Down Expand Up @@ -78,6 +78,7 @@ impl frame_system::Config for Test {
}

impl pallet_transaction_payment::Config for Test {
type Event = Event;
type OnChargeTransaction = CurrencyAdapter<Pallet<Test>, ()>;
type OperationalFeeMultiplier = ConstU8<5>;
type WeightToFee = IdentityFee<u64>;
Expand Down
14 changes: 2 additions & 12 deletions frame/balances/src/tests_reentrancy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,11 @@
#![cfg(test)]

use crate::{self as pallet_balances, Config, Pallet};
use crate::{self as pallet_balances, Config};
use frame_support::{
parameter_types,
traits::{ConstU32, ConstU64, ConstU8, StorageMapShim},
weights::IdentityFee,
traits::{ConstU32, ConstU64, StorageMapShim},
};
use pallet_transaction_payment::CurrencyAdapter;
use sp_core::H256;
use sp_io;
use sp_runtime::{testing::Header, traits::IdentityLookup};
Expand Down Expand Up @@ -83,14 +81,6 @@ impl frame_system::Config for Test {
type MaxConsumers = frame_support::traits::ConstU32<16>;
}

impl pallet_transaction_payment::Config for Test {
type OnChargeTransaction = CurrencyAdapter<Pallet<Test>, ()>;
type OperationalFeeMultiplier = ConstU8<5>;
type WeightToFee = IdentityFee<u64>;
type LengthToFee = IdentityFee<u64>;
type FeeMultiplierUpdate = ();
}

pub struct OnDustRemoval;
impl OnUnbalanced<NegativeImbalance<Test>> for OnDustRemoval {
fn on_nonzero_unbalanced(amount: NegativeImbalance<Test>) {
Expand Down
3 changes: 2 additions & 1 deletion frame/executive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,7 @@ mod tests {
{
System: frame_system::{Pallet, Call, Config, Storage, Event<T>},
Balances: pallet_balances::{Pallet, Call, Storage, Config<T>, Event<T>},
TransactionPayment: pallet_transaction_payment::{Pallet, Storage},
TransactionPayment: pallet_transaction_payment::{Pallet, Storage, Event<T>},
Custom: custom::{Pallet, Call, ValidateUnsigned, Inherent},
}
);
Expand Down Expand Up @@ -783,6 +783,7 @@ mod tests {
pub const TransactionByteFee: Balance = 0;
}
impl pallet_transaction_payment::Config for Runtime {
type Event = Event;
type OnChargeTransaction = CurrencyAdapter<Balances, ()>;
type OperationalFeeMultiplier = ConstU8<5>;
type WeightToFee = IdentityFee<Balance>;
Expand Down
3 changes: 2 additions & 1 deletion frame/transaction-payment/asset-tx-payment/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ frame_support::construct_runtime!(
{
System: system::{Pallet, Call, Config, Storage, Event<T>},
Balances: pallet_balances::{Pallet, Call, Storage, Config<T>, Event<T>},
TransactionPayment: pallet_transaction_payment::{Pallet, Storage},
TransactionPayment: pallet_transaction_payment::{Pallet, Storage, Event<T>},
Assets: pallet_assets::{Pallet, Call, Storage, Event<T>},
Authorship: pallet_authorship::{Pallet, Call, Storage},
AssetTxPayment: pallet_asset_tx_payment::{Pallet},
Expand Down Expand Up @@ -143,6 +143,7 @@ impl WeightToFeeT for TransactionByteFee {
}

impl pallet_transaction_payment::Config for Runtime {
type Event = Event;
type OnChargeTransaction = CurrencyAdapter<Balances, ()>;
type WeightToFee = WeightToFee;
type LengthToFee = TransactionByteFee;
Expand Down
25 changes: 22 additions & 3 deletions frame/transaction-payment/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,9 @@ pub mod pallet {

#[pallet::config]
pub trait Config: frame_system::Config {
/// The overarching event type.
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;

/// Handler for withdrawing, refunding and depositing the transaction fee.
/// Transaction fees are withdrawn before the transaction is executed.
/// After the transaction was executed the transaction weight can be
Expand Down Expand Up @@ -321,6 +324,14 @@ pub mod pallet {
}
}

#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
/// A transaction fee `actual_fee`, of which `tip` was added to the minimum inclusion fee,
/// has been paid by `who`.
TransactionFeePaid { who: T::AccountId, actual_fee: BalanceOf<T>, tip: BalanceOf<T> },
}

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_finalize(_: T::BlockNumber) {
Expand Down Expand Up @@ -734,6 +745,7 @@ where
T::OnChargeTransaction::correct_and_deposit_fee(
&who, info, post_info, actual_fee, tip, imbalance,
)?;
Pallet::<T>::deposit_event(Event::<T>::TransactionFeePaid { who, actual_fee, tip });
}
Ok(())
}
Expand Down Expand Up @@ -790,7 +802,7 @@ mod tests {
{
System: system::{Pallet, Call, Config, Storage, Event<T>},
Balances: pallet_balances::{Pallet, Call, Storage, Config<T>, Event<T>},
TransactionPayment: pallet_transaction_payment::{Pallet, Storage},
TransactionPayment: pallet_transaction_payment::{Pallet, Storage, Event<T>},
}
);

Expand Down Expand Up @@ -899,6 +911,7 @@ mod tests {
}

impl Config for Runtime {
type Event = Event;
type OnChargeTransaction = CurrencyAdapter<Balances, DealWithFees>;
type OperationalFeeMultiplier = OperationalFeeMultiplier;
type WeightToFee = WeightToFee;
Expand Down Expand Up @@ -1407,8 +1420,14 @@ mod tests {
&Ok(())
));
assert_eq!(Balances::total_balance(&user), 0);
// No events for such a scenario
assert_eq!(System::events().len(), 0);
// TransactionFeePaid Event
System::assert_has_event(Event::TransactionPayment(
pallet_transaction_payment::Event::TransactionFeePaid {
who: user,
actual_fee: 0,
tip: 0,
},
));
});
}

Expand Down

0 comments on commit 1df1e58

Please sign in to comment.