From 48519fd1bd01b95004a431b97354fd3e7128f24e Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Wed, 5 Sep 2018 11:39:47 +0200 Subject: [PATCH] Minor refactor for staking module (#659) * Somerhing wrong. * My attempt to fix * cfg_attr for serde * Fix tests --- substrate/runtime/staking/src/lib.rs | 35 +++++++++++--------------- substrate/runtime/staking/src/tests.rs | 4 +-- 2 files changed, 17 insertions(+), 22 deletions(-) diff --git a/substrate/runtime/staking/src/lib.rs b/substrate/runtime/staking/src/lib.rs index 18fea81cf5a99..42d35c9c3ec85 100644 --- a/substrate/runtime/staking/src/lib.rs +++ b/substrate/runtime/staking/src/lib.rs @@ -75,26 +75,26 @@ pub enum LockStatus { LockedUntil(BlockNumber), Bonded, } -/* + /// Preference of what happens on a slash event. -#[cfg_attr(feature = "std", derive(Debug, Serialize, Deserialize))] -#[derive(Encode, Decode, Eq, PartialEq, Clone, Copy)] -pub struct ValidatorPrefs { +#[derive(PartialEq, Eq, Clone, Encode, Decode)] +#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))] +pub struct ValidatorPrefs { /// Validator should ensure this many more slashes than is necessary before being unstaked. pub unstake_threshold: u32, // Reward that validator takes up-front; only the rest is split between themself and nominators. pub validator_payment: Balance, } -impl Default for ValidatorPrefs { +impl Default for ValidatorPrefs { fn default() -> Self { - ValidatorPreferences { + ValidatorPrefs { unstake_threshold: 3, - off_the_table: Default::default(), + validator_payment: Default::default(), } } } -*/ + pub trait Trait: balances::Trait + session::Trait { /// Some tokens minted. type OnRewardMinted: OnMinted<::Balance>; @@ -107,12 +107,13 @@ decl_module! { pub struct Module; #[cfg_attr(feature = "std", derive(Serialize, Deserialize))] + #[cfg_attr(feature = "std", serde(bound(deserialize = "T::Balance: ::serde::de::DeserializeOwned")))] pub enum Call where aux: T::PublicAux { fn stake(aux) -> Result = 0; fn unstake(aux, intentions_index: u32) -> Result = 1; fn nominate(aux, target: Address) -> Result = 2; fn unnominate(aux, target_index: u32) -> Result = 3; - fn register_preferences(aux, intentions_index: u32, unstake_threshold: u32, validator_payment: T::Balance) -> Result = 4; + fn register_preferences(aux, intentions_index: u32, prefs: ValidatorPrefs) -> Result = 4; } #[cfg_attr(feature = "std", derive(Serialize, Deserialize))] @@ -162,7 +163,7 @@ decl_storage! { // The current era index. pub CurrentEra get(current_era): required T::BlockNumber; // Preferences that a validator has. - pub ValidatorPreferences: map [ T::AccountId => (u32, T::Balance) ]; + pub ValidatorPreferences get(validator_preferences): default map [ T::AccountId => ValidatorPrefs ]; // All the accounts with a desire to stake. pub Intentions get(intentions): default Vec; // All nominator -> nominee relationships. @@ -193,11 +194,6 @@ impl Module { // PUBLIC IMMUTABLES - /// ValidatorPreferences getter, introduces a default. - pub fn validator_preferences(who: &T::AccountId) -> (u32, T::Balance) { - >::get(who).unwrap_or_else(|| (3, Zero::zero())) - } - /// MinimumValidatorCount getter, introduces a default. pub fn minimum_validator_count() -> usize { >::get().map(|v| v as usize).unwrap_or(DEFAULT_MINIMUM_VALIDATOR_COUNT) @@ -313,8 +309,7 @@ impl Module { fn register_preferences( aux: &T::PublicAux, intentions_index: u32, - unstake_threshold: u32, - validator_payment: T::Balance + prefs: ValidatorPrefs ) -> Result { let aux = aux.ref_into(); @@ -322,7 +317,7 @@ impl Module { return Err("Invalid index") } - >::insert(aux, (unstake_threshold, validator_payment)); + >::insert(aux, prefs); Ok(()) } @@ -391,7 +386,7 @@ impl Module { /// Reward a given validator by a specific amount. Add the reward to their, and their nominators' /// balance, pro-rata. fn reward_validator(who: &T::AccountId, reward: T::Balance) { - let off_the_table = reward.min(Self::validator_preferences(who).1); + let off_the_table = reward.min(Self::validator_preferences(who).validator_payment); let reward = reward - off_the_table; let validator_cut = if reward.is_zero() { Zero::zero() @@ -551,7 +546,7 @@ impl consensus::OnOfflineValidator for Module { let slash = Self::early_era_slash() << instances; let next_slash = slash << 1u32; let _ = Self::slash_validator(&v, slash); - if instances >= Self::validator_preferences(&v).0 + if instances >= Self::validator_preferences(&v).unstake_threshold || Self::slashable_balance(&v) < next_slash { if let Some(pos) = Self::intentions().into_iter().position(|x| &x == &v) { diff --git a/substrate/runtime/staking/src/tests.rs b/substrate/runtime/staking/src/tests.rs index 041f0538edb5d..4bf9ce9f41051 100644 --- a/substrate/runtime/staking/src/tests.rs +++ b/substrate/runtime/staking/src/tests.rs @@ -130,7 +130,7 @@ fn note_offline_auto_unstake_session_change_should_work() { with_externalities(&mut new_test_ext(0, 3, 3, 0, true, 10), || { Balances::set_free_balance(&10, 7000); Balances::set_free_balance(&20, 7000); - assert_ok!(Staking::register_preferences(&10, 0, 1, 0)); + assert_ok!(Staking::register_preferences(&10, 0, ValidatorPrefs { unstake_threshold: 1, validator_payment: 0 })); assert_eq!(Staking::intentions(), vec![10, 20]); @@ -355,7 +355,7 @@ fn rewards_with_off_the_table_should_work() { assert_eq!(Balances::total_balance(&3), 30); System::set_block_number(2); - assert_ok!(Staking::register_preferences(&1, Staking::intentions().into_iter().position(|i| i == 1).unwrap() as u32, 3, 4)); + assert_ok!(Staking::register_preferences(&1, Staking::intentions().into_iter().position(|i| i == 1).unwrap() as u32, ValidatorPrefs { unstake_threshold: 3, validator_payment: 4 })); Session::check_rotate_session(System::block_number()); assert_eq!(Balances::total_balance(&1), 16); assert_eq!(Balances::total_balance(&2), 24);