From da991efd5f84b144fc8f6e86699e3f05d1c9d839 Mon Sep 17 00:00:00 2001 From: Arkadiy Paronyan Date: Sun, 23 Sep 2018 20:45:06 +0200 Subject: [PATCH] Removed From bound (#790) --- substrate/core/client/db/src/light.rs | 7 +++---- substrate/core/client/src/cht.rs | 21 ++++++++++--------- .../core/client/src/light/call_executor.rs | 7 ++++--- substrate/core/client/src/light/fetcher.rs | 9 ++++---- substrate/core/service/src/components.rs | 8 ++----- 5 files changed, 24 insertions(+), 28 deletions(-) diff --git a/substrate/core/client/db/src/light.rs b/substrate/core/client/db/src/light.rs index f42d67e588..7d9d298833 100644 --- a/substrate/core/client/db/src/light.rs +++ b/substrate/core/client/db/src/light.rs @@ -28,7 +28,7 @@ use client::cht; use client::error::{ErrorKind as ClientErrorKind, Result as ClientResult}; use client::light::blockchain::Storage as LightBlockchainStorage; use codec::{Decode, Encode}; -use primitives::{AuthorityId, H256, Blake2Hasher}; +use primitives::{AuthorityId, Blake2Hasher}; use runtime_primitives::generic::BlockId; use runtime_primitives::traits::{Block as BlockT, Header as HeaderT, Zero, One, As, NumberFor}; @@ -183,7 +183,7 @@ impl BlockchainHeaderBackend for LightStorage } } -impl LightStorage where Block::Hash: From { +impl LightStorage { // note that a block is finalized. ensure that best chain contains the finalized // block number first. fn note_finalized(&self, transaction: &mut DBTransaction, header: &Block::Header, hash: Block::Hash) -> ClientResult<()> { @@ -212,7 +212,7 @@ impl LightStorage where Block::Hash: From { let mut build_cht = |header: &Block::Header| -> ClientResult<()> { if let Some(new_cht_number) = cht::is_build_required(cht::SIZE, *header.number()) { let new_cht_start: NumberFor = cht::start_number(cht::SIZE, new_cht_number); - let new_cht_root: Option = cht::compute_root::( + let new_cht_root = cht::compute_root::( cht::SIZE, new_cht_number, (new_cht_start.as_()..) .map(|num| self.hash(As::sa(num)).unwrap_or_default()) ); @@ -262,7 +262,6 @@ impl LightStorage where Block::Hash: From { impl LightBlockchainStorage for LightStorage where Block: BlockT, - Block::Hash: From, { fn import_header( &self, diff --git a/substrate/core/client/src/cht.rs b/substrate/core/client/src/cht.rs index 2f2f38523b..09ba0dac63 100644 --- a/substrate/core/client/src/cht.rs +++ b/substrate/core/client/src/cht.rs @@ -65,16 +65,15 @@ pub fn compute_root( cht_size: u64, cht_num: Header::Number, hashes: I, -) -> Option +) -> Option where Header: HeaderT, - Header::Hash: From, Hasher: hashdb::Hasher, Hasher::Out: Ord + Encodable, I: IntoIterator>, { build_pairs::(cht_size, cht_num, hashes) - .map(|pairs| triehash::trie_root::(pairs).into()) + .map(|pairs| triehash::trie_root::(pairs)) } /// Build CHT-based header proof. @@ -113,17 +112,19 @@ pub fn check_proof( ) -> ClientResult<()> where Header: HeaderT, - Header::Hash: From, + Header::Hash: AsRef<[u8]>, Hasher: hashdb::Hasher, - Hasher::Out: Ord + Encodable + HeapSizeOf + From, + Hasher::Out: Ord + Encodable + HeapSizeOf, Codec: NodeCodec, { + let mut root: Hasher::Out = Default::default(); + root.as_mut().copy_from_slice(local_root.as_ref()); let local_cht_key = encode_cht_key(local_number); - let local_cht_value = read_proof_check::(local_root.into(), remote_proof, + let local_cht_value = read_proof_check::(root, remote_proof, &local_cht_key).map_err(|e| ClientError::from(e))?; let local_cht_value = local_cht_value.ok_or_else(|| ClientErrorKind::InvalidHeaderProof)?; - let local_hash: Header::Hash = decode_cht_value(&local_cht_value).ok_or_else(|| ClientErrorKind::InvalidHeaderProof)?; - match local_hash == remote_hash { + let local_hash = decode_cht_value(&local_cht_value).ok_or_else(|| ClientErrorKind::InvalidHeaderProof)?; + match &local_hash[..] == remote_hash.as_ref() { true => Ok(()), false => Err(ClientErrorKind::InvalidHeaderProof.into()), } @@ -203,9 +204,9 @@ fn encode_cht_value>(hash: Hash) -> Vec { } /// Convert CHT value into block header hash. -pub fn decode_cht_value>(value: &[u8]) -> Option { +pub fn decode_cht_value(value: &[u8]) -> Option { match value.len() { - 32 => Some(H256::from_slice(&value[0..32]).into()), + 32 => Some(H256::from_slice(&value[0..32])), _ => None, } diff --git a/substrate/core/client/src/light/call_executor.rs b/substrate/core/client/src/light/call_executor.rs index a5827fc7bb..0b0544adc8 100644 --- a/substrate/core/client/src/light/call_executor.rs +++ b/substrate/core/client/src/light/call_executor.rs @@ -25,7 +25,6 @@ use runtime_primitives::generic::BlockId; use runtime_primitives::traits::{Block as BlockT, Header as HeaderT}; use state_machine::{Backend as StateBackend, CodeExecutor, OverlayedChanges, execution_proof_check, ExecutionManager}; -use primitives::H256; use patricia_trie::NodeCodec; use hashdb::Hasher; use rlp::Encodable; @@ -138,14 +137,16 @@ pub fn check_execution_proof( Header: HeaderT, E: CodeExecutor, H: Hasher, - H::Out: Ord + Encodable + HeapSizeOf + From, + H::Out: Ord + Encodable + HeapSizeOf, C: NodeCodec, { let local_state_root = request.header.state_root(); + let mut root: H::Out = Default::default(); + root.as_mut().copy_from_slice(local_state_root.as_ref()); let mut changes = OverlayedChanges::default(); let local_result = execution_proof_check::( - H256::from_slice(local_state_root.as_ref()).into(), + root, remote_proof, &mut changes, executor, diff --git a/substrate/core/client/src/light/fetcher.rs b/substrate/core/client/src/light/fetcher.rs index aaa957a810..1e837c0c8d 100644 --- a/substrate/core/client/src/light/fetcher.rs +++ b/substrate/core/client/src/light/fetcher.rs @@ -18,7 +18,6 @@ use futures::IntoFuture; -use primitives::H256; use hashdb::Hasher; use patricia_trie::NodeCodec; use rlp::Encodable; @@ -134,11 +133,10 @@ impl LightDataChecker { impl FetchChecker for LightDataChecker where Block: BlockT, - Block::Hash: Into + From, E: CodeExecutor, H: Hasher, C: NodeCodec + Sync + Send, - H::Out: Ord + Encodable + HeapSizeOf + From + From, + H::Out: Ord + Encodable + HeapSizeOf, { fn check_header_proof( &self, @@ -162,8 +160,9 @@ impl FetchChecker for LightDataChecker request: &RemoteReadRequest, remote_proof: Vec> ) -> ClientResult>> { - let local_state_root = request.header.state_root().clone(); - read_proof_check::(local_state_root.into(), remote_proof, &request.key).map_err(Into::into) + let mut root: H::Out = Default::default(); + root.as_mut().copy_from_slice(request.header.state_root().as_ref()); + read_proof_check::(root, remote_proof, &request.key).map_err(Into::into) } fn check_execution_proof( diff --git a/substrate/core/service/src/components.rs b/substrate/core/service/src/components.rs index 9badaa49fb..dd204fb735 100644 --- a/substrate/core/service/src/components.rs +++ b/substrate/core/service/src/components.rs @@ -29,7 +29,7 @@ use substrate_executor::{NativeExecutor, NativeExecutionDispatch}; use transaction_pool::{self, Options as TransactionPoolOptions, Pool as TransactionPool}; use runtime_primitives::{traits::Block as BlockT, traits::Header as HeaderT, BuildStorage}; use config::Configuration; -use primitives::{Blake2Hasher, RlpCodec, H256}; +use primitives::{Blake2Hasher, RlpCodec}; // Type aliases. // These exist mainly to avoid typing `::Foo` all over the code. @@ -214,11 +214,7 @@ pub struct LightComponents { _factory: PhantomData, } -impl Components for LightComponents - where - <::Block as BlockT>::Hash: From, - H256: From<<::Block as BlockT>::Hash>, -{ +impl Components for LightComponents { type Factory = Factory; type Executor = LightExecutor; type Backend = LightBackend;