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

Commit b2b90a6

Browse files
authored
pallet-merkle-mountain-range: Remove extra Hash type (#14214)
* pallet-merkle-mountain-range: Remove extra `Hash` type * FMT
1 parent 3c8666b commit b2b90a6

File tree

7 files changed

+20
-39
lines changed

7 files changed

+20
-39
lines changed

bin/node/runtime/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1453,7 +1453,6 @@ impl pallet_vesting::Config for Runtime {
14531453
impl pallet_mmr::Config for Runtime {
14541454
const INDEXING_PREFIX: &'static [u8] = b"mmr";
14551455
type Hashing = <Runtime as frame_system::Config>::Hashing;
1456-
type Hash = <Runtime as frame_system::Config>::Hash;
14571456
type LeafData = pallet_mmr::ParentNumberAndHash<Self>;
14581457
type OnNewRoot = ();
14591458
type WeightInfo = ();
@@ -1945,7 +1944,7 @@ mod mmr {
19451944
pub use pallet_mmr::primitives::*;
19461945

19471946
pub type Leaf = <<Runtime as pallet_mmr::Config>::LeafData as LeafDataProvider>::LeafData;
1948-
pub type Hash = <Runtime as pallet_mmr::Config>::Hash;
1947+
pub type Hash = <Hashing as sp_runtime::traits::Hash>::Output;
19491948
pub type Hashing = <Runtime as pallet_mmr::Config>::Hashing;
19501949
}
19511950

frame/beefy-mmr/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ where
5959
T: pallet_mmr::Config<Hash = sp_consensus_beefy::MmrRootHash>,
6060
T: pallet_beefy::Config,
6161
{
62-
fn on_new_root(root: &<T as pallet_mmr::Config>::Hash) {
62+
fn on_new_root(root: &sp_consensus_beefy::MmrRootHash) {
6363
let digest = sp_runtime::generic::DigestItem::Consensus(
6464
sp_consensus_beefy::BEEFY_ENGINE_ID,
6565
codec::Encode::encode(&sp_consensus_beefy::ConsensusLog::<
@@ -84,7 +84,7 @@ impl Convert<sp_consensus_beefy::crypto::AuthorityId, Vec<u8>> for BeefyEcdsaToE
8484
}
8585
}
8686

87-
type MerkleRootOf<T> = <T as pallet_mmr::Config>::Hash;
87+
type MerkleRootOf<T> = <<T as pallet_mmr::Config>::Hashing as sp_runtime::traits::Hash>::Output;
8888

8989
#[frame_support::pallet]
9090
pub mod pallet {

frame/beefy-mmr/src/mock.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use frame_support::{
2525
BasicExternalities,
2626
};
2727
use sp_consensus_beefy::mmr::MmrLeafVersion;
28-
use sp_core::{Hasher, H256};
28+
use sp_core::H256;
2929
use sp_runtime::{
3030
app_crypto::ecdsa::Public,
3131
impl_opaque_keys,
@@ -104,7 +104,7 @@ impl pallet_session::Config for Test {
104104
pub type MmrLeaf = sp_consensus_beefy::mmr::MmrLeaf<
105105
<Test as frame_system::Config>::BlockNumber,
106106
<Test as frame_system::Config>::Hash,
107-
<Test as pallet_mmr::Config>::Hash,
107+
crate::MerkleRootOf<Test>,
108108
Vec<u8>,
109109
>;
110110

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

114114
type Hashing = Keccak256;
115115

116-
type Hash = <Keccak256 as Hasher>::Out;
117-
118116
type LeafData = BeefyMmr;
119117

120118
type OnNewRoot = pallet_beefy_mmr::DepositBeefyDigest<Test>;

frame/merkle-mountain-range/src/lib.rs

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ type LeafOf<T, I> = <<T as Config<I>>::LeafData as primitives::LeafDataProvider>
113113

114114
/// Hashing used for the pallet.
115115
pub(crate) type HashingOf<T, I> = <T as Config<I>>::Hashing;
116+
/// Hash type used for the pallet.
117+
pub(crate) type HashOf<T, I> = <<T as Config<I>>::Hashing as traits::Hash>::Output;
116118

117119
#[frame_support::pallet]
118120
pub mod pallet {
@@ -146,24 +148,7 @@ pub mod pallet {
146148
///
147149
/// Then we create a tuple of these two hashes, SCALE-encode it (concatenate) and
148150
/// hash, to obtain a new MMR inner node - the new peak.
149-
type Hashing: traits::Hash<Output = <Self as Config<I>>::Hash>;
150-
151-
/// The hashing output type.
152-
///
153-
/// This type is actually going to be stored in the MMR.
154-
/// Required to be provided again, to satisfy trait bounds for storage items.
155-
type Hash: traits::Member
156-
+ traits::MaybeSerializeDeserialize
157-
+ sp_std::fmt::Debug
158-
+ sp_std::hash::Hash
159-
+ AsRef<[u8]>
160-
+ AsMut<[u8]>
161-
+ Copy
162-
+ Default
163-
+ codec::Codec
164-
+ codec::EncodeLike
165-
+ scale_info::TypeInfo
166-
+ MaxEncodedLen;
151+
type Hashing: traits::Hash;
167152

168153
/// Data stored in the leaf nodes.
169154
///
@@ -189,7 +174,7 @@ pub mod pallet {
189174
/// apart from having it in the storage. For instance you might output it in the header
190175
/// digest (see [`frame_system::Pallet::deposit_log`]) to make it available for Light
191176
/// Clients. Hook complexity should be `O(1)`.
192-
type OnNewRoot: primitives::OnNewRoot<<Self as Config<I>>::Hash>;
177+
type OnNewRoot: primitives::OnNewRoot<HashOf<Self, I>>;
193178

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

204188
/// Current size of the MMR (number of leaves).
205189
#[pallet::storage]
@@ -213,7 +197,7 @@ pub mod pallet {
213197
#[pallet::storage]
214198
#[pallet::getter(fn mmr_peak)]
215199
pub type Nodes<T: Config<I>, I: 'static = ()> =
216-
StorageMap<_, Identity, NodeIndex, <T as Config<I>>::Hash, OptionQuery>;
200+
StorageMap<_, Identity, NodeIndex, HashOf<T, I>, OptionQuery>;
217201

218202
#[pallet::hooks]
219203
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> {
338322
pub fn generate_proof(
339323
block_numbers: Vec<T::BlockNumber>,
340324
best_known_block_number: Option<T::BlockNumber>,
341-
) -> Result<(Vec<LeafOf<T, I>>, primitives::Proof<<T as Config<I>>::Hash>), primitives::Error> {
325+
) -> Result<(Vec<LeafOf<T, I>>, primitives::Proof<HashOf<T, I>>), primitives::Error> {
342326
// check whether best_known_block_number provided, else use current best block
343327
let best_known_block_number =
344328
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> {
359343
}
360344

361345
/// Return the on-chain MMR root hash.
362-
pub fn mmr_root() -> <T as Config<I>>::Hash {
346+
pub fn mmr_root() -> HashOf<T, I> {
363347
Self::mmr_root_hash()
364348
}
365349

@@ -371,7 +355,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
371355
/// or the proof is invalid.
372356
pub fn verify_leaves(
373357
leaves: Vec<LeafOf<T, I>>,
374-
proof: primitives::Proof<<T as Config<I>>::Hash>,
358+
proof: primitives::Proof<HashOf<T, I>>,
375359
) -> Result<(), primitives::Error> {
376360
if proof.leaf_count > Self::mmr_leaves() ||
377361
proof.leaf_count == 0 ||

frame/merkle-mountain-range/src/mmr/mmr.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use crate::{
2121
Hasher, Node, NodeOf,
2222
},
2323
primitives::{self, Error, NodeIndex},
24-
Config, HashingOf,
24+
Config, HashOf, HashingOf,
2525
};
2626
use sp_mmr_primitives::{mmr_lib, utils::NodesUtils};
2727
use sp_std::prelude::*;
@@ -95,7 +95,7 @@ where
9595
pub fn verify_leaves_proof(
9696
&self,
9797
leaves: Vec<L>,
98-
proof: primitives::Proof<<T as Config<I>>::Hash>,
98+
proof: primitives::Proof<HashOf<T, I>>,
9999
) -> Result<bool, Error> {
100100
let p = mmr_lib::MerkleProof::<NodeOf<T, I, L>, Hasher<HashingOf<T, I>, L>>::new(
101101
self.mmr.mmr_size(),
@@ -145,7 +145,7 @@ where
145145

146146
/// Commit the changes to underlying storage, return current number of leaves and
147147
/// calculate the new MMR's root hash.
148-
pub fn finalize(self) -> Result<(NodeIndex, <T as Config<I>>::Hash), Error> {
148+
pub fn finalize(self) -> Result<(NodeIndex, HashOf<T, I>), Error> {
149149
let root = self.mmr.get_root().map_err(|e| Error::GetRoot.log_error(e))?;
150150
self.mmr.commit().map_err(|e| Error::Commit.log_error(e))?;
151151
Ok((self.leaves, root.hash()))
@@ -166,7 +166,7 @@ where
166166
pub fn generate_proof(
167167
&self,
168168
leaf_indices: Vec<NodeIndex>,
169-
) -> Result<(Vec<L>, primitives::Proof<<T as Config<I>>::Hash>), Error> {
169+
) -> Result<(Vec<L>, primitives::Proof<HashOf<T, I>>), Error> {
170170
let positions = leaf_indices
171171
.iter()
172172
.map(|index| mmr_lib::leaf_index_to_pos(*index))

frame/merkle-mountain-range/src/mock.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ impl Config for Test {
7575
const INDEXING_PREFIX: &'static [u8] = b"mmr-";
7676

7777
type Hashing = Keccak256;
78-
type Hash = H256;
7978
type LeafData = Compact<Keccak256, (ParentNumberAndHash<Test>, LeafData)>;
8079
type OnNewRoot = ();
8180
type WeightInfo = ();

primitives/runtime/src/traits.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
//! Primitives for the runtime modules.
1919
2020
use crate::{
21-
codec::{Codec, Decode, Encode, MaxEncodedLen},
2221
generic::Digest,
2322
scale_info::{MetaType, StaticTypeInfo, TypeInfo},
2423
transaction_validity::{
@@ -27,6 +26,7 @@ use crate::{
2726
},
2827
DispatchResult,
2928
};
29+
use codec::{Codec, Decode, Encode, EncodeLike, MaxEncodedLen};
3030
use impl_trait_for_tuples::impl_for_tuples;
3131
#[cfg(feature = "serde")]
3232
use serde::{de::DeserializeOwned, Deserialize, Serialize};
@@ -694,6 +694,7 @@ pub trait Hash:
694694
+ Default
695695
+ Encode
696696
+ Decode
697+
+ EncodeLike
697698
+ MaxEncodedLen
698699
+ TypeInfo;
699700

0 commit comments

Comments
 (0)