Skip to content

Commit

Permalink
Merge pull request #2440 from subspace/subspace-mmr
Browse files Browse the repository at this point in the history
Integrate Merkle Mountain range into Consensus runtime.
  • Loading branch information
vedhavyas authored Jan 27, 2024
2 parents ca2f3f6 + 32caee6 commit 7f95420
Show file tree
Hide file tree
Showing 17 changed files with 662 additions and 11 deletions.
139 changes: 139 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions crates/pallet-subspace-mmr/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
[package]
name = "pallet-subspace-mmr"
homepage = "https://subspace.network"
repository = "https://github.com/subspace/subspace"
description = "Primitives for Subspace MMR"
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"
version = "0.1.0"
authors = ["Subspace Labs <https://subspace.network>"]
edition = "2021"
include = [
"/src",
"/Cargo.toml",
]

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
codec = { package = "parity-scale-codec", version = "3.6.5", default-features = false, features = ["derive"] }
frame-support = { version = "4.0.0-dev", default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "d6b500960579d73c43fc4ef550b703acfa61c4c8" }
frame-system = { version = "4.0.0-dev", default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "d6b500960579d73c43fc4ef550b703acfa61c4c8" }
scale-info = { version = "2.7.0", default-features = false, features = ["derive"] }
sp-core = { version = "21.0.0", default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "d6b500960579d73c43fc4ef550b703acfa61c4c8" }
sp-mmr-primitives = { version = "4.0.0-dev", default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "d6b500960579d73c43fc4ef550b703acfa61c4c8" }
sp-runtime = { version = "24.0.0", default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "d6b500960579d73c43fc4ef550b703acfa61c4c8" }
sp-std = { version = "8.0.0", default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "d6b500960579d73c43fc4ef550b703acfa61c4c8" }
sp-subspace-mmr = { version = "0.1.0", default-features = false, path = "../sp-subspace-mmr" }

[features]
default = ["std"]
std = [
"codec/std",
"frame-support/std",
"frame-system/std",
"scale-info/std",
"sp-core/std",
"sp-mmr-primitives/std",
"sp-runtime/std",
"sp-std/std",
"sp-subspace-mmr/std"
]
67 changes: 67 additions & 0 deletions crates/pallet-subspace-mmr/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright (C) 2022 Subspace Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Pallet that provides necessary Leaf data for MMR.

#![cfg_attr(not(feature = "std"), no_std)]
#![feature(associated_type_bounds)]

use frame_system::pallet_prelude::BlockNumberFor;
pub use pallet::*;
use sp_mmr_primitives::{LeafDataProvider, OnNewRoot};
use sp_runtime::traits::{CheckedSub, One};
use sp_runtime::DigestItem;
use sp_subspace_mmr::subspace_mmr_runtime_interface::get_mmr_leaf_data;
use sp_subspace_mmr::{LeafDataV0, MmrDigest, MmrLeaf};

#[frame_support::pallet]
mod pallet {
use frame_support::Parameter;
use sp_core::H256;

#[pallet::pallet]
pub struct Pallet<T>(_);

#[pallet::config]
pub trait Config: frame_system::Config<Hash: Into<H256> + From<H256>> {
type MmrRootHash: Parameter + Copy;
}
}

impl<T: Config> OnNewRoot<T::MmrRootHash> for Pallet<T> {
fn on_new_root(root: &T::MmrRootHash) {
let digest = DigestItem::new_mmr_root(*root);
<frame_system::Pallet<T>>::deposit_log(digest);
}
}

impl<T: Config> LeafDataProvider for Pallet<T> {
type LeafData = MmrLeaf<BlockNumberFor<T>, T::Hash>;

fn leaf_data() -> Self::LeafData {
let block_number = frame_system::Pallet::<T>::block_number()
.checked_sub(&One::one())
.expect("`block_number` will always be >= 1; qed");
let block_hash = frame_system::Pallet::<T>::parent_hash();
let leaf_data = get_mmr_leaf_data(block_hash.into())
.expect("leaf data for parent hash must always be derived; qed");
MmrLeaf::V0(LeafDataV0 {
block_number,
block_hash,
state_root: leaf_data.state_root.into(),
extrinsics_root: leaf_data.extrinsics_root.into(),
})
}
}
39 changes: 39 additions & 0 deletions crates/sp-subspace-mmr/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
[package]
name = "sp-subspace-mmr"
homepage = "https://subspace.network"
repository = "https://github.com/subspace/subspace"
description = "Primitives for Subspace MMR"
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"
version = "0.1.0"
authors = ["Subspace Labs <https://subspace.network>"]
edition = "2021"
include = [
"/src",
"/Cargo.toml",
]

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
codec = { package = "parity-scale-codec", version = "3.6.5", default-features = false, features = ["derive"] }
scale-info = { version = "2.7.0", default-features = false, features = ["derive"] }
sp-blockchain = { version = "4.0.0-dev", git = "https://github.com/subspace/polkadot-sdk", rev = "d6b500960579d73c43fc4ef550b703acfa61c4c8", optional = true }
sp-core = { version = "21.0.0", default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "d6b500960579d73c43fc4ef550b703acfa61c4c8" }
sp-externalities = { version = "0.19.0", default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "d6b500960579d73c43fc4ef550b703acfa61c4c8" }
sp-runtime = { version = "24.0.0", default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "d6b500960579d73c43fc4ef550b703acfa61c4c8" }
sp-runtime-interface = { version = "17.0.0", default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "d6b500960579d73c43fc4ef550b703acfa61c4c8" }
sp-std = { version = "8.0.0", default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "d6b500960579d73c43fc4ef550b703acfa61c4c8" }

[features]
default = ["std"]
std = [
"codec/std",
"scale-info/std",
"sp-blockchain",
"sp-core/std",
"sp-externalities/std",
"sp-runtime/std",
"sp-runtime-interface/std",
"sp-std/std",
]
Loading

0 comments on commit 7f95420

Please sign in to comment.