diff --git a/src/sync/io.rs b/src/sync/io.rs index 9806a3bf5..884babe3e 100644 --- a/src/sync/io.rs +++ b/src/sync/io.rs @@ -1,6 +1,7 @@ use client::BlockChainClient; -use util::network::{HandlerIo, PeerId, PacketId,}; +use util::{NetworkContext, PeerId, PacketId,}; use util::error::UtilError; +use sync::SyncMessage; /// IO interface for the syning handler. /// Provides peer connection management and an interface to the blockchain client. @@ -16,15 +17,15 @@ pub trait SyncIo { fn chain<'s>(&'s mut self) -> &'s mut BlockChainClient; } -/// Wraps `HandlerIo` and the blockchain client -pub struct NetSyncIo<'s, 'h> where 'h:'s { - network: &'s mut HandlerIo<'h>, +/// Wraps `NetworkContext` and the blockchain client +pub struct NetSyncIo<'s, 'h, 'io> where 'h: 's, 'io: 'h { + network: &'s mut NetworkContext<'h, 'io, SyncMessage>, chain: &'s mut BlockChainClient } -impl<'s, 'h> NetSyncIo<'s, 'h> { - /// Creates a new instance from the `HandlerIo` and the blockchain client reference. - pub fn new(network: &'s mut HandlerIo<'h>, chain: &'s mut BlockChainClient) -> NetSyncIo<'s,'h> { +impl<'s, 'h, 'io> NetSyncIo<'s, 'h, 'io> { + /// Creates a new instance from the `NetworkContext` and the blockchain client reference. + pub fn new(network: &'s mut NetworkContext<'h, 'io, SyncMessage>, chain: &'s mut BlockChainClient) -> NetSyncIo<'s,'h,'io> { NetSyncIo { network: network, chain: chain, @@ -32,7 +33,7 @@ impl<'s, 'h> NetSyncIo<'s, 'h> { } } -impl<'s, 'h> SyncIo for NetSyncIo<'s, 'h> { +impl<'s, 'h, 'op> SyncIo for NetSyncIo<'s, 'h, 'op> { fn disable_peer(&mut self, peer_id: &PeerId) { self.network.disable_peer(*peer_id); } diff --git a/src/sync/mod.rs b/src/sync/mod.rs index 300465014..50e20c439 100644 --- a/src/sync/mod.rs +++ b/src/sync/mod.rs @@ -24,7 +24,9 @@ use std::sync::Arc; use client::BlockChainClient; -use util::network::{ProtocolHandler, NetworkService, HandlerIo, TimerToken, PeerId, Message}; +use util::network::{NetworkProtocolHandler, NetworkService, NetworkContext, PeerId}; +use util::TimerToken; +use util::Bytes; use sync::chain::ChainSync; use sync::io::NetSyncIo; @@ -35,6 +37,13 @@ mod range_collection; #[cfg(test)] mod tests; +/// Message type for external events +pub enum SyncMessage { + /// New block has been imported into the blockchain + NewBlock(Bytes) +} + + /// Ethereum network protocol handler pub struct EthSync { /// Shared blockchain client. TODO: this should evetually become an IPC endpoint @@ -47,7 +56,7 @@ pub use self::chain::SyncStatus; impl EthSync { /// Creates and register protocol with the network service - pub fn register(service: &mut NetworkService, chain: Arc) { + pub fn register(service: &mut NetworkService, chain: Arc) { let sync = Box::new(EthSync { chain: chain, sync: ChainSync::new(), @@ -61,39 +70,39 @@ impl EthSync { } /// Stop sync - pub fn stop(&mut self, io: &mut HandlerIo) { + pub fn stop(&mut self, io: &mut NetworkContext) { self.sync.abort(&mut NetSyncIo::new(io, Arc::get_mut(&mut self.chain).unwrap())); } /// Restart sync - pub fn restart(&mut self, io: &mut HandlerIo) { + pub fn restart(&mut self, io: &mut NetworkContext) { self.sync.restart(&mut NetSyncIo::new(io, Arc::get_mut(&mut self.chain).unwrap())); } } -impl ProtocolHandler for EthSync { - fn initialize(&mut self, io: &mut HandlerIo) { +impl NetworkProtocolHandler for EthSync { + fn initialize(&mut self, io: &mut NetworkContext) { self.sync.restart(&mut NetSyncIo::new(io, Arc::get_mut(&mut self.chain).unwrap())); io.register_timer(1000).unwrap(); } - fn read(&mut self, io: &mut HandlerIo, peer: &PeerId, packet_id: u8, data: &[u8]) { + fn read(&mut self, io: &mut NetworkContext, peer: &PeerId, packet_id: u8, data: &[u8]) { self.sync.on_packet(&mut NetSyncIo::new(io, Arc::get_mut(&mut self.chain).unwrap()), peer, packet_id, data); } - fn connected(&mut self, io: &mut HandlerIo, peer: &PeerId) { + fn connected(&mut self, io: &mut NetworkContext, peer: &PeerId) { self.sync.on_peer_connected(&mut NetSyncIo::new(io, Arc::get_mut(&mut self.chain).unwrap()), peer); } - fn disconnected(&mut self, io: &mut HandlerIo, peer: &PeerId) { + fn disconnected(&mut self, io: &mut NetworkContext, peer: &PeerId) { self.sync.on_peer_aborting(&mut NetSyncIo::new(io, Arc::get_mut(&mut self.chain).unwrap()), peer); } - fn timeout(&mut self, io: &mut HandlerIo, _timer: TimerToken) { + fn timeout(&mut self, io: &mut NetworkContext, _timer: TimerToken) { self.sync.maintain_sync(&mut NetSyncIo::new(io, Arc::get_mut(&mut self.chain).unwrap())); } - fn message(&mut self, _io: &mut HandlerIo, _message: &Message) { + fn message(&mut self, _io: &mut NetworkContext, _message: &SyncMessage) { } }