Skip to content

Commit d0fface

Browse files
committed
Add test cases for verification of reportDoubleVote
1 parent 0a6af40 commit d0fface

File tree

6 files changed

+333
-7
lines changed

6 files changed

+333
-7
lines changed

core/src/client/test_client.rs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@ use std::ops::Range;
3636
use std::sync::atomic::{AtomicUsize, Ordering as AtomicOrder};
3737
use 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};
4040
use cmerkle::skewed_merkle_root;
4141
use cnetwork::NodeId;
42+
use cstate::tests::helpers::empty_top_state;
4243
use cstate::{FindActionHandler, StateDB, TopLevelState};
4344
use ctimer::{TimeoutHandler, TimerToken};
4445
use 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};
6163
use crate::consensus::EngineError;
6264
use crate::db::{COL_STATE, NUM_COLUMNS};
6365
use 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

107113
impl 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

313341
pub fn get_temp_state_db() -> StateDB {
@@ -627,6 +655,10 @@ impl TermInfo for TestBlockChainClient {
627655

628656
impl 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
}

core/src/consensus/mod.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ mod null_engine;
2121
mod signer;
2222
mod simple_poa;
2323
mod solo;
24-
mod stake;
24+
pub mod stake;
2525
mod tendermint;
2626
mod validator_set;
2727
mod vote_collector;
@@ -31,7 +31,10 @@ pub use self::cuckoo::Cuckoo;
3131
pub use self::null_engine::NullEngine;
3232
pub use self::simple_poa::SimplePoA;
3333
pub use self::solo::Solo;
34-
pub use self::tendermint::{Tendermint, TendermintParams, TimeGapParams};
34+
pub use self::tendermint::{
35+
message_info_rlp, ConsensusMessage, Height, Step, Tendermint, TendermintParams, TimeGapParams, View, VoteOn,
36+
VoteStep,
37+
};
3538
pub use self::validator_set::validator_list::RoundRobinValidator;
3639
pub use self::validator_set::ValidatorSet;
3740
pub use self::vote_collector::Message;
@@ -49,7 +52,6 @@ use ctypes::{CommonParams, Header};
4952
use primitives::{Bytes, H256, U256};
5053

5154
use self::bit_set::BitSet;
52-
use self::tendermint::types::View;
5355
use crate::account_provider::AccountProvider;
5456
use crate::block::{ExecutedBlock, SealedBlock};
5557
use crate::client::ConsensusClient;

core/src/consensus/stake/action_data.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,15 @@ pub struct Validator {
238238
}
239239

240240
impl Validator {
241+
pub fn new_for_test(delegation: StakeQuantity, deposit: Deposit, pubkey: Public) -> Self {
242+
Self {
243+
weight: delegation,
244+
delegation,
245+
deposit,
246+
pubkey,
247+
}
248+
}
249+
241250
fn new(delegation: StakeQuantity, deposit: Deposit, pubkey: Public) -> Self {
242251
Self {
243252
weight: delegation,
@@ -263,6 +272,10 @@ impl Validator {
263272
#[derive(Debug)]
264273
pub struct Validators(Vec<Validator>);
265274
impl Validators {
275+
pub fn from_vector_to_test(vec: Vec<Validator>) -> Self {
276+
Validators(vec)
277+
}
278+
266279
pub fn load_from_state(state: &TopLevelState) -> StateResult<Self> {
267280
let key = &*VALIDATORS_KEY;
268281
let validators = state.action_data(&key)?.map(|data| decode_list(&data)).unwrap_or_default();

0 commit comments

Comments
 (0)