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

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

Merged
merged 2 commits into from
May 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <Runtime as frame_system::Config>::Hashing;
type Hash = <Runtime as frame_system::Config>::Hash;
type LeafData = pallet_mmr::ParentNumberAndHash<Self>;
type OnNewRoot = ();
type WeightInfo = ();
Expand Down Expand Up @@ -1945,7 +1944,7 @@ mod mmr {
pub use pallet_mmr::primitives::*;

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;
}

Expand Down
4 changes: 2 additions & 2 deletions frame/beefy-mmr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ where
T: pallet_mmr::Config<Hash = sp_consensus_beefy::MmrRootHash>,
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(
sp_consensus_beefy::BEEFY_ENGINE_ID,
codec::Encode::encode(&sp_consensus_beefy::ConsensusLog::<
Expand All @@ -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]
pub mod pallet {
Expand Down
6 changes: 2 additions & 4 deletions frame/beefy-mmr/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -104,7 +104,7 @@ impl pallet_session::Config for Test {
pub type MmrLeaf = sp_consensus_beefy::mmr::MmrLeaf<
<Test as frame_system::Config>::BlockNumber,
<Test as frame_system::Config>::Hash,
<Test as pallet_mmr::Config>::Hash,
crate::MerkleRootOf<Test>,
Vec<u8>,
>;

Expand All @@ -113,8 +113,6 @@ impl pallet_mmr::Config for Test {

type Hashing = Keccak256;

type Hash = <Keccak256 as Hasher>::Out;

type LeafData = BeefyMmr;

type OnNewRoot = pallet_beefy_mmr::DepositBeefyDigest<Test>;
Expand Down
34 changes: 9 additions & 25 deletions frame/merkle-mountain-range/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ type LeafOf<T, I> = <<T as Config<I>>::LeafData as primitives::LeafDataProvider>

/// Hashing used for the pallet.
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]
pub mod pallet {
Expand Down Expand Up @@ -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<Output = <Self as Config<I>>::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.
///
Expand All @@ -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<<Self as Config<I>>::Hash>;
type OnNewRoot: primitives::OnNewRoot<HashOf<Self, I>>;

/// Weights for this pallet.
type WeightInfo: WeightInfo;
Expand All @@ -198,8 +183,7 @@ pub mod pallet {
/// Latest MMR Root hash.
#[pallet::storage]
#[pallet::getter(fn mmr_root_hash)]
pub type RootHash<T: Config<I>, I: 'static = ()> =
StorageValue<_, <T as Config<I>>::Hash, ValueQuery>;
pub type RootHash<T: Config<I>, I: 'static = ()> = StorageValue<_, HashOf<T, I>, ValueQuery>;

/// Current size of the MMR (number of leaves).
#[pallet::storage]
Expand All @@ -213,7 +197,7 @@ pub mod pallet {
#[pallet::storage]
#[pallet::getter(fn mmr_peak)]
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]
impl<T: Config<I>, I: 'static> Hooks<BlockNumberFor<T>> for Pallet<T, I> {
Expand Down Expand Up @@ -338,7 +322,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
pub fn generate_proof(
block_numbers: Vec<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
let best_known_block_number =
best_known_block_number.unwrap_or_else(|| <frame_system::Pallet<T>>::block_number());
Expand All @@ -359,7 +343,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
}

/// 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()
}

Expand All @@ -371,7 +355,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
/// or the proof is invalid.
pub fn verify_leaves(
leaves: Vec<LeafOf<T, I>>,
proof: primitives::Proof<<T as Config<I>>::Hash>,
proof: primitives::Proof<HashOf<T, I>>,
) -> Result<(), primitives::Error> {
if proof.leaf_count > Self::mmr_leaves() ||
proof.leaf_count == 0 ||
Expand Down
8 changes: 4 additions & 4 deletions frame/merkle-mountain-range/src/mmr/mmr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down Expand Up @@ -95,7 +95,7 @@ where
pub fn verify_leaves_proof(
&self,
leaves: Vec<L>,
proof: primitives::Proof<<T as Config<I>>::Hash>,
proof: primitives::Proof<HashOf<T, I>>,
) -> Result<bool, Error> {
let p = mmr_lib::MerkleProof::<NodeOf<T, I, L>, Hasher<HashingOf<T, I>, L>>::new(
self.mmr.mmr_size(),
Expand Down Expand Up @@ -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, <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))?;
self.mmr.commit().map_err(|e| Error::Commit.log_error(e))?;
Ok((self.leaves, root.hash()))
Expand All @@ -166,7 +166,7 @@ where
pub fn generate_proof(
&self,
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
.iter()
.map(|index| mmr_lib::leaf_index_to_pos(*index))
Expand Down
1 change: 0 additions & 1 deletion frame/merkle-mountain-range/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ impl Config for Test {
const INDEXING_PREFIX: &'static [u8] = b"mmr-";

type Hashing = Keccak256;
type Hash = H256;
type LeafData = Compact<Keccak256, (ParentNumberAndHash<Test>, LeafData)>;
type OnNewRoot = ();
type WeightInfo = ();
Expand Down
3 changes: 2 additions & 1 deletion primitives/runtime/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -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};
Expand Down Expand Up @@ -694,6 +694,7 @@ pub trait Hash:
+ Default
+ Encode
+ Decode
+ EncodeLike
+ MaxEncodedLen
+ TypeInfo;

Expand Down