Skip to content

Commit

Permalink
update: ValidatorPrefs
Browse files Browse the repository at this point in the history
  • Loading branch information
aurexav committed Nov 26, 2019
1 parent 7ad23af commit 129416a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 35 deletions.
64 changes: 36 additions & 28 deletions srml/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,19 +105,22 @@ pub enum StakerStatus<AccountId> {

#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug)]
pub struct ValidatorPrefs {
pub node_name: Vec<u8>,
/// Validator should ensure this many more slashes than is necessary before being unstaked.
#[codec(compact)]
pub unstake_threshold: u32,
/// percent of Reward that validator takes up-front; only the rest is split between themselves and
/// nominators.
pub validator_payment_ratio: Perbill,
#[codec(compact)]
pub validator_payment_ratio: u32,
}

impl Default for ValidatorPrefs {
fn default() -> Self {
ValidatorPrefs {
node_name: vec![],
unstake_threshold: 3,
validator_payment_ratio: Default::default(),
validator_payment_ratio: 0,
}
}
}
Expand Down Expand Up @@ -378,19 +381,15 @@ decl_storage! {
/// The rest of the slashed value is handled by the `Slash`.
pub SlashRewardFraction get(fn slash_reward_fraction) config(): Perbill;

/// A mapping from still-bonded eras to the first session index of that era.
BondedEras: Vec<(EraIndex, SessionIndex)>;

/// All slashes that have occurred in a given era.
EraSlashJournal get(fn era_slash_journal):
map EraIndex => Vec<SlashJournalEntry<T::AccountId, ExtendedBalance>>;

pub NodeName get(node_name): map T::AccountId => Vec<u8>;

pub RingPool get(ring_pool): RingBalanceOf<T>;

pub KtonPool get(kton_pool): KtonBalanceOf<T>;

/// A mapping from still-bonded eras to the first session index of that era.
BondedEras: Vec<(EraIndex, SessionIndex)>;

/// All slashes that have occurred in a given era.
EraSlashJournal get(fn era_slash_journal): map EraIndex => Vec<SlashJournalEntry<T::AccountId, ExtendedBalance>>;
}
add_extra_genesis {
config(stakers):
Expand All @@ -403,21 +402,22 @@ decl_storage! {
T::Lookup::unlookup(controller.clone()),
StakingBalance::Ring(balance),
RewardDestination::Stash,
12
12,
);
let _ = match status {
StakerStatus::Validator => {
<Module<T>>::validate(
T::Origin::from(Some(controller.clone()).into()),
[0;8].to_vec(),
0,
3
ValidatorPrefs {
node_name: vec![0; 8],
..Default::default()
},
)
},
StakerStatus::Nominator(votes) => {
<Module<T>>::nominate(
T::Origin::from(Some(controller.clone()).into()),
votes.iter().map(|l| {T::Lookup::unlookup(l.clone())}).collect()
votes.iter().map(|l| {T::Lookup::unlookup(l.clone())}).collect(),
)
}, _ => Ok(())
};
Expand Down Expand Up @@ -681,24 +681,32 @@ decl_module! {
<Ledger<T>>::insert(&controller, ledger);
}

fn validate(origin, name: Vec<u8>, ratio: u32, unstake_threshold: u32) {
fn validate(origin, prefs: ValidatorPrefs) {
let controller = ensure_signed(origin)?;
let ledger = Self::ledger(&controller).ok_or("not a controller")?;
let stash = &ledger.stash;

ensure!(
!prefs.node_name.is_empty(),
"node name can not be empty",
);
ensure!(
unstake_threshold <= MAX_UNSTAKE_THRESHOLD,
"unstake threshold too large"
prefs.unstake_threshold <= MAX_UNSTAKE_THRESHOLD,
"unstake threshold too large",
);

let stash = &ledger.stash;
let mut prefs = prefs;
// at most 100%
let ratio = Perbill::from_percent(ratio.min(100));
let prefs = ValidatorPrefs { unstake_threshold: unstake_threshold, validator_payment_ratio: ratio };
prefs.validator_payment_ratio = prefs.validator_payment_ratio.min(100);

<Nominators<T>>::remove(stash);
<Validators<T>>::insert(stash, prefs);
if !<NodeName<T>>::exists(&controller) {
<NodeName<T>>::insert(controller, name);
Self::deposit_event(RawEvent::NodeNameUpdated);
}
<Validators<T>>::mutate(stash, |prefs_| {
let exists = !prefs_.node_name.is_empty();
*prefs_ = prefs;
if exists {
Self::deposit_event(RawEvent::NodeNameUpdated);
}
});
}

fn nominate(origin, targets: Vec<<T::Lookup as StaticLookup>::Source>) {
Expand Down Expand Up @@ -1141,7 +1149,7 @@ impl<T: Trait> Module<T> {
/// nominators' balance, pro-rata based on their exposure, after having removed the validator's
/// pre-payout cut.
fn reward_validator(stash: &T::AccountId, reward: RingBalanceOf<T>) -> RingPositiveImbalanceOf<T> {
let off_the_table = Self::validators(stash).validator_payment_ratio * reward;
let off_the_table = Perbill::from_percent(Self::validators(stash).validator_payment_ratio) * reward;
let reward = reward - off_the_table;
let mut imbalance = <RingPositiveImbalanceOf<T>>::zero();
let validator_cut = if reward.is_zero() {
Expand Down
14 changes: 8 additions & 6 deletions srml/staking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use srml_support::{
};
use substrate_primitives::{crypto::key_types, H256};

use crate::{EraIndex, GenesisConfig, Module, Nominators, RewardDestination, StakerStatus, StakingBalance, Trait};
use crate::*;
use darwinia_support::TimeStamp;
use node_primitives::Balance;
use phragmen::ExtendedBalance;
Expand Down Expand Up @@ -418,13 +418,15 @@ pub fn bond_validator(acc: u64, val: Balance) {
acc,
StakingBalance::Ring(val),
RewardDestination::Controller,
0
0,
));
assert_ok!(Staking::validate(
Origin::signed(acc),
"test".as_bytes().to_owned(),
0,
0
ValidatorPrefs {
node_name: "test".as_bytes().to_vec(),
unstake_threshold: 0,
validator_payment_ratio: 0,
}
));
}

Expand All @@ -437,7 +439,7 @@ pub fn bond_nominator(acc: u64, val: Balance, target: Vec<u64>) {
acc,
StakingBalance::Ring(val),
RewardDestination::Controller,
0
0,
));
assert_ok!(Staking::nominate(Origin::signed(acc), target));
}
Expand Down
3 changes: 2 additions & 1 deletion types.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@
},

"ValidatorPrefs": {
"node_name": "Vec<u8>",
"unstake_threshold": "Compact<u32>",
"validator_payment_ratio": "Perbill"
"validator_payment_ratio": "Compact<u32>"
},

"StakingLedger": {
Expand Down

0 comments on commit 129416a

Please sign in to comment.