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
@@ -25,14 +25,12 @@ use crate::{
use sc_cli::{CliConfiguration, ImportParams, Result, SharedParams};
use sc_service::{Configuration, NativeExecutionDispatch};
use sp_runtime::traits::Block;
use std::str::FromStr;
impl InspectCmd {
/// Run the inspect command, passing the inspector.
pub fn run<B, RA, D>(&self, config: Configuration) -> Result<()>
where
B: Block,
B::Hash: FromStr,
RA: Send + Sync + 'static,
D: NativeExecutionDispatch + 'static,
{
+7 -25
View File
@@ -487,10 +487,7 @@ pub struct BlockImportOperation<Block: BlockT> {
set_head: Option<Block::Hash>,
}
impl<Block: BlockT> BlockImportOperation<Block>
where
Block::Hash: Ord,
{
impl<Block: BlockT> BlockImportOperation<Block> {
fn apply_storage(
&mut self,
storage: Storage,
@@ -519,10 +516,7 @@ where
}
}
impl<Block: BlockT> backend::BlockImportOperation<Block> for BlockImportOperation<Block>
where
Block::Hash: Ord,
{
impl<Block: BlockT> backend::BlockImportOperation<Block> for BlockImportOperation<Block> {
type State = InMemoryBackend<HashFor<Block>>;
fn state(&self) -> sp_blockchain::Result<Option<&Self::State>> {
@@ -611,20 +605,14 @@ where
///
/// > **Warning**: Doesn't support all the features necessary for a proper database. Only use this
/// > struct for testing purposes. Do **NOT** use in production.
pub struct Backend<Block: BlockT>
where
Block::Hash: Ord,
{
pub struct Backend<Block: BlockT> {
states: RwLock<HashMap<Block::Hash, InMemoryBackend<HashFor<Block>>>>,
blockchain: Blockchain<Block>,
import_lock: RwLock<()>,
pinned_blocks: RwLock<HashMap<Block::Hash, i64>>,
}
impl<Block: BlockT> Backend<Block>
where
Block::Hash: Ord,
{
impl<Block: BlockT> Backend<Block> {
/// Create a new instance of in-mem backend.
///
/// # Warning
@@ -650,10 +638,7 @@ where
}
}
impl<Block: BlockT> backend::AuxStore for Backend<Block>
where
Block::Hash: Ord,
{
impl<Block: BlockT> backend::AuxStore for Backend<Block> {
fn insert_aux<
'a,
'b: 'a,
@@ -673,10 +658,7 @@ where
}
}
impl<Block: BlockT> backend::Backend<Block> for Backend<Block>
where
Block::Hash: Ord,
{
impl<Block: BlockT> backend::Backend<Block> for Backend<Block> {
type BlockImportOperation = BlockImportOperation<Block>;
type Blockchain = Blockchain<Block>;
type State = InMemoryBackend<HashFor<Block>>;
@@ -809,7 +791,7 @@ where
}
}
impl<Block: BlockT> backend::LocalBackend<Block> for Backend<Block> where Block::Hash: Ord {}
impl<Block: BlockT> backend::LocalBackend<Block> for Backend<Block> {}
/// Check that genesis storage is valid.
pub fn check_genesis_storage(storage: &Storage) -> sp_blockchain::Result<()> {
@@ -54,7 +54,6 @@ impl CheckBlockCmd {
B: BlockT + for<'de> serde::Deserialize<'de>,
C: BlockBackend<B> + HeaderBackend<B> + Send + Sync + 'static,
IQ: sc_service::ImportQueue<B> + 'static,
B::Hash: FromStr,
<B::Hash as FromStr>::Err: Debug,
<<B::Header as HeaderT>::Number as FromStr>::Err: Debug,
{
@@ -59,7 +59,6 @@ impl ExportStateCmd {
B: BlockT,
C: UsageProvider<B> + StorageProvider<B, BA> + HeaderBackend<B>,
BA: sc_client_api::backend::Backend<B>,
B::Hash: FromStr,
<B::Hash as FromStr>::Err: Debug,
<<B::Header as HeaderT>::Number as FromStr>::Err: Debug,
{
-1
View File
@@ -113,7 +113,6 @@ impl BlockNumberOrHash {
/// Parse the inner value as `BlockId`.
pub fn parse<B: BlockT>(&self) -> Result<BlockId<B>, String>
where
B::Hash: FromStr,
<B::Hash as FromStr>::Err: std::fmt::Debug,
NumberFor<B>: FromStr,
<NumberFor<B> as FromStr>::Err: std::fmt::Debug,
@@ -718,7 +718,6 @@ pub fn run_grandpa_voter<Block: BlockT, BE: 'static, C, N, S, SC, VR>(
grandpa_params: GrandpaParams<Block, C, N, S, SC, VR>,
) -> sp_blockchain::Result<impl Future<Output = ()> + Send>
where
Block::Hash: Ord,
BE: Backend<Block> + 'static,
N: NetworkT<Block> + Sync + 'static,
S: SyncingT<Block> + Sync + 'static,
+2 -8
View File
@@ -423,10 +423,7 @@ impl<B: BlockT> StateBackend<HashFor<B>> for BenchmarkingState<B> {
&self,
delta: impl Iterator<Item = (&'a [u8], Option<&'a [u8]>)>,
state_version: StateVersion,
) -> (B::Hash, Self::Transaction)
where
B::Hash: Ord,
{
) -> (B::Hash, Self::Transaction) {
self.state
.borrow()
.as_ref()
@@ -438,10 +435,7 @@ impl<B: BlockT> StateBackend<HashFor<B>> for BenchmarkingState<B> {
child_info: &ChildInfo,
delta: impl Iterator<Item = (&'a [u8], Option<&'a [u8]>)>,
state_version: StateVersion,
) -> (B::Hash, bool, Self::Transaction)
where
B::Hash: Ord,
{
) -> (B::Hash, bool, Self::Transaction) {
self.state
.borrow()
.as_ref()
+2 -8
View File
@@ -243,10 +243,7 @@ impl<B: BlockT> StateBackend<HashFor<B>> for RefTrackingState<B> {
&self,
delta: impl Iterator<Item = (&'a [u8], Option<&'a [u8]>)>,
state_version: StateVersion,
) -> (B::Hash, Self::Transaction)
where
B::Hash: Ord,
{
) -> (B::Hash, Self::Transaction) {
self.state.storage_root(delta, state_version)
}
@@ -255,10 +252,7 @@ impl<B: BlockT> StateBackend<HashFor<B>> for RefTrackingState<B> {
child_info: &ChildInfo,
delta: impl Iterator<Item = (&'a [u8], Option<&'a [u8]>)>,
state_version: StateVersion,
) -> (B::Hash, bool, Self::Transaction)
where
B::Hash: Ord,
{
) -> (B::Hash, bool, Self::Transaction) {
self.state.child_storage_root(child_info, delta, state_version)
}
@@ -171,10 +171,7 @@ impl<S: StateBackend<HashFor<B>>, B: BlockT> StateBackend<HashFor<B>> for Record
&self,
delta: impl Iterator<Item = (&'a [u8], Option<&'a [u8]>)>,
state_version: StateVersion,
) -> (B::Hash, Self::Transaction)
where
B::Hash: Ord,
{
) -> (B::Hash, Self::Transaction) {
self.state.storage_root(delta, state_version)
}
@@ -183,10 +180,7 @@ impl<S: StateBackend<HashFor<B>>, B: BlockT> StateBackend<HashFor<B>> for Record
child_info: &ChildInfo,
delta: impl Iterator<Item = (&'a [u8], Option<&'a [u8]>)>,
state_version: StateVersion,
) -> (B::Hash, bool, Self::Transaction)
where
B::Hash: Ord,
{
) -> (B::Hash, bool, Self::Transaction) {
self.state.child_storage_root(child_info, delta, state_version)
}
@@ -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;
@@ -147,7 +147,6 @@ impl PalletCmd {
where
BB: BlockT + Debug,
<<<BB as BlockT>::Header as HeaderT>::Number as std::str::FromStr>::Err: std::fmt::Debug,
<BB as BlockT>::Hash: std::str::FromStr,
ExecDispatch: NativeExecutionDispatch + 'static,
{
if let Some(output_path) = &self.output {
@@ -41,7 +41,7 @@ pub(crate) async fn create_snapshot<Block, HostFns>(
) -> sc_cli::Result<()>
where
Block: BlockT + serde::de::DeserializeOwned,
Block::Hash: FromStr + serde::de::DeserializeOwned,
Block::Hash: serde::de::DeserializeOwned,
Block::Header: serde::de::DeserializeOwned,
<Block::Hash as FromStr>::Err: Debug,
NumberFor<Block>: FromStr,
@@ -68,7 +68,6 @@ pub struct ExecuteBlockCmd {
impl ExecuteBlockCmd {
fn block_ws_uri<Block: BlockT>(&self) -> String
where
Block::Hash: FromStr,
<Block::Hash as FromStr>::Err: Debug,
{
match (&self.block_ws_uri, &self.state) {
@@ -91,7 +90,6 @@ pub(crate) async fn execute_block<Block, HostFns>(
) -> sc_cli::Result<()>
where
Block: BlockT + serde::de::DeserializeOwned,
Block::Hash: FromStr,
<Block::Hash as FromStr>::Err: Debug,
Block::Hash: serde::de::DeserializeOwned,
Block::Header: serde::de::DeserializeOwned,
@@ -211,7 +211,6 @@ pub(crate) async fn fast_forward<Block, HostFns, BBIP>(
) -> Result<()>
where
Block: BlockT<Hash = H256> + DeserializeOwned,
Block::Hash: FromStr,
Block::Header: DeserializeOwned,
<Block::Hash as FromStr>::Err: Debug,
NumberFor<Block>: FromStr,
@@ -84,7 +84,6 @@ pub(crate) async fn follow_chain<Block, HostFns>(
) -> sc_cli::Result<()>
where
Block: BlockT<Hash = H256> + DeserializeOwned,
Block::Hash: FromStr,
Block::Header: DeserializeOwned,
<Block::Hash as FromStr>::Err: Debug,
NumberFor<Block>: FromStr,
@@ -46,7 +46,6 @@ pub struct OffchainWorkerCmd {
impl OffchainWorkerCmd {
fn header_ws_uri<Block: BlockT>(&self) -> String
where
Block::Hash: FromStr,
<Block::Hash as FromStr>::Err: Debug,
{
match (&self.header_ws_uri, &self.state) {
@@ -70,7 +69,6 @@ pub(crate) async fn offchain_worker<Block, HostFns>(
where
Block: BlockT + serde::de::DeserializeOwned,
Block::Header: serde::de::DeserializeOwned,
Block::Hash: FromStr,
<Block::Hash as FromStr>::Err: Debug,
NumberFor<Block>: FromStr,
<NumberFor<Block> as FromStr>::Err: Debug,
@@ -54,7 +54,6 @@ pub(crate) async fn on_runtime_upgrade<Block, HostFns>(
) -> sc_cli::Result<()>
where
Block: BlockT + serde::de::DeserializeOwned,
Block::Hash: FromStr,
<Block::Hash as FromStr>::Err: Debug,
Block::Header: serde::de::DeserializeOwned,
NumberFor<Block>: FromStr,
@@ -618,9 +618,7 @@ impl State {
try_runtime_check: bool,
) -> sc_cli::Result<RemoteExternalities<Block>>
where
Block::Hash: FromStr,
Block::Header: DeserializeOwned,
Block::Hash: DeserializeOwned,
<Block::Hash as FromStr>::Err: Debug,
{
let builder = match self {
@@ -732,7 +730,6 @@ impl TryRuntimeCmd {
where
Block: BlockT<Hash = H256> + DeserializeOwned,
Block::Header: DeserializeOwned,
Block::Hash: FromStr,
<Block::Hash as FromStr>::Err: Debug,
<NumberFor<Block> as FromStr>::Err: Debug,
<NumberFor<Block> as TryInto<u64>>::Error: Debug,
@@ -798,7 +795,6 @@ impl CliConfiguration for TryRuntimeCmd {
/// Get the hash type of the generic `Block` from a `hash_str`.
pub(crate) fn hash_of<Block: BlockT>(hash_str: &str) -> sc_cli::Result<Block::Hash>
where
Block::Hash: FromStr,
<Block::Hash as FromStr>::Err: Debug,
{
hash_str