Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Serde-serialisation of Streamable #127

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ rcgen = "0.13.1"
rsa = "0.9.5"
time = "0.3.22"
rusqlite = "0.31.0"
serde = { version = "1.0.198", features = ["derive"] }
clap = "4.5.8"
zstd = "0.13.2"
blocking-threadpool = "1.0.1"
Expand Down
2 changes: 2 additions & 0 deletions crates/chia-bls/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ workspace = true
[features]
py-bindings = ["dep:pyo3", "chia_py_streamable_macro", "chia-traits/py-bindings"]
arbitrary = ["dep:arbitrary"]
serde = ["dep:serde"]

[dependencies]
chia-traits = { workspace = true }
Expand All @@ -27,6 +28,7 @@ thiserror = { workspace = true }
pyo3 = { workspace = true, features = ["multiple-pymethods"], optional = true }
arbitrary = { workspace = true, optional = true }
lru = { workspace = true }
serde = { workspace = true, features = ["derive"], optional = true }

[dev-dependencies]
rand = { workspace = true }
Expand Down
10 changes: 10 additions & 0 deletions crates/chia-bls/src/public_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ impl<'a> arbitrary::Arbitrary<'a> for PublicKey {
}
}

#[cfg(feature = "serde")]
impl serde::Serialize for PublicKey {
fn serialize<S: serde::Serializer>(
&self,
serializer: S,
) -> std::result::Result<S::Ok, S::Error> {
serializer.serialize_str(&hex::encode(self.to_bytes()))
}
}

impl PublicKey {
pub fn from_bytes_unchecked(bytes: &[u8; 48]) -> Result<Self> {
// check if the element is canonical
Expand Down
10 changes: 10 additions & 0 deletions crates/chia-bls/src/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ impl<'a> arbitrary::Arbitrary<'a> for Signature {
}
}

#[cfg(feature = "serde")]
impl serde::Serialize for Signature {
fn serialize<S: serde::Serializer>(
&self,
serializer: S,
) -> std::result::Result<S::Ok, S::Error> {
serializer.serialize_str(&hex::encode(self.to_bytes()))
}
}

impl Signature {
pub fn from_bytes_unchecked(buf: &[u8; 96]) -> Result<Self> {
let p2 = unsafe {
Expand Down
2 changes: 2 additions & 0 deletions crates/chia-protocol/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ workspace = true
[features]
py-bindings = ["dep:pyo3", "dep:chia_py_streamable_macro", "chia-traits/py-bindings", "chia-bls/py-bindings"]
arbitrary = ["dep:arbitrary", "chia-bls/arbitrary"]
serde = ["dep:serde", "chia-bls/serde"]

[dependencies]
pyo3 = { workspace = true, features = ["multiple-pymethods", "num-bigint"], optional = true }
Expand All @@ -27,6 +28,7 @@ clvm-traits = { workspace = true, features = ["derive"] }
clvm-utils = { workspace = true }
chia-bls = { workspace = true }
arbitrary = { workspace = true, features = ["derive"], optional = true }
serde = { workspace = true, features = ["derive"], optional = true }

[dev-dependencies]
rstest = { workspace = true }
Expand Down
8 changes: 8 additions & 0 deletions crates/chia-protocol/src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use pyo3::types::PyBytes;

#[derive(Default, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct Bytes(Vec<u8>);

impl Bytes {
Expand Down Expand Up @@ -371,6 +372,13 @@ impl<const N: usize> Deref for BytesImpl<N> {
}
}

#[cfg(feature = "serde")]
impl<const N: usize> serde::Serialize for BytesImpl<N> {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(&hex::encode(self.0))
}
}

pub type Bytes32 = BytesImpl<32>;
pub type Bytes48 = BytesImpl<48>;
pub type Bytes96 = BytesImpl<96>;
Expand Down
2 changes: 2 additions & 0 deletions crates/chia-protocol/src/chia_protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use chia_py_streamable_macro::{PyJsonDict, PyStreamable};
#[repr(u8)]
#[cfg_attr(feature = "py-bindings", derive(PyJsonDict, PyStreamable))]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[derive(Streamable, Hash, Debug, Copy, Clone, Eq, PartialEq)]
pub enum ProtocolMessageTypes {
// Shared protocol (all services)
Expand Down Expand Up @@ -151,6 +152,7 @@ pub trait ChiaProtocolMessage {
#[repr(u8)]
#[cfg_attr(feature = "py-bindings", derive(PyJsonDict, PyStreamable))]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[derive(Streamable, Hash, Debug, Copy, Clone, Eq, PartialEq)]
pub enum NodeType {
FullNode = 1,
Expand Down
1 change: 1 addition & 0 deletions crates/chia-protocol/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use std::io::Cursor;
use std::ops::Deref;

#[cfg_attr(feature = "py-bindings", pyclass, derive(PyStreamable))]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Program(Bytes);

Expand Down
1 change: 1 addition & 0 deletions crates/chia-protocol/src/wallet_protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ use chia_py_streamable_macro::{PyJsonDict, PyStreamable};
#[cfg_attr(feature = "py-bindings", derive(PyJsonDict, PyStreamable))]
#[derive(Streamable, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub enum RejectStateReason {
Reorg = 0,
ExceededSubscriptionLimit = 1,
Expand Down
1 change: 1 addition & 0 deletions crates/chia_streamable_macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ pub fn streamable(attr: TokenStream, item: TokenStream) -> TokenStream {
)]
#main_derives
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
}
} else {
main_derives
Expand Down
Loading