Skip to content

Commit

Permalink
Feature/add extra acceleration in tx (paritytech#185)
Browse files Browse the repository at this point in the history
* Initial commit

* Pass tests

* Apply check_fee

* Accelerable?

* Pass tests

* Clean up

* Update wasm

* Nits

* Fix issue that acc only exists in signed extrinsic

* Update wasm

* Don't use expect in runtime

* 2019

* Wasm

* fix trait params
  • Loading branch information
liuchengxu authored Jan 7, 2019
1 parent 770b8d2 commit 47523b2
Show file tree
Hide file tree
Showing 16 changed files with 836 additions and 26 deletions.
18 changes: 18 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ members = [
"cli",
"rpc",
"rpc-servers",
"xr-primitives",

# xrml
"xrml/xsystem",
Expand Down
3 changes: 3 additions & 0 deletions primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ pub type ChainId = u32;
/// Index of a transaction in the relay chain. 32-bit should be plenty.
pub type Index = u64;

/// Bigger Acceleration means more chances be to included in a block for a transaction.
pub type Acceleration = u32;

pub type Signature = runtime_primitives::Ed25519Signature;

/// A timestamp: seconds since the unix epoch.
Expand Down
4 changes: 4 additions & 0 deletions runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ srml-treasury = { git = "https://github.com/chainpool/substrate", default-featur
# chainx
chainx-primitives = { path = "../primitives", default-features = false }

xr-primitives = { path = "../xr-primitives", default-features = false }

# chainx runtime module
xrml-xsystem = { path = "../xrml/xsystem", default-features = false }
xrml-xaccounts = { path = "../xrml/xaccounts", default-features = false }
Expand Down Expand Up @@ -91,6 +93,8 @@ std = [
"srml-treasury/std",
# chainx
"chainx-primitives/std",
# xr-primitives
"xr-primitives/std",
# chainx runtime
"xrml-xsystem/std",
"xrml-xaccounts/std",
Expand Down
19 changes: 14 additions & 5 deletions runtime/src/fee.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
// Copyright 2018 Chainpool

//use balances::Call as BalancesCall;
use xassets::Call as XAssetsCall;
use xbitcoin::Call as XbitcoinCall;
use xprocess::Call as XAssetsProcessCall;
use xstaking::Call as XStakingCall;

use Acceleration;
use Call;

pub trait CheckFee {
fn check_fee(&self) -> Option<u64>;
fn check_fee(&self, acc: Acceleration) -> 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 {
/// Return fee_power, which is part of the total_fee.
/// total_fee = base_fee * fee_power + byte_fee * bytes
///
/// fee_power = power_per_call * acceleration
fn check_fee(&self, acc: Acceleration) -> Option<u64> {
let base_power = match self {
// xassets
Call::XAssets(call) => match call {
XAssetsCall::transfer(_, _, _, _) => Some(10),
Expand All @@ -35,6 +38,7 @@ impl CheckFee for Call {
XbitcoinCall::push_transaction(_) => Some(10),
_ => None,
},
// xmining
Call::XStaking(call) => match call {
XStakingCall::register(_, _, _, _, _, _) => Some(100),
XStakingCall::refresh(_, _) => Some(100),
Expand All @@ -45,6 +49,11 @@ impl CheckFee for Call {
_ => None,
},
_ => None,
};

match base_power {
Some(p) => Some(p * acc as u64),
None => None,
}
}
}
16 changes: 12 additions & 4 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
// `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;

#[cfg(test)]
extern crate serde;

#[macro_use]
extern crate parity_codec_derive;
extern crate parity_codec as codec;
Expand Down Expand Up @@ -41,6 +48,7 @@ extern crate srml_treasury as treasury;

// chainx
extern crate chainx_primitives;
extern crate xr_primitives;
pub extern crate xrml_xaccounts as xaccounts;
pub extern crate xrml_xsystem as xsystem;
// fee;
Expand Down Expand Up @@ -90,8 +98,7 @@ pub use timestamp::Call as TimestampCall;

// chainx
use chainx_primitives::{
AccountId, AccountIndex, Balance, BlockNumber, BlockProducer, Hash, Index, SessionKey,
Signature,
Acceleration, AccountId, AccountIndex, Balance, BlockNumber, Hash, Index, SessionKey, Signature,
};
// chainx runtime
// xassets
Expand Down Expand Up @@ -315,8 +322,9 @@ pub type Header = generic::Header<BlockNumber, BlakeTwo256, Log>;
pub type Block = generic::Block<Header, UncheckedExtrinsic>;
/// BlockId type as expected by this runtime.
pub type BlockId = generic::BlockId<Block>;
/// Unchecked extrinsic type as expected by this runtime.
pub type UncheckedExtrinsic = generic::UncheckedMortalExtrinsic<Address, Index, Call, Signature>;
/// Custom Unchecked extrinsic type as expected by this runtime.
pub type UncheckedExtrinsic =
xr_primitives::generic::UncheckedMortalExtrinsic<Address, Index, Call, Signature, Acceleration>;
/// Executive: handles dispatch to the various modules.
pub type Executive =
xexecutive::Executive<Runtime, Block, balances::ChainContext<Runtime>, XFeeManager, AllModules>;
Expand Down
Loading

0 comments on commit 47523b2

Please sign in to comment.