Skip to content

Commit a059ad2

Browse files
Seulgi Kimsgkim126
authored andcommitted
Store validator list in the states
And changes the way to calculate the order of the block proposer.
1 parent bcab5db commit a059ad2

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

spec/Dynamic-Validator.md

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,17 +83,25 @@ It means there can be 100 valid candidates with the potential to be validators.
8383
The rate of becoming the block proposer is related to the number of delegations that the validator received.
8484
In other words, CodeChain allows the validator that receives more delegations to generate more blocks than others.
8585

86+
## How to update validators
8687
```rust
87-
let mut validators: Vec<(u64, u64, Account)> = // (Delegation, Deposit, Account)
88-
let mut proposers: Vec<Account> = vec![];
89-
validators.reverse_sort();
90-
let min_delegation = validator.last().0;
91-
while !validators.is_empty() {
92-
for (delegation, _, validator) in validators {
93-
proposers.push_back(validator);
94-
}
95-
validators.retain(|(delegation, _, _)| delegation > min_delegation);
96-
validators.for_each(|(mut delegation, _, _)| delegation -= min_delegation);
88+
let initial_validators: Vec<(Delegation, Deposit, Public)> = // the validator list at the term begin
89+
let validators: Vec<(Delegation, Deposit, Public)> = // (Delegation, Deposit, Pubkey)
90+
91+
let min_delegation: u64 = initial_validators.into_iter().map(|(delegation, _, _)| delegation).min();
92+
93+
let author_index = validators.position(|(_, _, pubkey)| pubkey == block_author).unwrap();
94+
for (&mut delegation, _, pubkey) in validators[(author_index + 1)..] {
95+
// Deprioritize fast since they are neglecting as a validator.
96+
delegation -= min_delegation * 2;
97+
}
98+
// Deprioritize author
99+
validators[author_index].2 -= min_delegation;
100+
101+
validators.sort();
102+
103+
if validators.into_iter().all(|(delegation, _, _)| delegation == 0) {
104+
validators = initial_validators;
97105
}
98106
```
99107

@@ -224,10 +232,13 @@ banned = [ address+ ], address asc
224232
jailed = [ [address, deposits, custody_until, released_at]+ ], address asc
225233
term_id = [ the last block number of the previous term, the current term id ]
226234
intermediate_rewards = [ [ address, rewards ]+ address asc, [ address, rewards ]+ address asc ]
235+
validators = [ [ delegation, deposit, pubkey ] ] (delegation, deposit, pubkey) asc
227236
```
228237

229238
### on TermEnd events
230239
1. Update `term_id` to the current block number and the next term id
231-
3. Remove the expired candidates and give back the deposits
240+
2. Remove the expired candidates and give back the deposits
232241
3. Remove the jailed accounts if the current term is greater than `released_at` and give back the deposits
233242
4. Calculate rewards of the previous block and update `intermediate_rewards`
243+
5. Elect validators
244+
* Store validators in the ascending order

0 commit comments

Comments
 (0)