Skip to content

Commit

Permalink
Remove needless weight data from Validator
Browse files Browse the repository at this point in the history
In randomized leader election environment, CodeChain doesn't need
to manage weight values. Previously, it is utilized to guarantee
stake-proportional leader election but now the property comes from
the nature of the probability model.
  • Loading branch information
HoOngEe committed Jan 23, 2020
1 parent 94e0302 commit b4304fa
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 41 deletions.
34 changes: 0 additions & 34 deletions core/src/consensus/stake/action_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,6 @@ impl<'a> Delegation<'a> {

#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd, RlpDecodable, RlpEncodable)]
pub struct Validator {
weight: StakeQuantity,
delegation: StakeQuantity,
deposit: Deposit,
pubkey: Public,
Expand All @@ -245,7 +244,6 @@ pub struct Validator {
impl Validator {
pub fn new_for_test(delegation: StakeQuantity, deposit: Deposit, pubkey: Public) -> Self {
Self {
weight: delegation,
delegation,
deposit,
pubkey,
Expand All @@ -254,17 +252,12 @@ impl Validator {

fn new(delegation: StakeQuantity, deposit: Deposit, pubkey: Public) -> Self {
Self {
weight: delegation,
delegation,
deposit,
pubkey,
}
}

fn reset(&mut self) {
self.weight = self.delegation;
}

pub fn pubkey(&self) -> &Public {
&self.pubkey
}
Expand Down Expand Up @@ -345,28 +338,6 @@ impl Validators {
Ok(())
}

pub fn update_weight(&mut self, block_author: &Address) {
let min_delegation = self.min_delegation();
for Validator {
weight,
pubkey,
..
} in self.0.iter_mut().rev()
{
if public_to_address(pubkey) == *block_author {
// block author
*weight = weight.saturating_sub(min_delegation);
break
}
// neglecting validators
*weight = weight.saturating_sub(min_delegation * 2);
}
if self.0.iter().all(|validator| validator.weight == 0) {
self.0.iter_mut().for_each(Validator::reset);
}
self.0.sort_unstable();
}

pub fn remove(&mut self, target: &Address) {
self.0.retain(
|Validator {
Expand All @@ -379,10 +350,6 @@ impl Validators {
pub fn delegation(&self, pubkey: &Public) -> Option<StakeQuantity> {
self.0.iter().find(|validator| validator.pubkey == *pubkey).map(|&validator| validator.delegation)
}

fn min_delegation(&self) -> StakeQuantity {
self.0.iter().map(|&validator| validator.delegation).min().expect("There must be at least one validators")
}
}

impl Deref for Validators {
Expand Down Expand Up @@ -1758,7 +1725,6 @@ mod tests {
pubkey: *pubkey,
deposit: 0,
delegation: 0,
weight: 0,
})
.collect(),
);
Expand Down
6 changes: 0 additions & 6 deletions core/src/consensus/stake/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,12 +341,6 @@ pub fn move_current_to_previous_intermediate_rewards(state: &mut TopLevelState)
rewards.save_to_state(state)
}

pub fn update_validator_weights(state: &mut TopLevelState, block_author: &Address) -> StateResult<()> {
let mut validators = Validators::load_from_state(state)?;
validators.update_weight(block_author);
validators.save_to_state(state)
}

fn change_params(
state: &mut TopLevelState,
metadata_seq: u64,
Expand Down
1 change: 0 additions & 1 deletion core/src/consensus/tendermint/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ impl ConsensusEngine for Tendermint {
self.machine.add_balance(block, &author, block_author_reward)?;
}
_ => {
stake::update_validator_weights(block.state_mut(), &author)?;
stake::add_intermediate_rewards(block.state_mut(), author, block_author_reward)?;
}
}
Expand Down

0 comments on commit b4304fa

Please sign in to comment.