Skip to content

Commit 7f95420

Browse files
authored
Merge pull request #2440 from subspace/subspace-mmr
Integrate Merkle Mountain range into Consensus runtime.
2 parents ca2f3f6 + 32caee6 commit 7f95420

File tree

17 files changed

+662
-11
lines changed

17 files changed

+662
-11
lines changed

Cargo.lock

Lines changed: 139 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
[package]
2+
name = "pallet-subspace-mmr"
3+
homepage = "https://subspace.network"
4+
repository = "https://github.com/subspace/subspace"
5+
description = "Primitives for Subspace MMR"
6+
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"
7+
version = "0.1.0"
8+
authors = ["Subspace Labs <https://subspace.network>"]
9+
edition = "2021"
10+
include = [
11+
"/src",
12+
"/Cargo.toml",
13+
]
14+
15+
[package.metadata.docs.rs]
16+
targets = ["x86_64-unknown-linux-gnu"]
17+
18+
[dependencies]
19+
codec = { package = "parity-scale-codec", version = "3.6.5", default-features = false, features = ["derive"] }
20+
frame-support = { version = "4.0.0-dev", default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "d6b500960579d73c43fc4ef550b703acfa61c4c8" }
21+
frame-system = { version = "4.0.0-dev", default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "d6b500960579d73c43fc4ef550b703acfa61c4c8" }
22+
scale-info = { version = "2.7.0", default-features = false, features = ["derive"] }
23+
sp-core = { version = "21.0.0", default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "d6b500960579d73c43fc4ef550b703acfa61c4c8" }
24+
sp-mmr-primitives = { version = "4.0.0-dev", default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "d6b500960579d73c43fc4ef550b703acfa61c4c8" }
25+
sp-runtime = { version = "24.0.0", default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "d6b500960579d73c43fc4ef550b703acfa61c4c8" }
26+
sp-std = { version = "8.0.0", default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "d6b500960579d73c43fc4ef550b703acfa61c4c8" }
27+
sp-subspace-mmr = { version = "0.1.0", default-features = false, path = "../sp-subspace-mmr" }
28+
29+
[features]
30+
default = ["std"]
31+
std = [
32+
"codec/std",
33+
"frame-support/std",
34+
"frame-system/std",
35+
"scale-info/std",
36+
"sp-core/std",
37+
"sp-mmr-primitives/std",
38+
"sp-runtime/std",
39+
"sp-std/std",
40+
"sp-subspace-mmr/std"
41+
]
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright (C) 2022 Subspace Labs, Inc.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
//! Pallet that provides necessary Leaf data for MMR.
17+
18+
#![cfg_attr(not(feature = "std"), no_std)]
19+
#![feature(associated_type_bounds)]
20+
21+
use frame_system::pallet_prelude::BlockNumberFor;
22+
pub use pallet::*;
23+
use sp_mmr_primitives::{LeafDataProvider, OnNewRoot};
24+
use sp_runtime::traits::{CheckedSub, One};
25+
use sp_runtime::DigestItem;
26+
use sp_subspace_mmr::subspace_mmr_runtime_interface::get_mmr_leaf_data;
27+
use sp_subspace_mmr::{LeafDataV0, MmrDigest, MmrLeaf};
28+
29+
#[frame_support::pallet]
30+
mod pallet {
31+
use frame_support::Parameter;
32+
use sp_core::H256;
33+
34+
#[pallet::pallet]
35+
pub struct Pallet<T>(_);
36+
37+
#[pallet::config]
38+
pub trait Config: frame_system::Config<Hash: Into<H256> + From<H256>> {
39+
type MmrRootHash: Parameter + Copy;
40+
}
41+
}
42+
43+
impl<T: Config> OnNewRoot<T::MmrRootHash> for Pallet<T> {
44+
fn on_new_root(root: &T::MmrRootHash) {
45+
let digest = DigestItem::new_mmr_root(*root);
46+
<frame_system::Pallet<T>>::deposit_log(digest);
47+
}
48+
}
49+
50+
impl<T: Config> LeafDataProvider for Pallet<T> {
51+
type LeafData = MmrLeaf<BlockNumberFor<T>, T::Hash>;
52+
53+
fn leaf_data() -> Self::LeafData {
54+
let block_number = frame_system::Pallet::<T>::block_number()
55+
.checked_sub(&One::one())
56+
.expect("`block_number` will always be >= 1; qed");
57+
let block_hash = frame_system::Pallet::<T>::parent_hash();
58+
let leaf_data = get_mmr_leaf_data(block_hash.into())
59+
.expect("leaf data for parent hash must always be derived; qed");
60+
MmrLeaf::V0(LeafDataV0 {
61+
block_number,
62+
block_hash,
63+
state_root: leaf_data.state_root.into(),
64+
extrinsics_root: leaf_data.extrinsics_root.into(),
65+
})
66+
}
67+
}

crates/sp-subspace-mmr/Cargo.toml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
[package]
2+
name = "sp-subspace-mmr"
3+
homepage = "https://subspace.network"
4+
repository = "https://github.com/subspace/subspace"
5+
description = "Primitives for Subspace MMR"
6+
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"
7+
version = "0.1.0"
8+
authors = ["Subspace Labs <https://subspace.network>"]
9+
edition = "2021"
10+
include = [
11+
"/src",
12+
"/Cargo.toml",
13+
]
14+
15+
[package.metadata.docs.rs]
16+
targets = ["x86_64-unknown-linux-gnu"]
17+
18+
[dependencies]
19+
codec = { package = "parity-scale-codec", version = "3.6.5", default-features = false, features = ["derive"] }
20+
scale-info = { version = "2.7.0", default-features = false, features = ["derive"] }
21+
sp-blockchain = { version = "4.0.0-dev", git = "https://github.com/subspace/polkadot-sdk", rev = "d6b500960579d73c43fc4ef550b703acfa61c4c8", optional = true }
22+
sp-core = { version = "21.0.0", default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "d6b500960579d73c43fc4ef550b703acfa61c4c8" }
23+
sp-externalities = { version = "0.19.0", default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "d6b500960579d73c43fc4ef550b703acfa61c4c8" }
24+
sp-runtime = { version = "24.0.0", default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "d6b500960579d73c43fc4ef550b703acfa61c4c8" }
25+
sp-runtime-interface = { version = "17.0.0", default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "d6b500960579d73c43fc4ef550b703acfa61c4c8" }
26+
sp-std = { version = "8.0.0", default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "d6b500960579d73c43fc4ef550b703acfa61c4c8" }
27+
28+
[features]
29+
default = ["std"]
30+
std = [
31+
"codec/std",
32+
"scale-info/std",
33+
"sp-blockchain",
34+
"sp-core/std",
35+
"sp-externalities/std",
36+
"sp-runtime/std",
37+
"sp-runtime-interface/std",
38+
"sp-std/std",
39+
]

0 commit comments

Comments
 (0)