applied client interface changes to sync tests

This commit is contained in:
debris 2016-02-10 22:16:25 +01:00
parent 484a4d8bdd
commit df0fa06e8a
5 changed files with 32 additions and 48 deletions

View File

@ -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);
}
}
}

View File

@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
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<Bytes> = client.block_header_at(1);
let bad_block:Option<Bytes> = 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());
}

View File

@ -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);

View File

@ -15,7 +15,7 @@
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
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);
}
}

View File

@ -15,7 +15,7 @@
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
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<HashMap<H256, Bytes>>,
@ -77,10 +76,17 @@ impl TestBlockChainClient {
let index = blocks_read.len() - delta;
blocks_read[&index].clone()
}
fn block_hash(&self, id: BlockId) -> Option<H256> {
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<U256> {
fn block_total_difficulty(&self, _id: BlockId) -> Option<U256> {
Some(U256::zero())
}
@ -92,51 +98,28 @@ impl BlockChainClient for TestBlockChainClient {
unimplemented!();
}
fn block_header(&self, h: &H256) -> Option<Bytes> {
self.blocks.read().unwrap().get(h).map(|r| Rlp::new(r).at(0).as_raw().to_vec())
fn block_header(&self, id: BlockId) -> Option<Bytes> {
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<Bytes> {
self.blocks.read().unwrap().get(h).map(|r| {
fn block_body(&self, id: BlockId) -> Option<Bytes> {
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<Bytes> {
self.blocks.read().unwrap().get(h).cloned()
fn block(&self, id: BlockId) -> Option<Bytes> {
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<U256> {
unimplemented!();
}
fn block_header_at(&self, n: BlockNumber) -> Option<Bytes> {
self.numbers.read().unwrap().get(&(n as usize)).and_then(|h| self.block_header(h))
}
fn block_body_at(&self, n: BlockNumber) -> Option<Bytes> {
self.numbers.read().unwrap().get(&(n as usize)).and_then(|h| self.block_body(h))
}
fn block_at(&self, n: BlockNumber) -> Option<Bytes> {
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
}
}