define block hash provider and default impl using frame_system (#4080)

This PR introduces `BlockHashProvider` into `pallet_mmr::Config`
This type is used to get `block_hash` for a given `block_number` rather
than directly using `frame_system::Pallet::block_hash`

The `DefaultBlockHashProvider` uses `frame_system::Pallet::block_hash`
to get the `block_hash`

Closes: #4062
This commit is contained in:
Vedhavyas Singareddi
2024-04-13 03:27:05 +05:30
committed by GitHub
parent 5601f2865b
commit 5b513cc0e9
7 changed files with 32 additions and 2 deletions
@@ -103,6 +103,24 @@ 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(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(block_number: BlockNumberFor<T>) -> T::Hash {
frame_system::Pallet::<T>::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<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;
}
@@ -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::<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(ancestor_parent_block_num);
let temp_key = Pallet::<T, I>::node_temp_offchain_key(pos, ancestor_parent_hash);
debug!(
target: "runtime::mmr::offchain",
@@ -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 = ();
}