Skip to content

Commit

Permalink
Feature/refactor fee (paritytech#162)
Browse files Browse the repository at this point in the history
* Move xexecutive from xrml path to runtime path

* Move fee from xrml path to runtime

* Add CheckFee trait

* run ok

* Format

* Support runtime module test
  • Loading branch information
gguoss committed Dec 20, 2018
1 parent f82ad32 commit 1b3df86
Show file tree
Hide file tree
Showing 12 changed files with 69 additions and 173 deletions.
30 changes: 1 addition & 29 deletions Cargo.lock

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

2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@ members = [
# xrml
"xrml/xsystem",
"xrml/xsupport",
"xrml/xexecutive",
# xrml/xfee
"xrml/xfee/manager",
"xrml/xfee/config",
# xrml/assets
"xrml/xassets/assets",
]
3 changes: 1 addition & 2 deletions runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ substrate-consensus-aura-primitives = { git = "https://github.com/chainpool/subs
sr-primitives = { git = "https://github.com/chainpool/substrate" }
sr-version = { git = "https://github.com/chainpool/substrate" }
sr-std = { git = "https://github.com/chainpool/substrate" }
sr-io = { git = "https://github.com/chainpool/substrate" }

srml-support = { git = "https://github.com/chainpool/substrate" }
srml-system = { git = "https://github.com/chainpool/substrate" }
Expand All @@ -39,7 +40,6 @@ chainx-primitives = { path = "../primitives" }

# chainx runtime module
xrml-xsystem = { path = "../xrml/xsystem" }
xrml-executive = { path = "../xrml/xexecutive" }
# fee
xrml-fee-manager = { path = "../xrml/xfee/manager" }
# assets
Expand Down Expand Up @@ -77,7 +77,6 @@ std = [
"chainx-primitives/std",
# chainx runtime
"xrml-xsystem/std",
"xrml-executive/std",
# fee
"xrml-fee-manager/std",
# asset
Expand Down
21 changes: 21 additions & 0 deletions runtime/src/fee.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2018 Chainpool

use balances::Call as BalancesCall;
use Call;

pub trait CheckFee {
fn check_fee(&self) -> Option<u64>;
}

impl CheckFee for Call {
fn check_fee(&self) -> Option<u64> {
// ret fee_power, total_fee = base_fee * fee_power + byte_fee * bytes
match self {
Call::Balances(call) => match call {
BalancesCall::transfer(_, _) => Some(1),
_ => None,
},
_ => None,
}
}
}
15 changes: 9 additions & 6 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
// `construct_runtime!` does a lot of recursion and requires us to increase the limit to 256.
#![recursion_limit = "256"]

#[cfg(test)]
#[macro_use]
extern crate hex_literal;

extern crate parity_codec as codec;
#[macro_use]
extern crate parity_codec_derive;
Expand All @@ -14,6 +18,7 @@ extern crate serde;

#[macro_use]
extern crate substrate_client as client;
extern crate sr_io as runtime_io;
extern crate substrate_consensus_aura_primitives as consensus_aura;
extern crate substrate_primitives as primitives;

Expand Down Expand Up @@ -42,24 +47,22 @@ extern crate srml_treasury as treasury;

// chainx
extern crate chainx_primitives;
// chainx runtime module
extern crate xrml_executive as xexective;
extern crate xrml_xsystem as xsystem;
// fee;
extern crate xrml_fee_manager as fee_manager;
// assets;
extern crate xrml_xassets_assets as xassets;

mod fee;
mod xexecutive;

pub use balances::address::Address as RawAddress;
use consensus_aura::api as aura_api;
pub use runtime_primitives::{Perbill, Permill};

#[cfg(feature = "std")]
use council::{motions as council_motions, voting as council_voting};
use grandpa::fg_primitives::{self, ScheduledChange};
use rstd::prelude::*;

use primitives::u32_trait::{_2, _4};
use primitives::OpaqueMetadata;

use runtime_primitives::generic;
Expand Down Expand Up @@ -258,7 +261,7 @@ pub type BlockId = generic::BlockId<Block>;
pub type UncheckedExtrinsic = generic::UncheckedMortalExtrinsic<Address, Index, Call, Signature>;
/// Executive: handles dispatch to the various modules.
pub type Executive =
xexective::Executive<Runtime, Block, balances::ChainContext<Runtime>, XFeeManager, AllModules>;
xexecutive::Executive<Runtime, Block, balances::ChainContext<Runtime>, XFeeManager, AllModules>;

// define tokenbalances module type
//pub type TokenBalance = u128;
Expand Down
87 changes: 34 additions & 53 deletions xrml/xexecutive/src/lib.rs → runtime/src/xexecutive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,49 +17,22 @@

//! Executive: Handles all of the top-level stuff; essentially just executing blocks/extrinsics.

#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(test)]
#[macro_use]
extern crate parity_codec_derive;

#[cfg_attr(test, macro_use)]
extern crate srml_support as runtime_support;

extern crate parity_codec as codec;
extern crate sr_io as runtime_io;
extern crate sr_primitives as primitives;
#[cfg_attr(not(feature = "std"), macro_use)]
extern crate sr_std as rstd;
extern crate srml_system as system;

#[cfg(test)]
#[macro_use]
extern crate hex_literal;

#[cfg(test)]
extern crate substrate_primitives;

#[cfg(test)]
extern crate srml_balances as balances;
extern crate xrml_fee_config;
extern crate xrml_fee_manager;

use codec::{Codec, Encode};
use primitives::traits::{
self, Applyable, As, CheckEqual, Checkable, Digest, Hash, Header, Member, OnFinalise, One, Zero,
};
use primitives::transaction_validity::{
TransactionLongevity, TransactionPriority, TransactionValidity,
};
use primitives::{ApplyError, ApplyOutcome};
use fee::CheckFee;
use fee_manager::MakePayment;
use rstd::marker::PhantomData;
use rstd::prelude::*;
use rstd::result;
use runtime_support::Dispatchable;
use runtime_io;
use runtime_primitives::traits::{
self, Applyable, As, CheckEqual, Checkable, Digest, Hash, Header, OnFinalise, One, Zero,
};
use runtime_primitives::transaction_validity::{
TransactionLongevity, TransactionPriority, TransactionValidity,
};
use runtime_primitives::{ApplyError, ApplyOutcome};
use srml_support::Dispatchable;
use system::extrinsics_root;
use xrml_fee_config::FeeMap;
use xrml_fee_manager::MakePayment;

mod internal {
pub enum ApplyError {
Expand Down Expand Up @@ -88,7 +61,7 @@ impl<
> Executive<System, Block, Context, Payment, Finalisation> 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 + Member,
<<Block::Extrinsic as Checkable<Context>>::Checked as Applyable>::Call: Dispatchable + CheckFee,
<<<Block::Extrinsic as Checkable<Context>>::Checked as Applyable>::Call as Dispatchable>::Origin: From<Option<System::AccountId>>
{
/// Start the execution of a particular block.
Expand Down Expand Up @@ -184,9 +157,8 @@ impl<
}

let (f, s) = xt.deconstruct();
let fee_map = FeeMap::<<<Block::Extrinsic as Checkable<Context>>::Checked as Applyable>::Call>{_marker: Default::default(),};
if let Some(pay) = fee_map.find(&f) {
Payment::make_payment(&s.clone().unwrap(), encoded_len, pay).map_err(|_| internal::ApplyError::CantPay)?;
if let Some(fee_power) = f.check_fee() {
Payment::make_payment(&s.clone().unwrap(), encoded_len, fee_power).map_err(|_| internal::ApplyError::CantPay)?;

// AUDIT: Under no circumstances may this function panic from here onwards.

Expand Down Expand Up @@ -270,9 +242,8 @@ impl<
};

let (f, s) = xt.deconstruct();
let fee_map = FeeMap::<<<Block::Extrinsic as Checkable<Context>>::Checked as Applyable>::Call>{_marker: Default::default(),};
if let Some(pay) = fee_map.find(&f) {
if Payment::make_payment(&s.clone().unwrap(), encoded_len, pay).is_err() {
if let Some(fee_power) = f.check_fee() {
if Payment::make_payment(&s.clone().unwrap(), encoded_len, fee_power).is_err() {
return TransactionValidity::Invalid
} else {
return valid
Expand All @@ -287,11 +258,11 @@ impl<
mod tests {
use super::*;
use balances::Call;
use primitives::testing::{Block, Digest, DigestItem, Header};
use primitives::traits::{BlakeTwo256, Header as HeaderT};
use primitives::BuildStorage;
use primitives::{Blake2Hasher, H256};
use runtime_io::with_externalities;
use substrate_primitives::{Blake2Hasher, H256};
use runtime_primitives::testing::{Block, Digest, DigestItem, Header};
use runtime_primitives::traits::{BlakeTwo256, Header as HeaderT};
use runtime_primitives::BuildStorage;
use system;

impl_outer_origin! {
Expand Down Expand Up @@ -327,13 +298,21 @@ mod tests {
type EnsureAccountLiquid = ();
type Event = MetaEvent;
}
impl fee_manager::Trait for Runtime {}

impl CheckFee for Call<Runtime> {
fn check_fee(&self) -> Option<u64> {
// ret fee_power, total_fee = base_fee * fee_power + byte_fee * bytes
Some(1)
}
}

type TestXt = primitives::testing::TestXt<Call<Runtime>>;
type TestXt = runtime_primitives::testing::TestXt<Call<Runtime>>;
type Executive = super::Executive<
Runtime,
Block<TestXt>,
balances::ChainContext<Runtime>,
balances::Module<Runtime>,
fee_manager::Module<Runtime>,
(),
>;

Expand All @@ -357,7 +336,8 @@ mod tests {
.unwrap()
.0,
);
let xt = primitives::testing::TestXt(Some(1), 0, Call::transfer(2.into(), 69.into()));
let xt =
runtime_primitives::testing::TestXt(Some(1), 0, Call::transfer(2.into(), 69.into()));
let mut t = runtime_io::TestExternalities::<Blake2Hasher>::new(t);
with_externalities(&mut t, || {
Executive::initialise_block(&Header::new(
Expand Down Expand Up @@ -452,7 +432,8 @@ mod tests {
#[test]
fn bad_extrinsic_not_inserted() {
let mut t = new_test_ext();
let xt = primitives::testing::TestXt(Some(1), 42, Call::transfer(33.into(), 69.into()));
let xt =
runtime_primitives::testing::TestXt(Some(1), 42, Call::transfer(33.into(), 69.into()));
with_externalities(&mut t, || {
Executive::initialise_block(&Header::new(
1,
Expand Down
36 changes: 0 additions & 36 deletions xrml/xexecutive/Cargo.toml

This file was deleted.

Empty file removed xrml/xfee/config/.gitkeep
Empty file.
15 changes: 0 additions & 15 deletions xrml/xfee/config/Cargo.toml

This file was deleted.

23 changes: 0 additions & 23 deletions xrml/xfee/config/src/lib.rs

This file was deleted.

Loading

0 comments on commit 1b3df86

Please sign in to comment.