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:
Xiliang Chen
2019-02-15 23:21:38 +13:00
committed by Bastian Köcher
parent 6a6c3155a6
commit fafffdb771
31 changed files with 672 additions and 90 deletions
+6 -5
View File
@@ -78,6 +78,7 @@ use srml_support::traits::OnFreeBalanceZero;
use system::{ensure_signed, RawOrigin};
use runtime_io::{blake2_256, twox_128};
use timestamp;
use fees;
pub type CodeHash<T> = <T as system::Trait>::Hash;
@@ -91,7 +92,7 @@ pub trait ComputeDispatchFee<Call, Balance> {
fn compute_dispatch_fee(call: &Call) -> Balance;
}
pub trait Trait: balances::Trait + timestamp::Trait {
pub trait Trait: fees::Trait + balances::Trait + timestamp::Trait {
/// The outer call dispatch type.
type Call: Parameter + Dispatchable<Origin=<Self as system::Trait>::Origin>;
@@ -135,14 +136,14 @@ where
}
/// The default dispatch fee computor computes the fee in the same way that
/// implementation of `MakePayment` for balances module does.
/// implementation of `ChargeBytesFee` for fees module does.
pub struct DefaultDispatchFeeComputor<T: Trait>(PhantomData<T>);
impl<T: Trait> ComputeDispatchFee<T::Call, T::Balance> for DefaultDispatchFeeComputor<T> {
fn compute_dispatch_fee(call: &T::Call) -> T::Balance {
let encoded_len = parity_codec::Encode::encode(&call).len();
let base_fee = <balances::Module<T>>::transaction_base_fee();
let byte_fee = <balances::Module<T>>::transaction_byte_fee();
base_fee + byte_fee * <T::Balance as As<u64>>::sa(encoded_len as u64)
let base_fee = <fees::Module<T>>::transaction_base_fee();
let byte_fee = <fees::Module<T>>::transaction_byte_fee();
<T::Balance as As<u64>>::sa(base_fee.as_() + byte_fee.as_() * encoded_len as u64)
}
}
+7 -3
View File
@@ -27,6 +27,7 @@ use runtime_io;
use srml_support::{StorageMap, StorageDoubleMap, assert_ok, impl_outer_event, impl_outer_dispatch, impl_outer_origin};
use substrate_primitives::{Blake2Hasher};
use system::{self, Phase, EventRecord};
use fees;
use {wabt, balances, consensus};
use hex_literal::*;
use assert_matches::assert_matches;
@@ -44,7 +45,7 @@ mod contract {
}
impl_outer_event! {
pub enum MetaEvent for Test {
balances<T>, contract<T>,
balances<T>, contract<T>, fees<T>,
}
}
impl_outer_origin! {
@@ -88,6 +89,11 @@ impl consensus::Trait for Test {
type SessionKey = UintAuthorityId;
type InherentOfflineReport = ();
}
impl fees::Trait for Test {
type Event = MetaEvent;
type Amount = u64;
type TransferAsset = Balances;
}
impl Trait for Test {
type Call = Call;
type Gas = u64;
@@ -165,8 +171,6 @@ impl ExtBuilder {
t.extend(
balances::GenesisConfig::<Test> {
balances: vec![],
transaction_base_fee: 0,
transaction_byte_fee: 0,
existential_deposit: self.existential_deposit,
transfer_fee: self.transfer_fee,
creation_fee: self.creation_fee,