@@ -36,9 +36,10 @@ use std::ops::Range;
3636use std:: sync:: atomic:: { AtomicUsize , Ordering as AtomicOrder } ;
3737use std:: sync:: Arc ;
3838
39- use ckey:: { public_to_address, Address , Generator , NetworkId , PlatformAddress , Public , Random } ;
39+ use ckey:: { public_to_address, Address , Generator , KeyPair , NetworkId , PlatformAddress , Private , Public , Random } ;
4040use cmerkle:: skewed_merkle_root;
4141use cnetwork:: NodeId ;
42+ use cstate:: tests:: helpers:: empty_top_state;
4243use cstate:: { FindActionHandler , StateDB , TopLevelState } ;
4344use ctimer:: { TimeoutHandler , TimerToken } ;
4445use ctypes:: transaction:: { Action , Transaction } ;
@@ -58,6 +59,7 @@ use crate::client::{
5859 AccountData , BlockChainClient , BlockChainTrait , BlockProducer , BlockStatus , EngineInfo , ImportBlock ,
5960 MiningBlockChainClient , StateInfo , StateOrBlock , TermInfo ,
6061} ;
62+ use crate :: consensus:: stake:: { Validator , Validators } ;
6163use crate :: consensus:: EngineError ;
6264use crate :: db:: { COL_STATE , NUM_COLUMNS } ;
6365use crate :: encoded;
@@ -102,6 +104,10 @@ pub struct TestBlockChainClient {
102104 pub history : RwLock < Option < u64 > > ,
103105 /// Term ID
104106 pub term_id : Option < u64 > ,
107+ /// Fixed validator keys
108+ pub validator_keys : RwLock < HashMap < Public , Private > > ,
109+ /// Fixed validators
110+ pub validators : Validators ,
105111}
106112
107113impl Default for TestBlockChainClient {
@@ -154,6 +160,8 @@ impl TestBlockChainClient {
154160 latest_block_timestamp : RwLock :: new ( 10_000_000 ) ,
155161 history : RwLock :: new ( None ) ,
156162 term_id : None ,
163+ validator_keys : RwLock :: new ( HashMap :: new ( ) ) ,
164+ validators : Validators :: from_vector_to_test ( vec ! [ ] ) ,
157165 } ;
158166
159167 // insert genesis hash.
@@ -308,6 +316,26 @@ impl TestBlockChainClient {
308316 pub fn set_history ( & self , h : Option < u64 > ) {
309317 * self . history . write ( ) = h;
310318 }
319+
320+ /// Set validators which can be brought from state.
321+ pub fn set_random_validators ( & mut self , count : usize ) {
322+ let mut pubkeys: Vec < Public > = vec ! [ ] ;
323+ for _ in 0 ..count {
324+ let random_priv_key = Private :: from ( H256 :: random ( ) ) ;
325+ let key_pair = KeyPair :: from_private ( random_priv_key) . unwrap ( ) ;
326+ self . validator_keys . write ( ) . insert ( * key_pair. public ( ) , * key_pair. private ( ) ) ;
327+ pubkeys. push ( * key_pair. public ( ) ) ;
328+ }
329+ let fixed_validators: Validators = Validators :: from_vector_to_test (
330+ pubkeys. into_iter ( ) . map ( |pubkey| Validator :: new_for_test ( 0 , 0 , pubkey) ) . collect ( ) ,
331+ ) ;
332+
333+ self . validators = fixed_validators;
334+ }
335+
336+ pub fn get_validators ( & self ) -> & Validators {
337+ & self . validators
338+ }
311339}
312340
313341pub fn get_temp_state_db ( ) -> StateDB {
@@ -627,6 +655,10 @@ impl TermInfo for TestBlockChainClient {
627655
628656impl StateInfo for TestBlockChainClient {
629657 fn state_at ( & self , _id : BlockId ) -> Option < TopLevelState > {
630- None
658+ let statedb = StateDB :: new_with_memorydb ( ) ;
659+ let mut top_state = empty_top_state ( statedb) ;
660+ let _ = self . validators . save_to_state ( & mut top_state) ;
661+
662+ Some ( top_state)
631663 }
632664}
0 commit comments