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
+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)
}