Skip to content

Commit

Permalink
move Coin, CoinState and RespondToPhUpdates into chia-protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
arvidn committed Nov 15, 2022
1 parent 21ac7e0 commit 5a5ddf4
Show file tree
Hide file tree
Showing 17 changed files with 106 additions and 84 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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]
Expand Down
5 changes: 3 additions & 2 deletions chia-protocol/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
30 changes: 18 additions & 12 deletions wheel/src/coin.rs → chia-protocol/src/coin.rs
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -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()))
Expand Down
24 changes: 24 additions & 0 deletions chia-protocol/src/coin_state.rs
Original file line number Diff line number Diff line change
@@ -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<u32>,
#[cfg_attr(features = "py-bindings", pyo3(get))]
created_height: Option<u32>,
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
7 changes: 7 additions & 0 deletions chia-protocol/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;
25 changes: 25 additions & 0 deletions chia-protocol/src/respond_to_ph_updates.rs
Original file line number Diff line number Diff line change
@@ -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<Bytes32>,
#[cfg_attr(features = "py-bindings", pyo3(get))]
min_height: u32,
#[cfg_attr(features = "py-bindings", pyo3(get))]
coin_states: Vec<CoinState>,
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use chia_protocol::bytes::{Bytes, BytesImpl};
use crate::bytes::{Bytes, BytesImpl};
use pyo3::prelude::*;
use pyo3::types::PyList;

Expand Down
16 changes: 16 additions & 0 deletions chia_py_streamable_macro/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>"]
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"
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub fn py_streamable_macro(input: TokenStream) -> TokenStream {
#[staticmethod]
pub fn from_bytes(blob: &[u8]) -> PyResult<Self> {
let mut input = std::io::Cursor::<&[u8]>::new(blob);
Self::parse(&mut input).map_err(|e| <chia_protocol::chia_error::Error as Into<PyErr>>::into(e))
Self::parse(&mut input).map_err(|e| <chia_error::Error as Into<PyErr>>::into(e))
}

// returns the type as well as the number of bytes read from the buffer
Expand All @@ -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| <chia_protocol::chia_error::Error as Into<PyErr>>::into(e)).map(|v| (v, input.position() as u32))
Self::parse(&mut input).map_err(|e| <chia_error::Error as Into<PyErr>>::into(e)).map(|v| (v, input.position() as u32))
}

pub fn get_hash<'p>(&self, py: Python<'p>) -> PyResult<&'p pyo3::types::PyBytes> {
Expand All @@ -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::<u8>::new();
self.stream(&mut writer).map_err(|e| <chia_protocol::chia_error::Error as Into<PyErr>>::into(e))?;
self.stream(&mut writer).map_err(|e| <chia_error::Error as Into<PyErr>>::into(e))?;
Ok(pyo3::types::PyBytes::new(py, &writer))
}

Expand Down
2 changes: 1 addition & 1 deletion wheel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
11 changes: 0 additions & 11 deletions wheel/py_streamable/Cargo.toml

This file was deleted.

6 changes: 3 additions & 3 deletions wheel/src/api.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
20 changes: 0 additions & 20 deletions wheel/src/coin_state.rs

This file was deleted.

5 changes: 0 additions & 5 deletions wheel/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
21 changes: 0 additions & 21 deletions wheel/src/respond_to_ph_updates.rs

This file was deleted.

6 changes: 3 additions & 3 deletions wheel/src/run_generator.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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)]
Expand Down

0 comments on commit 5a5ddf4

Please sign in to comment.