@@ -22,29 +22,15 @@ use ckey::Ed25519Public as Public;
2222use cstate:: { CurrentValidatorSet , NextValidatorSet , SimpleValidator } ;
2323use ctypes:: util:: unexpected:: OutOfBounds ;
2424use ctypes:: BlockHash ;
25+ use ctypes:: BlockId ;
2526use parking_lot:: RwLock ;
26- use std:: cmp:: Reverse ;
2727use std:: sync:: { Arc , Weak } ;
2828
2929#[ derive( Default ) ]
3030pub struct DynamicValidator {
3131 client : RwLock < Option < Weak < dyn ConsensusClient > > > ,
3232}
3333
34- pub struct WeightOrderedValidators ( Vec < Public > ) ;
35-
36- pub struct WeightIndex ( usize ) ;
37-
38- impl WeightOrderedValidators {
39- pub fn len ( & self ) -> usize {
40- self . 0 . len ( )
41- }
42-
43- pub fn get ( & self , index : WeightIndex ) -> Option < & Public > {
44- self . 0 . get ( index. 0 )
45- }
46- }
47-
4834impl DynamicValidator {
4935 fn next_validators ( & self , hash : BlockHash ) -> Vec < SimpleValidator > {
5036 let client: Arc < dyn ConsensusClient > =
@@ -69,20 +55,6 @@ impl DynamicValidator {
6955 validators. into_iter ( ) . map ( |val| * val. pubkey ( ) ) . collect ( )
7056 }
7157
72- fn validators_order_by_weight ( & self , hash : BlockHash ) -> WeightOrderedValidators {
73- let mut validators = self . next_validators ( hash) ;
74- // Should we cache the sorted validator?
75- validators. sort_unstable_by_key ( |v| {
76- (
77- Reverse ( v. weight ( ) ) ,
78- Reverse ( v. deposit ( ) ) ,
79- v. nominated_at_block_number ( ) ,
80- v. nominated_at_transaction_index ( ) ,
81- )
82- } ) ;
83- WeightOrderedValidators ( validators. into_iter ( ) . map ( |val| * val. pubkey ( ) ) . collect ( ) )
84- }
85-
8658 pub fn proposer_index ( & self , parent : BlockHash , proposed_view : u64 ) -> usize {
8759 let propser = self . next_block_proposer ( & parent, proposed_view) ;
8860 self . get_index ( & parent, & propser) . expect ( "We know propser is included in a validator set" )
@@ -138,11 +110,22 @@ impl ValidatorSet for DynamicValidator {
138110 self . validators ( * parent) . binary_search ( public) . ok ( )
139111 }
140112
113+ // This code assumes that validator set is not changed.
114+ // Later this code should be moved to UpdateConsensus
141115 fn next_block_proposer ( & self , parent : & BlockHash , view : u64 ) -> Public {
142- let validators = self . validators_order_by_weight ( * parent) ;
143- let n_validators = validators. len ( ) ;
144- let index = WeightIndex ( view as usize % n_validators) ;
145- * validators. get ( index) . unwrap ( )
116+ let client: Arc < dyn ConsensusClient > = self . client . read ( ) . as_ref ( ) . and_then ( Weak :: upgrade) . unwrap ( ) ;
117+ let parent_header = client. block_header ( & BlockId :: from ( * parent) ) . expect ( "Parent must be finalized" ) ;
118+ if parent_header. number ( ) == 0 {
119+ return self . get ( parent, 0 )
120+ }
121+
122+ let proposer = parent_header. author ( ) ;
123+ let grand_parent = parent_header. parent_hash ( ) ;
124+ let prev_proposer_idx =
125+ self . get_index ( & grand_parent, & proposer) . expect ( "The proposer must be in the validator set" ) ;
126+ let proposer_index = prev_proposer_idx + 1 + view as usize ;
127+ ctrace ! ( ENGINE , "Proposer index: {}" , proposer_index) ;
128+ self . get ( & parent, proposer_index)
146129 }
147130
148131 fn count ( & self , parent : & BlockHash ) -> usize {
0 commit comments