From 6c4763baff3d8179676a3c1660fe7063fd56a8ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20Thei=C3=9Fen?= Date: Fri, 15 May 2020 13:12:12 +0200 Subject: [PATCH 1/7] Emit a PaymentParameters event once per block This contains per-block paramaters need to calculate fees off-chain. --- Cargo.lock | 1 + bin/node-template/runtime/src/lib.rs | 3 ++- bin/node/executor/Cargo.toml | 1 + bin/node/executor/tests/basic.rs | 27 +++++++++++++++++++++++++-- bin/node/runtime/src/lib.rs | 3 ++- frame/balances/src/tests_composite.rs | 1 + frame/balances/src/tests_local.rs | 1 + frame/contracts/src/tests.rs | 3 +++ frame/executive/src/lib.rs | 3 +++ frame/transaction-payment/src/lib.rs | 23 +++++++++++++++++++++-- 10 files changed, 60 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 336bc936f873a..951adbb3feeb8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3491,6 +3491,7 @@ dependencies = [ name = "node-executor" version = "2.0.0-dev" dependencies = [ + "assert_matches", "criterion 0.3.1", "frame-benchmarking", "frame-support", diff --git a/bin/node-template/runtime/src/lib.rs b/bin/node-template/runtime/src/lib.rs index b1b73f3b49fee..9981f8b36f6fb 100644 --- a/bin/node-template/runtime/src/lib.rs +++ b/bin/node-template/runtime/src/lib.rs @@ -233,6 +233,7 @@ parameter_types! { } impl transaction_payment::Trait for Runtime { + type Event = Event; type Currency = balances::Module; type OnTransactionPayment = (); type TransactionByteFee = TransactionByteFee; @@ -262,7 +263,7 @@ construct_runtime!( Aura: aura::{Module, Config, Inherent(Timestamp)}, Grandpa: grandpa::{Module, Call, Storage, Config, Event}, Balances: balances::{Module, Call, Storage, Config, Event}, - TransactionPayment: transaction_payment::{Module, Storage}, + TransactionPayment: transaction_payment::{Module, Storage, Event}, Sudo: sudo::{Module, Call, Config, Storage, Event}, // Used for the module template in `./template.rs` TemplateModule: template::{Module, Call, Storage, Event}, diff --git a/bin/node/executor/Cargo.toml b/bin/node/executor/Cargo.toml index 014ac9a4c8474..04c5e9c6a50ee 100644 --- a/bin/node/executor/Cargo.toml +++ b/bin/node/executor/Cargo.toml @@ -24,6 +24,7 @@ trie-root = "0.16.0" frame-benchmarking = { version = "2.0.0-dev", path = "../../../frame/benchmarking" } [dev-dependencies] +assert_matches = "1.3.0" criterion = "0.3.0" frame-support = { version = "2.0.0-dev", path = "../../../frame/support" } frame-system = { version = "2.0.0-dev", path = "../../../frame/system" } diff --git a/bin/node/executor/tests/basic.rs b/bin/node/executor/tests/basic.rs index 9350c3546ff85..8ab16932112a0 100644 --- a/bin/node/executor/tests/basic.rs +++ b/bin/node/executor/tests/basic.rs @@ -38,6 +38,7 @@ use node_runtime::{ use node_primitives::{Balance, Hash}; use wabt; use node_testing::keyring::*; +use assert_matches::assert_matches; pub mod common; use self::common::{*, sign}; @@ -355,7 +356,18 @@ fn full_native_block_import_works() { topics: vec![], }, ]; - assert_eq!(System::events(), events); + let mut is_events = System::events(); + assert_matches!( + is_events.remove(0), + EventRecord { + phase: Phase::Initialization, + event: Event::pallet_transaction_payment( + pallet_transaction_payment::Event::PaymentParameters(_) + ), + topics: top + } if top == vec![] + ); + assert_eq!(is_events, events); }); fm = t.execute_with(TransactionPayment::next_fee_multiplier); @@ -436,7 +448,18 @@ fn full_native_block_import_works() { topics: vec![], }, ]; - assert_eq!(System::events(), events); + let mut is_events = System::events(); + assert_matches!( + is_events.remove(0), + EventRecord { + phase: Phase::Initialization, + event: Event::pallet_transaction_payment( + pallet_transaction_payment::Event::PaymentParameters(_) + ), + topics: top + } if top == vec![] + ); + assert_eq!(is_events, events); }); } diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index bc44bee8dbf60..5df53318ea602 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -232,6 +232,7 @@ parameter_types! { } impl pallet_transaction_payment::Trait for Runtime { + type Event = Event; type Currency = Balances; type OnTransactionPayment = DealWithFees; type TransactionByteFee = TransactionByteFee; @@ -713,7 +714,7 @@ construct_runtime!( Authorship: pallet_authorship::{Module, Call, Storage, Inherent}, Indices: pallet_indices::{Module, Call, Storage, Config, Event}, Balances: pallet_balances::{Module, Call, Storage, Config, Event}, - TransactionPayment: pallet_transaction_payment::{Module, Storage}, + TransactionPayment: pallet_transaction_payment::{Module, Storage, Event}, Staking: pallet_staking::{Module, Call, Config, Storage, Event, ValidateUnsigned}, Session: pallet_session::{Module, Call, Storage, Event, Config}, Democracy: pallet_democracy::{Module, Call, Storage, Config, Event}, diff --git a/frame/balances/src/tests_composite.rs b/frame/balances/src/tests_composite.rs index e78171376c16b..f040448ba5082 100644 --- a/frame/balances/src/tests_composite.rs +++ b/frame/balances/src/tests_composite.rs @@ -83,6 +83,7 @@ parameter_types! { pub const TransactionByteFee: u64 = 1; } impl pallet_transaction_payment::Trait for Test { + type Event = (); type Currency = Module; type OnTransactionPayment = (); type TransactionByteFee = TransactionByteFee; diff --git a/frame/balances/src/tests_local.rs b/frame/balances/src/tests_local.rs index 736afe9cbada6..3c7ae1c3cf62f 100644 --- a/frame/balances/src/tests_local.rs +++ b/frame/balances/src/tests_local.rs @@ -83,6 +83,7 @@ parameter_types! { pub const TransactionByteFee: u64 = 1; } impl pallet_transaction_payment::Trait for Test { + type Event = (); type Currency = Module; type OnTransactionPayment = (); type TransactionByteFee = TransactionByteFee; diff --git a/frame/contracts/src/tests.rs b/frame/contracts/src/tests.rs index 218a5c99372d4..40575c212f542 100644 --- a/frame/contracts/src/tests.rs +++ b/frame/contracts/src/tests.rs @@ -52,12 +52,14 @@ mod contracts { } use pallet_balances as balances; +use pallet_transaction_payment as payment; impl_outer_event! { pub enum MetaEvent for Test { system, balances, contracts, + payment, } } impl_outer_origin! { @@ -148,6 +150,7 @@ impl Convert> for Test { } impl pallet_transaction_payment::Trait for Test { + type Event = MetaEvent; type Currency = Balances; type OnTransactionPayment = (); type TransactionByteFee = TransactionByteFee; diff --git a/frame/executive/src/lib.rs b/frame/executive/src/lib.rs index 499fd3ebdfa31..e2fccb3fea1c3 100644 --- a/frame/executive/src/lib.rs +++ b/frame/executive/src/lib.rs @@ -512,6 +512,7 @@ mod tests { type Custom = custom::Module; use pallet_balances as balances; + use pallet_transaction_payment as payment; impl_outer_origin! { pub enum Origin for Runtime { } @@ -521,6 +522,7 @@ mod tests { pub enum MetaEvent for Runtime { system, balances, + payment, } } impl_outer_dispatch! { @@ -585,6 +587,7 @@ mod tests { pub const TransactionByteFee: Balance = 0; } impl pallet_transaction_payment::Trait for Runtime { + type Event = MetaEvent; type Currency = Balances; type OnTransactionPayment = (); type TransactionByteFee = TransactionByteFee; diff --git a/frame/transaction-payment/src/lib.rs b/frame/transaction-payment/src/lib.rs index 42004021d6cad..598c98fa30720 100644 --- a/frame/transaction-payment/src/lib.rs +++ b/frame/transaction-payment/src/lib.rs @@ -35,7 +35,7 @@ use sp_std::prelude::*; use codec::{Encode, Decode}; use frame_support::{ - decl_storage, decl_module, + decl_storage, decl_module, decl_event, traits::{Currency, Get, OnUnbalanced, ExistenceRequirement, WithdrawReason, Imbalance}, weights::{Weight, DispatchInfo, PostDispatchInfo, GetDispatchInfo, Pays}, dispatch::DispatchResult, @@ -52,6 +52,7 @@ use sp_runtime::{ }, }; use pallet_transaction_payment_rpc_runtime_api::RuntimeDispatchInfo; +use frame_system as system; type Multiplier = Fixed128; type BalanceOf = @@ -60,6 +61,9 @@ type NegativeImbalanceOf = <::Currency as Currency<::AccountId>>::NegativeImbalance; pub trait Trait: frame_system::Trait { + /// The overarching event type. + type Event: From + Into<::Event>; + /// The currency type in which fees will be paid. type Currency: Currency + Send + Sync; @@ -84,14 +88,28 @@ decl_storage! { } } +decl_event!( + pub enum Event { + /// Per block event that exposes parameters needed to calculate fees off-chain. + PaymentParameters(Multiplier), + } +); + decl_module! { pub struct Module for enum Call where origin: T::Origin { /// The fee to be paid for making a transaction; the per-byte portion. const TransactionByteFee: BalanceOf = T::TransactionByteFee::get(); + fn deposit_event() = default; + + fn on_initialize() -> Weight { + Self::deposit_event(Event::PaymentParameters(NextFeeMultiplier::get())); + 0 + } + fn on_finalize() { NextFeeMultiplier::mutate(|fm| { - *fm = T::FeeMultiplierUpdate::convert(*fm) + *fm = T::FeeMultiplierUpdate::convert(*fm); }); } } @@ -418,6 +436,7 @@ mod tests { } impl Trait for Runtime { + type Event = (); type Currency = pallet_balances::Module; type OnTransactionPayment = (); type TransactionByteFee = TransactionByteFee; From 6e185c2d07abc8eba826a7b9e6f3194fa129babc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20Thei=C3=9Fen?= Date: Fri, 15 May 2020 17:07:24 +0200 Subject: [PATCH 2/7] Add WeightToFee trait --- Cargo.lock | 46 +++++++++++----------- bin/node-template/runtime/src/lib.rs | 6 +-- bin/node/executor/tests/basic.rs | 11 ++++-- bin/node/executor/tests/fees.rs | 11 +++--- bin/node/runtime/src/impls.rs | 21 +++------- bin/node/runtime/src/lib.rs | 11 +++--- frame/balances/src/tests_composite.rs | 6 +-- frame/balances/src/tests_local.rs | 6 +-- frame/contracts/src/tests.rs | 4 +- frame/executive/src/lib.rs | 12 +++--- frame/support/Cargo.toml | 1 + frame/support/src/weights.rs | 56 +++++++++++++++++++++++++++ frame/transaction-payment/Cargo.toml | 1 + frame/transaction-payment/src/lib.rs | 34 ++++++++++++---- 14 files changed, 149 insertions(+), 77 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 951adbb3feeb8..2a233d871551f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -749,7 +749,7 @@ dependencies = [ "log", "regalloc", "serde", - "smallvec 1.3.0", + "smallvec 1.4.0", "target-lexicon", "thiserror", ] @@ -787,7 +787,7 @@ checksum = "e45f82e3446dd1ebb8c2c2f6a6b0e6cd6cd52965c7e5f7b1b35e9a9ace31ccde" dependencies = [ "cranelift-codegen", "log", - "smallvec 1.3.0", + "smallvec 1.4.0", "target-lexicon", ] @@ -1488,6 +1488,7 @@ dependencies = [ "paste", "pretty_assertions", "serde", + "smallvec 1.4.0", "sp-arithmetic", "sp-core", "sp-inherents", @@ -1875,7 +1876,7 @@ dependencies = [ "byteorder 1.3.4", "fallible-iterator", "indexmap", - "smallvec 1.3.0", + "smallvec 1.4.0", "stable_deref_trait", ] @@ -2495,7 +2496,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e763b2a9b500ba47948061d1e8bc3b5f03a8a1f067dbcf822a4d2c84d2b54a3a" dependencies = [ "parity-util-mem", - "smallvec 1.3.0", + "smallvec 1.4.0", ] [[package]] @@ -2524,7 +2525,7 @@ dependencies = [ "parking_lot 0.10.2", "regex", "rocksdb", - "smallvec 1.3.0", + "smallvec 1.4.0", ] [[package]] @@ -2630,7 +2631,7 @@ dependencies = [ "parity-multiaddr 0.9.0", "parking_lot 0.10.2", "pin-project", - "smallvec 1.3.0", + "smallvec 1.4.0", "wasm-timer", ] @@ -2661,7 +2662,7 @@ dependencies = [ "ring", "rw-stream-sink", "sha2", - "smallvec 1.3.0", + "smallvec 1.4.0", "thiserror", "unsigned-varint", "void", @@ -2714,7 +2715,7 @@ dependencies = [ "prost", "prost-build", "rand 0.7.3", - "smallvec 1.3.0", + "smallvec 1.4.0", ] [[package]] @@ -2737,7 +2738,7 @@ dependencies = [ "prost-build", "rand 0.7.3", "sha2", - "smallvec 1.3.0", + "smallvec 1.4.0", "unsigned-varint", "wasm-timer", ] @@ -2754,7 +2755,7 @@ dependencies = [ "log", "prost", "prost-build", - "smallvec 1.3.0", + "smallvec 1.4.0", "wasm-timer", ] @@ -2778,7 +2779,7 @@ dependencies = [ "prost-build", "rand 0.7.3", "sha2", - "smallvec 1.3.0", + "smallvec 1.4.0", "uint", "unsigned-varint", "void", @@ -2802,7 +2803,7 @@ dependencies = [ "log", "net2", "rand 0.7.3", - "smallvec 1.3.0", + "smallvec 1.4.0", "void", "wasm-timer", ] @@ -2931,7 +2932,7 @@ dependencies = [ "libp2p-core", "log", "rand 0.7.3", - "smallvec 1.3.0", + "smallvec 1.4.0", "void", "wasm-timer", ] @@ -3299,7 +3300,7 @@ dependencies = [ "futures 0.3.4", "log", "pin-project", - "smallvec 1.3.0", + "smallvec 1.4.0", "unsigned-varint", ] @@ -4653,6 +4654,7 @@ dependencies = [ "pallet-balances", "pallet-transaction-payment-rpc-runtime-api", "parity-scale-codec", + "smallvec 1.4.0", "sp-core", "sp-io", "sp-runtime", @@ -4848,7 +4850,7 @@ dependencies = [ "parity-util-mem-derive", "parking_lot 0.10.2", "primitive-types", - "smallvec 1.3.0", + "smallvec 1.4.0", "winapi 0.3.8", ] @@ -4924,7 +4926,7 @@ dependencies = [ "cloudabi", "libc", "redox_syscall", - "smallvec 1.3.0", + "smallvec 1.4.0", "winapi 0.3.8", ] @@ -5615,7 +5617,7 @@ checksum = "b27b256b41986ac5141b37b8bbba85d314fbf546c182eb255af6720e07e4f804" dependencies = [ "log", "rustc-hash", - "smallvec 1.3.0", + "smallvec 1.4.0", ] [[package]] @@ -7199,9 +7201,9 @@ dependencies = [ [[package]] name = "smallvec" -version = "1.3.0" +version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "05720e22615919e4734f6a99ceae50d00226c3c5aca406e102ebc33298214e0a" +checksum = "c7cb5678e1615754284ec264d9bb5b4c27d2018577fd90ac0ceb578591ed5ee4" [[package]] name = "snow" @@ -7248,7 +7250,7 @@ dependencies = [ "log", "rand 0.7.3", "sha1", - "smallvec 1.3.0", + "smallvec 1.4.0", "static_assertions", "thiserror", ] @@ -8974,7 +8976,7 @@ dependencies = [ "hashbrown", "log", "rustc-hex", - "smallvec 1.3.0", + "smallvec 1.4.0", ] [[package]] @@ -9078,7 +9080,7 @@ version = "0.1.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5479532badd04e128284890390c1e876ef7a993d0570b3597ae43dfa1d59afa4" dependencies = [ - "smallvec 1.3.0", + "smallvec 1.4.0", ] [[package]] diff --git a/bin/node-template/runtime/src/lib.rs b/bin/node-template/runtime/src/lib.rs index 9981f8b36f6fb..86e49b7436236 100644 --- a/bin/node-template/runtime/src/lib.rs +++ b/bin/node-template/runtime/src/lib.rs @@ -15,7 +15,7 @@ use sp_runtime::{ transaction_validity::{TransactionValidity, TransactionSource}, }; use sp_runtime::traits::{ - BlakeTwo256, Block as BlockT, IdentityLookup, Verify, ConvertInto, IdentifyAccount, NumberFor, + BlakeTwo256, Block as BlockT, IdentityLookup, Verify, IdentifyAccount, NumberFor, }; use sp_api::impl_runtime_apis; use sp_consensus_aura::sr25519::AuthorityId as AuraId; @@ -35,7 +35,7 @@ pub use frame_support::{ construct_runtime, parameter_types, StorageValue, traits::{KeyOwnerProofSystem, Randomness}, weights::{ - Weight, + Weight, IdentityFee, constants::{BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight, WEIGHT_PER_SECOND}, }, }; @@ -237,7 +237,7 @@ impl transaction_payment::Trait for Runtime { type Currency = balances::Module; type OnTransactionPayment = (); type TransactionByteFee = TransactionByteFee; - type WeightToFee = ConvertInto; + type WeightToFee = IdentityFee; type FeeMultiplierUpdate = (); } diff --git a/bin/node/executor/tests/basic.rs b/bin/node/executor/tests/basic.rs index 8ab16932112a0..81976d9fc0904 100644 --- a/bin/node/executor/tests/basic.rs +++ b/bin/node/executor/tests/basic.rs @@ -19,12 +19,15 @@ use codec::{Encode, Decode, Joiner}; use frame_support::{ StorageValue, StorageMap, traits::Currency, - weights::{GetDispatchInfo, DispatchInfo, DispatchClass, constants::ExtrinsicBaseWeight}, + weights::{ + GetDispatchInfo, DispatchInfo, DispatchClass, constants::ExtrinsicBaseWeight, + WeightToFeePolynomial, + }, }; use sp_core::{NeverNativeValue, traits::Externalities, storage::well_known_keys}; use sp_runtime::{ ApplyExtrinsicResult, Fixed128, - traits::{Hash as HashT, Convert}, + traits::Hash as HashT, transaction_validity::InvalidTransaction, }; use pallet_contracts::ContractAddressFor; @@ -55,9 +58,9 @@ fn transfer_fee(extrinsic: &E, fee_multiplier: Fixed128) -> Balance { let length_fee = TransactionByteFee::get() * (extrinsic.encode().len() as Balance); let base_weight = ExtrinsicBaseWeight::get(); - let base_fee = ::WeightToFee::convert(base_weight); + let base_fee = ::WeightToFee::calc(&base_weight); let weight = default_transfer_call().get_dispatch_info().weight; - let weight_fee = ::WeightToFee::convert(weight); + let weight_fee = ::WeightToFee::calc(&weight); base_fee + fee_multiplier.saturated_multiply_accumulate(length_fee + weight_fee) } diff --git a/bin/node/executor/tests/fees.rs b/bin/node/executor/tests/fees.rs index 0e55f781e78c2..c4c3ca0bfc656 100644 --- a/bin/node/executor/tests/fees.rs +++ b/bin/node/executor/tests/fees.rs @@ -19,16 +19,15 @@ use codec::{Encode, Joiner}; use frame_support::{ StorageValue, StorageMap, traits::Currency, - weights::{GetDispatchInfo, constants::ExtrinsicBaseWeight}, + weights::{GetDispatchInfo, constants::ExtrinsicBaseWeight, IdentityFee, WeightToFeePolynomial}, }; use sp_core::NeverNativeValue; -use sp_runtime::{Fixed128, Perbill, traits::Convert}; +use sp_runtime::{Fixed128, Perbill}; use node_runtime::{ CheckedExtrinsic, Call, Runtime, Balances, TransactionPayment, - TransactionByteFee, WeightFeeCoefficient, + TransactionByteFee, constants::currency::*, }; -use node_runtime::impls::LinearWeightToFee; use node_primitives::Balance; use node_testing::keyring::*; @@ -181,13 +180,13 @@ fn transaction_fee_is_correct_ultimate() { let mut balance_alice = (100 - 69) * DOLLARS; let base_weight = ExtrinsicBaseWeight::get(); - let base_fee = LinearWeightToFee::::convert(base_weight); + let base_fee = IdentityFee::::calc(&base_weight); let length_fee = TransactionByteFee::get() * (xt.clone().encode().len() as Balance); balance_alice -= length_fee; let weight = default_transfer_call().get_dispatch_info().weight; - let weight_fee = LinearWeightToFee::::convert(weight); + let weight_fee = IdentityFee::::calc(&weight); // we know that weight to fee multiplier is effect-less in block 1. // current weight of transfer = 200_000_000 diff --git a/bin/node/runtime/src/impls.rs b/bin/node/runtime/src/impls.rs index ef994392b52c0..884bde08df347 100644 --- a/bin/node/runtime/src/impls.rs +++ b/bin/node/runtime/src/impls.rs @@ -21,7 +21,9 @@ use core::num::NonZeroI128; use node_primitives::Balance; use sp_runtime::traits::{Convert, Saturating}; use sp_runtime::{Fixed128, Perquintill}; -use frame_support::{traits::{OnUnbalanced, Currency, Get}, weights::Weight}; +use frame_support::{ + traits::{OnUnbalanced, Currency, Get}, +}; use crate::{Balances, System, Authorship, MaximumBlockWeight, NegativeImbalance}; pub struct Author; @@ -47,18 +49,6 @@ impl Convert for CurrencyToVoteHandler { fn convert(x: u128) -> Balance { x * Self::factor() } } -/// Convert from weight to balance via a simple coefficient multiplication -/// The associated type C encapsulates a constant in units of balance per weight -pub struct LinearWeightToFee(sp_std::marker::PhantomData); - -impl> Convert for LinearWeightToFee { - fn convert(w: Weight) -> Balance { - // setting this to zero will disable the weight fee. - let coefficient = C::get(); - Balance::from(w).saturating_mul(coefficient) - } -} - /// Update the given multiplier based on the following formula /// /// diff = (previous_block_weight - target_weight)/max_weight @@ -120,7 +110,7 @@ mod tests { use sp_runtime::assert_eq_error_rate; use crate::{MaximumBlockWeight, AvailableBlockRatio, Runtime}; use crate::{constants::currency::*, TransactionPayment, TargetBlockFullness}; - use frame_support::weights::Weight; + use frame_support::weights::{Weight, WeightToFeePolynomial}; use core::num::NonZeroI128; fn max() -> Weight { @@ -228,7 +218,8 @@ mod tests { if fm == next { panic!("The fee should ever increase"); } fm = next; iterations += 1; - let fee = ::WeightToFee::convert(tx_weight); + let fee = + ::WeightToFee::calc(&tx_weight); let adjusted_fee = fm.saturated_multiply_accumulate(fee); println!( "iteration {}, new fm = {:?}. Fee at this point is: {} units / {} millicents, \ diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index 5df53318ea602..0a5d7eeaaeee8 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -26,7 +26,7 @@ use sp_std::prelude::*; use frame_support::{ construct_runtime, parameter_types, debug, weights::{ - Weight, + Weight, IdentityFee, constants::{BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight, WEIGHT_PER_SECOND}, }, traits::{Currency, Imbalance, KeyOwnerProofSystem, OnUnbalanced, Randomness, LockIdentifier}, @@ -74,7 +74,7 @@ pub use pallet_staking::StakerStatus; /// Implementations of some helper traits passed into runtime modules as associated types. pub mod impls; -use impls::{CurrencyToVoteHandler, Author, LinearWeightToFee, TargetedFeeAdjustment}; +use impls::{CurrencyToVoteHandler, Author, TargetedFeeAdjustment}; /// Constant values used within the runtime. pub mod constants; @@ -224,9 +224,6 @@ impl pallet_balances::Trait for Runtime { parameter_types! { pub const TransactionByteFee: Balance = 10 * MILLICENTS; - // In the Substrate node, a weight of 10_000_000 (smallest non-zero weight) - // is mapped to 10_000_000 units of fees, hence: - pub const WeightFeeCoefficient: Balance = 1; // for a sane configuration, this should always be less than `AvailableBlockRatio`. pub const TargetBlockFullness: Perquintill = Perquintill::from_percent(25); } @@ -236,7 +233,9 @@ impl pallet_transaction_payment::Trait for Runtime { type Currency = Balances; type OnTransactionPayment = DealWithFees; type TransactionByteFee = TransactionByteFee; - type WeightToFee = LinearWeightToFee; + // In the Substrate node, a weight of 10_000_000 (smallest non-zero weight) + // is mapped to 10_000_000 units of fees, hence: + type WeightToFee = IdentityFee; type FeeMultiplierUpdate = TargetedFeeAdjustment; } diff --git a/frame/balances/src/tests_composite.rs b/frame/balances/src/tests_composite.rs index f040448ba5082..a36ac34afbb5e 100644 --- a/frame/balances/src/tests_composite.rs +++ b/frame/balances/src/tests_composite.rs @@ -21,14 +21,14 @@ use sp_runtime::{ Perbill, - traits::{ConvertInto, IdentityLookup}, + traits::IdentityLookup, testing::Header, }; use sp_core::H256; use sp_io; use frame_support::{impl_outer_origin, parameter_types}; use frame_support::traits::Get; -use frame_support::weights::{Weight, DispatchInfo}; +use frame_support::weights::{Weight, DispatchInfo, IdentityFee}; use std::cell::RefCell; use crate::{GenesisConfig, Module, Trait, decl_tests, tests::CallWithDispatchInfo}; @@ -87,7 +87,7 @@ impl pallet_transaction_payment::Trait for Test { type Currency = Module; type OnTransactionPayment = (); type TransactionByteFee = TransactionByteFee; - type WeightToFee = ConvertInto; + type WeightToFee = IdentityFee; type FeeMultiplierUpdate = (); } impl Trait for Test { diff --git a/frame/balances/src/tests_local.rs b/frame/balances/src/tests_local.rs index 3c7ae1c3cf62f..efa509a86e511 100644 --- a/frame/balances/src/tests_local.rs +++ b/frame/balances/src/tests_local.rs @@ -21,14 +21,14 @@ use sp_runtime::{ Perbill, - traits::{ConvertInto, IdentityLookup}, + traits::IdentityLookup, testing::Header, }; use sp_core::H256; use sp_io; use frame_support::{impl_outer_origin, parameter_types}; use frame_support::traits::{Get, StorageMapShim}; -use frame_support::weights::{Weight, DispatchInfo}; +use frame_support::weights::{Weight, DispatchInfo, IdentityFee}; use std::cell::RefCell; use crate::{GenesisConfig, Module, Trait, decl_tests, tests::CallWithDispatchInfo}; @@ -87,7 +87,7 @@ impl pallet_transaction_payment::Trait for Test { type Currency = Module; type OnTransactionPayment = (); type TransactionByteFee = TransactionByteFee; - type WeightToFee = ConvertInto; + type WeightToFee = IdentityFee; type FeeMultiplierUpdate = (); } impl Trait for Test { diff --git a/frame/contracts/src/tests.rs b/frame/contracts/src/tests.rs index 40575c212f542..44b612e217c22 100644 --- a/frame/contracts/src/tests.rs +++ b/frame/contracts/src/tests.rs @@ -37,7 +37,7 @@ use frame_support::{ assert_ok, assert_err, assert_err_ignore_postinfo, impl_outer_dispatch, impl_outer_event, impl_outer_origin, parameter_types, storage::child, StorageMap, StorageValue, traits::{Currency, Get}, - weights::{DispatchInfo, DispatchClass, Weight, PostDispatchInfo, Pays}, + weights::{DispatchInfo, DispatchClass, Weight, PostDispatchInfo, Pays, IdentityFee}, }; use std::{cell::RefCell, sync::atomic::{AtomicUsize, Ordering}}; use sp_core::storage::well_known_keys; @@ -154,7 +154,7 @@ impl pallet_transaction_payment::Trait for Test { type Currency = Balances; type OnTransactionPayment = (); type TransactionByteFee = TransactionByteFee; - type WeightToFee = Test; + type WeightToFee = IdentityFee>; type FeeMultiplierUpdate = (); } diff --git a/frame/executive/src/lib.rs b/frame/executive/src/lib.rs index e2fccb3fea1c3..15757fab1334b 100644 --- a/frame/executive/src/lib.rs +++ b/frame/executive/src/lib.rs @@ -454,12 +454,12 @@ mod tests { use sp_core::H256; use sp_runtime::{ generic::Era, Perbill, DispatchError, testing::{Digest, Header, Block}, - traits::{Header as HeaderT, BlakeTwo256, IdentityLookup, Convert, ConvertInto}, + traits::{Header as HeaderT, BlakeTwo256, IdentityLookup}, transaction_validity::{InvalidTransaction, UnknownTransaction, TransactionValidityError}, }; use frame_support::{ impl_outer_event, impl_outer_origin, parameter_types, impl_outer_dispatch, - weights::{Weight, RuntimeDbWeight}, + weights::{Weight, RuntimeDbWeight, IdentityFee, WeightToFeePolynomial}, traits::{Currency, LockIdentifier, LockableCurrency, WithdrawReasons, WithdrawReason}, }; use frame_system::{self as system, Call as SystemCall, ChainContext, LastRuntimeUpgradeInfo}; @@ -591,7 +591,7 @@ mod tests { type Currency = Balances; type OnTransactionPayment = (); type TransactionByteFee = TransactionByteFee; - type WeightToFee = ConvertInto; + type WeightToFee = IdentityFee; type FeeMultiplierUpdate = (); } impl custom::Trait for Runtime {} @@ -677,7 +677,8 @@ mod tests { }.assimilate_storage(&mut t).unwrap(); let xt = TestXt::new(Call::Balances(BalancesCall::transfer(2, 69)), sign_extra(1, 0, 0)); let weight = xt.get_dispatch_info().weight + ::ExtrinsicBaseWeight::get(); - let fee: Balance = ::WeightToFee::convert(weight); + let fee: Balance + = ::WeightToFee::calc(&weight); let mut t = sp_io::TestExternalities::new(t); t.execute_with(|| { Executive::initialize_block(&Header::new( @@ -873,7 +874,8 @@ mod tests { ); let weight = xt.get_dispatch_info().weight + ::ExtrinsicBaseWeight::get(); - let fee: Balance = ::WeightToFee::convert(weight); + let fee: Balance = + ::WeightToFee::calc(&weight); Executive::initialize_block(&Header::new( 1, H256::default(), diff --git a/frame/support/Cargo.toml b/frame/support/Cargo.toml index 3213f0dfe4ff2..61363be2df64d 100644 --- a/frame/support/Cargo.toml +++ b/frame/support/Cargo.toml @@ -29,6 +29,7 @@ once_cell = { version = "1", default-features = false, optional = true } sp-state-machine = { version = "0.8.0-dev", optional = true, path = "../../primitives/state-machine" } bitmask = { version = "0.5.0", default-features = false } impl-trait-for-tuples = "0.1.3" +smallvec = "1.4.0" [dev-dependencies] pretty_assertions = "0.6.1" diff --git a/frame/support/src/weights.rs b/frame/support/src/weights.rs index a0d150cbe9394..27f8eeb7cca53 100644 --- a/frame/support/src/weights.rs +++ b/frame/support/src/weights.rs @@ -135,6 +135,9 @@ use sp_runtime::{ generic::{CheckedExtrinsic, UncheckedExtrinsic}, }; use crate::dispatch::{DispatchErrorWithPostInfo, DispatchResultWithPostInfo, DispatchError}; +use sp_runtime::traits::SaturatedConversion; +use sp_arithmetic::{Perbill, traits::{BaseArithmetic, Saturating}}; +use smallvec::{smallvec, SmallVec}; /// Re-export priority as type pub use sp_runtime::transaction_validity::TransactionPriority; @@ -530,6 +533,59 @@ impl RuntimeDbWeight { } } +#[derive(Clone, Encode, Decode)] +pub struct WeightToFeeCoefficient { + pub coeff_integer: Balance, + pub coeff_frac: Perbill, + pub negative: bool, + pub degree: u8, +} + +pub type WeightToFeeCoefficients = SmallVec<[WeightToFeeCoefficient; 4]>; + +pub trait WeightToFeePolynomial { + type Balance: BaseArithmetic + From + Copy; + + fn polynomial() -> WeightToFeeCoefficients; + + fn calc(weight: &Weight) -> Self::Balance { + Self::polynomial().iter().fold(Self::Balance::saturated_from(0u32), |mut acc, args| { + let w = Self::Balance::saturated_from(*weight).saturating_pow(args.degree.into()); + + // The sum could get negative. Therefore we only sum with the accumulator. + let frac = args.coeff_frac.mul_ceil(w); + let integer = args.coeff_integer.saturating_mul(w); + + if args.negative { + acc = acc.saturating_sub(frac); + acc = acc.saturating_sub(integer); + } else { + acc = acc.saturating_add(frac); + acc = acc.saturating_add(integer); + } + + acc + }) + } +} + +pub struct IdentityFee(sp_std::marker::PhantomData); + +impl WeightToFeePolynomial for IdentityFee where + T: BaseArithmetic + From + Copy +{ + type Balance = T; + + fn polynomial() -> WeightToFeeCoefficients { + smallvec!(WeightToFeeCoefficient { + coeff_integer: 1u32.into(), + coeff_frac: Perbill::zero(), + negative: false, + degree: 1, + }) + } +} + #[cfg(test)] #[allow(dead_code)] mod tests { diff --git a/frame/transaction-payment/Cargo.toml b/frame/transaction-payment/Cargo.toml index 0f8b05c3ddd50..56efe907f5eb5 100644 --- a/frame/transaction-payment/Cargo.toml +++ b/frame/transaction-payment/Cargo.toml @@ -18,6 +18,7 @@ sp-runtime = { version = "2.0.0-dev", default-features = false, path = "../../pr frame-support = { version = "2.0.0-dev", default-features = false, path = "../support" } frame-system = { version = "2.0.0-dev", default-features = false, path = "../system" } pallet-transaction-payment-rpc-runtime-api = { version = "2.0.0-dev", default-features = false, path = "./rpc/runtime-api" } +smallvec = "1.4.0" [dev-dependencies] sp-io = { version = "2.0.0-dev", path = "../../primitives/io" } diff --git a/frame/transaction-payment/src/lib.rs b/frame/transaction-payment/src/lib.rs index 598c98fa30720..9e8e83bb760f3 100644 --- a/frame/transaction-payment/src/lib.rs +++ b/frame/transaction-payment/src/lib.rs @@ -37,7 +37,10 @@ use codec::{Encode, Decode}; use frame_support::{ decl_storage, decl_module, decl_event, traits::{Currency, Get, OnUnbalanced, ExistenceRequirement, WithdrawReason, Imbalance}, - weights::{Weight, DispatchInfo, PostDispatchInfo, GetDispatchInfo, Pays}, + weights::{ + Weight, DispatchInfo, PostDispatchInfo, GetDispatchInfo, Pays, WeightToFeePolynomial, + WeightToFeeCoefficient, + }, dispatch::DispatchResult, }; use sp_runtime::{ @@ -76,7 +79,7 @@ pub trait Trait: frame_system::Trait { type TransactionByteFee: Get>; /// Convert a weight value into a deductible fee based on the currency type. - type WeightToFee: Convert>; + type WeightToFee: WeightToFeePolynomial>; /// Update the multiplier of the next block, based on the previous block's weight. type FeeMultiplierUpdate: Convert; @@ -100,6 +103,10 @@ decl_module! { /// The fee to be paid for making a transaction; the per-byte portion. const TransactionByteFee: BalanceOf = T::TransactionByteFee::get(); + /// The polynomial that is applied in order to derive fee from weight. + const WeightToFee: Vec>> = + T::WeightToFee::polynomial().to_vec(); + fn deposit_event() = default; fn on_initialize() -> Weight { @@ -201,7 +208,7 @@ impl Module { // cap the weight to the maximum defined in runtime, otherwise it will be the // `Bounded` maximum of its data type, which is not desired. let capped_weight = weight.min(::MaximumBlockWeight::get()); - T::WeightToFee::convert(capped_weight) + T::WeightToFee::calc(&capped_weight) } } @@ -336,7 +343,10 @@ mod tests { use codec::Encode; use frame_support::{ impl_outer_dispatch, impl_outer_origin, parameter_types, - weights::{DispatchClass, DispatchInfo, PostDispatchInfo, GetDispatchInfo, Weight}, + weights::{ + DispatchClass, DispatchInfo, PostDispatchInfo, GetDispatchInfo, Weight, + WeightToFeePolynomial, WeightToFeeCoefficients, WeightToFeeCoefficient, + }, }; use pallet_balances::Call as BalancesCall; use pallet_transaction_payment_rpc_runtime_api::RuntimeDispatchInfo; @@ -347,6 +357,7 @@ mod tests { Perbill, }; use std::cell::RefCell; + use smallvec::smallvec; const CALL: &::Call = &Call::Balances(BalancesCall::transfer(2, 69)); @@ -428,10 +439,17 @@ mod tests { fn get() -> u64 { TRANSACTION_BYTE_FEE.with(|v| *v.borrow()) } } - pub struct WeightToFee(u64); - impl Convert for WeightToFee { - fn convert(t: Weight) -> u64 { - WEIGHT_TO_FEE.with(|v| *v.borrow() * (t as u64)) + pub struct WeightToFee; + impl WeightToFeePolynomial for WeightToFee { + type Balance = u64; + + fn polynomial() -> WeightToFeeCoefficients { + smallvec![WeightToFeeCoefficient { + degree: 1, + coeff_frac: Perbill::zero(), + coeff_integer: WEIGHT_TO_FEE.with(|v| *v.borrow()), + negative: false, + }] } } From ce414751fc62e78742d5358c7a7a620e7774f95e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20Thei=C3=9Fen?= Date: Mon, 18 May 2020 15:56:32 +0200 Subject: [PATCH 3/7] Add documentation to polynomial types --- frame/support/src/weights.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/frame/support/src/weights.rs b/frame/support/src/weights.rs index 27f8eeb7cca53..a3bba033de54b 100644 --- a/frame/support/src/weights.rs +++ b/frame/support/src/weights.rs @@ -533,21 +533,50 @@ impl RuntimeDbWeight { } } +/// One coefficient and its position in the `WeightToFeePolynomial`. +/// +/// One term of polynomial is calculated as: +/// +/// ``` +/// coeff_integer * x^(degree) + coeff_frac * x^(degree) +/// ``` +/// +/// The `negative` value encodes whether the term is added or substracted from the +/// overall polynomial result. #[derive(Clone, Encode, Decode)] pub struct WeightToFeeCoefficient { + /// The integral part of the coefficient. pub coeff_integer: Balance, + /// The fractional part of the coefficient. pub coeff_frac: Perbill, + /// True iff the coefficient should be interpreted as negative. pub negative: bool, + /// Degree/exponent of the term. pub degree: u8, } +/// A list of coefficients that represent one polynomial. pub type WeightToFeeCoefficients = SmallVec<[WeightToFeeCoefficient; 4]>; +/// A trait that describes the weight to fee calculation as polynomial. +/// +/// An implementor should only implement the `polynomial` function. pub trait WeightToFeePolynomial { + /// The type that is returned as result from polynomial evaluation. type Balance: BaseArithmetic + From + Copy; + /// Returns a polynomial that describes the weight to fee conversion. + /// + /// This is the only function that should be manually implemented. Please note + /// that all calculation is done in the probably unsigned `Balance` type. This means + /// that the order of coefficients is important as putting the negative coefficients + /// first will most likely saturate the result to zero mid evaluation. fn polynomial() -> WeightToFeeCoefficients; + /// Calculates the fee from the passed `weight` according to the `polynomial`. + /// + /// This should not be overriden in most circumstances. Calculation is done in the + /// `Balance` type and never overflows. All evaluation is saturating. fn calc(weight: &Weight) -> Self::Balance { Self::polynomial().iter().fold(Self::Balance::saturated_from(0u32), |mut acc, args| { let w = Self::Balance::saturated_from(*weight).saturating_pow(args.degree.into()); @@ -569,6 +598,7 @@ pub trait WeightToFeePolynomial { } } +/// Implementor of `WeightToFeePolynomial` that maps one unit of weight to one unit of fee. pub struct IdentityFee(sp_std::marker::PhantomData); impl WeightToFeePolynomial for IdentityFee where From 856ebf648e7aa3559438603cdb7accbcd640554d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20Thei=C3=9Fen?= Date: Tue, 19 May 2020 10:38:15 +0200 Subject: [PATCH 4/7] Ignore pseudo code snippet for doc tests --- frame/support/src/weights.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frame/support/src/weights.rs b/frame/support/src/weights.rs index a3bba033de54b..494ef7c95a41f 100644 --- a/frame/support/src/weights.rs +++ b/frame/support/src/weights.rs @@ -537,7 +537,7 @@ impl RuntimeDbWeight { /// /// One term of polynomial is calculated as: /// -/// ``` +/// ```ignore /// coeff_integer * x^(degree) + coeff_frac * x^(degree) /// ``` /// From 6ba33fbf6861361cdf462d8c408e0896991bece3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20Thei=C3=9Fen?= Date: Tue, 19 May 2020 11:32:33 +0200 Subject: [PATCH 5/7] Use `Mul` implementation of Perbill --- frame/support/src/weights.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/frame/support/src/weights.rs b/frame/support/src/weights.rs index 494ef7c95a41f..689db3f100d1b 100644 --- a/frame/support/src/weights.rs +++ b/frame/support/src/weights.rs @@ -582,7 +582,8 @@ pub trait WeightToFeePolynomial { let w = Self::Balance::saturated_from(*weight).saturating_pow(args.degree.into()); // The sum could get negative. Therefore we only sum with the accumulator. - let frac = args.coeff_frac.mul_ceil(w); + // The Perbill Mul implementation is non overflowing. + let frac = args.coeff_frac * w; let integer = args.coeff_integer.saturating_mul(w); if args.negative { From 992763d2a07c333d160e17f9d586b381c266f7d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20Thei=C3=9Fen?= Date: Tue, 19 May 2020 11:45:42 +0200 Subject: [PATCH 6/7] Add tests for WeightToFeePolynomial --- frame/support/src/weights.rs | 60 ++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/frame/support/src/weights.rs b/frame/support/src/weights.rs index 689db3f100d1b..771f908ecf75f 100644 --- a/frame/support/src/weights.rs +++ b/frame/support/src/weights.rs @@ -738,4 +738,64 @@ mod tests { 1000 ); } + + type Balance = u64; + + // 0.5x^3 + 2.333x2 + 7x - 10_000 + struct Poly; + impl WeightToFeePolynomial for Poly { + type Balance = Balance; + + fn polynomial() -> WeightToFeeCoefficients { + smallvec![ + WeightToFeeCoefficient { + coeff_integer: 0, + coeff_frac: Perbill::from_fraction(0.5), + negative: false, + degree: 3 + }, + WeightToFeeCoefficient { + coeff_integer: 2, + coeff_frac: Perbill::from_rational_approximation(1u32, 3u32), + negative: false, + degree: 2 + }, + WeightToFeeCoefficient { + coeff_integer: 7, + coeff_frac: Perbill::zero(), + negative: false, + degree: 1 + }, + WeightToFeeCoefficient { + coeff_integer: 10_000, + coeff_frac: Perbill::zero(), + negative: true, + degree: 0 + }, + ] + } + } + + #[test] + fn polynomial_works() { + assert_eq!(Poly::calc(&100), 514033); + assert_eq!(Poly::calc(&10_123), 518917034928); + } + + #[test] + fn polynomial_does_not_underflow() { + assert_eq!(Poly::calc(&0), 0); + } + + #[test] + fn polynomial_does_not_overflow() { + assert_eq!(Poly::calc(&Weight::max_value()), Balance::max_value() - 10_000); + } + + #[test] + fn identity_fee_works() { + assert_eq!(IdentityFee::::calc(&0), 0); + assert_eq!(IdentityFee::::calc(&50), 50); + assert_eq!(IdentityFee::::calc(&Weight::max_value()), Balance::max_value()); + } } From c742ed4a05483d23306a9eea8d1a714bbcd722b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20Thei=C3=9Fen?= Date: Tue, 19 May 2020 14:09:54 +0200 Subject: [PATCH 7/7] Revert "Emit a PaymentParameters event once per block" This reverts commit 6c4763baff3d8179676a3c1660fe7063fd56a8ca. --- Cargo.lock | 1 - bin/node-template/runtime/src/lib.rs | 3 +-- bin/node/executor/Cargo.toml | 1 - bin/node/executor/tests/basic.rs | 27 ++------------------------- bin/node/runtime/src/lib.rs | 3 +-- frame/balances/src/tests_composite.rs | 1 - frame/balances/src/tests_local.rs | 1 - frame/contracts/src/tests.rs | 3 --- frame/executive/src/lib.rs | 3 --- frame/transaction-payment/src/lib.rs | 21 +-------------------- 10 files changed, 5 insertions(+), 59 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2a233d871551f..f06c107286ca3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3492,7 +3492,6 @@ dependencies = [ name = "node-executor" version = "2.0.0-dev" dependencies = [ - "assert_matches", "criterion 0.3.1", "frame-benchmarking", "frame-support", diff --git a/bin/node-template/runtime/src/lib.rs b/bin/node-template/runtime/src/lib.rs index 86e49b7436236..d0540e0604599 100644 --- a/bin/node-template/runtime/src/lib.rs +++ b/bin/node-template/runtime/src/lib.rs @@ -233,7 +233,6 @@ parameter_types! { } impl transaction_payment::Trait for Runtime { - type Event = Event; type Currency = balances::Module; type OnTransactionPayment = (); type TransactionByteFee = TransactionByteFee; @@ -263,7 +262,7 @@ construct_runtime!( Aura: aura::{Module, Config, Inherent(Timestamp)}, Grandpa: grandpa::{Module, Call, Storage, Config, Event}, Balances: balances::{Module, Call, Storage, Config, Event}, - TransactionPayment: transaction_payment::{Module, Storage, Event}, + TransactionPayment: transaction_payment::{Module, Storage}, Sudo: sudo::{Module, Call, Config, Storage, Event}, // Used for the module template in `./template.rs` TemplateModule: template::{Module, Call, Storage, Event}, diff --git a/bin/node/executor/Cargo.toml b/bin/node/executor/Cargo.toml index 04c5e9c6a50ee..014ac9a4c8474 100644 --- a/bin/node/executor/Cargo.toml +++ b/bin/node/executor/Cargo.toml @@ -24,7 +24,6 @@ trie-root = "0.16.0" frame-benchmarking = { version = "2.0.0-dev", path = "../../../frame/benchmarking" } [dev-dependencies] -assert_matches = "1.3.0" criterion = "0.3.0" frame-support = { version = "2.0.0-dev", path = "../../../frame/support" } frame-system = { version = "2.0.0-dev", path = "../../../frame/system" } diff --git a/bin/node/executor/tests/basic.rs b/bin/node/executor/tests/basic.rs index 81976d9fc0904..857f438e1c16a 100644 --- a/bin/node/executor/tests/basic.rs +++ b/bin/node/executor/tests/basic.rs @@ -41,7 +41,6 @@ use node_runtime::{ use node_primitives::{Balance, Hash}; use wabt; use node_testing::keyring::*; -use assert_matches::assert_matches; pub mod common; use self::common::{*, sign}; @@ -359,18 +358,7 @@ fn full_native_block_import_works() { topics: vec![], }, ]; - let mut is_events = System::events(); - assert_matches!( - is_events.remove(0), - EventRecord { - phase: Phase::Initialization, - event: Event::pallet_transaction_payment( - pallet_transaction_payment::Event::PaymentParameters(_) - ), - topics: top - } if top == vec![] - ); - assert_eq!(is_events, events); + assert_eq!(System::events(), events); }); fm = t.execute_with(TransactionPayment::next_fee_multiplier); @@ -451,18 +439,7 @@ fn full_native_block_import_works() { topics: vec![], }, ]; - let mut is_events = System::events(); - assert_matches!( - is_events.remove(0), - EventRecord { - phase: Phase::Initialization, - event: Event::pallet_transaction_payment( - pallet_transaction_payment::Event::PaymentParameters(_) - ), - topics: top - } if top == vec![] - ); - assert_eq!(is_events, events); + assert_eq!(System::events(), events); }); } diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index 0a5d7eeaaeee8..e0a0cb9c6a739 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -229,7 +229,6 @@ parameter_types! { } impl pallet_transaction_payment::Trait for Runtime { - type Event = Event; type Currency = Balances; type OnTransactionPayment = DealWithFees; type TransactionByteFee = TransactionByteFee; @@ -713,7 +712,7 @@ construct_runtime!( Authorship: pallet_authorship::{Module, Call, Storage, Inherent}, Indices: pallet_indices::{Module, Call, Storage, Config, Event}, Balances: pallet_balances::{Module, Call, Storage, Config, Event}, - TransactionPayment: pallet_transaction_payment::{Module, Storage, Event}, + TransactionPayment: pallet_transaction_payment::{Module, Storage}, Staking: pallet_staking::{Module, Call, Config, Storage, Event, ValidateUnsigned}, Session: pallet_session::{Module, Call, Storage, Event, Config}, Democracy: pallet_democracy::{Module, Call, Storage, Config, Event}, diff --git a/frame/balances/src/tests_composite.rs b/frame/balances/src/tests_composite.rs index a36ac34afbb5e..2782eb10bb101 100644 --- a/frame/balances/src/tests_composite.rs +++ b/frame/balances/src/tests_composite.rs @@ -83,7 +83,6 @@ parameter_types! { pub const TransactionByteFee: u64 = 1; } impl pallet_transaction_payment::Trait for Test { - type Event = (); type Currency = Module; type OnTransactionPayment = (); type TransactionByteFee = TransactionByteFee; diff --git a/frame/balances/src/tests_local.rs b/frame/balances/src/tests_local.rs index efa509a86e511..43e86eb483d25 100644 --- a/frame/balances/src/tests_local.rs +++ b/frame/balances/src/tests_local.rs @@ -83,7 +83,6 @@ parameter_types! { pub const TransactionByteFee: u64 = 1; } impl pallet_transaction_payment::Trait for Test { - type Event = (); type Currency = Module; type OnTransactionPayment = (); type TransactionByteFee = TransactionByteFee; diff --git a/frame/contracts/src/tests.rs b/frame/contracts/src/tests.rs index 44b612e217c22..9e3c777262dc2 100644 --- a/frame/contracts/src/tests.rs +++ b/frame/contracts/src/tests.rs @@ -52,14 +52,12 @@ mod contracts { } use pallet_balances as balances; -use pallet_transaction_payment as payment; impl_outer_event! { pub enum MetaEvent for Test { system, balances, contracts, - payment, } } impl_outer_origin! { @@ -150,7 +148,6 @@ impl Convert> for Test { } impl pallet_transaction_payment::Trait for Test { - type Event = MetaEvent; type Currency = Balances; type OnTransactionPayment = (); type TransactionByteFee = TransactionByteFee; diff --git a/frame/executive/src/lib.rs b/frame/executive/src/lib.rs index 15757fab1334b..02a5daa3fa00b 100644 --- a/frame/executive/src/lib.rs +++ b/frame/executive/src/lib.rs @@ -512,7 +512,6 @@ mod tests { type Custom = custom::Module; use pallet_balances as balances; - use pallet_transaction_payment as payment; impl_outer_origin! { pub enum Origin for Runtime { } @@ -522,7 +521,6 @@ mod tests { pub enum MetaEvent for Runtime { system, balances, - payment, } } impl_outer_dispatch! { @@ -587,7 +585,6 @@ mod tests { pub const TransactionByteFee: Balance = 0; } impl pallet_transaction_payment::Trait for Runtime { - type Event = MetaEvent; type Currency = Balances; type OnTransactionPayment = (); type TransactionByteFee = TransactionByteFee; diff --git a/frame/transaction-payment/src/lib.rs b/frame/transaction-payment/src/lib.rs index 9e8e83bb760f3..59db9f5b8a827 100644 --- a/frame/transaction-payment/src/lib.rs +++ b/frame/transaction-payment/src/lib.rs @@ -35,7 +35,7 @@ use sp_std::prelude::*; use codec::{Encode, Decode}; use frame_support::{ - decl_storage, decl_module, decl_event, + decl_storage, decl_module, traits::{Currency, Get, OnUnbalanced, ExistenceRequirement, WithdrawReason, Imbalance}, weights::{ Weight, DispatchInfo, PostDispatchInfo, GetDispatchInfo, Pays, WeightToFeePolynomial, @@ -55,7 +55,6 @@ use sp_runtime::{ }, }; use pallet_transaction_payment_rpc_runtime_api::RuntimeDispatchInfo; -use frame_system as system; type Multiplier = Fixed128; type BalanceOf = @@ -64,9 +63,6 @@ type NegativeImbalanceOf = <::Currency as Currency<::AccountId>>::NegativeImbalance; pub trait Trait: frame_system::Trait { - /// The overarching event type. - type Event: From + Into<::Event>; - /// The currency type in which fees will be paid. type Currency: Currency + Send + Sync; @@ -91,13 +87,6 @@ decl_storage! { } } -decl_event!( - pub enum Event { - /// Per block event that exposes parameters needed to calculate fees off-chain. - PaymentParameters(Multiplier), - } -); - decl_module! { pub struct Module for enum Call where origin: T::Origin { /// The fee to be paid for making a transaction; the per-byte portion. @@ -107,13 +96,6 @@ decl_module! { const WeightToFee: Vec>> = T::WeightToFee::polynomial().to_vec(); - fn deposit_event() = default; - - fn on_initialize() -> Weight { - Self::deposit_event(Event::PaymentParameters(NextFeeMultiplier::get())); - 0 - } - fn on_finalize() { NextFeeMultiplier::mutate(|fm| { *fm = T::FeeMultiplierUpdate::convert(*fm); @@ -454,7 +436,6 @@ mod tests { } impl Trait for Runtime { - type Event = (); type Currency = pallet_balances::Module; type OnTransactionPayment = (); type TransactionByteFee = TransactionByteFee;