#![warn(missing_docs)]
#![cfg_attr(not(feature = "std"), no_std)]
pub use pezbp_header_pez_chain::StoredHeaderData;
pub use call_info::{BridgeTeyrchainCall, SubmitTeyrchainHeadsInfo};
use pezbp_pezkuwi_core::teyrchains::{ParaHash, ParaHead, ParaId};
use codec::{Decode, Encode, MaxEncodedLen};
use pezbp_runtime::{
BlockNumberOf, Chain, HashOf, HeaderOf, StorageDoubleMapKeyProvider, StorageMapKeyProvider,
Teyrchain,
};
use pezframe_support::{weights::Weight, Blake2_128Concat, Twox64Concat};
use pezsp_core::storage::StorageKey;
use pezsp_runtime::{traits::Header as HeaderT, RuntimeDebug};
use pezsp_std::{marker::PhantomData, prelude::*};
use scale_info::TypeInfo;
pub type RelayBlockHash = pezbp_pezkuwi_core::Hash;
pub type RelayBlockNumber = pezbp_pezkuwi_core::BlockNumber;
pub type RelayBlockHasher = pezbp_pezkuwi_core::Hasher;
mod call_info;
#[derive(Clone, Decode, Encode, MaxEncodedLen, PartialEq, RuntimeDebug, TypeInfo)]
pub struct BestParaHeadHash {
pub at_relay_block_number: RelayBlockNumber,
pub head_hash: ParaHash,
}
#[derive(Decode, Encode, MaxEncodedLen, PartialEq, RuntimeDebug, TypeInfo)]
pub struct ParaInfo {
pub best_head_hash: BestParaHeadHash,
pub next_imported_hash_position: u32,
}
pub fn teyrchain_head_storage_key_at_source(
paras_pallet_name: &str,
para_id: ParaId,
) -> StorageKey {
pezbp_runtime::storage_map_final_key::<Twox64Concat>(
paras_pallet_name,
"Heads",
¶_id.encode(),
)
}
pub struct ParasInfoKeyProvider;
impl StorageMapKeyProvider for ParasInfoKeyProvider {
const MAP_NAME: &'static str = "ParasInfo";
type Hasher = Blake2_128Concat;
type Key = ParaId;
type Value = ParaInfo;
}
pub struct ImportedParaHeadsKeyProvider;
impl StorageDoubleMapKeyProvider for ImportedParaHeadsKeyProvider {
const MAP_NAME: &'static str = "ImportedParaHeads";
type Hasher1 = Blake2_128Concat;
type Key1 = ParaId;
type Hasher2 = Blake2_128Concat;
type Key2 = ParaHash;
type Value = ParaStoredHeaderData;
}
#[derive(Clone, Decode, Encode, PartialEq, RuntimeDebug, TypeInfo)]
pub struct ParaStoredHeaderData(pub Vec<u8>);
impl ParaStoredHeaderData {
pub fn decode_teyrchain_head_data<C: Chain>(
&self,
) -> Result<StoredHeaderData<BlockNumberOf<C>, HashOf<C>>, codec::Error> {
StoredHeaderData::<BlockNumberOf<C>, HashOf<C>>::decode(&mut &self.0[..])
}
}
pub trait ParaStoredHeaderDataBuilder {
fn max_free_head_size() -> u32;
fn supported_teyrchains() -> u32;
fn try_build(para_id: ParaId, para_head: &ParaHead) -> Option<ParaStoredHeaderData>;
}
pub struct SingleParaStoredHeaderDataBuilder<C: Teyrchain>(PhantomData<C>);
impl<C: Teyrchain> ParaStoredHeaderDataBuilder for SingleParaStoredHeaderDataBuilder<C> {
fn max_free_head_size() -> u32 {
C::MAX_HEADER_SIZE
}
fn supported_teyrchains() -> u32 {
1
}
fn try_build(para_id: ParaId, para_head: &ParaHead) -> Option<ParaStoredHeaderData> {
if para_id == ParaId(C::TEYRCHAIN_ID) {
let header = HeaderOf::<C>::decode(&mut ¶_head.0[..]).ok()?;
return Some(ParaStoredHeaderData(
StoredHeaderData { number: *header.number(), state_root: *header.state_root() }
.encode(),
));
}
None
}
}
#[impl_trait_for_tuples::impl_for_tuples(1, 30)]
#[tuple_types_custom_trait_bound(Teyrchain)]
impl ParaStoredHeaderDataBuilder for C {
fn max_free_head_size() -> u32 {
let mut result = 0_u32;
for_tuples!( #(
result = pezsp_std::cmp::max(
result,
SingleParaStoredHeaderDataBuilder::<C>::max_free_head_size(),
);
)* );
result
}
fn supported_teyrchains() -> u32 {
let mut result = 0;
for_tuples!( #(
result += SingleParaStoredHeaderDataBuilder::<C>::supported_teyrchains();
)* );
result
}
fn try_build(para_id: ParaId, para_head: &ParaHead) -> Option<ParaStoredHeaderData> {
for_tuples!( #(
let maybe_para_head = SingleParaStoredHeaderDataBuilder::<C>::try_build(para_id, para_head);
if let Some(maybe_para_head) = maybe_para_head {
return Some(maybe_para_head);
}
)* );
None
}
}
pub trait OnNewHead {
fn on_new_head(id: ParaId, head: &ParaHead) -> Weight;
}
#[impl_trait_for_tuples::impl_for_tuples(8)]
impl OnNewHead for Tuple {
fn on_new_head(id: ParaId, head: &ParaHead) -> Weight {
let mut weight: Weight = Default::default();
for_tuples!( #( weight.saturating_accrue(Tuple::on_new_head(id, head)); )* );
weight
}
}