From c610fbcc2222456fb0dc75d9d06be5110164751a Mon Sep 17 00:00:00 2001 From: arvidn Date: Tue, 15 Nov 2022 13:36:05 +0100 Subject: [PATCH] move Coin, CoinState and RespondToPhUpdates into chia-protocol --- .github/workflows/build-test.yml | 2 +- Cargo.toml | 2 +- chia-protocol/Cargo.toml | 5 ++-- {wheel => chia-protocol}/src/coin.rs | 30 +++++++++++-------- chia-protocol/src/coin_state.rs | 24 +++++++++++++++ .../src/from_json_dict.rs | 2 +- chia-protocol/src/lib.rs | 7 +++++ chia-protocol/src/respond_to_ph_updates.rs | 25 ++++++++++++++++ {wheel => chia-protocol}/src/to_json_dict.rs | 2 +- chia_py_streamable_macro/Cargo.toml | 16 ++++++++++ .../src/lib.rs | 6 ++-- wheel/Cargo.toml | 2 +- wheel/py_streamable/Cargo.toml | 11 ------- wheel/src/api.rs | 6 ++-- wheel/src/coin_state.rs | 20 ------------- wheel/src/lib.rs | 5 ---- wheel/src/respond_to_ph_updates.rs | 21 ------------- wheel/src/run_generator.rs | 6 ++-- 18 files changed, 107 insertions(+), 85 deletions(-) rename {wheel => chia-protocol}/src/coin.rs (64%) create mode 100644 chia-protocol/src/coin_state.rs rename {wheel => chia-protocol}/src/from_json_dict.rs (98%) create mode 100644 chia-protocol/src/respond_to_ph_updates.rs rename {wheel => chia-protocol}/src/to_json_dict.rs (97%) create mode 100644 chia_py_streamable_macro/Cargo.toml rename {wheel/py_streamable => chia_py_streamable_macro}/src/lib.rs (96%) delete mode 100644 wheel/py_streamable/Cargo.toml delete mode 100644 wheel/src/coin_state.rs delete mode 100644 wheel/src/respond_to_ph_updates.rs diff --git a/.github/workflows/build-test.yml b/.github/workflows/build-test.yml index 8db98df78..cb4d032de 100644 --- a/.github/workflows/build-test.yml +++ b/.github/workflows/build-test.yml @@ -333,7 +333,7 @@ jobs: with: toolchain: stable - name: cargo test - run: cargo test --workspace --all-features + run: cargo test --workspace upload: name: Upload to PyPI - ${{ matrix.os.name }} ${{ matrix.python.major-dot-minor }} ${{ matrix.arch.name }} diff --git a/Cargo.toml b/Cargo.toml index 27c71d5f1..aa9c6d356 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ # the "wheel" crate is excluded from the workspace because pyo3 has problems with # "cargo test" and "cargo bench" [workspace] -members = ["wasm", "chia_streamable_macro", "chia-bls", "clvm-utils", "chia-protocol"] +members = ["wasm", "chia_streamable_macro", "chia-bls", "clvm-utils", "chia-protocol", "chia_py_streamable_macro"] exclude = ["wheel"] [package] diff --git a/chia-protocol/Cargo.toml b/chia-protocol/Cargo.toml index 6cc765f1c..6dc7a418d 100644 --- a/chia-protocol/Cargo.toml +++ b/chia-protocol/Cargo.toml @@ -9,13 +9,14 @@ homepage = "https://github.com/Chia-Network/chia_rs/chia-protocol/" repository = "https://github.com/Chia-Network/chia_rs/chia-protocol/" [features] -py-bindings = ["dep:pyo3"] +py-bindings = ["dep:pyo3", "dep:chia_py_streamable_macro"] [dependencies] -pyo3 = { version = "=0.15.1", features = ["extension-module"], optional = true } +pyo3 = { version = "=0.15.1", features = ["extension-module", "multiple-pymethods"], optional = true } sha2 = "=0.10.2" hex = "=0.4.3" chia_streamable_macro = { version = "=0.2.3", path = "../chia_streamable_macro" } +chia_py_streamable_macro = { path = "../chia_py_streamable_macro", version = "=0.1.0", optional = true } clvmr = "=0.1.24" [lib] diff --git a/wheel/src/coin.rs b/chia-protocol/src/coin.rs similarity index 64% rename from wheel/src/coin.rs rename to chia-protocol/src/coin.rs index 531595dca..32e8c2110 100644 --- a/wheel/src/coin.rs +++ b/chia-protocol/src/coin.rs @@ -1,29 +1,34 @@ +#[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 py_streamable::PyStreamable; -use chia_protocol::bytes::Bytes32; -use chia_protocol::chia_error; -use chia_protocol::streamable::Streamable; -use clvmr::sha2::{Digest, Sha256}; +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; -#[pyclass] -#[derive(Streamable, PyStreamable, Hash, Debug, Clone, Eq, PartialEq)] +#[cfg_attr(feature = "py-bindings", pyclass, derive(PyStreamable))] +#[derive(Streamable, Hash, Debug, Clone, Eq, PartialEq)] pub struct Coin { - #[pyo3(get)] + #[cfg_attr(features = "py-bindings", pyo3(get))] parent_coin_info: Bytes32, - #[pyo3(get)] + #[cfg_attr(features = "py-bindings", pyo3(get))] puzzle_hash: Bytes32, - #[pyo3(get)] + #[cfg_attr(features = "py-bindings", pyo3(get))] amount: u64, } impl Coin { - fn coin_id(&self) -> [u8; 32] { + pub fn coin_id(&self) -> [u8; 32] { let mut hasher = Sha256::new(); hasher.update(self.parent_coin_info); hasher.update(self.puzzle_hash); @@ -51,7 +56,8 @@ impl Coin { } } -#[pymethods] +#[cfg(feature = "py-bindings")] +#[cfg_attr(feature = "py-bindings", pymethods)] impl Coin { fn name<'p>(&self, py: Python<'p>) -> PyResult<&'p PyBytes> { Ok(PyBytes::new(py, &self.coin_id())) diff --git a/chia-protocol/src/coin_state.rs b/chia-protocol/src/coin_state.rs new file mode 100644 index 000000000..18cfb5c8b --- /dev/null +++ b/chia-protocol/src/coin_state.rs @@ -0,0 +1,24 @@ +use crate::chia_error; +use crate::coin::Coin; +use crate::streamable::Streamable; +use chia_streamable_macro::Streamable; + +#[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; +#[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 { + #[cfg_attr(features = "py-bindings", pyo3(get))] + coin: Coin, + #[cfg_attr(features = "py-bindings", pyo3(get))] + spent_height: Option, + #[cfg_attr(features = "py-bindings", pyo3(get))] + created_height: Option, +} diff --git a/wheel/src/from_json_dict.rs b/chia-protocol/src/from_json_dict.rs similarity index 98% rename from wheel/src/from_json_dict.rs rename to chia-protocol/src/from_json_dict.rs index 716ff24ef..8578540a0 100644 --- a/wheel/src/from_json_dict.rs +++ b/chia-protocol/src/from_json_dict.rs @@ -2,7 +2,7 @@ use pyo3::exceptions::PyValueError; use pyo3::PyAny; use pyo3::PyResult; -use chia_protocol::bytes::{Bytes, BytesImpl}; +use crate::bytes::{Bytes, BytesImpl}; use hex::FromHex; use std::convert::TryInto; diff --git a/chia-protocol/src/lib.rs b/chia-protocol/src/lib.rs index 0ce934f47..e1b092a17 100644 --- a/chia-protocol/src/lib.rs +++ b/chia-protocol/src/lib.rs @@ -1,3 +1,10 @@ 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 respond_to_ph_updates; pub mod streamable; +#[cfg(feature = "py-bindings")] +pub mod to_json_dict; diff --git a/chia-protocol/src/respond_to_ph_updates.rs b/chia-protocol/src/respond_to_ph_updates.rs new file mode 100644 index 000000000..bb6f21cd7 --- /dev/null +++ b/chia-protocol/src/respond_to_ph_updates.rs @@ -0,0 +1,25 @@ +use crate::bytes::Bytes32; +use crate::chia_error; +use crate::coin_state::CoinState; +use crate::streamable::Streamable; +use chia_streamable_macro::Streamable; + +#[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; +#[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 { + #[cfg_attr(features = "py-bindings", pyo3(get))] + puzzle_hashes: Vec, + #[cfg_attr(features = "py-bindings", pyo3(get))] + min_height: u32, + #[cfg_attr(features = "py-bindings", pyo3(get))] + coin_states: Vec, +} diff --git a/wheel/src/to_json_dict.rs b/chia-protocol/src/to_json_dict.rs similarity index 97% rename from wheel/src/to_json_dict.rs rename to chia-protocol/src/to_json_dict.rs index 70a4598b1..cfa829c85 100644 --- a/wheel/src/to_json_dict.rs +++ b/chia-protocol/src/to_json_dict.rs @@ -1,4 +1,4 @@ -use chia_protocol::bytes::{Bytes, BytesImpl}; +use crate::bytes::{Bytes, BytesImpl}; use pyo3::prelude::*; use pyo3::types::PyList; diff --git a/chia_py_streamable_macro/Cargo.toml b/chia_py_streamable_macro/Cargo.toml new file mode 100644 index 000000000..bb4165fc6 --- /dev/null +++ b/chia_py_streamable_macro/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "chia_py_streamable_macro" +version = "0.1.0" +edition = "2021" +license = "Apache-2.0" +description = "Derive macro to create python bindings for Chia types" +authors = ["Arvid Norberg "] +homepage = "https://github.com/Chia-Network/chia_rs/chia_py_streamable_macro/" +repository = "https://github.com/Chia-Network/chia_rs/chia_py_streamable_macro/" + +[lib] +proc-macro = true + +[dependencies] +syn = "1.0.86" +quote = "1.0.15" diff --git a/wheel/py_streamable/src/lib.rs b/chia_py_streamable_macro/src/lib.rs similarity index 96% rename from wheel/py_streamable/src/lib.rs rename to chia_py_streamable_macro/src/lib.rs index 6e60ecbf5..8bd3b72fc 100644 --- a/wheel/py_streamable/src/lib.rs +++ b/chia_py_streamable_macro/src/lib.rs @@ -65,7 +65,7 @@ pub fn py_streamable_macro(input: TokenStream) -> TokenStream { #[staticmethod] pub fn from_bytes(blob: &[u8]) -> PyResult { let mut input = std::io::Cursor::<&[u8]>::new(blob); - Self::parse(&mut input).map_err(|e| >::into(e)) + Self::parse(&mut input).map_err(|e| >::into(e)) } // returns the type as well as the number of bytes read from the buffer @@ -78,7 +78,7 @@ pub fn py_streamable_macro(input: TokenStream) -> TokenStream { std::slice::from_raw_parts(blob.buf_ptr() as *const u8, blob.len_bytes()) }; let mut input = std::io::Cursor::<&[u8]>::new(slice); - Self::parse(&mut input).map_err(|e| >::into(e)).map(|v| (v, input.position() as u32)) + Self::parse(&mut input).map_err(|e| >::into(e)).map(|v| (v, input.position() as u32)) } pub fn get_hash<'p>(&self, py: Python<'p>) -> PyResult<&'p pyo3::types::PyBytes> { @@ -88,7 +88,7 @@ pub fn py_streamable_macro(input: TokenStream) -> TokenStream { } pub fn to_bytes<'p>(&self, py: Python<'p>) -> PyResult<&'p pyo3::types::PyBytes> { let mut writer = Vec::::new(); - self.stream(&mut writer).map_err(|e| >::into(e))?; + self.stream(&mut writer).map_err(|e| >::into(e))?; Ok(pyo3::types::PyBytes::new(py, &writer)) } diff --git a/wheel/Cargo.toml b/wheel/Cargo.toml index 796b01dca..7428bde31 100644 --- a/wheel/Cargo.toml +++ b/wheel/Cargo.toml @@ -19,6 +19,6 @@ chia = { path = "..", features = ["py-bindings"] } chia-protocol = { path = "../chia-protocol", features = ["py-bindings"] } clvmr = "=0.1.24" pyo3 = { version = "=0.15.1", features = ["extension-module", "multiple-pymethods"] } -py_streamable = { path = "py_streamable" } +chia_py_streamable_macro = { path = "../chia_py_streamable_macro" } chia_streamable_macro = { version = "0.2.3", path = "../chia_streamable_macro" } hex = "=0.4.3" diff --git a/wheel/py_streamable/Cargo.toml b/wheel/py_streamable/Cargo.toml deleted file mode 100644 index 1c023b086..000000000 --- a/wheel/py_streamable/Cargo.toml +++ /dev/null @@ -1,11 +0,0 @@ -[package] -name = "py_streamable" -version = "0.1.0" -edition = "2021" - -[lib] -proc-macro = true - -[dependencies] -syn = "1.0.86" -quote = "1.0.15" diff --git a/wheel/src/api.rs b/wheel/src/api.rs index 094f2665a..1a11c2787 100644 --- a/wheel/src/api.rs +++ b/wheel/src/api.rs @@ -1,6 +1,6 @@ -use crate::coin::Coin; -use crate::coin_state::CoinState; -use crate::respond_to_ph_updates::RespondToPhUpdates; +use chia_protocol::coin::Coin; +use chia_protocol::coin_state::CoinState; +use chia_protocol::respond_to_ph_updates::RespondToPhUpdates; use crate::run_generator::{PySpend, PySpendBundleConditions, __pyo3_get_function_run_generator}; use chia::gen::flags::COND_ARGS_NIL; use chia::gen::flags::NO_UNKNOWN_CONDS; diff --git a/wheel/src/coin_state.rs b/wheel/src/coin_state.rs deleted file mode 100644 index 30b216476..000000000 --- a/wheel/src/coin_state.rs +++ /dev/null @@ -1,20 +0,0 @@ -use crate::coin::Coin; -use crate::from_json_dict::FromJsonDict; -use crate::to_json_dict::ToJsonDict; -use chia_protocol::chia_error; -use chia_protocol::streamable::Streamable; -use chia_streamable_macro::Streamable; -use py_streamable::PyStreamable; - -use pyo3::prelude::*; - -#[pyclass] -#[derive(Streamable, PyStreamable, Hash, Debug, Clone, Eq, PartialEq)] -pub struct CoinState { - #[pyo3(get)] - coin: Coin, - #[pyo3(get)] - spent_height: Option, - #[pyo3(get)] - created_height: Option, -} diff --git a/wheel/src/lib.rs b/wheel/src/lib.rs index b2fc34c7c..36e9b054e 100644 --- a/wheel/src/lib.rs +++ b/wheel/src/lib.rs @@ -2,10 +2,5 @@ mod adapt_response; mod api; -mod coin; -mod coin_state; -mod from_json_dict; -mod respond_to_ph_updates; mod run_generator; mod run_program; -mod to_json_dict; diff --git a/wheel/src/respond_to_ph_updates.rs b/wheel/src/respond_to_ph_updates.rs deleted file mode 100644 index f05aefd9f..000000000 --- a/wheel/src/respond_to_ph_updates.rs +++ /dev/null @@ -1,21 +0,0 @@ -use crate::from_json_dict::FromJsonDict; -use crate::to_json_dict::ToJsonDict; -use chia_protocol::chia_error; -use chia_protocol::streamable::Streamable; -use chia_streamable_macro::Streamable; -use py_streamable::PyStreamable; - -use crate::coin_state::CoinState; -use chia_protocol::bytes::Bytes32; -use pyo3::prelude::*; - -#[pyclass(unsendable)] -#[derive(Streamable, PyStreamable, Hash, Debug, Clone, Eq, PartialEq)] -pub struct RespondToPhUpdates { - #[pyo3(get)] - puzzle_hashes: Vec, - #[pyo3(get)] - min_height: u32, - #[pyo3(get)] - coin_states: Vec, -} diff --git a/wheel/src/run_generator.rs b/wheel/src/run_generator.rs index 7296e4884..6defbe9c4 100644 --- a/wheel/src/run_generator.rs +++ b/wheel/src/run_generator.rs @@ -1,6 +1,6 @@ use super::adapt_response::eval_err_to_pyresult; -use super::from_json_dict::FromJsonDict; -use super::to_json_dict::ToJsonDict; +use chia_protocol::from_json_dict::FromJsonDict; +use chia_protocol::to_json_dict::ToJsonDict; use chia_protocol::bytes::{Bytes, Bytes32, Bytes48}; use chia::gen::conditions::{parse_spends, Spend, SpendBundleConditions}; @@ -19,7 +19,7 @@ use pyo3::prelude::*; use chia_protocol::chia_error; use chia_protocol::streamable::Streamable; use chia_streamable_macro::Streamable; -use py_streamable::PyStreamable; +use chia_py_streamable_macro::PyStreamable; #[pyclass(name = "Spend")] #[derive(Streamable, PyStreamable, Hash, Debug, Clone, Eq, PartialEq)]