From df0fa06e8a38b5a708d08d4782cbd0578c42bbd5 Mon Sep 17 00:00:00 2001 From: debris Date: Wed, 10 Feb 2016 22:16:25 +0100 Subject: [PATCH] applied client interface changes to sync tests --- ethcore/src/blockchain.rs | 2 +- ethcore/src/tests/client.rs | 10 +++---- sync/src/chain.rs | 1 + sync/src/tests/chain.rs | 8 ++--- sync/src/tests/helpers.rs | 59 +++++++++++++------------------------ 5 files changed, 32 insertions(+), 48 deletions(-) diff --git a/ethcore/src/blockchain.rs b/ethcore/src/blockchain.rs index e75676855..9240ff800 100644 --- a/ethcore/src/blockchain.rs +++ b/ethcore/src/blockchain.rs @@ -859,7 +859,7 @@ mod tests { let transactions = bc.transactions(&b1_hash).unwrap(); assert_eq!(transactions.len(), 7); for t in transactions { - assert_eq!(bc.transaction(&t.hash()).unwrap(), t); + assert_eq!(bc.transaction(&bc.transaction_address(&t.hash()).unwrap()).unwrap(), t); } } } diff --git a/ethcore/src/tests/client.rs b/ethcore/src/tests/client.rs index 697647187..8132b26cf 100644 --- a/ethcore/src/tests/client.rs +++ b/ethcore/src/tests/client.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with Parity. If not, see . -use client::{BlockChainClient,Client}; +use client::{BlockChainClient, Client, BlockId}; use tests::helpers::*; use common::*; @@ -44,7 +44,7 @@ fn imports_good_block() { client.flush_queue(); client.import_verified_blocks(&IoChannel::disconnected()); - let block = client.block_header_at(1).unwrap(); + let block = client.block_header(BlockId::Number(1)).unwrap(); assert!(!block.is_empty()); } @@ -53,7 +53,7 @@ fn query_none_block() { let dir = RandomTempPath::new(); let client = Client::new(get_test_spec(), dir.as_path(), IoChannel::disconnected()).unwrap(); - let non_existant = client.block_header_at(188); + let non_existant = client.block_header(BlockId::Number(188)); assert!(non_existant.is_none()); } @@ -61,7 +61,7 @@ fn query_none_block() { fn query_bad_block() { let client_result = get_test_client_with_blocks(vec![get_bad_state_dummy_block()]); let client = client_result.reference(); - let bad_block:Option = client.block_header_at(1); + let bad_block:Option = client.block_header(BlockId::Number(1)); assert!(bad_block.is_none()); } @@ -80,7 +80,7 @@ fn returns_chain_info() { fn imports_block_sequence() { let client_result = generate_dummy_client(6); let client = client_result.reference(); - let block = client.block_header_at(5).unwrap(); + let block = client.block_header(BlockId::Number(5)).unwrap(); assert!(!block.is_empty()); } diff --git a/sync/src/chain.rs b/sync/src/chain.rs index b3dfc71f5..91e1ccbd5 100644 --- a/sync/src/chain.rs +++ b/sync/src/chain.rs @@ -1061,6 +1061,7 @@ impl ChainSync { for block_hash in route.blocks { let mut hash_rlp = RlpStream::new_list(2); let difficulty = chain.block_total_difficulty(BlockId::Hash(block_hash.clone())).expect("Mallformed block without a difficulty on the chain!"); + hash_rlp.append(&block_hash); hash_rlp.append(&difficulty); rlp_stream.append_raw(&hash_rlp.out(), 1); diff --git a/sync/src/tests/chain.rs b/sync/src/tests/chain.rs index 6526d8500..f560f4ca6 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}; +use ethcore::client::{BlockChainClient, BlockId}; use io::SyncIo; use chain::{SyncState}; use super::helpers::*; @@ -27,7 +27,7 @@ fn two_peers() { net.peer_mut(1).chain.add_blocks(1000, false); net.peer_mut(2).chain.add_blocks(1000, false); net.sync(); - assert!(net.peer(0).chain.block_at(1000).is_some()); + assert!(net.peer(0).chain.block(BlockId::Number(1000)).is_some()); assert_eq!(net.peer(0).chain.blocks.read().unwrap().deref(), net.peer(1).chain.blocks.read().unwrap().deref()); } @@ -60,7 +60,7 @@ fn empty_blocks() { net.peer_mut(2).chain.add_blocks(5, n % 2 == 0); } net.sync(); - assert!(net.peer(0).chain.block_at(1000).is_some()); + assert!(net.peer(0).chain.block(BlockId::Number(1000)).is_some()); assert_eq!(net.peer(0).chain.blocks.read().unwrap().deref(), net.peer(1).chain.blocks.read().unwrap().deref()); } @@ -148,4 +148,4 @@ fn propagade_blocks() { assert!(!net.peer(0).queue.is_empty()); // NEW_BLOCK_PACKET assert_eq!(0x07, net.peer(0).queue[0].packet_id); -} \ No newline at end of file +} diff --git a/sync/src/tests/helpers.rs b/sync/src/tests/helpers.rs index f8c08dc93..384b5bd65 100644 --- a/sync/src/tests/helpers.rs +++ b/sync/src/tests/helpers.rs @@ -15,7 +15,7 @@ // along with Parity. If not, see . use util::*; -use ethcore::client::{BlockChainClient, BlockStatus, TreeRoute, BlockChainInfo}; +use ethcore::client::{BlockChainClient, BlockStatus, TreeRoute, BlockChainInfo, TransactionId, BlockId}; use ethcore::block_queue::BlockQueueInfo; use ethcore::header::{Header as BlockHeader, BlockNumber}; use ethcore::error::*; @@ -23,7 +23,6 @@ use io::SyncIo; use chain::{ChainSync}; use ethcore::receipt::Receipt; use ethcore::transaction::LocalizedTransaction; -use ethcore::blockchain::TransactionId; pub struct TestBlockChainClient { pub blocks: RwLock>, @@ -77,10 +76,17 @@ impl TestBlockChainClient { let index = blocks_read.len() - delta; blocks_read[&index].clone() } + + fn block_hash(&self, id: BlockId) -> Option { + match id { + BlockId::Hash(hash) => Some(hash), + BlockId::Number(n) => self.numbers.read().unwrap().get(&(n as usize)).cloned() + } + } } impl BlockChainClient for TestBlockChainClient { - fn block_total_difficulty(&self, _h: &H256) -> Option { + fn block_total_difficulty(&self, _id: BlockId) -> Option { Some(U256::zero()) } @@ -92,51 +98,28 @@ impl BlockChainClient for TestBlockChainClient { unimplemented!(); } - fn block_header(&self, h: &H256) -> Option { - self.blocks.read().unwrap().get(h).map(|r| Rlp::new(r).at(0).as_raw().to_vec()) + fn block_header(&self, id: BlockId) -> Option { + self.block_hash(id).and_then(|hash| self.blocks.read().unwrap().get(&hash).map(|r| Rlp::new(r).at(0).as_raw().to_vec())) } - fn block_body(&self, h: &H256) -> Option { - self.blocks.read().unwrap().get(h).map(|r| { + fn block_body(&self, id: BlockId) -> Option { + self.block_hash(id).and_then(|hash| self.blocks.read().unwrap().get(&hash).map(|r| { let mut stream = RlpStream::new_list(2); stream.append_raw(Rlp::new(&r).at(1).as_raw(), 1); stream.append_raw(Rlp::new(&r).at(2).as_raw(), 1); stream.out() - }) + })) } - fn block(&self, h: &H256) -> Option { - self.blocks.read().unwrap().get(h).cloned() + fn block(&self, id: BlockId) -> Option { + self.block_hash(id).and_then(|hash| self.blocks.read().unwrap().get(&hash).cloned()) } - fn block_status(&self, h: &H256) -> BlockStatus { - match self.blocks.read().unwrap().get(h) { - Some(_) => BlockStatus::InChain, - None => BlockStatus::Unknown - } - } - - fn block_total_difficulty_at(&self, _number: BlockNumber) -> Option { - unimplemented!(); - } - - fn block_header_at(&self, n: BlockNumber) -> Option { - self.numbers.read().unwrap().get(&(n as usize)).and_then(|h| self.block_header(h)) - } - - fn block_body_at(&self, n: BlockNumber) -> Option { - self.numbers.read().unwrap().get(&(n as usize)).and_then(|h| self.block_body(h)) - } - - fn block_at(&self, n: BlockNumber) -> Option { - self.numbers.read().unwrap().get(&(n as usize)).map(|h| self.blocks.read().unwrap().get(h).unwrap().clone()) - } - - fn block_status_at(&self, n: BlockNumber) -> BlockStatus { - if (n as usize) < self.blocks.read().unwrap().len() { - BlockStatus::InChain - } else { - BlockStatus::Unknown + fn block_status(&self, id: BlockId) -> BlockStatus { + match id { + BlockId::Number(number) if (number as usize) < self.blocks.read().unwrap().len() => BlockStatus::InChain, + BlockId::Hash(ref hash) if self.blocks.read().unwrap().get(hash).is_some() => BlockStatus::InChain, + _ => BlockStatus::Unknown } }