pallet_bridge_parachains/
proofs.rs1use crate::{Config, GrandpaPalletOf, RelayBlockHash, RelayBlockHasher};
20use bp_header_chain::{HeaderChain, HeaderChainError};
21use bp_parachains::parachain_head_storage_key_at_source;
22use bp_polkadot_core::parachains::{ParaHead, ParaId};
23use bp_runtime::{RawStorageProof, StorageProofChecker, StorageProofError};
24use codec::Decode;
25use frame_support::traits::Get;
26
27pub trait StorageProofAdapter<T: Config<I>, I: 'static> {
30 fn read_and_decode_optional_value<D: Decode>(
32 &mut self,
33 key: &impl AsRef<[u8]>,
34 ) -> Result<Option<D>, StorageProofError>;
35
36 fn ensure_no_unused_keys(self) -> Result<(), StorageProofError>;
38
39 fn read_parachain_head(
41 &mut self,
42 parachain: ParaId,
43 ) -> Result<Option<ParaHead>, StorageProofError> {
44 let parachain_head_key =
45 parachain_head_storage_key_at_source(T::ParasPalletName::get(), parachain);
46 self.read_and_decode_optional_value(¶chain_head_key)
47 }
48}
49
50pub type ParachainsStorageProofAdapter<T, I> = RawStorageProofAdapter<T, I>;
52
53pub struct RawStorageProofAdapter<T: Config<I>, I: 'static> {
55 storage: StorageProofChecker<RelayBlockHasher>,
56 _dummy: sp_std::marker::PhantomData<(T, I)>,
57}
58
59impl<T: Config<I>, I: 'static> RawStorageProofAdapter<T, I> {
60 pub fn try_new_with_verified_storage_proof(
62 relay_block_hash: RelayBlockHash,
63 storage_proof: RawStorageProof,
64 ) -> Result<Self, HeaderChainError> {
65 GrandpaPalletOf::<T, I>::verify_storage_proof(relay_block_hash, storage_proof)
66 .map(|storage| RawStorageProofAdapter::<T, I> { storage, _dummy: Default::default() })
67 }
68}
69
70impl<T: Config<I>, I: 'static> StorageProofAdapter<T, I> for RawStorageProofAdapter<T, I> {
71 fn read_and_decode_optional_value<D: Decode>(
72 &mut self,
73 key: &impl AsRef<[u8]>,
74 ) -> Result<Option<D>, StorageProofError> {
75 self.storage.read_and_decode_opt_value(key.as_ref())
76 }
77
78 fn ensure_no_unused_keys(self) -> Result<(), StorageProofError> {
79 self.storage.ensure_no_unused_nodes()
80 }
81}