pallet-merkle-mountain-range: Remove extra Hash type (#14214)

* pallet-merkle-mountain-range: Remove extra `Hash` type

* FMT
This commit is contained in:
Bastian Köcher
2023-05-24 17:14:53 +02:00
committed by GitHub
parent db90f3b622
commit 5bf4ff56bc
7 changed files with 20 additions and 39 deletions
+1 -2
View File
@@ -1453,7 +1453,6 @@ impl pallet_vesting::Config for Runtime {
impl pallet_mmr::Config for Runtime { impl pallet_mmr::Config for Runtime {
const INDEXING_PREFIX: &'static [u8] = b"mmr"; const INDEXING_PREFIX: &'static [u8] = b"mmr";
type Hashing = <Runtime as frame_system::Config>::Hashing; type Hashing = <Runtime as frame_system::Config>::Hashing;
type Hash = <Runtime as frame_system::Config>::Hash;
type LeafData = pallet_mmr::ParentNumberAndHash<Self>; type LeafData = pallet_mmr::ParentNumberAndHash<Self>;
type OnNewRoot = (); type OnNewRoot = ();
type WeightInfo = (); type WeightInfo = ();
@@ -1945,7 +1944,7 @@ mod mmr {
pub use pallet_mmr::primitives::*; pub use pallet_mmr::primitives::*;
pub type Leaf = <<Runtime as pallet_mmr::Config>::LeafData as LeafDataProvider>::LeafData; pub type Leaf = <<Runtime as pallet_mmr::Config>::LeafData as LeafDataProvider>::LeafData;
pub type Hash = <Runtime as pallet_mmr::Config>::Hash; pub type Hash = <Hashing as sp_runtime::traits::Hash>::Output;
pub type Hashing = <Runtime as pallet_mmr::Config>::Hashing; pub type Hashing = <Runtime as pallet_mmr::Config>::Hashing;
} }
+2 -2
View File
@@ -59,7 +59,7 @@ where
T: pallet_mmr::Config<Hash = sp_consensus_beefy::MmrRootHash>, T: pallet_mmr::Config<Hash = sp_consensus_beefy::MmrRootHash>,
T: pallet_beefy::Config, T: pallet_beefy::Config,
{ {
fn on_new_root(root: &<T as pallet_mmr::Config>::Hash) { fn on_new_root(root: &sp_consensus_beefy::MmrRootHash) {
let digest = sp_runtime::generic::DigestItem::Consensus( let digest = sp_runtime::generic::DigestItem::Consensus(
sp_consensus_beefy::BEEFY_ENGINE_ID, sp_consensus_beefy::BEEFY_ENGINE_ID,
codec::Encode::encode(&sp_consensus_beefy::ConsensusLog::< codec::Encode::encode(&sp_consensus_beefy::ConsensusLog::<
@@ -84,7 +84,7 @@ impl Convert<sp_consensus_beefy::crypto::AuthorityId, Vec<u8>> for BeefyEcdsaToE
} }
} }
type MerkleRootOf<T> = <T as pallet_mmr::Config>::Hash; type MerkleRootOf<T> = <<T as pallet_mmr::Config>::Hashing as sp_runtime::traits::Hash>::Output;
#[frame_support::pallet] #[frame_support::pallet]
pub mod pallet { pub mod pallet {
+2 -4
View File
@@ -25,7 +25,7 @@ use frame_support::{
BasicExternalities, BasicExternalities,
}; };
use sp_consensus_beefy::mmr::MmrLeafVersion; use sp_consensus_beefy::mmr::MmrLeafVersion;
use sp_core::{Hasher, H256}; use sp_core::H256;
use sp_runtime::{ use sp_runtime::{
app_crypto::ecdsa::Public, app_crypto::ecdsa::Public,
impl_opaque_keys, impl_opaque_keys,
@@ -104,7 +104,7 @@ impl pallet_session::Config for Test {
pub type MmrLeaf = sp_consensus_beefy::mmr::MmrLeaf< pub type MmrLeaf = sp_consensus_beefy::mmr::MmrLeaf<
<Test as frame_system::Config>::BlockNumber, <Test as frame_system::Config>::BlockNumber,
<Test as frame_system::Config>::Hash, <Test as frame_system::Config>::Hash,
<Test as pallet_mmr::Config>::Hash, crate::MerkleRootOf<Test>,
Vec<u8>, Vec<u8>,
>; >;
@@ -113,8 +113,6 @@ impl pallet_mmr::Config for Test {
type Hashing = Keccak256; type Hashing = Keccak256;
type Hash = <Keccak256 as Hasher>::Out;
type LeafData = BeefyMmr; type LeafData = BeefyMmr;
type OnNewRoot = pallet_beefy_mmr::DepositBeefyDigest<Test>; type OnNewRoot = pallet_beefy_mmr::DepositBeefyDigest<Test>;
@@ -113,6 +113,8 @@ type LeafOf<T, I> = <<T as Config<I>>::LeafData as primitives::LeafDataProvider>
/// Hashing used for the pallet. /// Hashing used for the pallet.
pub(crate) type HashingOf<T, I> = <T as Config<I>>::Hashing; pub(crate) type HashingOf<T, I> = <T as Config<I>>::Hashing;
/// Hash type used for the pallet.
pub(crate) type HashOf<T, I> = <<T as Config<I>>::Hashing as traits::Hash>::Output;
#[frame_support::pallet] #[frame_support::pallet]
pub mod 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 /// 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. /// hash, to obtain a new MMR inner node - the new peak.
type Hashing: traits::Hash<Output = <Self as Config<I>>::Hash>; type Hashing: traits::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;
/// Data stored in the leaf nodes. /// 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 /// 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 /// digest (see [`frame_system::Pallet::deposit_log`]) to make it available for Light
/// Clients. Hook complexity should be `O(1)`. /// Clients. Hook complexity should be `O(1)`.
type OnNewRoot: primitives::OnNewRoot<<Self as Config<I>>::Hash>; type OnNewRoot: primitives::OnNewRoot<HashOf<Self, I>>;
/// Weights for this pallet. /// Weights for this pallet.
type WeightInfo: WeightInfo; type WeightInfo: WeightInfo;
@@ -198,8 +183,7 @@ pub mod pallet {
/// Latest MMR Root hash. /// Latest MMR Root hash.
#[pallet::storage] #[pallet::storage]
#[pallet::getter(fn mmr_root_hash)] #[pallet::getter(fn mmr_root_hash)]
pub type RootHash<T: Config<I>, I: 'static = ()> = pub type RootHash<T: Config<I>, I: 'static = ()> = StorageValue<_, HashOf<T, I>, ValueQuery>;
StorageValue<_, <T as Config<I>>::Hash, ValueQuery>;
/// Current size of the MMR (number of leaves). /// Current size of the MMR (number of leaves).
#[pallet::storage] #[pallet::storage]
@@ -213,7 +197,7 @@ pub mod pallet {
#[pallet::storage] #[pallet::storage]
#[pallet::getter(fn mmr_peak)] #[pallet::getter(fn mmr_peak)]
pub type Nodes<T: Config<I>, I: 'static = ()> = pub type Nodes<T: Config<I>, I: 'static = ()> =
StorageMap<_, Identity, NodeIndex, <T as Config<I>>::Hash, OptionQuery>; StorageMap<_, Identity, NodeIndex, HashOf<T, I>, OptionQuery>;
#[pallet::hooks] #[pallet::hooks]
impl<T: Config<I>, I: 'static> Hooks<BlockNumberFor<T>> for Pallet<T, I> { impl<T: Config<I>, I: 'static> Hooks<BlockNumberFor<T>> for Pallet<T, I> {
@@ -338,7 +322,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
pub fn generate_proof( pub fn generate_proof(
block_numbers: Vec<T::BlockNumber>, block_numbers: Vec<T::BlockNumber>,
best_known_block_number: Option<T::BlockNumber>, best_known_block_number: Option<T::BlockNumber>,
) -> Result<(Vec<LeafOf<T, I>>, primitives::Proof<<T as Config<I>>::Hash>), primitives::Error> { ) -> Result<(Vec<LeafOf<T, I>>, primitives::Proof<HashOf<T, I>>), primitives::Error> {
// check whether best_known_block_number provided, else use current best block // check whether best_known_block_number provided, else use current best block
let best_known_block_number = let best_known_block_number =
best_known_block_number.unwrap_or_else(|| <frame_system::Pallet<T>>::block_number()); best_known_block_number.unwrap_or_else(|| <frame_system::Pallet<T>>::block_number());
@@ -359,7 +343,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
} }
/// Return the on-chain MMR root hash. /// Return the on-chain MMR root hash.
pub fn mmr_root() -> <T as Config<I>>::Hash { pub fn mmr_root() -> HashOf<T, I> {
Self::mmr_root_hash() Self::mmr_root_hash()
} }
@@ -371,7 +355,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
/// or the proof is invalid. /// or the proof is invalid.
pub fn verify_leaves( pub fn verify_leaves(
leaves: Vec<LeafOf<T, I>>, leaves: Vec<LeafOf<T, I>>,
proof: primitives::Proof<<T as Config<I>>::Hash>, proof: primitives::Proof<HashOf<T, I>>,
) -> Result<(), primitives::Error> { ) -> Result<(), primitives::Error> {
if proof.leaf_count > Self::mmr_leaves() || if proof.leaf_count > Self::mmr_leaves() ||
proof.leaf_count == 0 || proof.leaf_count == 0 ||
@@ -21,7 +21,7 @@ use crate::{
Hasher, Node, NodeOf, Hasher, Node, NodeOf,
}, },
primitives::{self, Error, NodeIndex}, primitives::{self, Error, NodeIndex},
Config, HashingOf, Config, HashOf, HashingOf,
}; };
use sp_mmr_primitives::{mmr_lib, utils::NodesUtils}; use sp_mmr_primitives::{mmr_lib, utils::NodesUtils};
use sp_std::prelude::*; use sp_std::prelude::*;
@@ -95,7 +95,7 @@ where
pub fn verify_leaves_proof( pub fn verify_leaves_proof(
&self, &self,
leaves: Vec<L>, leaves: Vec<L>,
proof: primitives::Proof<<T as Config<I>>::Hash>, proof: primitives::Proof<HashOf<T, I>>,
) -> Result<bool, Error> { ) -> Result<bool, Error> {
let p = mmr_lib::MerkleProof::<NodeOf<T, I, L>, Hasher<HashingOf<T, I>, L>>::new( let p = mmr_lib::MerkleProof::<NodeOf<T, I, L>, Hasher<HashingOf<T, I>, L>>::new(
self.mmr.mmr_size(), self.mmr.mmr_size(),
@@ -145,7 +145,7 @@ where
/// Commit the changes to underlying storage, return current number of leaves and /// Commit the changes to underlying storage, return current number of leaves and
/// calculate the new MMR's root hash. /// calculate the new MMR's root hash.
pub fn finalize(self) -> Result<(NodeIndex, <T as Config<I>>::Hash), Error> { pub fn finalize(self) -> Result<(NodeIndex, HashOf<T, I>), Error> {
let root = self.mmr.get_root().map_err(|e| Error::GetRoot.log_error(e))?; 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))?; self.mmr.commit().map_err(|e| Error::Commit.log_error(e))?;
Ok((self.leaves, root.hash())) Ok((self.leaves, root.hash()))
@@ -166,7 +166,7 @@ where
pub fn generate_proof( pub fn generate_proof(
&self, &self,
leaf_indices: Vec<NodeIndex>, leaf_indices: Vec<NodeIndex>,
) -> Result<(Vec<L>, primitives::Proof<<T as Config<I>>::Hash>), Error> { ) -> Result<(Vec<L>, primitives::Proof<HashOf<T, I>>), Error> {
let positions = leaf_indices let positions = leaf_indices
.iter() .iter()
.map(|index| mmr_lib::leaf_index_to_pos(*index)) .map(|index| mmr_lib::leaf_index_to_pos(*index))
@@ -75,7 +75,6 @@ impl Config for Test {
const INDEXING_PREFIX: &'static [u8] = b"mmr-"; const INDEXING_PREFIX: &'static [u8] = b"mmr-";
type Hashing = Keccak256; type Hashing = Keccak256;
type Hash = H256;
type LeafData = Compact<Keccak256, (ParentNumberAndHash<Test>, LeafData)>; type LeafData = Compact<Keccak256, (ParentNumberAndHash<Test>, LeafData)>;
type OnNewRoot = (); type OnNewRoot = ();
type WeightInfo = (); type WeightInfo = ();
+2 -1
View File
@@ -18,7 +18,6 @@
//! Primitives for the runtime modules. //! Primitives for the runtime modules.
use crate::{ use crate::{
codec::{Codec, Decode, Encode, MaxEncodedLen},
generic::Digest, generic::Digest,
scale_info::{MetaType, StaticTypeInfo, TypeInfo}, scale_info::{MetaType, StaticTypeInfo, TypeInfo},
transaction_validity::{ transaction_validity::{
@@ -27,6 +26,7 @@ use crate::{
}, },
DispatchResult, DispatchResult,
}; };
use codec::{Codec, Decode, Encode, EncodeLike, MaxEncodedLen};
use impl_trait_for_tuples::impl_for_tuples; use impl_trait_for_tuples::impl_for_tuples;
#[cfg(feature = "serde")] #[cfg(feature = "serde")]
use serde::{de::DeserializeOwned, Deserialize, Serialize}; use serde::{de::DeserializeOwned, Deserialize, Serialize};
@@ -694,6 +694,7 @@ pub trait Hash:
+ Default + Default
+ Encode + Encode
+ Decode + Decode
+ EncodeLike
+ MaxEncodedLen + MaxEncodedLen
+ TypeInfo; + TypeInfo;