mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-24 11:21:08 +00:00
Serialize encoded logs. (#1478)
This commit is contained in:
@@ -19,7 +19,7 @@
|
|||||||
use rstd::prelude::*;
|
use rstd::prelude::*;
|
||||||
|
|
||||||
use codec::{Decode, Encode, Codec, Input};
|
use codec::{Decode, Encode, Codec, Input};
|
||||||
use traits::{self, Member, DigestItem as DigestItemT, MaybeSerializeDebug, MaybeHash};
|
use traits::{self, Member, DigestItem as DigestItemT, MaybeHash};
|
||||||
|
|
||||||
use substrate_primitives::hash::H512 as Signature;
|
use substrate_primitives::hash::H512 as Signature;
|
||||||
|
|
||||||
@@ -57,7 +57,7 @@ impl<Item> traits::Digest for Digest<Item> where
|
|||||||
/// Digest item that is able to encode/decode 'system' digest items and
|
/// Digest item that is able to encode/decode 'system' digest items and
|
||||||
/// provide opaque access to other items.
|
/// provide opaque access to other items.
|
||||||
#[derive(PartialEq, Eq, Clone)]
|
#[derive(PartialEq, Eq, Clone)]
|
||||||
#[cfg_attr(feature = "std", derive(Debug, Serialize))]
|
#[cfg_attr(feature = "std", derive(Debug))]
|
||||||
pub enum DigestItem<Hash, AuthorityId> {
|
pub enum DigestItem<Hash, AuthorityId> {
|
||||||
/// System digest item announcing that authorities set has been changed
|
/// System digest item announcing that authorities set has been changed
|
||||||
/// in the block. Contains the new set of authorities.
|
/// in the block. Contains the new set of authorities.
|
||||||
@@ -72,6 +72,15 @@ pub enum DigestItem<Hash, AuthorityId> {
|
|||||||
Other(Vec<u8>),
|
Other(Vec<u8>),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "std")]
|
||||||
|
impl<Hash: Encode, AuthorityId: Encode> ::serde::Serialize for DigestItem<Hash, AuthorityId> {
|
||||||
|
fn serialize<S>(&self, seq: S) -> Result<S::Ok, S::Error> where S: ::serde::Serializer {
|
||||||
|
self.using_encoded(|bytes| {
|
||||||
|
::substrate_primitives::bytes::serialize(bytes, seq)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/// A 'referencing view' for digest item. Does not own its contents. Used by
|
/// A 'referencing view' for digest item. Does not own its contents. Used by
|
||||||
/// final runtime implementations for encoding/decoding its log items.
|
/// final runtime implementations for encoding/decoding its log items.
|
||||||
@@ -123,8 +132,8 @@ impl<Hash, AuthorityId> DigestItem<Hash, AuthorityId> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<
|
impl<
|
||||||
Hash: Codec + Member + MaybeSerializeDebug,
|
Hash: Codec + Member,
|
||||||
AuthorityId: Codec + Member + MaybeSerializeDebug + MaybeHash
|
AuthorityId: Codec + Member + MaybeHash,
|
||||||
> traits::DigestItem for DigestItem<Hash, AuthorityId> {
|
> traits::DigestItem for DigestItem<Hash, AuthorityId> {
|
||||||
type Hash = Hash;
|
type Hash = Hash;
|
||||||
type AuthorityId = AuthorityId;
|
type AuthorityId = AuthorityId;
|
||||||
@@ -207,3 +216,25 @@ impl<'a, Hash: Encode, AuthorityId: Encode> Encode for DigestItemRef<'a, Hash, A
|
|||||||
v
|
v
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn should_serialize_digest() {
|
||||||
|
let digest = Digest {
|
||||||
|
logs: vec![
|
||||||
|
DigestItem::AuthoritiesChange(vec![1]),
|
||||||
|
DigestItem::ChangesTrieRoot(4),
|
||||||
|
DigestItem::Seal(1, 15.into()),
|
||||||
|
DigestItem::Other(vec![1, 2, 3]),
|
||||||
|
],
|
||||||
|
};
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
::serde_json::to_string(&digest).unwrap(),
|
||||||
|
r#"{"logs":["0x010401000000","0x0204000000","0x0301000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000f","0x000c010203"]}"#
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -588,7 +588,7 @@ pub trait Applyable: Sized + Send + Sync {
|
|||||||
/// Something that acts like a `Digest` - it can have `Log`s `push`ed onto it and these `Log`s are
|
/// Something that acts like a `Digest` - it can have `Log`s `push`ed onto it and these `Log`s are
|
||||||
/// each `Codec`.
|
/// each `Codec`.
|
||||||
pub trait Digest: Member + MaybeSerializeDebugButNotDeserialize + Default {
|
pub trait Digest: Member + MaybeSerializeDebugButNotDeserialize + Default {
|
||||||
type Hash: Member + MaybeSerializeDebugButNotDeserialize;
|
type Hash: Member;
|
||||||
type Item: DigestItem<Hash = Self::Hash>;
|
type Item: DigestItem<Hash = Self::Hash>;
|
||||||
|
|
||||||
/// Get reference to all digest items.
|
/// Get reference to all digest items.
|
||||||
@@ -611,8 +611,8 @@ pub trait Digest: Member + MaybeSerializeDebugButNotDeserialize + Default {
|
|||||||
///
|
///
|
||||||
/// If the runtime does not supports some 'system' items, use `()` as a stub.
|
/// If the runtime does not supports some 'system' items, use `()` as a stub.
|
||||||
pub trait DigestItem: Codec + Member + MaybeSerializeDebugButNotDeserialize {
|
pub trait DigestItem: Codec + Member + MaybeSerializeDebugButNotDeserialize {
|
||||||
type Hash: Member + MaybeSerializeDebugButNotDeserialize;
|
type Hash: Member;
|
||||||
type AuthorityId: Member + MaybeSerializeDebugButNotDeserialize + MaybeHash + codec::Encode + codec::Decode;
|
type AuthorityId: Member + MaybeHash + codec::Encode + codec::Decode;
|
||||||
|
|
||||||
/// Returns Some if the entry is the `AuthoritiesChange` entry.
|
/// Returns Some if the entry is the `AuthoritiesChange` entry.
|
||||||
fn as_authorities_change(&self) -> Option<&[Self::AuthorityId]>;
|
fn as_authorities_change(&self) -> Option<&[Self::AuthorityId]>;
|
||||||
|
|||||||
Reference in New Issue
Block a user