referrerpolicy=no-referrer-when-downgrade

pallet_bridge_parachains/
proofs.rs

1// Copyright 2019-2021 Parity Technologies (UK) Ltd.
2// This file is part of Parity Bridges Common.
3
4// Parity Bridges Common is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// Parity Bridges Common is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with Parity Bridges Common.  If not, see <http://www.gnu.org/licenses/>.
16
17//! Tools for parachain head proof verification.
18
19use 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
27/// Abstraction over storage proof manipulation, hiding implementation details of actual storage
28/// proofs.
29pub trait StorageProofAdapter<T: Config<I>, I: 'static> {
30	/// Read and decode optional value from the proof.
31	fn read_and_decode_optional_value<D: Decode>(
32		&mut self,
33		key: &impl AsRef<[u8]>,
34	) -> Result<Option<D>, StorageProofError>;
35
36	/// Checks if each key was read.
37	fn ensure_no_unused_keys(self) -> Result<(), StorageProofError>;
38
39	/// Read parachain head from storage proof.
40	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(&parachain_head_key)
47	}
48}
49
50/// Actual storage proof adapter for parachain proofs.
51pub type ParachainsStorageProofAdapter<T, I> = RawStorageProofAdapter<T, I>;
52
53/// A `StorageProofAdapter` implementation for raw storage proofs.
54pub 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	/// Try to create a new instance of `RawStorageProofAdapter`.
61	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}