2020-09-22 14:53:52 +02:00
|
|
|
// Copyright 2015-2020 Parity Technologies (UK) Ltd.
|
|
|
|
// This file is part of OpenEthereum.
|
2016-02-05 13:40:41 +01:00
|
|
|
|
2020-09-22 14:53:52 +02:00
|
|
|
// OpenEthereum is free software: you can redistribute it and/or modify
|
2016-02-05 13:40:41 +01:00
|
|
|
// 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.
|
|
|
|
|
2020-09-22 14:53:52 +02:00
|
|
|
// OpenEthereum is distributed in the hope that it will be useful,
|
2016-02-05 13:40:41 +01:00
|
|
|
// 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
|
2020-09-22 14:53:52 +02:00
|
|
|
// along with OpenEthereum. If not, see <http://www.gnu.org/licenses/>.
|
2016-02-05 13:40:41 +01:00
|
|
|
|
2016-02-02 15:29:53 +01:00
|
|
|
//! Creates and registers client and network services.
|
|
|
|
|
2018-04-14 21:35:58 +02:00
|
|
|
use std::{path::Path, sync::Arc, time::Duration};
|
2018-03-13 11:49:57 +01:00
|
|
|
|
|
|
|
use ansi_term::Colour;
|
|
|
|
use io::{IoContext, IoError, IoHandler, IoService, TimerToken};
|
2018-01-19 13:46:31 +01:00
|
|
|
use stop_guard::StopGuard;
|
2016-07-20 18:13:56 +02:00
|
|
|
|
2019-01-04 14:05:46 +01:00
|
|
|
use blockchain::{BlockChainDB, BlockChainDBHandler};
|
2018-04-09 14:21:37 +02:00
|
|
|
use ethcore::{
|
|
|
|
client::{ChainNotify, Client, ClientConfig, ClientIoMessage},
|
2018-03-13 11:49:57 +01:00
|
|
|
error::{Error as EthcoreError, ErrorKind},
|
|
|
|
miner::Miner,
|
|
|
|
snapshot::{
|
|
|
|
service::{Service as SnapshotService, ServiceParams as SnapServiceParams},
|
2019-06-25 15:38:29 +02:00
|
|
|
Error as SnapshotError, RestorationStatus, SnapshotService as _SnapshotService,
|
2020-08-05 06:08:03 +02:00
|
|
|
},
|
2018-03-13 11:49:57 +01:00
|
|
|
spec::Spec,
|
2019-06-25 15:38:29 +02:00
|
|
|
};
|
2018-04-09 16:14:33 +02:00
|
|
|
|
|
|
|
use Error;
|
|
|
|
|
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>,
|
2020-07-29 10:36:15 +02:00
|
|
|
database: Arc<dyn 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,
|
2020-07-29 10:36:15 +02:00
|
|
|
blockchain_db: Arc<dyn BlockChainDB>,
|
2016-09-07 15:27:28 +02:00
|
|
|
snapshot_path: &Path,
|
2020-07-29 10:36:15 +02:00
|
|
|
restoration_db_handler: Box<dyn BlockChainDBHandler>,
|
2017-10-16 17:50:25 +02:00
|
|
|
_ipc_path: &Path,
|
2016-07-11 17:02:42 +02:00
|
|
|
miner: Arc<Miner>,
|
|
|
|
) -> Result<ClientService, Error> {
|
2021-01-14 15:55:52 +01:00
|
|
|
let io_service = IoService::<ClientIoMessage>::start("Client")?;
|
2020-08-05 06:08:03 +02: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())
|
|
|
|
);
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-09-07 15:27:28 +02:00
|
|
|
let pruning = config.pruning;
|
2018-11-28 11:30:05 +01: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());
|
2020-08-05 06:08:03 +02:00
|
|
|
|
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(),
|
2018-11-18 00:06:34 +01:00
|
|
|
client: client.clone(),
|
2016-09-07 15:27:28 +02:00
|
|
|
};
|
2016-12-27 12:53:56 +01:00
|
|
|
let snapshot = Arc::new(SnapshotService::new(snapshot_params)?);
|
2020-08-05 06:08:03 +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)?;
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2017-09-05 17:54:05 +02:00
|
|
|
spec.engine.register_client(Arc::downgrade(&client) as _);
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-01-19 13:46:31 +01:00
|
|
|
let stop_guard = StopGuard::new();
|
2020-08-05 06:08:03 +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-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
|
|
|
})
|
|
|
|
}
|
2020-08-05 06:08:03 +02: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,
|
2020-07-29 10:36:15 +02:00
|
|
|
handler: Arc<dyn IoHandler<ClientIoMessage> + Send>,
|
2016-07-11 17:02:42 +02:00
|
|
|
) -> Result<(), IoError> {
|
|
|
|
self.io_service.register_handler(handler)
|
2016-01-16 13:30:27 +01:00
|
|
|
}
|
2020-08-05 06:08:03 +02: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
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-08-05 17:00:46 +02:00
|
|
|
/// Get snapshot interface.
|
|
|
|
pub fn snapshot_service(&self) -> Arc<SnapshotService> {
|
|
|
|
self.snapshot.clone()
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
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()
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-07-11 17:02:42 +02:00
|
|
|
/// Set the actor to be notified on certain chain events
|
2020-07-29 10:36:15 +02:00
|
|
|
pub fn add_notify(&self, notify: Arc<dyn ChainNotify>) {
|
2016-07-20 12:36:20 +02:00
|
|
|
self.client.add_notify(notify);
|
2016-01-16 13:30:27 +01:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2017-02-20 17:21:55 +01:00
|
|
|
/// Get a handle to the database.
|
2020-07-29 10:36:15 +02:00
|
|
|
pub fn db(&self) -> Arc<dyn BlockChainDB> {
|
2018-06-20 15:13:07 +02:00
|
|
|
self.database.clone()
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-05-29 12:23:15 +02:00
|
|
|
/// Shutdown the Client Service
|
|
|
|
pub fn shutdown(&self) {
|
2019-06-25 15:38:29 +02:00
|
|
|
trace!(target: "shutdown", "Shutting down Client Service");
|
2018-05-29 12:23:15 +02:00
|
|
|
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
|
|
|
}
|
2020-08-05 06:08:03 +02: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 =
|
2020-09-14 16:08:57 +02:00
|
|
|
if let RestorationStatus::Ongoing { .. } = self.snapshot.restoration_status() {
|
2017-10-10 20:30:11 +02:00
|
|
|
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-16 18:30:27 +01:00
|
|
|
}
|
2020-08-05 06:08:03 +02: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;
|
2020-08-05 06:08:03 +02:00
|
|
|
|
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);
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
}
|
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();
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-11-01 11:09:46 +01:00
|
|
|
let res = thread::Builder::new()
|
|
|
|
.name("Periodic Snapshot".into())
|
|
|
|
.spawn(move || {
|
|
|
|
if let Err(e) = snapshot.take_snapshot(&*client, num) {
|
2019-06-25 15:38:29 +02:00
|
|
|
match e {
|
|
|
|
EthcoreError(
|
|
|
|
ErrorKind::Snapshot(SnapshotError::SnapshotAborted),
|
|
|
|
_,
|
|
|
|
) => info!("Snapshot aborted"),
|
|
|
|
_ => warn!("Failed to take snapshot at block #{}: {}", num, e),
|
2016-11-01 11:09:46 +01:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
}
|
2016-11-01 11:09:46 +01:00
|
|
|
});
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-11-01 11:09:46 +01:00
|
|
|
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 {
|
2018-03-13 11:49:57 +01:00
|
|
|
use std::{sync::Arc, thread, time};
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-01-19 17:32:53 +01:00
|
|
|
use tempdir::TempDir;
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-03-13 11:49:57 +01:00
|
|
|
use super::*;
|
|
|
|
use ethcore::{client::ClientConfig, miner::Miner, spec::Spec, test_helpers};
|
2019-01-04 14:05:46 +01:00
|
|
|
use ethcore_db::NUM_COLUMNS;
|
2018-06-20 15:13:07 +02:00
|
|
|
use kvdb_rocksdb::{CompactionProfile, DatabaseConfig};
|
2020-08-05 06:08:03 +02:00
|
|
|
|
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");
|
2020-08-05 06:08:03 +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);
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-04-09 14:21:37 +02:00
|
|
|
client_db_config.memory_budget = client_config.db_cache_size;
|
|
|
|
client_db_config.compaction = CompactionProfile::auto(&client_path);
|
2020-08-05 06:08:03 +02:00
|
|
|
|
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);
|
2020-08-05 06:08:03 +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)),
|
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
|
|
|
}
|