minimal header import and client service
This commit is contained in:
parent
f776f48023
commit
5346539ef8
@ -35,13 +35,18 @@ use request;
|
||||
|
||||
use self::header_chain::HeaderChain;
|
||||
|
||||
pub use self::service::Service;
|
||||
|
||||
pub mod cht;
|
||||
|
||||
mod header_chain;
|
||||
mod service;
|
||||
|
||||
/// Configuration for the light client.
|
||||
#[derive(Debug, Default, Clone)]
|
||||
pub struct Config {
|
||||
queue: queue::Config,
|
||||
/// Verification queue config.
|
||||
pub queue: queue::Config,
|
||||
}
|
||||
|
||||
/// Trait for interacting with the header chain abstractly.
|
||||
@ -139,6 +144,28 @@ impl Client {
|
||||
pub fn cht_root(&self, i: usize) -> Option<H256> {
|
||||
self.chain.cht_root(i)
|
||||
}
|
||||
|
||||
/// Import a set of pre-verified headers from the queue.
|
||||
pub fn import_verified(&self) {
|
||||
const MAX: usize = 256;
|
||||
|
||||
let mut bad = Vec::new();
|
||||
let mut good = Vec::new();
|
||||
for verified_header in self.queue.drain(MAX) {
|
||||
let hash = verified_header.hash();
|
||||
|
||||
match self.chain.insert(::rlp::encode(&verified_header).to_vec()) {
|
||||
Ok(()) => good.push(hash),
|
||||
Err(e) => {
|
||||
debug!(target: "client", "Error importing header {}: {}", hash, e);
|
||||
bad.push(hash);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.queue.mark_as_bad(&bad);
|
||||
self.queue.mark_as_good(&good);
|
||||
}
|
||||
}
|
||||
|
||||
impl LightChainClient for Client {
|
||||
|
73
ethcore/light/src/client/service.rs
Normal file
73
ethcore/light/src/client/service.rs
Normal file
@ -0,0 +1,73 @@
|
||||
// Copyright 2015, 2016 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Parity.
|
||||
|
||||
// Parity is free software: you can redistribute it and/or modify
|
||||
// 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.
|
||||
|
||||
// Parity is distributed in the hope that it will be useful,
|
||||
// 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
|
||||
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Minimal IO service for light client.
|
||||
//! Just handles block import messages and passes them to the client.
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use ethcore::service::ClientIoMessage;
|
||||
use ethcore::spec::Spec;
|
||||
use io::{IoContext, IoError, IoHandler, IoService};
|
||||
|
||||
use super::{Client, Config as ClientConfig};
|
||||
|
||||
/// Light client service.
|
||||
pub struct Service {
|
||||
client: Arc<Client>,
|
||||
_io_service: IoService<ClientIoMessage>,
|
||||
}
|
||||
|
||||
impl Service {
|
||||
/// Start the service: initialize I/O workers and client itself.
|
||||
pub fn start(config: ClientConfig, spec: &Spec) -> Result<Self, IoError> {
|
||||
let io_service = try!(IoService::<ClientIoMessage>::start());
|
||||
let client = Arc::new(Client::new(config, spec, io_service.channel()));
|
||||
try!(io_service.register_handler(Arc::new(ImportBlocks(client.clone()))));
|
||||
|
||||
Ok(Service {
|
||||
client: client,
|
||||
_io_service: io_service,
|
||||
})
|
||||
}
|
||||
|
||||
/// Get a handle to the client.
|
||||
pub fn client(&self) -> &Arc<Client> {
|
||||
&self.client
|
||||
}
|
||||
}
|
||||
|
||||
struct ImportBlocks(Arc<Client>);
|
||||
|
||||
impl IoHandler<ClientIoMessage> for ImportBlocks {
|
||||
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;
|
||||
|
||||
#[test]
|
||||
fn it_works() {
|
||||
let spec = Spec::new_test();
|
||||
Service::start(Default::default(), &spec).unwrap();
|
||||
}
|
||||
}
|
@ -225,6 +225,7 @@ impl Fetcher {
|
||||
}
|
||||
|
||||
self.complete_requests.insert(subchain_parent, request);
|
||||
self.collect_ready();
|
||||
}
|
||||
|
||||
// state transition not triggered until drain is finished.
|
||||
@ -267,8 +268,6 @@ impl Fetcher {
|
||||
}
|
||||
|
||||
fn drain(mut self, headers: &mut Vec<Header>, max: usize) -> SyncRound {
|
||||
self.collect_ready();
|
||||
|
||||
let max = ::std::cmp::min(max, self.ready.len());
|
||||
headers.extend(self.ready.drain(0..max));
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user