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
Merge branch 'master' into fee
  • Loading branch information
shaunxw committed Feb 8, 2019
commit f4ef7b526ac884ea4ecdbe9f98a11771d9723fab
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

116 changes: 115 additions & 1 deletion srml/balances/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use runtime_support::{StorageValue, StorageMap, Parameter};
use runtime_support::traits::{UpdateBalanceOutcome, Currency, EnsureAccountLiquid, OnFreeBalanceZero};
use runtime_support::dispatch::Result;
use primitives::traits::{Zero, SimpleArithmetic, TransferAsset,
As, StaticLookup, Member, CheckedAdd, CheckedSub};
As, StaticLookup, Member, CheckedAdd, CheckedSub, MaybeSerializeDebug};
use system::{IsDeadAccount, OnNewAccount, ensure_signed};

mod mock;
Expand Down Expand Up @@ -337,6 +337,120 @@ impl<T: Trait> Module<T> {
}
}

impl<T: Trait> Currency<T::AccountId> for Module<T>
where
T::Balance: MaybeSerializeDebug
{
type Balance = T::Balance;

fn total_balance(who: &T::AccountId) -> Self::Balance {
Self::free_balance(who) + Self::reserved_balance(who)
}

fn can_slash(who: &T::AccountId, value: Self::Balance) -> bool {
Self::free_balance(who) >= value
}

fn can_reserve(who: &T::AccountId, value: Self::Balance) -> bool {
if T::EnsureAccountLiquid::ensure_account_liquid(who).is_ok() {
Self::free_balance(who) >= value
} else {
false
}
}

fn total_issuance() -> Self:: Balance {
Self::total_issuance()
}

fn free_balance(who: &T::AccountId) -> Self::Balance {
Self::free_balance(who)
}

fn reserved_balance(who: &T::AccountId) -> Self::Balance {
Self::reserved_balance(who)
}

fn slash(who: &T::AccountId, value: Self::Balance) -> Option<Self::Balance> {
let free_balance = Self::free_balance(who);
let free_slash = cmp::min(free_balance, value);
Self::set_free_balance(who, free_balance - free_slash);
Self::decrease_total_stake_by(free_slash);
if free_slash < value {
Self::slash_reserved(who, value - free_slash)
} else {
None
}
}

fn reward(who: &T::AccountId, value: Self::Balance) -> result::Result<(), &'static str> {
if Self::total_balance(who).is_zero() {
return Err("beneficiary account must pre-exist");
}
Self::set_free_balance(who, Self::free_balance(who) + value);
Self::increase_total_stake_by(value);
Ok(())
}

fn increase_free_balance_creating(who: &T::AccountId, value: Self::Balance) -> UpdateBalanceOutcome {
Self::set_free_balance_creating(who, Self::free_balance(who) + value)
}

fn reserve(who: &T::AccountId, value: Self::Balance) -> result::Result<(), &'static str> {
let b = Self::free_balance(who);
if b < value {
return Err("not enough free funds")
}
T::EnsureAccountLiquid::ensure_account_liquid(who)?;
Self::set_reserved_balance(who, Self::reserved_balance(who) + value);
Self::set_free_balance(who, b - value);
Ok(())
}

fn unreserve(who: &T::AccountId, value: Self::Balance) -> Option<Self::Balance> {
let b = Self::reserved_balance(who);
let actual = cmp::min(b, value);
Self::set_free_balance(who, Self::free_balance(who) + actual);
Self::set_reserved_balance(who, b - actual);
if actual == value {
None
} else {
Some(value - actual)
}
}

fn slash_reserved(who: &T::AccountId, value: Self::Balance) -> Option<Self::Balance> {
let b = Self::reserved_balance(who);
let slash = cmp::min(b, value);
Self::set_reserved_balance(who, b - slash);
Self::decrease_total_stake_by(slash);
if value == slash {
None
} else {
Some(value - slash)
}
}

fn repatriate_reserved(
slashed: &T::AccountId,
beneficiary: &T::AccountId,
value: Self::Balance
) -> result::Result<Option<Self::Balance>, &'static str> {
if Self::total_balance(beneficiary).is_zero() {
return Err("beneficiary account must pre-exist");
}
let b = Self::reserved_balance(slashed);
let slash = cmp::min(b, value);
Self::set_free_balance(beneficiary, Self::free_balance(beneficiary) + slash);
Self::set_reserved_balance(slashed, b - slash);
if value == slash {
Ok(None)
} else {
Ok(Some(value - slash))
}
}
}

impl<T: Trait> TransferAsset<T::AccountId> for Module<T> {
type Amount = T::Balance;

Expand Down
6 changes: 3 additions & 3 deletions srml/executive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ use rstd::prelude::*;
use rstd::marker::PhantomData;
use rstd::result;
use primitives::traits::{self, Header, Zero, One, Checkable, Applyable, CheckEqual, OnFinalise,
ChargeBytesFee, Hash, As, Digest};
OnInitialise, ChargeBytesFee, Hash, As, Digest};
use runtime_support::Dispatchable;
use codec::{Codec, Encode};
use system::extrinsics_root;
Expand Down Expand Up @@ -83,8 +83,8 @@ impl<
System: system::Trait,
Block: traits::Block<Header=System::Header, Hash=System::Hash>,
Payment: ChargeBytesFee<System::AccountId>,
Finalisation: OnFinalise<System::BlockNumber>,
> Executive<System, Block, Context, Payment, Finalisation> where
AllModules: OnInitialise<System::BlockNumber> + OnFinalise<System::BlockNumber>,
> Executive<System, Block, Context, Payment, AllModules> where
Block::Extrinsic: Checkable<Context> + Codec,
<Block::Extrinsic as Checkable<Context>>::Checked: Applyable<Index=System::Index, AccountId=System::AccountId>,
<<Block::Extrinsic as Checkable<Context>>::Checked as Applyable>::Call: Dispatchable,
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.