mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-10 17:11:03 +00:00
rococo-runtime: use mmr-specific Hash (#7318)
pallet-mmr can be configured with a different hashing scheme than frame-system pallet. Make sure to use this custom type for MmrApi. Signed-off-by: Adrian Catangiu <adrian@parity.io>
This commit is contained in:
@@ -70,7 +70,6 @@ use pallet_im_online::sr25519::AuthorityId as ImOnlineId;
|
||||
use pallet_session::historical as session_historical;
|
||||
use pallet_transaction_payment::{CurrencyAdapter, FeeDetails, RuntimeDispatchInfo};
|
||||
use sp_core::{ConstU128, OpaqueMetadata, H256};
|
||||
use sp_mmr_primitives as mmr;
|
||||
use sp_runtime::{
|
||||
create_runtime_str, generic, impl_opaque_keys,
|
||||
traits::{
|
||||
@@ -1281,6 +1280,16 @@ impl pallet_beefy::Config for Runtime {
|
||||
pallet_beefy::EquivocationReportSystem<Self, Offences, Historical, ReportLongevity>;
|
||||
}
|
||||
|
||||
/// MMR helper types.
|
||||
mod mmr {
|
||||
use super::Runtime;
|
||||
pub use pallet_mmr::primitives::*;
|
||||
|
||||
pub type Leaf = <<Runtime as pallet_mmr::Config>::LeafData as LeafDataProvider>::LeafData;
|
||||
pub type Hashing = <Runtime as pallet_mmr::Config>::Hashing;
|
||||
pub type Hash = <Hashing as sp_runtime::traits::Hash>::Output;
|
||||
}
|
||||
|
||||
impl pallet_mmr::Config for Runtime {
|
||||
const INDEXING_PREFIX: &'static [u8] = mmr::INDEXING_PREFIX;
|
||||
type Hashing = Keccak256;
|
||||
@@ -1314,7 +1323,7 @@ impl BeefyDataProvider<H256> for ParasProvider {
|
||||
.filter_map(|id| Paras::para_head(&id).map(|head| (id.into(), head.0)))
|
||||
.collect();
|
||||
para_heads.sort();
|
||||
binary_merkle_tree::merkle_root::<<Runtime as pallet_mmr::Config>::Hashing, _>(
|
||||
binary_merkle_tree::merkle_root::<mmr::Hashing, _>(
|
||||
para_heads.into_iter().map(|pair| pair.encode()),
|
||||
)
|
||||
.into()
|
||||
@@ -1731,8 +1740,6 @@ mod benches {
|
||||
);
|
||||
}
|
||||
|
||||
pub type MmrHashing = <Runtime as pallet_mmr::Config>::Hashing;
|
||||
|
||||
#[cfg(not(feature = "disable-runtime-api"))]
|
||||
sp_api::impl_runtime_apis! {
|
||||
impl sp_api::Core<Block> for Runtime {
|
||||
@@ -1972,8 +1979,8 @@ sp_api::impl_runtime_apis! {
|
||||
}
|
||||
|
||||
#[api_version(2)]
|
||||
impl mmr::MmrApi<Block, Hash, BlockNumber> for Runtime {
|
||||
fn mmr_root() -> Result<Hash, mmr::Error> {
|
||||
impl mmr::MmrApi<Block, mmr::Hash, BlockNumber> for Runtime {
|
||||
fn mmr_root() -> Result<mmr::Hash, mmr::Error> {
|
||||
Ok(Mmr::mmr_root())
|
||||
}
|
||||
|
||||
@@ -1984,7 +1991,7 @@ sp_api::impl_runtime_apis! {
|
||||
fn generate_proof(
|
||||
block_numbers: Vec<BlockNumber>,
|
||||
best_known_block_number: Option<BlockNumber>,
|
||||
) -> Result<(Vec<mmr::EncodableOpaqueLeaf>, mmr::Proof<Hash>), mmr::Error> {
|
||||
) -> Result<(Vec<mmr::EncodableOpaqueLeaf>, mmr::Proof<mmr::Hash>), mmr::Error> {
|
||||
Mmr::generate_proof(block_numbers, best_known_block_number).map(
|
||||
|(leaves, proof)| {
|
||||
(
|
||||
@@ -1998,24 +2005,23 @@ sp_api::impl_runtime_apis! {
|
||||
)
|
||||
}
|
||||
|
||||
fn verify_proof(leaves: Vec<mmr::EncodableOpaqueLeaf>, proof: mmr::Proof<Hash>)
|
||||
fn verify_proof(leaves: Vec<mmr::EncodableOpaqueLeaf>, proof: mmr::Proof<mmr::Hash>)
|
||||
-> Result<(), mmr::Error>
|
||||
{
|
||||
pub type MmrLeaf = <<Runtime as pallet_mmr::Config>::LeafData as mmr::LeafDataProvider>::LeafData;
|
||||
let leaves = leaves.into_iter().map(|leaf|
|
||||
leaf.into_opaque_leaf()
|
||||
.try_decode()
|
||||
.ok_or(mmr::Error::Verify)).collect::<Result<Vec<MmrLeaf>, mmr::Error>>()?;
|
||||
.ok_or(mmr::Error::Verify)).collect::<Result<Vec<mmr::Leaf>, mmr::Error>>()?;
|
||||
Mmr::verify_leaves(leaves, proof)
|
||||
}
|
||||
|
||||
fn verify_proof_stateless(
|
||||
root: Hash,
|
||||
root: mmr::Hash,
|
||||
leaves: Vec<mmr::EncodableOpaqueLeaf>,
|
||||
proof: mmr::Proof<Hash>
|
||||
proof: mmr::Proof<mmr::Hash>
|
||||
) -> Result<(), mmr::Error> {
|
||||
let nodes = leaves.into_iter().map(|leaf|mmr::DataOrHash::Data(leaf.into_opaque_leaf())).collect();
|
||||
pallet_mmr::verify_leaves_proof::<MmrHashing, _>(root, nodes, proof)
|
||||
pallet_mmr::verify_leaves_proof::<mmr::Hashing, _>(root, nodes, proof)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user