@@ -36,9 +36,10 @@ use std::ops::Range;
36
36
use std:: sync:: atomic:: { AtomicUsize , Ordering as AtomicOrder } ;
37
37
use std:: sync:: Arc ;
38
38
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 } ;
40
40
use cmerkle:: skewed_merkle_root;
41
41
use cnetwork:: NodeId ;
42
+ use cstate:: tests:: helpers:: empty_top_state;
42
43
use cstate:: { FindActionHandler , StateDB , TopLevelState } ;
43
44
use ctimer:: { TimeoutHandler , TimerToken } ;
44
45
use ctypes:: transaction:: { Action , Transaction } ;
@@ -58,6 +59,7 @@ use crate::client::{
58
59
AccountData , BlockChainClient , BlockChainTrait , BlockProducer , BlockStatus , EngineInfo , ImportBlock ,
59
60
MiningBlockChainClient , StateInfo , StateOrBlock , TermInfo ,
60
61
} ;
62
+ use crate :: consensus:: stake:: { Validator , Validators } ;
61
63
use crate :: consensus:: EngineError ;
62
64
use crate :: db:: { COL_STATE , NUM_COLUMNS } ;
63
65
use crate :: encoded;
@@ -102,6 +104,10 @@ pub struct TestBlockChainClient {
102
104
pub history : RwLock < Option < u64 > > ,
103
105
/// Term ID
104
106
pub term_id : Option < u64 > ,
107
+ /// Fixed validator keys
108
+ pub validator_keys : RwLock < HashMap < Public , Private > > ,
109
+ /// Fixed validators
110
+ pub validators : Validators ,
105
111
}
106
112
107
113
impl Default for TestBlockChainClient {
@@ -154,6 +160,8 @@ impl TestBlockChainClient {
154
160
latest_block_timestamp : RwLock :: new ( 10_000_000 ) ,
155
161
history : RwLock :: new ( None ) ,
156
162
term_id : None ,
163
+ validator_keys : RwLock :: new ( HashMap :: new ( ) ) ,
164
+ validators : Validators :: from_vector_to_test ( vec ! [ ] ) ,
157
165
} ;
158
166
159
167
// insert genesis hash.
@@ -308,6 +316,26 @@ impl TestBlockChainClient {
308
316
pub fn set_history ( & self , h : Option < u64 > ) {
309
317
* self . history . write ( ) = h;
310
318
}
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
+ }
311
339
}
312
340
313
341
pub fn get_temp_state_db ( ) -> StateDB {
@@ -627,6 +655,10 @@ impl TermInfo for TestBlockChainClient {
627
655
628
656
impl StateInfo for TestBlockChainClient {
629
657
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)
631
663
}
632
664
}
0 commit comments