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;
|
2016-01-09 18:50:45 +01:00
|
|
|
/// use ethcore::ethereum;
|
2016-01-09 18:40:13 +01:00
|
|
|
///
|
|
|
|
/// fn main() {
|
|
|
|
/// let mut service = NetworkService::start().unwrap();
|
|
|
|
/// let dir = env::temp_dir();
|
2016-01-11 12:28:59 +01:00
|
|
|
/// let client = Arc::new(Client::new(ethereum::new_frontier(), &dir).unwrap());
|
2016-01-09 18:40:13 +01:00
|
|
|
/// EthSync::register(&mut service, client);
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
|
2016-01-14 19:03:48 +01:00
|
|
|
use std::ops::*;
|
|
|
|
use std::sync::*;
|
|
|
|
use client::Client;
|
2016-01-13 23:15:44 +01:00
|
|
|
use util::network::{NetworkProtocolHandler, NetworkService, NetworkContext, PeerId, NetworkIoMessage};
|
2016-01-07 20:43:37 +01:00
|
|
|
use sync::chain::ChainSync;
|
2016-01-21 16:48:37 +01:00
|
|
|
use util::{Bytes, TimerToken};
|
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-21 16:48:37 +01:00
|
|
|
const SYNC_TIMER: usize = 0;
|
|
|
|
|
2016-01-13 15:10:48 +01:00
|
|
|
/// Message type for external events
|
2016-01-21 16:48:37 +01:00
|
|
|
#[derive(Clone)]
|
2016-01-13 15:10:48 +01:00
|
|
|
pub enum SyncMessage {
|
|
|
|
/// New block has been imported into the blockchain
|
2016-01-21 16:48:37 +01:00
|
|
|
NewChainBlock(Bytes), //TODO: use Cow
|
2016-01-13 23:15:44 +01:00
|
|
|
/// A block is ready
|
2016-01-17 23:07:58 +01:00
|
|
|
BlockVerified,
|
2016-01-13 15:10:48 +01:00
|
|
|
}
|
|
|
|
|
2016-01-19 17:02:01 +01:00
|
|
|
/// TODO [arkpar] Please document me
|
2016-01-13 23:15:44 +01:00
|
|
|
pub type NetSyncMessage = NetworkIoMessage<SyncMessage>;
|
2016-01-13 15:10:48 +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-14 19:03:48 +01:00
|
|
|
chain: Arc<RwLock<Client>>,
|
2016-01-09 18:40:13 +01:00
|
|
|
/// Sync strategy
|
2016-01-21 16:48:37 +01:00
|
|
|
sync: RwLock<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
|
2016-01-14 19:03:48 +01:00
|
|
|
pub fn register(service: &mut NetworkService<SyncMessage>, chain: Arc<RwLock<Client>>) {
|
2016-01-21 16:48:37 +01:00
|
|
|
let sync = Arc::new(EthSync {
|
2016-01-07 16:08:12 +01:00
|
|
|
chain: chain,
|
2016-01-21 16:48:37 +01:00
|
|
|
sync: RwLock::new(ChainSync::new()),
|
2016-01-09 18:40:13 +01:00
|
|
|
});
|
2016-01-21 16:48:37 +01:00
|
|
|
service.register_protocol(sync.clone(), "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 {
|
2016-01-21 16:48:37 +01:00
|
|
|
self.sync.read().unwrap().status()
|
2015-12-25 14:55:55 +01:00
|
|
|
}
|
|
|
|
|
2016-01-09 18:40:13 +01:00
|
|
|
/// Stop sync
|
2016-01-13 15:10:48 +01:00
|
|
|
pub fn stop(&mut self, io: &mut NetworkContext<SyncMessage>) {
|
2016-01-21 16:48:37 +01:00
|
|
|
self.sync.write().unwrap().abort(&mut NetSyncIo::new(io, self.chain.write().unwrap().deref_mut()));
|
2015-12-25 14:55:55 +01:00
|
|
|
}
|
|
|
|
|
2016-01-09 18:40:13 +01:00
|
|
|
/// Restart sync
|
2016-01-13 15:10:48 +01:00
|
|
|
pub fn restart(&mut self, io: &mut NetworkContext<SyncMessage>) {
|
2016-01-21 16:48:37 +01:00
|
|
|
self.sync.write().unwrap().restart(&mut NetSyncIo::new(io, self.chain.write().unwrap().deref_mut()));
|
2015-12-25 14:55:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-13 15:10:48 +01:00
|
|
|
impl NetworkProtocolHandler<SyncMessage> for EthSync {
|
2016-01-21 16:48:37 +01:00
|
|
|
fn initialize(&self, io: &NetworkContext<SyncMessage>) {
|
|
|
|
io.register_timer(SYNC_TIMER, 1000).unwrap();
|
2015-12-22 22:19:50 +01:00
|
|
|
}
|
|
|
|
|
2016-01-21 16:48:37 +01:00
|
|
|
fn read(&self, io: &NetworkContext<SyncMessage>, peer: &PeerId, packet_id: u8, data: &[u8]) {
|
|
|
|
self.sync.write().unwrap().on_packet(&mut NetSyncIo::new(io, self.chain.write().unwrap().deref_mut()) , *peer, packet_id, data);
|
2015-12-22 22:19:50 +01:00
|
|
|
}
|
|
|
|
|
2016-01-21 16:48:37 +01:00
|
|
|
fn connected(&self, io: &NetworkContext<SyncMessage>, peer: &PeerId) {
|
|
|
|
self.sync.write().unwrap().on_peer_connected(&mut NetSyncIo::new(io, self.chain.write().unwrap().deref_mut()), *peer);
|
2015-12-22 22:19:50 +01:00
|
|
|
}
|
|
|
|
|
2016-01-21 16:48:37 +01:00
|
|
|
fn disconnected(&self, io: &NetworkContext<SyncMessage>, peer: &PeerId) {
|
|
|
|
self.sync.write().unwrap().on_peer_aborting(&mut NetSyncIo::new(io, self.chain.write().unwrap().deref_mut()), *peer);
|
2015-12-22 22:19:50 +01:00
|
|
|
}
|
2015-12-28 12:03:05 +01:00
|
|
|
|
2016-01-21 16:48:37 +01:00
|
|
|
fn timeout(&self, io: &NetworkContext<SyncMessage>, timer: TimerToken) {
|
|
|
|
if timer == SYNC_TIMER {
|
|
|
|
self.sync.write().unwrap().maintain_sync(&mut NetSyncIo::new(io, self.chain.write().unwrap().deref_mut()));
|
|
|
|
}
|
2015-12-28 12:03:05 +01:00
|
|
|
}
|
2015-12-22 22:19:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|