openethereum/sync/src/lib.rs

205 lines
6.3 KiB
Rust
Raw Normal View History

2016-02-05 13:40:41 +01:00
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
2016-01-29 15:56:06 +01:00
#![warn(missing_docs)]
2016-03-11 11:16:49 +01:00
#![cfg_attr(feature="dev", feature(plugin))]
#![cfg_attr(feature="dev", plugin(clippy))]
2016-02-15 00:51:50 +01:00
// Keeps consistency (all lines with `.clone()`) and helpful when changing ref to non-ref.
2016-03-11 11:16:49 +01:00
#![cfg_attr(feature="dev", allow(clone_on_copy))]
2016-03-11 10:57:58 +01:00
// In most cases it expresses function flow better
#![cfg_attr(feature="dev", allow(if_not_else))]
2016-02-15 00:51:50 +01:00
2016-01-29 15:56:06 +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;
//! extern crate ethsync;
//! use std::env;
//! use std::sync::Arc;
//! use util::network::{NetworkService, NetworkConfiguration};
2016-02-25 16:32:34 +01:00
//! use ethcore::client::{Client, ClientConfig};
2016-02-24 21:23:58 +01:00
//! use ethsync::{EthSync, SyncConfig};
2016-01-29 15:56:06 +01:00
//! use ethcore::ethereum;
2016-06-01 12:44:11 +02:00
//! use ethcore::miner::Miner;
2016-01-29 15:56:06 +01:00
//!
//! fn main() {
2016-06-17 18:26:54 +02:00
//! let mut service = NetworkService::new(NetworkConfiguration::new()).unwrap();
//! service.start().unwrap();
2016-01-29 15:56:06 +01:00
//! let dir = env::temp_dir();
//! let miner = Miner::new(false, ethereum::new_frontier(), None);
2016-06-20 10:28:38 +02:00
//! let client = Client::new(
//! ClientConfig::default(),
//! ethereum::new_frontier(true),
2016-06-20 10:28:38 +02:00
//! &dir,
//! Arc::new(miner),
2016-06-20 10:28:38 +02:00
//! service.io().channel()
//! ).unwrap();
2016-06-17 18:26:54 +02:00
//! let sync = EthSync::new(SyncConfig::default(), client);
//! EthSync::register(&mut service, sync);
2016-01-29 15:56:06 +01:00
//! }
//! ```
#[macro_use]
extern crate log;
#[macro_use]
extern crate ethcore_util as util;
extern crate ethcore;
extern crate env_logger;
2016-02-02 14:54:46 +01:00
extern crate time;
2016-02-05 18:34:08 +01:00
extern crate rand;
2016-02-24 22:37:28 +01:00
#[macro_use]
extern crate heapsize;
2016-01-09 18:40:13 +01:00
2016-01-14 19:03:48 +01:00
use std::ops::*;
use std::sync::*;
2016-01-21 23:33:52 +01:00
use util::network::{NetworkProtocolHandler, NetworkService, NetworkContext, PeerId};
2016-02-19 00:50:23 +01:00
use util::TimerToken;
2016-04-06 23:03:07 +02:00
use util::{U256, ONE_U256};
use ethcore::client::Client;
2016-06-17 12:58:28 +02:00
use ethcore::service::{SyncMessage, NetSyncMessage};
2016-01-29 15:56:06 +01:00
use io::NetSyncIo;
2016-06-17 12:58:28 +02:00
use util::io::IoChannel;
2016-06-17 18:26:54 +02:00
use util::{NetworkIoMessage, NetworkError};
use chain::ChainSync;
2015-12-22 22:19:50 +01:00
mod chain;
2016-05-16 14:41:41 +02:00
mod blocks;
2016-01-09 18:40:13 +01:00
mod io;
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-02-24 21:23:58 +01:00
/// Sync configuration
pub struct SyncConfig {
/// Max blocks to download ahead
pub max_download_ahead_blocks: usize,
/// Network ID
pub network_id: U256,
}
impl Default for SyncConfig {
fn default() -> SyncConfig {
SyncConfig {
max_download_ahead_blocks: 20000,
network_id: ONE_U256,
}
}
}
/// Current sync status
pub trait SyncProvider: Send + Sync {
/// Get sync status
fn status(&self) -> SyncStatus;
2016-06-17 12:58:28 +02:00
/// Start the network
fn start_network(&self);
/// Stop the network
fn stop_network(&self);
}
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-21 23:33:52 +01:00
chain: Arc<Client>,
2016-01-09 18:40:13 +01:00
/// Sync strategy
2016-06-17 12:58:28 +02:00
sync: RwLock<ChainSync>,
/// IO communication chnnel.
io_channel: RwLock<IoChannel<NetSyncMessage>>,
2015-12-22 22:19:50 +01:00
}
2016-02-10 16:28:59 +01:00
pub use self::chain::{SyncStatus, SyncState};
2015-12-25 14:55:55 +01:00
impl EthSync {
2016-01-09 18:40:13 +01:00
/// Creates and register protocol with the network service
2016-06-17 18:26:54 +02:00
pub fn new(config: SyncConfig, chain: Arc<Client>) -> Arc<EthSync> {
2016-05-31 20:54:02 +02:00
let sync = ChainSync::new(config, chain.deref());
2016-06-18 15:11:10 +02:00
Arc::new(EthSync {
2016-01-07 16:08:12 +01:00
chain: chain,
2016-05-16 14:41:41 +02:00
sync: RwLock::new(sync),
2016-06-17 12:58:28 +02:00
io_channel: RwLock::new(IoChannel::disconnected()),
2016-06-18 15:11:10 +02:00
})
2015-12-25 14:55:55 +01:00
}
2016-06-17 18:26:54 +02:00
/// Register protocol with the network service
pub fn register(service: &NetworkService<SyncMessage>, sync: Arc<EthSync>) -> Result<(), NetworkError> {
service.register_protocol(sync.clone(), "eth", &[62u8, 63u8])
}
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 23:33:52 +01:00
self.sync.write().unwrap().abort(&mut NetSyncIo::new(io, self.chain.deref()));
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 23:33:52 +01:00
self.sync.write().unwrap().restart(&mut NetSyncIo::new(io, self.chain.deref()));
2015-12-25 14:55:55 +01:00
}
}
impl SyncProvider for EthSync {
/// Get sync status
fn status(&self) -> SyncStatus {
self.sync.read().unwrap().status()
}
2016-06-17 12:58:28 +02:00
fn start_network(&self) {
self.io_channel.read().unwrap().send(NetworkIoMessage::User(SyncMessage::StartNetwork)).expect("Error sending IO notification");
}
fn stop_network(&self) {
self.io_channel.read().unwrap().send(NetworkIoMessage::User(SyncMessage::StopNetwork)).expect("Error sending IO notification");
}
2015-12-25 14:55:55 +01:00
}
2016-01-13 15:10:48 +01:00
impl NetworkProtocolHandler<SyncMessage> for EthSync {
2016-02-02 14:54:46 +01:00
fn initialize(&self, io: &NetworkContext<SyncMessage>) {
io.register_timer(0, 1000).expect("Error registering sync timer");
2016-06-17 12:58:28 +02:00
*self.io_channel.write().unwrap() = io.io_channel();
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]) {
2016-06-20 17:28:48 +02:00
ChainSync::dispatch_packet(&self.sync, &mut NetSyncIo::new(io, self.chain.deref()) , *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) {
2016-01-21 23:33:52 +01:00
self.sync.write().unwrap().on_peer_connected(&mut NetSyncIo::new(io, self.chain.deref()), *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) {
2016-01-21 23:33:52 +01:00
self.sync.write().unwrap().on_peer_aborting(&mut NetSyncIo::new(io, self.chain.deref()), *peer);
2015-12-22 22:19:50 +01:00
}
2016-02-02 14:54:46 +01:00
fn timeout(&self, io: &NetworkContext<SyncMessage>, _timer: TimerToken) {
self.sync.write().unwrap().maintain_peers(&mut NetSyncIo::new(io, self.chain.deref()));
2016-02-04 02:28:16 +01:00
self.sync.write().unwrap().maintain_sync(&mut NetSyncIo::new(io, self.chain.deref()));
2016-02-02 14:54:46 +01:00
}
2016-02-07 01:00:43 +01:00
2016-06-03 11:36:30 +02:00
#[cfg_attr(feature="dev", allow(single_match))]
2016-02-07 01:00:43 +01:00
fn message(&self, io: &NetworkContext<SyncMessage>, message: &SyncMessage) {
2016-05-31 21:13:32 +02:00
match *message {
SyncMessage::NewChainBlocks { ref imported, ref invalid, ref enacted, ref retracted } => {
let mut sync_io = NetSyncIo::new(io, self.chain.deref());
self.sync.write().unwrap().chain_new_blocks(&mut sync_io, imported, invalid, enacted, retracted);
},
_ => {/* Ignore other messages */},
}
2016-02-07 01:00:43 +01:00
}
2016-02-10 16:28:59 +01:00
}