2018-06-04 10:19:50 +02:00
|
|
|
// Copyright 2015-2018 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;
|
2018-04-14 21:35:58 +02:00
|
|
|
use std::time::Duration;
|
2018-03-13 11:49:57 +01:00
|
|
|
|
|
|
|
use ansi_term::Colour;
|
2018-08-29 14:31:04 +02:00
|
|
|
use ethereum_types::H256;
|
2018-03-13 11:49:57 +01:00
|
|
|
use io::{IoContext, TimerToken, IoHandler, IoService, IoError};
|
2018-01-19 13:46:31 +01:00
|
|
|
use stop_guard::StopGuard;
|
2016-07-20 18:13:56 +02:00
|
|
|
|
2018-04-10 12:13:49 +02:00
|
|
|
use sync::PrivateTxHandler;
|
2018-06-20 15:13:07 +02:00
|
|
|
use ethcore::{BlockChainDB, BlockChainDBHandler};
|
2018-04-09 14:21:37 +02:00
|
|
|
use ethcore::client::{Client, ClientConfig, ChainNotify, ClientIoMessage};
|
2018-03-13 11:49:57 +01:00
|
|
|
use ethcore::miner::Miner;
|
|
|
|
use ethcore::snapshot::service::{Service as SnapshotService, ServiceParams as SnapServiceParams};
|
2018-05-29 12:23:15 +02:00
|
|
|
use ethcore::snapshot::{SnapshotService as _SnapshotService, RestorationStatus};
|
2018-03-13 11:49:57 +01:00
|
|
|
use ethcore::spec::Spec;
|
2018-04-09 16:14:33 +02:00
|
|
|
use ethcore::account_provider::AccountProvider;
|
|
|
|
|
2018-05-09 08:49:34 +02:00
|
|
|
use ethcore_private_tx::{self, Importer};
|
2018-04-09 16:14:33 +02:00
|
|
|
use Error;
|
|
|
|
|
|
|
|
pub struct PrivateTxService {
|
|
|
|
provider: Arc<ethcore_private_tx::Provider>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PrivateTxService {
|
|
|
|
fn new(provider: Arc<ethcore_private_tx::Provider>) -> Self {
|
|
|
|
PrivateTxService {
|
|
|
|
provider,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns underlying provider.
|
|
|
|
pub fn provider(&self) -> Arc<ethcore_private_tx::Provider> {
|
|
|
|
self.provider.clone()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PrivateTxHandler for PrivateTxService {
|
2018-08-29 14:31:04 +02:00
|
|
|
fn import_private_transaction(&self, rlp: &[u8]) -> Result<H256, String> {
|
|
|
|
match self.provider.import_private_transaction(rlp) {
|
|
|
|
Ok(import_result) => Ok(import_result),
|
|
|
|
Err(err) => {
|
|
|
|
warn!(target: "privatetx", "Unable to import private transaction packet: {}", err);
|
|
|
|
bail!(err.to_string())
|
|
|
|
}
|
|
|
|
}
|
2018-04-09 16:14:33 +02:00
|
|
|
}
|
|
|
|
|
2018-08-29 14:31:04 +02:00
|
|
|
fn import_signed_private_transaction(&self, rlp: &[u8]) -> Result<H256, String> {
|
|
|
|
match self.provider.import_signed_private_transaction(rlp) {
|
|
|
|
Ok(import_result) => Ok(import_result),
|
|
|
|
Err(err) => {
|
|
|
|
warn!(target: "privatetx", "Unable to import signed private transaction packet: {}", err);
|
|
|
|
bail!(err.to_string())
|
|
|
|
}
|
|
|
|
}
|
2018-04-09 16:14:33 +02:00
|
|
|
}
|
|
|
|
}
|
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>,
|
2018-04-09 16:14:33 +02:00
|
|
|
private_tx: Arc<PrivateTxService>,
|
2018-06-20 15:13:07 +02:00
|
|
|
database: Arc<BlockChainDB>,
|
2018-01-19 13:46:31 +01:00
|
|
|
_stop_guard: 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,
|
2018-06-20 15:13:07 +02:00
|
|
|
blockchain_db: Arc<BlockChainDB>,
|
2016-09-07 15:27:28 +02:00
|
|
|
snapshot_path: &Path,
|
2018-06-20 15:13:07 +02:00
|
|
|
restoration_db_handler: Box<BlockChainDBHandler>,
|
2017-10-16 17:50:25 +02:00
|
|
|
_ipc_path: &Path,
|
2016-07-11 17:02:42 +02:00
|
|
|
miner: Arc<Miner>,
|
2018-04-09 16:14:33 +02:00
|
|
|
account_provider: Arc<AccountProvider>,
|
|
|
|
encryptor: Box<ethcore_private_tx::Encryptor>,
|
|
|
|
private_tx_conf: ethcore_private_tx::ProviderConfig,
|
2016-07-11 17:02:42 +02:00
|
|
|
) -> 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 pruning = config.pruning;
|
2018-06-20 15:13:07 +02:00
|
|
|
let client = Client::new(config, &spec, blockchain_db.clone(), miner.clone(), io_service.channel())?;
|
2018-07-13 12:23:57 +02:00
|
|
|
miner.set_io_channel(io_service.channel());
|
2018-08-17 17:01:32 +02:00
|
|
|
miner.set_in_chain_checker(&client.clone());
|
2016-09-07 15:27:28 +02:00
|
|
|
|
|
|
|
let snapshot_params = SnapServiceParams {
|
|
|
|
engine: spec.engine.clone(),
|
|
|
|
genesis_block: spec.genesis_block(),
|
2018-04-09 14:21:37 +02:00
|
|
|
restoration_db_handler: restoration_db_handler,
|
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
|
|
|
|
2018-04-13 17:34:27 +02:00
|
|
|
let provider = Arc::new(ethcore_private_tx::Provider::new(
|
|
|
|
client.clone(),
|
|
|
|
miner,
|
|
|
|
account_provider,
|
|
|
|
encryptor,
|
|
|
|
private_tx_conf,
|
2018-05-09 08:49:34 +02:00
|
|
|
io_service.channel(),
|
|
|
|
));
|
2018-04-09 16:14:33 +02:00
|
|
|
let private_tx = Arc::new(PrivateTxService::new(provider));
|
|
|
|
|
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
|
|
|
|
2018-01-19 13:46:31 +01:00
|
|
|
let stop_guard = StopGuard::new();
|
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,
|
2018-04-09 16:14:33 +02:00
|
|
|
private_tx,
|
2018-06-20 15:13:07 +02:00
|
|
|
database: blockchain_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()
|
|
|
|
}
|
|
|
|
|
2018-04-09 16:14:33 +02:00
|
|
|
/// Get private transaction service.
|
|
|
|
pub fn private_tx_service(&self) -> Arc<PrivateTxService> {
|
|
|
|
self.private_tx.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.
|
2018-06-20 15:13:07 +02:00
|
|
|
pub fn db(&self) -> Arc<BlockChainDB> { self.database.clone() }
|
2018-05-29 12:23:15 +02:00
|
|
|
|
|
|
|
/// Shutdown the Client Service
|
|
|
|
pub fn shutdown(&self) {
|
|
|
|
self.snapshot.shutdown();
|
|
|
|
}
|
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;
|
|
|
|
|
2018-04-14 21:35:58 +02:00
|
|
|
const CLIENT_TICK: Duration = Duration::from_secs(5);
|
|
|
|
const SNAPSHOT_TICK: Duration = Duration::from_secs(10);
|
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>) {
|
2018-04-14 21:35:58 +02:00
|
|
|
io.register_timer(CLIENT_TICK_TIMER, CLIENT_TICK).expect("Error registering client timer");
|
|
|
|
io.register_timer(SNAPSHOT_TICK_TIMER, SNAPSHOT_TICK).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) {
|
2018-05-09 08:49:34 +02:00
|
|
|
trace_time!("service::read");
|
2016-09-06 17:44:11 +02:00
|
|
|
match timer {
|
2017-10-10 20:30:11 +02:00
|
|
|
CLIENT_TICK_TIMER => {
|
2018-03-13 11:49:57 +01:00
|
|
|
use ethcore::snapshot::SnapshotService;
|
2017-10-10 20:30:11 +02:00
|
|
|
let snapshot_restoration = if let RestorationStatus::Ongoing{..} = self.snapshot.status() { true } else { false };
|
|
|
|
self.client.tick(snapshot_restoration)
|
|
|
|
},
|
2016-09-06 17:44:11 +02:00
|
|
|
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-07-19 09:21:41 +02:00
|
|
|
fn message(&self, _io: &IoContext<ClientIoMessage>, net_message: &ClientIoMessage) {
|
2018-05-09 08:49:34 +02:00
|
|
|
trace_time!("service::message");
|
2016-11-01 11:09:46 +01:00
|
|
|
use std::thread;
|
|
|
|
|
2016-07-06 18:28:11 +02:00
|
|
|
match *net_message {
|
2018-05-09 08:49:34 +02:00
|
|
|
ClientIoMessage::BlockVerified => {
|
|
|
|
self.client.import_verified_blocks();
|
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);
|
|
|
|
}
|
|
|
|
}
|
2018-05-09 08:49:34 +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
|
|
|
},
|
2018-05-09 08:49:34 +02:00
|
|
|
ClientIoMessage::Execute(ref exec) => {
|
|
|
|
(*exec.0)(&self.client);
|
|
|
|
}
|
2016-07-06 18:28:11 +02:00
|
|
|
_ => {} // ignore other messages
|
2016-01-13 23:15:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-30 14:00:36 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2016-05-31 22:24:32 +02:00
|
|
|
use std::sync::Arc;
|
2018-03-13 11:49:57 +01:00
|
|
|
use std::{time, thread};
|
|
|
|
|
2018-01-19 17:32:53 +01:00
|
|
|
use tempdir::TempDir;
|
2016-01-30 14:15:37 +01:00
|
|
|
|
2018-04-09 16:14:33 +02:00
|
|
|
use ethcore::account_provider::AccountProvider;
|
2018-03-13 11:49:57 +01:00
|
|
|
use ethcore::client::ClientConfig;
|
|
|
|
use ethcore::miner::Miner;
|
|
|
|
use ethcore::spec::Spec;
|
2018-04-09 14:21:37 +02:00
|
|
|
use ethcore::db::NUM_COLUMNS;
|
2018-06-20 15:13:07 +02:00
|
|
|
use ethcore::test_helpers;
|
|
|
|
use kvdb_rocksdb::{DatabaseConfig, CompactionProfile};
|
2018-03-13 11:49:57 +01:00
|
|
|
use super::*;
|
|
|
|
|
2018-04-09 16:14:33 +02:00
|
|
|
use ethcore_private_tx;
|
|
|
|
|
2016-01-30 14:00:36 +01:00
|
|
|
#[test]
|
|
|
|
fn it_can_be_started() {
|
2018-01-19 17:32:53 +01:00
|
|
|
let tempdir = TempDir::new("").unwrap();
|
|
|
|
let client_path = tempdir.path().join("client");
|
|
|
|
let snapshot_path = tempdir.path().join("snapshot");
|
2016-08-05 17:00:46 +02:00
|
|
|
|
2018-04-09 14:21:37 +02:00
|
|
|
let client_config = ClientConfig::default();
|
|
|
|
let mut client_db_config = DatabaseConfig::with_columns(NUM_COLUMNS);
|
|
|
|
|
|
|
|
client_db_config.memory_budget = client_config.db_cache_size;
|
|
|
|
client_db_config.compaction = CompactionProfile::auto(&client_path);
|
|
|
|
|
2018-06-20 15:13:07 +02:00
|
|
|
let client_db_handler = test_helpers::restoration_db_handler(client_db_config.clone());
|
|
|
|
let client_db = client_db_handler.open(&client_path).unwrap();
|
|
|
|
let restoration_db_handler = test_helpers::restoration_db_handler(client_db_config);
|
2018-04-09 14:21:37 +02:00
|
|
|
|
2018-03-12 18:05:52 +01:00
|
|
|
let spec = Spec::new_test();
|
2016-06-23 21:04:23 +02:00
|
|
|
let service = ClientService::start(
|
|
|
|
ClientConfig::default(),
|
2016-08-05 23:33:55 +02:00
|
|
|
&spec,
|
2018-04-09 14:21:37 +02:00
|
|
|
client_db,
|
2016-09-07 15:27:28 +02:00
|
|
|
&snapshot_path,
|
2018-04-09 14:21:37 +02:00
|
|
|
restoration_db_handler,
|
2018-01-19 17:32:53 +01:00
|
|
|
tempdir.path(),
|
2018-04-13 17:34:27 +02:00
|
|
|
Arc::new(Miner::new_for_tests(&spec, None)),
|
2018-04-09 16:14:33 +02:00
|
|
|
Arc::new(AccountProvider::transient_provider()),
|
|
|
|
Box::new(ethcore_private_tx::NoopEncryptor),
|
2018-04-13 17:34:27 +02:00
|
|
|
Default::default(),
|
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());
|
2018-01-19 17:32:53 +01:00
|
|
|
thread::park_timeout(time::Duration::from_millis(100));
|
2016-01-30 14:00:36 +01:00
|
|
|
}
|
2016-02-02 15:29:53 +01:00
|
|
|
}
|