@@ -24,7 +24,7 @@ use parking_lot::RwLock;
2424use super :: { RoundRobinValidator , ValidatorSet } ;
2525use crate :: client:: ConsensusClient ;
2626use crate :: consensus:: bit_set:: BitSet ;
27- use crate :: consensus:: stake:: { get_validators , Validator } ;
27+ use crate :: consensus:: stake:: { CurrentValidators , NextValidators , Validator } ;
2828use crate :: consensus:: EngineError ;
2929
3030/// Validator set containing a known set of public keys.
@@ -41,10 +41,10 @@ impl DynamicValidator {
4141 }
4242 }
4343
44- fn validators ( & self , parent : BlockHash ) -> Option < Vec < Validator > > {
44+ fn next_validators ( & self , hash : BlockHash ) -> Option < Vec < Validator > > {
4545 let client: Arc < dyn ConsensusClient > =
4646 self . client . read ( ) . as_ref ( ) . and_then ( Weak :: upgrade) . expect ( "Client is not initialized" ) ;
47- let block_id = parent . into ( ) ;
47+ let block_id = hash . into ( ) ;
4848 let term_id = client. current_term_id ( block_id) . expect (
4949 "valdators() is called when creating a block or verifying a block.
5050 Minor creates a block only when the parent block is imported.
@@ -54,7 +54,7 @@ impl DynamicValidator {
5454 return None
5555 }
5656 let state = client. state_at ( block_id) ?;
57- let validators = get_validators ( & state) . unwrap ( ) ;
57+ let validators = NextValidators :: load_from_state ( & state) . unwrap ( ) ;
5858 if validators. is_empty ( ) {
5959 None
6060 } else {
@@ -64,12 +64,39 @@ impl DynamicValidator {
6464 }
6565 }
6666
67- fn validators_pubkey ( & self , parent : BlockHash ) -> Option < Vec < Public > > {
68- self . validators ( parent) . map ( |validators| validators. into_iter ( ) . map ( |val| * val. pubkey ( ) ) . collect ( ) )
67+ fn current_validators ( & self , hash : BlockHash ) -> Option < Vec < Validator > > {
68+ let client: Arc < dyn ConsensusClient > =
69+ self . client . read ( ) . as_ref ( ) . and_then ( Weak :: upgrade) . expect ( "Client is not initialized" ) ;
70+ let block_id = hash. into ( ) ;
71+ let term_id = client. current_term_id ( block_id) . expect (
72+ "valdators() is called when creating a block or verifying a block.
73+ Minor creates a block only when the parent block is imported.
74+ The n'th block is verified only when the parent block is imported." ,
75+ ) ;
76+ if term_id == 0 {
77+ return None
78+ }
79+ let state = client. state_at ( block_id) ?;
80+ let validators = CurrentValidators :: load_from_state ( & state) . unwrap ( ) ;
81+ if validators. is_empty ( ) {
82+ None
83+ } else {
84+ let mut validators: Vec < _ > = validators. into ( ) ;
85+ validators. reverse ( ) ;
86+ Some ( validators)
87+ }
88+ }
89+
90+ fn validators_pubkey ( & self , hash : BlockHash ) -> Option < Vec < Public > > {
91+ self . next_validators ( hash) . map ( |validators| validators. into_iter ( ) . map ( |val| * val. pubkey ( ) ) . collect ( ) )
92+ }
93+
94+ fn current_validators_pubkey ( & self , hash : BlockHash ) -> Option < Vec < Public > > {
95+ self . current_validators ( hash) . map ( |validators| validators. into_iter ( ) . map ( |val| * val. pubkey ( ) ) . collect ( ) )
6996 }
7097
7198 pub fn proposer_index ( & self , parent : BlockHash , prev_proposer_index : usize , proposed_view : usize ) -> usize {
72- if let Some ( validators) = self . validators ( parent) {
99+ if let Some ( validators) = self . next_validators ( parent) {
73100 let num_validators = validators. len ( ) ;
74101 proposed_view % num_validators
75102 } else {
@@ -136,15 +163,15 @@ impl ValidatorSet for DynamicValidator {
136163 }
137164
138165 fn count ( & self , parent : & BlockHash ) -> usize {
139- if let Some ( validators) = self . validators ( * parent) {
166+ if let Some ( validators) = self . next_validators ( * parent) {
140167 validators. len ( )
141168 } else {
142169 self . initial_list . count ( parent)
143170 }
144171 }
145172
146173 fn check_enough_votes ( & self , parent : & BlockHash , votes : & BitSet ) -> Result < ( ) , EngineError > {
147- if let Some ( validators) = self . validators ( * parent) {
174+ if let Some ( validators) = self . next_validators ( * parent) {
148175 let mut voted_delegation = 0u64 ;
149176 let n_validators = validators. len ( ) ;
150177 for index in votes. true_index_iter ( ) {
@@ -181,11 +208,19 @@ impl ValidatorSet for DynamicValidator {
181208 * client_lock = Some ( client) ;
182209 }
183210
184- fn addresses ( & self , parent : & BlockHash ) -> Vec < Address > {
185- if let Some ( validators) = self . validators_pubkey ( * parent) {
211+ fn current_addresses ( & self , hash : & BlockHash ) -> Vec < Address > {
212+ if let Some ( validators) = self . current_validators_pubkey ( * hash) {
213+ validators. iter ( ) . map ( public_to_address) . collect ( )
214+ } else {
215+ self . initial_list . next_addresses ( hash)
216+ }
217+ }
218+
219+ fn next_addresses ( & self , hash : & BlockHash ) -> Vec < Address > {
220+ if let Some ( validators) = self . validators_pubkey ( * hash) {
186221 validators. iter ( ) . map ( public_to_address) . collect ( )
187222 } else {
188- self . initial_list . addresses ( parent )
223+ self . initial_list . next_addresses ( hash )
189224 }
190225 }
191226}
0 commit comments