@@ -22,29 +22,15 @@ use ckey::Ed25519Public as Public;
22
22
use cstate:: { CurrentValidatorSet , NextValidatorSet , SimpleValidator } ;
23
23
use ctypes:: util:: unexpected:: OutOfBounds ;
24
24
use ctypes:: BlockHash ;
25
+ use ctypes:: BlockId ;
25
26
use parking_lot:: RwLock ;
26
- use std:: cmp:: Reverse ;
27
27
use std:: sync:: { Arc , Weak } ;
28
28
29
29
#[ derive( Default ) ]
30
30
pub struct DynamicValidator {
31
31
client : RwLock < Option < Weak < dyn ConsensusClient > > > ,
32
32
}
33
33
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
-
48
34
impl DynamicValidator {
49
35
fn next_validators ( & self , hash : BlockHash ) -> Vec < SimpleValidator > {
50
36
let client: Arc < dyn ConsensusClient > =
@@ -69,20 +55,6 @@ impl DynamicValidator {
69
55
validators. into_iter ( ) . map ( |val| * val. pubkey ( ) ) . collect ( )
70
56
}
71
57
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
-
86
58
pub fn proposer_index ( & self , parent : BlockHash , proposed_view : u64 ) -> usize {
87
59
let propser = self . next_block_proposer ( & parent, proposed_view) ;
88
60
self . get_index ( & parent, & propser) . expect ( "We know propser is included in a validator set" )
@@ -138,11 +110,22 @@ impl ValidatorSet for DynamicValidator {
138
110
self . validators ( * parent) . binary_search ( public) . ok ( )
139
111
}
140
112
113
+ // This code assumes that validator set is not changed.
114
+ // Later this code should be moved to UpdateConsensus
141
115
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)
146
129
}
147
130
148
131
fn count ( & self , parent : & BlockHash ) -> usize {
0 commit comments