2017-01-25 18:51:41 +01:00
|
|
|
// Copyright 2015-2017 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
|
|
|
|
2017-05-23 12:26:39 +02:00
|
|
|
use std::sync::{Weak, Arc};
|
2017-02-09 19:58:29 +01:00
|
|
|
|
2016-12-05 17:09:05 +01:00
|
|
|
use ethcore::block_status::BlockStatus;
|
2018-03-13 11:49:57 +01:00
|
|
|
use ethcore::client::{ClientReport, EnvInfo, ClientIoMessage};
|
2017-09-26 14:19:08 +02:00
|
|
|
use ethcore::engines::{epoch, EthEngine, EpochChange, EpochTransition, Proof};
|
|
|
|
use ethcore::machine::EthereumMachine;
|
2018-03-27 13:56:59 +02:00
|
|
|
use ethcore::error::{Error, BlockImportError};
|
2016-12-13 21:09:57 +01:00
|
|
|
use ethcore::ids::BlockId;
|
2017-09-05 17:54:05 +02:00
|
|
|
use ethcore::header::{BlockNumber, Header};
|
2016-12-13 20:13:55 +01:00
|
|
|
use ethcore::verification::queue::{self, HeaderQueue};
|
2016-12-05 17:09:05 +01:00
|
|
|
use ethcore::blockchain_info::BlockChainInfo;
|
2018-03-27 13:56:59 +02:00
|
|
|
use ethcore::spec::{Spec, SpecHardcodedSync};
|
2016-12-28 15:53:11 +01:00
|
|
|
use ethcore::encoded;
|
2016-09-19 11:29:50 +02:00
|
|
|
use io::IoChannel;
|
2017-09-02 20:09:13 +02:00
|
|
|
use parking_lot::{Mutex, RwLock};
|
2018-01-10 13:35:18 +01:00
|
|
|
use ethereum_types::{H256, U256};
|
2017-09-05 17:54:05 +02:00
|
|
|
use futures::{IntoFuture, Future};
|
2017-08-03 18:18:19 +02:00
|
|
|
|
2018-03-27 13:56:59 +02:00
|
|
|
use kvdb::KeyValueDB;
|
2016-09-19 11:29:50 +02:00
|
|
|
|
2017-09-05 17:54:05 +02:00
|
|
|
use self::fetch::ChainDataFetcher;
|
2018-03-27 13:56:59 +02:00
|
|
|
use self::header_chain::{AncestryIter, HeaderChain, HardcodedSync};
|
2016-12-13 14:48:03 +01:00
|
|
|
|
2017-04-05 13:50:11 +02:00
|
|
|
use cache::Cache;
|
|
|
|
|
2016-12-15 18:45:11 +01:00
|
|
|
pub use self::service::Service;
|
|
|
|
|
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
|
|
|
|
2017-09-05 17:54:05 +02:00
|
|
|
pub mod fetch;
|
|
|
|
|
2016-12-13 14:48:03 +01:00
|
|
|
/// Configuration for the light client.
|
2017-07-10 13:21:11 +02:00
|
|
|
#[derive(Debug, Clone)]
|
2016-12-13 14:48:03 +01:00
|
|
|
pub struct Config {
|
2016-12-15 18:45:11 +01:00
|
|
|
/// Verification queue config.
|
|
|
|
pub queue: queue::Config,
|
2017-03-21 20:57:13 +01:00
|
|
|
/// Chain column in database.
|
|
|
|
pub chain_column: Option<u32>,
|
2017-07-10 13:21:11 +02:00
|
|
|
/// Should it do full verification of blocks?
|
|
|
|
pub verify_full: bool,
|
2017-07-27 13:50:12 +02:00
|
|
|
/// Should it check the seal of blocks?
|
|
|
|
pub check_seal: bool,
|
2018-03-27 13:56:59 +02:00
|
|
|
/// Disable hardcoded sync.
|
|
|
|
pub no_hardcoded_sync: bool,
|
2017-07-10 13:21:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Config {
|
|
|
|
fn default() -> Config {
|
|
|
|
Config {
|
|
|
|
queue: Default::default(),
|
|
|
|
chain_column: None,
|
|
|
|
verify_full: true,
|
2017-07-27 13:50:12 +02:00
|
|
|
check_seal: true,
|
2018-03-27 13:56:59 +02:00
|
|
|
no_hardcoded_sync: false,
|
2017-07-10 13:21:11 +02:00
|
|
|
}
|
|
|
|
}
|
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 {
|
2017-09-05 17:54:05 +02:00
|
|
|
/// Adds a new `LightChainNotify` listener.
|
|
|
|
fn add_listener(&self, listener: Weak<LightChainNotify>);
|
|
|
|
|
2016-12-14 23:26:15 +01:00
|
|
|
/// 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>;
|
|
|
|
|
2017-04-12 12:07:54 +02:00
|
|
|
/// Attempt to get a block hash by block id.
|
|
|
|
fn block_hash(&self, id: BlockId) -> Option<H256>;
|
|
|
|
|
2017-02-09 18:42:18 +01:00
|
|
|
/// Attempt to get block header by block id.
|
|
|
|
fn block_header(&self, id: BlockId) -> Option<encoded::Header>;
|
|
|
|
|
2017-02-13 16:49:01 +01:00
|
|
|
/// Get the best block header.
|
|
|
|
fn best_block_header(&self) -> encoded::Header;
|
|
|
|
|
2017-04-12 12:07:54 +02:00
|
|
|
/// Get a block's chain score by ID.
|
|
|
|
fn score(&self, id: BlockId) -> Option<U256>;
|
|
|
|
|
2017-02-17 17:08:46 +01:00
|
|
|
/// Get an iterator over a block and its ancestry.
|
|
|
|
fn ancestry_iter<'a>(&'a self, start: BlockId) -> Box<Iterator<Item=encoded::Header> + 'a>;
|
|
|
|
|
2017-08-21 13:46:58 +02:00
|
|
|
/// Get the signing chain ID.
|
|
|
|
fn signing_chain_id(&self) -> Option<u64>;
|
2017-02-16 16:08:58 +01:00
|
|
|
|
2017-04-03 11:37:07 +02:00
|
|
|
/// Get environment info for execution at a given block.
|
|
|
|
/// Fails if that block's header is not stored.
|
|
|
|
fn env_info(&self, id: BlockId) -> Option<EnvInfo>;
|
|
|
|
|
|
|
|
/// Get a handle to the consensus engine.
|
2017-09-26 14:19:08 +02:00
|
|
|
fn engine(&self) -> &Arc<EthEngine>;
|
2017-04-03 11:37:07 +02:00
|
|
|
|
2016-12-14 23:26:15 +01:00
|
|
|
/// Query whether a block is known.
|
|
|
|
fn is_known(&self, hash: &H256) -> bool;
|
|
|
|
|
|
|
|
/// Clear the queue.
|
|
|
|
fn clear_queue(&self);
|
|
|
|
|
2017-01-11 14:39:03 +01:00
|
|
|
/// Flush the queue.
|
|
|
|
fn flush_queue(&self);
|
|
|
|
|
2016-12-14 23:26:15 +01:00
|
|
|
/// Get queue info.
|
|
|
|
fn queue_info(&self) -> queue::QueueInfo;
|
|
|
|
|
|
|
|
/// Get the `i`th CHT root.
|
|
|
|
fn cht_root(&self, i: usize) -> Option<H256>;
|
2017-04-19 14:30:00 +02:00
|
|
|
|
|
|
|
/// Get the EIP-86 transition block number.
|
2017-09-05 17:54:05 +02:00
|
|
|
fn eip86_transition(&self) -> BlockNumber;
|
2017-07-10 13:21:11 +02:00
|
|
|
|
|
|
|
/// Get a report of import activity since the last call.
|
|
|
|
fn report(&self) -> ClientReport;
|
2016-12-14 23:26:15 +01:00
|
|
|
}
|
|
|
|
|
2017-05-23 12:26:39 +02:00
|
|
|
/// An actor listening to light chain events.
|
|
|
|
pub trait LightChainNotify: Send + Sync {
|
|
|
|
/// Notifies about imported headers.
|
|
|
|
fn new_headers(&self, good: &[H256]);
|
|
|
|
}
|
|
|
|
|
2017-02-09 18:42:18 +01:00
|
|
|
/// Something which can be treated as a `LightChainClient`.
|
|
|
|
pub trait AsLightClient {
|
|
|
|
/// The kind of light client this can be treated as.
|
|
|
|
type Client: LightChainClient;
|
|
|
|
|
|
|
|
/// Access the underlying light client.
|
|
|
|
fn as_light_client(&self) -> &Self::Client;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: LightChainClient> AsLightClient for T {
|
|
|
|
type Client = Self;
|
|
|
|
|
|
|
|
fn as_light_client(&self) -> &Self { self }
|
|
|
|
}
|
|
|
|
|
2016-10-27 15:09:17 +02:00
|
|
|
/// Light client implementation.
|
2017-09-05 17:54:05 +02:00
|
|
|
pub struct Client<T> {
|
2016-12-13 14:48:03 +01:00
|
|
|
queue: HeaderQueue,
|
2017-09-26 14:19:08 +02:00
|
|
|
engine: Arc<EthEngine>,
|
2016-12-13 14:48:03 +01:00
|
|
|
chain: HeaderChain,
|
2016-12-23 14:50:42 +01:00
|
|
|
report: RwLock<ClientReport>,
|
2016-12-16 23:53:20 +01:00
|
|
|
import_lock: Mutex<()>,
|
2017-03-21 20:57:13 +01:00
|
|
|
db: Arc<KeyValueDB>,
|
2017-05-23 12:26:39 +02:00
|
|
|
listeners: RwLock<Vec<Weak<LightChainNotify>>>,
|
2017-09-05 17:54:05 +02:00
|
|
|
fetcher: T,
|
2017-07-10 13:21:11 +02:00
|
|
|
verify_full: bool,
|
2016-09-19 11:29:50 +02:00
|
|
|
}
|
|
|
|
|
2017-09-05 17:54:05 +02:00
|
|
|
impl<T: ChainDataFetcher> Client<T> {
|
2016-12-13 14:48:03 +01:00
|
|
|
/// Create a new `Client`.
|
2017-09-05 17:54:05 +02:00
|
|
|
pub fn new(
|
|
|
|
config: Config,
|
|
|
|
db: Arc<KeyValueDB>,
|
|
|
|
chain_col: Option<u32>,
|
|
|
|
spec: &Spec,
|
|
|
|
fetcher: T,
|
|
|
|
io_channel: IoChannel<ClientIoMessage>,
|
|
|
|
cache: Arc<Mutex<Cache>>
|
2018-03-27 13:56:59 +02:00
|
|
|
) -> Result<Self, Error> {
|
2017-03-21 20:57:13 +01:00
|
|
|
Ok(Client {
|
2017-07-27 13:50:12 +02:00
|
|
|
queue: HeaderQueue::new(config.queue, spec.engine.clone(), io_channel, config.check_seal),
|
2017-02-09 19:58:29 +01:00
|
|
|
engine: spec.engine.clone(),
|
2018-03-27 13:56:59 +02:00
|
|
|
chain: {
|
|
|
|
let hs_cfg = if config.no_hardcoded_sync { HardcodedSync::Deny } else { HardcodedSync::Allow };
|
|
|
|
HeaderChain::new(db.clone(), chain_col, &spec, cache, hs_cfg)?
|
|
|
|
},
|
2016-12-23 14:50:42 +01:00
|
|
|
report: RwLock::new(ClientReport::default()),
|
2016-12-16 23:53:20 +01:00
|
|
|
import_lock: Mutex::new(()),
|
2017-03-21 20:57:13 +01:00
|
|
|
db: db,
|
2017-05-23 12:26:39 +02:00
|
|
|
listeners: RwLock::new(vec![]),
|
2017-09-05 17:54:05 +02:00
|
|
|
fetcher: fetcher,
|
2017-07-10 13:21:11 +02:00
|
|
|
verify_full: config.verify_full,
|
2017-03-21 20:57:13 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-03-27 13:56:59 +02:00
|
|
|
/// Generates the specifications for hardcoded sync. This is typically only called manually
|
|
|
|
/// from time to time by a Parity developer in order to update the chain specifications.
|
|
|
|
///
|
|
|
|
/// Returns `None` if we are at the genesis block.
|
|
|
|
pub fn read_hardcoded_sync(&self) -> Result<Option<SpecHardcodedSync>, Error> {
|
|
|
|
self.chain.read_hardcoded_sync()
|
|
|
|
}
|
|
|
|
|
2017-05-23 12:26:39 +02:00
|
|
|
/// Adds a new `LightChainNotify` listener.
|
|
|
|
pub fn add_listener(&self, listener: Weak<LightChainNotify>) {
|
|
|
|
self.listeners.write().push(listener);
|
|
|
|
}
|
|
|
|
|
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-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 {
|
2017-02-04 17:19:39 +01:00
|
|
|
let best_hdr = self.chain.best_header();
|
|
|
|
let best_td = self.chain.best_block().total_difficulty;
|
|
|
|
|
2016-12-13 14:48:03 +01:00
|
|
|
let first_block = self.chain.first_block();
|
|
|
|
let genesis_hash = self.chain.genesis_hash();
|
|
|
|
|
|
|
|
BlockChainInfo {
|
2017-02-04 17:19:39 +01:00
|
|
|
total_difficulty: best_td,
|
|
|
|
pending_total_difficulty: best_td + self.queue.total_difficulty(),
|
2016-12-13 14:48:03 +01:00
|
|
|
genesis_hash: genesis_hash,
|
2017-02-04 17:19:39 +01:00
|
|
|
best_block_hash: best_hdr.hash(),
|
|
|
|
best_block_number: best_hdr.number(),
|
|
|
|
best_block_timestamp: best_hdr.timestamp(),
|
2016-12-13 14:48:03 +01:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2017-04-12 12:07:54 +02:00
|
|
|
/// Attempt to get a block hash by block id.
|
|
|
|
pub fn block_hash(&self, id: BlockId) -> Option<H256> {
|
|
|
|
self.chain.block_hash(id)
|
|
|
|
}
|
|
|
|
|
2016-12-13 21:09:57 +01:00
|
|
|
/// Get a block header by Id.
|
2017-02-08 20:22:41 +01:00
|
|
|
pub fn block_header(&self, id: BlockId) -> Option<encoded::Header> {
|
|
|
|
self.chain.block_header(id)
|
2016-12-13 21:09:57 +01:00
|
|
|
}
|
|
|
|
|
2017-02-13 16:49:01 +01:00
|
|
|
/// Get the best block header.
|
|
|
|
pub fn best_block_header(&self) -> encoded::Header {
|
|
|
|
self.chain.best_header()
|
|
|
|
}
|
|
|
|
|
2017-04-12 12:07:54 +02:00
|
|
|
/// Get a block's chain score.
|
|
|
|
pub fn score(&self, id: BlockId) -> Option<U256> {
|
|
|
|
self.chain.score(id)
|
|
|
|
}
|
|
|
|
|
2017-02-17 17:08:46 +01:00
|
|
|
/// Get an iterator over a block and its ancestry.
|
|
|
|
pub fn ancestry_iter(&self, start: BlockId) -> AncestryIter {
|
|
|
|
self.chain.ancestry_iter(start)
|
|
|
|
}
|
|
|
|
|
2017-08-21 13:46:58 +02:00
|
|
|
/// Get the signing chain id.
|
|
|
|
pub fn signing_chain_id(&self) -> Option<u64> {
|
|
|
|
self.engine.signing_chain_id(&self.latest_env_info())
|
2017-02-16 16:08:58 +01:00
|
|
|
}
|
|
|
|
|
2017-01-11 14:39:03 +01:00
|
|
|
/// Flush the header queue.
|
|
|
|
pub fn flush_queue(&self) {
|
|
|
|
self.queue.flush()
|
|
|
|
}
|
|
|
|
|
2016-12-13 21:09:57 +01:00
|
|
|
/// 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());
|
2017-07-26 15:48:00 +02:00
|
|
|
trace!(target: "client", "importing block {}", num);
|
2016-12-15 18:45:11 +01:00
|
|
|
|
2017-07-10 13:21:11 +02:00
|
|
|
if self.verify_full && !self.check_header(&mut bad, &verified_header) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-09-05 17:54:05 +02:00
|
|
|
let write_proof_result = match self.check_epoch_signal(&verified_header) {
|
|
|
|
Ok(Some(proof)) => self.write_pending_proof(&verified_header, proof),
|
|
|
|
Ok(None) => Ok(()),
|
|
|
|
Err(e) =>
|
|
|
|
panic!("Unable to fetch epoch transition proof: {:?}", e),
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Err(e) = write_proof_result {
|
|
|
|
warn!(target: "client", "Error writing pending transition proof to DB: {:?} \
|
|
|
|
The node may not be able to synchronize further.", e);
|
|
|
|
}
|
|
|
|
|
|
|
|
let epoch_proof = self.engine.is_epoch_end(
|
|
|
|
&verified_header,
|
2018-05-09 12:05:56 +02:00
|
|
|
&|h| self.chain.block_header(BlockId::Hash(h)).and_then(|hdr| hdr.decode().ok()),
|
2017-09-05 17:54:05 +02:00
|
|
|
&|h| self.chain.pending_transition(h),
|
|
|
|
);
|
2017-07-10 13:21:11 +02:00
|
|
|
|
2017-03-21 20:57:13 +01:00
|
|
|
let mut tx = self.db.transaction();
|
2017-09-05 17:54:05 +02:00
|
|
|
let pending = match self.chain.insert(&mut tx, verified_header, epoch_proof) {
|
2017-03-23 04:00:22 +01:00
|
|
|
Ok(pending) => {
|
2016-12-23 14:50:42 +01:00
|
|
|
good.push(hash);
|
|
|
|
self.report.write().blocks_imported += 1;
|
2017-03-23 04:00:22 +01:00
|
|
|
pending
|
2016-12-23 14:50:42 +01:00
|
|
|
}
|
2016-12-15 18:45:11 +01:00
|
|
|
Err(e) => {
|
2017-09-05 17:54:05 +02:00
|
|
|
debug!(target: "client", "Error importing header {:?}: {:?}", (num, hash), e);
|
2016-12-15 18:45:11 +01:00
|
|
|
bad.push(hash);
|
2017-07-10 13:21:11 +02:00
|
|
|
continue;
|
2016-12-15 18:45:11 +01:00
|
|
|
}
|
2017-03-23 04:00:22 +01:00
|
|
|
};
|
2017-07-10 13:21:11 +02:00
|
|
|
|
2017-03-21 20:57:13 +01:00
|
|
|
self.db.write_buffered(tx);
|
2017-03-23 04:00:22 +01:00
|
|
|
self.chain.apply_pending(pending);
|
2017-07-10 13:21:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Err(e) = self.db.flush() {
|
|
|
|
panic!("Database flush failed: {}. Check disk health and space.", e);
|
2016-12-15 18:45:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
self.queue.mark_as_bad(&bad);
|
|
|
|
self.queue.mark_as_good(&good);
|
2017-05-23 12:26:39 +02:00
|
|
|
|
|
|
|
self.notify(|listener| listener.new_headers(&good));
|
2016-12-15 18:45:11 +01:00
|
|
|
}
|
2016-12-23 14:50:42 +01:00
|
|
|
|
|
|
|
/// Get a report about blocks imported.
|
|
|
|
pub fn report(&self) -> ClientReport {
|
2017-07-10 13:21:11 +02:00
|
|
|
self.report.read().clone()
|
2016-12-23 14:50:42 +01:00
|
|
|
}
|
2016-12-23 14:54:31 +01:00
|
|
|
|
|
|
|
/// Get blockchain mem usage in bytes.
|
|
|
|
pub fn chain_mem_used(&self) -> usize {
|
2017-08-30 16:04:47 +02:00
|
|
|
use heapsize::HeapSizeOf;
|
2016-12-23 14:54:31 +01:00
|
|
|
|
|
|
|
self.chain.heap_size_of_children()
|
|
|
|
}
|
2017-02-09 19:58:29 +01:00
|
|
|
|
|
|
|
/// Get a handle to the verification engine.
|
2017-09-26 14:19:08 +02:00
|
|
|
pub fn engine(&self) -> &Arc<EthEngine> {
|
2017-03-08 14:39:44 +01:00
|
|
|
&self.engine
|
2017-02-09 19:58:29 +01:00
|
|
|
}
|
2017-02-16 16:08:58 +01:00
|
|
|
|
2017-03-08 14:39:44 +01:00
|
|
|
/// Get the latest environment info.
|
|
|
|
pub fn latest_env_info(&self) -> EnvInfo {
|
|
|
|
self.env_info(BlockId::Latest)
|
|
|
|
.expect("Best block header and recent hashes always stored; qed")
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get environment info for a given block.
|
|
|
|
pub fn env_info(&self, id: BlockId) -> Option<EnvInfo> {
|
|
|
|
let header = match self.block_header(id) {
|
|
|
|
Some(hdr) => hdr,
|
|
|
|
None => return None,
|
|
|
|
};
|
2017-02-16 16:08:58 +01:00
|
|
|
|
2017-03-08 14:39:44 +01:00
|
|
|
Some(EnvInfo {
|
2017-02-16 16:08:58 +01:00
|
|
|
number: header.number(),
|
|
|
|
author: header.author(),
|
|
|
|
timestamp: header.timestamp(),
|
|
|
|
difficulty: header.difficulty(),
|
2017-03-08 14:39:44 +01:00
|
|
|
last_hashes: self.build_last_hashes(header.parent_hash()),
|
2017-02-16 16:08:58 +01:00
|
|
|
gas_used: Default::default(),
|
|
|
|
gas_limit: header.gas_limit(),
|
2017-03-08 14:39:44 +01:00
|
|
|
})
|
2017-02-16 16:08:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn build_last_hashes(&self, mut parent_hash: H256) -> Arc<Vec<H256>> {
|
|
|
|
let mut v = Vec::with_capacity(256);
|
|
|
|
for _ in 0..255 {
|
|
|
|
v.push(parent_hash);
|
|
|
|
match self.block_header(BlockId::Hash(parent_hash)) {
|
|
|
|
Some(header) => parent_hash = header.hash(),
|
|
|
|
None => break,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Arc::new(v)
|
|
|
|
}
|
2017-05-23 12:26:39 +02:00
|
|
|
|
|
|
|
fn notify<F: Fn(&LightChainNotify)>(&self, f: F) {
|
|
|
|
for listener in &*self.listeners.read() {
|
|
|
|
if let Some(listener) = listener.upgrade() {
|
|
|
|
f(&*listener)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-07-10 13:21:11 +02:00
|
|
|
|
2017-07-26 15:48:00 +02:00
|
|
|
// return false if should skip, true otherwise. may push onto bad if
|
2017-07-10 13:21:11 +02:00
|
|
|
// should skip.
|
|
|
|
fn check_header(&self, bad: &mut Vec<H256>, verified_header: &Header) -> bool {
|
|
|
|
let hash = verified_header.hash();
|
|
|
|
let parent_header = match self.chain.block_header(BlockId::Hash(*verified_header.parent_hash())) {
|
|
|
|
Some(header) => header,
|
2017-07-26 15:48:00 +02:00
|
|
|
None => {
|
|
|
|
trace!(target: "client", "No parent for block ({}, {})",
|
|
|
|
verified_header.number(), hash);
|
|
|
|
return false // skip import of block with missing parent.
|
|
|
|
}
|
2017-07-10 13:21:11 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Verify Block Family
|
2018-05-09 12:05:56 +02:00
|
|
|
|
|
|
|
let verify_family_result = {
|
|
|
|
parent_header.decode()
|
|
|
|
.map_err(|dec_err| dec_err.into())
|
|
|
|
.and_then(|decoded| {
|
|
|
|
self.engine.verify_block_family(&verified_header, &decoded)
|
|
|
|
})
|
|
|
|
|
|
|
|
};
|
2017-07-10 13:21:11 +02:00
|
|
|
if let Err(e) = verify_family_result {
|
|
|
|
warn!(target: "client", "Stage 3 block verification failed for #{} ({})\nError: {:?}",
|
|
|
|
verified_header.number(), verified_header.hash(), e);
|
|
|
|
bad.push(hash);
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
// "external" verification.
|
2017-09-26 14:19:08 +02:00
|
|
|
let verify_external_result = self.engine.verify_block_external(&verified_header);
|
2017-07-10 13:21:11 +02:00
|
|
|
if let Err(e) = verify_external_result {
|
|
|
|
warn!(target: "client", "Stage 4 block verification failed for #{} ({})\nError: {:?}",
|
|
|
|
verified_header.number(), verified_header.hash(), e);
|
|
|
|
|
|
|
|
bad.push(hash);
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
true
|
|
|
|
}
|
2017-09-05 17:54:05 +02:00
|
|
|
|
2017-09-26 14:19:08 +02:00
|
|
|
fn check_epoch_signal(&self, verified_header: &Header) -> Result<Option<Proof<EthereumMachine>>, T::Error> {
|
|
|
|
use ethcore::machine::{AuxiliaryRequest, AuxiliaryData};
|
|
|
|
|
|
|
|
let mut block: Option<Vec<u8>> = None;
|
|
|
|
let mut receipts: Option<Vec<_>> = None;
|
|
|
|
|
|
|
|
loop {
|
|
|
|
|
|
|
|
|
|
|
|
let is_signal = {
|
|
|
|
let auxiliary = AuxiliaryData {
|
|
|
|
bytes: block.as_ref().map(|x| &x[..]),
|
|
|
|
receipts: receipts.as_ref().map(|x| &x[..]),
|
2017-09-05 17:54:05 +02:00
|
|
|
};
|
|
|
|
|
2017-09-26 14:19:08 +02:00
|
|
|
self.engine.signals_epoch_end(verified_header, auxiliary)
|
|
|
|
};
|
2017-09-05 17:54:05 +02:00
|
|
|
|
2017-09-26 14:19:08 +02:00
|
|
|
// check with any auxiliary data fetched so far
|
|
|
|
match is_signal {
|
|
|
|
EpochChange::No => return Ok(None),
|
|
|
|
EpochChange::Yes(proof) => return Ok(Some(proof)),
|
|
|
|
EpochChange::Unsure(unsure) => {
|
|
|
|
let (b, r) = match unsure {
|
|
|
|
AuxiliaryRequest::Body =>
|
|
|
|
(Some(self.fetcher.block_body(verified_header)), None),
|
|
|
|
AuxiliaryRequest::Receipts =>
|
|
|
|
(None, Some(self.fetcher.block_receipts(verified_header))),
|
|
|
|
AuxiliaryRequest::Both => (
|
|
|
|
Some(self.fetcher.block_body(verified_header)),
|
|
|
|
Some(self.fetcher.block_receipts(verified_header)),
|
|
|
|
),
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(b) = b {
|
|
|
|
block = Some(b.into_future().wait()?.into_inner());
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(r) = r {
|
|
|
|
receipts = Some(r.into_future().wait()?);
|
|
|
|
}
|
2017-09-05 17:54:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// attempts to fetch the epoch proof from the network until successful.
|
2017-09-26 14:19:08 +02:00
|
|
|
fn write_pending_proof(&self, header: &Header, proof: Proof<EthereumMachine>) -> Result<(), T::Error> {
|
2017-09-05 17:54:05 +02:00
|
|
|
let proof = match proof {
|
|
|
|
Proof::Known(known) => known,
|
|
|
|
Proof::WithState(state_dependent) => {
|
|
|
|
self.fetcher.epoch_transition(
|
|
|
|
header.hash(),
|
|
|
|
self.engine.clone(),
|
|
|
|
state_dependent
|
|
|
|
).into_future().wait()?
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut batch = self.db.transaction();
|
|
|
|
self.chain.insert_pending_transition(&mut batch, header.hash(), epoch::PendingTransition {
|
|
|
|
proof: proof,
|
|
|
|
});
|
|
|
|
self.db.write_buffered(batch);
|
|
|
|
Ok(())
|
|
|
|
}
|
2016-12-13 21:09:57 +01:00
|
|
|
}
|
|
|
|
|
2017-09-05 17:54:05 +02:00
|
|
|
impl<T: ChainDataFetcher> LightChainClient for Client<T> {
|
|
|
|
fn add_listener(&self, listener: Weak<LightChainNotify>) {
|
|
|
|
Client::add_listener(self, listener)
|
|
|
|
}
|
|
|
|
|
2016-12-14 23:26:15 +01:00
|
|
|
fn chain_info(&self) -> BlockChainInfo { Client::chain_info(self) }
|
|
|
|
|
|
|
|
fn queue_header(&self, header: Header) -> Result<H256, BlockImportError> {
|
|
|
|
self.import_header(header)
|
|
|
|
}
|
|
|
|
|
2017-04-12 12:07:54 +02:00
|
|
|
fn block_hash(&self, id: BlockId) -> Option<H256> {
|
|
|
|
Client::block_hash(self, id)
|
|
|
|
}
|
|
|
|
|
2017-02-09 18:42:18 +01:00
|
|
|
fn block_header(&self, id: BlockId) -> Option<encoded::Header> {
|
|
|
|
Client::block_header(self, id)
|
|
|
|
}
|
|
|
|
|
2017-02-13 16:49:01 +01:00
|
|
|
fn best_block_header(&self) -> encoded::Header {
|
|
|
|
Client::best_block_header(self)
|
|
|
|
}
|
|
|
|
|
2017-04-12 12:07:54 +02:00
|
|
|
fn score(&self, id: BlockId) -> Option<U256> {
|
|
|
|
Client::score(self, id)
|
|
|
|
}
|
|
|
|
|
2017-02-17 17:08:46 +01:00
|
|
|
fn ancestry_iter<'a>(&'a self, start: BlockId) -> Box<Iterator<Item=encoded::Header> + 'a> {
|
|
|
|
Box::new(Client::ancestry_iter(self, start))
|
|
|
|
}
|
|
|
|
|
2017-08-21 13:46:58 +02:00
|
|
|
fn signing_chain_id(&self) -> Option<u64> {
|
|
|
|
Client::signing_chain_id(self)
|
2017-02-16 16:08:58 +01:00
|
|
|
}
|
|
|
|
|
2017-04-03 11:37:07 +02:00
|
|
|
fn env_info(&self, id: BlockId) -> Option<EnvInfo> {
|
|
|
|
Client::env_info(self, id)
|
|
|
|
}
|
|
|
|
|
2017-09-26 14:19:08 +02:00
|
|
|
fn engine(&self) -> &Arc<EthEngine> {
|
2017-04-03 11:37:07 +02:00
|
|
|
Client::engine(self)
|
|
|
|
}
|
|
|
|
|
2016-12-14 23:26:15 +01:00
|
|
|
fn is_known(&self, hash: &H256) -> bool {
|
|
|
|
self.status(hash) == BlockStatus::InChain
|
|
|
|
}
|
|
|
|
|
|
|
|
fn clear_queue(&self) {
|
|
|
|
self.queue.clear()
|
|
|
|
}
|
|
|
|
|
2017-01-11 14:39:03 +01:00
|
|
|
fn flush_queue(&self) {
|
|
|
|
Client::flush_queue(self);
|
|
|
|
}
|
|
|
|
|
2016-12-14 23:26:15 +01:00
|
|
|
fn queue_info(&self) -> queue::QueueInfo {
|
|
|
|
self.queue.queue_info()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn cht_root(&self, i: usize) -> Option<H256> {
|
|
|
|
Client::cht_root(self, i)
|
|
|
|
}
|
2017-04-19 14:30:00 +02:00
|
|
|
|
2017-09-05 17:54:05 +02:00
|
|
|
fn eip86_transition(&self) -> BlockNumber {
|
2017-04-19 14:30:00 +02:00
|
|
|
self.engine().params().eip86_transition
|
|
|
|
}
|
2017-07-10 13:21:11 +02:00
|
|
|
|
|
|
|
fn report(&self) -> ClientReport {
|
|
|
|
Client::report(self)
|
|
|
|
}
|
2016-12-14 23:26:15 +01:00
|
|
|
}
|
2017-09-05 17:54:05 +02:00
|
|
|
|
2018-03-03 18:42:13 +01:00
|
|
|
impl<T: ChainDataFetcher> ::ethcore::client::ChainInfo for Client<T> {
|
|
|
|
fn chain_info(&self) -> BlockChainInfo {
|
|
|
|
Client::chain_info(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-05 17:54:05 +02:00
|
|
|
impl<T: ChainDataFetcher> ::ethcore::client::EngineClient for Client<T> {
|
|
|
|
fn update_sealing(&self) { }
|
|
|
|
fn submit_seal(&self, _block_hash: H256, _seal: Vec<Vec<u8>>) { }
|
|
|
|
fn broadcast_consensus_message(&self, _message: Vec<u8>) { }
|
|
|
|
|
|
|
|
fn epoch_transition_for(&self, parent_hash: H256) -> Option<EpochTransition> {
|
|
|
|
self.chain.epoch_transition_for(parent_hash).map(|(hdr, proof)| EpochTransition {
|
|
|
|
block_hash: hdr.hash(),
|
|
|
|
block_number: hdr.number(),
|
|
|
|
proof: proof,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-09-05 17:24:35 +02:00
|
|
|
fn as_full_client(&self) -> Option<&::ethcore::client::BlockChainClient> {
|
|
|
|
None
|
2017-09-05 17:54:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn block_number(&self, id: BlockId) -> Option<BlockNumber> {
|
|
|
|
self.block_header(id).map(|hdr| hdr.number())
|
|
|
|
}
|
2018-02-15 01:39:29 +01:00
|
|
|
|
|
|
|
fn block_header(&self, id: BlockId) -> Option<encoded::Header> {
|
|
|
|
Client::block_header(self, id)
|
|
|
|
}
|
2017-09-05 17:54:05 +02:00
|
|
|
}
|