2019-01-07 11:33:07 +01:00
|
|
|
// Copyright 2015-2019 Parity Technologies (UK) Ltd.
|
|
|
|
// This file is part of Parity Ethereum.
|
2016-12-15 18:45:11 +01:00
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is free software: you can redistribute it and/or modify
|
2016-12-15 18:45:11 +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.
|
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is distributed in the hope that it will be useful,
|
2016-12-15 18:45:11 +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
|
2019-01-07 11:33:07 +01:00
|
|
|
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
|
2016-12-15 18:45:11 +01:00
|
|
|
|
|
|
|
//! Minimal IO service for light client.
|
|
|
|
//! Just handles block import messages and passes them to the client.
|
|
|
|
|
|
|
|
use std::{fmt, sync::Arc};
|
|
|
|
|
2019-01-04 14:05:46 +01:00
|
|
|
use ethcore::{client::ClientIoMessage, error::Error as CoreError, spec::Spec};
|
|
|
|
use ethcore_blockchain::BlockChainDB;
|
2018-03-27 13:56:59 +02:00
|
|
|
use ethcore_db as db;
|
2016-12-15 18:45:11 +01:00
|
|
|
use io::{IoContext, IoError, IoHandler, IoService};
|
|
|
|
|
2017-04-05 13:50:11 +02:00
|
|
|
use cache::Cache;
|
2017-09-02 20:09:13 +02:00
|
|
|
use parking_lot::Mutex;
|
2017-04-05 13:50:11 +02:00
|
|
|
|
2018-05-01 14:16:03 +02:00
|
|
|
use super::{ChainDataFetcher, Client, Config as ClientConfig, LightChainNotify};
|
2016-12-15 18:45:11 +01:00
|
|
|
|
2017-03-21 20:57:13 +01:00
|
|
|
/// Errors on service initialization.
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Error {
|
2018-03-27 13:56:59 +02:00
|
|
|
/// Core error.
|
|
|
|
Core(CoreError),
|
2017-03-21 20:57:13 +01:00
|
|
|
/// I/O service error.
|
|
|
|
Io(IoError),
|
|
|
|
}
|
|
|
|
|
2018-03-27 13:56:59 +02:00
|
|
|
impl From<CoreError> for Error {
|
|
|
|
#[inline]
|
|
|
|
fn from(err: CoreError) -> Error {
|
|
|
|
Error::Core(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-21 20:57:13 +01:00
|
|
|
impl fmt::Display for Error {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match *self {
|
2018-03-27 13:56:59 +02:00
|
|
|
Error::Core(ref msg) => write!(f, "Core error: {}", msg),
|
2017-03-21 20:57:13 +01:00
|
|
|
Error::Io(ref err) => write!(f, "I/O service error: {}", err),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-15 18:45:11 +01:00
|
|
|
/// Light client service.
|
2017-09-05 17:54:05 +02:00
|
|
|
pub struct Service<T> {
|
|
|
|
client: Arc<Client<T>>,
|
2017-03-23 22:20:00 +01:00
|
|
|
io_service: IoService<ClientIoMessage>,
|
2016-12-15 18:45:11 +01:00
|
|
|
}
|
|
|
|
|
2017-09-05 17:54:05 +02:00
|
|
|
impl<T: ChainDataFetcher> Service<T> {
|
2016-12-15 18:45:11 +01:00
|
|
|
/// Start the service: initialize I/O workers and client itself.
|
2018-06-20 15:13:07 +02:00
|
|
|
pub fn start(
|
|
|
|
config: ClientConfig,
|
|
|
|
spec: &Spec,
|
|
|
|
fetcher: T,
|
2020-07-29 10:36:15 +02:00
|
|
|
db: Arc<dyn BlockChainDB>,
|
2018-06-20 15:13:07 +02:00
|
|
|
cache: Arc<Mutex<Cache>>,
|
|
|
|
) -> Result<Self, Error> {
|
2017-03-21 20:57:13 +01:00
|
|
|
let io_service = IoService::<ClientIoMessage>::start().map_err(Error::Io)?;
|
|
|
|
let client = Arc::new(Client::new(
|
|
|
|
config,
|
2018-06-20 15:13:07 +02:00
|
|
|
db.key_value().clone(),
|
2017-03-21 20:57:13 +01:00
|
|
|
db::COL_LIGHT_CHAIN,
|
|
|
|
spec,
|
2017-09-05 17:54:05 +02:00
|
|
|
fetcher,
|
2017-03-21 20:57:13 +01:00
|
|
|
io_service.channel(),
|
2017-04-05 13:50:11 +02:00
|
|
|
cache,
|
2018-03-27 13:56:59 +02:00
|
|
|
)?);
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2017-03-21 20:57:13 +01:00
|
|
|
io_service
|
|
|
|
.register_handler(Arc::new(ImportBlocks(client.clone())))
|
|
|
|
.map_err(Error::Io)?;
|
2017-09-05 17:54:05 +02:00
|
|
|
spec.engine.register_client(Arc::downgrade(&client) as _);
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-12-15 18:45:11 +01:00
|
|
|
Ok(Service { client, io_service })
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-05-01 14:16:03 +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 LightChainNotify>) {
|
2018-05-01 14:16:03 +02:00
|
|
|
self.client.add_listener(Arc::downgrade(¬ify));
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2017-03-23 22:20:00 +01:00
|
|
|
/// Register an I/O handler on the service.
|
|
|
|
pub fn register_handler(
|
|
|
|
&self,
|
2020-07-29 10:36:15 +02:00
|
|
|
handler: Arc<dyn IoHandler<ClientIoMessage> + Send>,
|
2017-03-23 22:20:00 +01:00
|
|
|
) -> Result<(), IoError> {
|
|
|
|
self.io_service.register_handler(handler)
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-12-15 18:45:11 +01:00
|
|
|
/// Get a handle to the client.
|
2017-09-05 17:54:05 +02:00
|
|
|
pub fn client(&self) -> &Arc<Client<T>> {
|
2016-12-15 18:45:11 +01:00
|
|
|
&self.client
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-05 17:54:05 +02:00
|
|
|
struct ImportBlocks<T>(Arc<Client<T>>);
|
2016-12-15 18:45:11 +01:00
|
|
|
|
2017-09-05 17:54:05 +02:00
|
|
|
impl<T: ChainDataFetcher> IoHandler<ClientIoMessage> for ImportBlocks<T> {
|
2016-12-15 18:45:11 +01:00
|
|
|
fn message(&self, _io: &IoContext<ClientIoMessage>, message: &ClientIoMessage) {
|
|
|
|
if let ClientIoMessage::BlockVerified = *message {
|
|
|
|
self.0.import_verified();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::Service;
|
|
|
|
use ethcore::spec::Spec;
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2017-04-07 17:37:47 +02:00
|
|
|
use cache::Cache;
|
2017-09-05 17:54:05 +02:00
|
|
|
use client::fetch;
|
2018-03-14 12:29:52 +01:00
|
|
|
use ethcore::test_helpers;
|
2017-09-02 20:09:13 +02:00
|
|
|
use parking_lot::Mutex;
|
2018-06-20 15:13:07 +02:00
|
|
|
use std::{sync::Arc, time::Duration};
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-12-15 18:45:11 +01:00
|
|
|
#[test]
|
|
|
|
fn it_works() {
|
2018-06-20 15:13:07 +02:00
|
|
|
let db = test_helpers::new_db();
|
2016-12-15 18:45:11 +01:00
|
|
|
let spec = Spec::new_test();
|
2018-03-14 12:29:52 +01:00
|
|
|
let cache = Arc::new(Mutex::new(Cache::new(
|
|
|
|
Default::default(),
|
|
|
|
Duration::from_secs(6 * 3600),
|
|
|
|
)));
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-03-01 19:53:15 +01:00
|
|
|
Service::start(Default::default(), &spec, fetch::unavailable(), db, cache).unwrap();
|
2016-12-15 18:45:11 +01:00
|
|
|
}
|
|
|
|
}
|