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
|
2016-03-11 19:17:09 +01:00
|
|
|
#![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;
|
2016-07-11 17:02:42 +02:00
|
|
|
//! use util::network::{NetworkConfiguration};
|
|
|
|
//! use util::io::IoChannel;
|
2016-02-25 16:32:34 +01:00
|
|
|
//! use ethcore::client::{Client, ClientConfig};
|
2016-07-11 17:02:42 +02:00
|
|
|
//! use ethsync::{EthSync, SyncConfig, ManageNetwork};
|
2016-01-29 15:56:06 +01:00
|
|
|
//! use ethcore::ethereum;
|
2016-07-09 17:22:20 +02:00
|
|
|
//! use ethcore::miner::{GasPricer, Miner};
|
2016-01-29 15:56:06 +01:00
|
|
|
//!
|
|
|
|
//! fn main() {
|
|
|
|
//! let dir = env::temp_dir();
|
2016-07-08 17:26:06 +02:00
|
|
|
//! let miner = Miner::new(
|
|
|
|
//! Default::default(),
|
2016-07-09 16:59:15 +02:00
|
|
|
//! GasPricer::new_fixed(20_000_000_000u64.into()),
|
2016-07-08 17:26:06 +02:00
|
|
|
//! ethereum::new_frontier(),
|
|
|
|
//! None
|
|
|
|
//! );
|
2016-06-20 10:28:38 +02:00
|
|
|
//! let client = Client::new(
|
|
|
|
//! ClientConfig::default(),
|
2016-07-05 18:18:35 +02:00
|
|
|
//! ethereum::new_frontier(),
|
2016-06-20 10:28:38 +02:00
|
|
|
//! &dir,
|
2016-06-24 12:41:48 +02:00
|
|
|
//! miner,
|
2016-07-11 17:02:42 +02:00
|
|
|
//! IoChannel::disconnected()
|
2016-06-20 10:28:38 +02:00
|
|
|
//! ).unwrap();
|
2016-07-11 17:02:42 +02:00
|
|
|
//! let sync = EthSync::new(SyncConfig::default(), client, NetworkConfiguration::new()).unwrap();
|
|
|
|
//! sync.start_network();
|
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::*;
|
2016-07-13 19:59:59 +02:00
|
|
|
use std::sync::Arc;
|
2016-07-11 17:02:42 +02:00
|
|
|
use util::network::{NetworkProtocolHandler, NetworkService, NetworkContext, PeerId, NetworkConfiguration};
|
2016-07-13 19:59:59 +02:00
|
|
|
use util::{TimerToken, U256, H256, RwLock, UtilError};
|
2016-07-11 17:02:42 +02:00
|
|
|
use ethcore::client::{Client, ChainNotify};
|
2016-01-29 15:56:06 +01:00
|
|
|
use io::NetSyncIo;
|
2016-03-08 15:46:44 +01:00
|
|
|
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-07-11 17:02:42 +02:00
|
|
|
/// Ethereum sync protocol
|
|
|
|
pub const ETH_PROTOCOL: &'static str = "eth";
|
|
|
|
|
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,
|
2016-07-06 11:23:29 +02:00
|
|
|
network_id: U256::from(1),
|
2016-02-24 21:23:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-09 17:31:43 +01:00
|
|
|
/// Current sync status
|
2016-03-10 17:32:17 +01:00
|
|
|
pub trait SyncProvider: Send + Sync {
|
2016-03-09 17:31:43 +01:00
|
|
|
/// Get sync status
|
|
|
|
fn status(&self) -> SyncStatus;
|
|
|
|
}
|
|
|
|
|
2016-01-09 18:40:13 +01:00
|
|
|
/// Ethereum network protocol handler
|
2015-12-25 14:55:55 +01:00
|
|
|
pub struct EthSync {
|
2016-07-11 17:02:42 +02:00
|
|
|
/// Network service
|
|
|
|
network: NetworkService,
|
|
|
|
/// Protocol handler
|
|
|
|
handler: Arc<SyncProtocolHandler>,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub use self::chain::{SyncStatus, SyncState};
|
|
|
|
|
|
|
|
impl EthSync {
|
|
|
|
/// Creates and register protocol with the network service
|
|
|
|
pub fn new(config: SyncConfig, chain: Arc<Client>, network_config: NetworkConfiguration) -> Result<Arc<EthSync>, UtilError> {
|
|
|
|
let chain_sync = ChainSync::new(config, chain.deref());
|
|
|
|
let service = try!(NetworkService::new(network_config));
|
|
|
|
let sync = Arc::new(EthSync{
|
|
|
|
network: service,
|
|
|
|
handler: Arc::new(SyncProtocolHandler { sync: RwLock::new(chain_sync), chain: chain }),
|
|
|
|
});
|
|
|
|
|
|
|
|
Ok(sync)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SyncProvider for EthSync {
|
|
|
|
/// Get sync status
|
|
|
|
fn status(&self) -> SyncStatus {
|
2016-07-13 19:59:59 +02:00
|
|
|
self.handler.sync.read().status()
|
2016-07-11 17:02:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct SyncProtocolHandler {
|
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>,
|
2015-12-22 22:19:50 +01:00
|
|
|
}
|
|
|
|
|
2016-07-11 17:02:42 +02:00
|
|
|
impl NetworkProtocolHandler for SyncProtocolHandler {
|
|
|
|
fn initialize(&self, io: &NetworkContext) {
|
|
|
|
io.register_timer(0, 1000).expect("Error registering sync timer");
|
|
|
|
}
|
2015-12-25 14:55:55 +01:00
|
|
|
|
2016-07-11 17:02:42 +02:00
|
|
|
fn read(&self, io: &NetworkContext, peer: &PeerId, packet_id: u8, data: &[u8]) {
|
|
|
|
ChainSync::dispatch_packet(&self.sync, &mut NetSyncIo::new(io, self.chain.deref()), *peer, packet_id, data);
|
2015-12-25 14:55:55 +01:00
|
|
|
}
|
|
|
|
|
2016-07-11 17:02:42 +02:00
|
|
|
fn connected(&self, io: &NetworkContext, peer: &PeerId) {
|
2016-07-13 19:59:59 +02:00
|
|
|
self.sync.write().on_peer_connected(&mut NetSyncIo::new(io, self.chain.deref()), *peer);
|
2016-06-17 18:26:54 +02:00
|
|
|
}
|
|
|
|
|
2016-07-11 17:02:42 +02:00
|
|
|
fn disconnected(&self, io: &NetworkContext, peer: &PeerId) {
|
2016-07-13 19:59:59 +02:00
|
|
|
self.sync.write().on_peer_aborting(&mut NetSyncIo::new(io, self.chain.deref()), *peer);
|
2015-12-25 14:55:55 +01:00
|
|
|
}
|
|
|
|
|
2016-07-11 17:02:42 +02:00
|
|
|
fn timeout(&self, io: &NetworkContext, _timer: TimerToken) {
|
2016-07-13 19:59:59 +02:00
|
|
|
self.sync.write().maintain_peers(&mut NetSyncIo::new(io, self.chain.deref()));
|
|
|
|
self.sync.write().maintain_sync(&mut NetSyncIo::new(io, self.chain.deref()));
|
2015-12-25 14:55:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-11 17:02:42 +02:00
|
|
|
impl ChainNotify for EthSync {
|
|
|
|
fn new_blocks(&self,
|
|
|
|
imported: Vec<H256>,
|
|
|
|
invalid: Vec<H256>,
|
|
|
|
enacted: Vec<H256>,
|
|
|
|
retracted: Vec<H256>,
|
|
|
|
sealed: Vec<H256>)
|
|
|
|
{
|
|
|
|
self.network.with_context(ETH_PROTOCOL, |context| {
|
|
|
|
let mut sync_io = NetSyncIo::new(context, self.handler.chain.deref());
|
2016-07-13 19:59:59 +02:00
|
|
|
self.handler.sync.write().chain_new_blocks(
|
2016-07-11 17:02:42 +02:00
|
|
|
&mut sync_io,
|
|
|
|
&imported,
|
|
|
|
&invalid,
|
|
|
|
&enacted,
|
|
|
|
&retracted,
|
|
|
|
&sealed);
|
|
|
|
});
|
2016-03-09 17:31:43 +01:00
|
|
|
}
|
2016-06-17 12:58:28 +02:00
|
|
|
|
2016-07-11 17:02:42 +02:00
|
|
|
fn start(&self) {
|
|
|
|
self.network.start().unwrap_or_else(|e| warn!("Error starting network: {:?}", e));
|
|
|
|
self.network.register_protocol(self.handler.clone(), ETH_PROTOCOL, &[62u8, 63u8])
|
|
|
|
.unwrap_or_else(|e| warn!("Error registering ethereum protocol: {:?}", e));
|
2016-06-17 12:58:28 +02:00
|
|
|
}
|
|
|
|
|
2016-07-11 17:02:42 +02:00
|
|
|
fn stop(&self) {
|
|
|
|
self.network.stop().unwrap_or_else(|e| warn!("Error stopping network: {:?}", e));
|
2016-06-17 12:58:28 +02:00
|
|
|
}
|
2015-12-25 14:55:55 +01:00
|
|
|
}
|
|
|
|
|
2016-07-11 17:02:42 +02:00
|
|
|
/// Trait for managing network
|
|
|
|
pub trait ManageNetwork : Send + Sync {
|
|
|
|
/// Set mode for reserved peers (allow/deny peers that are unreserved)
|
|
|
|
fn set_non_reserved_mode(&self, mode: ::util::network::NonReservedPeerMode);
|
|
|
|
/// Remove reservation for the peer
|
|
|
|
fn remove_reserved_peer(&self, peer: &str) -> Result<(), String>;
|
|
|
|
/// Add reserved peer
|
|
|
|
fn add_reserved_peer(&self, peer: &str) -> Result<(), String>;
|
|
|
|
/// Start network
|
|
|
|
fn start_network(&self);
|
|
|
|
/// Stop network
|
|
|
|
fn stop_network(&self);
|
|
|
|
/// Query the current configuration of the network
|
|
|
|
fn network_config(&self) -> NetworkConfiguration;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ManageNetwork for EthSync {
|
|
|
|
fn set_non_reserved_mode(&self, mode: ::util::network::NonReservedPeerMode) {
|
|
|
|
self.network.set_non_reserved_mode(mode);
|
2015-12-22 22:19:50 +01:00
|
|
|
}
|
|
|
|
|
2016-07-11 17:02:42 +02:00
|
|
|
fn remove_reserved_peer(&self, peer: &str) -> Result<(), String> {
|
|
|
|
self.network.remove_reserved_peer(peer).map_err(|e| format!("{:?}", e))
|
2015-12-22 22:19:50 +01:00
|
|
|
}
|
|
|
|
|
2016-07-11 17:02:42 +02:00
|
|
|
fn add_reserved_peer(&self, peer: &str) -> Result<(), String> {
|
|
|
|
self.network.add_reserved_peer(peer).map_err(|e| format!("{:?}", e))
|
2015-12-22 22:19:50 +01:00
|
|
|
}
|
|
|
|
|
2016-07-11 17:02:42 +02:00
|
|
|
fn start_network(&self) {
|
|
|
|
self.start();
|
2015-12-22 22:19:50 +01:00
|
|
|
}
|
2016-02-02 14:54:46 +01:00
|
|
|
|
2016-07-11 17:02:42 +02:00
|
|
|
fn stop_network(&self) {
|
|
|
|
self.network.with_context(ETH_PROTOCOL, |context| {
|
|
|
|
let mut sync_io = NetSyncIo::new(context, self.handler.chain.deref());
|
2016-07-13 19:59:59 +02:00
|
|
|
self.handler.sync.write().abort(&mut sync_io);
|
2016-07-11 17:02:42 +02:00
|
|
|
});
|
|
|
|
self.stop();
|
2016-02-02 14:54:46 +01:00
|
|
|
}
|
2016-02-07 01:00:43 +01:00
|
|
|
|
2016-07-11 17:02:42 +02:00
|
|
|
fn network_config(&self) -> NetworkConfiguration {
|
|
|
|
self.network.config().clone()
|
2016-02-07 01:00:43 +01:00
|
|
|
}
|
2016-02-10 16:28:59 +01:00
|
|
|
}
|