Reformat the source code
This commit is contained in:
@@ -18,68 +18,70 @@
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use common_types::encoded;
|
||||
use common_types::header::Header;
|
||||
use common_types::receipt::Receipt;
|
||||
use ethcore::engines::{EthEngine, StateDependentProof};
|
||||
use ethcore::machine::EthereumMachine;
|
||||
use common_types::{encoded, header::Header, receipt::Receipt};
|
||||
use ethcore::{
|
||||
engines::{EthEngine, StateDependentProof},
|
||||
machine::EthereumMachine,
|
||||
};
|
||||
use ethereum_types::H256;
|
||||
use futures::future::IntoFuture;
|
||||
|
||||
/// Provides full chain data.
|
||||
pub trait ChainDataFetcher: Send + Sync + 'static {
|
||||
/// Error type when data unavailable.
|
||||
type Error: ::std::fmt::Debug;
|
||||
/// Error type when data unavailable.
|
||||
type Error: ::std::fmt::Debug;
|
||||
|
||||
/// Future for fetching block body.
|
||||
type Body: IntoFuture<Item=encoded::Block, Error=Self::Error>;
|
||||
/// Future for fetching block receipts.
|
||||
type Receipts: IntoFuture<Item=Vec<Receipt>, Error=Self::Error>;
|
||||
/// Future for fetching epoch transition
|
||||
type Transition: IntoFuture<Item=Vec<u8>, Error=Self::Error>;
|
||||
/// Future for fetching block body.
|
||||
type Body: IntoFuture<Item = encoded::Block, Error = Self::Error>;
|
||||
/// Future for fetching block receipts.
|
||||
type Receipts: IntoFuture<Item = Vec<Receipt>, Error = Self::Error>;
|
||||
/// Future for fetching epoch transition
|
||||
type Transition: IntoFuture<Item = Vec<u8>, Error = Self::Error>;
|
||||
|
||||
/// Fetch a block body.
|
||||
fn block_body(&self, header: &Header) -> Self::Body;
|
||||
/// Fetch a block body.
|
||||
fn block_body(&self, header: &Header) -> Self::Body;
|
||||
|
||||
/// Fetch block receipts.
|
||||
fn block_receipts(&self, header: &Header) -> Self::Receipts;
|
||||
/// Fetch block receipts.
|
||||
fn block_receipts(&self, header: &Header) -> Self::Receipts;
|
||||
|
||||
/// Fetch epoch transition proof at given header.
|
||||
fn epoch_transition(
|
||||
&self,
|
||||
_hash: H256,
|
||||
_engine: Arc<EthEngine>,
|
||||
_checker: Arc<StateDependentProof<EthereumMachine>>
|
||||
) -> Self::Transition;
|
||||
/// Fetch epoch transition proof at given header.
|
||||
fn epoch_transition(
|
||||
&self,
|
||||
_hash: H256,
|
||||
_engine: Arc<EthEngine>,
|
||||
_checker: Arc<StateDependentProof<EthereumMachine>>,
|
||||
) -> Self::Transition;
|
||||
}
|
||||
|
||||
/// Fetcher implementation which cannot fetch anything.
|
||||
pub struct Unavailable;
|
||||
|
||||
/// Create a fetcher which has all data unavailable.
|
||||
pub fn unavailable() -> Unavailable { Unavailable }
|
||||
pub fn unavailable() -> Unavailable {
|
||||
Unavailable
|
||||
}
|
||||
|
||||
impl ChainDataFetcher for Unavailable {
|
||||
type Error = &'static str;
|
||||
type Error = &'static str;
|
||||
|
||||
type Body = Result<encoded::Block, &'static str>;
|
||||
type Receipts = Result<Vec<Receipt>, &'static str>;
|
||||
type Transition = Result<Vec<u8>, &'static str>;
|
||||
type Body = Result<encoded::Block, &'static str>;
|
||||
type Receipts = Result<Vec<Receipt>, &'static str>;
|
||||
type Transition = Result<Vec<u8>, &'static str>;
|
||||
|
||||
fn block_body(&self, _header: &Header) -> Self::Body {
|
||||
Err("fetching block bodies unavailable")
|
||||
}
|
||||
fn block_body(&self, _header: &Header) -> Self::Body {
|
||||
Err("fetching block bodies unavailable")
|
||||
}
|
||||
|
||||
fn block_receipts(&self, _header: &Header) -> Self::Receipts {
|
||||
Err("fetching block receipts unavailable")
|
||||
}
|
||||
fn block_receipts(&self, _header: &Header) -> Self::Receipts {
|
||||
Err("fetching block receipts unavailable")
|
||||
}
|
||||
|
||||
fn epoch_transition(
|
||||
&self,
|
||||
_hash: H256,
|
||||
_engine: Arc<EthEngine>,
|
||||
_checker: Arc<StateDependentProof<EthereumMachine>>
|
||||
) -> Self::Transition {
|
||||
Err("fetching epoch transition proofs unavailable")
|
||||
}
|
||||
fn epoch_transition(
|
||||
&self,
|
||||
_hash: H256,
|
||||
_engine: Arc<EthEngine>,
|
||||
_checker: Arc<StateDependentProof<EthereumMachine>>,
|
||||
) -> Self::Transition {
|
||||
Err("fetching epoch transition proofs unavailable")
|
||||
}
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -17,118 +17,126 @@
|
||||
//! Minimal IO service for light client.
|
||||
//! Just handles block import messages and passes them to the client.
|
||||
|
||||
use std::fmt;
|
||||
use std::sync::Arc;
|
||||
use std::{fmt, sync::Arc};
|
||||
|
||||
use ethcore_db as db;
|
||||
use ethcore::{client::ClientIoMessage, error::Error as CoreError, spec::Spec};
|
||||
use ethcore_blockchain::BlockChainDB;
|
||||
use ethcore::client::ClientIoMessage;
|
||||
use ethcore::error::Error as CoreError;
|
||||
use ethcore::spec::Spec;
|
||||
use ethcore_db as db;
|
||||
use io::{IoContext, IoError, IoHandler, IoService};
|
||||
|
||||
use cache::Cache;
|
||||
use parking_lot::Mutex;
|
||||
|
||||
use super::{ChainDataFetcher, LightChainNotify, Client, Config as ClientConfig};
|
||||
use super::{ChainDataFetcher, Client, Config as ClientConfig, LightChainNotify};
|
||||
|
||||
/// Errors on service initialization.
|
||||
#[derive(Debug)]
|
||||
pub enum Error {
|
||||
/// Core error.
|
||||
Core(CoreError),
|
||||
/// I/O service error.
|
||||
Io(IoError),
|
||||
/// Core error.
|
||||
Core(CoreError),
|
||||
/// I/O service error.
|
||||
Io(IoError),
|
||||
}
|
||||
|
||||
impl From<CoreError> for Error {
|
||||
#[inline]
|
||||
fn from(err: CoreError) -> Error {
|
||||
Error::Core(err)
|
||||
}
|
||||
#[inline]
|
||||
fn from(err: CoreError) -> Error {
|
||||
Error::Core(err)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Error {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match *self {
|
||||
Error::Core(ref msg) => write!(f, "Core error: {}", msg),
|
||||
Error::Io(ref err) => write!(f, "I/O service error: {}", err),
|
||||
}
|
||||
}
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match *self {
|
||||
Error::Core(ref msg) => write!(f, "Core error: {}", msg),
|
||||
Error::Io(ref err) => write!(f, "I/O service error: {}", err),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Light client service.
|
||||
pub struct Service<T> {
|
||||
client: Arc<Client<T>>,
|
||||
io_service: IoService<ClientIoMessage>,
|
||||
client: Arc<Client<T>>,
|
||||
io_service: IoService<ClientIoMessage>,
|
||||
}
|
||||
|
||||
impl<T: ChainDataFetcher> Service<T> {
|
||||
/// Start the service: initialize I/O workers and client itself.
|
||||
pub fn start(config: ClientConfig, spec: &Spec, fetcher: T, db: Arc<BlockChainDB>, cache: Arc<Mutex<Cache>>) -> Result<Self, Error> {
|
||||
let io_service = IoService::<ClientIoMessage>::start().map_err(Error::Io)?;
|
||||
let client = Arc::new(Client::new(config,
|
||||
db.key_value().clone(),
|
||||
db::COL_LIGHT_CHAIN,
|
||||
spec,
|
||||
fetcher,
|
||||
io_service.channel(),
|
||||
cache,
|
||||
)?);
|
||||
/// Start the service: initialize I/O workers and client itself.
|
||||
pub fn start(
|
||||
config: ClientConfig,
|
||||
spec: &Spec,
|
||||
fetcher: T,
|
||||
db: Arc<BlockChainDB>,
|
||||
cache: Arc<Mutex<Cache>>,
|
||||
) -> Result<Self, Error> {
|
||||
let io_service = IoService::<ClientIoMessage>::start().map_err(Error::Io)?;
|
||||
let client = Arc::new(Client::new(
|
||||
config,
|
||||
db.key_value().clone(),
|
||||
db::COL_LIGHT_CHAIN,
|
||||
spec,
|
||||
fetcher,
|
||||
io_service.channel(),
|
||||
cache,
|
||||
)?);
|
||||
|
||||
io_service.register_handler(Arc::new(ImportBlocks(client.clone()))).map_err(Error::Io)?;
|
||||
spec.engine.register_client(Arc::downgrade(&client) as _);
|
||||
io_service
|
||||
.register_handler(Arc::new(ImportBlocks(client.clone())))
|
||||
.map_err(Error::Io)?;
|
||||
spec.engine.register_client(Arc::downgrade(&client) as _);
|
||||
|
||||
Ok(Service {
|
||||
client,
|
||||
io_service,
|
||||
})
|
||||
}
|
||||
Ok(Service { client, io_service })
|
||||
}
|
||||
|
||||
/// Set the actor to be notified on certain chain events
|
||||
pub fn add_notify(&self, notify: Arc<LightChainNotify>) {
|
||||
self.client.add_listener(Arc::downgrade(¬ify));
|
||||
}
|
||||
/// Set the actor to be notified on certain chain events
|
||||
pub fn add_notify(&self, notify: Arc<LightChainNotify>) {
|
||||
self.client.add_listener(Arc::downgrade(¬ify));
|
||||
}
|
||||
|
||||
/// Register an I/O handler on the service.
|
||||
pub fn register_handler(&self, handler: Arc<IoHandler<ClientIoMessage> + Send>) -> Result<(), IoError> {
|
||||
self.io_service.register_handler(handler)
|
||||
}
|
||||
/// Register an I/O handler on the service.
|
||||
pub fn register_handler(
|
||||
&self,
|
||||
handler: Arc<IoHandler<ClientIoMessage> + Send>,
|
||||
) -> Result<(), IoError> {
|
||||
self.io_service.register_handler(handler)
|
||||
}
|
||||
|
||||
/// Get a handle to the client.
|
||||
pub fn client(&self) -> &Arc<Client<T>> {
|
||||
&self.client
|
||||
}
|
||||
/// Get a handle to the client.
|
||||
pub fn client(&self) -> &Arc<Client<T>> {
|
||||
&self.client
|
||||
}
|
||||
}
|
||||
|
||||
struct ImportBlocks<T>(Arc<Client<T>>);
|
||||
|
||||
impl<T: ChainDataFetcher> IoHandler<ClientIoMessage> for ImportBlocks<T> {
|
||||
fn message(&self, _io: &IoContext<ClientIoMessage>, message: &ClientIoMessage) {
|
||||
if let ClientIoMessage::BlockVerified = *message {
|
||||
self.0.import_verified();
|
||||
}
|
||||
}
|
||||
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;
|
||||
use super::Service;
|
||||
use ethcore::spec::Spec;
|
||||
|
||||
use std::sync::Arc;
|
||||
use cache::Cache;
|
||||
use client::fetch;
|
||||
use std::time::Duration;
|
||||
use parking_lot::Mutex;
|
||||
use ethcore::test_helpers;
|
||||
use cache::Cache;
|
||||
use client::fetch;
|
||||
use ethcore::test_helpers;
|
||||
use parking_lot::Mutex;
|
||||
use std::{sync::Arc, time::Duration};
|
||||
|
||||
#[test]
|
||||
fn it_works() {
|
||||
let db = test_helpers::new_db();
|
||||
let spec = Spec::new_test();
|
||||
let cache = Arc::new(Mutex::new(Cache::new(Default::default(), Duration::from_secs(6 * 3600))));
|
||||
#[test]
|
||||
fn it_works() {
|
||||
let db = test_helpers::new_db();
|
||||
let spec = Spec::new_test();
|
||||
let cache = Arc::new(Mutex::new(Cache::new(
|
||||
Default::default(),
|
||||
Duration::from_secs(6 * 3600),
|
||||
)));
|
||||
|
||||
Service::start(Default::default(), &spec, fetch::unavailable(), db, cache).unwrap();
|
||||
}
|
||||
Service::start(Default::default(), &spec, fetch::unavailable(), db, cache).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user