2016-12-11 19:31:31 +01:00
|
|
|
// Copyright 2015, 2016 Parity Technologies (UK) Ltd.
|
2016-09-19 11:29:50 +02:00
|
|
|
// 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/>.
|
|
|
|
|
2016-12-11 15:40:31 +01:00
|
|
|
//! Light client implementation. Stores data from light sync
|
2016-09-19 11:29:50 +02:00
|
|
|
|
2016-12-05 17:09:05 +01:00
|
|
|
use ethcore::block_import_error::BlockImportError;
|
|
|
|
use ethcore::block_status::BlockStatus;
|
2016-12-13 21:09:57 +01:00
|
|
|
use ethcore::ids::BlockId;
|
2016-12-14 23:26:15 +01:00
|
|
|
use ethcore::header::Header;
|
2016-12-13 20:13:55 +01:00
|
|
|
use ethcore::verification::queue::{self, HeaderQueue};
|
2016-12-19 13:15:37 +01:00
|
|
|
use ethcore::transaction::PendingTransaction;
|
2016-12-05 17:09:05 +01:00
|
|
|
use ethcore::blockchain_info::BlockChainInfo;
|
2016-12-13 14:48:03 +01:00
|
|
|
use ethcore::spec::Spec;
|
|
|
|
use ethcore::service::ClientIoMessage;
|
2016-09-19 11:29:50 +02:00
|
|
|
use io::IoChannel;
|
2016-12-13 14:48:03 +01:00
|
|
|
|
2016-12-11 15:40:31 +01:00
|
|
|
use util::hash::{H256, H256FastMap};
|
2016-11-04 17:19:01 +01:00
|
|
|
use util::{Bytes, Mutex};
|
2016-09-19 11:29:50 +02:00
|
|
|
|
2016-12-05 17:09:05 +01:00
|
|
|
use provider::Provider;
|
|
|
|
use request;
|
2016-11-04 19:40:11 +01:00
|
|
|
|
2016-12-13 14:48:03 +01:00
|
|
|
use self::header_chain::HeaderChain;
|
|
|
|
|
2016-12-15 18:45:11 +01:00
|
|
|
pub use self::service::Service;
|
|
|
|
|
2016-12-13 21:09:57 +01:00
|
|
|
pub mod cht;
|
2016-12-15 18:45:11 +01:00
|
|
|
|
2016-12-12 22:59:05 +01:00
|
|
|
mod header_chain;
|
2016-12-15 18:45:11 +01:00
|
|
|
mod service;
|
2016-12-12 22:59:05 +01:00
|
|
|
|
2016-12-13 14:48:03 +01:00
|
|
|
/// Configuration for the light client.
|
|
|
|
#[derive(Debug, Default, Clone)]
|
|
|
|
pub struct Config {
|
2016-12-15 18:45:11 +01:00
|
|
|
/// Verification queue config.
|
|
|
|
pub queue: queue::Config,
|
2016-12-13 14:48:03 +01:00
|
|
|
}
|
|
|
|
|
2016-12-14 23:26:15 +01:00
|
|
|
/// Trait for interacting with the header chain abstractly.
|
|
|
|
pub trait LightChainClient: Send + Sync {
|
|
|
|
/// Get chain info.
|
|
|
|
fn chain_info(&self) -> BlockChainInfo;
|
|
|
|
|
|
|
|
/// Queue header to be verified. Required that all headers queued have their
|
|
|
|
/// parent queued prior.
|
|
|
|
fn queue_header(&self, header: Header) -> Result<H256, BlockImportError>;
|
|
|
|
|
|
|
|
/// Query whether a block is known.
|
|
|
|
fn is_known(&self, hash: &H256) -> bool;
|
|
|
|
|
|
|
|
/// Clear the queue.
|
|
|
|
fn clear_queue(&self);
|
|
|
|
|
|
|
|
/// Get queue info.
|
|
|
|
fn queue_info(&self) -> queue::QueueInfo;
|
|
|
|
|
|
|
|
/// Get the `i`th CHT root.
|
|
|
|
fn cht_root(&self, i: usize) -> Option<H256>;
|
|
|
|
}
|
|
|
|
|
2016-10-27 15:09:17 +02:00
|
|
|
/// Light client implementation.
|
2016-09-19 11:29:50 +02:00
|
|
|
pub struct Client {
|
2016-12-13 14:48:03 +01:00
|
|
|
queue: HeaderQueue,
|
|
|
|
chain: HeaderChain,
|
2016-12-19 13:15:37 +01:00
|
|
|
tx_pool: Mutex<H256FastMap<PendingTransaction>>,
|
2016-12-16 23:53:20 +01:00
|
|
|
import_lock: Mutex<()>,
|
2016-09-19 11:29:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Client {
|
2016-12-13 14:48:03 +01:00
|
|
|
/// Create a new `Client`.
|
|
|
|
pub fn new(config: Config, spec: &Spec, io_channel: IoChannel<ClientIoMessage>) -> Self {
|
|
|
|
Client {
|
|
|
|
queue: HeaderQueue::new(config.queue, spec.engine.clone(), io_channel, true),
|
|
|
|
chain: HeaderChain::new(&::rlp::encode(&spec.genesis_header())),
|
|
|
|
tx_pool: Mutex::new(Default::default()),
|
2016-12-16 23:53:20 +01:00
|
|
|
import_lock: Mutex::new(()),
|
2016-12-13 14:48:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-14 23:26:15 +01:00
|
|
|
/// Import a header to the queue for additional verification.
|
|
|
|
pub fn import_header(&self, header: Header) -> Result<H256, BlockImportError> {
|
2016-12-13 14:48:03 +01:00
|
|
|
self.queue.import(header).map_err(Into::into)
|
2016-09-19 11:29:50 +02:00
|
|
|
}
|
|
|
|
|
2016-12-11 15:40:31 +01:00
|
|
|
/// Import a local transaction.
|
2016-12-19 13:15:37 +01:00
|
|
|
pub fn import_own_transaction(&self, tx: PendingTransaction) {
|
|
|
|
self.tx_pool.lock().insert(tx.transaction.hash(), tx);
|
2016-12-12 22:59:05 +01:00
|
|
|
}
|
2016-12-11 15:40:31 +01:00
|
|
|
|
2016-09-19 11:29:50 +02:00
|
|
|
/// Fetch a vector of all pending transactions.
|
2016-12-19 13:15:37 +01:00
|
|
|
pub fn ready_transactions(&self) -> Vec<PendingTransaction> {
|
|
|
|
let best_num = self.chain.best_block().number;
|
|
|
|
self.tx_pool.lock()
|
|
|
|
.values()
|
|
|
|
.filter(|t| t.min_block.as_ref().map_or(true, |x| x <= &best_num))
|
|
|
|
.cloned()
|
|
|
|
.collect()
|
2016-09-19 11:29:50 +02:00
|
|
|
}
|
|
|
|
|
2016-12-13 20:13:55 +01:00
|
|
|
/// Inquire about the status of a given header.
|
|
|
|
pub fn status(&self, hash: &H256) -> BlockStatus {
|
|
|
|
match self.queue.status(hash) {
|
|
|
|
queue::Status::Unknown => self.chain.status(hash),
|
|
|
|
other => other.into(),
|
|
|
|
}
|
2016-09-19 11:29:50 +02:00
|
|
|
}
|
|
|
|
|
2016-12-13 21:09:57 +01:00
|
|
|
/// Get the chain info.
|
|
|
|
pub fn chain_info(&self) -> BlockChainInfo {
|
2016-12-13 14:48:03 +01:00
|
|
|
let best_block = self.chain.best_block();
|
|
|
|
let first_block = self.chain.first_block();
|
|
|
|
let genesis_hash = self.chain.genesis_hash();
|
|
|
|
|
|
|
|
BlockChainInfo {
|
|
|
|
total_difficulty: best_block.total_difficulty,
|
|
|
|
pending_total_difficulty: best_block.total_difficulty,
|
|
|
|
genesis_hash: genesis_hash,
|
|
|
|
best_block_hash: best_block.hash,
|
|
|
|
best_block_number: best_block.number,
|
|
|
|
ancient_block_hash: if first_block.is_some() { Some(genesis_hash) } else { None },
|
|
|
|
ancient_block_number: if first_block.is_some() { Some(0) } else { None },
|
|
|
|
first_block_hash: first_block.as_ref().map(|first| first.hash),
|
|
|
|
first_block_number: first_block.as_ref().map(|first| first.number),
|
|
|
|
}
|
2016-10-27 15:45:59 +02:00
|
|
|
}
|
2016-10-27 15:09:17 +02:00
|
|
|
|
2016-12-13 21:09:57 +01:00
|
|
|
/// Get the header queue info.
|
|
|
|
pub fn queue_info(&self) -> queue::QueueInfo {
|
|
|
|
self.queue.queue_info()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get a block header by Id.
|
|
|
|
pub fn get_header(&self, id: BlockId) -> Option<Bytes> {
|
|
|
|
self.chain.get_header(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the `i`th CHT root.
|
|
|
|
pub fn cht_root(&self, i: usize) -> Option<H256> {
|
|
|
|
self.chain.cht_root(i)
|
|
|
|
}
|
2016-12-15 18:45:11 +01:00
|
|
|
|
|
|
|
/// Import a set of pre-verified headers from the queue.
|
|
|
|
pub fn import_verified(&self) {
|
|
|
|
const MAX: usize = 256;
|
|
|
|
|
2016-12-16 23:53:20 +01:00
|
|
|
let _lock = self.import_lock.lock();
|
|
|
|
|
2016-12-15 18:45:11 +01:00
|
|
|
let mut bad = Vec::new();
|
|
|
|
let mut good = Vec::new();
|
|
|
|
for verified_header in self.queue.drain(MAX) {
|
2016-12-16 23:53:20 +01:00
|
|
|
let (num, hash) = (verified_header.number(), verified_header.hash());
|
2016-12-15 18:45:11 +01:00
|
|
|
|
|
|
|
match self.chain.insert(::rlp::encode(&verified_header).to_vec()) {
|
|
|
|
Ok(()) => good.push(hash),
|
|
|
|
Err(e) => {
|
2016-12-16 23:53:20 +01:00
|
|
|
debug!(target: "client", "Error importing header {:?}: {}", (num, hash), e);
|
2016-12-15 18:45:11 +01:00
|
|
|
bad.push(hash);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.queue.mark_as_bad(&bad);
|
|
|
|
self.queue.mark_as_good(&good);
|
|
|
|
}
|
2016-12-13 21:09:57 +01:00
|
|
|
}
|
|
|
|
|
2016-12-14 23:26:15 +01:00
|
|
|
impl LightChainClient for Client {
|
|
|
|
fn chain_info(&self) -> BlockChainInfo { Client::chain_info(self) }
|
|
|
|
|
|
|
|
fn queue_header(&self, header: Header) -> Result<H256, BlockImportError> {
|
|
|
|
self.import_header(header)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_known(&self, hash: &H256) -> bool {
|
|
|
|
self.status(hash) == BlockStatus::InChain
|
|
|
|
}
|
|
|
|
|
|
|
|
fn clear_queue(&self) {
|
|
|
|
self.queue.clear()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn queue_info(&self) -> queue::QueueInfo {
|
|
|
|
self.queue.queue_info()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn cht_root(&self, i: usize) -> Option<H256> {
|
|
|
|
Client::cht_root(self, i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-13 21:09:57 +01:00
|
|
|
// dummy implementation -- may draw from canonical cache further on.
|
|
|
|
impl Provider for Client {
|
|
|
|
fn chain_info(&self) -> BlockChainInfo {
|
|
|
|
Client::chain_info(self)
|
|
|
|
}
|
|
|
|
|
2016-11-09 15:36:26 +01:00
|
|
|
fn reorg_depth(&self, _a: &H256, _b: &H256) -> Option<u64> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
fn earliest_state(&self) -> Option<u64> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2016-11-04 19:40:11 +01:00
|
|
|
fn block_headers(&self, _req: request::Headers) -> Vec<Bytes> {
|
|
|
|
Vec::new()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn block_bodies(&self, _req: request::Bodies) -> Vec<Bytes> {
|
2016-10-27 15:09:17 +02:00
|
|
|
Vec::new()
|
|
|
|
}
|
|
|
|
|
2016-11-04 19:40:11 +01:00
|
|
|
fn receipts(&self, _req: request::Receipts) -> Vec<Bytes> {
|
2016-10-27 15:09:17 +02:00
|
|
|
Vec::new()
|
|
|
|
}
|
|
|
|
|
2016-11-04 19:40:11 +01:00
|
|
|
fn proofs(&self, _req: request::StateProofs) -> Vec<Bytes> {
|
2016-10-27 15:09:17 +02:00
|
|
|
Vec::new()
|
|
|
|
}
|
|
|
|
|
2016-11-15 14:53:30 +01:00
|
|
|
fn contract_code(&self, _req: request::ContractCodes) -> Vec<Bytes> {
|
2016-10-27 15:09:17 +02:00
|
|
|
Vec::new()
|
|
|
|
}
|
|
|
|
|
2016-11-04 19:40:11 +01:00
|
|
|
fn header_proofs(&self, _req: request::HeaderProofs) -> Vec<Bytes> {
|
2016-10-27 15:09:17 +02:00
|
|
|
Vec::new()
|
|
|
|
}
|
|
|
|
|
2016-12-19 13:15:37 +01:00
|
|
|
fn ready_transactions(&self) -> Vec<PendingTransaction> {
|
|
|
|
Client::ready_transactions(self)
|
2016-10-27 15:09:17 +02:00
|
|
|
}
|
2016-12-12 22:59:05 +01:00
|
|
|
}
|