From 16761dc16ec7a208947abd8c89cab99c0acb2165 Mon Sep 17 00:00:00 2001 From: ron Date: Fri, 10 Jan 2025 15:37:48 +0800 Subject: [PATCH] Optimize imports --- .../frame/merkle-mountain-range/src/lib.rs | 50 +++++++++---------- .../merkle-mountain-range/src/mmr/mmr.rs | 26 +++++----- .../merkle-mountain-range/src/mmr/storage.rs | 12 ++--- 3 files changed, 44 insertions(+), 44 deletions(-) diff --git a/substrate/frame/merkle-mountain-range/src/lib.rs b/substrate/frame/merkle-mountain-range/src/lib.rs index 3d04fdf04a85..c46db68cb9c5 100644 --- a/substrate/frame/merkle-mountain-range/src/lib.rs +++ b/substrate/frame/merkle-mountain-range/src/lib.rs @@ -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::*; @@ -136,7 +137,7 @@ impl BenchmarkHelper for () { type ModuleMmr = mmr::Mmr>; /// Leaf data. -type LeafOf = <>::LeafData as primitives::LeafDataProvider>::LeafData; +type LeafOf = <>::LeafData as LeafDataProvider>::LeafData; /// Hashing used for the pallet. pub(crate) type HashingOf = >::Hashing; @@ -191,7 +192,7 @@ 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. /// @@ -199,7 +200,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>; + type OnNewRoot: OnNewRoot>; /// Block hash provider for a given block number. type BlockHashProvider: BlockHashProvider< @@ -241,9 +242,8 @@ pub mod pallet { #[pallet::hooks] impl, I: 'static> Hooks> for Pallet { fn on_initialize(_n: BlockNumberFor) -> Weight { - use primitives::LeafDataProvider; let leaves = NumberOfLeaves::::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 @@ -261,12 +261,12 @@ pub mod pallet { return T::WeightInfo::on_initialize(peaks_before as u32) }, }; - >::on_new_root(&root); + >::on_new_root(&root); NumberOfLeaves::::put(leaves); RootHash::::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) } @@ -283,28 +283,28 @@ pub mod pallet { pub fn verify_leaves_proof( root: H::Output, leaves: Vec>, - proof: primitives::LeafProof, -) -> Result<(), primitives::Error> + proof: LeafProof, +) -> Result<(), Error> where H: Hash, - L: primitives::FullLeaf, + L: FullLeaf, { let is_valid = mmr::verify_leaves_proof::(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( root: H::Output, - ancestry_proof: primitives::AncestryProof, + ancestry_proof: AncestryProof, ) -> Result where H: Hash, - L: primitives::FullLeaf, + L: FullLeaf, { mmr::verify_ancestry_proof::(root, ancestry_proof) .map_err(|_| Error::Verify.log_debug(("The ancestry proof is incorrect.", root))) @@ -376,7 +376,7 @@ impl, I: 'static> Pallet { pub fn generate_proof( block_numbers: Vec>, best_known_block_number: Option>, - ) -> Result<(Vec>, primitives::LeafProof>), primitives::Error> { + ) -> Result<(Vec>, LeafProof>), 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()); @@ -386,7 +386,7 @@ impl, I: 'static> Pallet { // we need to translate the block_numbers into leaf indices. let leaf_indices = block_numbers .iter() - .map(|block_num| -> Result { + .map(|block_num| -> Result { Self::block_num_to_leaf_index(*block_num) }) .collect::, _>>()?; @@ -403,14 +403,15 @@ impl, I: 'static> Pallet { /// or the proof is invalid. pub fn verify_leaves( leaves: Vec>, - proof: primitives::LeafProof>, - ) -> Result<(), primitives::Error> { + proof: LeafProof>, + ) -> Result<(), Error> { if proof.leaf_count > NumberOfLeaves::::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::Mmr::new(proof.leaf_count); @@ -418,14 +419,14 @@ impl, I: 'static> Pallet { 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, best_known_block_number: Option>, - ) -> Result>, Error> { + ) -> Result>, 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()); @@ -438,8 +439,7 @@ impl, I: 'static> Pallet { } #[cfg(feature = "runtime-benchmarks")] - pub fn generate_mock_ancestry_proof() -> Result>, Error> - { + pub fn generate_mock_ancestry_proof() -> Result>, Error> { let leaf_count = Self::block_num_to_leaf_count(>::block_number())?; let mmr: ModuleMmr = mmr::Mmr::new(leaf_count); mmr.generate_mock_ancestry_proof() @@ -447,7 +447,7 @@ impl, I: 'static> Pallet { pub fn verify_ancestry_proof( root: HashOf, - ancestry_proof: primitives::AncestryProof>, + ancestry_proof: AncestryProof>, ) -> Result, Error> { verify_ancestry_proof::, LeafOf>(root, ancestry_proof) } diff --git a/substrate/frame/merkle-mountain-range/src/mmr/mmr.rs b/substrate/frame/merkle-mountain-range/src/mmr/mmr.rs index 5dd5a9dcf38c..a9818ba47101 100644 --- a/substrate/frame/merkle-mountain-range/src/mmr/mmr.rs +++ b/substrate/frame/merkle-mountain-range/src/mmr/mmr.rs @@ -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, }; @@ -36,11 +36,11 @@ use frame::prelude::*; pub fn verify_leaves_proof( root: H::Output, leaves: Vec>, - proof: primitives::LeafProof, + proof: LeafProof, ) -> Result where H: Hash, - L: primitives::FullLeaf, + L: FullLeaf, { let size = NodesUtils::new(proof.leaf_count).size(); @@ -107,7 +107,7 @@ pub struct Mmr where T: Config, I: 'static, - L: primitives::FullLeaf, + L: FullLeaf, Storage: MMRStoreReadOps> + mmr_lib::MMRStoreWriteOps>, { @@ -119,7 +119,7 @@ impl Mmr where T: Config, I: 'static, - L: primitives::FullLeaf, + L: FullLeaf, Storage: MMRStoreReadOps> + mmr_lib::MMRStoreWriteOps>, { @@ -136,7 +136,7 @@ where pub fn verify_leaves_proof( &self, leaves: Vec, - proof: primitives::LeafProof>, + proof: LeafProof>, ) -> Result { let p = mmr_lib::MerkleProof::, Hasher, L>>::new( self.mmr.mmr_size(), @@ -170,7 +170,7 @@ impl Mmr where T: Config, I: 'static, - L: primitives::FullLeaf, + L: FullLeaf, { /// Push another item to the MMR. /// @@ -198,7 +198,7 @@ impl Mmr where T: Config, I: 'static, - L: primitives::FullLeaf + codec::Decode, + L: FullLeaf + codec::Decode, { /// Generate a proof for given leaf indices. /// @@ -207,7 +207,7 @@ where pub fn generate_proof( &self, leaf_indices: Vec, - ) -> Result<(Vec, primitives::LeafProof>), Error> { + ) -> Result<(Vec, LeafProof>), Error> { let positions = leaf_indices .iter() .map(|index| mmr_lib::leaf_index_to_pos(*index)) @@ -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(), @@ -236,14 +236,14 @@ where pub fn generate_ancestry_proof( &self, prev_leaf_count: LeafIndex, - ) -> Result>, Error> { + ) -> Result>, 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, diff --git a/substrate/frame/merkle-mountain-range/src/mmr/storage.rs b/substrate/frame/merkle-mountain-range/src/mmr/storage.rs index d6962d7f57d6..c201c0ea846d 100644 --- a/substrate/frame/merkle-mountain-range/src/mmr/storage.rs +++ b/substrate/frame/merkle-mountain-range/src/mmr/storage.rs @@ -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}; @@ -86,7 +86,7 @@ impl mmr_lib::MMRStoreReadOps> for Storage, I: 'static, - L: primitives::FullLeaf + codec::Decode, + L: FullLeaf + Decode, { fn get_elem(&self, pos: NodeIndex) -> mmr_lib::Result>> { // Find out which leaf added node `pos` in the MMR. @@ -124,7 +124,7 @@ impl mmr_lib::MMRStoreWriteOps> for Storage, I: 'static, - L: primitives::FullLeaf + codec::Decode, + L: FullLeaf + Decode, { fn append(&mut self, _: NodeIndex, _: Vec>) -> mmr_lib::Result<()> { panic!("MMR must not be altered in the off-chain context.") @@ -135,7 +135,7 @@ impl mmr_lib::MMRStoreReadOps> for Storage, I: 'static, - L: primitives::FullLeaf, + L: FullLeaf, { fn get_elem(&self, pos: NodeIndex) -> mmr_lib::Result>> { Ok(Nodes::::get(pos).map(Node::Hash)) @@ -146,7 +146,7 @@ impl mmr_lib::MMRStoreWriteOps> for Storage, I: 'static, - L: primitives::FullLeaf, + L: FullLeaf, { fn append(&mut self, pos: NodeIndex, elems: Vec>) -> mmr_lib::Result<()> { if elems.is_empty() { @@ -209,7 +209,7 @@ impl Storage where T: Config, I: 'static, - L: primitives::FullLeaf, + L: FullLeaf, { fn store_to_offchain( pos: NodeIndex,