Skip to content

Commit

Permalink
Optimize imports
Browse files Browse the repository at this point in the history
  • Loading branch information
yrong committed Jan 10, 2025
1 parent 565985b commit 16761dc
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 44 deletions.
50 changes: 25 additions & 25 deletions substrate/frame/merkle-mountain-range/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ use log;
pub use frame::prelude::*;

pub use sp_mmr_primitives::{
self as primitives, utils, utils::NodesUtils, Error, LeafDataProvider, LeafIndex, NodeIndex,
self as primitives, utils, utils::NodesUtils, AncestryProof, Error, FullLeaf, LeafDataProvider,
LeafIndex, LeafProof, NodeIndex, OnNewRoot,
};

pub use pallet::*;
Expand Down Expand Up @@ -136,7 +137,7 @@ impl BenchmarkHelper for () {
type ModuleMmr<StorageType, T, I> = mmr::Mmr<StorageType, T, I, LeafOf<T, I>>;

/// Leaf data.
type LeafOf<T, I> = <<T as Config<I>>::LeafData as primitives::LeafDataProvider>::LeafData;
type LeafOf<T, I> = <<T as Config<I>>::LeafData as LeafDataProvider>::LeafData;

/// Hashing used for the pallet.
pub(crate) type HashingOf<T, I> = <T as Config<I>>::Hashing;
Expand Down Expand Up @@ -191,15 +192,15 @@ pub mod pallet {
/// two forks with identical line of ancestors compete to write the same offchain key, but
/// that's fine as long as leaves only contain data coming from ancestors - conflicting
/// writes are identical).
type LeafData: primitives::LeafDataProvider;
type LeafData: LeafDataProvider;

/// A hook to act on the new MMR root.
///
/// For some applications it might be beneficial to make the MMR root available externally
/// 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<HashOf<Self, I>>;
type OnNewRoot: OnNewRoot<HashOf<Self, I>>;

/// Block hash provider for a given block number.
type BlockHashProvider: BlockHashProvider<
Expand Down Expand Up @@ -241,9 +242,8 @@ pub mod pallet {
#[pallet::hooks]
impl<T: Config<I>, I: 'static> Hooks<BlockNumberFor<T>> for Pallet<T, I> {
fn on_initialize(_n: BlockNumberFor<T>) -> Weight {
use primitives::LeafDataProvider;
let leaves = NumberOfLeaves::<T, I>::get();
let peaks_before = primitives::utils::NodesUtils::new(leaves).number_of_peaks();
let peaks_before = NodesUtils::new(leaves).number_of_peaks();
let data = T::LeafData::leaf_data();

// append new leaf to MMR
Expand All @@ -261,12 +261,12 @@ pub mod pallet {
return T::WeightInfo::on_initialize(peaks_before as u32)
},
};
<T::OnNewRoot as primitives::OnNewRoot<_>>::on_new_root(&root);
<T::OnNewRoot as OnNewRoot<_>>::on_new_root(&root);

NumberOfLeaves::<T, I>::put(leaves);
RootHash::<T, I>::put(root);

let peaks_after = primitives::utils::NodesUtils::new(leaves).number_of_peaks();
let peaks_after = NodesUtils::new(leaves).number_of_peaks();

T::WeightInfo::on_initialize(peaks_before.max(peaks_after) as u32)
}
Expand All @@ -283,28 +283,28 @@ pub mod pallet {
pub fn verify_leaves_proof<H, L>(
root: H::Output,
leaves: Vec<mmr::Node<H, L>>,
proof: primitives::LeafProof<H::Output>,
) -> Result<(), primitives::Error>
proof: LeafProof<H::Output>,
) -> Result<(), Error>
where
H: Hash,
L: primitives::FullLeaf,
L: FullLeaf,
{
let is_valid = mmr::verify_leaves_proof::<H, L>(root, leaves, proof)?;
if is_valid {
Ok(())
} else {
Err(primitives::Error::Verify.log_debug(("The proof is incorrect.", root)))
Err(Error::Verify.log_debug(("The proof is incorrect.", root)))
}
}

/// Stateless ancestry proof verification.
pub fn verify_ancestry_proof<H, L>(
root: H::Output,
ancestry_proof: primitives::AncestryProof<H::Output>,
ancestry_proof: AncestryProof<H::Output>,
) -> Result<H::Output, Error>
where
H: Hash,
L: primitives::FullLeaf,
L: FullLeaf,
{
mmr::verify_ancestry_proof::<H, L>(root, ancestry_proof)
.map_err(|_| Error::Verify.log_debug(("The ancestry proof is incorrect.", root)))
Expand Down Expand Up @@ -376,7 +376,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
pub fn generate_proof(
block_numbers: Vec<BlockNumberFor<T>>,
best_known_block_number: Option<BlockNumberFor<T>>,
) -> Result<(Vec<LeafOf<T, I>>, primitives::LeafProof<HashOf<T, I>>), primitives::Error> {
) -> Result<(Vec<LeafOf<T, I>>, LeafProof<HashOf<T, I>>), 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 @@ -386,7 +386,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
// we need to translate the block_numbers into leaf indices.
let leaf_indices = block_numbers
.iter()
.map(|block_num| -> Result<LeafIndex, primitives::Error> {
.map(|block_num| -> Result<LeafIndex, Error> {
Self::block_num_to_leaf_index(*block_num)
})
.collect::<Result<Vec<LeafIndex>, _>>()?;
Expand All @@ -403,29 +403,30 @@ 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::LeafProof<HashOf<T, I>>,
) -> Result<(), primitives::Error> {
proof: LeafProof<HashOf<T, I>>,
) -> Result<(), Error> {
if proof.leaf_count > NumberOfLeaves::<T, I>::get() ||
proof.leaf_count == 0 ||
proof.items.len().saturating_add(leaves.len()) as u64 > proof.leaf_count
{
return Err(primitives::Error::Verify
.log_debug("The proof has incorrect number of leaves or proof items."))
return Err(
Error::Verify.log_debug("The proof has incorrect number of leaves or proof items.")
)
}

let mmr: ModuleMmr<mmr::storage::OffchainStorage, T, I> = mmr::Mmr::new(proof.leaf_count);
let is_valid = mmr.verify_leaves_proof(leaves, proof)?;
if is_valid {
Ok(())
} else {
Err(primitives::Error::Verify.log_debug("The proof is incorrect."))
Err(Error::Verify.log_debug("The proof is incorrect."))
}
}

pub fn generate_ancestry_proof(
prev_block_number: BlockNumberFor<T>,
best_known_block_number: Option<BlockNumberFor<T>>,
) -> Result<primitives::AncestryProof<HashOf<T, I>>, Error> {
) -> Result<AncestryProof<HashOf<T, I>>, 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 @@ -438,16 +439,15 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
}

#[cfg(feature = "runtime-benchmarks")]
pub fn generate_mock_ancestry_proof() -> Result<primitives::AncestryProof<HashOf<T, I>>, Error>
{
pub fn generate_mock_ancestry_proof() -> Result<AncestryProof<HashOf<T, I>>, Error> {
let leaf_count = Self::block_num_to_leaf_count(<frame_system::Pallet<T>>::block_number())?;
let mmr: ModuleMmr<mmr::storage::OffchainStorage, T, I> = mmr::Mmr::new(leaf_count);
mmr.generate_mock_ancestry_proof()
}

pub fn verify_ancestry_proof(
root: HashOf<T, I>,
ancestry_proof: primitives::AncestryProof<HashOf<T, I>>,
ancestry_proof: AncestryProof<HashOf<T, I>>,
) -> Result<HashOf<T, I>, Error> {
verify_ancestry_proof::<HashingOf<T, I>, LeafOf<T, I>>(root, ancestry_proof)
}
Expand Down
26 changes: 13 additions & 13 deletions substrate/frame/merkle-mountain-range/src/mmr/mmr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ use crate::{
Hasher, Node, NodeOf,
},
primitives::{
self, mmr_lib, mmr_lib::MMRStoreReadOps, utils::NodesUtils, AncestryProof, Error, FullLeaf,
LeafIndex, NodeIndex,
mmr_lib, mmr_lib::MMRStoreReadOps, utils::NodesUtils, AncestryProof, Error, FullLeaf,
LeafIndex, LeafProof, NodeIndex,
},
Config, HashOf, HashingOf,
};
Expand All @@ -36,11 +36,11 @@ use frame::prelude::*;
pub fn verify_leaves_proof<H, L>(
root: H::Output,
leaves: Vec<Node<H, L>>,
proof: primitives::LeafProof<H::Output>,
proof: LeafProof<H::Output>,
) -> Result<bool, Error>
where
H: Hash,
L: primitives::FullLeaf,
L: FullLeaf,
{
let size = NodesUtils::new(proof.leaf_count).size();

Expand Down Expand Up @@ -107,7 +107,7 @@ pub struct Mmr<StorageType, T, I, L>
where
T: Config<I>,
I: 'static,
L: primitives::FullLeaf,
L: FullLeaf,
Storage<StorageType, T, I, L>:
MMRStoreReadOps<NodeOf<T, I, L>> + mmr_lib::MMRStoreWriteOps<NodeOf<T, I, L>>,
{
Expand All @@ -119,7 +119,7 @@ impl<StorageType, T, I, L> Mmr<StorageType, T, I, L>
where
T: Config<I>,
I: 'static,
L: primitives::FullLeaf,
L: FullLeaf,
Storage<StorageType, T, I, L>:
MMRStoreReadOps<NodeOf<T, I, L>> + mmr_lib::MMRStoreWriteOps<NodeOf<T, I, L>>,
{
Expand All @@ -136,7 +136,7 @@ where
pub fn verify_leaves_proof(
&self,
leaves: Vec<L>,
proof: primitives::LeafProof<HashOf<T, I>>,
proof: LeafProof<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 @@ -170,7 +170,7 @@ impl<T, I, L> Mmr<RuntimeStorage, T, I, L>
where
T: Config<I>,
I: 'static,
L: primitives::FullLeaf,
L: FullLeaf,
{
/// Push another item to the MMR.
///
Expand Down Expand Up @@ -198,7 +198,7 @@ impl<T, I, L> Mmr<OffchainStorage, T, I, L>
where
T: Config<I>,
I: 'static,
L: primitives::FullLeaf + codec::Decode,
L: FullLeaf + codec::Decode,
{
/// Generate a proof for given leaf indices.
///
Expand All @@ -207,7 +207,7 @@ where
pub fn generate_proof(
&self,
leaf_indices: Vec<NodeIndex>,
) -> Result<(Vec<L>, primitives::LeafProof<HashOf<T, I>>), Error> {
) -> Result<(Vec<L>, LeafProof<HashOf<T, I>>), Error> {
let positions = leaf_indices
.iter()
.map(|index| mmr_lib::leaf_index_to_pos(*index))
Expand All @@ -225,7 +225,7 @@ where
self.mmr
.gen_proof(positions)
.map_err(|e| Error::GenerateProof.log_error(e))
.map(|p| primitives::LeafProof {
.map(|p| LeafProof {
leaf_indices,
leaf_count,
items: p.proof_items().iter().map(|x| x.hash()).collect(),
Expand All @@ -236,14 +236,14 @@ where
pub fn generate_ancestry_proof(
&self,
prev_leaf_count: LeafIndex,
) -> Result<primitives::AncestryProof<HashOf<T, I>>, Error> {
) -> Result<AncestryProof<HashOf<T, I>>, Error> {
let prev_mmr_size = NodesUtils::new(prev_leaf_count).size();
let raw_ancestry_proof = self
.mmr
.gen_ancestry_proof(prev_mmr_size)
.map_err(|e| Error::GenerateProof.log_error(e))?;

Ok(primitives::AncestryProof {
Ok(AncestryProof {
prev_peaks: raw_ancestry_proof.prev_peaks.into_iter().map(|p| p.hash()).collect(),
prev_leaf_count,
leaf_count: self.leaves,
Expand Down
12 changes: 6 additions & 6 deletions substrate/frame/merkle-mountain-range/src/mmr/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
use crate::{
mmr::{Node, NodeOf},
primitives::{self, mmr_lib, mmr_lib::helper, utils::NodesUtils, NodeIndex},
primitives::{mmr_lib, mmr_lib::helper, utils::NodesUtils, FullLeaf, NodeIndex},
BlockHashProvider, Config, Nodes, NumberOfLeaves, Pallet,
};
use alloc::{vec, vec::Vec};
Expand Down Expand Up @@ -86,7 +86,7 @@ impl<T, I, L> mmr_lib::MMRStoreReadOps<NodeOf<T, I, L>> for Storage<OffchainStor
where
T: Config<I>,
I: 'static,
L: primitives::FullLeaf + codec::Decode,
L: FullLeaf + Decode,
{
fn get_elem(&self, pos: NodeIndex) -> mmr_lib::Result<Option<NodeOf<T, I, L>>> {
// Find out which leaf added node `pos` in the MMR.
Expand Down Expand Up @@ -124,7 +124,7 @@ impl<T, I, L> mmr_lib::MMRStoreWriteOps<NodeOf<T, I, L>> for Storage<OffchainSto
where
T: Config<I>,
I: 'static,
L: primitives::FullLeaf + codec::Decode,
L: FullLeaf + Decode,
{
fn append(&mut self, _: NodeIndex, _: Vec<NodeOf<T, I, L>>) -> mmr_lib::Result<()> {
panic!("MMR must not be altered in the off-chain context.")
Expand All @@ -135,7 +135,7 @@ impl<T, I, L> mmr_lib::MMRStoreReadOps<NodeOf<T, I, L>> for Storage<RuntimeStora
where
T: Config<I>,
I: 'static,
L: primitives::FullLeaf,
L: FullLeaf,
{
fn get_elem(&self, pos: NodeIndex) -> mmr_lib::Result<Option<NodeOf<T, I, L>>> {
Ok(Nodes::<T, I>::get(pos).map(Node::Hash))
Expand All @@ -146,7 +146,7 @@ impl<T, I, L> mmr_lib::MMRStoreWriteOps<NodeOf<T, I, L>> for Storage<RuntimeStor
where
T: Config<I>,
I: 'static,
L: primitives::FullLeaf,
L: FullLeaf,
{
fn append(&mut self, pos: NodeIndex, elems: Vec<NodeOf<T, I, L>>) -> mmr_lib::Result<()> {
if elems.is_empty() {
Expand Down Expand Up @@ -209,7 +209,7 @@ impl<T, I, L> Storage<RuntimeStorage, T, I, L>
where
T: Config<I>,
I: 'static,
L: primitives::FullLeaf,
L: FullLeaf,
{
fn store_to_offchain(
pos: NodeIndex,
Expand Down

0 comments on commit 16761dc

Please sign in to comment.