Skip to content

Commit

Permalink
define block hash provider and default impl using frame_system (#4080)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
vedhavyas authored Apr 12, 2024
1 parent 5601f28 commit 5b513cc
Show file tree
Hide file tree
Showing 7 changed files with 32 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 @@ -1195,6 +1195,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
2 changes: 2 additions & 0 deletions substrate/frame/beefy-mmr/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ impl pallet_mmr::Config for Test {

type OnNewRoot = pallet_beefy_mmr::DepositBeefyDigest<Test>;

type BlockHashProvider = pallet_mmr::DefaultBlockHashProvider<Test>;

type WeightInfo = ();
}

Expand Down
24 changes: 24 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,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;
}
Expand Down Expand Up @@ -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;
}
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,
BlockHashProvider, Config, Nodes, NumberOfLeaves, Pallet,
};

/// 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(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 5b513cc

Please sign in to comment.