use crate::{Config, GrandpaPalletOf, RelayBlockHash, RelayBlockHasher};
use bp_header_chain::{HeaderChain, HeaderChainError};
use bp_parachains::parachain_head_storage_key_at_source;
use bp_polkadot_core::parachains::{ParaHead, ParaId};
use bp_runtime::{RawStorageProof, StorageProofChecker, StorageProofError};
use codec::Decode;
use frame_support::traits::Get;
pub trait StorageProofAdapter<T: Config<I>, I: 'static> {
fn read_and_decode_optional_value<D: Decode>(
&mut self,
key: &impl AsRef<[u8]>,
) -> Result<Option<D>, StorageProofError>;
fn ensure_no_unused_keys(self) -> Result<(), StorageProofError>;
fn read_parachain_head(
&mut self,
parachain: ParaId,
) -> Result<Option<ParaHead>, StorageProofError> {
let parachain_head_key =
parachain_head_storage_key_at_source(T::ParasPalletName::get(), parachain);
self.read_and_decode_optional_value(¶chain_head_key)
}
}
pub type ParachainsStorageProofAdapter<T, I> = RawStorageProofAdapter<T, I>;
pub struct RawStorageProofAdapter<T: Config<I>, I: 'static> {
storage: StorageProofChecker<RelayBlockHasher>,
_dummy: sp_std::marker::PhantomData<(T, I)>,
}
impl<T: Config<I>, I: 'static> RawStorageProofAdapter<T, I> {
pub fn try_new_with_verified_storage_proof(
relay_block_hash: RelayBlockHash,
storage_proof: RawStorageProof,
) -> Result<Self, HeaderChainError> {
GrandpaPalletOf::<T, I>::verify_storage_proof(relay_block_hash, storage_proof)
.map(|storage| RawStorageProofAdapter::<T, I> { storage, _dummy: Default::default() })
}
}
impl<T: Config<I>, I: 'static> StorageProofAdapter<T, I> for RawStorageProofAdapter<T, I> {
fn read_and_decode_optional_value<D: Decode>(
&mut self,
key: &impl AsRef<[u8]>,
) -> Result<Option<D>, StorageProofError> {
self.storage.read_and_decode_opt_value(key.as_ref())
}
fn ensure_no_unused_keys(self) -> Result<(), StorageProofError> {
self.storage.ensure_no_unused_nodes()
}
}