Skip to content

Commit

Permalink
define block hash provider and default impl using frame_system
Browse files Browse the repository at this point in the history
  • Loading branch information
vedhavyas committed Apr 11, 2024
1 parent 92e1425 commit 2fb5e4c
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 2 deletions.
1 change: 1 addition & 0 deletions polkadot/runtime/rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1190,6 +1190,7 @@ impl pallet_mmr::Config for Runtime {
type OnNewRoot = pallet_beefy_mmr::DepositBeefyDigest<Runtime>;
type WeightInfo = ();
type LeafData = pallet_beefy_mmr::Pallet<Runtime>;
type BlockHashProvider = pallet_mmr::DefaultBlockHashProvider<Runtime>;
}

parameter_types! {
Expand Down
1 change: 1 addition & 0 deletions polkadot/runtime/westend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ impl pallet_mmr::Config for Runtime {
type OnNewRoot = pallet_beefy_mmr::DepositBeefyDigest<Runtime>;
type WeightInfo = ();
type LeafData = pallet_beefy_mmr::Pallet<Runtime>;
type BlockHashProvider = pallet_mmr::DefaultBlockHashProvider<Runtime>;
}

/// MMR helper types.
Expand Down
1 change: 1 addition & 0 deletions substrate/bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1602,6 +1602,7 @@ impl pallet_mmr::Config for Runtime {
type Hashing = Keccak256;
type LeafData = pallet_mmr::ParentNumberAndHash<Self>;
type OnNewRoot = pallet_beefy_mmr::DepositBeefyDigest<Runtime>;
type BlockHashProvider = pallet_mmr::DefaultBlockHashProvider<Runtime>;
type WeightInfo = ();
}

Expand Down
19 changes: 19 additions & 0 deletions substrate/frame/merkle-mountain-range/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,22 @@ impl<T: frame_system::Config> LeafDataProvider for ParentNumberAndHash<T> {
}
}

/// Block hash provider for a given block number.
pub trait BlockHashProvider<BlockNumber, BlockHash>{
fn block_hash_for(block_number: BlockNumber) -> BlockHash;
}

/// Default implementation of BlockHashProvider using frame_system.
pub struct DefaultBlockHashProvider<T: frame_system::Config>{
_phantom: sp_std::marker::PhantomData<T>,
}

impl<T: frame_system::Config> BlockHashProvider<BlockNumberFor<T>, T::Hash> for DefaultBlockHashProvider<T> {
fn block_hash_for(block_number: BlockNumberFor<T>) -> T::Hash {
frame_system::Pallet::<T>::block_hash(block_number)
}
}

pub trait WeightInfo {
fn on_initialize(peaks: NodeIndex) -> Weight;
}
Expand Down Expand Up @@ -177,6 +193,9 @@ pub mod pallet {
/// Clients. Hook complexity should be `O(1)`.
type OnNewRoot: primitives::OnNewRoot<HashOf<Self, I>>;

/// Block hash provider for a given block number.
type BlockHashProvider: BlockHashProvider<BlockNumberFor<Self>, <Self as frame_system::Config>::Hash>;

/// Weights for this pallet.
type WeightInfo: WeightInfo;
}
Expand Down
4 changes: 2 additions & 2 deletions substrate/frame/merkle-mountain-range/src/mmr/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use sp_std::prelude::*;
use crate::{
mmr::{Node, NodeOf},
primitives::{self, NodeIndex},
Config, Nodes, NumberOfLeaves, Pallet,
Config, Nodes, NumberOfLeaves, Pallet, BlockHashProvider
};

/// A marker type for runtime-specific storage implementation.
Expand Down Expand Up @@ -87,7 +87,7 @@ where
// Fall through to searching node using fork-specific key.
let ancestor_parent_block_num =
Pallet::<T, I>::leaf_index_to_parent_block_num(ancestor_leaf_idx, leaves);
let ancestor_parent_hash = <frame_system::Pallet<T>>::block_hash(ancestor_parent_block_num);
let ancestor_parent_hash = T::BlockHashProvider::block_hash_for(ancestor_parent_block_num);
let temp_key = Pallet::<T, I>::node_temp_offchain_key(pos, ancestor_parent_hash);
debug!(
target: "runtime::mmr::offchain",
Expand Down
1 change: 1 addition & 0 deletions substrate/frame/merkle-mountain-range/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ impl Config for Test {
type Hashing = Keccak256;
type LeafData = Compact<Keccak256, (ParentNumberAndHash<Test>, LeafData)>;
type OnNewRoot = ();
type BlockHashProvider = DefaultBlockHashProvider<Test>;
type WeightInfo = ();
}

Expand Down

0 comments on commit 2fb5e4c

Please sign in to comment.