Add Event to Pallet Transaction Payment (#11618)

* 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 <25483142+joepetrowski@users.noreply.github.com>

* update the name of the event and fmt.

Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>
Co-authored-by: joe petrowski <25483142+joepetrowski@users.noreply.github.com>
This commit is contained in:
ZhiYong
2022-06-16 09:30:49 +08:00
committed by GitHub
parent b71e180446
commit e252c274c1
14 changed files with 77 additions and 24 deletions
+1
View File
@@ -4664,6 +4664,7 @@ dependencies = [
"pallet-contracts", "pallet-contracts",
"pallet-im-online", "pallet-im-online",
"pallet-timestamp", "pallet-timestamp",
"pallet-transaction-payment",
"pallet-treasury", "pallet-treasury",
"parity-scale-codec", "parity-scale-codec",
"sc-executor", "sc-executor",
@@ -249,6 +249,7 @@ impl pallet_balances::Config for Runtime {
} }
impl pallet_transaction_payment::Config for Runtime { impl pallet_transaction_payment::Config for Runtime {
type Event = Event;
type OnChargeTransaction = CurrencyAdapter<Balances, ()>; type OnChargeTransaction = CurrencyAdapter<Balances, ()>;
type OperationalFeeMultiplier = ConstU8<5>; type OperationalFeeMultiplier = ConstU8<5>;
type WeightToFee = IdentityFee<Balance>; type WeightToFee = IdentityFee<Balance>;
+4 -3
View File
@@ -135,9 +135,10 @@ impl core::Benchmark for ImportBenchmark {
.inspect_state(|| { .inspect_state(|| {
match self.block_type { match self.block_type {
BlockType::RandomTransfersKeepAlive => { 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 // 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 // - withdraw (Balances::Withdraw) for charging the transaction fee
// - new account (System::NewAccount) as we always transfer fund to // - new account (System::NewAccount) as we always transfer fund to
// non-existent account // non-existent account
@@ -148,7 +149,7 @@ impl core::Benchmark for ImportBenchmark {
// - extrinsic success // - extrinsic success
assert_eq!( assert_eq!(
node_runtime::System::events().len(), node_runtime::System::events().len(),
(self.block.extrinsics.len() - 1) * 7 + 1, (self.block.extrinsics.len() - 1) * 8 + 1,
); );
}, },
BlockType::Noop => { BlockType::Noop => {
+1
View File
@@ -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-im-online = { version = "4.0.0-dev", path = "../../../frame/im-online" }
pallet-timestamp = { version = "4.0.0-dev", path = "../../../frame/timestamp" } pallet-timestamp = { version = "4.0.0-dev", path = "../../../frame/timestamp" }
pallet-treasury = { version = "4.0.0-dev", path = "../../../frame/treasury" } 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-application-crypto = { version = "6.0.0", path = "../../../primitives/application-crypto" }
sp-consensus-babe = { version = "0.10.0-dev", path = "../../../primitives/consensus/babe" } sp-consensus-babe = { version = "0.10.0-dev", path = "../../../primitives/consensus/babe" }
sp-externalities = { version = "0.12.0", path = "../../../primitives/externalities" } sp-externalities = { version = "0.12.0", path = "../../../primitives/externalities" }
@@ -417,6 +417,17 @@ fn full_native_block_import_works() {
event: Event::Treasury(pallet_treasury::Event::Deposit { value: fees * 8 / 10 }), event: Event::Treasury(pallet_treasury::Event::Deposit { value: fees * 8 / 10 }),
topics: vec![], 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 { EventRecord {
phase: Phase::ApplyExtrinsic(1), phase: Phase::ApplyExtrinsic(1),
event: Event::System(frame_system::Event::ExtrinsicSuccess { event: Event::System(frame_system::Event::ExtrinsicSuccess {
@@ -488,6 +499,17 @@ fn full_native_block_import_works() {
event: Event::Treasury(pallet_treasury::Event::Deposit { value: fees * 8 / 10 }), event: Event::Treasury(pallet_treasury::Event::Deposit { value: fees * 8 / 10 }),
topics: vec![], 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 { EventRecord {
phase: Phase::ApplyExtrinsic(1), phase: Phase::ApplyExtrinsic(1),
event: Event::System(frame_system::Event::ExtrinsicSuccess { event: Event::System(frame_system::Event::ExtrinsicSuccess {
@@ -525,6 +547,17 @@ fn full_native_block_import_works() {
event: Event::Treasury(pallet_treasury::Event::Deposit { value: fees * 8 / 10 }), event: Event::Treasury(pallet_treasury::Event::Deposit { value: fees * 8 / 10 }),
topics: vec![], 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 { EventRecord {
phase: Phase::ApplyExtrinsic(2), phase: Phase::ApplyExtrinsic(2),
event: Event::System(frame_system::Event::ExtrinsicSuccess { event: Event::System(frame_system::Event::ExtrinsicSuccess {
+1
View File
@@ -447,6 +447,7 @@ parameter_types! {
} }
impl pallet_transaction_payment::Config for Runtime { impl pallet_transaction_payment::Config for Runtime {
type Event = Event;
type OnChargeTransaction = CurrencyAdapter<Balances, DealWithFees>; type OnChargeTransaction = CurrencyAdapter<Balances, DealWithFees>;
type OperationalFeeMultiplier = OperationalFeeMultiplier; type OperationalFeeMultiplier = OperationalFeeMultiplier;
type WeightToFee = IdentityFee<Balance>; type WeightToFee = IdentityFee<Balance>;
+3 -2
View File
@@ -106,7 +106,7 @@ impl<H: Clone + AsRef<[u8]>> Database<H> for DbAdapter {
} }
return None return None
}, },
Change::Reference(col, key) => Change::Reference(col, key) => {
if ref_counted_column(col) { if ref_counted_column(col) {
// FIXME accessing value is not strictly needed, optimize this in parity-db. // FIXME accessing value is not strictly needed, optimize this in parity-db.
let value = <Self as Database<H>>::get(self, col, key.as_ref()); let value = <Self as Database<H>>::get(self, col, key.as_ref());
@@ -116,7 +116,8 @@ impl<H: Clone + AsRef<[u8]>> Database<H> for DbAdapter {
not_ref_counted_column.push(col); not_ref_counted_column.push(col);
} }
return None return None
}, }
},
Change::Release(col, key) => Change::Release(col, key) =>
if ref_counted_column(col) { if ref_counted_column(col) {
(col as u8, key.as_ref().to_vec(), None) (col as u8, key.as_ref().to_vec(), None)
+1
View File
@@ -158,6 +158,7 @@ mod tests;
mod benchmarking; mod benchmarking;
mod tests_composite; mod tests_composite;
mod tests_local; mod tests_local;
#[cfg(test)]
mod tests_reentrancy; mod tests_reentrancy;
pub mod weights; pub mod weights;
@@ -40,7 +40,7 @@ frame_support::construct_runtime!(
{ {
System: frame_system::{Pallet, Call, Config, Storage, Event<T>}, System: frame_system::{Pallet, Call, Config, Storage, Event<T>},
Balances: pallet_balances::{Pallet, Call, Storage, Config<T>, 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>},
} }
); );
@@ -77,6 +77,7 @@ impl frame_system::Config for Test {
} }
impl pallet_transaction_payment::Config for Test { impl pallet_transaction_payment::Config for Test {
type Event = Event;
type OnChargeTransaction = CurrencyAdapter<Pallet<Test>, ()>; type OnChargeTransaction = CurrencyAdapter<Pallet<Test>, ()>;
type OperationalFeeMultiplier = ConstU8<5>; type OperationalFeeMultiplier = ConstU8<5>;
type WeightToFee = IdentityFee<u64>; type WeightToFee = IdentityFee<u64>;
+2 -1
View File
@@ -41,7 +41,7 @@ frame_support::construct_runtime!(
{ {
System: frame_system::{Pallet, Call, Config, Storage, Event<T>}, System: frame_system::{Pallet, Call, Config, Storage, Event<T>},
Balances: pallet_balances::{Pallet, Call, Storage, Config<T>, 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>},
} }
); );
@@ -78,6 +78,7 @@ impl frame_system::Config for Test {
} }
impl pallet_transaction_payment::Config for Test { impl pallet_transaction_payment::Config for Test {
type Event = Event;
type OnChargeTransaction = CurrencyAdapter<Pallet<Test>, ()>; type OnChargeTransaction = CurrencyAdapter<Pallet<Test>, ()>;
type OperationalFeeMultiplier = ConstU8<5>; type OperationalFeeMultiplier = ConstU8<5>;
type WeightToFee = IdentityFee<u64>; type WeightToFee = IdentityFee<u64>;
@@ -19,13 +19,11 @@
#![cfg(test)] #![cfg(test)]
use crate::{self as pallet_balances, Config, Pallet}; use crate::{self as pallet_balances, Config};
use frame_support::{ use frame_support::{
parameter_types, parameter_types,
traits::{ConstU32, ConstU64, ConstU8, StorageMapShim}, traits::{ConstU32, ConstU64, StorageMapShim},
weights::IdentityFee,
}; };
use pallet_transaction_payment::CurrencyAdapter;
use sp_core::H256; use sp_core::H256;
use sp_io; use sp_io;
use sp_runtime::{testing::Header, traits::IdentityLookup}; use sp_runtime::{testing::Header, traits::IdentityLookup};
@@ -83,14 +81,6 @@ impl frame_system::Config for Test {
type MaxConsumers = frame_support::traits::ConstU32<16>; 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; pub struct OnDustRemoval;
impl OnUnbalanced<NegativeImbalance<Test>> for OnDustRemoval { impl OnUnbalanced<NegativeImbalance<Test>> for OnDustRemoval {
fn on_nonzero_unbalanced(amount: NegativeImbalance<Test>) { fn on_nonzero_unbalanced(amount: NegativeImbalance<Test>) {
+2 -1
View File
@@ -722,7 +722,7 @@ mod tests {
{ {
System: frame_system::{Pallet, Call, Config, Storage, Event<T>}, System: frame_system::{Pallet, Call, Config, Storage, Event<T>},
Balances: pallet_balances::{Pallet, Call, Storage, Config<T>, 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}, Custom: custom::{Pallet, Call, ValidateUnsigned, Inherent},
} }
); );
@@ -783,6 +783,7 @@ mod tests {
pub const TransactionByteFee: Balance = 0; pub const TransactionByteFee: Balance = 0;
} }
impl pallet_transaction_payment::Config for Runtime { impl pallet_transaction_payment::Config for Runtime {
type Event = Event;
type OnChargeTransaction = CurrencyAdapter<Balances, ()>; type OnChargeTransaction = CurrencyAdapter<Balances, ()>;
type OperationalFeeMultiplier = ConstU8<5>; type OperationalFeeMultiplier = ConstU8<5>;
type WeightToFee = IdentityFee<Balance>; type WeightToFee = IdentityFee<Balance>;
@@ -48,7 +48,7 @@ frame_support::construct_runtime!(
{ {
System: system::{Pallet, Call, Config, Storage, Event<T>}, System: system::{Pallet, Call, Config, Storage, Event<T>},
Balances: pallet_balances::{Pallet, Call, Storage, Config<T>, 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>}, Assets: pallet_assets::{Pallet, Call, Storage, Event<T>},
Authorship: pallet_authorship::{Pallet, Call, Storage}, Authorship: pallet_authorship::{Pallet, Call, Storage},
AssetTxPayment: pallet_asset_tx_payment::{Pallet}, AssetTxPayment: pallet_asset_tx_payment::{Pallet},
@@ -143,6 +143,7 @@ impl WeightToFeeT for TransactionByteFee {
} }
impl pallet_transaction_payment::Config for Runtime { impl pallet_transaction_payment::Config for Runtime {
type Event = Event;
type OnChargeTransaction = CurrencyAdapter<Balances, ()>; type OnChargeTransaction = CurrencyAdapter<Balances, ()>;
type WeightToFee = WeightToFee; type WeightToFee = WeightToFee;
type LengthToFee = TransactionByteFee; type LengthToFee = TransactionByteFee;
+22 -3
View File
@@ -249,6 +249,9 @@ pub mod pallet {
#[pallet::config] #[pallet::config]
pub trait Config: frame_system::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. /// Handler for withdrawing, refunding and depositing the transaction fee.
/// Transaction fees are withdrawn before the transaction is executed. /// Transaction fees are withdrawn before the transaction is executed.
/// After the transaction was executed the transaction weight can be /// After the transaction was executed the transaction weight can be
@@ -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] #[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> { impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_finalize(_: T::BlockNumber) { fn on_finalize(_: T::BlockNumber) {
@@ -734,6 +745,7 @@ where
T::OnChargeTransaction::correct_and_deposit_fee( T::OnChargeTransaction::correct_and_deposit_fee(
&who, info, post_info, actual_fee, tip, imbalance, &who, info, post_info, actual_fee, tip, imbalance,
)?; )?;
Pallet::<T>::deposit_event(Event::<T>::TransactionFeePaid { who, actual_fee, tip });
} }
Ok(()) Ok(())
} }
@@ -790,7 +802,7 @@ mod tests {
{ {
System: system::{Pallet, Call, Config, Storage, Event<T>}, System: system::{Pallet, Call, Config, Storage, Event<T>},
Balances: pallet_balances::{Pallet, Call, Storage, Config<T>, 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>},
} }
); );
@@ -899,6 +911,7 @@ mod tests {
} }
impl Config for Runtime { impl Config for Runtime {
type Event = Event;
type OnChargeTransaction = CurrencyAdapter<Balances, DealWithFees>; type OnChargeTransaction = CurrencyAdapter<Balances, DealWithFees>;
type OperationalFeeMultiplier = OperationalFeeMultiplier; type OperationalFeeMultiplier = OperationalFeeMultiplier;
type WeightToFee = WeightToFee; type WeightToFee = WeightToFee;
@@ -1407,8 +1420,14 @@ mod tests {
&Ok(()) &Ok(())
)); ));
assert_eq!(Balances::total_balance(&user), 0); assert_eq!(Balances::total_balance(&user), 0);
// No events for such a scenario // TransactionFeePaid Event
assert_eq!(System::events().len(), 0); System::assert_has_event(Event::TransactionPayment(
pallet_transaction_payment::Event::TransactionFeePaid {
who: user,
actual_fee: 0,
tip: 0,
},
));
}); });
} }