Reduce usage of Blake2Hasher (#5132)

This reduces the usage of `Blake2Hasher` in the code base and replaces
it with `BlakeTwo256`. The most important change is the removal of the
custom extern function for `Blake2Hasher`. The runtime `Hash` trait is
now also simplified and directly requires that the implementing type
implements `Hashable`.
This commit is contained in:
Benjamin Kampmann
2020-03-05 08:51:03 +01:00
committed by GitHub
parent 406fa981bb
commit 5a33228ea9
64 changed files with 372 additions and 451 deletions
@@ -66,7 +66,7 @@ impl<Hashing: Hash> RandomNumberGenerator<Hashing> {
loop {
if self.offset() + needed > self.current.as_ref().len() {
// rehash
self.current = Hashing::hash(self.current.as_ref());
self.current = <Hashing as Hash>::hash(self.current.as_ref());
self.offset = 0;
}
let data = &self.current.as_ref()[self.offset()..self.offset() + needed];
+16 -13
View File
@@ -25,7 +25,7 @@ use std::fmt::Display;
use std::str::FromStr;
#[cfg(feature = "std")]
use serde::{Serialize, Deserialize, de::DeserializeOwned};
use sp_core::{self, Hasher, Blake2Hasher, TypeId, RuntimeDebug};
use sp_core::{self, Hasher, TypeId, RuntimeDebug};
use crate::codec::{Codec, Encode, Decode};
use crate::transaction_validity::{
ValidTransaction, TransactionValidity, TransactionValidityError, UnknownTransaction,
@@ -369,20 +369,19 @@ pub trait OffchainWorker<BlockNumber> {
/// Abstraction around hashing
// Stupid bug in the Rust compiler believes derived
// traits must be fulfilled by all type parameters.
pub trait Hash: 'static + MaybeSerializeDeserialize + Debug + Clone + Eq + PartialEq {
pub trait Hash: 'static + MaybeSerializeDeserialize + Debug + Clone + Eq + PartialEq + Hasher<Out = <Self as Hash>::Output> {
/// The hash type produced.
type Output: Member + MaybeSerializeDeserialize + Debug + sp_std::hash::Hash
+ AsRef<[u8]> + AsMut<[u8]> + Copy + Default + Encode + Decode;
/// The associated hash_db Hasher type.
type Hasher: Hasher<Out=Self::Output>;
/// Produce the hash of some byte-slice.
fn hash(s: &[u8]) -> Self::Output;
fn hash(s: &[u8]) -> Self::Output {
<Self as Hasher>::hash(s)
}
/// Produce the hash of some codec-encodable value.
fn hash_of<S: Encode>(s: &S) -> Self::Output {
Encode::using_encoded(s, Self::hash)
Encode::using_encoded(s, <Self as Hasher>::hash)
}
/// The ordered Patricia tree root of the given `input`.
@@ -397,12 +396,18 @@ pub trait Hash: 'static + MaybeSerializeDeserialize + Debug + Clone + Eq + Parti
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
pub struct BlakeTwo256;
impl Hash for BlakeTwo256 {
type Output = sp_core::H256;
type Hasher = Blake2Hasher;
fn hash(s: &[u8]) -> Self::Output {
impl Hasher for BlakeTwo256 {
type Out = sp_core::H256;
type StdHasher = hash256_std_hasher::Hash256StdHasher;
const LENGTH: usize = 32;
fn hash(s: &[u8]) -> Self::Out {
sp_io::hashing::blake2_256(s).into()
}
}
impl Hash for BlakeTwo256 {
type Output = sp_core::H256;
fn trie_root(input: Vec<(Vec<u8>, Vec<u8>)>) -> Self::Output {
sp_io::trie::blake2_256_root(input)
@@ -616,8 +621,6 @@ pub trait ExtrinsicMetadata {
type SignedExtensions: SignedExtension;
}
/// Extract the hasher type for a block.
pub type HasherFor<B> = <HashFor<B> as Hash>::Hasher;
/// Extract the hashing type for a block.
pub type HashFor<B> = <<B as Block>::Header as Header>::Hashing;
/// Extract the number type for a block.