Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
refactor: define trait HashOutput for some Hash associate type (#14220
Browse files Browse the repository at this point in the history
)

* define trait `HashOutput`

* improve

* improve

* Update primitives/runtime/src/traits.rs

Co-authored-by: Bastian Köcher <[email protected]>

* remove `Block::Hash: Ord`

* fmt

* add `MaybeFromStr`

* cleanup

* fix

* remove useless `HashOutput`

---------

Co-authored-by: Bastian Köcher <[email protected]>
  • Loading branch information
yjhmelody and bkchr authored Jun 8, 2023
1 parent aeb8c31 commit 85b0807
Show file tree
Hide file tree
Showing 19 changed files with 85 additions and 146 deletions.
2 changes: 0 additions & 2 deletions bin/node/inspect/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
{
Expand Down
32 changes: 7 additions & 25 deletions client/api/src/in_mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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>> {
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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>>;
Expand Down Expand Up @@ -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<()> {
Expand Down
1 change: 0 additions & 1 deletion client/cli/src/commands/check_block_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
{
Expand Down
1 change: 0 additions & 1 deletion client/cli/src/commands/export_state_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
{
Expand Down
1 change: 0 additions & 1 deletion client/cli/src/params/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 0 additions & 1 deletion client/consensus/grandpa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 2 additions & 8 deletions client/db/src/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down
10 changes: 2 additions & 8 deletions client/db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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)
}

Expand Down
10 changes: 2 additions & 8 deletions client/db/src/record_stats_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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)
}

Expand Down
46 changes: 17 additions & 29 deletions primitives/runtime/src/generic/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -79,55 +79,55 @@ impl<Number, Hash> traits::Header for Header<Number, Hash>
where
Number: Member
+ MaybeSerializeDeserialize
+ MaybeFromStr
+ Debug
+ sp_std::hash::Hash
+ MaybeDisplay
+ AtLeast32BitUnsigned
+ 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
}
Expand All @@ -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>
Expand All @@ -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.
Expand Down
Loading

0 comments on commit 85b0807

Please sign in to comment.