Skip to content

Commit 32caee6

Browse files
committed
update host fn to panic if header is not available and update misleading comment and some minor nits
1 parent f6eb400 commit 32caee6

File tree

9 files changed

+15
-21
lines changed

9 files changed

+15
-21
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/pallet-subspace-mmr/Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ targets = ["x86_64-unknown-linux-gnu"]
1919
codec = { package = "parity-scale-codec", version = "3.6.5", default-features = false, features = ["derive"] }
2020
frame-support = { version = "4.0.0-dev", default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "d6b500960579d73c43fc4ef550b703acfa61c4c8" }
2121
frame-system = { version = "4.0.0-dev", default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "d6b500960579d73c43fc4ef550b703acfa61c4c8" }
22-
log = { version = "0.4.20", default-features = false }
2322
scale-info = { version = "2.7.0", default-features = false, features = ["derive"] }
2423
sp-core = { version = "21.0.0", default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "d6b500960579d73c43fc4ef550b703acfa61c4c8" }
2524
sp-mmr-primitives = { version = "4.0.0-dev", default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "d6b500960579d73c43fc4ef550b703acfa61c4c8" }
@@ -33,7 +32,6 @@ std = [
3332
"codec/std",
3433
"frame-support/std",
3534
"frame-system/std",
36-
"log/std",
3735
"scale-info/std",
3836
"sp-core/std",
3937
"sp-mmr-primitives/std",

crates/pallet-subspace-mmr/src/lib.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,10 @@
1919
#![feature(associated_type_bounds)]
2020

2121
use frame_system::pallet_prelude::BlockNumberFor;
22-
use log::error;
2322
pub use pallet::*;
2423
use sp_mmr_primitives::{LeafDataProvider, OnNewRoot};
25-
use sp_runtime::traits::One;
26-
use sp_runtime::{DigestItem, Saturating};
24+
use sp_runtime::traits::{CheckedSub, One};
25+
use sp_runtime::DigestItem;
2726
use sp_subspace_mmr::subspace_mmr_runtime_interface::get_mmr_leaf_data;
2827
use sp_subspace_mmr::{LeafDataV0, MmrDigest, MmrLeaf};
2928

@@ -52,16 +51,12 @@ impl<T: Config> LeafDataProvider for Pallet<T> {
5251
type LeafData = MmrLeaf<BlockNumberFor<T>, T::Hash>;
5352

5453
fn leaf_data() -> Self::LeafData {
55-
let block_number = frame_system::Pallet::<T>::block_number().saturating_sub(One::one());
54+
let block_number = frame_system::Pallet::<T>::block_number()
55+
.checked_sub(&One::one())
56+
.expect("`block_number` will always be >= 1; qed");
5657
let block_hash = frame_system::Pallet::<T>::parent_hash();
57-
// unfortunately, we Leaf data provider trait expects the impl to be infallible
58-
// but our host function might fail for any reason but practically shouldn't since
59-
// we are querying the immediate parent block
60-
// We will just log an error in case of such ever happening
61-
let leaf_data = get_mmr_leaf_data(block_hash.into()).unwrap_or_else(|| {
62-
error!(target: "runtime::subspace_mmr", "Failed to fetch leaf data for Block hash{block_hash:?}");
63-
Default::default()
64-
});
58+
let leaf_data = get_mmr_leaf_data(block_hash.into())
59+
.expect("leaf data for parent hash must always be derived; qed");
6560
MmrLeaf::V0(LeafDataV0 {
6661
block_number,
6762
block_hash,

crates/sp-subspace-mmr/src/host_functions.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,9 @@ where
4747
let header = self
4848
.consensus_client
4949
.header(consensus_block_hash.into())
50-
.ok()
51-
.flatten()?;
50+
.expect(
51+
"Database error is fatal in host function, there is no recovery from this; qed",
52+
)?;
5253

5354
Some(LeafData {
5455
state_root: H256::from_slice(header.state_root().as_ref()),

crates/sp-subspace-mmr/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pub enum MmrLeaf<BlockNumber, Hash> {
4141
pub struct LeafDataV0<BlockNumber, Hash> {
4242
pub block_number: BlockNumber,
4343
pub block_hash: Hash,
44-
/// Can be used to prove specific storage after block was imported
44+
/// Can be used to prove specific storage after block was pruned
4545
pub state_root: Hash,
4646
/// Can be used to prove block body
4747
pub extrinsics_root: Hash,

crates/sp-subspace-mmr/src/runtime_interface.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use sp_runtime_interface::runtime_interface;
1111
#[runtime_interface]
1212
pub trait SubspaceMmrRuntimeInterface {
1313
/// Returns the MMR leaf.
14-
#[allow(dead_code)]
1514
fn get_mmr_leaf_data(&mut self, consensus_block_hash: H256) -> Option<LeafData> {
1615
self.extension::<SubspaceMmrExtension>()
1716
.expect("No `SubspaceMmrExtension` associated for the current context!")

crates/subspace-runtime/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ runtime-benchmarks = [
132132
"orml-vesting/runtime-benchmarks",
133133
"pallet-balances/runtime-benchmarks",
134134
"pallet-domains/runtime-benchmarks",
135+
"pallet-mmr/runtime-benchmarks",
135136
"pallet-runtime-configs/runtime-benchmarks",
136137
"pallet-subspace/runtime-benchmarks",
137138
"pallet-timestamp/runtime-benchmarks",

crates/subspace-runtime/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,7 @@ mod benches {
802802
[frame_system, SystemBench::<Runtime>]
803803
[pallet_balances, Balances]
804804
[pallet_domains, Domains]
805+
[pallet_mmr, Mmr]
805806
[pallet_runtime_configs, RuntimeConfigs]
806807
[pallet_subspace, Subspace]
807808
[pallet_timestamp, Timestamp]

crates/subspace-service/src/config.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,8 @@ impl From<SubstrateConfiguration> for Configuration {
183183
}),
184184
telemetry_endpoints: configuration.telemetry_endpoints,
185185
default_heap_pages: None,
186-
// Offchain worker is not used and indexing is used by MMR gadget to
187-
// prune fork mmr leaves once a given block is finalized
186+
// Offchain worker is not used
187+
// indexing is used to store the mmr leaves from Runtime
188188
offchain_worker: OffchainWorkerConfig {
189189
enabled: false,
190190
indexing_enabled: true,

0 commit comments

Comments
 (0)