From e297e598ce6d9e7b13cfb529a3087203367c9bd9 Mon Sep 17 00:00:00 2001 From: arkpar Date: Wed, 13 Jan 2016 23:15:44 +0100 Subject: [PATCH] Client service --- src/bin/client.rs | 13 ++----------- src/client.rs | 10 ++++++++-- src/error.rs | 12 ++++++++++++ src/lib.rs | 1 + src/queue.rs | 7 +++++-- src/sync/mod.rs | 7 +++++-- 6 files changed, 33 insertions(+), 17 deletions(-) diff --git a/src/bin/client.rs b/src/bin/client.rs index a644ecd1b..5a3e907be 100644 --- a/src/bin/client.rs +++ b/src/bin/client.rs @@ -4,23 +4,14 @@ extern crate rustc_serialize; extern crate env_logger; use std::io::*; -use std::env; -use std::sync::Arc; use util::hash::*; -use util::network::{NetworkService}; -use ethcore::client::Client; -use ethcore::sync::EthSync; +use ethcore::service::ClientService; use ethcore::ethereum; fn main() { ::env_logger::init().ok(); - let mut service = NetworkService::start().unwrap(); - //TODO: replace with proper genesis and chain params. let spec = ethereum::new_frontier(); - let mut dir = env::temp_dir(); - dir.push(H32::random().hex()); - let client = Arc::new(Client::new(spec, &dir).unwrap()); - EthSync::register(&mut service, client); + let mut _service = ClientService::start(spec).unwrap(); loop { let mut cmd = String::new(); stdin().read_line(&mut cmd).unwrap(); diff --git a/src/client.rs b/src/client.rs index 100ac6bd8..f5a4b43a6 100644 --- a/src/client.rs +++ b/src/client.rs @@ -6,6 +6,7 @@ use header::BlockNumber; use spec::Spec; use engine::Engine; use queue::BlockQueue; +use sync::NetSyncMessage; /// General block status pub enum BlockStatus { @@ -99,15 +100,20 @@ pub struct Client { } impl Client { - pub fn new(spec: Spec, path: &Path) -> Result { + pub fn new(spec: Spec, path: &Path, message_channel: IoChannel ) -> Result { let chain = Arc::new(RwLock::new(BlockChain::new(&spec.genesis_block(), path))); let engine = Arc::new(try!(spec.to_engine())); Ok(Client { chain: chain.clone(), _engine: engine.clone(), - queue: BlockQueue::new(chain.clone(), engine.clone()), + queue: BlockQueue::new(chain.clone(), engine.clone(), message_channel), }) } + + + pub fn import_verified_block(&mut self, bytes: Bytes) { + self.chain.write().unwrap().insert_block(&bytes); + } } impl BlockChainClient for Client { diff --git a/src/error.rs b/src/error.rs index 01618c66c..b78b3dbec 100644 --- a/src/error.rs +++ b/src/error.rs @@ -121,6 +121,18 @@ impl From for Error { } } +impl From for Error { + fn from(err: UtilError) -> Error { + Error::Util(err) + } +} + +impl From for Error { + fn from(err: IoError) -> Error { + Error::Util(From::from(err)) + } +} + // TODO: uncomment below once https://github.com/rust-lang/rust/issues/27336 sorted. /*#![feature(concat_idents)] macro_rules! assimilate { diff --git a/src/lib.rs b/src/lib.rs index 0b81f4fd3..574d442a4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -111,6 +111,7 @@ pub mod views; pub mod blockchain; pub mod extras; pub mod evm; +pub mod service; #[cfg(test)] mod tests; diff --git a/src/queue.rs b/src/queue.rs index 9cef88181..60026e818 100644 --- a/src/queue.rs +++ b/src/queue.rs @@ -4,20 +4,23 @@ use views::{BlockView}; use verification::*; use error::*; use engine::Engine; +use sync::*; /// A queue of blocks. Sits between network or other I/O and the BlockChain. /// Sorts them ready for blockchain insertion. pub struct BlockQueue { bc: Arc>, engine: Arc>, + message_channel: IoChannel } impl BlockQueue { /// Creates a new queue instance. - pub fn new(bc: Arc>, engine: Arc>) -> BlockQueue { + pub fn new(bc: Arc>, engine: Arc>, message_channel: IoChannel) -> BlockQueue { BlockQueue { bc: bc, engine: engine, + message_channel: message_channel } } @@ -34,7 +37,7 @@ impl BlockQueue { try!(verify_block_basic(bytes, self.engine.deref().deref())); try!(verify_block_unordered(bytes, self.engine.deref().deref())); try!(verify_block_final(bytes, self.engine.deref().deref(), self.bc.read().unwrap().deref())); - self.bc.write().unwrap().insert_block(bytes); + try!(self.message_channel.send(UserMessage(SyncMessage::BlockVerified(bytes.to_vec()))).map_err(|e| Error::from(e))); Ok(()) } } diff --git a/src/sync/mod.rs b/src/sync/mod.rs index 50e20c439..e2cf5529d 100644 --- a/src/sync/mod.rs +++ b/src/sync/mod.rs @@ -24,7 +24,7 @@ use std::sync::Arc; use client::BlockChainClient; -use util::network::{NetworkProtocolHandler, NetworkService, NetworkContext, PeerId}; +use util::network::{NetworkProtocolHandler, NetworkService, NetworkContext, PeerId, NetworkIoMessage}; use util::TimerToken; use util::Bytes; use sync::chain::ChainSync; @@ -40,9 +40,12 @@ mod tests; /// Message type for external events pub enum SyncMessage { /// New block has been imported into the blockchain - NewBlock(Bytes) + NewChainBlock(Bytes), + /// A block is ready + BlockVerified(Bytes), } +pub type NetSyncMessage = NetworkIoMessage; /// Ethereum network protocol handler pub struct EthSync {