From 2b91c922c14d21d6704d606397a5a2369c86d0bf Mon Sep 17 00:00:00 2001 From: Robert Habermeier Date: Thu, 16 Feb 2017 16:08:58 +0100 Subject: [PATCH] get signing network ID for light client --- ethcore/light/src/client/mod.rs | 41 ++++++++++++++++++++++++++++++++- rpc/src/v1/helpers/dispatch.rs | 2 +- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/ethcore/light/src/client/mod.rs b/ethcore/light/src/client/mod.rs index a113b4367..bebe89e0e 100644 --- a/ethcore/light/src/client/mod.rs +++ b/ethcore/light/src/client/mod.rs @@ -20,7 +20,7 @@ use std::sync::Arc; use ethcore::block_import_error::BlockImportError; use ethcore::block_status::BlockStatus; -use ethcore::client::ClientReport; +use ethcore::client::{ClientReport, EnvInfo}; use ethcore::engines::Engine; use ethcore::ids::BlockId; use ethcore::header::Header; @@ -62,6 +62,9 @@ pub trait LightChainClient: Send + Sync { /// Get the best block header. fn best_block_header(&self) -> encoded::Header; + /// Get the signing network ID. + fn signing_network_id(&self) -> Option; + /// Query whether a block is known. fn is_known(&self, hash: &H256) -> bool; @@ -164,6 +167,11 @@ impl Client { self.chain.best_header() } + /// Get the signing network id. + pub fn signing_network_id(&self) -> Option { + self.engine.signing_network_id(&self.latest_env_info()) + } + /// Flush the header queue. pub fn flush_queue(&self) { self.queue.flush() @@ -217,6 +225,33 @@ impl Client { pub fn engine(&self) -> &Engine { &*self.engine } + + fn latest_env_info(&self) -> EnvInfo { + let header = self.best_block_header(); + + EnvInfo { + number: header.number(), + author: header.author(), + timestamp: header.timestamp(), + difficulty: header.difficulty(), + last_hashes: self.build_last_hashes(header.hash()), + gas_used: Default::default(), + gas_limit: header.gas_limit(), + } + } + + fn build_last_hashes(&self, mut parent_hash: H256) -> Arc> { + 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) + } } impl LightChainClient for Client { @@ -234,6 +269,10 @@ impl LightChainClient for Client { Client::best_block_header(self) } + fn signing_network_id(&self) -> Option { + Client::signing_network_id(self) + } + fn is_known(&self, hash: &H256) -> bool { self.status(hash) == BlockStatus::InChain } diff --git a/rpc/src/v1/helpers/dispatch.rs b/rpc/src/v1/helpers/dispatch.rs index de0207d79..e8fbf9b76 100644 --- a/rpc/src/v1/helpers/dispatch.rs +++ b/rpc/src/v1/helpers/dispatch.rs @@ -207,7 +207,7 @@ impl Dispatcher for LightDispatcher { fn sign(&self, accounts: Arc, filled: FilledTransactionRequest, password: SignWith) -> BoxFuture, Error> { - let network_id = None; // TODO: fetch from client. + let network_id = self.client.signing_network_id(); let address = filled.from; let best_header = self.client.best_block_header();