Migrate pallet-mmr to the new pallet attribute macro (#9181)

* Migrate pallet-mmr to the new pallet attribute macro

Signed-off-by: koushiro <koushiro.cqx@gmail.com>

* fix typo

Signed-off-by: koushiro <koushiro.cqx@gmail.com>

* use instance macro

Co-authored-by: thiolliere <gui.thiolliere@gmail.com>
This commit is contained in:
Qinxuan Chen
2021-07-15 05:30:31 +08:00
committed by GitHub
parent de2fdd15cb
commit 2e986dd352
11 changed files with 149 additions and 125 deletions
@@ -16,7 +16,7 @@
// limitations under the License.
use crate::{
Config, HashingOf, Instance,
Config, HashingOf,
mmr::{
Node, NodeOf, Hasher,
storage::{Storage, OffchainStorage, RuntimeStorage},
@@ -58,7 +58,7 @@ pub fn verify_leaf_proof<H, L>(
/// vs [Off-chain](crate::mmr::storage::OffchainStorage)).
pub struct Mmr<StorageType, T, I, L> where
T: Config<I>,
I: Instance,
I: 'static,
L: primitives::FullLeaf,
Storage<StorageType, T, I, L>: mmr_lib::MMRStore<NodeOf<T, I, L>>,
{
@@ -72,7 +72,7 @@ pub struct Mmr<StorageType, T, I, L> where
impl<StorageType, T, I, L> Mmr<StorageType, T, I, L> where
T: Config<I>,
I: Instance,
I: 'static,
L: primitives::FullLeaf,
Storage<StorageType, T, I, L>: mmr_lib::MMRStore<NodeOf<T, I, L>>,
{
@@ -116,7 +116,7 @@ impl<StorageType, T, I, L> Mmr<StorageType, T, I, L> where
/// Runtime specific MMR functions.
impl<T, I, L> Mmr<RuntimeStorage, T, I, L> where
T: Config<I>,
I: Instance,
I: 'static,
L: primitives::FullLeaf,
{
@@ -145,7 +145,7 @@ impl<T, I, L> Mmr<RuntimeStorage, T, I, L> where
/// Off-chain specific MMR functions.
impl<T, I, L> Mmr<OffchainStorage, T, I, L> where
T: Config<I>,
I: Instance,
I: 'static,
L: primitives::FullLeaf + codec::Decode,
{
/// Generate a proof for given leaf index.
@@ -18,12 +18,12 @@
//! A MMR storage implementations.
use codec::Encode;
use crate::mmr::{NodeOf, Node};
use crate::{NumberOfLeaves, Nodes, Module, Config, Instance, primitives};
use frame_support::{StorageMap, StorageValue};
#[cfg(not(feature = "std"))]
use sp_std::prelude::Vec;
use crate::mmr::{NodeOf, Node};
use crate::{NumberOfLeaves, Nodes, Pallet, Config, primitives};
/// A marker type for runtime-specific storage implementation.
///
/// Allows appending new items to the MMR and proof verification.
@@ -56,11 +56,11 @@ impl<StorageType, T, I, L> Default for Storage<StorageType, T, I, L> {
impl<T, I, L> mmr_lib::MMRStore<NodeOf<T, I, L>> for Storage<OffchainStorage, T, I, L> where
T: Config<I>,
I: Instance,
I: 'static,
L: primitives::FullLeaf + codec::Decode,
{
fn get_elem(&self, pos: u64) -> mmr_lib::Result<Option<NodeOf<T, I, L>>> {
let key = Module::<T, I>::offchain_key(pos);
let key = Pallet::<T, I>::offchain_key(pos);
// Retrieve the element from Off-chain DB.
Ok(sp_io::offchain
::local_storage_get(sp_core::offchain::StorageKind::PERSISTENT, &key)
@@ -74,7 +74,7 @@ impl<T, I, L> mmr_lib::MMRStore<NodeOf<T, I, L>> for Storage<OffchainStorage, T,
impl<T, I, L> mmr_lib::MMRStore<NodeOf<T, I, L>> for Storage<RuntimeStorage, T, I, L> where
T: Config<I>,
I: Instance,
I: 'static,
L: primitives::FullLeaf,
{
fn get_elem(&self, pos: u64) -> mmr_lib::Result<Option<NodeOf<T, I, L>>> {
@@ -84,7 +84,7 @@ impl<T, I, L> mmr_lib::MMRStore<NodeOf<T, I, L>> for Storage<RuntimeStorage, T,
}
fn append(&mut self, pos: u64, elems: Vec<NodeOf<T, I, L>>) -> mmr_lib::Result<()> {
let mut leaves = crate::NumberOfLeaves::<I>::get();
let mut leaves = crate::NumberOfLeaves::<T, I>::get();
let mut size = crate::mmr::utils::NodesUtils::new(leaves).size();
if pos != size {
return Err(mmr_lib::Error::InconsistentStore);
@@ -94,7 +94,7 @@ impl<T, I, L> mmr_lib::MMRStore<NodeOf<T, I, L>> for Storage<RuntimeStorage, T,
// on-chain we only store the hash (even if it's a leaf)
<Nodes<T, I>>::insert(size, elem.hash());
// Indexing API is used to store the full leaf content.
let key = Module::<T, I>::offchain_key(size);
let key = Pallet::<T, I>::offchain_key(size);
elem.using_encoded(|elem| sp_io::offchain_index::set(&key, elem));
size += 1;
@@ -103,7 +103,7 @@ impl<T, I, L> mmr_lib::MMRStore<NodeOf<T, I, L>> for Storage<RuntimeStorage, T,
}
}
NumberOfLeaves::<I>::put(leaves);
NumberOfLeaves::<T, I>::put(leaves);
Ok(())
}
@@ -114,7 +114,7 @@ mod tests {
let mut mmr = crate::mmr::Mmr::<
crate::mmr::storage::RuntimeStorage,
crate::mock::Test,
crate::DefaultInstance,
_,
_,
>::new(0);
for i in 0..*s {