diff --git a/polkadot/runtime/rococo/src/lib.rs b/polkadot/runtime/rococo/src/lib.rs index 894d7fac2f0a..2e8401469109 100644 --- a/polkadot/runtime/rococo/src/lib.rs +++ b/polkadot/runtime/rococo/src/lib.rs @@ -1190,6 +1190,7 @@ impl pallet_mmr::Config for Runtime { type OnNewRoot = pallet_beefy_mmr::DepositBeefyDigest; type WeightInfo = (); type LeafData = pallet_beefy_mmr::Pallet; + type BlockHashProvider = pallet_mmr::DefaultBlockHashProvider; } parameter_types! { diff --git a/polkadot/runtime/westend/src/lib.rs b/polkadot/runtime/westend/src/lib.rs index 4930610c1d80..3f59b89263c8 100644 --- a/polkadot/runtime/westend/src/lib.rs +++ b/polkadot/runtime/westend/src/lib.rs @@ -333,6 +333,7 @@ impl pallet_mmr::Config for Runtime { type OnNewRoot = pallet_beefy_mmr::DepositBeefyDigest; type WeightInfo = (); type LeafData = pallet_beefy_mmr::Pallet; + type BlockHashProvider = pallet_mmr::DefaultBlockHashProvider; } /// MMR helper types. diff --git a/substrate/bin/node/runtime/src/lib.rs b/substrate/bin/node/runtime/src/lib.rs index 65d3ba0ed6ee..e3e4ae107718 100644 --- a/substrate/bin/node/runtime/src/lib.rs +++ b/substrate/bin/node/runtime/src/lib.rs @@ -1602,6 +1602,7 @@ impl pallet_mmr::Config for Runtime { type Hashing = Keccak256; type LeafData = pallet_mmr::ParentNumberAndHash; type OnNewRoot = pallet_beefy_mmr::DepositBeefyDigest; + type BlockHashProvider = pallet_mmr::DefaultBlockHashProvider; type WeightInfo = (); } diff --git a/substrate/frame/beefy-mmr/src/mock.rs b/substrate/frame/beefy-mmr/src/mock.rs index 9d1ece7a1d8e..d59c219d3e71 100644 --- a/substrate/frame/beefy-mmr/src/mock.rs +++ b/substrate/frame/beefy-mmr/src/mock.rs @@ -90,6 +90,8 @@ impl pallet_mmr::Config for Test { type OnNewRoot = pallet_beefy_mmr::DepositBeefyDigest; + type BlockHashProvider = pallet_mmr::DefaultBlockHashProvider; + type WeightInfo = (); } diff --git a/substrate/frame/merkle-mountain-range/src/lib.rs b/substrate/frame/merkle-mountain-range/src/lib.rs index 7b6edb37b7f7..e2b40974579e 100644 --- a/substrate/frame/merkle-mountain-range/src/lib.rs +++ b/substrate/frame/merkle-mountain-range/src/lib.rs @@ -103,6 +103,24 @@ impl LeafDataProvider for ParentNumberAndHash { } } +/// Block hash provider for a given block number. +pub trait BlockHashProvider { + fn block_hash(block_number: BlockNumber) -> BlockHash; +} + +/// Default implementation of BlockHashProvider using frame_system. +pub struct DefaultBlockHashProvider { + _phantom: sp_std::marker::PhantomData, +} + +impl BlockHashProvider, T::Hash> + for DefaultBlockHashProvider +{ + fn block_hash(block_number: BlockNumberFor) -> T::Hash { + frame_system::Pallet::::block_hash(block_number) + } +} + pub trait WeightInfo { fn on_initialize(peaks: NodeIndex) -> Weight; } @@ -177,6 +195,12 @@ pub mod pallet { /// Clients. Hook complexity should be `O(1)`. type OnNewRoot: primitives::OnNewRoot>; + /// Block hash provider for a given block number. + type BlockHashProvider: BlockHashProvider< + BlockNumberFor, + ::Hash, + >; + /// Weights for this pallet. type WeightInfo: WeightInfo; } diff --git a/substrate/frame/merkle-mountain-range/src/mmr/storage.rs b/substrate/frame/merkle-mountain-range/src/mmr/storage.rs index 96a20c3445ee..f2acc35a137f 100644 --- a/substrate/frame/merkle-mountain-range/src/mmr/storage.rs +++ b/substrate/frame/merkle-mountain-range/src/mmr/storage.rs @@ -29,7 +29,7 @@ use sp_std::prelude::*; use crate::{ mmr::{Node, NodeOf}, primitives::{self, NodeIndex}, - Config, Nodes, NumberOfLeaves, Pallet, + BlockHashProvider, Config, Nodes, NumberOfLeaves, Pallet, }; /// A marker type for runtime-specific storage implementation. @@ -87,7 +87,7 @@ where // Fall through to searching node using fork-specific key. let ancestor_parent_block_num = Pallet::::leaf_index_to_parent_block_num(ancestor_leaf_idx, leaves); - let ancestor_parent_hash = >::block_hash(ancestor_parent_block_num); + let ancestor_parent_hash = T::BlockHashProvider::block_hash(ancestor_parent_block_num); let temp_key = Pallet::::node_temp_offchain_key(pos, ancestor_parent_hash); debug!( target: "runtime::mmr::offchain", diff --git a/substrate/frame/merkle-mountain-range/src/mock.rs b/substrate/frame/merkle-mountain-range/src/mock.rs index 212012a052a0..8318b20e8307 100644 --- a/substrate/frame/merkle-mountain-range/src/mock.rs +++ b/substrate/frame/merkle-mountain-range/src/mock.rs @@ -44,6 +44,7 @@ impl Config for Test { type Hashing = Keccak256; type LeafData = Compact, LeafData)>; type OnNewRoot = (); + type BlockHashProvider = DefaultBlockHashProvider; type WeightInfo = (); }