Skip to content

Commit

Permalink
Add "serde" feature to derive serde::Serialize for Streamable
Browse files Browse the repository at this point in the history
  • Loading branch information
xearl4 committed Feb 1, 2023
1 parent ae0f25d commit 5dbe0f0
Show file tree
Hide file tree
Showing 24 changed files with 70 additions and 0 deletions.
2 changes: 2 additions & 0 deletions chia-protocol/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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"]
4 changes: 4 additions & 0 deletions chia-protocol/src/bls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
18 changes: 18 additions & 0 deletions chia-protocol/src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8>);
Expand Down Expand Up @@ -72,6 +74,14 @@ impl fmt::Display for Bytes {
}
}

#[cfg(feature = "serde")]
impl Serialize for Bytes {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
let s = format!("0x{}", hex::encode(&self.0));
serializer.serialize_str(&s)
}
}

#[derive(Hash, PartialEq, Eq, Copy, Clone, PartialOrd, Ord)]
pub struct BytesImpl<const N: usize>([u8; N]);

Expand Down Expand Up @@ -159,6 +169,14 @@ impl<const N: usize> fmt::Display for BytesImpl<N> {
}
}

#[cfg(feature = "serde")]
impl<const N: usize> Serialize for BytesImpl<N> {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
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>;
Expand Down
3 changes: 3 additions & 0 deletions chia-protocol/src/chia_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions chia-protocol/src/chia_protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions chia-protocol/src/classgroup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 });

Expand Down
2 changes: 2 additions & 0 deletions chia-protocol/src/coin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions chia-protocol/src/coin_spend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions chia-protocol/src/coin_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions chia-protocol/src/end_of_sub_slot_bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions chia-protocol/src/fee_estimate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions chia-protocol/src/foliage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions chia-protocol/src/fullblock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<EndOfSubSlotBundle>,
Expand Down
2 changes: 2 additions & 0 deletions chia-protocol/src/header_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions chia-protocol/src/message_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),*
Expand All @@ -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),*
Expand Down
2 changes: 2 additions & 0 deletions chia-protocol/src/pool_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions chia-protocol/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 2 additions & 0 deletions chia-protocol/src/proof_of_space.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions chia-protocol/src/reward_chain_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions chia-protocol/src/slots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions chia-protocol/src/spend_bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<CoinSpend>,
Expand Down
2 changes: 2 additions & 0 deletions chia-protocol/src/vdf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions chia-protocol/src/wallet_protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions chia-protocol/src/weight_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ProofOfSpace>,
Expand Down

0 comments on commit 5dbe0f0

Please sign in to comment.