openethereum/src/sync/mod.rs

103 lines
2.9 KiB
Rust
Raw Normal View History

2016-01-09 18:40:13 +01:00
/// Blockchain sync module
/// Implements ethereum protocol version 63 as specified here:
/// https://github.com/ethereum/wiki/wiki/Ethereum-Wire-Protocol
///
/// Usage example:
///
/// ```rust
/// extern crate ethcore_util as util;
/// extern crate ethcore;
/// use std::env;
/// use std::sync::Arc;
/// use util::network::NetworkService;
/// use ethcore::client::Client;
/// use ethcore::sync::EthSync;
/// use ethcore::ethereum;
2016-01-11 11:51:31 +01:00
/// use ethcore::ethereum::ethash::Ethash;
2016-01-09 18:40:13 +01:00
///
/// fn main() {
/// let mut service = NetworkService::start().unwrap();
2016-01-11 11:51:31 +01:00
/// let engine = Ethash::new_arc(ethereum::new_frontier());
2016-01-09 18:40:13 +01:00
/// let dir = env::temp_dir();
2016-01-11 11:51:31 +01:00
/// let client = Arc::new(Client::new(engine, &dir));
2016-01-09 18:40:13 +01:00
/// EthSync::register(&mut service, client);
/// }
/// ```
2016-01-07 20:43:37 +01:00
use std::sync::Arc;
use client::BlockChainClient;
2016-01-09 18:40:13 +01:00
use util::network::{ProtocolHandler, NetworkService, HandlerIo, TimerToken, PeerId, Message};
2016-01-07 20:43:37 +01:00
use sync::chain::ChainSync;
2016-01-09 18:40:13 +01:00
use sync::io::NetSyncIo;
2015-12-22 22:19:50 +01:00
mod chain;
2016-01-09 18:40:13 +01:00
mod io;
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
2016-01-09 18:40:13 +01:00
/// Ethereum network protocol handler
2015-12-25 14:55:55 +01:00
pub struct EthSync {
2016-01-09 18:40:13 +01:00
/// Shared blockchain client. TODO: this should evetually become an IPC endpoint
2016-01-08 16:26:00 +01:00
chain: Arc<BlockChainClient + Send + Sized>,
2016-01-09 18:40:13 +01:00
/// Sync strategy
2015-12-24 17:18:47 +01:00
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-09 18:40:13 +01:00
/// Creates and register protocol with the network service
pub fn register(service: &mut NetworkService, chain: Arc<BlockChainClient + Send + Sized>) {
let sync = Box::new(EthSync {
2016-01-07 16:08:12 +01:00
chain: chain,
sync: ChainSync::new(),
2016-01-09 18:40:13 +01:00
});
service.register_protocol(sync, "eth", &[62u8, 63u8]).expect("Error registering eth protocol handler");
2015-12-25 14:55:55 +01:00
}
2016-01-09 18:40:13 +01:00
/// Get sync status
2015-12-25 14:55:55 +01:00
pub fn status(&self) -> SyncStatus {
self.sync.status()
}
2016-01-09 18:40:13 +01:00
/// Stop sync
pub fn stop(&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
}
2016-01-09 18:40:13 +01:00
/// Restart sync
pub fn restart(&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
}