From d6a78e54c900980cf3c35f69c6e0ac45bd0ca66a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20K=C3=B6cher?= Date: Wed, 24 May 2023 17:14:53 +0200 Subject: [PATCH] pallet-merkle-mountain-range: Remove extra `Hash` type (#14214) * pallet-merkle-mountain-range: Remove extra `Hash` type * FMT --- bin/node/runtime/src/lib.rs | 3 +- frame/beefy-mmr/src/lib.rs | 4 +-- frame/beefy-mmr/src/mock.rs | 6 ++-- frame/merkle-mountain-range/src/lib.rs | 34 ++++++---------------- frame/merkle-mountain-range/src/mmr/mmr.rs | 8 ++--- frame/merkle-mountain-range/src/mock.rs | 1 - primitives/runtime/src/traits.rs | 3 +- 7 files changed, 20 insertions(+), 39 deletions(-) diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index 47134f41f0ddf..4bab2528ae2f7 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -1453,7 +1453,6 @@ impl pallet_vesting::Config for Runtime { impl pallet_mmr::Config for Runtime { const INDEXING_PREFIX: &'static [u8] = b"mmr"; type Hashing = ::Hashing; - type Hash = ::Hash; type LeafData = pallet_mmr::ParentNumberAndHash; type OnNewRoot = (); type WeightInfo = (); @@ -1945,7 +1944,7 @@ mod mmr { pub use pallet_mmr::primitives::*; pub type Leaf = <::LeafData as LeafDataProvider>::LeafData; - pub type Hash = ::Hash; + pub type Hash = ::Output; pub type Hashing = ::Hashing; } diff --git a/frame/beefy-mmr/src/lib.rs b/frame/beefy-mmr/src/lib.rs index b9d4bdfd8eb59..686f3c56f82b1 100644 --- a/frame/beefy-mmr/src/lib.rs +++ b/frame/beefy-mmr/src/lib.rs @@ -59,7 +59,7 @@ where T: pallet_mmr::Config, T: pallet_beefy::Config, { - fn on_new_root(root: &::Hash) { + fn on_new_root(root: &sp_consensus_beefy::MmrRootHash) { let digest = sp_runtime::generic::DigestItem::Consensus( sp_consensus_beefy::BEEFY_ENGINE_ID, codec::Encode::encode(&sp_consensus_beefy::ConsensusLog::< @@ -84,7 +84,7 @@ impl Convert> for BeefyEcdsaToE } } -type MerkleRootOf = ::Hash; +type MerkleRootOf = <::Hashing as sp_runtime::traits::Hash>::Output; #[frame_support::pallet] pub mod pallet { diff --git a/frame/beefy-mmr/src/mock.rs b/frame/beefy-mmr/src/mock.rs index 8b3bedcb960b4..31484aaa6be70 100644 --- a/frame/beefy-mmr/src/mock.rs +++ b/frame/beefy-mmr/src/mock.rs @@ -25,7 +25,7 @@ use frame_support::{ BasicExternalities, }; use sp_consensus_beefy::mmr::MmrLeafVersion; -use sp_core::{Hasher, H256}; +use sp_core::H256; use sp_runtime::{ app_crypto::ecdsa::Public, impl_opaque_keys, @@ -104,7 +104,7 @@ impl pallet_session::Config for Test { pub type MmrLeaf = sp_consensus_beefy::mmr::MmrLeaf< ::BlockNumber, ::Hash, - ::Hash, + crate::MerkleRootOf, Vec, >; @@ -113,8 +113,6 @@ impl pallet_mmr::Config for Test { type Hashing = Keccak256; - type Hash = ::Out; - type LeafData = BeefyMmr; type OnNewRoot = pallet_beefy_mmr::DepositBeefyDigest; diff --git a/frame/merkle-mountain-range/src/lib.rs b/frame/merkle-mountain-range/src/lib.rs index 21d020ed55ecb..3adad1e955481 100644 --- a/frame/merkle-mountain-range/src/lib.rs +++ b/frame/merkle-mountain-range/src/lib.rs @@ -113,6 +113,8 @@ type LeafOf = <>::LeafData as primitives::LeafDataProvider> /// Hashing used for the pallet. pub(crate) type HashingOf = >::Hashing; +/// Hash type used for the pallet. +pub(crate) type HashOf = <>::Hashing as traits::Hash>::Output; #[frame_support::pallet] pub mod pallet { @@ -146,24 +148,7 @@ pub mod pallet { /// /// Then we create a tuple of these two hashes, SCALE-encode it (concatenate) and /// hash, to obtain a new MMR inner node - the new peak. - type Hashing: traits::Hash>::Hash>; - - /// The hashing output type. - /// - /// This type is actually going to be stored in the MMR. - /// Required to be provided again, to satisfy trait bounds for storage items. - type Hash: traits::Member - + traits::MaybeSerializeDeserialize - + sp_std::fmt::Debug - + sp_std::hash::Hash - + AsRef<[u8]> - + AsMut<[u8]> - + Copy - + Default - + codec::Codec - + codec::EncodeLike - + scale_info::TypeInfo - + MaxEncodedLen; + type Hashing: traits::Hash; /// Data stored in the leaf nodes. /// @@ -189,7 +174,7 @@ pub mod pallet { /// apart from having it in the storage. For instance you might output it in the header /// digest (see [`frame_system::Pallet::deposit_log`]) to make it available for Light /// Clients. Hook complexity should be `O(1)`. - type OnNewRoot: primitives::OnNewRoot<>::Hash>; + type OnNewRoot: primitives::OnNewRoot>; /// Weights for this pallet. type WeightInfo: WeightInfo; @@ -198,8 +183,7 @@ pub mod pallet { /// Latest MMR Root hash. #[pallet::storage] #[pallet::getter(fn mmr_root_hash)] - pub type RootHash, I: 'static = ()> = - StorageValue<_, >::Hash, ValueQuery>; + pub type RootHash, I: 'static = ()> = StorageValue<_, HashOf, ValueQuery>; /// Current size of the MMR (number of leaves). #[pallet::storage] @@ -213,7 +197,7 @@ pub mod pallet { #[pallet::storage] #[pallet::getter(fn mmr_peak)] pub type Nodes, I: 'static = ()> = - StorageMap<_, Identity, NodeIndex, >::Hash, OptionQuery>; + StorageMap<_, Identity, NodeIndex, HashOf, OptionQuery>; #[pallet::hooks] impl, I: 'static> Hooks> for Pallet { @@ -338,7 +322,7 @@ impl, I: 'static> Pallet { pub fn generate_proof( block_numbers: Vec, best_known_block_number: Option, - ) -> Result<(Vec>, primitives::Proof<>::Hash>), primitives::Error> { + ) -> Result<(Vec>, primitives::Proof>), primitives::Error> { // check whether best_known_block_number provided, else use current best block let best_known_block_number = best_known_block_number.unwrap_or_else(|| >::block_number()); @@ -359,7 +343,7 @@ impl, I: 'static> Pallet { } /// Return the on-chain MMR root hash. - pub fn mmr_root() -> >::Hash { + pub fn mmr_root() -> HashOf { Self::mmr_root_hash() } @@ -371,7 +355,7 @@ impl, I: 'static> Pallet { /// or the proof is invalid. pub fn verify_leaves( leaves: Vec>, - proof: primitives::Proof<>::Hash>, + proof: primitives::Proof>, ) -> Result<(), primitives::Error> { if proof.leaf_count > Self::mmr_leaves() || proof.leaf_count == 0 || diff --git a/frame/merkle-mountain-range/src/mmr/mmr.rs b/frame/merkle-mountain-range/src/mmr/mmr.rs index beb4ac977c3be..aeb3e7ea66414 100644 --- a/frame/merkle-mountain-range/src/mmr/mmr.rs +++ b/frame/merkle-mountain-range/src/mmr/mmr.rs @@ -21,7 +21,7 @@ use crate::{ Hasher, Node, NodeOf, }, primitives::{self, Error, NodeIndex}, - Config, HashingOf, + Config, HashOf, HashingOf, }; use sp_mmr_primitives::{mmr_lib, utils::NodesUtils}; use sp_std::prelude::*; @@ -95,7 +95,7 @@ where pub fn verify_leaves_proof( &self, leaves: Vec, - proof: primitives::Proof<>::Hash>, + proof: primitives::Proof>, ) -> Result { let p = mmr_lib::MerkleProof::, Hasher, L>>::new( self.mmr.mmr_size(), @@ -145,7 +145,7 @@ where /// Commit the changes to underlying storage, return current number of leaves and /// calculate the new MMR's root hash. - pub fn finalize(self) -> Result<(NodeIndex, >::Hash), Error> { + pub fn finalize(self) -> Result<(NodeIndex, HashOf), Error> { let root = self.mmr.get_root().map_err(|e| Error::GetRoot.log_error(e))?; self.mmr.commit().map_err(|e| Error::Commit.log_error(e))?; Ok((self.leaves, root.hash())) @@ -166,7 +166,7 @@ where pub fn generate_proof( &self, leaf_indices: Vec, - ) -> Result<(Vec, primitives::Proof<>::Hash>), Error> { + ) -> Result<(Vec, primitives::Proof>), Error> { let positions = leaf_indices .iter() .map(|index| mmr_lib::leaf_index_to_pos(*index)) diff --git a/frame/merkle-mountain-range/src/mock.rs b/frame/merkle-mountain-range/src/mock.rs index 292c80a483325..cea426368c35c 100644 --- a/frame/merkle-mountain-range/src/mock.rs +++ b/frame/merkle-mountain-range/src/mock.rs @@ -75,7 +75,6 @@ impl Config for Test { const INDEXING_PREFIX: &'static [u8] = b"mmr-"; type Hashing = Keccak256; - type Hash = H256; type LeafData = Compact, LeafData)>; type OnNewRoot = (); type WeightInfo = (); diff --git a/primitives/runtime/src/traits.rs b/primitives/runtime/src/traits.rs index d37db4802d78f..d1fda9f3d5256 100644 --- a/primitives/runtime/src/traits.rs +++ b/primitives/runtime/src/traits.rs @@ -18,7 +18,6 @@ //! Primitives for the runtime modules. use crate::{ - codec::{Codec, Decode, Encode, MaxEncodedLen}, generic::Digest, scale_info::{MetaType, StaticTypeInfo, TypeInfo}, transaction_validity::{ @@ -27,6 +26,7 @@ use crate::{ }, DispatchResult, }; +use codec::{Codec, Decode, Encode, EncodeLike, MaxEncodedLen}; use impl_trait_for_tuples::impl_for_tuples; #[cfg(feature = "serde")] use serde::{de::DeserializeOwned, Deserialize, Serialize}; @@ -694,6 +694,7 @@ pub trait Hash: + Default + Encode + Decode + + EncodeLike + MaxEncodedLen + TypeInfo;