@@ -83,17 +83,25 @@ It means there can be 100 valid candidates with the potential to be validators.
8383The rate of becoming the block proposer is related to the number of delegations that the validator received.
8484In 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
224232jailed = [ [address, deposits, custody_until, released_at]+ ], address asc
225233term_id = [ the last block number of the previous term, the current term id ]
226234intermediate_rewards = [ [ address, rewards ]+ address asc, [ address, rewards ]+ address asc ]
235+ validators = [ [ delegation, deposit, pubkey ] ] (delegation, deposit, pubkey) asc
227236```
228237
229238### on TermEnd events
2302391 . 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
2322413 . Remove the jailed accounts if the current term is greater than ` released_at ` and give back the deposits
2332424 . Calculate rewards of the previous block and update ` intermediate_rewards `
243+ 5 . Elect validators
244+ * Store validators in the ascending order
0 commit comments