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-02-02 15:29:53 +01:00
|
|
|
//! Creates and registers client and network services.
|
|
|
|
|
2016-01-13 23:15:53 +01:00
|
|
|
use util::*;
|
2016-02-10 16:35:52 +01:00
|
|
|
use util::panics::*;
|
2016-01-13 23:15:53 +01:00
|
|
|
use spec::Spec;
|
|
|
|
use error::*;
|
2016-02-25 14:09:39 +01:00
|
|
|
use client::{Client, ClientConfig};
|
2016-05-31 19:52:53 +02:00
|
|
|
use miner::Miner;
|
2016-01-13 23:15:53 +01:00
|
|
|
|
2016-01-21 23:33:52 +01:00
|
|
|
/// Message type for external and internal events
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub enum SyncMessage {
|
|
|
|
/// New block has been imported into the blockchain
|
2016-02-23 18:44:13 +01:00
|
|
|
NewChainBlocks {
|
|
|
|
/// Hashes of blocks imported to blockchain
|
2016-03-13 15:29:55 +01:00
|
|
|
imported: Vec<H256>,
|
|
|
|
/// Hashes of blocks not imported to blockchain (because were invalid)
|
|
|
|
invalid: Vec<H256>,
|
2016-03-06 11:11:59 +01:00
|
|
|
/// Hashes of blocks that were removed from canonical chain
|
2016-03-05 16:46:04 +01:00
|
|
|
retracted: Vec<H256>,
|
2016-03-13 15:29:55 +01:00
|
|
|
/// Hashes of blocks that are now included in cannonical chain
|
|
|
|
enacted: Vec<H256>,
|
2016-02-23 18:44:13 +01:00
|
|
|
},
|
2016-03-09 12:54:07 +01:00
|
|
|
/// Best Block Hash in chain has been changed
|
|
|
|
NewChainHead,
|
2016-02-10 16:35:52 +01:00
|
|
|
/// A block is ready
|
2016-01-21 23:33:52 +01:00
|
|
|
BlockVerified,
|
2016-06-17 12:58:28 +02:00
|
|
|
/// Start network command.
|
|
|
|
StartNetwork,
|
|
|
|
/// Stop network command.
|
|
|
|
StopNetwork,
|
2016-01-21 23:33:52 +01:00
|
|
|
}
|
|
|
|
|
2016-02-03 16:43:48 +01:00
|
|
|
/// IO Message type used for Network service
|
2016-01-21 23:33:52 +01:00
|
|
|
pub type NetSyncMessage = NetworkIoMessage<SyncMessage>;
|
|
|
|
|
2016-01-15 01:03:29 +01:00
|
|
|
/// Client service setup. Creates and registers client and network services with the IO subsystem.
|
2016-01-13 23:15:53 +01:00
|
|
|
pub struct ClientService {
|
2016-06-17 12:58:28 +02:00
|
|
|
net_service: Arc<NetworkService<SyncMessage>>,
|
2016-01-21 23:33:52 +01:00
|
|
|
client: Arc<Client>,
|
2016-02-10 16:35:52 +01:00
|
|
|
panic_handler: Arc<PanicHandler>
|
2016-01-13 23:15:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ClientService {
|
2016-01-15 01:03:29 +01:00
|
|
|
/// Start the service in a separate thread.
|
2016-06-17 12:58:28 +02:00
|
|
|
pub fn start(config: ClientConfig, spec: Spec, net_config: NetworkConfiguration, db_path: &Path, miner: Arc<Miner>, enable_network: bool) -> Result<ClientService, Error> {
|
2016-02-10 16:35:52 +01:00
|
|
|
let panic_handler = PanicHandler::new_in_arc();
|
2016-06-17 12:58:28 +02:00
|
|
|
let net_service = try!(NetworkService::new(net_config));
|
2016-02-10 16:35:52 +01:00
|
|
|
panic_handler.forward_from(&net_service);
|
2016-06-17 12:58:28 +02:00
|
|
|
if enable_network {
|
|
|
|
try!(net_service.start());
|
|
|
|
}
|
2016-02-10 16:35:52 +01:00
|
|
|
|
2016-01-14 19:03:48 +01:00
|
|
|
info!("Starting {}", net_service.host_info());
|
2016-04-09 19:20:35 +02:00
|
|
|
info!("Configured for {} using {:?} engine", spec.name, spec.engine.name());
|
2016-05-31 19:52:53 +02:00
|
|
|
let client = try!(Client::new(config, spec, db_path, miner, net_service.io().channel()));
|
2016-02-10 16:35:52 +01:00
|
|
|
panic_handler.forward_from(client.deref());
|
2016-01-21 16:48:37 +01:00
|
|
|
let client_io = Arc::new(ClientIoHandler {
|
2016-01-16 13:30:27 +01:00
|
|
|
client: client.clone()
|
2016-01-13 23:15:53 +01:00
|
|
|
});
|
|
|
|
try!(net_service.io().register_handler(client_io));
|
|
|
|
|
|
|
|
Ok(ClientService {
|
2016-06-17 12:58:28 +02:00
|
|
|
net_service: Arc::new(net_service),
|
2016-01-16 13:30:27 +01:00
|
|
|
client: client,
|
2016-02-10 16:35:52 +01:00
|
|
|
panic_handler: panic_handler,
|
2016-01-13 23:15:53 +01:00
|
|
|
})
|
|
|
|
}
|
2016-01-16 13:30:27 +01:00
|
|
|
|
2016-01-29 15:56:06 +01:00
|
|
|
/// Add a node to network
|
2016-01-23 23:53:20 +01:00
|
|
|
pub fn add_node(&mut self, _enode: &str) {
|
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
|
2016-01-29 15:56:06 +01:00
|
|
|
/// Get general IO interface
|
2016-06-17 12:58:28 +02:00
|
|
|
pub fn register_io_handler(&self, handler: Arc<IoHandler<NetSyncMessage> + Send>) -> Result<(), IoError> {
|
|
|
|
self.net_service.io().register_handler(handler)
|
2016-01-16 13:30:27 +01:00
|
|
|
}
|
|
|
|
|
2016-01-29 15:56:06 +01:00
|
|
|
/// Get client interface
|
2016-01-21 23:33:52 +01:00
|
|
|
pub fn client(&self) -> Arc<Client> {
|
2016-01-16 13:30:27 +01:00
|
|
|
self.client.clone()
|
2016-01-22 04:54:38 +01:00
|
|
|
}
|
2016-01-29 15:56:06 +01:00
|
|
|
|
|
|
|
/// Get network service component
|
2016-06-17 12:58:28 +02:00
|
|
|
pub fn network(&mut self) -> Arc<NetworkService<SyncMessage>> {
|
|
|
|
self.net_service.clone()
|
2016-01-16 13:30:27 +01:00
|
|
|
}
|
2016-01-13 23:15:53 +01:00
|
|
|
}
|
|
|
|
|
2016-02-10 16:35:52 +01:00
|
|
|
impl MayPanic for ClientService {
|
|
|
|
fn on_panic<F>(&self, closure: F) where F: OnPanicListener {
|
|
|
|
self.panic_handler.on_panic(closure);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-15 01:03:29 +01:00
|
|
|
/// IO interface for the Client handler
|
2016-01-13 23:15:53 +01:00
|
|
|
struct ClientIoHandler {
|
2016-01-21 23:33:52 +01:00
|
|
|
client: Arc<Client>
|
2016-01-13 23:15:53 +01:00
|
|
|
}
|
|
|
|
|
2016-01-21 17:21:51 +01:00
|
|
|
const CLIENT_TICK_TIMER: TimerToken = 0;
|
|
|
|
const CLIENT_TICK_MS: u64 = 5000;
|
|
|
|
|
2016-01-13 23:15:53 +01:00
|
|
|
impl IoHandler<NetSyncMessage> for ClientIoHandler {
|
2016-01-21 17:21:51 +01:00
|
|
|
fn initialize(&self, io: &IoContext<NetSyncMessage>) {
|
|
|
|
io.register_timer(CLIENT_TICK_TIMER, CLIENT_TICK_MS).expect("Error registering client timer");
|
|
|
|
}
|
|
|
|
|
|
|
|
fn timeout(&self, _io: &IoContext<NetSyncMessage>, timer: TimerToken) {
|
|
|
|
if timer == CLIENT_TICK_TIMER {
|
2016-01-21 23:33:52 +01:00
|
|
|
self.client.tick();
|
2016-01-21 17:21:51 +01:00
|
|
|
}
|
2016-01-16 18:30:27 +01:00
|
|
|
}
|
2016-01-13 23:15:53 +01:00
|
|
|
|
2016-03-11 11:16:49 +01:00
|
|
|
#[cfg_attr(feature="dev", allow(single_match))]
|
2016-01-21 23:33:52 +01:00
|
|
|
fn message(&self, io: &IoContext<NetSyncMessage>, net_message: &NetSyncMessage) {
|
2016-03-07 14:33:00 +01:00
|
|
|
if let UserMessage(ref message) = *net_message {
|
|
|
|
match *message {
|
|
|
|
SyncMessage::BlockVerified => {
|
2016-01-22 14:44:17 +01:00
|
|
|
self.client.import_verified_blocks(&io.channel());
|
|
|
|
},
|
|
|
|
_ => {}, // ignore other messages
|
2016-01-13 23:15:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-30 14:00:36 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use tests::helpers::*;
|
|
|
|
use util::network::*;
|
2016-02-19 17:11:24 +01:00
|
|
|
use devtools::*;
|
2016-02-25 14:09:39 +01:00
|
|
|
use client::ClientConfig;
|
2016-05-31 22:24:32 +02:00
|
|
|
use std::sync::Arc;
|
|
|
|
use miner::Miner;
|
2016-01-30 14:15:37 +01:00
|
|
|
|
2016-01-30 14:00:36 +01:00
|
|
|
#[test]
|
|
|
|
fn it_can_be_started() {
|
|
|
|
let spec = get_test_spec();
|
2016-02-11 01:40:22 +01:00
|
|
|
let temp_path = RandomTempPath::new();
|
2016-06-20 10:28:38 +02:00
|
|
|
let service = ClientService::start(ClientConfig::default(), spec, NetworkConfiguration::new_local(), &temp_path.as_path(), Arc::new(Miner::with_spec(spec)), false);
|
2016-01-30 14:00:36 +01:00
|
|
|
assert!(service.is_ok());
|
|
|
|
}
|
2016-02-02 15:29:53 +01:00
|
|
|
}
|