2015-12-22 22:19:50 +01:00
|
|
|
use std::sync::{Arc};
|
|
|
|
use eth::{BlockChainClient};
|
2015-12-28 12:03:05 +01:00
|
|
|
use util::network::{ProtocolHandler, NetworkService, HandlerIo, TimerToken, PeerId, PacketId, Message, Error as NetworkError};
|
2015-12-26 15:47:07 +01:00
|
|
|
use sync::chain::{ChainSync};
|
2015-12-22 22:19:50 +01:00
|
|
|
|
|
|
|
mod chain;
|
2015-12-25 14:55:55 +01:00
|
|
|
mod range_collection;
|
2015-12-22 22:19:50 +01:00
|
|
|
|
2015-12-25 14:55:55 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests;
|
2015-12-22 22:19:50 +01:00
|
|
|
|
2015-12-25 14:55:55 +01:00
|
|
|
pub fn new(_service: &mut NetworkService, eth_client: Arc<BlockChainClient+Send+Sized>) -> EthSync {
|
|
|
|
EthSync {
|
|
|
|
chain: eth_client,
|
|
|
|
sync: ChainSync::new(),
|
|
|
|
}
|
2015-12-22 22:19:50 +01:00
|
|
|
}
|
|
|
|
|
2015-12-26 15:47:07 +01:00
|
|
|
pub trait SyncIo {
|
|
|
|
fn disable_peer(&mut self, peer_id: &PeerId);
|
|
|
|
fn respond(&mut self, packet_id: PacketId, data: Vec<u8>) -> Result<(), NetworkError>;
|
|
|
|
fn send(&mut self, peer_id: PeerId, packet_id: PacketId, data: Vec<u8>) -> Result<(), NetworkError>;
|
|
|
|
fn chain<'s>(&'s mut self) -> &'s mut BlockChainClient;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct NetSyncIo<'s, 'h> where 'h:'s {
|
|
|
|
network: &'s mut HandlerIo<'h>,
|
|
|
|
chain: &'s mut BlockChainClient
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'s, 'h> NetSyncIo<'s, 'h> {
|
|
|
|
pub fn new(network: &'s mut HandlerIo<'h>, chain: &'s mut BlockChainClient) -> NetSyncIo<'s,'h> {
|
|
|
|
NetSyncIo {
|
|
|
|
network: network,
|
|
|
|
chain: chain,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'s, 'h> SyncIo for NetSyncIo<'s, 'h> {
|
|
|
|
fn disable_peer(&mut self, peer_id: &PeerId) {
|
|
|
|
self.network.disable_peer(*peer_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn respond(&mut self, packet_id: PacketId, data: Vec<u8>) -> Result<(), NetworkError>{
|
|
|
|
self.network.respond(packet_id, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn send(&mut self, peer_id: PeerId, packet_id: PacketId, data: Vec<u8>) -> Result<(), NetworkError>{
|
|
|
|
self.network.send(peer_id, packet_id, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn chain<'a>(&'a mut self) -> &'a mut BlockChainClient {
|
|
|
|
self.chain
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-25 14:55:55 +01:00
|
|
|
pub struct EthSync {
|
2015-12-24 17:18:47 +01:00
|
|
|
chain: Arc<BlockChainClient+Send+Sized>,
|
|
|
|
sync: ChainSync
|
2015-12-22 22:19:50 +01:00
|
|
|
}
|
|
|
|
|
2015-12-25 14:55:55 +01:00
|
|
|
pub use self::chain::SyncStatus;
|
|
|
|
|
|
|
|
impl EthSync {
|
2016-01-07 16:08:12 +01:00
|
|
|
pub fn new(chain: Arc<BlockChainClient+Send+Sized>) -> EthSync {
|
|
|
|
EthSync {
|
|
|
|
chain: chain,
|
|
|
|
sync: ChainSync::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-25 14:55:55 +01:00
|
|
|
pub fn is_syncing(&self) -> bool {
|
|
|
|
self.sync.is_syncing()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn status(&self) -> SyncStatus {
|
|
|
|
self.sync.status()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn stop_network(&mut self, io: &mut HandlerIo) {
|
2015-12-26 15:47:07 +01:00
|
|
|
self.sync.abort(&mut NetSyncIo::new(io, Arc::get_mut(&mut self.chain).unwrap()));
|
2015-12-25 14:55:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn start_network(&mut self, io: &mut HandlerIo) {
|
2015-12-26 15:47:07 +01:00
|
|
|
self.sync.restart(&mut NetSyncIo::new(io, Arc::get_mut(&mut self.chain).unwrap()));
|
2015-12-25 14:55:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-22 22:19:50 +01:00
|
|
|
impl ProtocolHandler for EthSync {
|
|
|
|
fn initialize(&mut self, io: &mut HandlerIo) {
|
2015-12-26 15:47:07 +01:00
|
|
|
self.sync.restart(&mut NetSyncIo::new(io, Arc::get_mut(&mut self.chain).unwrap()));
|
2015-12-25 14:55:55 +01:00
|
|
|
io.register_timer(1000).unwrap();
|
2015-12-22 22:19:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn read(&mut self, io: &mut HandlerIo, peer: &PeerId, packet_id: u8, data: &[u8]) {
|
2015-12-26 15:47:07 +01:00
|
|
|
self.sync.on_packet(&mut NetSyncIo::new(io, Arc::get_mut(&mut self.chain).unwrap()), peer, packet_id, data);
|
2015-12-22 22:19:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn connected(&mut self, io: &mut HandlerIo, peer: &PeerId) {
|
2015-12-26 15:47:07 +01:00
|
|
|
self.sync.on_peer_connected(&mut NetSyncIo::new(io, Arc::get_mut(&mut self.chain).unwrap()), peer);
|
2015-12-22 22:19:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn disconnected(&mut self, io: &mut HandlerIo, peer: &PeerId) {
|
2015-12-26 15:47:07 +01:00
|
|
|
self.sync.on_peer_aborting(&mut NetSyncIo::new(io, Arc::get_mut(&mut self.chain).unwrap()), peer);
|
2015-12-22 22:19:50 +01:00
|
|
|
}
|
|
|
|
|
2015-12-25 14:55:55 +01:00
|
|
|
fn timeout(&mut self, io: &mut HandlerIo, _timer: TimerToken) {
|
2015-12-26 15:47:07 +01:00
|
|
|
self.sync.maintain_sync(&mut NetSyncIo::new(io, Arc::get_mut(&mut self.chain).unwrap()));
|
2015-12-22 22:19:50 +01:00
|
|
|
}
|
2015-12-28 12:03:05 +01:00
|
|
|
|
|
|
|
fn message(&mut self, _io: &mut HandlerIo, _message: &Message) {
|
|
|
|
}
|
2015-12-22 22:19:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|