Reintroduce header chain trait (#1622)

* reintroduce header chain trait

* renive BridgedChainWithMessages::maximal_extrinsic_size
This commit is contained in:
Svyatoslav Nikolsky
2022-11-11 15:31:01 +03:00
committed by Bastian Köcher
parent 3e00c5c022
commit 6dcecf4425
19 changed files with 648 additions and 831 deletions
+41 -1
View File
@@ -19,19 +19,58 @@
#![cfg_attr(not(feature = "std"), no_std)]
use bp_runtime::BasicOperatingMode;
use bp_runtime::{BasicOperatingMode, Chain, HashOf, HasherOf, HeaderOf, StorageProofChecker};
use codec::{Codec, Decode, Encode, EncodeLike};
use core::{clone::Clone, cmp::Eq, default::Default, fmt::Debug};
use frame_support::PalletError;
use scale_info::TypeInfo;
#[cfg(feature = "std")]
use serde::{Deserialize, Serialize};
use sp_finality_grandpa::{AuthorityList, ConsensusLog, SetId, GRANDPA_ENGINE_ID};
use sp_runtime::{traits::Header as HeaderT, Digest, RuntimeDebug};
use sp_std::boxed::Box;
use sp_trie::StorageProof;
pub mod justification;
pub mod storage_keys;
/// Header chain error.
#[derive(Clone, Copy, Decode, Encode, Eq, PalletError, PartialEq, RuntimeDebug, TypeInfo)]
pub enum HeaderChainError {
/// Header with given hash is missing from the chain.
UnknownHeader,
/// The storage proof doesn't contains storage root.
StorageRootMismatch,
}
impl From<HeaderChainError> for &'static str {
fn from(err: HeaderChainError) -> &'static str {
match err {
HeaderChainError::UnknownHeader => "UnknownHeader",
HeaderChainError::StorageRootMismatch => "StorageRootMismatch",
}
}
}
/// Substrate header chain, abstracted from the way it is stored.
pub trait HeaderChain<C: Chain> {
/// Returns finalized header by its hash.
fn finalized_header(hash: HashOf<C>) -> Option<HeaderOf<C>>;
/// Parse storage proof using finalized header.
fn parse_finalized_storage_proof<R>(
hash: HashOf<C>,
storage_proof: StorageProof,
parse: impl FnOnce(StorageProofChecker<HasherOf<C>>) -> R,
) -> Result<R, HeaderChainError> {
let header = Self::finalized_header(hash).ok_or(HeaderChainError::UnknownHeader)?;
let storage_proof_checker =
bp_runtime::StorageProofChecker::new(*header.state_root(), storage_proof)
.map_err(|_| HeaderChainError::StorageRootMismatch)?;
Ok(parse(storage_proof_checker))
}
}
/// A type that can be used as a parameter in a dispatchable function.
///
/// When using `decl_module` all arguments for call functions must implement this trait.
@@ -79,6 +118,7 @@ pub trait FinalityProof<Number>: Clone + Send + Sync + Debug {
/// A trait that provides helper methods for querying the consensus log.
pub trait ConsensusLogReader {
/// Returns true if digest contains item that schedules authorities set change.
fn schedules_authorities_change(digest: &Digest) -> bool;
}