Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
85a1953
wip
xlc Jan 31, 2019
274801f
Split bytes fee charging and charging by amount into different traits.
shaunxw Jan 31, 2019
eccd12c
Move to edition 2018.
shaunxw Feb 1, 2019
81a3929
Implemented charge fee traits for fees module.
shaunxw Feb 1, 2019
5923d01
Implemented 'on_finalise' for fee module.
shaunxw Feb 4, 2019
dd504f8
Merge branch 'master' into fee
shaunxw Feb 4, 2019
50f2cd5
Updated fees finalize impl.
shaunxw Feb 6, 2019
d9b951a
Renaming and documentation update.
shaunxw Feb 6, 2019
02f6358
Added overflow & underflow check for fee calculation.
shaunxw Feb 7, 2019
25cbd29
Added mock and unit tests for fee module.
shaunxw Feb 7, 2019
13c31be
More unit tests for fees module.
shaunxw Feb 8, 2019
df8d4d1
Fixed srml-executive unit tests.
shaunxw Feb 8, 2019
f4ef7b5
Merge branch 'master' into fee
shaunxw Feb 8, 2019
ba58808
Remove transaction base/bytes fee from balances module, fix unit tests.
shaunxw Feb 9, 2019
b12405a
Merge remote-tracking branch 'upstream/master' into fee
xlc Feb 11, 2019
b23cd67
fix compile error
xlc Feb 11, 2019
544e18c
Merge branch 'fee' of github.com:xlc/substrate into fee
shaunxw Feb 11, 2019
670f431
Fixed unit test.
shaunxw Feb 12, 2019
a9a948a
Minor fixes.
shaunxw Feb 12, 2019
3b9faa4
Bump spec version.
shaunxw Feb 12, 2019
b420972
Merge branch 'master' into fee
shaunxw Feb 12, 2019
857d3dc
Bump spec version.
shaunxw Feb 12, 2019
d84c199
Updated fees module and runtime wasm.
shaunxw Feb 12, 2019
4908fbb
Fees module code style improvement; updated runtime wasm.
shaunxw Feb 13, 2019
a0af48d
Merge branch 'master' into fee
shaunxw Feb 13, 2019
70a14dc
Bump spec and impl version.
shaunxw Feb 13, 2019
4e6c4f8
Merge branch 'master' into fee
shaunxw Feb 13, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fees module code style improvement; updated runtime wasm.
  • Loading branch information
shaunxw committed Feb 13, 2019
commit 4908fbbfb0f9a405dfb7acc6c7f546509c3c33e2
Binary file not shown.
Binary file not shown.
44 changes: 14 additions & 30 deletions srml/fees/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2017-2018 Parity Technologies (UK) Ltd.
// Copyright 2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.

// Substrate is free software: you can redistribute it and/or modify
Expand All @@ -20,8 +20,10 @@
#![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 runtime_primitives::traits::{
As, Member, SimpleArithmetic, ChargeBytesFee, ChargeFee,
TransferAsset, CheckedAdd, CheckedSub, CheckedMul, Zero
};
use system;

mod mock;
Expand All @@ -46,9 +48,9 @@ decl_module! {
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.clone());
let fee = <CurrentTransactionFee<T>>::take(index);
if !fee.is_zero() {
Self::deposit_event(RawEvent::Charged(index.clone(), fee));
Self::deposit_event(RawEvent::Charged(index, fee));
}
});
}
Expand Down Expand Up @@ -79,16 +81,10 @@ decl_storage! {

impl<T: Trait> ChargeBytesFee<T::AccountId> for Module<T> {
fn charge_base_bytes_fee(transactor: &T::AccountId, encoded_len: usize) -> Result {
let bytes_fee = match Self::transaction_byte_fee().checked_mul(
let bytes_fee = Self::transaction_byte_fee().checked_mul(
&<T::Amount as As<u64>>::sa(encoded_len as u64)
) {
Some(f) => f,
None => return Err("bytes fee overflow"),
};
let overall = match Self::transaction_base_fee().checked_add(&bytes_fee) {
Some(f) => f,
None => return Err("bytes fee overflow"),
};
).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)
}
}
Expand All @@ -97,15 +93,9 @@ 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 = match <system::Module<T>>::extrinsic_index() {
Some(i) => i,
None => return Err("no extrinsic index found"),
};
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 = match current_fee.checked_add(&amount) {
Some(f) => f,
None => return Err("fee got overflow after charge"),
};
let new_fee = current_fee.checked_add(&amount).ok_or_else(|| "fee got overflow after charge")?;

T::TransferAsset::remove_from(transactor, amount)?;

Expand All @@ -114,15 +104,9 @@ impl<T: Trait> ChargeFee<T::AccountId> for Module<T> {
}

fn refund_fee(transactor: &T::AccountId, amount: T::Amount) -> Result {
let extrinsic_index = match <system::Module<T>>::extrinsic_index() {
Some(i) => i,
None => return Err("no extrinsic index found"),
};
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 = match current_fee.checked_sub(&amount) {
Some(f) => f,
None => return Err("fee got underflow after refund"),
};
let new_fee = current_fee.checked_sub(&amount).ok_or_else(|| "fee got underflow after refund")?;

T::TransferAsset::add_to(transactor, amount)?;

Expand Down
2 changes: 1 addition & 1 deletion srml/fees/src/mock.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2018 Parity Technologies (UK) Ltd.
// Copyright 2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.

// Substrate is free software: you can redistribute it and/or modify
Expand Down
2 changes: 1 addition & 1 deletion srml/fees/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2017-2018 Parity Technologies (UK) Ltd.
// Copyright 2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.

// Substrate is free software: you can redistribute it and/or modify
Expand Down