refactor: define trait HashOutput for some Hash associate type (#14220)

* define trait `HashOutput`

* improve

* improve

* Update primitives/runtime/src/traits.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* remove `Block::Hash: Ord`

* fmt

* add `MaybeFromStr`

* cleanup

* fix

* remove useless `HashOutput`

---------

Co-authored-by: Bastian Köcher <git@kchr.de>
This commit is contained in:
yjh
2023-06-08 10:02:13 +08:00
committed by GitHub
parent c31c6a1a73
commit ddb46a8aa0
19 changed files with 85 additions and 146 deletions
@@ -22,8 +22,8 @@ use crate::{
generic::Digest,
scale_info::TypeInfo,
traits::{
self, AtLeast32BitUnsigned, Hash as HashT, MaybeDisplay, MaybeSerialize,
MaybeSerializeDeserialize, Member, SimpleBitOps,
self, AtLeast32BitUnsigned, Hash as HashT, MaybeDisplay, MaybeFromStr,
MaybeSerializeDeserialize, Member,
},
};
#[cfg(feature = "serde")]
@@ -79,6 +79,7 @@ impl<Number, Hash> traits::Header for Header<Number, Hash>
where
Number: Member
+ MaybeSerializeDeserialize
+ MaybeFromStr
+ Debug
+ sp_std::hash::Hash
+ MaybeDisplay
@@ -86,48 +87,47 @@ where
+ Codec
+ Copy
+ Into<U256>
+ TryFrom<U256>
+ sp_std::str::FromStr,
+ TryFrom<U256>,
Hash: HashT,
Hash::Output: Default
+ sp_std::hash::Hash
+ Copy
+ Member
+ Ord
+ MaybeSerialize
+ Debug
+ MaybeDisplay
+ SimpleBitOps
+ Codec,
{
type Number = Number;
type Hash = <Hash as HashT>::Output;
type Hashing = Hash;
fn new(
number: Self::Number,
extrinsics_root: Self::Hash,
state_root: Self::Hash,
parent_hash: Self::Hash,
digest: Digest,
) -> Self {
Self { number, extrinsics_root, state_root, parent_hash, digest }
}
fn number(&self) -> &Self::Number {
&self.number
}
fn set_number(&mut self, num: Self::Number) {
self.number = num
}
fn extrinsics_root(&self) -> &Self::Hash {
&self.extrinsics_root
}
fn set_extrinsics_root(&mut self, root: Self::Hash) {
self.extrinsics_root = root
}
fn state_root(&self) -> &Self::Hash {
&self.state_root
}
fn set_state_root(&mut self, root: Self::Hash) {
self.state_root = root
}
fn parent_hash(&self) -> &Self::Hash {
&self.parent_hash
}
fn set_parent_hash(&mut self, hash: Self::Hash) {
self.parent_hash = hash
}
@@ -141,16 +141,6 @@ where
log::debug!(target: "header", "Retrieving mutable reference to digest");
&mut self.digest
}
fn new(
number: Self::Number,
extrinsics_root: Self::Hash,
state_root: Self::Hash,
parent_hash: Self::Hash,
digest: Digest,
) -> Self {
Self { number, extrinsics_root, state_root, parent_hash, digest }
}
}
impl<Number, Hash> Header<Number, Hash>
@@ -164,8 +154,6 @@ where
+ Into<U256>
+ TryFrom<U256>,
Hash: HashT,
Hash::Output:
Default + sp_std::hash::Hash + Copy + Member + MaybeDisplay + SimpleBitOps + Codec,
{
/// Convenience helper for computing the hash of the header without having
/// to import the trait.
+54 -49
View File
@@ -932,19 +932,7 @@ pub trait Hash:
+ 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
+ EncodeLike
+ MaxEncodedLen
+ TypeInfo;
type Output: HashOutput;
/// Produce the hash of some byte-slice.
fn hash(s: &[u8]) -> Self::Output {
@@ -963,6 +951,47 @@ pub trait Hash:
fn trie_root(input: Vec<(Vec<u8>, Vec<u8>)>, state_version: StateVersion) -> Self::Output;
}
/// Super trait with all the attributes for a hashing output.
pub trait HashOutput:
Member
+ MaybeSerializeDeserialize
+ MaybeDisplay
+ MaybeFromStr
+ Debug
+ sp_std::hash::Hash
+ AsRef<[u8]>
+ AsMut<[u8]>
+ Copy
+ Ord
+ Default
+ Encode
+ Decode
+ EncodeLike
+ MaxEncodedLen
+ TypeInfo
{
}
impl<T> HashOutput for T where
T: Member
+ MaybeSerializeDeserialize
+ MaybeDisplay
+ MaybeFromStr
+ Debug
+ sp_std::hash::Hash
+ AsRef<[u8]>
+ AsMut<[u8]>
+ Copy
+ Ord
+ Default
+ Encode
+ Decode
+ EncodeLike
+ MaxEncodedLen
+ TypeInfo
{
}
/// Blake2-256 Hash implementation.
#[derive(PartialEq, Eq, Clone, RuntimeDebug, TypeInfo)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
@@ -981,13 +1010,13 @@ impl Hasher for BlakeTwo256 {
impl Hash for BlakeTwo256 {
type Output = sp_core::H256;
fn trie_root(input: Vec<(Vec<u8>, Vec<u8>)>, version: StateVersion) -> Self::Output {
sp_io::trie::blake2_256_root(input, version)
}
fn ordered_trie_root(input: Vec<Vec<u8>>, version: StateVersion) -> Self::Output {
sp_io::trie::blake2_256_ordered_root(input, version)
}
fn trie_root(input: Vec<(Vec<u8>, Vec<u8>)>, version: StateVersion) -> Self::Output {
sp_io::trie::blake2_256_root(input, version)
}
}
/// Keccak-256 Hash implementation.
@@ -1008,13 +1037,13 @@ impl Hasher for Keccak256 {
impl Hash for Keccak256 {
type Output = sp_core::H256;
fn trie_root(input: Vec<(Vec<u8>, Vec<u8>)>, version: StateVersion) -> Self::Output {
sp_io::trie::keccak_256_root(input, version)
}
fn ordered_trie_root(input: Vec<Vec<u8>>, version: StateVersion) -> Self::Output {
sp_io::trie::keccak_256_ordered_root(input, version)
}
fn trie_root(input: Vec<(Vec<u8>, Vec<u8>)>, version: StateVersion) -> Self::Output {
sp_io::trie::keccak_256_root(input, version)
}
}
/// Something that can be checked for equality and printed out to a debug channel if bad.
@@ -1102,27 +1131,15 @@ pub trait Header: Clone + Send + Sync + Codec + Eq + MaybeSerialize + Debug + 's
/// Header number.
type Number: Member
+ MaybeSerializeDeserialize
+ MaybeFromStr
+ Debug
+ sp_std::hash::Hash
+ Copy
+ MaybeDisplay
+ AtLeast32BitUnsigned
+ Codec
+ sp_std::str::FromStr;
+ Codec;
/// Header hash type
type Hash: Member
+ MaybeSerializeDeserialize
+ Debug
+ sp_std::hash::Hash
+ Ord
+ Copy
+ MaybeDisplay
+ Default
+ SimpleBitOps
+ Codec
+ AsRef<[u8]>
+ AsMut<[u8]>
+ TypeInfo;
type Hash: HashOutput;
/// Hashing algorithm
type Hashing: Hash<Output = Self::Hash>;
@@ -1176,19 +1193,7 @@ pub trait Block: Clone + Send + Sync + Codec + Eq + MaybeSerialize + Debug + 'st
/// Header type.
type Header: Header<Hash = Self::Hash>;
/// Block hash type.
type Hash: Member
+ MaybeSerializeDeserialize
+ Debug
+ sp_std::hash::Hash
+ Ord
+ Copy
+ MaybeDisplay
+ Default
+ SimpleBitOps
+ Codec
+ AsRef<[u8]>
+ AsMut<[u8]>
+ TypeInfo;
type Hash: HashOutput;
/// Returns a reference to the header.
fn header(&self) -> &Self::Header;