From 569ce71e2c759b26601608f145d9b5efcb906919 Mon Sep 17 00:00:00 2001 From: Ron <yrong1997@gmail.com> Date: Mon, 20 Jan 2025 22:16:57 +0800 Subject: [PATCH] Migrate pallet-mmr to umbrella crate (#7081) Part of https://github.com/paritytech/polkadot-sdk/issues/6504 --- Cargo.lock | 8 +- prdoc/pr_7081.prdoc | 14 +++ .../frame/merkle-mountain-range/Cargo.toml | 24 +----- .../merkle-mountain-range/src/benchmarking.rs | 10 ++- .../src/default_weights.rs | 9 +- .../frame/merkle-mountain-range/src/lib.rs | 85 +++++++++---------- .../merkle-mountain-range/src/mmr/mmr.rs | 45 +++++----- .../merkle-mountain-range/src/mmr/mod.rs | 5 +- .../merkle-mountain-range/src/mmr/storage.rs | 38 +++++---- .../frame/merkle-mountain-range/src/mock.rs | 18 ++-- .../frame/merkle-mountain-range/src/tests.rs | 23 ++--- 11 files changed, 137 insertions(+), 142 deletions(-) create mode 100644 prdoc/pr_7081.prdoc diff --git a/Cargo.lock b/Cargo.lock index 0907830c5e7..50d36338cd2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -14147,18 +14147,12 @@ dependencies = [ name = "pallet-mmr" version = "27.0.0" dependencies = [ - "array-bytes", - "frame-benchmarking 28.0.0", - "frame-support 28.0.0", - "frame-system 28.0.0", "itertools 0.11.0", "log", "parity-scale-codec", + "polkadot-sdk-frame 0.1.0", "scale-info", - "sp-core 28.0.0", - "sp-io 30.0.0", "sp-mmr-primitives 26.0.0", - "sp-runtime 31.0.1", "sp-tracing 16.0.0", ] diff --git a/prdoc/pr_7081.prdoc b/prdoc/pr_7081.prdoc new file mode 100644 index 00000000000..be1d8aa6ee0 --- /dev/null +++ b/prdoc/pr_7081.prdoc @@ -0,0 +1,14 @@ +# Schema: Polkadot SDK PRDoc Schema (prdoc) v1.0.0 +# See doc at https://raw.githubusercontent.com/paritytech/polkadot-sdk/master/prdoc/schema_user.json + +title: '[pallet-mmr] Migrate to using frame umbrella crate' + +doc: + - audience: Runtime Dev + description: This PR migrates the pallet-mmr to use the frame umbrella crate. This + is part of the ongoing effort to migrate all pallets to use the frame umbrella crate. + The effort is tracked [here](https://github.com/paritytech/polkadot-sdk/issues/6504). + +crates: + - name: pallet-mmr + bump: minor diff --git a/substrate/frame/merkle-mountain-range/Cargo.toml b/substrate/frame/merkle-mountain-range/Cargo.toml index 04f5ab64100..ecbef01a920 100644 --- a/substrate/frame/merkle-mountain-range/Cargo.toml +++ b/substrate/frame/merkle-mountain-range/Cargo.toml @@ -16,18 +16,12 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] codec = { workspace = true } -frame-benchmarking = { optional = true, workspace = true } -frame-support = { workspace = true } -frame-system = { workspace = true } +frame = { workspace = true, features = ["runtime"] } log = { workspace = true } scale-info = { features = ["derive"], workspace = true } -sp-core = { workspace = true } -sp-io = { workspace = true } sp-mmr-primitives = { workspace = true } -sp-runtime = { workspace = true } [dev-dependencies] -array-bytes = { workspace = true, default-features = true } itertools = { workspace = true } sp-tracing = { workspace = true, default-features = true } @@ -35,24 +29,14 @@ sp-tracing = { workspace = true, default-features = true } default = ["std"] std = [ "codec/std", - "frame-benchmarking?/std", - "frame-support/std", - "frame-system/std", + "frame/std", "log/std", "scale-info/std", - "sp-core/std", - "sp-io/std", "sp-mmr-primitives/std", - "sp-runtime/std", ] runtime-benchmarks = [ - "frame-benchmarking/runtime-benchmarks", - "frame-support/runtime-benchmarks", - "frame-system/runtime-benchmarks", - "sp-runtime/runtime-benchmarks", + "frame/runtime-benchmarks", ] try-runtime = [ - "frame-support/try-runtime", - "frame-system/try-runtime", - "sp-runtime/try-runtime", + "frame/try-runtime", ] diff --git a/substrate/frame/merkle-mountain-range/src/benchmarking.rs b/substrate/frame/merkle-mountain-range/src/benchmarking.rs index 07afd9529eb..407f1f7ead6 100644 --- a/substrate/frame/merkle-mountain-range/src/benchmarking.rs +++ b/substrate/frame/merkle-mountain-range/src/benchmarking.rs @@ -20,8 +20,10 @@ #![cfg(feature = "runtime-benchmarks")] use crate::*; -use frame_benchmarking::v1::benchmarks_instance_pallet; -use frame_support::traits::OnInitialize; +use frame::{ + benchmarking::prelude::v1::benchmarks_instance_pallet, + deps::frame_support::traits::OnInitialize, +}; benchmarks_instance_pallet! { on_initialize { @@ -31,10 +33,10 @@ benchmarks_instance_pallet! { <<T as pallet::Config::<I>>::BenchmarkHelper as BenchmarkHelper>::setup(); for leaf in 0..(leaves - 1) { - Pallet::<T, I>::on_initialize((leaf as u32).into()); + <Pallet::<T, I> as OnInitialize<BlockNumberFor<T>>>::on_initialize((leaf as u32).into()); } }: { - Pallet::<T, I>::on_initialize((leaves as u32 - 1).into()); + <Pallet::<T, I> as OnInitialize<BlockNumberFor<T>>>::on_initialize((leaves as u32 - 1).into()); } verify { assert_eq!(crate::NumberOfLeaves::<T, I>::get(), leaves); } diff --git a/substrate/frame/merkle-mountain-range/src/default_weights.rs b/substrate/frame/merkle-mountain-range/src/default_weights.rs index b0ef0539018..d1ed12edd06 100644 --- a/substrate/frame/merkle-mountain-range/src/default_weights.rs +++ b/substrate/frame/merkle-mountain-range/src/default_weights.rs @@ -18,16 +18,13 @@ //! Default weights for the MMR Pallet //! This file was not auto-generated. -use frame_support::weights::{ - constants::{RocksDbWeight as DbWeight, WEIGHT_REF_TIME_PER_NANOS}, - Weight, -}; +use frame::{deps::frame_support::weights::constants::*, weights_prelude::*}; impl crate::WeightInfo for () { fn on_initialize(peaks: u32) -> Weight { let peaks = u64::from(peaks); // Reading the parent hash. - let leaf_weight = DbWeight::get().reads(1); + let leaf_weight = RocksDbWeight::get().reads(1); // Blake2 hash cost. let hash_weight = Weight::from_parts(2u64 * WEIGHT_REF_TIME_PER_NANOS, 0); // No-op hook. @@ -36,6 +33,6 @@ impl crate::WeightInfo for () { leaf_weight .saturating_add(hash_weight) .saturating_add(hook_weight) - .saturating_add(DbWeight::get().reads_writes(2 + peaks, 2 + peaks)) + .saturating_add(RocksDbWeight::get().reads_writes(2 + peaks, 2 + peaks)) } } diff --git a/substrate/frame/merkle-mountain-range/src/lib.rs b/substrate/frame/merkle-mountain-range/src/lib.rs index 7dfe95c8336..76d6c2a1ac7 100644 --- a/substrate/frame/merkle-mountain-range/src/lib.rs +++ b/substrate/frame/merkle-mountain-range/src/lib.rs @@ -59,20 +59,17 @@ extern crate alloc; use alloc::vec::Vec; -use frame_support::weights::Weight; -use frame_system::pallet_prelude::{BlockNumberFor, HeaderFor}; use log; -use sp_mmr_primitives::utils; -use sp_runtime::{ - traits::{self, One, Saturating}, - SaturatedConversion, -}; -pub use pallet::*; +use frame::prelude::*; + pub use sp_mmr_primitives::{ - self as primitives, utils::NodesUtils, Error, LeafDataProvider, LeafIndex, NodeIndex, + self as primitives, utils, utils::NodesUtils, AncestryProof, Error, FullLeaf, LeafDataProvider, + LeafIndex, LeafProof, NodeIndex, OnNewRoot, }; +pub use pallet::*; + #[cfg(feature = "runtime-benchmarks")] mod benchmarking; mod default_weights; @@ -90,11 +87,11 @@ mod tests; /// crate-local wrapper over [frame_system::Pallet]. Since the current block hash /// is not available (since the block is not finished yet), /// we use the `parent_hash` here along with parent block number. -pub struct ParentNumberAndHash<T: frame_system::Config> { - _phantom: core::marker::PhantomData<T>, +pub struct ParentNumberAndHash<T: Config> { + _phantom: PhantomData<T>, } -impl<T: frame_system::Config> LeafDataProvider for ParentNumberAndHash<T> { +impl<T: Config> LeafDataProvider for ParentNumberAndHash<T> { type LeafData = (BlockNumberFor<T>, <T as frame_system::Config>::Hash); fn leaf_data() -> Self::LeafData { @@ -111,13 +108,11 @@ pub trait BlockHashProvider<BlockNumber, BlockHash> { } /// Default implementation of BlockHashProvider using frame_system. -pub struct DefaultBlockHashProvider<T: frame_system::Config> { +pub struct DefaultBlockHashProvider<T: Config> { _phantom: core::marker::PhantomData<T>, } -impl<T: frame_system::Config> BlockHashProvider<BlockNumberFor<T>, T::Hash> - for DefaultBlockHashProvider<T> -{ +impl<T: Config> BlockHashProvider<BlockNumberFor<T>, T::Hash> for DefaultBlockHashProvider<T> { fn block_hash(block_number: BlockNumberFor<T>) -> T::Hash { frame_system::Pallet::<T>::block_hash(block_number) } @@ -142,17 +137,16 @@ 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; /// Hash type used for the pallet. -pub(crate) type HashOf<T, I> = <<T as Config<I>>::Hashing as traits::Hash>::Output; +pub(crate) type HashOf<T, I> = <<T as Config<I>>::Hashing as Hash>::Output; -#[frame_support::pallet] +#[frame::pallet] pub mod pallet { use super::*; - use frame_support::pallet_prelude::*; #[pallet::pallet] pub struct Pallet<T, I = ()>(PhantomData<(T, I)>); @@ -180,7 +174,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; + type Hashing: Hash; /// Data stored in the leaf nodes. /// @@ -198,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. /// @@ -206,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<HashOf<Self, I>>; + type OnNewRoot: OnNewRoot<HashOf<Self, I>>; /// Block hash provider for a given block number. type BlockHashProvider: BlockHashProvider< @@ -248,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 = sp_mmr_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 @@ -268,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 = sp_mmr_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) } @@ -290,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: traits::Hash, - L: primitives::FullLeaf, + H: Hash, + 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: traits::Hash, - L: primitives::FullLeaf, + H: Hash, + L: FullLeaf, { mmr::verify_ancestry_proof::<H, L>(root, ancestry_proof) .map_err(|_| Error::Verify.log_debug(("The ancestry proof is incorrect.", root))) @@ -383,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()); @@ -393,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>, _>>()?; @@ -410,14 +403,15 @@ 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); @@ -425,14 +419,14 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> { 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()); @@ -445,8 +439,7 @@ 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() @@ -454,7 +447,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> { 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) } diff --git a/substrate/frame/merkle-mountain-range/src/mmr/mmr.rs b/substrate/frame/merkle-mountain-range/src/mmr/mmr.rs index f9a4580b9bb..a9818ba4710 100644 --- a/substrate/frame/merkle-mountain-range/src/mmr/mmr.rs +++ b/substrate/frame/merkle-mountain-range/src/mmr/mmr.rs @@ -20,11 +20,14 @@ use crate::{ storage::{OffchainStorage, RuntimeStorage, Storage}, Hasher, Node, NodeOf, }, - primitives::{self, Error, NodeIndex}, + primitives::{ + mmr_lib, mmr_lib::MMRStoreReadOps, utils::NodesUtils, AncestryProof, Error, FullLeaf, + LeafIndex, LeafProof, NodeIndex, + }, Config, HashOf, HashingOf, }; use alloc::vec::Vec; -use sp_mmr_primitives::{mmr_lib, mmr_lib::MMRStoreReadOps, utils::NodesUtils, LeafIndex}; +use frame::prelude::*; /// Stateless verification of the proof for a batch of leaves. /// Note, the leaves should be sorted such that corresponding leaves and leaf indices have the @@ -33,11 +36,11 @@ use sp_mmr_primitives::{mmr_lib, mmr_lib::MMRStoreReadOps, utils::NodesUtils, Le 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: sp_runtime::traits::Hash, - L: primitives::FullLeaf, + H: Hash, + L: FullLeaf, { let size = NodesUtils::new(proof.leaf_count).size(); @@ -62,11 +65,11 @@ where 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: sp_runtime::traits::Hash, - L: primitives::FullLeaf, + H: Hash, + L: FullLeaf, { let mmr_size = NodesUtils::new(ancestry_proof.leaf_count).size(); @@ -104,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>>, { @@ -116,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>>, { @@ -133,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(), @@ -167,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. /// @@ -195,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. /// @@ -204,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)) @@ -222,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(), @@ -233,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, @@ -258,12 +261,10 @@ where /// The generated proof contains all the leafs in the MMR, so this way we can generate a proof /// with exactly `leaf_count` items. #[cfg(feature = "runtime-benchmarks")] - pub fn generate_mock_ancestry_proof( - &self, - ) -> Result<sp_mmr_primitives::AncestryProof<HashOf<T, I>>, Error> { + pub fn generate_mock_ancestry_proof(&self) -> Result<AncestryProof<HashOf<T, I>>, Error> { use crate::ModuleMmr; use alloc::vec; - use sp_mmr_primitives::mmr_lib::helper; + use mmr_lib::helper; let mmr: ModuleMmr<OffchainStorage, T, I> = Mmr::new(self.leaves); let store = <Storage<OffchainStorage, T, I, L>>::default(); @@ -289,7 +290,7 @@ where proof_items.push((leaf_pos, leaf)); } - Ok(sp_mmr_primitives::AncestryProof { + Ok(AncestryProof { prev_peaks, prev_leaf_count: self.leaves, leaf_count: self.leaves, diff --git a/substrate/frame/merkle-mountain-range/src/mmr/mod.rs b/substrate/frame/merkle-mountain-range/src/mmr/mod.rs index 5b73f53506e..85d00f8a65d 100644 --- a/substrate/frame/merkle-mountain-range/src/mmr/mod.rs +++ b/substrate/frame/merkle-mountain-range/src/mmr/mod.rs @@ -18,10 +18,9 @@ mod mmr; pub mod storage; -use sp_mmr_primitives::{mmr_lib, DataOrHash, FullLeaf}; -use sp_runtime::traits; - pub use self::mmr::{verify_ancestry_proof, verify_leaves_proof, Mmr}; +use crate::primitives::{mmr_lib, DataOrHash, FullLeaf}; +use frame::traits; /// Node type for runtime `T`. pub type NodeOf<T, I, L> = Node<<T as crate::Config<I>>::Hashing, L>; diff --git a/substrate/frame/merkle-mountain-range/src/mmr/storage.rs b/substrate/frame/merkle-mountain-range/src/mmr/storage.rs index 02852388b41..c201c0ea846 100644 --- a/substrate/frame/merkle-mountain-range/src/mmr/storage.rs +++ b/substrate/frame/merkle-mountain-range/src/mmr/storage.rs @@ -17,18 +17,22 @@ //! An MMR storage implementation. -use alloc::{vec, vec::Vec}; -use codec::Encode; -use core::iter::Peekable; -use log::{debug, trace}; -use sp_core::offchain::StorageKind; -use sp_mmr_primitives::{mmr_lib, mmr_lib::helper, utils::NodesUtils}; - use crate::{ mmr::{Node, NodeOf}, - primitives::{self, NodeIndex}, + primitives::{mmr_lib, mmr_lib::helper, utils::NodesUtils, FullLeaf, NodeIndex}, BlockHashProvider, Config, Nodes, NumberOfLeaves, Pallet, }; +use alloc::{vec, vec::Vec}; +use codec::Encode; +use core::iter::Peekable; +use frame::{ + deps::{ + sp_core::offchain::StorageKind, + sp_io::{offchain, offchain_index}, + }, + prelude::*, +}; +use log::{debug, trace}; /// A marker type for runtime-specific storage implementation. /// @@ -48,20 +52,20 @@ pub struct OffchainStorage; impl OffchainStorage { fn get(key: &[u8]) -> Option<Vec<u8>> { - sp_io::offchain::local_storage_get(StorageKind::PERSISTENT, &key) + offchain::local_storage_get(StorageKind::PERSISTENT, &key) } #[cfg(not(feature = "runtime-benchmarks"))] fn set<T: Config<I>, I: 'static>(key: &[u8], value: &[u8]) { - sp_io::offchain_index::set(key, value); + offchain_index::set(key, value); } #[cfg(feature = "runtime-benchmarks")] fn set<T: Config<I>, I: 'static>(key: &[u8], value: &[u8]) { if crate::pallet::UseLocalStorage::<T, I>::get() { - sp_io::offchain::local_storage_set(StorageKind::PERSISTENT, key, value); + offchain::local_storage_set(StorageKind::PERSISTENT, key, value); } else { - sp_io::offchain_index::set(key, value); + offchain_index::set(key, value); } } } @@ -82,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. @@ -120,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.") @@ -131,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)) @@ -142,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() { @@ -205,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, diff --git a/substrate/frame/merkle-mountain-range/src/mock.rs b/substrate/frame/merkle-mountain-range/src/mock.rs index 606719c6deb..4c234e0d94a 100644 --- a/substrate/frame/merkle-mountain-range/src/mock.rs +++ b/substrate/frame/merkle-mountain-range/src/mock.rs @@ -18,14 +18,20 @@ use crate as pallet_mmr; use crate::*; +use crate::{ + frame_system::DefaultConfig, + primitives::{Compact, LeafDataProvider}, +}; use codec::{Decode, Encode}; -use frame_support::{derive_impl, parameter_types}; -use sp_mmr_primitives::{Compact, LeafDataProvider}; -use sp_runtime::traits::Keccak256; +use frame::{ + deps::frame_support::derive_impl, + prelude::{frame_system, frame_system::config_preludes::TestDefaultConfig}, + testing_prelude::*, +}; -type Block = frame_system::mocking::MockBlock<Test>; +type Block = MockBlock<Test>; -frame_support::construct_runtime!( +construct_runtime!( pub enum Test { System: frame_system, @@ -33,7 +39,7 @@ frame_support::construct_runtime!( } ); -#[derive_impl(frame_system::config_preludes::TestDefaultConfig)] +#[derive_impl(TestDefaultConfig)] impl frame_system::Config for Test { type Block = Block; } diff --git a/substrate/frame/merkle-mountain-range/src/tests.rs b/substrate/frame/merkle-mountain-range/src/tests.rs index 93e3d06eaa0..ae0c58e91ab 100644 --- a/substrate/frame/merkle-mountain-range/src/tests.rs +++ b/substrate/frame/merkle-mountain-range/src/tests.rs @@ -17,19 +17,21 @@ use crate::{mock::*, *}; -use frame_support::traits::{Get, OnInitialize}; -use sp_core::{ - offchain::{testing::TestOffchainExt, OffchainDbExt, OffchainWorkerExt}, - H256, +use crate::primitives::{mmr_lib::helper, utils, Compact, LeafProof}; + +use frame::{ + deps::sp_core::{ + offchain::{testing::TestOffchainExt, OffchainDbExt, OffchainWorkerExt}, + H256, + }, + testing_prelude::*, }; -use sp_mmr_primitives::{mmr_lib::helper, utils, Compact, LeafProof}; -use sp_runtime::BuildStorage; -pub(crate) fn new_test_ext() -> sp_io::TestExternalities { +pub(crate) fn new_test_ext() -> TestState { frame_system::GenesisConfig::<Test>::default().build_storage().unwrap().into() } -fn register_offchain_ext(ext: &mut sp_io::TestExternalities) { +fn register_offchain_ext(ext: &mut TestState) { let (offchain, _offchain_state) = TestOffchainExt::with_offchain_db(ext.offchain_db()); ext.register_extension(OffchainDbExt::new(offchain.clone())); ext.register_extension(OffchainWorkerExt::new(offchain)); @@ -54,7 +56,7 @@ pub(crate) fn hex(s: &str) -> H256 { s.parse().unwrap() } -type BlockNumber = frame_system::pallet_prelude::BlockNumberFor<Test>; +type BlockNumber = BlockNumberFor<Test>; fn decode_node( v: Vec<u8>, @@ -517,7 +519,7 @@ fn should_verify() { } fn generate_and_verify_batch_proof( - ext: &mut sp_io::TestExternalities, + ext: &mut TestExternalities, block_numbers: &Vec<u64>, blocks_to_add: usize, ) { @@ -719,7 +721,6 @@ fn should_verify_on_the_next_block_since_there_is_no_pruning_yet() { #[test] fn should_verify_canonicalized() { - use frame_support::traits::Hooks; sp_tracing::init_for_tests(); // How deep is our fork-aware storage (in terms of blocks/leaves, nodes will be more). -- GitLab