Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Minor refactor for staking module #659

Merged
merged 5 commits into from
Sep 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 15 additions & 20 deletions substrate/runtime/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,26 +75,26 @@ pub enum LockStatus<BlockNumber: Parameter> {
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<Balance: Parameter + Codec + MaybeSerializeDebug + MaybeDeserialize> {
#[derive(PartialEq, Eq, Clone, Encode, Decode)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))]
pub struct ValidatorPrefs<Balance> {
/// 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<B: Parameter + Codec + Default> Default for ValidatorPrefs<B> {
impl<B: Default> Default for ValidatorPrefs<B> {
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<<Self as balances::Trait>::Balance>;
Expand All @@ -107,12 +107,13 @@ decl_module! {
pub struct Module<T: Trait>;

#[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<T::AccountId, T::AccountIndex>) -> 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<T::Balance>) -> Result = 4;
}

#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
Expand Down Expand Up @@ -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<T::Balance> ];
// All the accounts with a desire to stake.
pub Intentions get(intentions): default Vec<T::AccountId>;
// All nominator -> nominee relationships.
Expand Down Expand Up @@ -193,11 +194,6 @@ impl<T: Trait> Module<T> {

// PUBLIC IMMUTABLES

/// ValidatorPreferences getter, introduces a default.
pub fn validator_preferences(who: &T::AccountId) -> (u32, T::Balance) {
<ValidatorPreferences<T>>::get(who).unwrap_or_else(|| (3, Zero::zero()))
}

/// MinimumValidatorCount getter, introduces a default.
pub fn minimum_validator_count() -> usize {
<MinimumValidatorCount<T>>::get().map(|v| v as usize).unwrap_or(DEFAULT_MINIMUM_VALIDATOR_COUNT)
Expand Down Expand Up @@ -313,16 +309,15 @@ impl<T: Trait> Module<T> {
fn register_preferences(
aux: &T::PublicAux,
intentions_index: u32,
unstake_threshold: u32,
validator_payment: T::Balance
prefs: ValidatorPrefs<T::Balance>
) -> Result {
let aux = aux.ref_into();

if Self::intentions().get(intentions_index as usize) != Some(aux) {
return Err("Invalid index")
}

<ValidatorPreferences<T>>::insert(aux, (unstake_threshold, validator_payment));
<ValidatorPreferences<T>>::insert(aux, prefs);

Ok(())
}
Expand Down Expand Up @@ -391,7 +386,7 @@ impl<T: Trait> Module<T> {
/// 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()
Expand Down Expand Up @@ -551,7 +546,7 @@ impl<T: Trait> consensus::OnOfflineValidator for Module<T> {
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) {
Expand Down
4 changes: 2 additions & 2 deletions substrate/runtime/staking/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]);

Expand Down Expand Up @@ -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);
Expand Down