mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-09 20:11:09 +00:00
Transaction Fee Module (#1648)
* wip * Split bytes fee charging and charging by amount into different traits. * Move to edition 2018. * Implemented charge fee traits for fees module. * Implemented 'on_finalise' for fee module. * Updated fees finalize impl. * Renaming and documentation update. * Added overflow & underflow check for fee calculation. * Added mock and unit tests for fee module. * More unit tests for fees module. * Fixed srml-executive unit tests. * Remove transaction base/bytes fee from balances module, fix unit tests. * fix compile error * Fixed unit test. * Minor fixes. * Bump spec version. * Bump spec version. * Updated fees module and runtime wasm. * Fees module code style improvement; updated runtime wasm. * Bump spec and impl version.
This commit is contained in:
committed by
Bastian Köcher
parent
6a6c3155a6
commit
fafffdb771
@@ -0,0 +1,116 @@
|
||||
// Copyright 2019 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Substrate is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Handles all transaction fee related operations
|
||||
|
||||
// Ensure we're `no_std` when compiling for Wasm.
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
|
||||
use srml_support::{dispatch::Result, Parameter, StorageMap, decl_event, decl_storage, decl_module};
|
||||
use runtime_primitives::traits::{
|
||||
As, Member, SimpleArithmetic, ChargeBytesFee, ChargeFee,
|
||||
TransferAsset, CheckedAdd, CheckedSub, CheckedMul, Zero
|
||||
};
|
||||
use system;
|
||||
|
||||
mod mock;
|
||||
mod tests;
|
||||
|
||||
pub trait Trait: system::Trait {
|
||||
/// The overarching event type.
|
||||
type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;
|
||||
|
||||
/// The unit for fee amount
|
||||
type Amount: Member + Parameter + SimpleArithmetic + Default + Copy + As<u64>;
|
||||
|
||||
/// A function does the asset transfer between accounts
|
||||
type TransferAsset: TransferAsset<Self::AccountId, Amount = Self::Amount>;
|
||||
}
|
||||
|
||||
decl_module! {
|
||||
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
|
||||
fn deposit_event<T>() = default;
|
||||
|
||||
fn on_finalise() {
|
||||
let extrinsic_count = <system::Module<T>>::extrinsic_count();
|
||||
(0..extrinsic_count).for_each(|index| {
|
||||
// Deposit `Charged` event if some amount of fee charged.
|
||||
let fee = <CurrentTransactionFee<T>>::take(index);
|
||||
if !fee.is_zero() {
|
||||
Self::deposit_event(RawEvent::Charged(index, fee));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
decl_event!(
|
||||
pub enum Event<T> where <T as Trait>::Amount {
|
||||
/// Fee charged (extrinsic_index, fee_amount)
|
||||
Charged(u32, Amount),
|
||||
}
|
||||
);
|
||||
|
||||
decl_storage! {
|
||||
trait Store for Module<T: Trait> as Fees {
|
||||
/// The fee to be paid for making a transaction; the base.
|
||||
pub TransactionBaseFee get(transaction_base_fee) config(): T::Amount;
|
||||
/// The fee to be paid for making a transaction; the per-byte portion.
|
||||
pub TransactionByteFee get(transaction_byte_fee) config(): T::Amount;
|
||||
|
||||
/// The `extrinsic_index => accumulated_fees` map, containing records to
|
||||
/// track the overall charged fees for each transaction.
|
||||
///
|
||||
/// All records should be removed at finalise stage.
|
||||
CurrentTransactionFee get(current_transaction_fee): map u32 => T::Amount;
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Trait> ChargeBytesFee<T::AccountId> for Module<T> {
|
||||
fn charge_base_bytes_fee(transactor: &T::AccountId, encoded_len: usize) -> Result {
|
||||
let bytes_fee = Self::transaction_byte_fee().checked_mul(
|
||||
&<T::Amount as As<u64>>::sa(encoded_len as u64)
|
||||
).ok_or_else(|| "bytes fee overflow")?;
|
||||
let overall = Self::transaction_base_fee().checked_add(&bytes_fee).ok_or_else(|| "bytes fee overflow")?;
|
||||
Self::charge_fee(transactor, overall)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Trait> ChargeFee<T::AccountId> for Module<T> {
|
||||
type Amount = T::Amount;
|
||||
|
||||
fn charge_fee(transactor: &T::AccountId, amount: T::Amount) -> Result {
|
||||
let extrinsic_index = <system::Module<T>>::extrinsic_index().ok_or_else(|| "no extrinsic index found")?;
|
||||
let current_fee = Self::current_transaction_fee(extrinsic_index);
|
||||
let new_fee = current_fee.checked_add(&amount).ok_or_else(|| "fee got overflow after charge")?;
|
||||
|
||||
T::TransferAsset::remove_from(transactor, amount)?;
|
||||
|
||||
<CurrentTransactionFee<T>>::insert(extrinsic_index, new_fee);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn refund_fee(transactor: &T::AccountId, amount: T::Amount) -> Result {
|
||||
let extrinsic_index = <system::Module<T>>::extrinsic_index().ok_or_else(|| "no extrinsic index found")?;
|
||||
let current_fee = Self::current_transaction_fee(extrinsic_index);
|
||||
let new_fee = current_fee.checked_sub(&amount).ok_or_else(|| "fee got underflow after refund")?;
|
||||
|
||||
T::TransferAsset::add_to(transactor, amount)?;
|
||||
|
||||
<CurrentTransactionFee<T>>::insert(extrinsic_index, new_fee);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
// Copyright 2019 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Substrate is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Test utilities
|
||||
|
||||
#![cfg(test)]
|
||||
|
||||
use runtime_primitives::BuildStorage;
|
||||
use runtime_primitives::{
|
||||
traits::{IdentityLookup, BlakeTwo256, TransferAsset},
|
||||
testing::{Digest, DigestItem, Header},
|
||||
};
|
||||
use primitives::{H256, Blake2Hasher};
|
||||
use runtime_io;
|
||||
use srml_support::{impl_outer_origin, impl_outer_event};
|
||||
use crate::{GenesisConfig, Module, Trait, system};
|
||||
|
||||
impl_outer_origin!{
|
||||
pub enum Origin for Test {}
|
||||
}
|
||||
|
||||
mod fees {
|
||||
pub use crate::Event;
|
||||
}
|
||||
|
||||
impl_outer_event!{
|
||||
pub enum TestEvent for Test {
|
||||
fees<T>,
|
||||
}
|
||||
}
|
||||
|
||||
pub struct TransferAssetMock;
|
||||
|
||||
impl<AccountId> TransferAsset<AccountId> for TransferAssetMock {
|
||||
type Amount = u64;
|
||||
|
||||
fn transfer(_: &AccountId, _: &AccountId, _: Self::Amount) -> Result<(), &'static str> { Ok(()) }
|
||||
fn remove_from(_: &AccountId, _: Self::Amount) -> Result<(), &'static str> { Ok(()) }
|
||||
fn add_to(_: &AccountId, _: Self::Amount) -> Result<(), &'static str> { Ok(()) }
|
||||
}
|
||||
|
||||
// Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted.
|
||||
#[derive(Clone, PartialEq, Eq, Debug)]
|
||||
pub struct Test;
|
||||
impl system::Trait for Test {
|
||||
type Origin = Origin;
|
||||
type Index = u64;
|
||||
type BlockNumber = u64;
|
||||
type Hash = H256;
|
||||
type Hashing = BlakeTwo256;
|
||||
type Digest = Digest;
|
||||
type AccountId = u64;
|
||||
type Lookup = IdentityLookup<u64>;
|
||||
type Header = Header;
|
||||
type Event = TestEvent;
|
||||
type Log = DigestItem;
|
||||
}
|
||||
impl Trait for Test {
|
||||
type Amount = u64;
|
||||
type Event =TestEvent;
|
||||
type TransferAsset = TransferAssetMock;
|
||||
}
|
||||
|
||||
pub type System = system::Module<Test>;
|
||||
pub type Fees = Module<Test>;
|
||||
|
||||
pub struct ExtBuilder {
|
||||
transaction_base_fee: u64,
|
||||
transaction_byte_fee: u64,
|
||||
}
|
||||
impl Default for ExtBuilder {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
transaction_base_fee: 0,
|
||||
transaction_byte_fee: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
impl ExtBuilder {
|
||||
pub fn transaction_base_fee(mut self, transaction_base_fee: u64) -> Self {
|
||||
self.transaction_base_fee = transaction_base_fee;
|
||||
self
|
||||
}
|
||||
pub fn transaction_byte_fee(mut self, transaction_byte_fee: u64) -> Self {
|
||||
self.transaction_byte_fee = transaction_byte_fee;
|
||||
self
|
||||
}
|
||||
pub fn build(self) -> runtime_io::TestExternalities<Blake2Hasher> {
|
||||
let mut t = system::GenesisConfig::<Test>::default().build_storage().unwrap().0;
|
||||
t.extend(GenesisConfig::<Test> {
|
||||
transaction_base_fee: self.transaction_base_fee,
|
||||
transaction_byte_fee: self.transaction_byte_fee,
|
||||
}.build_storage().unwrap().0);
|
||||
t.into()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,174 @@
|
||||
// Copyright 2019 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Substrate is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Tests for the module.
|
||||
|
||||
#![cfg(test)]
|
||||
|
||||
use super::*;
|
||||
use runtime_io::with_externalities;
|
||||
use runtime_primitives::traits::{OnFinalise};
|
||||
use system::{EventRecord, Phase};
|
||||
|
||||
use mock::{Fees, System, ExtBuilder};
|
||||
use srml_support::{assert_ok, assert_err};
|
||||
|
||||
#[test]
|
||||
fn charge_base_bytes_fee_should_work() {
|
||||
with_externalities(
|
||||
&mut ExtBuilder::default()
|
||||
.transaction_base_fee(3)
|
||||
.transaction_byte_fee(5)
|
||||
.build(),
|
||||
|| {
|
||||
System::set_extrinsic_index(0);
|
||||
assert_ok!(Fees::charge_base_bytes_fee(&0, 7));
|
||||
assert_eq!(Fees::current_transaction_fee(0), 3 + 5 * 7);
|
||||
|
||||
System::set_extrinsic_index(1);
|
||||
assert_ok!(Fees::charge_base_bytes_fee(&0, 11));
|
||||
assert_eq!(Fees::current_transaction_fee(1), 3 + 5 * 11);
|
||||
|
||||
System::set_extrinsic_index(3);
|
||||
assert_ok!(Fees::charge_base_bytes_fee(&0, 13));
|
||||
assert_eq!(Fees::current_transaction_fee(3), 3 + 5 * 13);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn charge_base_bytes_fee_should_not_work_if_bytes_fee_overflow() {
|
||||
// bytes fee overflows.
|
||||
with_externalities(
|
||||
&mut ExtBuilder::default()
|
||||
.transaction_base_fee(0)
|
||||
.transaction_byte_fee(u64::max_value())
|
||||
.build(),
|
||||
|| {
|
||||
System::set_extrinsic_index(0);
|
||||
assert_err!(
|
||||
Fees::charge_base_bytes_fee(&0, 2),
|
||||
"bytes fee overflow"
|
||||
);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn charge_base_bytes_fee_should_not_work_if_overall_fee_overflow() {
|
||||
// bytes fee doesn't overflow, but overall fee (bytes_fee + base_fee) does
|
||||
with_externalities(
|
||||
&mut ExtBuilder::default()
|
||||
.transaction_base_fee(u64::max_value())
|
||||
.transaction_byte_fee(1)
|
||||
.build(),
|
||||
|| {
|
||||
System::set_extrinsic_index(0);
|
||||
assert_err!(
|
||||
Fees::charge_base_bytes_fee(&0, 1),
|
||||
"bytes fee overflow"
|
||||
);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn charge_fee_should_work() {
|
||||
with_externalities(&mut ExtBuilder::default().build(), || {
|
||||
System::set_extrinsic_index(0);
|
||||
assert_ok!(Fees::charge_fee(&0, 2));
|
||||
assert_ok!(Fees::charge_fee(&0, 3));
|
||||
assert_eq!(Fees::current_transaction_fee(0), 2 + 3);
|
||||
|
||||
System::set_extrinsic_index(2);
|
||||
assert_ok!(Fees::charge_fee(&0, 5));
|
||||
assert_ok!(Fees::charge_fee(&0, 7));
|
||||
assert_eq!(Fees::current_transaction_fee(2), 5 + 7);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn charge_fee_when_overflow_should_not_work() {
|
||||
with_externalities(&mut ExtBuilder::default().build(), || {
|
||||
System::set_extrinsic_index(0);
|
||||
assert_ok!(Fees::charge_fee(&0, u64::max_value()));
|
||||
assert_err!(Fees::charge_fee(&0, 1), "fee got overflow after charge");
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn refund_fee_should_work() {
|
||||
with_externalities(&mut ExtBuilder::default().build(), || {
|
||||
System::set_extrinsic_index(0);
|
||||
assert_ok!(Fees::charge_fee(&0, 5));
|
||||
assert_ok!(Fees::refund_fee(&0, 3));
|
||||
assert_eq!(Fees::current_transaction_fee(0), 5 - 3);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn refund_fee_when_underflow_should_not_work() {
|
||||
with_externalities(&mut ExtBuilder::default().build(), || {
|
||||
System::set_extrinsic_index(0);
|
||||
assert_err!(Fees::refund_fee(&0, 1), "fee got underflow after refund");
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn on_finalise_should_work() {
|
||||
with_externalities(&mut ExtBuilder::default().build(), || {
|
||||
// charge fees in extrinsic index 3
|
||||
System::set_extrinsic_index(3);
|
||||
assert_ok!(Fees::charge_fee(&0, 1));
|
||||
System::note_applied_extrinsic(&Ok(()), 1);
|
||||
// charge fees in extrinsic index 5
|
||||
System::set_extrinsic_index(5);
|
||||
assert_ok!(Fees::charge_fee(&0, 1));
|
||||
System::note_applied_extrinsic(&Ok(()), 1);
|
||||
System::note_finished_extrinsics();
|
||||
|
||||
// `current_transaction_fee`, `extrinsic_count` should be as expected.
|
||||
assert_eq!(Fees::current_transaction_fee(3), 1);
|
||||
assert_eq!(Fees::current_transaction_fee(5), 1);
|
||||
assert_eq!(System::extrinsic_count(), 5 + 1);
|
||||
|
||||
<Fees as OnFinalise<u64>>::on_finalise(1);
|
||||
|
||||
// When finalised, `CurrentTransactionFee` records should be cleared.
|
||||
assert_eq!(Fees::current_transaction_fee(3), 0);
|
||||
assert_eq!(Fees::current_transaction_fee(5), 0);
|
||||
|
||||
// When finalised, if any fee charged in a extrinsic, a `Charged` event should be deposited
|
||||
// for it.
|
||||
let fee_charged_events: Vec<EventRecord<mock::TestEvent>> = System::events()
|
||||
.into_iter()
|
||||
.filter(|e| match e.event {
|
||||
mock::TestEvent::fees(RawEvent::Charged(_, _)) => return true,
|
||||
_ => return false,
|
||||
})
|
||||
.collect();
|
||||
assert_eq!(fee_charged_events, vec![
|
||||
EventRecord {
|
||||
phase: Phase::Finalization,
|
||||
event: RawEvent::Charged(3, 1).into(),
|
||||
},
|
||||
EventRecord {
|
||||
phase: Phase::Finalization,
|
||||
event: RawEvent::Charged(5, 1).into(),
|
||||
},
|
||||
]);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user