#[cfg(feature = "std")]
use serde::{Deserialize, Serialize};
use sp_std::prelude::*;
use crate::ConsensusEngineId;
use crate::codec::{Decode, Encode, Input, Error};
use sp_core::{ChangesTrieConfiguration, RuntimeDebug};
#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, parity_util_mem::MallocSizeOf))]
pub struct Digest<Hash> {
#[cfg_attr(
feature = "std",
serde(bound(serialize = "Hash: codec::Codec", deserialize = "Hash: codec::Codec"))
)]
pub logs: Vec<DigestItem<Hash>>,
}
impl<Item> Default for Digest<Item> {
fn default() -> Self {
Digest { logs: Vec::new(), }
}
}
impl<Hash> Digest<Hash> {
pub fn logs(&self) -> &[DigestItem<Hash>] {
&self.logs
}
pub fn push(&mut self, item: DigestItem<Hash>) {
self.logs.push(item);
}
pub fn pop(&mut self) -> Option<DigestItem<Hash>> {
self.logs.pop()
}
pub fn log<T: ?Sized, F: Fn(&DigestItem<Hash>) -> Option<&T>>(&self, predicate: F) -> Option<&T> {
self.logs().iter()
.filter_map(predicate)
.next()
}
pub fn convert_first<T, F: Fn(&DigestItem<Hash>) -> Option<T>>(&self, predicate: F) -> Option<T> {
self.logs().iter()
.filter_map(predicate)
.next()
}
}
#[derive(PartialEq, Eq, Clone, RuntimeDebug)]
#[cfg_attr(feature = "std", derive(parity_util_mem::MallocSizeOf))]
pub enum DigestItem<Hash> {
ChangesTrieRoot(Hash),
PreRuntime(ConsensusEngineId, Vec<u8>),
Consensus(ConsensusEngineId, Vec<u8>),
Seal(ConsensusEngineId, Vec<u8>),
ChangesTrieSignal(ChangesTrieSignal),
Other(Vec<u8>),
}
#[derive(PartialEq, Eq, Clone, Encode, Decode)]
#[cfg_attr(feature = "std", derive(Debug, parity_util_mem::MallocSizeOf))]
pub enum ChangesTrieSignal {
NewConfiguration(Option<ChangesTrieConfiguration>),
}
#[cfg(feature = "std")]
impl<Hash: Encode> serde::Serialize for DigestItem<Hash> {
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 = "std")]
impl<'a, Hash: Decode> serde::Deserialize<'a> for DigestItem<Hash> {
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)))
}
}
#[derive(PartialEq, Eq, Clone, RuntimeDebug)]
pub enum DigestItemRef<'a, Hash: 'a> {
ChangesTrieRoot(&'a Hash),
PreRuntime(&'a ConsensusEngineId, &'a Vec<u8>),
Consensus(&'a ConsensusEngineId, &'a Vec<u8>),
Seal(&'a ConsensusEngineId, &'a Vec<u8>),
ChangesTrieSignal(&'a ChangesTrieSignal),
Other(&'a Vec<u8>),
}
#[repr(u32)]
#[derive(Encode, Decode)]
pub enum DigestItemType {
Other = 0,
ChangesTrieRoot = 2,
Consensus = 4,
Seal = 5,
PreRuntime = 6,
ChangesTrieSignal = 7,
}
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
pub enum OpaqueDigestItemId<'a> {
PreRuntime(&'a ConsensusEngineId),
Consensus(&'a ConsensusEngineId),
Seal(&'a ConsensusEngineId),
Other,
}
impl<Hash> DigestItem<Hash> {
pub fn dref<'a>(&'a self) -> DigestItemRef<'a, Hash> {
match *self {
DigestItem::ChangesTrieRoot(ref v) => DigestItemRef::ChangesTrieRoot(v),
DigestItem::PreRuntime(ref v, ref s) => DigestItemRef::PreRuntime(v, s),
DigestItem::Consensus(ref v, ref s) => DigestItemRef::Consensus(v, s),
DigestItem::Seal(ref v, ref s) => DigestItemRef::Seal(v, s),
DigestItem::ChangesTrieSignal(ref s) => DigestItemRef::ChangesTrieSignal(s),
DigestItem::Other(ref v) => DigestItemRef::Other(v),
}
}
pub fn as_changes_trie_root(&self) -> Option<&Hash> {
self.dref().as_changes_trie_root()
}
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_changes_trie_signal(&self) -> Option<&ChangesTrieSignal> {
self.dref().as_changes_trie_signal()
}
pub fn as_other(&self) -> Option<&[u8]> {
match *self {
DigestItem::Other(ref v) => Some(&v[..]),
_ => None,
}
}
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)
}
}
impl<Hash: Encode> Encode for DigestItem<Hash> {
fn encode(&self) -> Vec<u8> {
self.dref().encode()
}
}
impl<Hash: Encode> codec::EncodeLike for DigestItem<Hash> {}
impl<Hash: Decode> Decode for DigestItem<Hash> {
#[allow(deprecated)]
fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
let item_type: DigestItemType = Decode::decode(input)?;
match item_type {
DigestItemType::ChangesTrieRoot => Ok(DigestItem::ChangesTrieRoot(
Decode::decode(input)?,
)),
DigestItemType::PreRuntime => {
let vals: (ConsensusEngineId, Vec<u8>) = Decode::decode(input)?;
Ok(DigestItem::PreRuntime(vals.0, vals.1))
},
DigestItemType::Consensus => {
let vals: (ConsensusEngineId, Vec<u8>) = Decode::decode(input)?;
Ok(DigestItem::Consensus(vals.0, vals.1))
}
DigestItemType::Seal => {
let vals: (ConsensusEngineId, Vec<u8>) = Decode::decode(input)?;
Ok(DigestItem::Seal(vals.0, vals.1))
},
DigestItemType::ChangesTrieSignal => Ok(DigestItem::ChangesTrieSignal(
Decode::decode(input)?,
)),
DigestItemType::Other => Ok(DigestItem::Other(
Decode::decode(input)?,
)),
}
}
}
impl<'a, Hash> DigestItemRef<'a, Hash> {
pub fn as_changes_trie_root(&self) -> Option<&'a Hash> {
match *self {
DigestItemRef::ChangesTrieRoot(ref changes_trie_root) => Some(changes_trie_root),
_ => None,
}
}
pub fn as_pre_runtime(&self) -> Option<(ConsensusEngineId, &'a [u8])> {
match *self {
DigestItemRef::PreRuntime(consensus_engine_id, ref data) => Some((*consensus_engine_id, data)),
_ => None,
}
}
pub fn as_consensus(&self) -> Option<(ConsensusEngineId, &'a [u8])> {
match *self {
DigestItemRef::Consensus(consensus_engine_id, ref data) => Some((*consensus_engine_id, data)),
_ => None,
}
}
pub fn as_seal(&self) -> Option<(ConsensusEngineId, &'a [u8])> {
match *self {
DigestItemRef::Seal(consensus_engine_id, ref data) => Some((*consensus_engine_id, data)),
_ => None,
}
}
pub fn as_changes_trie_signal(&self) -> Option<&'a ChangesTrieSignal> {
match *self {
DigestItemRef::ChangesTrieSignal(ref changes_trie_signal) => Some(changes_trie_signal),
_ => None,
}
}
pub fn as_other(&self) -> Option<&'a [u8]> {
match *self {
DigestItemRef::Other(ref data) => Some(data),
_ => None,
}
}
pub fn try_as_raw(&self, id: OpaqueDigestItemId) -> Option<&'a [u8]> {
match (id, self) {
(OpaqueDigestItemId::Consensus(w), &DigestItemRef::Consensus(v, s)) |
(OpaqueDigestItemId::Seal(w), &DigestItemRef::Seal(v, s)) |
(OpaqueDigestItemId::PreRuntime(w), &DigestItemRef::PreRuntime(v, s))
if v == w => Some(&s[..]),
(OpaqueDigestItemId::Other, &DigestItemRef::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| Decode::decode(&mut x).ok())
}
}
impl<'a, Hash: Encode> Encode for DigestItemRef<'a, Hash> {
fn encode(&self) -> Vec<u8> {
let mut v = Vec::new();
match *self {
DigestItemRef::ChangesTrieRoot(changes_trie_root) => {
DigestItemType::ChangesTrieRoot.encode_to(&mut v);
changes_trie_root.encode_to(&mut v);
},
DigestItemRef::Consensus(val, data) => {
DigestItemType::Consensus.encode_to(&mut v);
(val, data).encode_to(&mut v);
},
DigestItemRef::Seal(val, sig) => {
DigestItemType::Seal.encode_to(&mut v);
(val, sig).encode_to(&mut v);
},
DigestItemRef::PreRuntime(val, data) => {
DigestItemType::PreRuntime.encode_to(&mut v);
(val, data).encode_to(&mut v);
},
DigestItemRef::ChangesTrieSignal(changes_trie_signal) => {
DigestItemType::ChangesTrieSignal.encode_to(&mut v);
changes_trie_signal.encode_to(&mut v);
},
DigestItemRef::Other(val) => {
DigestItemType::Other.encode_to(&mut v);
val.encode_to(&mut v);
},
}
v
}
}
impl ChangesTrieSignal {
pub fn as_new_configuration(&self) -> Option<&Option<ChangesTrieConfiguration>> {
match self {
ChangesTrieSignal::NewConfiguration(config) => Some(config),
}
}
}
impl<'a, Hash: Encode> codec::EncodeLike for DigestItemRef<'a, Hash> {}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn should_serialize_digest() {
let digest = Digest {
logs: vec![
DigestItem::ChangesTrieRoot(4),
DigestItem::Other(vec![1, 2, 3]),
DigestItem::Seal(*b"test", vec![1, 2, 3])
],
};
assert_eq!(
::serde_json::to_string(&digest).unwrap(),
r#"{"logs":["0x0204000000","0x000c010203","0x05746573740c010203"]}"#
);
}
}