#[cfg(all(not(feature = "std"), feature = "serde"))]
use alloc::format;
use alloc::vec::Vec;
use codec::DecodeAll;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use crate::{
codec::{Decode, Encode, Error, Input},
scale_info::{
build::{Fields, Variants},
Path, Type, TypeInfo,
},
ConsensusEngineId,
};
use sp_core::RuntimeDebug;
#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug, TypeInfo, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Digest {
pub logs: Vec<DigestItem>,
}
impl Digest {
pub fn logs(&self) -> &[DigestItem] {
&self.logs
}
pub fn push(&mut self, item: DigestItem) {
self.logs.push(item);
}
pub fn pop(&mut self) -> Option<DigestItem> {
self.logs.pop()
}
pub fn log<T: ?Sized, F: Fn(&DigestItem) -> Option<&T>>(&self, predicate: F) -> Option<&T> {
self.logs().iter().find_map(predicate)
}
pub fn convert_first<T, F: Fn(&DigestItem) -> Option<T>>(&self, predicate: F) -> Option<T> {
self.logs().iter().find_map(predicate)
}
}
#[derive(PartialEq, Eq, Clone, RuntimeDebug)]
pub enum DigestItem {
PreRuntime(ConsensusEngineId, Vec<u8>),
Consensus(ConsensusEngineId, Vec<u8>),
Seal(ConsensusEngineId, Vec<u8>),
Other(Vec<u8>),
RuntimeEnvironmentUpdated,
}
#[cfg(feature = "serde")]
impl serde::Serialize for DigestItem {
fn serialize<S>(&self, seq: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
self.using_encoded(|bytes| sp_core::bytes::serialize(bytes, seq))
}
}
#[cfg(feature = "serde")]
impl<'a> serde::Deserialize<'a> for DigestItem {
fn deserialize<D>(de: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'a>,
{
let r = sp_core::bytes::deserialize(de)?;
Decode::decode(&mut &r[..])
.map_err(|e| serde::de::Error::custom(format!("Decode error: {}", e)))
}
}
impl TypeInfo for DigestItem {
type Identity = Self;
fn type_info() -> Type {
Type::builder().path(Path::new("DigestItem", module_path!())).variant(
Variants::new()
.variant("PreRuntime", |v| {
v.index(DigestItemType::PreRuntime as u8).fields(
Fields::unnamed()
.field(|f| f.ty::<ConsensusEngineId>().type_name("ConsensusEngineId"))
.field(|f| f.ty::<Vec<u8>>().type_name("Vec<u8>")),
)
})
.variant("Consensus", |v| {
v.index(DigestItemType::Consensus as u8).fields(
Fields::unnamed()
.field(|f| f.ty::<ConsensusEngineId>().type_name("ConsensusEngineId"))
.field(|f| f.ty::<Vec<u8>>().type_name("Vec<u8>")),
)
})
.variant("Seal", |v| {
v.index(DigestItemType::Seal as u8).fields(
Fields::unnamed()
.field(|f| f.ty::<ConsensusEngineId>().type_name("ConsensusEngineId"))
.field(|f| f.ty::<Vec<u8>>().type_name("Vec<u8>")),
)
})
.variant("Other", |v| {
v.index(DigestItemType::Other as u8)
.fields(Fields::unnamed().field(|f| f.ty::<Vec<u8>>().type_name("Vec<u8>")))
})
.variant("RuntimeEnvironmentUpdated", |v| {
v.index(DigestItemType::RuntimeEnvironmentUpdated as u8).fields(Fields::unit())
}),
)
}
}
#[derive(PartialEq, Eq, Clone, RuntimeDebug)]
pub enum DigestItemRef<'a> {
PreRuntime(&'a ConsensusEngineId, &'a [u8]),
Consensus(&'a ConsensusEngineId, &'a [u8]),
Seal(&'a ConsensusEngineId, &'a [u8]),
Other(&'a [u8]),
RuntimeEnvironmentUpdated,
}
#[repr(u32)]
#[derive(Encode, Decode)]
pub enum DigestItemType {
Other = 0,
Consensus = 4,
Seal = 5,
PreRuntime = 6,
RuntimeEnvironmentUpdated = 8,
}
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
pub enum OpaqueDigestItemId<'a> {
PreRuntime(&'a ConsensusEngineId),
Consensus(&'a ConsensusEngineId),
Seal(&'a ConsensusEngineId),
Other,
}
impl DigestItem {
pub fn dref(&self) -> DigestItemRef {
match *self {
Self::PreRuntime(ref v, ref s) => DigestItemRef::PreRuntime(v, s),
Self::Consensus(ref v, ref s) => DigestItemRef::Consensus(v, s),
Self::Seal(ref v, ref s) => DigestItemRef::Seal(v, s),
Self::Other(ref v) => DigestItemRef::Other(v),
Self::RuntimeEnvironmentUpdated => DigestItemRef::RuntimeEnvironmentUpdated,
}
}
pub fn as_pre_runtime(&self) -> Option<(ConsensusEngineId, &[u8])> {
self.dref().as_pre_runtime()
}
pub fn as_consensus(&self) -> Option<(ConsensusEngineId, &[u8])> {
self.dref().as_consensus()
}
pub fn as_seal(&self) -> Option<(ConsensusEngineId, &[u8])> {
self.dref().as_seal()
}
pub fn as_other(&self) -> Option<&[u8]> {
self.dref().as_other()
}
pub fn try_as_raw(&self, id: OpaqueDigestItemId) -> Option<&[u8]> {
self.dref().try_as_raw(id)
}
pub fn try_to<T: Decode>(&self, id: OpaqueDigestItemId) -> Option<T> {
self.dref().try_to::<T>(id)
}
pub fn seal_try_to<T: Decode>(&self, id: &ConsensusEngineId) -> Option<T> {
self.dref().seal_try_to(id)
}
pub fn consensus_try_to<T: Decode>(&self, id: &ConsensusEngineId) -> Option<T> {
self.dref().consensus_try_to(id)
}
pub fn pre_runtime_try_to<T: Decode>(&self, id: &ConsensusEngineId) -> Option<T> {
self.dref().pre_runtime_try_to(id)
}
}
impl Encode for DigestItem {
fn encode(&self) -> Vec<u8> {
self.dref().encode()
}
}
impl codec::EncodeLike for DigestItem {}
impl Decode for DigestItem {
#[allow(deprecated)]
fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
let item_type: DigestItemType = Decode::decode(input)?;
match item_type {
DigestItemType::PreRuntime => {
let vals: (ConsensusEngineId, Vec<u8>) = Decode::decode(input)?;
Ok(Self::PreRuntime(vals.0, vals.1))
},
DigestItemType::Consensus => {
let vals: (ConsensusEngineId, Vec<u8>) = Decode::decode(input)?;
Ok(Self::Consensus(vals.0, vals.1))
},
DigestItemType::Seal => {
let vals: (ConsensusEngineId, Vec<u8>) = Decode::decode(input)?;
Ok(Self::Seal(vals.0, vals.1))
},
DigestItemType::Other => Ok(Self::Other(Decode::decode(input)?)),
DigestItemType::RuntimeEnvironmentUpdated => Ok(Self::RuntimeEnvironmentUpdated),
}
}
}
impl<'a> DigestItemRef<'a> {
pub fn as_pre_runtime(&self) -> Option<(ConsensusEngineId, &'a [u8])> {
match *self {
Self::PreRuntime(consensus_engine_id, data) => Some((*consensus_engine_id, data)),
_ => None,
}
}
pub fn as_consensus(&self) -> Option<(ConsensusEngineId, &'a [u8])> {
match *self {
Self::Consensus(consensus_engine_id, data) => Some((*consensus_engine_id, data)),
_ => None,
}
}
pub fn as_seal(&self) -> Option<(ConsensusEngineId, &'a [u8])> {
match *self {
Self::Seal(consensus_engine_id, data) => Some((*consensus_engine_id, data)),
_ => None,
}
}
pub fn as_other(&self) -> Option<&'a [u8]> {
match *self {
Self::Other(data) => Some(data),
_ => None,
}
}
pub fn try_as_raw(&self, id: OpaqueDigestItemId) -> Option<&'a [u8]> {
match (id, self) {
(OpaqueDigestItemId::Consensus(w), &Self::Consensus(v, s)) |
(OpaqueDigestItemId::Seal(w), &Self::Seal(v, s)) |
(OpaqueDigestItemId::PreRuntime(w), &Self::PreRuntime(v, s))
if v == w =>
Some(s),
(OpaqueDigestItemId::Other, &Self::Other(s)) => Some(s),
_ => None,
}
}
pub fn try_to<T: Decode>(&self, id: OpaqueDigestItemId) -> Option<T> {
self.try_as_raw(id).and_then(|mut x| DecodeAll::decode_all(&mut x).ok())
}
pub fn seal_try_to<T: Decode>(&self, id: &ConsensusEngineId) -> Option<T> {
self.as_seal()
.filter(|s| s.0 == *id)
.and_then(|mut d| DecodeAll::decode_all(&mut d.1).ok())
}
pub fn consensus_try_to<T: Decode>(&self, id: &ConsensusEngineId) -> Option<T> {
self.as_consensus()
.filter(|s| s.0 == *id)
.and_then(|mut d| DecodeAll::decode_all(&mut d.1).ok())
}
pub fn pre_runtime_try_to<T: Decode>(&self, id: &ConsensusEngineId) -> Option<T> {
self.as_pre_runtime()
.filter(|s| s.0 == *id)
.and_then(|mut d| DecodeAll::decode_all(&mut d.1).ok())
}
}
impl<'a> Encode for DigestItemRef<'a> {
fn encode(&self) -> Vec<u8> {
match *self {
Self::Consensus(val, data) => (DigestItemType::Consensus, val, data).encode(),
Self::Seal(val, sig) => (DigestItemType::Seal, val, sig).encode(),
Self::PreRuntime(val, data) => (DigestItemType::PreRuntime, val, data).encode(),
Self::Other(val) => (DigestItemType::Other, val).encode(),
Self::RuntimeEnvironmentUpdated => DigestItemType::RuntimeEnvironmentUpdated.encode(),
}
}
}
impl<'a> codec::EncodeLike for DigestItemRef<'a> {}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn should_serialize_digest() {
let digest = Digest {
logs: vec![DigestItem::Other(vec![1, 2, 3]), DigestItem::Seal(*b"test", vec![1, 2, 3])],
};
assert_eq!(
serde_json::to_string(&digest).unwrap(),
r#"{"logs":["0x000c010203","0x05746573740c010203"]}"#
);
}
#[test]
fn digest_item_type_info() {
let type_info = DigestItem::type_info();
let variants = if let scale_info::TypeDef::Variant(variant) = type_info.type_def {
variant.variants
} else {
panic!("Should be a TypeDef::TypeDefVariant")
};
let check = |digest_item_type: DigestItemType| {
let (variant_name, digest_item) = match digest_item_type {
DigestItemType::Other => ("Other", DigestItem::Other(Default::default())),
DigestItemType::Consensus =>
("Consensus", DigestItem::Consensus(Default::default(), Default::default())),
DigestItemType::Seal =>
("Seal", DigestItem::Seal(Default::default(), Default::default())),
DigestItemType::PreRuntime =>
("PreRuntime", DigestItem::PreRuntime(Default::default(), Default::default())),
DigestItemType::RuntimeEnvironmentUpdated =>
("RuntimeEnvironmentUpdated", DigestItem::RuntimeEnvironmentUpdated),
};
let encoded = digest_item.encode();
let variant = variants
.iter()
.find(|v| v.name == variant_name)
.expect(&format!("Variant {} not found", variant_name));
assert_eq!(encoded[0], variant.index)
};
check(DigestItemType::Other);
check(DigestItemType::Consensus);
check(DigestItemType::Seal);
check(DigestItemType::PreRuntime);
check(DigestItemType::RuntimeEnvironmentUpdated);
}
}