2017-01-25 18:51:41 +01:00
|
|
|
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
2016-02-05 13:40:41 +01:00
|
|
|
// 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.
|
|
|
|
|
2017-07-29 21:56:42 +02:00
|
|
|
use std::sync::Arc;
|
|
|
|
use std::path::Path;
|
2017-09-04 16:36:49 +02:00
|
|
|
use bigint::hash::H256;
|
2017-10-10 20:01:27 +02:00
|
|
|
use kvdb::{Database, DatabaseConfig, KeyValueDB};
|
2017-09-06 20:47:45 +02:00
|
|
|
use bytes::Bytes;
|
2016-08-05 10:32:04 +02:00
|
|
|
use io::*;
|
2016-01-13 23:15:53 +01:00
|
|
|
use spec::Spec;
|
|
|
|
use error::*;
|
2017-01-10 12:23:59 +01:00
|
|
|
use client::{Client, ClientConfig, ChainNotify};
|
2016-09-27 16:50:16 +02:00
|
|
|
use miner::Miner;
|
2017-01-25 11:03:36 +01:00
|
|
|
|
2016-08-25 22:20:44 +02:00
|
|
|
use snapshot::ManifestData;
|
2016-09-07 15:27:28 +02:00
|
|
|
use snapshot::service::{Service as SnapshotService, ServiceParams as SnapServiceParams};
|
2016-07-20 18:13:56 +02:00
|
|
|
use std::sync::atomic::AtomicBool;
|
2017-09-01 16:57:57 +02:00
|
|
|
use ansi_term::Colour;
|
2016-07-20 18:13:56 +02:00
|
|
|
|
|
|
|
#[cfg(feature="ipc")]
|
|
|
|
use nanoipc;
|
2016-01-13 23:15:53 +01:00
|
|
|
|
2016-01-21 23:33:52 +01:00
|
|
|
/// Message type for external and internal events
|
2016-11-28 12:47:33 +01:00
|
|
|
#[derive(Clone, PartialEq, Eq, Debug)]
|
2016-07-11 17:02:42 +02:00
|
|
|
pub enum ClientIoMessage {
|
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-19 14:35:42 +02:00
|
|
|
/// New transaction RLPs are ready to be imported
|
2016-12-10 21:22:19 +01:00
|
|
|
NewTransactions(Vec<Bytes>, usize),
|
2016-08-25 22:20:44 +02:00
|
|
|
/// Begin snapshot restoration
|
|
|
|
BeginRestoration(ManifestData),
|
2016-08-05 17:00:46 +02:00
|
|
|
/// Feed a state chunk to the snapshot service
|
|
|
|
FeedStateChunk(H256, Bytes),
|
|
|
|
/// Feed a block chunk to the snapshot service
|
|
|
|
FeedBlockChunk(H256, Bytes),
|
2016-09-02 16:15:25 +02:00
|
|
|
/// Take a snapshot for the block with given number.
|
|
|
|
TakeSnapshot(u64),
|
2016-11-28 16:42:36 +01:00
|
|
|
/// New consensus message received.
|
|
|
|
NewMessage(Bytes)
|
2016-01-21 23:33:52 +01:00
|
|
|
}
|
|
|
|
|
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-07-11 17:02:42 +02:00
|
|
|
io_service: Arc<IoService<ClientIoMessage>>,
|
2016-01-21 23:33:52 +01:00
|
|
|
client: Arc<Client>,
|
2016-08-05 17:00:46 +02:00
|
|
|
snapshot: Arc<SnapshotService>,
|
2017-02-20 17:21:55 +01:00
|
|
|
database: Arc<Database>,
|
2016-07-20 18:13:56 +02:00
|
|
|
_stop_guard: ::devtools::StopGuard,
|
2016-01-13 23:15:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ClientService {
|
2016-09-07 15:27:28 +02:00
|
|
|
/// Start the `ClientService`.
|
2016-07-11 17:02:42 +02:00
|
|
|
pub fn start(
|
|
|
|
config: ClientConfig,
|
2016-08-05 23:33:55 +02:00
|
|
|
spec: &Spec,
|
2016-09-07 15:27:28 +02:00
|
|
|
client_path: &Path,
|
|
|
|
snapshot_path: &Path,
|
2016-08-22 18:41:58 +02:00
|
|
|
ipc_path: &Path,
|
2016-07-11 17:02:42 +02:00
|
|
|
miner: Arc<Miner>,
|
|
|
|
) -> Result<ClientService, Error>
|
|
|
|
{
|
2016-12-27 12:53:56 +01:00
|
|
|
let io_service = IoService::<ClientIoMessage>::start()?;
|
2016-02-10 16:35:52 +01:00
|
|
|
|
2016-07-15 10:11:14 +02:00
|
|
|
info!("Configured for {} using {} engine", Colour::White.bold().paint(spec.name.clone()), Colour::Yellow.bold().paint(spec.engine.name()));
|
2016-08-05 17:00:46 +02:00
|
|
|
|
2016-09-07 15:27:28 +02:00
|
|
|
let mut db_config = DatabaseConfig::with_columns(::db::NUM_COLUMNS);
|
2016-09-27 18:16:32 +02:00
|
|
|
|
|
|
|
// give all rocksdb cache to state column; everything else has its
|
|
|
|
// own caches.
|
|
|
|
if let Some(size) = config.db_cache_size {
|
|
|
|
db_config.set_cache(::db::COL_STATE, size);
|
|
|
|
}
|
|
|
|
|
2016-10-27 08:28:12 +02:00
|
|
|
db_config.compaction = config.db_compaction.compaction_profile(client_path);
|
2016-09-07 15:27:28 +02:00
|
|
|
db_config.wal = config.db_wal;
|
2016-08-05 17:00:46 +02:00
|
|
|
|
2017-02-20 17:21:55 +01:00
|
|
|
let db = Arc::new(Database::open(
|
|
|
|
&db_config,
|
|
|
|
&client_path.to_str().expect("DB path could not be converted to string.")
|
|
|
|
).map_err(::client::Error::Database)?);
|
|
|
|
|
|
|
|
|
2016-09-07 15:27:28 +02:00
|
|
|
let pruning = config.pruning;
|
2017-02-20 17:21:55 +01:00
|
|
|
let client = Client::new(config, &spec, db.clone(), miner, io_service.channel())?;
|
2016-09-07 15:27:28 +02:00
|
|
|
|
|
|
|
let snapshot_params = SnapServiceParams {
|
|
|
|
engine: spec.engine.clone(),
|
|
|
|
genesis_block: spec.genesis_block(),
|
2016-09-27 18:16:32 +02:00
|
|
|
db_config: db_config.clone(),
|
2016-09-07 15:27:28 +02:00
|
|
|
pruning: pruning,
|
|
|
|
channel: io_service.channel(),
|
|
|
|
snapshot_root: snapshot_path.into(),
|
|
|
|
db_restore: client.clone(),
|
|
|
|
};
|
2016-12-27 12:53:56 +01:00
|
|
|
let snapshot = Arc::new(SnapshotService::new(snapshot_params)?);
|
2016-08-05 17:00:46 +02:00
|
|
|
|
2016-01-21 16:48:37 +01:00
|
|
|
let client_io = Arc::new(ClientIoHandler {
|
2016-08-05 17:00:46 +02:00
|
|
|
client: client.clone(),
|
|
|
|
snapshot: snapshot.clone(),
|
2016-01-13 23:15:53 +01:00
|
|
|
});
|
2016-12-27 12:53:56 +01:00
|
|
|
io_service.register_handler(client_io)?;
|
2016-01-13 23:15:53 +01:00
|
|
|
|
2017-09-05 17:54:05 +02:00
|
|
|
spec.engine.register_client(Arc::downgrade(&client) as _);
|
2016-09-27 12:12:18 +02:00
|
|
|
|
2016-07-20 18:13:56 +02:00
|
|
|
let stop_guard = ::devtools::StopGuard::new();
|
2016-09-06 15:31:13 +02:00
|
|
|
run_ipc(ipc_path, client.clone(), snapshot.clone(), stop_guard.share());
|
2016-07-20 18:13:56 +02:00
|
|
|
|
2016-01-13 23:15:53 +01:00
|
|
|
Ok(ClientService {
|
2016-07-11 17:02:42 +02:00
|
|
|
io_service: Arc::new(io_service),
|
2016-01-16 13:30:27 +01:00
|
|
|
client: client,
|
2016-08-05 17:00:46 +02:00
|
|
|
snapshot: snapshot,
|
2017-02-20 17:21:55 +01:00
|
|
|
database: db,
|
2016-07-20 18:13:56 +02:00
|
|
|
_stop_guard: stop_guard,
|
2016-01-13 23:15:53 +01:00
|
|
|
})
|
|
|
|
}
|
2016-01-16 13:30:27 +01:00
|
|
|
|
2016-01-29 15:56:06 +01:00
|
|
|
/// Get general IO interface
|
2016-07-11 17:02:42 +02:00
|
|
|
pub fn register_io_handler(&self, handler: Arc<IoHandler<ClientIoMessage> + Send>) -> Result<(), IoError> {
|
|
|
|
self.io_service.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
|
|
|
|
2016-08-05 17:00:46 +02:00
|
|
|
/// Get snapshot interface.
|
|
|
|
pub fn snapshot_service(&self) -> Arc<SnapshotService> {
|
|
|
|
self.snapshot.clone()
|
|
|
|
}
|
|
|
|
|
2016-01-29 15:56:06 +01:00
|
|
|
/// Get network service component
|
2016-07-11 17:02:42 +02:00
|
|
|
pub fn io(&self) -> Arc<IoService<ClientIoMessage>> {
|
|
|
|
self.io_service.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set the actor to be notified on certain chain events
|
2016-07-20 18:13:56 +02:00
|
|
|
pub fn add_notify(&self, notify: Arc<ChainNotify>) {
|
2016-07-20 12:36:20 +02:00
|
|
|
self.client.add_notify(notify);
|
2016-01-16 13:30:27 +01:00
|
|
|
}
|
2017-02-20 17:21:55 +01:00
|
|
|
|
|
|
|
/// Get a handle to the database.
|
|
|
|
pub fn db(&self) -> Arc<KeyValueDB> { self.database.clone() }
|
2016-01-13 23:15:53 +01:00
|
|
|
}
|
|
|
|
|
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-08-05 17:00:46 +02:00
|
|
|
client: Arc<Client>,
|
|
|
|
snapshot: Arc<SnapshotService>,
|
2016-01-13 23:15:53 +01:00
|
|
|
}
|
|
|
|
|
2016-01-21 17:21:51 +01:00
|
|
|
const CLIENT_TICK_TIMER: TimerToken = 0;
|
2016-09-06 17:44:11 +02:00
|
|
|
const SNAPSHOT_TICK_TIMER: TimerToken = 1;
|
|
|
|
|
2016-01-21 17:21:51 +01:00
|
|
|
const CLIENT_TICK_MS: u64 = 5000;
|
2016-09-06 17:44:11 +02:00
|
|
|
const SNAPSHOT_TICK_MS: u64 = 10000;
|
2016-01-21 17:21:51 +01:00
|
|
|
|
2016-07-11 17:02:42 +02:00
|
|
|
impl IoHandler<ClientIoMessage> for ClientIoHandler {
|
|
|
|
fn initialize(&self, io: &IoContext<ClientIoMessage>) {
|
2016-01-21 17:21:51 +01:00
|
|
|
io.register_timer(CLIENT_TICK_TIMER, CLIENT_TICK_MS).expect("Error registering client timer");
|
2016-09-06 17:44:11 +02:00
|
|
|
io.register_timer(SNAPSHOT_TICK_TIMER, SNAPSHOT_TICK_MS).expect("Error registering snapshot timer");
|
2016-01-21 17:21:51 +01:00
|
|
|
}
|
|
|
|
|
2016-07-11 17:02:42 +02:00
|
|
|
fn timeout(&self, _io: &IoContext<ClientIoMessage>, timer: TimerToken) {
|
2016-09-06 17:44:11 +02:00
|
|
|
match timer {
|
|
|
|
CLIENT_TICK_TIMER => self.client.tick(),
|
|
|
|
SNAPSHOT_TICK_TIMER => self.snapshot.tick(),
|
|
|
|
_ => warn!("IO service triggered unregistered timer '{}'", timer),
|
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-07-19 09:21:41 +02:00
|
|
|
fn message(&self, _io: &IoContext<ClientIoMessage>, net_message: &ClientIoMessage) {
|
2016-11-01 11:09:46 +01:00
|
|
|
use std::thread;
|
|
|
|
|
2016-07-06 18:28:11 +02:00
|
|
|
match *net_message {
|
2016-07-19 09:21:41 +02:00
|
|
|
ClientIoMessage::BlockVerified => { self.client.import_verified_blocks(); }
|
2016-12-10 21:22:19 +01:00
|
|
|
ClientIoMessage::NewTransactions(ref transactions, peer_id) => {
|
|
|
|
self.client.import_queued_transactions(transactions, peer_id);
|
2016-12-10 14:56:41 +01:00
|
|
|
}
|
2016-08-25 22:20:44 +02:00
|
|
|
ClientIoMessage::BeginRestoration(ref manifest) => {
|
2016-09-11 14:05:59 +02:00
|
|
|
if let Err(e) = self.snapshot.init_restore(manifest.clone(), true) {
|
2016-08-25 22:20:44 +02:00
|
|
|
warn!("Failed to initialize snapshot restoration: {}", e);
|
|
|
|
}
|
|
|
|
}
|
2016-08-05 17:00:46 +02:00
|
|
|
ClientIoMessage::FeedStateChunk(ref hash, ref chunk) => self.snapshot.feed_state_chunk(*hash, chunk),
|
|
|
|
ClientIoMessage::FeedBlockChunk(ref hash, ref chunk) => self.snapshot.feed_block_chunk(*hash, chunk),
|
2016-09-02 16:15:25 +02:00
|
|
|
ClientIoMessage::TakeSnapshot(num) => {
|
2016-11-01 11:09:46 +01:00
|
|
|
let client = self.client.clone();
|
|
|
|
let snapshot = self.snapshot.clone();
|
|
|
|
|
|
|
|
let res = thread::Builder::new().name("Periodic Snapshot".into()).spawn(move || {
|
|
|
|
if let Err(e) = snapshot.take_snapshot(&*client, num) {
|
|
|
|
warn!("Failed to take snapshot at block #{}: {}", num, e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if let Err(e) = res {
|
|
|
|
debug!(target: "snapshot", "Failed to initialize periodic snapshot thread: {:?}", e);
|
2016-09-02 16:15:25 +02:00
|
|
|
}
|
2016-09-27 12:12:18 +02:00
|
|
|
},
|
2016-12-11 12:32:01 +01:00
|
|
|
ClientIoMessage::NewMessage(ref message) => if let Err(e) = self.client.engine().handle_message(message) {
|
2016-12-10 11:01:23 +01:00
|
|
|
trace!(target: "poa", "Invalid message received: {}", e);
|
2016-09-27 12:12:18 +02:00
|
|
|
},
|
2016-07-06 18:28:11 +02:00
|
|
|
_ => {} // ignore other messages
|
2016-01-13 23:15:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-20 18:13:56 +02:00
|
|
|
#[cfg(feature="ipc")]
|
2016-09-06 15:31:13 +02:00
|
|
|
fn run_ipc(base_path: &Path, client: Arc<Client>, snapshot_service: Arc<SnapshotService>, stop: Arc<AtomicBool>) {
|
2016-08-23 12:41:12 +02:00
|
|
|
let mut path = base_path.to_owned();
|
2016-08-22 18:41:58 +02:00
|
|
|
path.push("parity-chain.ipc");
|
2016-08-23 12:41:12 +02:00
|
|
|
let socket_addr = format!("ipc://{}", path.to_string_lossy());
|
2016-09-06 15:31:13 +02:00
|
|
|
let s = stop.clone();
|
2016-07-20 18:13:56 +02:00
|
|
|
::std::thread::spawn(move || {
|
|
|
|
let mut worker = nanoipc::Worker::new(&(client as Arc<BlockChainClient>));
|
2016-08-22 18:41:58 +02:00
|
|
|
worker.add_reqrep(&socket_addr).expect("Ipc expected to initialize with no issues");
|
2016-07-20 18:13:56 +02:00
|
|
|
|
2016-09-06 15:31:13 +02:00
|
|
|
while !s.load(::std::sync::atomic::Ordering::Relaxed) {
|
|
|
|
worker.poll();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let mut path = base_path.to_owned();
|
|
|
|
path.push("parity-snapshot.ipc");
|
|
|
|
let socket_addr = format!("ipc://{}", path.to_string_lossy());
|
|
|
|
::std::thread::spawn(move || {
|
|
|
|
let mut worker = nanoipc::Worker::new(&(snapshot_service as Arc<::snapshot::SnapshotService>));
|
|
|
|
worker.add_reqrep(&socket_addr).expect("Ipc expected to initialize with no issues");
|
|
|
|
|
2016-07-20 18:13:56 +02:00
|
|
|
while !stop.load(::std::sync::atomic::Ordering::Relaxed) {
|
|
|
|
worker.poll();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(feature="ipc"))]
|
2016-09-06 15:31:13 +02:00
|
|
|
fn run_ipc(_base_path: &Path, _client: Arc<Client>, _snapshot_service: Arc<SnapshotService>, _stop: Arc<AtomicBool>) {
|
2016-07-20 18:13:56 +02:00
|
|
|
}
|
|
|
|
|
2016-01-30 14:00:36 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use tests::helpers::*;
|
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() {
|
2016-02-11 01:40:22 +01:00
|
|
|
let temp_path = RandomTempPath::new();
|
2016-09-07 15:27:28 +02:00
|
|
|
let path = temp_path.as_path().to_owned();
|
|
|
|
let client_path = {
|
|
|
|
let mut path = path.to_owned();
|
|
|
|
path.push("client");
|
|
|
|
path
|
|
|
|
};
|
|
|
|
|
|
|
|
let snapshot_path = {
|
|
|
|
let mut path = path.to_owned();
|
|
|
|
path.push("snapshot");
|
|
|
|
path
|
|
|
|
};
|
2016-08-05 17:00:46 +02:00
|
|
|
|
2016-08-05 23:33:55 +02:00
|
|
|
let spec = get_test_spec();
|
2016-06-23 21:04:23 +02:00
|
|
|
let service = ClientService::start(
|
|
|
|
ClientConfig::default(),
|
2016-08-05 23:33:55 +02:00
|
|
|
&spec,
|
2016-09-07 15:27:28 +02:00
|
|
|
&client_path,
|
|
|
|
&snapshot_path,
|
2016-08-22 18:41:58 +02:00
|
|
|
&path,
|
2016-08-05 23:33:55 +02:00
|
|
|
Arc::new(Miner::with_spec(&spec)),
|
2016-06-23 21:04:23 +02:00
|
|
|
);
|
2016-01-30 14:00:36 +01:00
|
|
|
assert!(service.is_ok());
|
2016-08-17 15:53:34 +02:00
|
|
|
drop(service.unwrap());
|
|
|
|
::std::thread::park_timeout(::std::time::Duration::from_millis(100));
|
2016-01-30 14:00:36 +01:00
|
|
|
}
|
2016-02-02 15:29:53 +01:00
|
|
|
}
|