Skip to content

Commit bd4ccd4

Browse files
authored
Add SwitchStore (paritytech#282)
1 parent efd461c commit bd4ccd4

File tree

10 files changed

+70
-25
lines changed

10 files changed

+70
-25
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cli/src/genesis_config.rs

-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,6 @@ pub fn testnet_genesis(genesis_spec: GenesisSpec) -> GenesisConfig {
191191
banned_account: auth1.into(),
192192
}),
193193
fee_manager: Some(XFeeManagerConfig {
194-
switch: false,
195194
producer_fee_proportion: (1, 10),
196195
_genesis_phantom_data: Default::default(),
197196
}),

rpc/src/chainx/impl_rpc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,7 @@ where
776776
let call_params = if let Ok(hex_call) = hex::decode(&call_params[2..]) {
777777
hex_call
778778
} else {
779-
return Err(HexDecodeErr.into())
779+
return Err(HexDecodeErr.into());
780780
};
781781
let call: Call = if let Some(call) = Decode::decode(&mut call_params.as_slice()) {
782782
call

runtime/src/fee.rs

+43-18
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
//use balances::Call as BalancesCall;
44
use bitcoin::Call as BitcoinCall;
5+
use fee_manager::SwitchStore;
56
use sdot::Call as SdotCall;
67
use sudo::Call as SudoCall;
78
use xassets::Call as XAssetsCall;
@@ -13,15 +14,18 @@ use xtokens::Call as XTokensCall;
1314
use Call;
1415

1516
pub trait CheckFee {
16-
fn check_fee(&self) -> Option<u64>;
17+
fn check_fee(&self, switch: SwitchStore) -> Option<u64>;
1718
}
1819

1920
impl CheckFee for Call {
2021
/// Return fee_power, which is part of the total_fee.
2122
/// total_fee = base_fee * fee_power + byte_fee * bytes
2223
///
2324
/// fee_power = power_per_call
24-
fn check_fee(&self) -> Option<u64> {
25+
fn check_fee(&self, switch: SwitchStore) -> Option<u64> {
26+
if switch.global {
27+
return None;
28+
};
2529
let base_power = match self {
2630
// xassets
2731
Call::XAssets(call) => match call {
@@ -37,13 +41,20 @@ impl CheckFee for Call {
3741
_ => None,
3842
},
3943
// xbridge
40-
Call::XBridgeOfBTC(call) => match call {
41-
BitcoinCall::push_header(_) => Some(20),
42-
BitcoinCall::push_transaction(_) => Some(10),
43-
BitcoinCall::create_withdraw_tx(_, _) => Some(50),
44-
BitcoinCall::sign_withdraw_tx(_, _) => Some(50),
45-
_ => None,
46-
},
44+
Call::XBridgeOfBTC(call) => {
45+
let power = if switch.xbtc {
46+
None
47+
} else {
48+
match call {
49+
BitcoinCall::push_header(_) => Some(20),
50+
BitcoinCall::push_transaction(_) => Some(10),
51+
BitcoinCall::create_withdraw_tx(_, _) => Some(50),
52+
BitcoinCall::sign_withdraw_tx(_, _) => Some(50),
53+
_ => None,
54+
}
55+
};
56+
power
57+
}
4758
// xmining
4859
Call::XStaking(call) => match call {
4960
XStakingCall::register(_) => Some(100),
@@ -59,20 +70,34 @@ impl CheckFee for Call {
5970
XTokensCall::claim(_) => Some(3),
6071
_ => None,
6172
},
62-
Call::XSpot(call) => match call {
63-
XSpotCall::put_order(_, _, _, _, _) => Some(8),
64-
XSpotCall::cancel_order(_, _) => Some(2),
65-
_ => None,
66-
},
73+
Call::XSpot(call) => {
74+
let power = if switch.spot {
75+
None
76+
} else {
77+
match call {
78+
XSpotCall::put_order(_, _, _, _, _) => Some(8),
79+
XSpotCall::cancel_order(_, _) => Some(2),
80+
_ => None,
81+
}
82+
};
83+
power
84+
}
6785
Call::Sudo(call) => match call {
6886
SudoCall::sudo(_) => Some(1),
6987
SudoCall::set_key(_) => Some(1),
7088
_ => None,
7189
},
72-
Call::XBridgeOfSDOT(call) => match call {
73-
SdotCall::claim(_, _, _) => Some(1),
74-
_ => None,
75-
},
90+
Call::XBridgeOfSDOT(call) => {
91+
let power = if switch.sdot {
92+
None
93+
} else {
94+
match call {
95+
SdotCall::claim(_, _, _) => Some(10),
96+
_ => None,
97+
}
98+
};
99+
power
100+
}
76101
_ => None,
77102
};
78103
base_power

runtime/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,8 @@ impl_runtime_apis! {
485485
return None;
486486
};
487487

488-
call.check_fee().map(|power|
488+
let switch = fee_manager::SwitchStore::default();
489+
call.check_fee(switch).map(|power|
489490
XFeeManager::transaction_fee(power, encoded_len)
490491
)
491492
}

runtime/src/xexecutive.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ pub struct Executive<System, Block, Context, Payment, Finalisation>(
5555

5656
impl<
5757
Context: Default,
58-
System: system::Trait,
58+
System: system::Trait + fee_manager::Trait,
5959
Block: traits::Block<Header=System::Header, Hash=System::Hash>,
6060
Payment: MakePayment<System::AccountId>,
6161
Finalisation: OnFinalise<System::BlockNumber>,
@@ -167,7 +167,8 @@ impl<
167167
if signed_extrinsic {
168168
// Acceleration definitely exists for a signed extrinsic.
169169
let acc = acc.unwrap();
170-
if let Some(fee_power) = f.check_fee() {
170+
let switch = <fee_manager::Module<System>>::switch();
171+
if let Some(fee_power) = f.check_fee(switch) {
171172
Payment::make_payment(&s.clone().unwrap(), encoded_len, fee_power, acc.as_() as u32).map_err(|_| internal::ApplyError::CantPay)?;
172173

173174
// AUDIT: Under no circumstances may this function panic from here onwards.
@@ -275,7 +276,8 @@ impl<
275276

276277
let acc = xt.acceleration().unwrap();
277278
let (f, s) = xt.deconstruct();
278-
if let Some(fee_power) = f.check_fee() {
279+
let switch = <fee_manager::Module<System>>::switch();
280+
if let Some(fee_power) = f.check_fee(switch) {
279281
if Payment::check_payment(&s.clone().unwrap(), encoded_len, fee_power, acc.as_() as u32).is_err() {
280282
return TransactionValidity::Invalid(ApplyError::CantPay as i8);
281283
} else {

runtime/wasm/Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

xrml/xfee/manager/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ authors = ["Chainpol <http://www.chainx.org>"]
66
[dependencies]
77
serde = { version = "1.0", default-features = false }
88
parity-codec = { version = "3.0", default-features = false }
9+
parity-codec-derive = { version = "3.0", default-features = false }
910
substrate-primitives = { git = "https://github.com/chainpool/substrate", default_features = false }
1011
sr-std = { git = "https://github.com/chainpool/substrate", default-features = false }
1112
sr-io = { git = "https://github.com/chainpool/substrate", default-features = false }
@@ -26,6 +27,7 @@ default = ["std"]
2627
std = [
2728
"serde/std",
2829
"parity-codec/std",
30+
"parity-codec-derive/std",
2931
"substrate-primitives/std",
3032
"sr-std/std",
3133
"sr-io/std",

xrml/xfee/manager/src/lib.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
extern crate sr_std as rstd;
66

7+
#[macro_use]
8+
extern crate parity_codec_derive;
79
extern crate parity_codec as codec;
810
extern crate sr_primitives;
911

@@ -51,9 +53,17 @@ decl_module! {
5153
}
5254
}
5355

56+
#[derive(PartialEq, Eq, Clone, Encode, Decode, Default)]
57+
pub struct SwitchStore {
58+
pub global: bool,
59+
pub spot: bool,
60+
pub xbtc: bool,
61+
pub sdot: bool,
62+
}
63+
5464
decl_storage! {
5565
trait Store for Module<T: Trait> as XFeeManager {
56-
pub Switch get(switch) config(): bool; // Emergency control
66+
pub Switch get(switch): SwitchStore; // Emergency control
5767
pub ProducerFeeProportion get(producer_fee_proportion) config(): (u32, u32);
5868
}
5969
add_extra_genesis {
@@ -88,6 +98,10 @@ impl<T: Trait> MakePayment<T::AccountId> for Module<T> {
8898
}
8999

90100
impl<T: Trait> Module<T> {
101+
pub fn set_switch(store: SwitchStore) {
102+
Switch::<T>::put(store);
103+
}
104+
91105
pub fn transaction_fee(power: u64, encoded_len: u64) -> T::Balance {
92106
<balances::Module<T>>::transaction_base_fee() * <T::Balance as As<u64>>::sa(power)
93107
+ <balances::Module<T>>::transaction_byte_fee()

0 commit comments

Comments
 (0)