2017-01-25 18:51:41 +01:00
|
|
|
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
2016-02-05 13:40:41 +01:00
|
|
|
// 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/>.
|
|
|
|
|
2017-07-29 17:12:07 +02:00
|
|
|
use std::collections::{VecDeque, HashSet, HashMap};
|
2017-07-29 21:56:42 +02:00
|
|
|
use std::sync::Arc;
|
2018-01-10 13:35:18 +01:00
|
|
|
use ethereum_types::H256;
|
2018-04-09 16:14:33 +02:00
|
|
|
use parking_lot::{RwLock, Mutex};
|
2017-09-06 20:47:45 +02:00
|
|
|
use bytes::Bytes;
|
2017-11-13 14:37:08 +01:00
|
|
|
use network::{self, PeerId, ProtocolId, PacketId, SessionInfo};
|
2016-09-06 15:31:13 +02:00
|
|
|
use tests::snapshot::*;
|
2018-04-09 16:14:33 +02:00
|
|
|
use ethcore::client::{TestBlockChainClient, BlockChainClient, Client as EthcoreClient,
|
|
|
|
ClientConfig, ChainNotify, ChainMessageType, ClientIoMessage};
|
2016-07-27 21:38:22 +02:00
|
|
|
use ethcore::header::BlockNumber;
|
2016-09-06 15:31:13 +02:00
|
|
|
use ethcore::snapshot::SnapshotService;
|
2016-12-06 19:23:15 +01:00
|
|
|
use ethcore::spec::Spec;
|
2017-01-18 18:49:50 +01:00
|
|
|
use ethcore::account_provider::AccountProvider;
|
2016-12-06 19:23:15 +01:00
|
|
|
use ethcore::miner::Miner;
|
2016-08-05 10:32:04 +02:00
|
|
|
use sync_io::SyncIo;
|
2018-04-09 16:14:33 +02:00
|
|
|
use io::{IoChannel, IoContext, IoHandler};
|
2016-10-24 16:24:35 +02:00
|
|
|
use api::WARP_SYNC_PROTOCOL_ID;
|
2018-04-09 16:14:33 +02:00
|
|
|
use chain::{ChainSync, ETH_PROTOCOL_VERSION_63, PAR_PROTOCOL_VERSION_3};
|
|
|
|
use SyncConfig;
|
|
|
|
use private_tx::{NoopPrivateTxHandler, PrivateTxHandler, SimplePrivateTxHandler};
|
2015-12-26 15:47:07 +01:00
|
|
|
|
2016-12-06 19:23:15 +01:00
|
|
|
pub trait FlushingBlockChainClient: BlockChainClient {
|
|
|
|
fn flush(&self) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FlushingBlockChainClient for EthcoreClient {
|
|
|
|
fn flush(&self) {
|
|
|
|
self.flush_queue();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FlushingBlockChainClient for TestBlockChainClient {}
|
|
|
|
|
|
|
|
pub struct TestIo<'p, C> where C: FlushingBlockChainClient, C: 'p {
|
|
|
|
pub chain: &'p C,
|
2016-09-06 15:31:13 +02:00
|
|
|
pub snapshot_service: &'p TestSnapshotService,
|
2016-12-11 12:32:01 +01:00
|
|
|
pub queue: &'p RwLock<VecDeque<TestPacket>>,
|
2016-02-04 19:30:31 +01:00
|
|
|
pub sender: Option<PeerId>,
|
2016-10-10 23:05:41 +02:00
|
|
|
pub to_disconnect: HashSet<PeerId>,
|
2016-12-11 12:32:01 +01:00
|
|
|
pub packets: Vec<TestPacket>,
|
2017-01-22 16:15:22 +01:00
|
|
|
pub peers_info: HashMap<PeerId, String>,
|
2016-10-18 18:16:00 +02:00
|
|
|
overlay: RwLock<HashMap<BlockNumber, Bytes>>,
|
2015-12-26 15:47:07 +01:00
|
|
|
}
|
|
|
|
|
2016-12-06 19:23:15 +01:00
|
|
|
impl<'p, C> TestIo<'p, C> where C: FlushingBlockChainClient, C: 'p {
|
2016-12-11 12:32:01 +01:00
|
|
|
pub fn new(chain: &'p C, ss: &'p TestSnapshotService, queue: &'p RwLock<VecDeque<TestPacket>>, sender: Option<PeerId>) -> TestIo<'p, C> {
|
2015-12-26 15:47:07 +01:00
|
|
|
TestIo {
|
|
|
|
chain: chain,
|
2016-09-06 15:31:13 +02:00
|
|
|
snapshot_service: ss,
|
2015-12-26 15:47:07 +01:00
|
|
|
queue: queue,
|
2016-10-10 23:05:41 +02:00
|
|
|
sender: sender,
|
|
|
|
to_disconnect: HashSet::new(),
|
2016-10-18 18:16:00 +02:00
|
|
|
overlay: RwLock::new(HashMap::new()),
|
2016-12-11 12:32:01 +01:00
|
|
|
packets: Vec::new(),
|
2017-01-22 16:15:22 +01:00
|
|
|
peers_info: HashMap::new(),
|
2015-12-26 15:47:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-11 12:32:01 +01:00
|
|
|
impl<'p, C> Drop for TestIo<'p, C> where C: FlushingBlockChainClient, C: 'p {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
self.queue.write().extend(self.packets.drain(..));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-06 19:23:15 +01:00
|
|
|
impl<'p, C> SyncIo for TestIo<'p, C> where C: FlushingBlockChainClient, C: 'p {
|
2016-10-10 23:05:41 +02:00
|
|
|
fn disable_peer(&mut self, peer_id: PeerId) {
|
|
|
|
self.disconnect_peer(peer_id);
|
2015-12-26 15:47:07 +01:00
|
|
|
}
|
|
|
|
|
2016-10-10 23:05:41 +02:00
|
|
|
fn disconnect_peer(&mut self, peer_id: PeerId) {
|
|
|
|
self.to_disconnect.insert(peer_id);
|
2016-02-02 14:54:46 +01:00
|
|
|
}
|
|
|
|
|
2016-06-17 18:26:54 +02:00
|
|
|
fn is_expired(&self) -> bool {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2017-11-13 14:37:08 +01:00
|
|
|
fn respond(&mut self, packet_id: PacketId, data: Vec<u8>) -> Result<(), network::Error> {
|
2016-12-11 12:32:01 +01:00
|
|
|
self.packets.push(TestPacket {
|
2015-12-26 15:47:07 +01:00
|
|
|
data: data,
|
|
|
|
packet_id: packet_id,
|
|
|
|
recipient: self.sender.unwrap()
|
|
|
|
});
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-11-13 14:37:08 +01:00
|
|
|
fn send(&mut self, peer_id: PeerId, packet_id: PacketId, data: Vec<u8>) -> Result<(), network::Error> {
|
2016-12-11 12:32:01 +01:00
|
|
|
self.packets.push(TestPacket {
|
2015-12-26 15:47:07 +01:00
|
|
|
data: data,
|
|
|
|
packet_id: packet_id,
|
|
|
|
recipient: peer_id,
|
|
|
|
});
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-11-13 14:37:08 +01:00
|
|
|
fn send_protocol(&mut self, _protocol: ProtocolId, peer_id: PeerId, packet_id: PacketId, data: Vec<u8>) -> Result<(), network::Error> {
|
2016-10-25 18:40:01 +02:00
|
|
|
self.send(peer_id, packet_id, data)
|
|
|
|
}
|
|
|
|
|
2016-05-31 22:24:32 +02:00
|
|
|
fn chain(&self) -> &BlockChainClient {
|
2016-12-11 12:32:01 +01:00
|
|
|
&*self.chain
|
2015-12-26 15:47:07 +01:00
|
|
|
}
|
2016-09-06 15:31:13 +02:00
|
|
|
|
2017-01-22 16:15:22 +01:00
|
|
|
fn peer_info(&self, peer_id: PeerId) -> String {
|
|
|
|
self.peers_info.get(&peer_id)
|
|
|
|
.cloned()
|
|
|
|
.unwrap_or_else(|| peer_id.to_string())
|
|
|
|
}
|
|
|
|
|
2016-09-06 15:31:13 +02:00
|
|
|
fn snapshot_service(&self) -> &SnapshotService {
|
|
|
|
self.snapshot_service
|
|
|
|
}
|
|
|
|
|
2016-10-12 20:18:59 +02:00
|
|
|
fn peer_session_info(&self, _peer_id: PeerId) -> Option<SessionInfo> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2016-09-06 15:31:13 +02:00
|
|
|
fn eth_protocol_version(&self, _peer: PeerId) -> u8 {
|
2018-04-09 16:14:33 +02:00
|
|
|
ETH_PROTOCOL_VERSION_63
|
2016-10-24 16:24:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn protocol_version(&self, protocol: &ProtocolId, peer_id: PeerId) -> u8 {
|
2018-04-09 16:14:33 +02:00
|
|
|
if protocol == &WARP_SYNC_PROTOCOL_ID { PAR_PROTOCOL_VERSION_3 } else { self.eth_protocol_version(peer_id) }
|
2016-09-06 15:31:13 +02:00
|
|
|
}
|
2016-10-18 18:16:00 +02:00
|
|
|
|
|
|
|
fn chain_overlay(&self) -> &RwLock<HashMap<BlockNumber, Bytes>> {
|
|
|
|
&self.overlay
|
|
|
|
}
|
2015-12-26 15:47:07 +01:00
|
|
|
}
|
|
|
|
|
2018-04-09 16:14:33 +02:00
|
|
|
/// Mock for emulution of async run of new blocks
|
|
|
|
struct NewBlockMessage {
|
|
|
|
imported: Vec<H256>,
|
|
|
|
invalid: Vec<H256>,
|
|
|
|
enacted: Vec<H256>,
|
|
|
|
retracted: Vec<H256>,
|
|
|
|
sealed: Vec<H256>,
|
|
|
|
proposed: Vec<Bytes>,
|
|
|
|
}
|
|
|
|
|
2016-12-27 11:23:57 +01:00
|
|
|
/// Abstract messages between peers.
|
|
|
|
pub trait Message {
|
|
|
|
/// The intended recipient of this message.
|
|
|
|
fn recipient(&self) -> PeerId;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Mock subprotocol packet
|
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-12-27 11:23:57 +01:00
|
|
|
impl Message for TestPacket {
|
|
|
|
fn recipient(&self) -> PeerId { self.recipient }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A peer which can be a member of the `TestNet`.
|
|
|
|
pub trait Peer {
|
|
|
|
type Message: Message;
|
|
|
|
|
|
|
|
/// Called on connection to other indicated peer.
|
|
|
|
fn on_connect(&self, other: PeerId);
|
|
|
|
|
|
|
|
/// Called on disconnect from other indicated peer.
|
|
|
|
fn on_disconnect(&self, other: PeerId);
|
|
|
|
|
|
|
|
/// Receive a message from another peer. Return a set of peers to disconnect.
|
|
|
|
fn receive_message(&self, from: PeerId, msg: Self::Message) -> HashSet<PeerId>;
|
|
|
|
|
|
|
|
/// Produce the next pending message to send to another peer.
|
|
|
|
fn pending_message(&self) -> Option<Self::Message>;
|
|
|
|
|
|
|
|
/// Whether this peer is done syncing (has no messages to send).
|
|
|
|
fn is_done(&self) -> bool;
|
|
|
|
|
|
|
|
/// Execute a "sync step". This is called for each peer after it sends a packet.
|
|
|
|
fn sync_step(&self);
|
|
|
|
|
|
|
|
/// Restart sync for a peer.
|
|
|
|
fn restart_sync(&self);
|
2018-04-09 16:14:33 +02:00
|
|
|
|
|
|
|
/// Process the queue of pending io messages
|
|
|
|
fn process_all_io_messages(&self);
|
|
|
|
|
|
|
|
/// Process the queue of new block messages
|
|
|
|
fn process_all_new_block_messages(&self);
|
2016-12-27 11:23:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct EthPeer<C> where C: FlushingBlockChainClient {
|
2016-12-11 12:32:01 +01:00
|
|
|
pub chain: Arc<C>,
|
2018-04-13 17:34:27 +02:00
|
|
|
pub miner: Arc<Miner>,
|
2016-09-06 15:31:13 +02:00
|
|
|
pub snapshot_service: Arc<TestSnapshotService>,
|
2016-06-20 17:28:48 +02:00
|
|
|
pub sync: RwLock<ChainSync>,
|
2016-12-06 19:23:15 +01:00
|
|
|
pub queue: RwLock<VecDeque<TestPacket>>,
|
2018-04-09 16:14:33 +02:00
|
|
|
pub private_tx_handler: Arc<PrivateTxHandler>,
|
|
|
|
pub io_queue: RwLock<VecDeque<ChainMessageType>>,
|
|
|
|
new_blocks_queue: RwLock<VecDeque<NewBlockMessage>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<C> EthPeer<C> where C: FlushingBlockChainClient {
|
|
|
|
fn is_io_queue_empty(&self) -> bool {
|
|
|
|
self.io_queue.read().is_empty()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_new_blocks_queue_empty(&self) -> bool {
|
|
|
|
self.new_blocks_queue.read().is_empty()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn process_io_message(&self, message: ChainMessageType) {
|
|
|
|
let mut io = TestIo::new(&*self.chain, &self.snapshot_service, &self.queue, None);
|
|
|
|
match message {
|
|
|
|
ChainMessageType::Consensus(data) => self.sync.write().propagate_consensus_packet(&mut io, data),
|
|
|
|
ChainMessageType::PrivateTransaction(data) => self.sync.write().propagate_private_transaction(&mut io, data),
|
|
|
|
ChainMessageType::SignedPrivateTransaction(data) => self.sync.write().propagate_signed_private_transaction(&mut io, data),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn process_new_block_message(&self, message: NewBlockMessage) {
|
|
|
|
let mut io = TestIo::new(&*self.chain, &self.snapshot_service, &self.queue, None);
|
|
|
|
self.sync.write().chain_new_blocks(
|
|
|
|
&mut io,
|
|
|
|
&message.imported,
|
|
|
|
&message.invalid,
|
|
|
|
&message.enacted,
|
|
|
|
&message.retracted,
|
|
|
|
&message.sealed,
|
|
|
|
&message.proposed
|
|
|
|
);
|
|
|
|
}
|
2015-12-26 15:47:07 +01:00
|
|
|
}
|
|
|
|
|
2016-12-27 11:23:57 +01:00
|
|
|
impl<C: FlushingBlockChainClient> Peer for EthPeer<C> {
|
|
|
|
type Message = TestPacket;
|
|
|
|
|
|
|
|
fn on_connect(&self, other: PeerId) {
|
|
|
|
self.sync.write().update_targets(&*self.chain);
|
2018-04-09 16:14:33 +02:00
|
|
|
self.sync.write().on_peer_connected(&mut TestIo::new(
|
|
|
|
&*self.chain,
|
|
|
|
&self.snapshot_service,
|
|
|
|
&self.queue,
|
|
|
|
Some(other)),
|
|
|
|
other);
|
2016-12-27 11:23:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn on_disconnect(&self, other: PeerId) {
|
|
|
|
let mut io = TestIo::new(&*self.chain, &self.snapshot_service, &self.queue, Some(other));
|
|
|
|
self.sync.write().on_peer_aborting(&mut io, other);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn receive_message(&self, from: PeerId, msg: TestPacket) -> HashSet<PeerId> {
|
|
|
|
let mut io = TestIo::new(&*self.chain, &self.snapshot_service, &self.queue, Some(from));
|
|
|
|
ChainSync::dispatch_packet(&self.sync, &mut io, from, msg.packet_id, &msg.data);
|
|
|
|
self.chain.flush();
|
|
|
|
io.to_disconnect.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn pending_message(&self) -> Option<TestPacket> {
|
|
|
|
self.chain.flush();
|
|
|
|
self.queue.write().pop_front()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_done(&self) -> bool {
|
2018-04-09 16:14:33 +02:00
|
|
|
self.queue.read().is_empty() && self.is_io_queue_empty() && self.is_new_blocks_queue_empty()
|
2016-12-27 11:23:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn sync_step(&self) {
|
|
|
|
self.chain.flush();
|
|
|
|
self.sync.write().maintain_peers(&mut TestIo::new(&*self.chain, &self.snapshot_service, &self.queue, None));
|
|
|
|
self.sync.write().maintain_sync(&mut TestIo::new(&*self.chain, &self.snapshot_service, &self.queue, None));
|
|
|
|
self.sync.write().propagate_new_transactions(&mut TestIo::new(&*self.chain, &self.snapshot_service, &self.queue, None));
|
|
|
|
}
|
|
|
|
|
|
|
|
fn restart_sync(&self) {
|
|
|
|
self.sync.write().restart(&mut TestIo::new(&*self.chain, &self.snapshot_service, &self.queue, None));
|
|
|
|
}
|
2018-04-09 16:14:33 +02:00
|
|
|
|
|
|
|
fn process_all_io_messages(&self) {
|
|
|
|
if !self.is_io_queue_empty() {
|
|
|
|
while let Some(message) = self.io_queue.write().pop_front() {
|
|
|
|
self.process_io_message(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn process_all_new_block_messages(&self) {
|
|
|
|
if !self.is_new_blocks_queue_empty() {
|
|
|
|
while let Some(message) = self.new_blocks_queue.write().pop_front() {
|
|
|
|
self.process_new_block_message(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-12-27 11:23:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct TestNet<P> {
|
|
|
|
pub peers: Vec<Arc<P>>,
|
2016-02-04 19:30:31 +01:00
|
|
|
pub started: bool,
|
2016-11-28 16:30:36 +01:00
|
|
|
pub disconnect_events: Vec<(PeerId, PeerId)>, //disconnected (initiated by, to)
|
2015-12-26 15:47:07 +01:00
|
|
|
}
|
|
|
|
|
2016-12-27 11:23:57 +01:00
|
|
|
impl TestNet<EthPeer<TestBlockChainClient>> {
|
|
|
|
pub fn new(n: usize) -> Self {
|
2016-10-29 13:07:06 +02:00
|
|
|
Self::new_with_config(n, SyncConfig::default())
|
2016-07-27 21:38:22 +02:00
|
|
|
}
|
|
|
|
|
2016-12-27 11:23:57 +01:00
|
|
|
pub fn new_with_fork(n: usize, fork: Option<(BlockNumber, H256)>) -> Self {
|
2016-10-29 13:07:06 +02:00
|
|
|
let mut config = SyncConfig::default();
|
|
|
|
config.fork_block = fork;
|
|
|
|
Self::new_with_config(n, config)
|
|
|
|
}
|
|
|
|
|
2016-12-27 11:23:57 +01:00
|
|
|
pub fn new_with_config(n: usize, config: SyncConfig) -> Self {
|
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,
|
2016-11-28 16:30:36 +01:00
|
|
|
disconnect_events: Vec::new(),
|
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-09-06 15:31:13 +02:00
|
|
|
let ss = Arc::new(TestSnapshotService::new());
|
2018-04-09 16:14:33 +02:00
|
|
|
let private_tx_handler = Arc::new(NoopPrivateTxHandler);
|
|
|
|
let sync = ChainSync::new(config.clone(), &chain, private_tx_handler.clone());
|
2016-12-27 11:23:57 +01:00
|
|
|
net.peers.push(Arc::new(EthPeer {
|
2016-06-20 17:28:48 +02:00
|
|
|
sync: RwLock::new(sync),
|
2016-09-06 15:31:13 +02:00
|
|
|
snapshot_service: ss,
|
2016-12-11 12:32:01 +01:00
|
|
|
chain: Arc::new(chain),
|
2018-04-13 17:34:27 +02:00
|
|
|
miner: Arc::new(Miner::new_for_tests(&Spec::new_test(), None)),
|
2016-12-06 19:23:15 +01:00
|
|
|
queue: RwLock::new(VecDeque::new()),
|
2018-04-09 16:14:33 +02:00
|
|
|
private_tx_handler,
|
|
|
|
io_queue: RwLock::new(VecDeque::new()),
|
|
|
|
new_blocks_queue: RwLock::new(VecDeque::new()),
|
2016-12-06 19:23:15 +01:00
|
|
|
}));
|
2015-12-26 15:47:07 +01:00
|
|
|
}
|
|
|
|
net
|
|
|
|
}
|
2018-04-09 16:14:33 +02:00
|
|
|
|
|
|
|
// relies on Arc uniqueness, which is only true when we haven't registered a ChainNotify.
|
|
|
|
pub fn peer_mut(&mut self, i: usize) -> &mut EthPeer<TestBlockChainClient> {
|
|
|
|
Arc::get_mut(&mut self.peers[i]).expect("Arc never exposed externally")
|
|
|
|
}
|
2016-12-06 19:23:15 +01:00
|
|
|
}
|
|
|
|
|
2016-12-27 11:23:57 +01:00
|
|
|
impl TestNet<EthPeer<EthcoreClient>> {
|
2018-04-09 16:14:33 +02:00
|
|
|
pub fn with_spec_and_accounts<F>(
|
|
|
|
n: usize,
|
|
|
|
config: SyncConfig,
|
|
|
|
spec_factory: F,
|
|
|
|
accounts: Option<Arc<AccountProvider>>,
|
|
|
|
private_tx_handler: bool,
|
|
|
|
) -> Self
|
2016-12-06 19:23:15 +01:00
|
|
|
where F: Fn() -> Spec
|
|
|
|
{
|
|
|
|
let mut net = TestNet {
|
|
|
|
peers: Vec::new(),
|
|
|
|
started: false,
|
|
|
|
disconnect_events: Vec::new(),
|
|
|
|
};
|
2017-02-20 17:21:55 +01:00
|
|
|
for _ in 0..n {
|
2018-04-09 16:14:33 +02:00
|
|
|
if private_tx_handler {
|
|
|
|
net.add_peer_with_private_config(config.clone(), spec_factory(), accounts.clone());
|
|
|
|
} else {
|
|
|
|
net.add_peer(config.clone(), spec_factory(), accounts.clone());
|
|
|
|
}
|
2016-12-06 19:23:15 +01:00
|
|
|
}
|
2017-02-20 17:21:55 +01:00
|
|
|
net
|
2016-12-06 19:23:15 +01:00
|
|
|
}
|
2017-04-10 20:03:18 +02:00
|
|
|
|
2018-04-09 16:14:33 +02:00
|
|
|
pub fn add_peer_with_private_config(&mut self, config: SyncConfig, spec: Spec, accounts: Option<Arc<AccountProvider>>) {
|
|
|
|
let channel = IoChannel::disconnected();
|
2018-04-13 17:34:27 +02:00
|
|
|
let miner = Arc::new(Miner::new_for_tests(&spec, accounts.clone()));
|
2018-04-09 16:14:33 +02:00
|
|
|
let client = EthcoreClient::new(
|
|
|
|
ClientConfig::default(),
|
|
|
|
&spec,
|
|
|
|
Arc::new(::kvdb_memorydb::create(::ethcore::db::NUM_COLUMNS.unwrap_or(0))),
|
2018-04-13 17:34:27 +02:00
|
|
|
miner.clone(),
|
2018-04-09 16:14:33 +02:00
|
|
|
channel.clone()
|
|
|
|
).unwrap();
|
|
|
|
|
|
|
|
let private_tx_handler = Arc::new(SimplePrivateTxHandler::default());
|
|
|
|
let ss = Arc::new(TestSnapshotService::new());
|
|
|
|
let sync = ChainSync::new(config, &*client, private_tx_handler.clone());
|
|
|
|
let peer = Arc::new(EthPeer {
|
|
|
|
sync: RwLock::new(sync),
|
|
|
|
snapshot_service: ss,
|
|
|
|
chain: client,
|
2018-04-13 17:34:27 +02:00
|
|
|
miner,
|
2018-04-09 16:14:33 +02:00
|
|
|
queue: RwLock::new(VecDeque::new()),
|
|
|
|
private_tx_handler,
|
|
|
|
io_queue: RwLock::new(VecDeque::new()),
|
|
|
|
new_blocks_queue: RwLock::new(VecDeque::new()),
|
|
|
|
});
|
|
|
|
peer.chain.add_notify(peer.clone());
|
|
|
|
//private_provider.add_notify(peer.clone());
|
|
|
|
self.peers.push(peer);
|
|
|
|
}
|
|
|
|
|
2017-04-10 20:03:18 +02:00
|
|
|
pub fn add_peer(&mut self, config: SyncConfig, spec: Spec, accounts: Option<Arc<AccountProvider>>) {
|
2018-04-13 17:34:27 +02:00
|
|
|
let miner = Arc::new(Miner::new_for_tests(&spec, accounts));
|
2017-04-10 20:03:18 +02:00
|
|
|
let client = EthcoreClient::new(
|
|
|
|
ClientConfig::default(),
|
|
|
|
&spec,
|
2017-10-15 16:17:15 +02:00
|
|
|
Arc::new(::kvdb_memorydb::create(::ethcore::db::NUM_COLUMNS.unwrap_or(0))),
|
2018-04-13 17:34:27 +02:00
|
|
|
miner.clone(),
|
2017-04-10 20:03:18 +02:00
|
|
|
IoChannel::disconnected(),
|
|
|
|
).unwrap();
|
|
|
|
|
|
|
|
let ss = Arc::new(TestSnapshotService::new());
|
2018-04-09 16:14:33 +02:00
|
|
|
let private_tx_handler = Arc::new(NoopPrivateTxHandler);
|
|
|
|
let sync = ChainSync::new(config, &*client, private_tx_handler.clone());
|
2017-04-10 20:03:18 +02:00
|
|
|
let peer = Arc::new(EthPeer {
|
|
|
|
sync: RwLock::new(sync),
|
|
|
|
snapshot_service: ss,
|
|
|
|
queue: RwLock::new(VecDeque::new()),
|
2018-04-13 17:34:27 +02:00
|
|
|
chain: client,
|
|
|
|
miner,
|
2018-04-09 16:14:33 +02:00
|
|
|
private_tx_handler,
|
|
|
|
io_queue: RwLock::new(VecDeque::new()),
|
|
|
|
new_blocks_queue: RwLock::new(VecDeque::new()),
|
2017-04-10 20:03:18 +02:00
|
|
|
});
|
|
|
|
peer.chain.add_notify(peer.clone());
|
|
|
|
self.peers.push(peer);
|
|
|
|
}
|
2016-12-06 19:23:15 +01:00
|
|
|
}
|
2015-12-26 15:47:07 +01:00
|
|
|
|
2016-12-27 11:23:57 +01:00
|
|
|
impl<P> TestNet<P> where P: Peer {
|
|
|
|
pub fn peer(&self, i: usize) -> &P {
|
2016-11-28 13:20:49 +01:00
|
|
|
&self.peers[i]
|
2015-12-27 00:48:03 +01:00
|
|
|
}
|
|
|
|
|
2015-12-26 15:47:07 +01:00
|
|
|
pub fn start(&mut self) {
|
2016-12-13 13:23:10 +01:00
|
|
|
if self.started {
|
|
|
|
return;
|
|
|
|
}
|
2015-12-26 15:47:07 +01:00
|
|
|
for peer in 0..self.peers.len() {
|
|
|
|
for client in 0..self.peers.len() {
|
|
|
|
if peer != client {
|
2016-12-27 11:23:57 +01:00
|
|
|
self.peers[peer].on_connect(client as PeerId);
|
2015-12-26 15:47:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-12-13 13:23:10 +01:00
|
|
|
self.started = true;
|
2015-12-26 15:47:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn sync_step(&mut self) {
|
|
|
|
for peer in 0..self.peers.len() {
|
2016-12-27 11:23:57 +01:00
|
|
|
let packet = self.peers[peer].pending_message();
|
2016-12-06 19:23:15 +01:00
|
|
|
if let Some(packet) = packet {
|
2016-10-10 23:05:41 +02:00
|
|
|
let disconnecting = {
|
2016-12-27 11:23:57 +01:00
|
|
|
let recipient = packet.recipient();
|
|
|
|
trace!("--- {} -> {} ---", peer, recipient);
|
|
|
|
let to_disconnect = self.peers[recipient].receive_message(peer as PeerId, packet);
|
2016-10-10 23:05:41 +02:00
|
|
|
for d in &to_disconnect {
|
|
|
|
// notify this that disconnecting peers are disconnecting
|
2016-12-27 11:23:57 +01:00
|
|
|
self.peers[recipient].on_disconnect(*d as PeerId);
|
2016-11-28 16:30:36 +01:00
|
|
|
self.disconnect_events.push((peer, *d));
|
2016-10-10 23:05:41 +02:00
|
|
|
}
|
|
|
|
to_disconnect
|
|
|
|
};
|
|
|
|
for d in &disconnecting {
|
|
|
|
// notify other peers that this peer is disconnecting
|
2016-12-27 11:23:57 +01:00
|
|
|
self.peers[*d].on_disconnect(peer as PeerId);
|
2016-10-10 23:05:41 +02:00
|
|
|
}
|
2015-12-26 15:47:07 +01:00
|
|
|
}
|
2016-10-10 23:05:41 +02:00
|
|
|
|
|
|
|
self.sync_step_peer(peer);
|
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) {
|
2016-12-27 11:23:57 +01:00
|
|
|
self.peers[peer_num].sync_step();
|
2016-02-05 18:34:08 +01:00
|
|
|
}
|
|
|
|
|
2016-02-03 21:42:30 +01:00
|
|
|
pub fn restart_peer(&mut self, i: usize) {
|
2016-12-27 11:23:57 +01:00
|
|
|
self.peers[i].restart_sync();
|
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();
|
2018-04-09 16:14:33 +02:00
|
|
|
self.deliver_io_messages();
|
|
|
|
self.deliver_new_block_messages();
|
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) {
|
2016-12-13 13:23:10 +01:00
|
|
|
self.start();
|
2016-02-03 21:42:30 +01:00
|
|
|
for _ in 0..count {
|
|
|
|
self.sync_step();
|
2015-12-26 15:47:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-09 16:14:33 +02:00
|
|
|
pub fn deliver_io_messages(&mut self) {
|
|
|
|
for peer in self.peers.iter() {
|
|
|
|
peer.process_all_io_messages();
|
|
|
|
}
|
2015-12-26 15:47:07 +01:00
|
|
|
}
|
2016-12-27 11:23:57 +01:00
|
|
|
|
2018-04-09 16:14:33 +02:00
|
|
|
pub fn deliver_new_block_messages(&mut self) {
|
|
|
|
for peer in self.peers.iter() {
|
|
|
|
peer.process_all_new_block_messages();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn done(&self) -> bool {
|
|
|
|
self.peers.iter().all(|p| p.is_done())
|
2016-12-27 11:23:57 +01:00
|
|
|
}
|
|
|
|
}
|
2016-02-07 01:00:43 +01:00
|
|
|
|
2016-12-27 11:23:57 +01:00
|
|
|
impl<C: FlushingBlockChainClient> TestNet<EthPeer<C>> {
|
2016-03-07 12:16:37 +01:00
|
|
|
pub fn trigger_chain_new_blocks(&mut self, peer_id: usize) {
|
2016-12-27 11:23:57 +01:00
|
|
|
let peer = &mut self.peers[peer_id];
|
2016-12-11 12:32:01 +01:00
|
|
|
peer.sync.write().chain_new_blocks(&mut TestIo::new(&*peer.chain, &peer.snapshot_service, &peer.queue, None), &[], &[], &[], &[], &[], &[]);
|
2016-02-07 01:00:43 +01:00
|
|
|
}
|
2015-12-26 15:47:07 +01:00
|
|
|
}
|
2016-12-06 19:23:15 +01:00
|
|
|
|
2018-04-09 16:14:33 +02:00
|
|
|
pub struct TestIoHandler {
|
|
|
|
pub client: Arc<EthcoreClient>,
|
|
|
|
pub private_tx_queued: Mutex<usize>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TestIoHandler {
|
|
|
|
pub fn new(client: Arc<EthcoreClient>) -> Self {
|
|
|
|
TestIoHandler {
|
|
|
|
client,
|
|
|
|
private_tx_queued: Mutex::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IoHandler<ClientIoMessage> for TestIoHandler {
|
|
|
|
fn message(&self, _io: &IoContext<ClientIoMessage>, net_message: &ClientIoMessage) {
|
|
|
|
match *net_message {
|
|
|
|
ClientIoMessage::NewMessage(ref message) => if let Err(e) = self.client.engine().handle_message(message) {
|
|
|
|
panic!("Invalid message received: {}", e);
|
|
|
|
},
|
|
|
|
ClientIoMessage::NewPrivateTransaction => {
|
|
|
|
*self.private_tx_queued.lock() += 1;
|
|
|
|
},
|
|
|
|
_ => {} // ignore other messages
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-27 11:23:57 +01:00
|
|
|
impl ChainNotify for EthPeer<EthcoreClient> {
|
2016-12-06 19:23:15 +01:00
|
|
|
fn new_blocks(&self,
|
|
|
|
imported: Vec<H256>,
|
|
|
|
invalid: Vec<H256>,
|
|
|
|
enacted: Vec<H256>,
|
|
|
|
retracted: Vec<H256>,
|
|
|
|
sealed: Vec<H256>,
|
|
|
|
proposed: Vec<Bytes>,
|
|
|
|
_duration: u64)
|
|
|
|
{
|
2018-04-09 16:14:33 +02:00
|
|
|
self.new_blocks_queue.write().push_back(NewBlockMessage {
|
|
|
|
imported,
|
|
|
|
invalid,
|
|
|
|
enacted,
|
|
|
|
retracted,
|
|
|
|
sealed,
|
|
|
|
proposed,
|
|
|
|
});
|
2016-12-06 19:23:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn start(&self) {}
|
|
|
|
|
|
|
|
fn stop(&self) {}
|
|
|
|
|
2018-04-09 16:14:33 +02:00
|
|
|
fn broadcast(&self, message_type: ChainMessageType) {
|
|
|
|
self.io_queue.write().push_back(message_type)
|
2016-12-06 19:23:15 +01:00
|
|
|
}
|
|
|
|
}
|