applied client interface changes to sync tests
This commit is contained in:
parent
484a4d8bdd
commit
df0fa06e8a
@ -859,7 +859,7 @@ mod tests {
|
|||||||
let transactions = bc.transactions(&b1_hash).unwrap();
|
let transactions = bc.transactions(&b1_hash).unwrap();
|
||||||
assert_eq!(transactions.len(), 7);
|
assert_eq!(transactions.len(), 7);
|
||||||
for t in transactions {
|
for t in transactions {
|
||||||
assert_eq!(bc.transaction(&t.hash()).unwrap(), t);
|
assert_eq!(bc.transaction(&bc.transaction_address(&t.hash()).unwrap()).unwrap(), t);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -14,7 +14,7 @@
|
|||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
use client::{BlockChainClient,Client};
|
use client::{BlockChainClient, Client, BlockId};
|
||||||
use tests::helpers::*;
|
use tests::helpers::*;
|
||||||
use common::*;
|
use common::*;
|
||||||
|
|
||||||
@ -44,7 +44,7 @@ fn imports_good_block() {
|
|||||||
client.flush_queue();
|
client.flush_queue();
|
||||||
client.import_verified_blocks(&IoChannel::disconnected());
|
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());
|
assert!(!block.is_empty());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -53,7 +53,7 @@ fn query_none_block() {
|
|||||||
let dir = RandomTempPath::new();
|
let dir = RandomTempPath::new();
|
||||||
let client = Client::new(get_test_spec(), dir.as_path(), IoChannel::disconnected()).unwrap();
|
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());
|
assert!(non_existant.is_none());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -61,7 +61,7 @@ fn query_none_block() {
|
|||||||
fn query_bad_block() {
|
fn query_bad_block() {
|
||||||
let client_result = get_test_client_with_blocks(vec![get_bad_state_dummy_block()]);
|
let client_result = get_test_client_with_blocks(vec![get_bad_state_dummy_block()]);
|
||||||
let client = client_result.reference();
|
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());
|
assert!(bad_block.is_none());
|
||||||
}
|
}
|
||||||
@ -80,7 +80,7 @@ fn returns_chain_info() {
|
|||||||
fn imports_block_sequence() {
|
fn imports_block_sequence() {
|
||||||
let client_result = generate_dummy_client(6);
|
let client_result = generate_dummy_client(6);
|
||||||
let client = client_result.reference();
|
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());
|
assert!(!block.is_empty());
|
||||||
}
|
}
|
||||||
|
@ -1061,6 +1061,7 @@ impl ChainSync {
|
|||||||
for block_hash in route.blocks {
|
for block_hash in route.blocks {
|
||||||
let mut hash_rlp = RlpStream::new_list(2);
|
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!");
|
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(&block_hash);
|
||||||
hash_rlp.append(&difficulty);
|
hash_rlp.append(&difficulty);
|
||||||
rlp_stream.append_raw(&hash_rlp.out(), 1);
|
rlp_stream.append_raw(&hash_rlp.out(), 1);
|
||||||
|
@ -15,7 +15,7 @@
|
|||||||
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
use util::*;
|
use util::*;
|
||||||
use ethcore::client::{BlockChainClient};
|
use ethcore::client::{BlockChainClient, BlockId};
|
||||||
use io::SyncIo;
|
use io::SyncIo;
|
||||||
use chain::{SyncState};
|
use chain::{SyncState};
|
||||||
use super::helpers::*;
|
use super::helpers::*;
|
||||||
@ -27,7 +27,7 @@ fn two_peers() {
|
|||||||
net.peer_mut(1).chain.add_blocks(1000, false);
|
net.peer_mut(1).chain.add_blocks(1000, false);
|
||||||
net.peer_mut(2).chain.add_blocks(1000, false);
|
net.peer_mut(2).chain.add_blocks(1000, false);
|
||||||
net.sync();
|
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());
|
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.peer_mut(2).chain.add_blocks(5, n % 2 == 0);
|
||||||
}
|
}
|
||||||
net.sync();
|
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());
|
assert_eq!(net.peer(0).chain.blocks.read().unwrap().deref(), net.peer(1).chain.blocks.read().unwrap().deref());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -15,7 +15,7 @@
|
|||||||
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
use util::*;
|
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::block_queue::BlockQueueInfo;
|
||||||
use ethcore::header::{Header as BlockHeader, BlockNumber};
|
use ethcore::header::{Header as BlockHeader, BlockNumber};
|
||||||
use ethcore::error::*;
|
use ethcore::error::*;
|
||||||
@ -23,7 +23,6 @@ use io::SyncIo;
|
|||||||
use chain::{ChainSync};
|
use chain::{ChainSync};
|
||||||
use ethcore::receipt::Receipt;
|
use ethcore::receipt::Receipt;
|
||||||
use ethcore::transaction::LocalizedTransaction;
|
use ethcore::transaction::LocalizedTransaction;
|
||||||
use ethcore::blockchain::TransactionId;
|
|
||||||
|
|
||||||
pub struct TestBlockChainClient {
|
pub struct TestBlockChainClient {
|
||||||
pub blocks: RwLock<HashMap<H256, Bytes>>,
|
pub blocks: RwLock<HashMap<H256, Bytes>>,
|
||||||
@ -77,10 +76,17 @@ impl TestBlockChainClient {
|
|||||||
let index = blocks_read.len() - delta;
|
let index = blocks_read.len() - delta;
|
||||||
blocks_read[&index].clone()
|
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 {
|
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())
|
Some(U256::zero())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -92,51 +98,28 @@ impl BlockChainClient for TestBlockChainClient {
|
|||||||
unimplemented!();
|
unimplemented!();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn block_header(&self, h: &H256) -> Option<Bytes> {
|
fn block_header(&self, id: BlockId) -> Option<Bytes> {
|
||||||
self.blocks.read().unwrap().get(h).map(|r| Rlp::new(r).at(0).as_raw().to_vec())
|
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> {
|
fn block_body(&self, id: BlockId) -> Option<Bytes> {
|
||||||
self.blocks.read().unwrap().get(h).map(|r| {
|
self.block_hash(id).and_then(|hash| self.blocks.read().unwrap().get(&hash).map(|r| {
|
||||||
let mut stream = RlpStream::new_list(2);
|
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(1).as_raw(), 1);
|
||||||
stream.append_raw(Rlp::new(&r).at(2).as_raw(), 1);
|
stream.append_raw(Rlp::new(&r).at(2).as_raw(), 1);
|
||||||
stream.out()
|
stream.out()
|
||||||
})
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn block(&self, h: &H256) -> Option<Bytes> {
|
fn block(&self, id: BlockId) -> Option<Bytes> {
|
||||||
self.blocks.read().unwrap().get(h).cloned()
|
self.block_hash(id).and_then(|hash| self.blocks.read().unwrap().get(&hash).cloned())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn block_status(&self, h: &H256) -> BlockStatus {
|
fn block_status(&self, id: BlockId) -> BlockStatus {
|
||||||
match self.blocks.read().unwrap().get(h) {
|
match id {
|
||||||
Some(_) => BlockStatus::InChain,
|
BlockId::Number(number) if (number as usize) < self.blocks.read().unwrap().len() => BlockStatus::InChain,
|
||||||
None => BlockStatus::Unknown
|
BlockId::Hash(ref hash) if self.blocks.read().unwrap().get(hash).is_some() => BlockStatus::InChain,
|
||||||
}
|
_ => 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
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user