Skip to content

Commit

Permalink
storage fee (paritytech#675)
Browse files Browse the repository at this point in the history
* storage fee

* provide root call for set fee

* simplify the code in fee.rs

* Wrap method matching by introducing a marco

* Refactoring a bit

* Use pattern guards in match patterns

* .

* Nit

* Nits

* Adjust the method weight finely

* Nit

* Update wasm
  • Loading branch information
atenjin authored Jun 16, 2019
1 parent 3efd334 commit 34ea817
Show file tree
Hide file tree
Showing 10 changed files with 7,488 additions and 93 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

Binary file modified cli/src/chainx_runtime.compact.wasm
Binary file not shown.
197 changes: 111 additions & 86 deletions runtime/src/fee.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
// Copyright 2018-2019 Chainpool.
use rstd::collections::btree_map::BTreeMap;

use xr_primitives::XString;

use xfee_manager::SwitchStore;

Expand All @@ -15,108 +18,130 @@ use xtokens::Call as XTokensCall;
use crate::Call;

pub trait CheckFee {
fn check_fee(&self, switch: SwitchStore) -> Option<u64>;
fn check_fee(
&self,
switch: SwitchStore,
method_weight_map: BTreeMap<XString, u64>,
) -> Option<u64>;
}

impl CheckFee for Call {
/// Return fee_power, which is part of the total_fee.
/// total_fee = base_fee * fee_power + byte_fee * bytes
///
/// fee_power = power_per_call
fn check_fee(&self, switch: SwitchStore) -> Option<u64> {
// must allow
let first_check = match self {
Call::XMultiSig(call) => match call {
// XMultiSigCall::deploy(_, _) => Some(1000),
XMultiSigCall::execute(_, _) => Some(50),
XMultiSigCall::confirm(_, _) => Some(25),
XMultiSigCall::remove_multi_sig_for(_, _) => Some(1000),
_ => None,
},
_ => None,
};

// hit
if first_check.is_some() {
return first_check;
fn check_fee(
&self,
switch: SwitchStore,
method_weight_map: BTreeMap<XString, u64>,
) -> Option<u64> {
// MultiSigCall is on the top priority and can't be forbidden.
if let Call::XMultiSig(call) = self {
match call {
XMultiSigCall::execute(..) => return Some(50),
XMultiSigCall::confirm(..) => return Some(25),
XMultiSigCall::remove_multi_sig_for(..) => return Some(1000),
_ => (),
}
}

// Check if a certain emergency switch is on.
if switch.global {
return None;
};

let base_power = match self {
// xassets
Call::XAssets(call) => match call {
XAssetsCall::transfer(_, _, _, _) => Some(1),
_ => None,
},
Call::XAssetsProcess(call) => match call {
XAssetsProcessCall::withdraw(_, _, _, _) => Some(3),
XAssetsProcessCall::revoke_withdraw(_) => Some(10),
_ => None,
},
// xbridge
Call::XBridgeOfBTC(call) => {
let power = if switch.xbtc {
None
} else {
match call {
XBitcoinCall::push_header(_) => Some(10),
XBitcoinCall::push_transaction(_) => Some(8),
XBitcoinCall::create_withdraw_tx(_, _) => Some(5),
XBitcoinCall::sign_withdraw_tx(_) => Some(5),
_ => None,
}
};
power
match self {
Call::XSpot(..) if switch.spot => {
return None;
}
Call::XBridgeOfSDOT(call) => {
let power = if switch.sdot {
None
} else {
match call {
SdotCall::claim(_, _, _) => Some(2),
_ => None,
}
};
power
Call::XBridgeOfBTC(..) if switch.xbtc => {
return None;
}
Call::XBridgeFeatures(call) => match call {
XBridgeFeaturesCall::setup_bitcoin_trustee(_, _, _) => Some(1000),
_ => None,
},
// xmining
Call::XStaking(call) => match call {
XStakingCall::register(_) => Some(1_000_000),
XStakingCall::refresh(_, _, _, _) => Some(1000),
XStakingCall::nominate(_, _, _) => Some(5),
XStakingCall::unnominate(_, _, _) => Some(3),
XStakingCall::renominate(_, _, _, _) => Some(8),
XStakingCall::unfreeze(_, _) => Some(2),
XStakingCall::claim(_) => Some(30),

_ => None,
},
Call::XTokens(call) => match call {
XTokensCall::claim(_) => Some(3),
_ => None,
},
Call::XSpot(call) => {
let power = if switch.spot {
None
} else {
match call {
XSpotCall::put_order(_, _, _, _, _) => Some(8),
XSpotCall::cancel_order(_, _) => Some(2),
_ => None,
}
};
power
Call::XBridgeOfSDOT(..) if switch.sdot => {
return None;
}
_ => (),
}

_ => None,
};
base_power
macro_rules! get_method_call_weight {
($module:ty, $func:ty, $default:expr) => {
{
let method_weight_key = stringify!($module $func).as_bytes().to_vec();
let method_weight = method_weight_map.get(&method_weight_key);
Some(method_weight.map(|x| *x).unwrap_or($default))
}
};
}

macro_rules! match_method_call {
(
$(
$module:ident, $module_call:ident => (
$(
$method:ident : $default:expr,
)+
);
)+
) => {
match self {
$(
Call::$module(call) => match call {
$(
$module_call::$method(..) => get_method_call_weight!($module, $method, $default),
)+
_ => None,
},
)+
_ => None,
}
};
}

match_method_call! {

XAssets, XAssetsCall => (
transfer : 1,
);

XAssetsProcess, XAssetsProcessCall => (
withdraw : 3,
revoke_withdraw : 10,
);

XBridgeOfBTC, XBitcoinCall => (
push_header : 10,
push_transaction : 8,
sign_withdraw_tx : 5,
create_withdraw_tx : 5,
);

XStaking, XStakingCall => (
claim : 3,
refresh : 10_000,
nominate : 5,
unfreeze : 2,
register : 100_000,
unnominate : 3,
renominate : 800,
);

XTokens, XTokensCall => (
claim : 3,
);

XSpot, XSpotCall => (
put_order : 8,
cancel_order : 2,
);

XBridgeOfSDOT, SdotCall => (
claim : 2,
);

XBridgeFeatures, XBridgeFeaturesCall => (
setup_bitcoin_trustee : 1000,
);

}
}
}
5 changes: 3 additions & 2 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,8 +444,9 @@ impl_runtime_apis! {
};

let switch = xfee_manager::SwitchStore::default();
call.check_fee(switch).map(|power|
XFeeManager::transaction_fee(power, encoded_len)
let method_call_weight = XFeeManager::method_call_weight();
call.check_fee(switch, method_call_weight).map(|weight|
XFeeManager::transaction_fee(weight, encoded_len)
)
}
}
Expand Down
Loading

0 comments on commit 34ea817

Please sign in to comment.