Skip to content

Commit 2ad21a8

Browse files
committed
Remove needless weight data from Validator
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.
1 parent 416031a commit 2ad21a8

File tree

3 files changed

+0
-41
lines changed

3 files changed

+0
-41
lines changed

core/src/consensus/stake/action_data.rs

-34
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,6 @@ impl<'a> Delegation<'a> {
236236

237237
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd, RlpDecodable, RlpEncodable)]
238238
pub struct Validator {
239-
weight: StakeQuantity,
240239
delegation: StakeQuantity,
241240
deposit: Deposit,
242241
pubkey: Public,
@@ -245,7 +244,6 @@ pub struct Validator {
245244
impl Validator {
246245
pub fn new_for_test(delegation: StakeQuantity, deposit: Deposit, pubkey: Public) -> Self {
247246
Self {
248-
weight: delegation,
249247
delegation,
250248
deposit,
251249
pubkey,
@@ -254,17 +252,12 @@ impl Validator {
254252

255253
fn new(delegation: StakeQuantity, deposit: Deposit, pubkey: Public) -> Self {
256254
Self {
257-
weight: delegation,
258255
delegation,
259256
deposit,
260257
pubkey,
261258
}
262259
}
263260

264-
fn reset(&mut self) {
265-
self.weight = self.delegation;
266-
}
267-
268261
pub fn pubkey(&self) -> &Public {
269262
&self.pubkey
270263
}
@@ -345,28 +338,6 @@ impl Validators {
345338
Ok(())
346339
}
347340

348-
pub fn update_weight(&mut self, block_author: &Address) {
349-
let min_delegation = self.min_delegation();
350-
for Validator {
351-
weight,
352-
pubkey,
353-
..
354-
} in self.0.iter_mut().rev()
355-
{
356-
if public_to_address(pubkey) == *block_author {
357-
// block author
358-
*weight = weight.saturating_sub(min_delegation);
359-
break
360-
}
361-
// neglecting validators
362-
*weight = weight.saturating_sub(min_delegation * 2);
363-
}
364-
if self.0.iter().all(|validator| validator.weight == 0) {
365-
self.0.iter_mut().for_each(Validator::reset);
366-
}
367-
self.0.sort_unstable();
368-
}
369-
370341
pub fn remove(&mut self, target: &Address) {
371342
self.0.retain(
372343
|Validator {
@@ -379,10 +350,6 @@ impl Validators {
379350
pub fn delegation(&self, pubkey: &Public) -> Option<StakeQuantity> {
380351
self.0.iter().find(|validator| validator.pubkey == *pubkey).map(|&validator| validator.delegation)
381352
}
382-
383-
fn min_delegation(&self) -> StakeQuantity {
384-
self.0.iter().map(|&validator| validator.delegation).min().expect("There must be at least one validators")
385-
}
386353
}
387354

388355
impl Deref for Validators {
@@ -1758,7 +1725,6 @@ mod tests {
17581725
pubkey: *pubkey,
17591726
deposit: 0,
17601727
delegation: 0,
1761-
weight: 0,
17621728
})
17631729
.collect(),
17641730
);

core/src/consensus/stake/mod.rs

-6
Original file line numberDiff line numberDiff line change
@@ -341,12 +341,6 @@ pub fn move_current_to_previous_intermediate_rewards(state: &mut TopLevelState)
341341
rewards.save_to_state(state)
342342
}
343343

344-
pub fn update_validator_weights(state: &mut TopLevelState, block_author: &Address) -> StateResult<()> {
345-
let mut validators = Validators::load_from_state(state)?;
346-
validators.update_weight(block_author);
347-
validators.save_to_state(state)
348-
}
349-
350344
fn change_params(
351345
state: &mut TopLevelState,
352346
metadata_seq: u64,

core/src/consensus/tendermint/engine.rs

-1
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,6 @@ impl ConsensusEngine for Tendermint {
184184
self.machine.add_balance(block, &author, block_author_reward)?;
185185
}
186186
_ => {
187-
stake::update_validator_weights(block.state_mut(), &author)?;
188187
stake::add_intermediate_rewards(block.state_mut(), author, block_author_reward)?;
189188
}
190189
}

0 commit comments

Comments
 (0)