openethereum/src/sync/mod.rs

69 lines
1.8 KiB
Rust
Raw Normal View History

2015-12-22 22:19:50 +01:00
use std::sync::{Arc};
use eth::{BlockChainClient};
2015-12-24 17:18:47 +01:00
use util::network::{ProtocolHandler, NetworkService, HandlerIo, TimerToken, PeerId};
use sync::chain::{ChainSync, SyncIo};
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-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 {
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) {
self.sync.abort(&mut SyncIo::new(io, Arc::get_mut(&mut self.chain).unwrap()));
}
pub fn start_network(&mut self, io: &mut HandlerIo) {
self.sync.restart(&mut SyncIo::new(io, Arc::get_mut(&mut self.chain).unwrap()));
}
}
2015-12-22 22:19:50 +01:00
impl ProtocolHandler for EthSync {
fn initialize(&mut self, io: &mut HandlerIo) {
2015-12-25 14:55:55 +01:00
self.sync.restart(&mut SyncIo::new(io, Arc::get_mut(&mut self.chain).unwrap()));
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-24 17:18:47 +01:00
self.sync.on_packet(&mut SyncIo::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-24 17:18:47 +01:00
self.sync.on_peer_connected(&mut SyncIo::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-24 17:18:47 +01:00
self.sync.on_peer_aborting(&mut SyncIo::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) {
self.sync.maintain_sync(&mut SyncIo::new(io, Arc::get_mut(&mut self.chain).unwrap()));
2015-12-22 22:19:50 +01:00
}
}