Skip to content

Commit

Permalink
Make slash for missed blocks configurable (paritytech#563)
Browse files Browse the repository at this point in the history
* Make slash for missed blocks configurable

* Build wasm and nits

* Set minimal_penalty to 0.01 PCX by default

* Build wasm

* Pass staking tests

* Remove unneeded impl

* Build wasm
  • Loading branch information
liuchengxu committed May 8, 2019
1 parent bac4bb3 commit 99e62d4
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 4 deletions.
3 changes: 3 additions & 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.
3 changes: 2 additions & 1 deletion cli/src/genesis_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ pub fn testnet_genesis(genesis_spec: GenesisSpec) -> GenesisConfig {
bonding_duration,
intention_bonding_duration,
current_era: 0,
minimum_penalty: 10_000_000, // 0.1 PCX by default
minimum_penalty: 1_000_000, // 0.01 PCX by default
missed_blocks_severity: 3,
}),
xtokens: Some(XTokensConfig {
token_discount: vec![
Expand Down
Binary file not shown.
4 changes: 4 additions & 0 deletions xrml/xmining/staking/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ timestamp = { package ="srml-timestamp", git = "https://github.com/chainpool/sub
# ChainX
xfee-manager = { package = "xrml-xfee-manager", path = "../../xfee/manager" }
xrecords = { package = "xrml-xassets-records", path = "../../xassets/records" }
xbitcoin = { package = "xrml-xbridge-bitcoin", path = "../../xbridge/bitcoin" }
xbridge-features = { package = "xrml-xbridge-features", path = "../../xbridge/features" }
xbridge-common = { package = "xrml-xbridge-common", path = "../../xbridge/common" }
xmultisig = { package = "xrml-xmultisig", path = "../../xmultisig", default-features = false }

[features]
default = ["std"]
Expand Down
8 changes: 8 additions & 0 deletions xrml/xmining/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,12 @@ decl_module! {
<ValidatorCount<T>>::put(new);
}

/// The severity of missed blocks per session.
fn set_missed_blocks_severity(new: Compact<u32>) {
let new: u32 = new.into();
<MissedBlockSeverity<T>>::put(new);
}

/// Force there to be a new era. This also forces a new session immediately after.
/// `apply_rewards` should be true for validators to get the session reward.
fn force_new_era(apply_rewards: bool) -> Result {
Expand Down Expand Up @@ -317,6 +323,8 @@ decl_storage! {
pub OfflineValidatorsPerSession get(offline_validators_per_session): Vec<T::AccountId>;
/// Total blocks that each active validator missed in the current session.
pub MissedOfPerSession get(missed_of_per_session): map T::AccountId => u32;
/// The higher the severity, the more slash for missed blocks.
pub MissedBlockSeverity get(missed_blocks_severity) config(): u32;
}
}

Expand Down
81 changes: 79 additions & 2 deletions xrml/xmining/staking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use super::*;

// Substrate
use parity_codec::{Decode, Encode};
use primitives::{
testing::{ConvertUintAuthorityId, Digest, DigestItem, Header, UintAuthorityId},
traits::BlakeTwo256,
Expand Down Expand Up @@ -58,10 +59,73 @@ impl timestamp::Trait for Test {
}

impl xaccounts::Trait for Test {
type Event = ();
type DetermineIntentionJackpotAccountId = DummyDetermineIntentionJackpotAccountId;
}

impl xbridge_features::Trait for Test {
type TrusteeMultiSig = DummyMultiSigIdFor;
type Event = ();
}

impl xmultisig::Trait for Test {
type MultiSig = DummyMultiSig;
type GenesisMultiSig = DummyGenesisMultiSig;
type Proposal = DummyTrusteeCall;
type Event = ();
}

pub struct DummyMultiSig;
impl xmultisig::MultiSigFor<u64, H256> for DummyMultiSig {
fn multi_sig_addr_for(who: &u64) -> u64 {
who + 2
}

fn multi_sig_id_for(_who: &u64, _addr: &u64, _data: &[u8]) -> H256 {
H256::default()
}
}

#[derive(Clone, Eq, PartialEq, Encode, Decode, Debug)]
pub struct DummyTrusteeCall;
impl xmultisig::TrusteeCall<u64> for DummyTrusteeCall {
fn allow(&self) -> bool {
true
}

fn exec(&self, _execiser: &u64) -> Result {
Ok(())
}
}

impl support::dispatch::Dispatchable for DummyTrusteeCall {
type Origin = Origin;
type Trait = DummyTrusteeCall;
fn dispatch(self, _origin: Origin) -> support::dispatch::Result {
Ok(())
}
}

pub struct DummyMultiSigIdFor;
impl xbridge_features::TrusteeMultiSigFor<u64> for DummyMultiSigIdFor {
fn multi_sig_addr_for_trustees(_chain: xassets::Chain, _who: &Vec<u64>) -> u64 {
1
}
}

pub struct DummyBitcoinTrusteeMultiSig;
impl xbridge_common::traits::TrusteeMultiSig<u64> for DummyBitcoinTrusteeMultiSig {
fn multisig_for_trustees() -> u64 {
777
}
}

pub struct DummyGenesisMultiSig;
impl xmultisig::GenesisMultiSig<u64> for DummyGenesisMultiSig {
fn gen_genesis_multisig(_accounts: Vec<u64>) -> (u64, u64) {
(666, 888)
}
}

pub struct DummyDetermineIntentionJackpotAccountId;
impl xaccounts::IntentionJackpotAccountIdFor<u64> for DummyDetermineIntentionJackpotAccountId {
fn accountid_for(origin: &u64) -> u64 {
Expand Down Expand Up @@ -109,9 +173,20 @@ impl xsession::Trait for Test {
}

impl xbitcoin::Trait for Test {
type AccountExtractor = DummyExtractor;
type TrusteeSessionProvider = XBridgeFeatures;
type TrusteeMultiSigProvider = DummyBitcoinTrusteeMultiSig;
type CrossChainProvider = XBridgeFeatures;
type Event = ();
}

pub struct DummyExtractor;
impl xbridge_common::traits::Extractable<u64> for DummyExtractor {
fn account_info(_data: &[u8]) -> Option<(u64, Option<Vec<u8>>)> {
Some((999, None))
}
}

impl xrecords::Trait for Test {
type Event = ();
}
Expand Down Expand Up @@ -180,7 +255,7 @@ pub fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
sessions_per_era: 1,
sessions_per_epoch: 10,
minimum_penalty: 10_000_000, // 0.1 PCX by default
validator_stake_threshold: 1,
missed_blocks_severity: 3,
}
.build_storage()
.unwrap()
Expand Down Expand Up @@ -216,5 +291,7 @@ pub type Indices = indices::Module<Test>;
pub type System = system::Module<Test>;
pub type XSession = xsession::Module<Test>;
pub type XAssets = xassets::Module<Test>;
pub type XMultiSig = xmultisig::Module<Test>;
pub type XAccounts = xaccounts::Module<Test>;
pub type XBridgeFeatures = xbridge_features::Module<Test>;
pub type XStaking = Module<Test>;
2 changes: 1 addition & 1 deletion xrml/xmining/staking/src/shifter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl<T: Trait> Module<T> {
let missed = <MissedOfPerSession<T>>::take(who) as u64;
let reward_per_block = Self::reward_of_per_block(my_reward);
let total_slash = cmp::max(
T::Balance::sa(reward_per_block.as_() * 10 * missed),
T::Balance::sa(reward_per_block.as_() * missed * Self::missed_blocks_severity() as u64),
T::Balance::sa(Self::minimum_penalty().as_() * missed),
);

Expand Down

0 comments on commit 99e62d4

Please sign in to comment.