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

Commit

Permalink
Emit a PaymentParameters event once per block
Browse files Browse the repository at this point in the history
This contains per-block paramaters need to calculate
fees off-chain.
  • Loading branch information
athei committed May 19, 2020
1 parent 9891eab commit 6c4763b
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 6 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.

3 changes: 2 additions & 1 deletion bin/node-template/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ parameter_types! {
}

impl transaction_payment::Trait for Runtime {
type Event = Event;
type Currency = balances::Module<Runtime>;
type OnTransactionPayment = ();
type TransactionByteFee = TransactionByteFee;
Expand Down Expand Up @@ -262,7 +263,7 @@ construct_runtime!(
Aura: aura::{Module, Config<T>, Inherent(Timestamp)},
Grandpa: grandpa::{Module, Call, Storage, Config, Event},
Balances: balances::{Module, Call, Storage, Config<T>, Event<T>},
TransactionPayment: transaction_payment::{Module, Storage},
TransactionPayment: transaction_payment::{Module, Storage, Event},
Sudo: sudo::{Module, Call, Config<T>, Storage, Event<T>},
// Used for the module template in `./template.rs`
TemplateModule: template::{Module, Call, Storage, Event<T>},
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 @@ -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" }
Expand Down
27 changes: 25 additions & 2 deletions bin/node/executor/tests/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
});
}

Expand Down
3 changes: 2 additions & 1 deletion bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -713,7 +714,7 @@ construct_runtime!(
Authorship: pallet_authorship::{Module, Call, Storage, Inherent},
Indices: pallet_indices::{Module, Call, Storage, Config<T>, Event<T>},
Balances: pallet_balances::{Module, Call, Storage, Config<T>, Event<T>},
TransactionPayment: pallet_transaction_payment::{Module, Storage},
TransactionPayment: pallet_transaction_payment::{Module, Storage, Event},
Staking: pallet_staking::{Module, Call, Config<T>, Storage, Event<T>, ValidateUnsigned},
Session: pallet_session::{Module, Call, Storage, Event, Config<T>},
Democracy: pallet_democracy::{Module, Call, Storage, Config, Event<T>},
Expand Down
1 change: 1 addition & 0 deletions frame/balances/src/tests_composite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ parameter_types! {
pub const TransactionByteFee: u64 = 1;
}
impl pallet_transaction_payment::Trait for Test {
type Event = ();
type Currency = Module<Test>;
type OnTransactionPayment = ();
type TransactionByteFee = TransactionByteFee;
Expand Down
1 change: 1 addition & 0 deletions frame/balances/src/tests_local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ parameter_types! {
pub const TransactionByteFee: u64 = 1;
}
impl pallet_transaction_payment::Trait for Test {
type Event = ();
type Currency = Module<Test>;
type OnTransactionPayment = ();
type TransactionByteFee = TransactionByteFee;
Expand Down
3 changes: 3 additions & 0 deletions frame/contracts/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>,
balances<T>,
contracts<T>,
payment,
}
}
impl_outer_origin! {
Expand Down Expand Up @@ -148,6 +150,7 @@ impl Convert<Weight, BalanceOf<Self>> for Test {
}

impl pallet_transaction_payment::Trait for Test {
type Event = MetaEvent;
type Currency = Balances;
type OnTransactionPayment = ();
type TransactionByteFee = TransactionByteFee;
Expand Down
3 changes: 3 additions & 0 deletions frame/executive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,7 @@ mod tests {
type Custom = custom::Module<Runtime>;

use pallet_balances as balances;
use pallet_transaction_payment as payment;

impl_outer_origin! {
pub enum Origin for Runtime { }
Expand All @@ -521,6 +522,7 @@ mod tests {
pub enum MetaEvent for Runtime {
system<T>,
balances<T>,
payment,
}
}
impl_outer_dispatch! {
Expand Down Expand Up @@ -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;
Expand Down
23 changes: 21 additions & 2 deletions frame/transaction-payment/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<T> =
Expand All @@ -60,6 +61,9 @@ type NegativeImbalanceOf<T> =
<<T as Trait>::Currency as Currency<<T as frame_system::Trait>::AccountId>>::NegativeImbalance;

pub trait Trait: frame_system::Trait {
/// The overarching event type.
type Event: From<Event> + Into<<Self as frame_system::Trait>::Event>;

/// The currency type in which fees will be paid.
type Currency: Currency<Self::AccountId> + Send + Sync;

Expand All @@ -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<T: Trait> for enum Call where origin: T::Origin {
/// The fee to be paid for making a transaction; the per-byte portion.
const TransactionByteFee: BalanceOf<T> = 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);
});
}
}
Expand Down Expand Up @@ -418,6 +436,7 @@ mod tests {
}

impl Trait for Runtime {
type Event = ();
type Currency = pallet_balances::Module<Runtime>;
type OnTransactionPayment = ();
type TransactionByteFee = TransactionByteFee;
Expand Down

0 comments on commit 6c4763b

Please sign in to comment.