From f553997e49900a29075fd18db6cd5d8182414295 Mon Sep 17 00:00:00 2001 From: arkpar Date: Wed, 27 Jul 2016 15:12:37 +0200 Subject: [PATCH] Test --- sync/src/tests/chain.rs | 21 ++++++++++++++++++++- sync/src/tests/helpers.rs | 10 ++++++++-- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/sync/src/tests/chain.rs b/sync/src/tests/chain.rs index e15d804e227..2a84b0f991c 100644 --- a/sync/src/tests/chain.rs +++ b/sync/src/tests/chain.rs @@ -15,7 +15,7 @@ // along with Parity. If not, see . use util::*; -use ethcore::client::{BlockChainClient, BlockID, EachBlockWith}; +use ethcore::client::{TestBlockChainClient, BlockChainClient, BlockID, EachBlockWith}; use chain::{SyncState}; use super::helpers::*; @@ -95,6 +95,25 @@ fn forked() { assert_eq!(net.peer(2).chain.numbers.read().deref(), &peer1_chain); } +#[test] +fn net_hard_fork() { + ::env_logger::init().ok(); + let ref_client = TestBlockChainClient::new(); + ref_client.add_blocks(50, EachBlockWith::Uncle); + { + let mut net = TestNet::new_with_fork(2, Some((50, ref_client.block_hash(BlockID::Number(50)).unwrap()))); + net.peer_mut(0).chain.add_blocks(100, EachBlockWith::Uncle); + net.sync(); + assert_eq!(net.peer(1).chain.chain_info().best_block_number, 100); + } + { + let mut net = TestNet::new_with_fork(2, Some((50, ref_client.block_hash(BlockID::Number(50)).unwrap()))); + net.peer_mut(0).chain.add_blocks(100, EachBlockWith::Nothing); + net.sync(); + assert_eq!(net.peer(1).chain.chain_info().best_block_number, 0); + } +} + #[test] fn restart() { let mut net = TestNet::new(3); diff --git a/sync/src/tests/helpers.rs b/sync/src/tests/helpers.rs index d5fda2e7040..68820ec86bd 100644 --- a/sync/src/tests/helpers.rs +++ b/sync/src/tests/helpers.rs @@ -16,6 +16,7 @@ use util::*; use ethcore::client::{TestBlockChainClient, BlockChainClient}; +use ethcore::header::BlockNumber; use io::SyncIo; use chain::ChainSync; use ::SyncConfig; @@ -89,13 +90,19 @@ pub struct TestNet { impl TestNet { pub fn new(n: usize) -> TestNet { + Self::new_with_fork(n, None) + } + + pub fn new_with_fork(n: usize, fork: Option<(BlockNumber, H256)>) -> TestNet { let mut net = TestNet { peers: Vec::new(), started: false, }; for _ in 0..n { let chain = TestBlockChainClient::new(); - let sync = ChainSync::new(SyncConfig::default(), &chain); + let mut config = SyncConfig::default(); + config.fork_block = fork; + let sync = ChainSync::new(config, &chain); net.peers.push(TestPeer { sync: RwLock::new(sync), chain: chain, @@ -104,7 +111,6 @@ impl TestNet { } net } - pub fn peer(&self, i: usize) -> &TestPeer { self.peers.get(i).unwrap() }