#[cfg(not(feature = "std"))]
extern crate alloc;
use crate::grandpa::GrandpaJustification;
use crate::{Config, EncodedBlockHash, EncodedBlockNumber, Error};
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
use codec::Decode;
use frame_support::Parameter;
use num_traits::AsPrimitive;
use sp_runtime::generic;
use sp_runtime::traits::{
AtLeast32BitUnsigned, Hash as HashT, Header as HeaderT, MaybeDisplay,
MaybeSerializeDeserialize, Member, Saturating, SimpleBitOps,
};
use sp_std::hash::Hash;
use sp_std::str::FromStr;
pub(crate) type OpaqueExtrinsic = Vec<u8>;
pub type SignedBlock<Header> = generic::SignedBlock<generic::Block<Header, OpaqueExtrinsic>>;
pub trait Chain {
type BlockNumber: Parameter
+ Member
+ MaybeSerializeDeserialize
+ Hash
+ Copy
+ Default
+ MaybeDisplay
+ AtLeast32BitUnsigned
+ FromStr
+ AsPrimitive<usize>
+ Default
+ Saturating;
type Hash: Parameter
+ Member
+ MaybeSerializeDeserialize
+ Hash
+ Ord
+ Copy
+ MaybeDisplay
+ Default
+ SimpleBitOps
+ AsRef<[u8]>
+ AsMut<[u8]>;
type Header: Parameter
+ HeaderT<Number = Self::BlockNumber, Hash = Self::Hash>
+ MaybeSerializeDeserialize;
type Hasher: HashT<Output = Self::Hash>;
fn decode_block<T: Config>(block: &[u8]) -> Result<SignedBlock<Self::Header>, Error<T>> {
SignedBlock::<Self::Header>::decode(&mut &*block).map_err(|error| {
log::error!("Cannot decode block, error: {:?}", error);
Error::<T>::FailedDecodingBlock
})
}
fn decode_header<T: Config>(header: &[u8]) -> Result<Self::Header, Error<T>> {
Self::Header::decode(&mut &*header).map_err(|error| {
log::error!("Cannot decode header, error: {:?}", error);
Error::<T>::FailedDecodingHeader
})
}
fn decode_grandpa_justifications<T: Config>(
justifications: &[u8],
) -> Result<GrandpaJustification<Self::Header>, Error<T>> {
GrandpaJustification::<Self::Header>::decode(&mut &*justifications).map_err(|error| {
log::error!("Cannot decode justifications, error: {:?}", error);
Error::<T>::FailedDecodingJustifications
})
}
fn decode_block_number_and_hash<T: Config>(
pair: (EncodedBlockNumber, EncodedBlockHash),
) -> Result<(Self::BlockNumber, Self::Hash), Error<T>> {
let number = Self::decode_block_number::<T>(pair.0.as_slice())?;
let hash = Self::decode_block_hash::<T>(pair.1.as_slice())?;
Ok((number, hash))
}
fn decode_block_number<T: Config>(number: &[u8]) -> Result<Self::BlockNumber, Error<T>> {
Self::BlockNumber::decode(&mut &*number).map_err(|error| {
log::error!("Cannot decode block number, error: {:?}", error);
Error::<T>::FailedDecodingBlockNumber
})
}
fn decode_block_hash<T: Config>(hash: &[u8]) -> Result<Self::Hash, Error<T>> {
Self::Hash::decode(&mut &*hash).map_err(|error| {
log::error!("Cannot decode block hash, error: {:?}", error);
Error::<T>::FailedDecodingBlockHash
})
}
}