From 5dbe0f07967fd57bc951d7412b3f42c763520644 Mon Sep 17 00:00:00 2001 From: "xchdata.io" Date: Mon, 16 Jan 2023 17:34:53 +0100 Subject: [PATCH] Add "serde" feature to derive serde::Serialize for Streamable --- chia-protocol/Cargo.toml | 2 ++ chia-protocol/src/bls.rs | 4 ++++ chia-protocol/src/bytes.rs | 18 ++++++++++++++++++ chia-protocol/src/chia_error.rs | 3 +++ chia-protocol/src/chia_protocol.rs | 4 ++++ chia-protocol/src/classgroup.rs | 2 ++ chia-protocol/src/coin.rs | 2 ++ chia-protocol/src/coin_spend.rs | 2 ++ chia-protocol/src/coin_state.rs | 2 ++ chia-protocol/src/end_of_sub_slot_bundle.rs | 2 ++ chia-protocol/src/fee_estimate.rs | 2 ++ chia-protocol/src/foliage.rs | 2 ++ chia-protocol/src/fullblock.rs | 2 ++ chia-protocol/src/header_block.rs | 2 ++ chia-protocol/src/message_struct.rs | 2 ++ chia-protocol/src/pool_target.rs | 2 ++ chia-protocol/src/program.rs | 3 +++ chia-protocol/src/proof_of_space.rs | 2 ++ chia-protocol/src/reward_chain_block.rs | 2 ++ chia-protocol/src/slots.rs | 2 ++ chia-protocol/src/spend_bundle.rs | 2 ++ chia-protocol/src/vdf.rs | 2 ++ chia-protocol/src/wallet_protocol.rs | 2 ++ chia-protocol/src/weight_proof.rs | 2 ++ 24 files changed, 70 insertions(+) diff --git a/chia-protocol/Cargo.toml b/chia-protocol/Cargo.toml index 11f1e2b32..8bdd7150a 100644 --- a/chia-protocol/Cargo.toml +++ b/chia-protocol/Cargo.toml @@ -10,6 +10,7 @@ repository = "https://github.com/Chia-Network/chia_rs/chia-protocol/" [features] py-bindings = ["dep:pyo3", "dep:chia_py_streamable_macro"] +serde = ["dep:serde"] [dependencies] pyo3 = { version = "=0.15.1", features = ["extension-module", "multiple-pymethods"], optional = true } @@ -18,6 +19,7 @@ hex = "=0.4.3" chia_streamable_macro = { version = "=0.2.4", path = "../chia_streamable_macro" } chia_py_streamable_macro = { path = "../chia_py_streamable_macro", version = "=0.1.3", optional = true } clvmr = "=0.2.2" +serde = { version = "1.0.152", features = ["derive"], optional = true } [lib] crate-type = ["rlib"] diff --git a/chia-protocol/src/bls.rs b/chia-protocol/src/bls.rs index de1a8d0de..b4fbc3b5a 100644 --- a/chia-protocol/src/bls.rs +++ b/chia-protocol/src/bls.rs @@ -11,11 +11,15 @@ use crate::to_json_dict::ToJsonDict; use chia_py_streamable_macro::PyStreamable; #[cfg(feature = "py-bindings")] use pyo3::prelude::*; +#[cfg(feature = "serde")] +use serde::{Serialize}; #[cfg_attr(feature = "py-bindings", pyclass, derive(PyStreamable))] +#[cfg_attr(feature = "serde", derive(Serialize))] #[derive(Streamable, Hash, Debug, Clone, Eq, PartialEq)] pub struct G1Element(Bytes48); #[cfg_attr(feature = "py-bindings", pyclass, derive(PyStreamable))] +#[cfg_attr(feature = "serde", derive(Serialize))] #[derive(Streamable, Hash, Debug, Clone, Eq, PartialEq)] pub struct G2Element(Bytes96); diff --git a/chia-protocol/src/bytes.rs b/chia-protocol/src/bytes.rs index f3edf200a..05913fac7 100644 --- a/chia-protocol/src/bytes.rs +++ b/chia-protocol/src/bytes.rs @@ -13,6 +13,8 @@ use std::ops::Deref; use pyo3::prelude::*; #[cfg(feature = "py-bindings")] use pyo3::types::PyBytes; +#[cfg(feature = "serde")] +use serde::{Serialize, Serializer}; #[derive(Hash, Debug, Clone, Eq, PartialEq, PartialOrd, Ord)] pub struct Bytes(Vec); @@ -72,6 +74,14 @@ impl fmt::Display for Bytes { } } +#[cfg(feature = "serde")] +impl Serialize for Bytes { + fn serialize(&self, serializer: S) -> Result { + let s = format!("0x{}", hex::encode(&self.0)); + serializer.serialize_str(&s) + } +} + #[derive(Hash, PartialEq, Eq, Copy, Clone, PartialOrd, Ord)] pub struct BytesImpl([u8; N]); @@ -159,6 +169,14 @@ impl fmt::Display for BytesImpl { } } +#[cfg(feature = "serde")] +impl Serialize for BytesImpl { + fn serialize(&self, serializer: S) -> Result { + let s = format!("0x{}", hex::encode(&self.0)); + serializer.serialize_str(&s) + } +} + pub type Bytes32 = BytesImpl<32>; pub type Bytes48 = BytesImpl<48>; pub type Bytes96 = BytesImpl<96>; diff --git a/chia-protocol/src/chia_error.rs b/chia-protocol/src/chia_error.rs index 7e873732e..b3b463218 100644 --- a/chia-protocol/src/chia_error.rs +++ b/chia-protocol/src/chia_error.rs @@ -5,7 +5,10 @@ use std::fmt; use pyo3::exceptions; #[cfg(feature = "py-bindings")] use pyo3::PyErr; +#[cfg(feature = "serde")] +use serde::{Serialize}; +#[cfg_attr(feature = "serde", derive(Serialize))] #[derive(Debug, PartialEq, Eq)] pub enum Error { InvalidBool, diff --git a/chia-protocol/src/chia_protocol.rs b/chia-protocol/src/chia_protocol.rs index 672ef6ac7..ba1b8b582 100644 --- a/chia-protocol/src/chia_protocol.rs +++ b/chia-protocol/src/chia_protocol.rs @@ -16,9 +16,12 @@ use chia_py_streamable_macro::PyStreamable; use pyo3::prelude::*; #[cfg(feature = "py-bindings")] use std::io::Cursor; +#[cfg(feature = "serde")] +use serde::{Serialize}; #[repr(u8)] #[cfg_attr(feature = "py-bindings", derive(PyStreamable))] +#[cfg_attr(feature = "serde", derive(Serialize))] #[derive(Streamable, Hash, Debug, Copy, Clone, Eq, PartialEq)] pub enum ProtocolMessageTypes { // Shared protocol (all services) @@ -138,6 +141,7 @@ pub trait ChiaProtocolMessage { #[repr(u8)] #[cfg_attr(feature = "py-bindings", derive(PyStreamable))] +#[cfg_attr(feature = "serde", derive(Serialize))] #[derive(Streamable, Hash, Debug, Copy, Clone, Eq, PartialEq)] pub enum NodeType { FullNode = 1, diff --git a/chia-protocol/src/classgroup.rs b/chia-protocol/src/classgroup.rs index 2c6224919..66a78cf26 100644 --- a/chia-protocol/src/classgroup.rs +++ b/chia-protocol/src/classgroup.rs @@ -12,6 +12,8 @@ use crate::to_json_dict::ToJsonDict; use chia_py_streamable_macro::PyStreamable; #[cfg(feature = "py-bindings")] use pyo3::prelude::*; +#[cfg(feature = "serde")] +use serde::{Serialize}; streamable_struct!(ClassgroupElement { data: Bytes100 }); diff --git a/chia-protocol/src/coin.rs b/chia-protocol/src/coin.rs index f83bde3fe..c93396842 100644 --- a/chia-protocol/src/coin.rs +++ b/chia-protocol/src/coin.rs @@ -16,6 +16,8 @@ use chia_py_streamable_macro::PyStreamable; use pyo3::prelude::*; #[cfg(feature = "py-bindings")] use pyo3::types::PyBytes; +#[cfg(feature = "serde")] +use serde::{Serialize}; streamable_struct!(Coin { parent_coin_info: Bytes32, diff --git a/chia-protocol/src/coin_spend.rs b/chia-protocol/src/coin_spend.rs index e4e81ea4e..23a91487a 100644 --- a/chia-protocol/src/coin_spend.rs +++ b/chia-protocol/src/coin_spend.rs @@ -14,6 +14,8 @@ use crate::to_json_dict::ToJsonDict; use chia_py_streamable_macro::PyStreamable; #[cfg(feature = "py-bindings")] use pyo3::prelude::*; +#[cfg(feature = "serde")] +use serde::{Serialize}; streamable_struct!(CoinSpend { coin: Coin, diff --git a/chia-protocol/src/coin_state.rs b/chia-protocol/src/coin_state.rs index f10894750..8791eab64 100644 --- a/chia-protocol/src/coin_state.rs +++ b/chia-protocol/src/coin_state.rs @@ -12,6 +12,8 @@ use crate::to_json_dict::ToJsonDict; use chia_py_streamable_macro::PyStreamable; #[cfg(feature = "py-bindings")] use pyo3::prelude::*; +#[cfg(feature = "serde")] +use serde::{Serialize}; streamable_struct! (CoinState { coin: Coin, diff --git a/chia-protocol/src/end_of_sub_slot_bundle.rs b/chia-protocol/src/end_of_sub_slot_bundle.rs index 5900eb64d..72f77ac46 100644 --- a/chia-protocol/src/end_of_sub_slot_bundle.rs +++ b/chia-protocol/src/end_of_sub_slot_bundle.rs @@ -16,6 +16,8 @@ use crate::to_json_dict::ToJsonDict; use chia_py_streamable_macro::PyStreamable; #[cfg(feature = "py-bindings")] use pyo3::prelude::*; +#[cfg(feature = "serde")] +use serde::{Serialize}; streamable_struct! (EndOfSubSlotBundle { challenge_chain: ChallengeChainSubSlot, diff --git a/chia-protocol/src/fee_estimate.rs b/chia-protocol/src/fee_estimate.rs index 7b076b810..1b3da079e 100644 --- a/chia-protocol/src/fee_estimate.rs +++ b/chia-protocol/src/fee_estimate.rs @@ -12,6 +12,8 @@ use crate::to_json_dict::ToJsonDict; use chia_py_streamable_macro::PyStreamable; #[cfg(feature = "py-bindings")] use pyo3::prelude::*; +#[cfg(feature = "serde")] +use serde::{Serialize}; streamable_struct!(FeeRate { // Represents Fee Rate in mojos divided by CLVM Cost. diff --git a/chia-protocol/src/foliage.rs b/chia-protocol/src/foliage.rs index ae57e3300..a51321b19 100644 --- a/chia-protocol/src/foliage.rs +++ b/chia-protocol/src/foliage.rs @@ -16,6 +16,8 @@ use crate::to_json_dict::ToJsonDict; use chia_py_streamable_macro::PyStreamable; #[cfg(feature = "py-bindings")] use pyo3::prelude::*; +#[cfg(feature = "serde")] +use serde::{Serialize}; streamable_struct! (TransactionsInfo { // Information that goes along with each transaction block diff --git a/chia-protocol/src/fullblock.rs b/chia-protocol/src/fullblock.rs index a0a3df6b4..463278a59 100644 --- a/chia-protocol/src/fullblock.rs +++ b/chia-protocol/src/fullblock.rs @@ -17,6 +17,8 @@ use crate::to_json_dict::ToJsonDict; use chia_py_streamable_macro::PyStreamable; #[cfg(feature = "py-bindings")] use pyo3::prelude::*; +#[cfg(feature = "serde")] +use serde::{Serialize}; streamable_struct! (FullBlock { finished_sub_slots: Vec, diff --git a/chia-protocol/src/header_block.rs b/chia-protocol/src/header_block.rs index dedf71bc4..dca59ba03 100644 --- a/chia-protocol/src/header_block.rs +++ b/chia-protocol/src/header_block.rs @@ -17,6 +17,8 @@ use crate::to_json_dict::ToJsonDict; use chia_py_streamable_macro::PyStreamable; #[cfg(feature = "py-bindings")] use pyo3::prelude::*; +#[cfg(feature = "serde")] +use serde::{Serialize}; streamable_struct! (HeaderBlock { // If first sb diff --git a/chia-protocol/src/message_struct.rs b/chia-protocol/src/message_struct.rs index d04685e9e..3368225ed 100644 --- a/chia-protocol/src/message_struct.rs +++ b/chia-protocol/src/message_struct.rs @@ -2,6 +2,7 @@ macro_rules! message_struct { ($name:ident {$($field:ident: $t:ty $(,)? )*}) => { #[cfg_attr(feature = "py-bindings", pyclass, derive(PyStreamable))] + #[cfg_attr(feature = "serde", derive(Serialize))] #[derive(Streamable, Hash, Debug, Clone, Eq, PartialEq)] pub struct $name { $(pub $field: $t),* @@ -19,6 +20,7 @@ macro_rules! message_struct { macro_rules! streamable_struct { ($name:ident {$($field:ident: $t:ty $(,)? )*}) => { #[cfg_attr(feature = "py-bindings", pyclass, derive(PyStreamable))] + #[cfg_attr(feature = "serde", derive(Serialize))] #[derive(Streamable, Hash, Debug, Clone, Eq, PartialEq)] pub struct $name { $(pub $field: $t),* diff --git a/chia-protocol/src/pool_target.rs b/chia-protocol/src/pool_target.rs index ce5607944..47501198b 100644 --- a/chia-protocol/src/pool_target.rs +++ b/chia-protocol/src/pool_target.rs @@ -13,6 +13,8 @@ use crate::to_json_dict::ToJsonDict; use chia_py_streamable_macro::PyStreamable; #[cfg(feature = "py-bindings")] use pyo3::prelude::*; +#[cfg(feature = "serde")] +use serde::{Serialize}; streamable_struct!(PoolTarget { puzzle_hash: Bytes32, diff --git a/chia-protocol/src/program.rs b/chia-protocol/src/program.rs index 3cce99080..b5565ad7e 100644 --- a/chia-protocol/src/program.rs +++ b/chia-protocol/src/program.rs @@ -15,8 +15,11 @@ use crate::to_json_dict::ToJsonDict; use chia_py_streamable_macro::PyStreamable; #[cfg(feature = "py-bindings")] use pyo3::prelude::*; +#[cfg(feature = "serde")] +use serde::{Serialize}; #[cfg_attr(feature = "py-bindings", pyclass, derive(PyStreamable))] +#[cfg_attr(feature = "serde", derive(Serialize))] #[derive(Hash, Debug, Clone, Eq, PartialEq)] pub struct Program(Bytes); diff --git a/chia-protocol/src/proof_of_space.rs b/chia-protocol/src/proof_of_space.rs index e9ffad647..dc4d73671 100644 --- a/chia-protocol/src/proof_of_space.rs +++ b/chia-protocol/src/proof_of_space.rs @@ -13,6 +13,8 @@ use crate::to_json_dict::ToJsonDict; use chia_py_streamable_macro::PyStreamable; #[cfg(feature = "py-bindings")] use pyo3::prelude::*; +#[cfg(feature = "serde")] +use serde::{Serialize}; streamable_struct! (ProofOfSpace { challenge: Bytes32, diff --git a/chia-protocol/src/reward_chain_block.rs b/chia-protocol/src/reward_chain_block.rs index 0442fdcd2..465cd6ab3 100644 --- a/chia-protocol/src/reward_chain_block.rs +++ b/chia-protocol/src/reward_chain_block.rs @@ -16,6 +16,8 @@ use crate::to_json_dict::ToJsonDict; use chia_py_streamable_macro::PyStreamable; #[cfg(feature = "py-bindings")] use pyo3::prelude::*; +#[cfg(feature = "serde")] +use serde::{Serialize}; streamable_struct! (RewardChainBlockUnfinished { total_iters: u128, diff --git a/chia-protocol/src/slots.rs b/chia-protocol/src/slots.rs index 68e1ff960..d90ffe440 100644 --- a/chia-protocol/src/slots.rs +++ b/chia-protocol/src/slots.rs @@ -17,6 +17,8 @@ use crate::to_json_dict::ToJsonDict; use chia_py_streamable_macro::PyStreamable; #[cfg(feature = "py-bindings")] use pyo3::prelude::*; +#[cfg(feature = "serde")] +use serde::{Serialize}; // The hash of this is used as the challenge_hash for the ICC VDF streamable_struct! (ChallengeBlockInfo { diff --git a/chia-protocol/src/spend_bundle.rs b/chia-protocol/src/spend_bundle.rs index d462b9669..6cd1ed5de 100644 --- a/chia-protocol/src/spend_bundle.rs +++ b/chia-protocol/src/spend_bundle.rs @@ -14,6 +14,8 @@ use crate::to_json_dict::ToJsonDict; use chia_py_streamable_macro::PyStreamable; #[cfg(feature = "py-bindings")] use pyo3::prelude::*; +#[cfg(feature = "serde")] +use serde::{Serialize}; streamable_struct! (SpendBundle { coin_spends: Vec, diff --git a/chia-protocol/src/vdf.rs b/chia-protocol/src/vdf.rs index 52c01cff4..91b1a797b 100644 --- a/chia-protocol/src/vdf.rs +++ b/chia-protocol/src/vdf.rs @@ -14,6 +14,8 @@ use crate::to_json_dict::ToJsonDict; use chia_py_streamable_macro::PyStreamable; #[cfg(feature = "py-bindings")] use pyo3::prelude::*; +#[cfg(feature = "serde")] +use serde::{Serialize}; streamable_struct!(VDFInfo { challenge: Bytes32, diff --git a/chia-protocol/src/wallet_protocol.rs b/chia-protocol/src/wallet_protocol.rs index 0e7769d35..664015446 100644 --- a/chia-protocol/src/wallet_protocol.rs +++ b/chia-protocol/src/wallet_protocol.rs @@ -22,6 +22,8 @@ use crate::to_json_dict::ToJsonDict; use chia_py_streamable_macro::PyStreamable; #[cfg(feature = "py-bindings")] use pyo3::prelude::*; +#[cfg(feature = "serde")] +use serde::{Serialize}; message_struct!(RequestPuzzleSolution { coin_name: Bytes32, diff --git a/chia-protocol/src/weight_proof.rs b/chia-protocol/src/weight_proof.rs index dadb873e5..37fe6609e 100644 --- a/chia-protocol/src/weight_proof.rs +++ b/chia-protocol/src/weight_proof.rs @@ -14,6 +14,8 @@ use crate::to_json_dict::ToJsonDict; use chia_py_streamable_macro::PyStreamable; #[cfg(feature = "py-bindings")] use pyo3::prelude::*; +#[cfg(feature = "serde")] +use serde::{Serialize}; streamable_struct! (SubSlotData { proof_of_space: Option,