From 90512160882ec3edbda7bb79ecb4d3cdc51ef00d Mon Sep 17 00:00:00 2001 From: arvidn Date: Thu, 15 Dec 2022 13:22:55 +0100 Subject: [PATCH] introduce streamable_struct macro, to make it more succinct to define new streamable types. The main fix is to make all members public --- chia-protocol/src/coin.rs | 21 ++++++++++----------- chia-protocol/src/coin_state.rs | 7 +++---- chia-protocol/src/lib.rs | 10 ++++++---- chia-protocol/src/message_struct.rs | 10 ++++++++++ chia-protocol/src/respond_to_ph_updates.rs | 7 +++---- wheel/generate_type_stubs.py | 2 ++ 6 files changed, 34 insertions(+), 23 deletions(-) create mode 100644 chia-protocol/src/message_struct.rs diff --git a/chia-protocol/src/coin.rs b/chia-protocol/src/coin.rs index 3e6c97198..f83bde3fe 100644 --- a/chia-protocol/src/coin.rs +++ b/chia-protocol/src/coin.rs @@ -1,28 +1,27 @@ +use crate::bytes::Bytes32; +use crate::chia_error; +use crate::streamable::Streamable; +use crate::streamable_struct; +use chia_streamable_macro::Streamable; +use sha2::{Digest, Sha256}; +use std::convert::TryInto; + #[cfg(feature = "py-bindings")] use crate::from_json_dict::FromJsonDict; #[cfg(feature = "py-bindings")] use crate::to_json_dict::ToJsonDict; #[cfg(feature = "py-bindings")] use chia_py_streamable_macro::PyStreamable; -use chia_streamable_macro::Streamable; - -use crate::bytes::Bytes32; -use crate::chia_error; -use crate::streamable::Streamable; #[cfg(feature = "py-bindings")] use pyo3::prelude::*; #[cfg(feature = "py-bindings")] use pyo3::types::PyBytes; -use sha2::{Digest, Sha256}; -use std::convert::TryInto; -#[cfg_attr(feature = "py-bindings", pyclass, derive(PyStreamable))] -#[derive(Streamable, Hash, Debug, Clone, Eq, PartialEq)] -pub struct Coin { +streamable_struct!(Coin { parent_coin_info: Bytes32, puzzle_hash: Bytes32, amount: u64, -} +}); impl Coin { pub fn coin_id(&self) -> [u8; 32] { diff --git a/chia-protocol/src/coin_state.rs b/chia-protocol/src/coin_state.rs index 10c412d4f..f10894750 100644 --- a/chia-protocol/src/coin_state.rs +++ b/chia-protocol/src/coin_state.rs @@ -1,6 +1,7 @@ use crate::chia_error; use crate::coin::Coin; use crate::streamable::Streamable; +use crate::streamable_struct; use chia_streamable_macro::Streamable; #[cfg(feature = "py-bindings")] @@ -12,10 +13,8 @@ use chia_py_streamable_macro::PyStreamable; #[cfg(feature = "py-bindings")] use pyo3::prelude::*; -#[cfg_attr(feature = "py-bindings", pyclass, derive(PyStreamable))] -#[derive(Streamable, Hash, Debug, Clone, Eq, PartialEq)] -pub struct CoinState { +streamable_struct! (CoinState { coin: Coin, spent_height: Option, created_height: Option, -} +}); diff --git a/chia-protocol/src/lib.rs b/chia-protocol/src/lib.rs index 4d15fecae..ce64a099e 100644 --- a/chia-protocol/src/lib.rs +++ b/chia-protocol/src/lib.rs @@ -1,11 +1,13 @@ +#[cfg(feature = "py-bindings")] +pub mod from_json_dict; +#[cfg(feature = "py-bindings")] +pub mod to_json_dict; + pub mod bls; pub mod bytes; pub mod chia_error; pub mod coin; pub mod coin_state; -#[cfg(feature = "py-bindings")] -pub mod from_json_dict; +pub mod message_struct; pub mod respond_to_ph_updates; pub mod streamable; -#[cfg(feature = "py-bindings")] -pub mod to_json_dict; diff --git a/chia-protocol/src/message_struct.rs b/chia-protocol/src/message_struct.rs new file mode 100644 index 000000000..c8fed7080 --- /dev/null +++ b/chia-protocol/src/message_struct.rs @@ -0,0 +1,10 @@ +#[macro_export] +macro_rules! streamable_struct { + ($name:ident {$($field:ident: $t:ty $(,)? )*}) => { + #[cfg_attr(feature = "py-bindings", pyclass, derive(PyStreamable))] + #[derive(Streamable, Hash, Debug, Clone, Eq, PartialEq)] + pub struct $name { + $(pub $field: $t),* + } + } +} diff --git a/chia-protocol/src/respond_to_ph_updates.rs b/chia-protocol/src/respond_to_ph_updates.rs index 2be090c38..9a3963eac 100644 --- a/chia-protocol/src/respond_to_ph_updates.rs +++ b/chia-protocol/src/respond_to_ph_updates.rs @@ -2,6 +2,7 @@ use crate::bytes::Bytes32; use crate::chia_error; use crate::coin_state::CoinState; use crate::streamable::Streamable; +use crate::streamable_struct; use chia_streamable_macro::Streamable; #[cfg(feature = "py-bindings")] @@ -13,10 +14,8 @@ use chia_py_streamable_macro::PyStreamable; #[cfg(feature = "py-bindings")] use pyo3::prelude::*; -#[cfg_attr(feature = "py-bindings", pyclass(unsendable), derive(PyStreamable))] -#[derive(Streamable, Hash, Debug, Clone, Eq, PartialEq)] -pub struct RespondToPhUpdates { +streamable_struct! (RespondToPhUpdates { puzzle_hashes: Vec, min_height: u32, coin_states: Vec, -} +}); diff --git a/wheel/generate_type_stubs.py b/wheel/generate_type_stubs.py index 877c63b6f..7620a575a 100644 --- a/wheel/generate_type_stubs.py +++ b/wheel/generate_type_stubs.py @@ -71,6 +71,8 @@ def parse_rust_source(filename: str) -> List[Tuple[str, List[str]]]: if not in_struct: if line.startswith("pub struct ") and "{" in line: in_struct = line.split("pub struct ")[1].split("{")[0].strip() + elif line.startswith("streamable_struct! (") and "{" in line: + in_struct = line.split("streamable_struct! (")[1].split("{")[0].strip() elif line.startswith("pub struct ") and "(" in line and ");" in line: name = line.split("pub struct ")[1].split("(")[0].strip() rust_args = line.split("(")[1].split(");")[0]