Client service

This commit is contained in:
arkpar 2016-01-13 23:15:44 +01:00
parent 0de2a031d1
commit e297e598ce
6 changed files with 33 additions and 17 deletions

View File

@ -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();

View File

@ -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<Client, Error> {
pub fn new(spec: Spec, path: &Path, message_channel: IoChannel<NetSyncMessage> ) -> Result<Client, Error> {
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 {

View File

@ -121,6 +121,18 @@ impl From<DecoderError> for Error {
}
}
impl From<UtilError> for Error {
fn from(err: UtilError) -> Error {
Error::Util(err)
}
}
impl From<IoError> 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 {

View File

@ -111,6 +111,7 @@ pub mod views;
pub mod blockchain;
pub mod extras;
pub mod evm;
pub mod service;
#[cfg(test)]
mod tests;

View File

@ -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<RwLock<BlockChain>>,
engine: Arc<Box<Engine>>,
message_channel: IoChannel<NetSyncMessage>
}
impl BlockQueue {
/// Creates a new queue instance.
pub fn new(bc: Arc<RwLock<BlockChain>>, engine: Arc<Box<Engine>>) -> BlockQueue {
pub fn new(bc: Arc<RwLock<BlockChain>>, engine: Arc<Box<Engine>>, message_channel: IoChannel<NetSyncMessage>) -> 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(())
}
}

View File

@ -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<SyncMessage>;
/// Ethereum network protocol handler
pub struct EthSync {