Skip to content

Commit 91125ae

Browse files
Seulgi Kimmergify[bot]
authored andcommitted
Consider the amount of deposit on the election
1 parent 3197592 commit 91125ae

File tree

1 file changed

+27
-29
lines changed

1 file changed

+27
-29
lines changed

core/src/consensus/stake/action_data.rs

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -250,60 +250,58 @@ impl Validators {
250250
assert!(max_num_of_validators > min_num_of_validators);
251251

252252
let active_candidates = Candidates::active(&state, min_deposit).unwrap();
253-
let candidates: HashMap<_, _> =
254-
active_candidates.keys().map(|pubkey| (public_to_address(pubkey), *pubkey)).collect();
253+
let mut candidates: HashMap<_, (_, Deposit)> = active_candidates
254+
.into_iter()
255+
.map(|(pubkey, deposit)| (public_to_address(&pubkey), (pubkey, deposit)))
256+
.collect();
255257

256258
let banned = Banned::load_from_state(&state)?;
257259
for address in candidates.keys() {
258260
assert!(!banned.0.contains(address), "{} is banned address", address);
259261
}
260262

261263
// step 1
262-
let mut delegatees: Vec<(StakeQuantity, Public)> = Stakeholders::delegatees(&state)?
264+
let mut validators: Vec<(StakeQuantity, Deposit, Public)> = Stakeholders::delegatees(&state)?
263265
.into_iter()
264-
.filter_map(|(address, delegation)| candidates.get(&address).map(|pubkey| (delegation, *pubkey)))
266+
.filter_map(|(address, delegation)| {
267+
candidates.remove(&address).map(|(pubkey, deposit)| (delegation, deposit, pubkey))
268+
})
265269
.collect();
266270

267-
delegatees.sort_unstable();
268-
delegatees.reverse();
269-
let the_highest_score_dropout = delegatees.get(max_num_of_validators).map(|(delegation, _address)| *delegation);
270-
let the_lowest_score_first_class = delegatees.get(min_num_of_validators).map(|(delegation, _address)| *delegation)
271+
validators.sort_unstable();
272+
validators.reverse();
273+
let the_highest_score_dropout =
274+
validators.get(max_num_of_validators).map(|(delegation, deposit, _address)| (*delegation, *deposit));
275+
let the_lowest_score_first_class = validators.get(min_num_of_validators).map(|(delegation, deposit, _address)| (*delegation, *deposit))
271276
// None means there are less than MIN_NUM_OF_VALIDATORS. Allow all remains.
272277
.unwrap_or_default();
273278

274279
// step 2
275-
delegatees.truncate(max_num_of_validators);
280+
validators.truncate(max_num_of_validators);
276281

277282
// step 3
278283
if let Some(the_highest_score_dropout) = the_highest_score_dropout {
279-
delegatees.retain(|(delegation, _address)| *delegation > the_highest_score_dropout);
284+
validators.retain(|(delegation, deposit, _address)| (*delegation, *deposit) > the_highest_score_dropout);
280285
}
281286

282-
if delegatees.len() < min_num_of_validators {
287+
if validators.len() < min_num_of_validators {
283288
cerror!(
284289
ENGINE,
285290
"There must be something wrong. {}, {} < {}",
286-
"delegatees.len() < min_num_of_validators",
287-
delegatees.len(),
291+
"validators.len() < min_num_of_validators",
292+
validators.len(),
288293
min_num_of_validators
289294
);
290295
}
291-
let validators = delegatees
292-
.into_iter()
293-
.filter(|(delegation, _pubkey)| {
294-
// step 4
295-
if *delegation >= the_lowest_score_first_class {
296-
true
297-
} else {
298-
// step 5
299-
*delegation >= delegation_threshold
300-
}
301-
})
302-
.map(|(delegation, pubkey)| {
303-
let deposit = *active_candidates.get(&pubkey).unwrap();
304-
(delegation, deposit, pubkey)
305-
})
306-
.collect();
296+
validators.retain(|(delegation, deposit, _pubkey)| {
297+
// step 4
298+
if (*delegation, *deposit) >= the_lowest_score_first_class {
299+
true
300+
} else {
301+
// step 5
302+
*delegation >= delegation_threshold
303+
}
304+
});
307305

308306
Ok(Self(validators))
309307
}

0 commit comments

Comments
 (0)