From a14d3071ddbdc032f1f40fa4267c2e074b2c220c 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..d216d9c71 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..051285b86 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..3ee4f2745 100644 --- a/chia-protocol/src/chia_protocol.rs +++ b/chia-protocol/src/chia_protocol.rs @@ -14,11 +14,14 @@ 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(feature = "py-bindings")] use std::io::Cursor; #[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..34f18d0ca 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..e8a54a62c 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..c95e5dfb5 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..ddaeb8b62 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..cff201677 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..c8802bf24 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..5f1bbff0f 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..0fbd7c0f2 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..5f7a6d00d 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..7594a99f7 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..75dfa1e8d 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..384f33615 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..45f960ed5 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..98cbe153f 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..3b195161f 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..d98a13c53 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..87e25071b 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..55902917f 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,