openethereum/sync/src/tests/helpers.rs

186 lines
4.7 KiB
Rust
Raw Normal View History

2016-02-05 13:40:41 +01:00
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
use util::*;
2016-06-01 12:44:11 +02:00
use ethcore::client::{TestBlockChainClient, BlockChainClient};
2016-07-27 21:38:22 +02:00
use ethcore::header::BlockNumber;
use io::SyncIo;
use chain::ChainSync;
2016-02-24 21:23:58 +01:00
use ::SyncConfig;
2015-12-26 15:47:07 +01:00
2016-02-04 19:30:31 +01:00
pub struct TestIo<'p> {
pub chain: &'p mut TestBlockChainClient,
pub queue: &'p mut VecDeque<TestPacket>,
pub sender: Option<PeerId>,
2015-12-26 15:47:07 +01:00
}
impl<'p> TestIo<'p> {
2016-02-04 20:03:14 +01:00
pub fn new(chain: &'p mut TestBlockChainClient, queue: &'p mut VecDeque<TestPacket>, sender: Option<PeerId>) -> TestIo<'p> {
2015-12-26 15:47:07 +01:00
TestIo {
chain: chain,
queue: queue,
sender: sender
}
}
}
impl<'p> SyncIo for TestIo<'p> {
fn disable_peer(&mut self, _peer_id: PeerId) {
2015-12-26 15:47:07 +01:00
}
2016-02-02 14:54:46 +01:00
fn disconnect_peer(&mut self, _peer_id: PeerId) {
}
2016-06-17 18:26:54 +02:00
fn is_expired(&self) -> bool {
false
}
2016-01-10 15:08:57 +01:00
fn respond(&mut self, packet_id: PacketId, data: Vec<u8>) -> Result<(), UtilError> {
2015-12-26 15:47:07 +01:00
self.queue.push_back(TestPacket {
data: data,
packet_id: packet_id,
recipient: self.sender.unwrap()
});
Ok(())
}
2016-01-10 15:08:57 +01:00
fn send(&mut self, peer_id: PeerId, packet_id: PacketId, data: Vec<u8>) -> Result<(), UtilError> {
2015-12-26 15:47:07 +01:00
self.queue.push_back(TestPacket {
data: data,
packet_id: packet_id,
recipient: peer_id,
});
Ok(())
}
2016-05-31 22:24:32 +02:00
fn chain(&self) -> &BlockChainClient {
2015-12-26 15:47:07 +01:00
self.chain
}
}
2016-02-04 19:30:31 +01:00
pub struct TestPacket {
pub data: Bytes,
pub packet_id: PacketId,
pub recipient: PeerId,
2015-12-26 15:47:07 +01:00
}
2016-02-04 19:30:31 +01:00
pub struct TestPeer {
pub chain: TestBlockChainClient,
2016-06-20 17:28:48 +02:00
pub sync: RwLock<ChainSync>,
2016-02-04 19:30:31 +01:00
pub queue: VecDeque<TestPacket>,
2015-12-26 15:47:07 +01:00
}
2016-02-04 19:30:31 +01:00
pub struct TestNet {
pub peers: Vec<TestPeer>,
pub started: bool,
2015-12-26 15:47:07 +01:00
}
impl TestNet {
pub fn new(n: usize) -> TestNet {
2016-07-27 21:38:22 +02:00
Self::new_with_fork(n, None)
}
pub fn new_with_fork(n: usize, fork: Option<(BlockNumber, H256)>) -> TestNet {
2015-12-26 15:47:07 +01:00
let mut net = TestNet {
peers: Vec::new(),
2016-02-03 21:42:30 +01:00
started: false,
2015-12-26 15:47:07 +01:00
};
for _ in 0..n {
2016-05-16 14:41:41 +02:00
let chain = TestBlockChainClient::new();
2016-07-27 21:38:22 +02:00
let mut config = SyncConfig::default();
config.fork_block = fork;
let sync = ChainSync::new(config, &chain);
2015-12-26 15:47:07 +01:00
net.peers.push(TestPeer {
2016-06-20 17:28:48 +02:00
sync: RwLock::new(sync),
2016-05-16 14:41:41 +02:00
chain: chain,
2015-12-26 15:47:07 +01:00
queue: VecDeque::new(),
});
}
net
}
2015-12-27 00:48:03 +01:00
pub fn peer(&self, i: usize) -> &TestPeer {
self.peers.get(i).unwrap()
}
pub fn peer_mut(&mut self, i: usize) -> &mut TestPeer {
2015-12-26 15:47:07 +01:00
self.peers.get_mut(i).unwrap()
}
pub fn start(&mut self) {
for peer in 0..self.peers.len() {
for client in 0..self.peers.len() {
if peer != client {
let mut p = self.peers.get_mut(peer).unwrap();
p.sync.write().on_peer_connected(&mut TestIo::new(&mut p.chain, &mut p.queue, Some(client as PeerId)), client as PeerId);
2015-12-26 15:47:07 +01:00
}
}
}
}
pub fn sync_step(&mut self) {
for peer in 0..self.peers.len() {
2016-01-19 13:47:30 +01:00
if let Some(packet) = self.peers[peer].queue.pop_front() {
let mut p = self.peers.get_mut(packet.recipient).unwrap();
trace!("--- {} -> {} ---", peer, packet.recipient);
2016-06-20 17:28:48 +02:00
ChainSync::dispatch_packet(&p.sync, &mut TestIo::new(&mut p.chain, &mut p.queue, Some(peer as PeerId)), peer as PeerId, packet.packet_id, &packet.data);
2016-01-19 13:47:30 +01:00
trace!("----------------");
2015-12-26 15:47:07 +01:00
}
let mut p = self.peers.get_mut(peer).unwrap();
p.sync.write().maintain_sync(&mut TestIo::new(&mut p.chain, &mut p.queue, None));
2015-12-26 15:47:07 +01:00
}
}
2016-02-05 18:34:08 +01:00
pub fn sync_step_peer(&mut self, peer_num: usize) {
let mut peer = self.peer_mut(peer_num);
peer.sync.write().maintain_sync(&mut TestIo::new(&mut peer.chain, &mut peer.queue, None));
2016-02-05 18:34:08 +01:00
}
2016-02-03 21:42:30 +01:00
pub fn restart_peer(&mut self, i: usize) {
let peer = self.peer_mut(i);
peer.sync.write().restart(&mut TestIo::new(&mut peer.chain, &mut peer.queue, None));
2016-02-03 21:42:30 +01:00
}
pub fn sync(&mut self) -> u32 {
2015-12-26 15:47:07 +01:00
self.start();
2016-02-03 21:42:30 +01:00
let mut total_steps = 0;
2015-12-26 15:47:07 +01:00
while !self.done() {
2016-02-03 21:42:30 +01:00
self.sync_step();
2016-05-17 10:32:05 +02:00
total_steps += 1;
2016-02-03 21:42:30 +01:00
}
total_steps
}
pub fn sync_steps(&mut self, count: usize) {
if !self.started {
self.start();
self.started = true;
}
for _ in 0..count {
self.sync_step();
2015-12-26 15:47:07 +01:00
}
}
pub fn done(&self) -> bool {
self.peers.iter().all(|p| p.queue.is_empty())
}
2016-02-07 01:00:43 +01:00
pub fn trigger_chain_new_blocks(&mut self, peer_id: usize) {
2016-02-07 01:00:43 +01:00
let mut peer = self.peer_mut(peer_id);
peer.sync.write().chain_new_blocks(&mut TestIo::new(&mut peer.chain, &mut peer.queue, None), &[], &[], &[], &[], &[]);
2016-02-07 01:00:43 +01:00
}
2015-12-26 15:47:07 +01:00
}