From 7f3e718851166eb75ff8c4f99d302f041844340e Mon Sep 17 00:00:00 2001 From: Robert Habermeier Date: Tue, 22 Aug 2017 21:02:40 +0800 Subject: [PATCH] EngineClient implementation for light client --- ethcore/light/src/client/mod.rs | 51 ++++++++++++++++++++++++----- ethcore/light/src/client/service.rs | 3 ++ 2 files changed, 46 insertions(+), 8 deletions(-) diff --git a/ethcore/light/src/client/mod.rs b/ethcore/light/src/client/mod.rs index c13d6ba91..e880ef3ba 100644 --- a/ethcore/light/src/client/mod.rs +++ b/ethcore/light/src/client/mod.rs @@ -19,11 +19,11 @@ use std::sync::{Weak, Arc}; use ethcore::block_status::BlockStatus; -use ethcore::client::{ClientReport, EnvInfo}; -use ethcore::engines::{epoch, Engine, EpochChange, Proof, Unsure}; -use ethcore::error::BlockImportError; +use ethcore::client::{TransactionImportResult, ClientReport, EnvInfo}; +use ethcore::engines::{epoch, Engine, EpochChange, EpochTransition, Proof, Unsure}; +use ethcore::error::{TransactionError, BlockImportError, Error as EthcoreError}; use ethcore::ids::BlockId; -use ethcore::header::Header; +use ethcore::header::{BlockNumber, Header}; use ethcore::verification::queue::{self, HeaderQueue}; use ethcore::blockchain_info::BlockChainInfo; use ethcore::spec::Spec; @@ -33,7 +33,7 @@ use io::IoChannel; use futures::{IntoFuture, Future}; -use util::{H256, U256, Mutex, RwLock}; +use util::{Address, H256, U256, Mutex, RwLock}; use util::kvdb::{KeyValueDB, CompactionProfile}; use self::fetch::ChainDataFetcher; @@ -131,7 +131,7 @@ pub trait LightChainClient: Send + Sync { fn cht_root(&self, i: usize) -> Option; /// Get the EIP-86 transition block number. - fn eip86_transition(&self) -> u64; + fn eip86_transition(&self) -> BlockNumber; /// Get a report of import activity since the last call. fn report(&self) -> ClientReport; @@ -555,7 +555,7 @@ impl LightChainClient for Client { Box::new(Client::ancestry_iter(self, start)) } - fn signing_network_id(&self) -> Option { + fn signing_network_id(&self) -> Option { Client::signing_network_id(self) } @@ -587,7 +587,7 @@ impl LightChainClient for Client { Client::cht_root(self, i) } - fn eip86_transition(&self) -> u64 { + fn eip86_transition(&self) -> BlockNumber { self.engine().params().eip86_transition } @@ -595,3 +595,38 @@ impl LightChainClient for Client { Client::report(self) } } + +impl ::ethcore::client::EngineClient for Client { + fn update_sealing(&self) { } + fn submit_seal(&self, _block_hash: H256, _seal: Vec>) { } + fn broadcast_consensus_message(&self, _message: Vec) { } + + fn epoch_transition_for(&self, parent_hash: H256) -> Option { + self.chain.epoch_transition_for(parent_hash).map(|(hdr, proof)| EpochTransition { + block_hash: hdr.hash(), + block_number: hdr.number(), + proof: proof, + }) + } + + fn chain_info(&self) -> BlockChainInfo { + Client::chain_info(self) + } + + fn call_contract(&self, _id: BlockId, _address: Address, _data: Vec) -> Result, String> { + Err("Contract calling not supported by light client".into()) + } + + fn transact_contract(&self, _address: Address, _data: Vec) + -> Result + { + // TODO: these are only really used for misbehavior reporting. + // no relevant clients will be running light clients, but maybe + // they could be at some point? + Err(TransactionError::LimitReached.into()) + } + + fn block_number(&self, id: BlockId) -> Option { + self.block_header(id).map(|hdr| hdr.number()) + } +} diff --git a/ethcore/light/src/client/service.rs b/ethcore/light/src/client/service.rs index 59a5d28c6..458db85c8 100644 --- a/ethcore/light/src/client/service.rs +++ b/ethcore/light/src/client/service.rs @@ -85,7 +85,10 @@ impl Service { io_service.channel(), cache, ).map_err(Error::Database)?); + io_service.register_handler(Arc::new(ImportBlocks(client.clone()))).map_err(Error::Io)?; + spec.engine.register_client(Arc::downgrade(&client) as _); + Ok(Service { client: client, io_service: io_service,