From c428da8e49225e52e8e389a6b5ac95310aa2c133 Mon Sep 17 00:00:00 2001 From: Kyle Altendorf Date: Wed, 8 Jan 2025 12:02:28 -0500 Subject: [PATCH] Revert "Merge pull request #846 from Chia-Network/catchup/long_lived_initial_datalayer_from_main_ea4928bcf6973a7e68f97a73e058b2634bd5d804" This reverts commit 4362b8c344ca16420c11dbd2b85e033748dffffc, reversing changes made to 87336621cf6e49347c42aacb673246d2f8c51a1e. --- Cargo.lock | 44 +++++++++--------- Cargo.toml | 10 ++-- LICENSE | 2 +- .../fuzz/fuzz_targets/blspy-fidelity.rs | 32 ++++--------- crates/chia-bls/src/bls_cache.rs | 17 +++---- crates/chia-bls/src/gtelement.rs | 5 +- crates/chia-bls/src/public_key.rs | 22 ++++----- crates/chia-bls/src/secret_key.rs | 22 ++++----- crates/chia-bls/src/signature.rs | 22 ++++----- crates/chia-consensus/src/merkle_tree.rs | 2 +- crates/chia-datalayer/src/merkle.rs | 46 ++++++++----------- crates/chia-protocol/src/block_record.rs | 12 ++--- crates/chia-protocol/src/bytes.rs | 46 +++++++++++-------- crates/chia-protocol/src/chia_protocol.rs | 8 +--- crates/chia-protocol/src/coin_spend.rs | 2 +- crates/chia-protocol/src/lazy_node.rs | 14 ++++-- crates/chia-protocol/src/program.rs | 4 +- crates/chia-protocol/src/spend_bundle.rs | 8 ++-- crates/chia-protocol/src/wallet_protocol.rs | 8 +--- crates/chia-traits/src/from_json_dict.rs | 2 +- crates/chia-traits/src/int.rs | 16 +++---- crates/chia-traits/src/to_json_dict.rs | 9 ++-- package-lock.json | 9 ++-- package.json | 2 +- wheel/src/api.rs | 17 ++++--- 25 files changed, 172 insertions(+), 209 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6f5090090..fc7b5c9d3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -101,9 +101,9 @@ dependencies = [ [[package]] name = "anyhow" -version = "1.0.95" +version = "1.0.94" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "34ac096ce696dc2fcabef30516bb13c0a68a11d30131d3df6f04711467681b04" +checksum = "c1fd03a028ef38ba2276dce7e33fcd6369c158a1bca17946c4b1b701891c1ff7" [[package]] name = "arbitrary" @@ -636,9 +636,9 @@ dependencies = [ [[package]] name = "clap" -version = "4.5.23" +version = "4.5.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3135e7ec2ef7b10c6ed8950f0f792ed96ee093fa088608f1c76e569722700c84" +checksum = "69371e34337c4c984bbe322360c2547210bf632eb2814bbe78a6e87a2935bd2b" dependencies = [ "clap_builder", "clap_derive", @@ -646,9 +646,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.5.23" +version = "4.5.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "30582fc632330df2bd26877bde0c1f4470d57c582bbc070376afcd04d8cb4838" +checksum = "6e24c1b4099818523236a8ca881d2b45db98dadfb4625cf6608c12069fcbbde1" dependencies = [ "anstream", "anstyle", @@ -670,9 +670,9 @@ dependencies = [ [[package]] name = "clap_lex" -version = "0.7.4" +version = "0.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f46ad14479a25103f283c0f10005961cf086d8dc42205bb44c46ac563475dca6" +checksum = "1462739cb27611015575c0c11df5df7601141071f07518d56fcc1be504cbec97" [[package]] name = "clvm-derive" @@ -2026,9 +2026,9 @@ dependencies = [ [[package]] name = "pyo3" -version = "0.23.3" +version = "0.22.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e484fd2c8b4cb67ab05a318f1fd6fa8f199fcc30819f08f07d200809dba26c15" +checksum = "f402062616ab18202ae8319da13fa4279883a2b8a9d9f83f20dbade813ce1884" dependencies = [ "cfg-if", "indoc", @@ -2046,9 +2046,9 @@ dependencies = [ [[package]] name = "pyo3-build-config" -version = "0.23.3" +version = "0.22.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dc0e0469a84f208e20044b98965e1561028180219e35352a2afaf2b942beff3b" +checksum = "b14b5775b5ff446dd1056212d778012cbe8a0fbffd368029fd9e25b514479c38" dependencies = [ "once_cell", "target-lexicon", @@ -2056,9 +2056,9 @@ dependencies = [ [[package]] name = "pyo3-ffi" -version = "0.23.3" +version = "0.22.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eb1547a7f9966f6f1a0f0227564a9945fe36b90da5a93b3933fc3dc03fae372d" +checksum = "9ab5bcf04a2cdcbb50c7d6105de943f543f9ed92af55818fd17b660390fc8636" dependencies = [ "libc", "pyo3-build-config", @@ -2066,9 +2066,9 @@ dependencies = [ [[package]] name = "pyo3-macros" -version = "0.23.3" +version = "0.22.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fdb6da8ec6fa5cedd1626c886fc8749bdcbb09424a86461eb8cdf096b7c33257" +checksum = "0fd24d897903a9e6d80b968368a34e1525aeb719d568dba8b3d4bfa5dc67d453" dependencies = [ "proc-macro2", "pyo3-macros-backend", @@ -2078,9 +2078,9 @@ dependencies = [ [[package]] name = "pyo3-macros-backend" -version = "0.23.3" +version = "0.22.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "38a385202ff5a92791168b1136afae5059d3ac118457bb7bc304c197c2d33e7d" +checksum = "36c011a03ba1e50152b4b394b479826cad97e7a21eb52df179cd91ac411cbfbe" dependencies = [ "heck", "proc-macro2", @@ -2150,9 +2150,9 @@ dependencies = [ [[package]] name = "rcgen" -version = "0.13.2" +version = "0.13.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "75e669e5202259b5314d1ea5397316ad400819437857b90861765f24c4cf80a2" +checksum = "54077e1872c46788540de1ea3d7f4ccb1983d12f9aa909b234468676c1a36779" dependencies = [ "pem", "ring", @@ -2486,9 +2486,9 @@ checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" [[package]] name = "syn" -version = "2.0.95" +version = "2.0.90" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "46f71c0377baf4ef1cc3e3402ded576dccc315800fbc62dfc7fe04b009773b4a" +checksum = "919d3b74a5dd0ccd15aeb8f93e7006bd9e14c295087c9896a110f490752bcf31" dependencies = [ "proc-macro2", "quote", diff --git a/Cargo.toml b/Cargo.toml index e62c5d246..73332b09b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -129,16 +129,16 @@ clvm-traits-fuzz = { path = "./crates/clvm-traits/fuzz", version = "0.16.0" } clvm-utils-fuzz = { path = "./crates/clvm-utils/fuzz", version = "0.16.0" } blst = { version = "0.3.12", features = ["portable"] } clvmr = "0.10.0" -syn = "2.0.95" +syn = "2.0.90" quote = "1.0.32" proc-macro2 = "1.0.92" proc-macro-crate = "1.3.1" -anyhow = "1.0.95" +anyhow = "1.0.94" sha2 = "0.10.8" hkdf = "0.12.0" hex = "0.4.3" thiserror = "1.0.69" -pyo3 = "0.23.3" +pyo3 = "0.22.6" arbitrary = "1.4.1" rand = "0.8.5" criterion = "0.5.1" @@ -153,11 +153,11 @@ num-traits = "0.2.15" num-bigint = "0.4.5" text-diff = "0.4.0" lazy_static = "1.4.0" -rcgen = "0.13.2" +rcgen = "0.13.1" rsa = "0.9.7" time = "0.3.22" rusqlite = "0.31.0" -clap = "4.5.23" +clap = "4.5.22" zstd = "0.13.2" blocking-threadpool = "1.0.1" libfuzzer-sys = "0.4" diff --git a/LICENSE b/LICENSE index ef0cf9fb3..7e8bb6bb2 100644 --- a/LICENSE +++ b/LICENSE @@ -186,7 +186,7 @@ same "printed page" as the copyright notice for easier identification within third-party archives. - Copyright 2025 Chia Network Inc. + Copyright 2024 Chia Network Inc. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/crates/chia-bls/fuzz/fuzz_targets/blspy-fidelity.rs b/crates/chia-bls/fuzz/fuzz_targets/blspy-fidelity.rs index 3d4f385c4..37beb1a60 100644 --- a/crates/chia-bls/fuzz/fuzz_targets/blspy-fidelity.rs +++ b/crates/chia-bls/fuzz/fuzz_targets/blspy-fidelity.rs @@ -35,7 +35,7 @@ fuzz_target!(|data: &[u8]| { print(sys.executable) "#, None, None).unwrap(); */ - let blspy = py.import("blspy").unwrap(); + let blspy = py.import_bound("blspy").unwrap(); let aug = blspy.getattr("AugSchemeMPL").unwrap(); // Generate key pair from seed @@ -43,7 +43,7 @@ fuzz_target!(|data: &[u8]| { let py_sk = aug .call_method1( "key_gen", - PyTuple::new(py, [PyBytes::new(py, data)]).unwrap(), + PyTuple::new_bound(py, [PyBytes::new_bound(py, data)]), ) .unwrap(); @@ -66,14 +66,10 @@ fuzz_target!(|data: &[u8]| { let py_sk1 = aug .call_method1( "derive_child_sk_unhardened", - PyTuple::new( + PyTuple::new_bound( py, - [ - py_sk.clone(), - idx.into_pyobject(py).unwrap().clone().into_any(), - ], - ) - .unwrap(), + [py_sk.clone(), idx.to_object(py).bind(py).clone().into_any()], + ), ) .unwrap(); assert_eq!(to_bytes(&py_sk1), rust_sk1.to_bytes()); @@ -82,11 +78,7 @@ fuzz_target!(|data: &[u8]| { let py_pk1 = aug .call_method1( "derive_child_pk_unhardened", - PyTuple::new( - py, - [py_pk, idx.into_pyobject(py).unwrap().clone().into_any()], - ) - .unwrap(), + PyTuple::new_bound(py, [py_pk, idx.to_object(py).bind(py).clone().into_any()]), ) .unwrap(); assert_eq!(to_bytes(&py_pk1), rust_pk1.to_bytes()); @@ -96,7 +88,7 @@ fuzz_target!(|data: &[u8]| { let py_sig1 = aug .call_method1( "sign", - PyTuple::new(py, [py_sk1, PyBytes::new(py, data).into_any()]).unwrap(), + PyTuple::new_bound(py, [py_sk1, PyBytes::new_bound(py, data).into_any()]), ) .unwrap(); assert_eq!(to_bytes(&py_sig1), rust_sig1.to_bytes()); @@ -107,11 +99,7 @@ fuzz_target!(|data: &[u8]| { let py_sk2 = aug .call_method1( "derive_child_sk", - PyTuple::new( - py, - [py_sk, idx.into_pyobject(py).unwrap().clone().into_any()], - ) - .unwrap(), + PyTuple::new_bound(py, [py_sk, idx.to_object(py).bind(py).clone().into_any()]), ) .unwrap(); assert_eq!(to_bytes(&py_sk2), rust_sk2.to_bytes()); @@ -121,7 +109,7 @@ fuzz_target!(|data: &[u8]| { let py_sig2 = aug .call_method1( "sign", - PyTuple::new(py, [py_sk2, PyBytes::new(py, data).into_any()]).unwrap(), + PyTuple::new_bound(py, [py_sk2, PyBytes::new_bound(py, data).into_any()]), ) .unwrap(); assert_eq!(to_bytes(&py_sig2), rust_sig2.to_bytes()); @@ -131,7 +119,7 @@ fuzz_target!(|data: &[u8]| { let py_agg = aug .call_method1( "aggregate", - PyTuple::new(py, [PyList::new(py, [py_sig1, py_sig2]).unwrap()]).unwrap(), + PyTuple::new_bound(py, [PyList::new_bound(py, [py_sig1, py_sig2])]), ) .unwrap(); assert_eq!(to_bytes(&py_agg), rust_agg.to_bytes()); diff --git a/crates/chia-bls/src/bls_cache.rs b/crates/chia-bls/src/bls_cache.rs index 101256b98..188acc183 100644 --- a/crates/chia-bls/src/bls_cache.rs +++ b/crates/chia-bls/src/bls_cache.rs @@ -163,12 +163,12 @@ impl BlsCache { sig: &Signature, ) -> PyResult { let pks = pks - .try_iter()? + .iter()? .map(|item| item?.extract()) .collect::>>()?; let msgs = msgs - .try_iter()? + .iter()? .map(|item| item?.extract()) .collect::>>()?; @@ -184,13 +184,10 @@ impl BlsCache { pub fn py_items(&self, py: pyo3::Python<'_>) -> PyResult { use pyo3::prelude::*; use pyo3::types::PyBytes; - let ret = PyList::empty(py); + let ret = PyList::empty_bound(py); let c = self.cache.lock().expect("cache"); for (key, value) in &c.items { - ret.append(( - PyBytes::new(py, key), - value.clone().into_pyobject(py)?.into_any(), - ))?; + ret.append((PyBytes::new_bound(py, key), value.clone().into_py(py)))?; } Ok(ret.into()) } @@ -198,7 +195,7 @@ impl BlsCache { #[pyo3(name = "update")] pub fn py_update(&self, other: &Bound<'_, PySequence>) -> PyResult<()> { let mut c = self.cache.lock().expect("cache"); - for item in other.borrow().try_iter()? { + for item in other.borrow().iter()? { let (key, value): (Vec, GTElement) = item?.extract()?; c.put( key.try_into() @@ -212,11 +209,11 @@ impl BlsCache { #[pyo3(name = "evict")] pub fn py_evict(&self, pks: &Bound<'_, PyList>, msgs: &Bound<'_, PyList>) -> PyResult<()> { let pks = pks - .try_iter()? + .iter()? .map(|item| item?.extract()) .collect::>>()?; let msgs = msgs - .try_iter()? + .iter()? .map(|item| item?.extract()) .collect::>>()?; self.evict(pks.into_iter().zip(msgs)); diff --git a/crates/chia-bls/src/gtelement.rs b/crates/chia-bls/src/gtelement.rs index 30dad4d63..373890c2f 100644 --- a/crates/chia-bls/src/gtelement.rs +++ b/crates/chia-bls/src/gtelement.rs @@ -148,10 +148,7 @@ mod pybindings { impl ToJsonDict for GTElement { fn to_json_dict(&self, py: Python<'_>) -> PyResult { let bytes = self.to_bytes(); - Ok(("0x".to_string() + &hex::encode(bytes)) - .into_pyobject(py)? - .into_any() - .unbind()) + Ok(("0x".to_string() + &hex::encode(bytes)).into_py(py)) } } diff --git a/crates/chia-bls/src/public_key.rs b/crates/chia-bls/src/public_key.rs index 8d1eed6e9..8c6140083 100644 --- a/crates/chia-bls/src/public_key.rs +++ b/crates/chia-bls/src/public_key.rs @@ -371,10 +371,7 @@ mod pybindings { impl ToJsonDict for PublicKey { fn to_json_dict(&self, py: Python<'_>) -> PyResult { let bytes = self.to_bytes(); - Ok(("0x".to_string() + &hex::encode(bytes)) - .into_pyobject(py)? - .into_any() - .unbind()) + Ok(("0x".to_string() + &hex::encode(bytes)).into_py(py)) } } @@ -737,7 +734,7 @@ mod tests { mod pytests { use super::*; use crate::SecretKey; - use pyo3::Python; + use pyo3::{IntoPy, Python}; use rand::rngs::StdRng; use rand::{Rng, SeedableRng}; use rstest::rstest; @@ -753,7 +750,7 @@ mod pytests { let pk = sk.public_key(); Python::with_gil(|py| { let string = pk.to_json_dict(py).expect("to_json_dict"); - let py_class = py.get_type::(); + let py_class = py.get_type_bound::(); let pk2: PublicKey = PublicKey::from_json_dict(&py_class, py, string.bind(py)) .unwrap() .extract(py) @@ -772,14 +769,11 @@ mod pytests { fn test_json_dict(#[case] input: &str, #[case] msg: &str) { pyo3::prepare_freethreaded_python(); Python::with_gil(|py| { - let py_class = py.get_type::(); - let err = PublicKey::from_json_dict( - &py_class, - py, - &input.to_string().into_pyobject(py).unwrap().into_any(), - ) - .unwrap_err(); - assert_eq!(err.value(py).to_string(), msg.to_string()); + let py_class = py.get_type_bound::(); + let err = + PublicKey::from_json_dict(&py_class, py, input.to_string().into_py(py).bind(py)) + .unwrap_err(); + assert_eq!(err.value_bound(py).to_string(), msg.to_string()); }); } } diff --git a/crates/chia-bls/src/secret_key.rs b/crates/chia-bls/src/secret_key.rs index 935af33a9..9c9998122 100644 --- a/crates/chia-bls/src/secret_key.rs +++ b/crates/chia-bls/src/secret_key.rs @@ -311,10 +311,7 @@ mod pybindings { impl ToJsonDict for SecretKey { fn to_json_dict(&self, py: Python<'_>) -> PyResult { let bytes = self.to_bytes(); - Ok(("0x".to_string() + &hex::encode(bytes)) - .into_pyobject(py)? - .into_any() - .unbind()) + Ok(("0x".to_string() + &hex::encode(bytes)).into_py(py)) } } @@ -559,7 +556,7 @@ mod tests { #[cfg(feature = "py-bindings")] mod pytests { use super::*; - use pyo3::Python; + use pyo3::{IntoPy, Python}; use rand::rngs::StdRng; use rand::{Rng, SeedableRng}; use rstest::rstest; @@ -574,7 +571,7 @@ mod pytests { let sk = SecretKey::from_seed(&data); Python::with_gil(|py| { let string = sk.to_json_dict(py).expect("to_json_dict"); - let py_class = py.get_type::(); + let py_class = py.get_type_bound::(); let sk2 = SecretKey::from_json_dict(&py_class, py, string.bind(py)) .unwrap() .extract(py) @@ -609,14 +606,11 @@ mod pytests { fn test_json_dict(#[case] input: &str, #[case] msg: &str) { pyo3::prepare_freethreaded_python(); Python::with_gil(|py| { - let py_class = py.get_type::(); - let err = SecretKey::from_json_dict( - &py_class, - py, - &input.to_string().into_pyobject(py).unwrap().into_any(), - ) - .unwrap_err(); - assert_eq!(err.value(py).to_string(), msg.to_string()); + let py_class = py.get_type_bound::(); + let err = + SecretKey::from_json_dict(&py_class, py, input.to_string().into_py(py).bind(py)) + .unwrap_err(); + assert_eq!(err.value_bound(py).to_string(), msg.to_string()); }); } } diff --git a/crates/chia-bls/src/signature.rs b/crates/chia-bls/src/signature.rs index 5000c1b3a..01eed3b9b 100644 --- a/crates/chia-bls/src/signature.rs +++ b/crates/chia-bls/src/signature.rs @@ -536,10 +536,7 @@ mod pybindings { impl ToJsonDict for Signature { fn to_json_dict(&self, py: Python<'_>) -> PyResult { let bytes = self.to_bytes(); - Ok(("0x".to_string() + &hex::encode(bytes)) - .into_pyobject(py)? - .into_any() - .unbind()) + Ok(("0x".to_string() + &hex::encode(bytes)).into_py(py)) } } @@ -1257,7 +1254,7 @@ mod tests { mod pytests { use super::*; - use pyo3::Python; + use pyo3::{IntoPy, Python}; use rand::rngs::StdRng; use rand::{Rng, SeedableRng}; use rstest::rstest; @@ -1275,7 +1272,7 @@ mod pytests { let sig = sign(&sk, msg); Python::with_gil(|py| { let string = sig.to_json_dict(py).expect("to_json_dict"); - let py_class = py.get_type::(); + let py_class = py.get_type_bound::(); let sig2 = Signature::from_json_dict(&py_class, py, string.bind(py)) .unwrap() .extract(py) @@ -1294,14 +1291,11 @@ mod pytests { fn test_json_dict(#[case] input: &str, #[case] msg: &str) { pyo3::prepare_freethreaded_python(); Python::with_gil(|py| { - let py_class = py.get_type::(); - let err = Signature::from_json_dict( - &py_class, - py, - &input.to_string().into_pyobject(py).unwrap().into_any(), - ) - .unwrap_err(); - assert_eq!(err.value(py).to_string(), msg.to_string()); + let py_class = py.get_type_bound::(); + let err = + Signature::from_json_dict(&py_class, py, input.to_string().into_py(py).bind(py)) + .unwrap_err(); + assert_eq!(err.value_bound(py).to_string(), msg.to_string()); }); } } diff --git a/crates/chia-consensus/src/merkle_tree.rs b/crates/chia-consensus/src/merkle_tree.rs index e031d5c8a..52a3d53b3 100644 --- a/crates/chia-consensus/src/merkle_tree.rs +++ b/crates/chia-consensus/src/merkle_tree.rs @@ -368,7 +368,7 @@ impl MerkleSet { included_leaf: [u8; 32], ) -> PyResult<(bool, PyObject)> { match self.generate_proof(&included_leaf) { - Ok((included, proof)) => Ok((included, PyBytes::new(py, &proof).into())), + Ok((included, proof)) => Ok((included, PyBytes::new_bound(py, &proof).into())), Err(_) => Err(PyValueError::new_err("invalid proof")), } } diff --git a/crates/chia-datalayer/src/merkle.rs b/crates/chia-datalayer/src/merkle.rs index 4d5e1f90d..03d6a53c1 100644 --- a/crates/chia-datalayer/src/merkle.rs +++ b/crates/chia-datalayer/src/merkle.rs @@ -1,7 +1,7 @@ #[cfg(feature = "py-bindings")] use pyo3::{ - buffer::PyBuffer, exceptions::PyValueError, pyclass, pymethods, types::PyInt, Bound, - FromPyObject, IntoPyObject, PyAny, PyErr, PyResult, Python, + buffer::PyBuffer, exceptions::PyValueError, pyclass, pymethods, FromPyObject, IntoPy, PyObject, + PyResult, Python, }; use chia_protocol::Bytes32; @@ -20,13 +20,9 @@ use thiserror::Error; pub struct TreeIndex(u32); #[cfg(feature = "py-bindings")] -impl<'py> IntoPyObject<'py> for TreeIndex { - type Target = PyInt; - type Output = Bound<'py, Self::Target>; - type Error = std::convert::Infallible; - - fn into_pyobject(self, py: Python<'py>) -> Result { - self.0.into_pyobject(py) +impl IntoPy for TreeIndex { + fn into_py(self, py: Python<'_>) -> PyObject { + self.0.into_py(py) } } @@ -46,13 +42,9 @@ type Hash = Bytes32; pub struct KvId(i64); #[cfg(feature = "py-bindings")] -impl<'py> IntoPyObject<'py> for KvId { - type Target = PyInt; - type Output = Bound<'py, Self::Target>; - type Error = std::convert::Infallible; - - fn into_pyobject(self, py: Python<'py>) -> Result { - self.0.into_pyobject(py) +impl IntoPy for KvId { + fn into_py(self, py: Python<'_>) -> PyObject { + self.0.into_py(py) } } @@ -322,15 +314,11 @@ impl Node { } #[cfg(feature = "py-bindings")] -impl<'py> IntoPyObject<'py> for Node { - type Target = PyAny; - type Output = Bound<'py, Self::Target>; - type Error = PyErr; - - fn into_pyobject(self, py: Python<'py>) -> Result { +impl IntoPy for Node { + fn into_py(self, py: Python<'_>) -> PyObject { match self { - Node::Internal(node) => Ok(node.into_pyobject(py)?.into_any()), - Node::Leaf(node) => Ok(node.into_pyobject(py)?.into_any()), + Node::Internal(node) => node.into_py(py), + Node::Leaf(node) => node.into_py(py), } } } @@ -1291,14 +1279,15 @@ impl MerkleBlob { index: TreeIndex, py: Python<'_>, ) -> PyResult { - let list = pyo3::types::PyList::empty(py); + let list = pyo3::types::PyList::empty_bound(py); for (index, node) in self .get_lineage_with_indexes(index) .map_err(|e| PyValueError::new_err(e.to_string()))? { + use pyo3::conversion::IntoPy; use pyo3::types::PyListMethods; - list.append((index.into_pyobject(py)?, node.into_pyobject(py)?))?; + list.append((index.into_py(py), node.into_py(py)))?; } Ok(list.into()) @@ -1306,12 +1295,13 @@ impl MerkleBlob { #[pyo3(name = "get_nodes_with_indexes")] pub fn py_get_nodes_with_indexes(&self, py: Python<'_>) -> PyResult { - let list = pyo3::types::PyList::empty(py); + let list = pyo3::types::PyList::empty_bound(py); for item in MerkleBlobParentFirstIterator::new(&self.blob) { + use pyo3::conversion::IntoPy; use pyo3::types::PyListMethods; let (index, block) = item.map_err(|e| PyValueError::new_err(e.to_string()))?; - list.append((index.into_pyobject(py)?, block.node.into_pyobject(py)?))?; + list.append((index.into_py(py), block.node.into_py(py)))?; } Ok(list.into()) diff --git a/crates/chia-protocol/src/block_record.rs b/crates/chia-protocol/src/block_record.rs index c90e0d77b..2478fbe21 100644 --- a/crates/chia-protocol/src/block_record.rs +++ b/crates/chia-protocol/src/block_record.rs @@ -132,12 +132,12 @@ impl BlockRecord { } fn sp_iters_impl(&self, py: Python<'_>, constants: &Bound<'_, PyAny>) -> PyResult { - let ctx = PyDict::new(py); + let ctx = PyDict::new_bound(py); ctx.set_item("sub_slot_iters", self.sub_slot_iters)?; ctx.set_item("signage_point_index", self.signage_point_index)?; ctx.set_item("constants", constants)?; - py.run( - c"from chia.consensus.pot_iterations import calculate_ip_iters, calculate_sp_iters\n\ + py.run_bound( + "from chia.consensus.pot_iterations import calculate_ip_iters, calculate_sp_iters\n\ ret = calculate_sp_iters(constants, sub_slot_iters, signage_point_index)\n", None, Some(&ctx), @@ -146,13 +146,13 @@ impl BlockRecord { } fn ip_iters_impl(&self, py: Python<'_>, constants: &Bound<'_, PyAny>) -> PyResult { - let ctx = PyDict::new(py); + let ctx = PyDict::new_bound(py); ctx.set_item("sub_slot_iters", self.sub_slot_iters)?; ctx.set_item("signage_point_index", self.signage_point_index)?; ctx.set_item("required_iters", self.required_iters)?; ctx.set_item("constants", constants)?; - py.run( - c"from chia.consensus.pot_iterations import calculate_ip_iters, calculate_sp_iters\n\ + py.run_bound( + "from chia.consensus.pot_iterations import calculate_ip_iters, calculate_sp_iters\n\ ret = calculate_ip_iters(constants, sub_slot_iters, signage_point_index, required_iters)\n", None, Some(&ctx), diff --git a/crates/chia-protocol/src/bytes.rs b/crates/chia-protocol/src/bytes.rs index 74f567829..74f5c7962 100644 --- a/crates/chia-protocol/src/bytes.rs +++ b/crates/chia-protocol/src/bytes.rs @@ -86,7 +86,7 @@ impl Streamable for Bytes { #[cfg(feature = "py-bindings")] impl ToJsonDict for Bytes { fn to_json_dict(&self, py: Python<'_>) -> PyResult { - Ok(format!("0x{self}").into_pyobject(py)?.into_any().unbind()) + Ok(format!("0x{self}").to_object(py)) } } @@ -226,7 +226,7 @@ impl Streamable for BytesImpl { #[cfg(feature = "py-bindings")] impl ToJsonDict for BytesImpl { fn to_json_dict(&self, py: Python<'_>) -> PyResult { - Ok(format!("0x{self}").into_pyobject(py)?.into_any().unbind()) + Ok(format!("0x{self}").to_object(py)) } } @@ -390,13 +390,16 @@ impl From for Bytes32 { } #[cfg(feature = "py-bindings")] -impl<'py, const N: usize> IntoPyObject<'py> for BytesImpl { - type Target = PyAny; - type Output = Bound<'py, Self::Target>; - type Error = PyErr; +impl ToPyObject for BytesImpl { + fn to_object(&self, py: Python<'_>) -> PyObject { + ChiaToPython::to_python(self, py).unwrap().into() + } +} - fn into_pyobject(self, py: Python<'py>) -> Result { - ChiaToPython::to_python(&self, py) +#[cfg(feature = "py-bindings")] +impl IntoPy for BytesImpl { + fn into_py(self, py: Python<'_>) -> PyObject { + ChiaToPython::to_python(&self, py).unwrap().into() } } @@ -404,15 +407,15 @@ impl<'py, const N: usize> IntoPyObject<'py> for BytesImpl { impl ChiaToPython for BytesImpl { fn to_python<'a>(&self, py: Python<'a>) -> PyResult> { if N == 32 { - let bytes_module = PyModule::import(py, "chia_rs.sized_bytes")?; + let bytes_module = PyModule::import_bound(py, "chia_rs.sized_bytes")?; let ty = bytes_module.getattr("bytes32")?; - ty.call1((self.0.into_pyobject(py)?,)) + ty.call1((self.0.into_py(py),)) } else if N == 48 { - let bytes_module = PyModule::import(py, "chia_rs.sized_bytes")?; + let bytes_module = PyModule::import_bound(py, "chia_rs.sized_bytes")?; let ty = bytes_module.getattr("bytes48")?; - ty.call1((self.0.into_pyobject(py)?,)) + ty.call1((self.0.into_py(py),)) } else { - Ok(PyBytes::new(py, &self.0).into_any()) + Ok(PyBytes::new_bound(py, &self.0).into_any()) } } } @@ -428,20 +431,23 @@ impl<'py, const N: usize> FromPyObject<'py> for BytesImpl { } #[cfg(feature = "py-bindings")] -impl<'py> IntoPyObject<'py> for Bytes { - type Target = PyAny; - type Output = Bound<'py, Self::Target>; - type Error = std::convert::Infallible; +impl ToPyObject for Bytes { + fn to_object(&self, py: Python<'_>) -> PyObject { + PyBytes::new_bound(py, &self.0).into() + } +} - fn into_pyobject(self, py: Python<'py>) -> Result { - Ok(PyBytes::new(py, &self.0).into_any()) +#[cfg(feature = "py-bindings")] +impl IntoPy for Bytes { + fn into_py(self, py: Python<'_>) -> PyObject { + PyBytes::new_bound(py, &self.0).into() } } #[cfg(feature = "py-bindings")] impl ChiaToPython for Bytes { fn to_python<'a>(&self, py: Python<'a>) -> PyResult> { - Ok(PyBytes::new(py, &self.0).into_any()) + Ok(PyBytes::new_bound(py, &self.0).into_any()) } } diff --git a/crates/chia-protocol/src/chia_protocol.rs b/crates/chia-protocol/src/chia_protocol.rs index 655a03e92..a24fe53b8 100644 --- a/crates/chia-protocol/src/chia_protocol.rs +++ b/crates/chia-protocol/src/chia_protocol.rs @@ -146,9 +146,7 @@ pub enum ProtocolMessageTypes { #[cfg(feature = "py-bindings")] impl chia_traits::ChiaToPython for ProtocolMessageTypes { fn to_python<'a>(&self, py: pyo3::Python<'a>) -> pyo3::PyResult> { - Ok(pyo3::IntoPyObject::into_pyobject(*self as u8, py)? - .clone() - .into_any()) + Ok(pyo3::IntoPy::into_py(*self, py).bind(py).clone().into_any()) } } @@ -173,9 +171,7 @@ pub enum NodeType { #[cfg(feature = "py-bindings")] impl chia_traits::ChiaToPython for NodeType { fn to_python<'a>(&self, py: pyo3::Python<'a>) -> pyo3::PyResult> { - Ok(pyo3::IntoPyObject::into_pyobject(*self as u8, py)? - .clone() - .into_any()) + Ok(pyo3::IntoPy::into_py(*self, py).bind(py).clone().into_any()) } } diff --git a/crates/chia-protocol/src/coin_spend.rs b/crates/chia-protocol/src/coin_spend.rs index 0cfdd3910..ffbd3c9c1 100644 --- a/crates/chia-protocol/src/coin_spend.rs +++ b/crates/chia-protocol/src/coin_spend.rs @@ -23,6 +23,6 @@ impl CoinSpend { // Convert result into potential child class let instance = cls.call1((cs.coin, cs.puzzle_reveal, cs.solution))?; - Ok(instance.into_pyobject(py)?.unbind()) + Ok(instance.into_py(py)) } } diff --git a/crates/chia-protocol/src/lazy_node.rs b/crates/chia-protocol/src/lazy_node.rs index 8bde2f037..59b9824e4 100644 --- a/crates/chia-protocol/src/lazy_node.rs +++ b/crates/chia-protocol/src/lazy_node.rs @@ -1,6 +1,6 @@ use clvmr::{allocator::NodePtr, allocator::SExp, Allocator}; use pyo3::prelude::*; -use pyo3::types::PyBytes; +use pyo3::types::{PyBytes, PyTuple}; use std::rc::Rc; #[pyclass(subclass, unsendable, frozen)] @@ -10,6 +10,12 @@ pub struct LazyNode { node: NodePtr, } +impl ToPyObject for LazyNode { + fn to_object(&self, py: Python<'_>) -> PyObject { + Bound::new(py, self.clone()).unwrap().to_object(py) + } +} + #[pymethods] impl LazyNode { #[getter(pair)] @@ -18,7 +24,7 @@ impl LazyNode { SExp::Pair(p1, p2) => { let r1 = Self::new(self.allocator.clone(), *p1); let r2 = Self::new(self.allocator.clone(), *p2); - let v = (r1, r2).into_pyobject(py)?; + let v = PyTuple::new_bound(py, &[r1, r2]); Ok(Some(v.into())) } SExp::Atom => Ok(None), @@ -28,7 +34,9 @@ impl LazyNode { #[getter(atom)] pub fn atom(&self, py: Python<'_>) -> Option { match &self.allocator.sexp(self.node) { - SExp::Atom => Some(PyBytes::new(py, self.allocator.atom(self.node).as_ref()).into()), + SExp::Atom => { + Some(PyBytes::new_bound(py, self.allocator.atom(self.node).as_ref()).into()) + } SExp::Pair(..) => None, } } diff --git a/crates/chia-protocol/src/program.rs b/crates/chia-protocol/src/program.rs index 71e5787eb..45067a97a 100644 --- a/crates/chia-protocol/src/program.rs +++ b/crates/chia-protocol/src/program.rs @@ -297,9 +297,9 @@ fn clvm_serialize(a: &mut Allocator, o: &Bound<'_, PyAny>) -> PyResult #[cfg(feature = "py-bindings")] fn to_program(py: Python<'_>, node: LazyNode) -> PyResult> { - let int_module = PyModule::import(py, "chia.types.blockchain_format.program")?; + let int_module = PyModule::import_bound(py, "chia.types.blockchain_format.program")?; let ty = int_module.getattr("Program")?; - ty.call1((node.into_pyobject(py)?,)) + ty.call1((node.into_py(py),)) } #[cfg(feature = "py-bindings")] diff --git a/crates/chia-protocol/src/spend_bundle.rs b/crates/chia-protocol/src/spend_bundle.rs index ffa29c4ad..b5ab9dc9b 100644 --- a/crates/chia-protocol/src/spend_bundle.rs +++ b/crates/chia-protocol/src/spend_bundle.rs @@ -102,10 +102,10 @@ impl SpendBundle { ) -> PyResult { let aggregated = Bound::new(py, Self::aggregate(&spend_bundles))?; if aggregated.is_exact_instance(cls) { - Ok(aggregated.into_pyobject(py)?.unbind().into_any()) + Ok(aggregated.into_py(py)) } else { - let instance = cls.call_method1("from_parent", (aggregated.into_pyobject(py)?,))?; - Ok(instance.into_pyobject(py)?.unbind().into_any()) + let instance = cls.call_method1("from_parent", (aggregated.into_py(py),))?; + Ok(instance.into_py(py)) } } @@ -122,7 +122,7 @@ impl SpendBundle { None, )?; - Ok(instance.into_pyobject(py)?.unbind()) + Ok(instance.into_py(py)) } #[pyo3(name = "name")] diff --git a/crates/chia-protocol/src/wallet_protocol.rs b/crates/chia-protocol/src/wallet_protocol.rs index 29c5425d4..731edc490 100644 --- a/crates/chia-protocol/src/wallet_protocol.rs +++ b/crates/chia-protocol/src/wallet_protocol.rs @@ -300,9 +300,7 @@ pub enum RejectStateReason { #[cfg(feature = "py-bindings")] impl chia_traits::ChiaToPython for RejectStateReason { fn to_python<'a>(&self, py: pyo3::Python<'a>) -> pyo3::PyResult> { - Ok(pyo3::IntoPyObject::into_pyobject(*self as u8, py)? - .clone() - .into_any()) + Ok(pyo3::IntoPy::into_py(*self, py).bind(py).clone()) } } @@ -320,9 +318,7 @@ pub enum MempoolRemoveReason { #[cfg(feature = "py-bindings")] impl chia_traits::ChiaToPython for MempoolRemoveReason { fn to_python<'a>(&self, py: pyo3::Python<'a>) -> pyo3::PyResult> { - Ok(pyo3::IntoPyObject::into_pyobject(*self as u8, py)? - .clone() - .into_any()) + Ok(pyo3::IntoPy::into_py(*self, py).bind(py).clone()) } } diff --git a/crates/chia-traits/src/from_json_dict.rs b/crates/chia-traits/src/from_json_dict.rs index 0bb601ffe..e15f78289 100644 --- a/crates/chia-traits/src/from_json_dict.rs +++ b/crates/chia-traits/src/from_json_dict.rs @@ -51,7 +51,7 @@ where { fn from_json_dict(o: &Bound<'_, PyAny>) -> PyResult { let mut ret = Vec::::new(); - for v in o.try_iter()? { + for v in o.iter()? { ret.push(::from_json_dict(&v?)?); } Ok(ret) diff --git a/crates/chia-traits/src/int.rs b/crates/chia-traits/src/int.rs index 7c710a2c7..2b49e155c 100644 --- a/crates/chia-traits/src/int.rs +++ b/crates/chia-traits/src/int.rs @@ -10,9 +10,9 @@ macro_rules! primitive_int { ($t:ty, $name:expr) => { impl ChiaToPython for $t { fn to_python<'a>(&self, py: Python<'a>) -> PyResult> { - let int_module = PyModule::import(py, "chia_rs.sized_ints")?; + let int_module = PyModule::import_bound(py, "chia_rs.sized_ints")?; let ty = int_module.getattr($name)?; - ty.call1((self.into_pyobject(py)?.into_any(),)) + ty.call1((self.into_py(py),)) } } }; @@ -40,7 +40,7 @@ impl ChiaToPython for Option { impl ChiaToPython for Vec { fn to_python<'a>(&self, py: Python<'a>) -> PyResult> { - let ret = PyList::empty(py); + let ret = PyList::empty_bound(py); for v in self { ret.append(v.to_python(py)?)?; } @@ -50,32 +50,32 @@ impl ChiaToPython for Vec { impl ChiaToPython for bool { fn to_python<'a>(&self, py: Python<'a>) -> PyResult> { - Ok(PyBool::new(py, *self).as_any().clone()) + Ok(PyBool::new_bound(py, *self).as_any().clone()) } } impl ChiaToPython for String { fn to_python<'a>(&self, py: Python<'a>) -> PyResult> { - Ok(PyString::new(py, self.as_str()).into_any()) + Ok(PyString::new_bound(py, self.as_str()).into_any()) } } impl ChiaToPython for (T, U) { fn to_python<'a>(&self, py: Python<'a>) -> PyResult> { - Ok(PyTuple::new(py, [self.0.to_python(py)?, self.1.to_python(py)?])?.into_any()) + Ok(PyTuple::new_bound(py, [self.0.to_python(py)?, self.1.to_python(py)?]).into_any()) } } impl ChiaToPython for (T, U, V) { fn to_python<'a>(&self, py: Python<'a>) -> PyResult> { - Ok(PyTuple::new( + Ok(PyTuple::new_bound( py, [ self.0.to_python(py)?, self.1.to_python(py)?, self.2.to_python(py)?, ], - )? + ) .into_any()) } } diff --git a/crates/chia-traits/src/to_json_dict.rs b/crates/chia-traits/src/to_json_dict.rs index 789ef80ac..48e60a0b4 100644 --- a/crates/chia-traits/src/to_json_dict.rs +++ b/crates/chia-traits/src/to_json_dict.rs @@ -1,6 +1,5 @@ use pyo3::prelude::*; use pyo3::types::PyList; -use pyo3::BoundObject; pub trait ToJsonDict { fn to_json_dict(&self, py: Python<'_>) -> PyResult; @@ -10,7 +9,7 @@ macro_rules! to_json_primitive { ($t:ty) => { impl $crate::to_json_dict::ToJsonDict for $t { fn to_json_dict(&self, py: Python<'_>) -> pyo3::PyResult { - Ok(self.into_pyobject(py)?.into_any().unbind()) + Ok(self.to_object(py)) } } }; @@ -31,7 +30,7 @@ to_json_primitive!(String); impl ToJsonDict for Vec { fn to_json_dict(&self, py: Python<'_>) -> PyResult { - let list = PyList::empty(py); + let list = PyList::empty_bound(py); for v in self { list.append(v.to_json_dict(py)?)?; } @@ -51,7 +50,7 @@ impl ToJsonDict for Option { // if we need more of these, we should probably make a macro impl ToJsonDict for (T, U) { fn to_json_dict(&self, py: Python<'_>) -> PyResult { - let list = PyList::empty(py); + let list = PyList::empty_bound(py); list.append(self.0.to_json_dict(py)?)?; list.append(self.1.to_json_dict(py)?)?; Ok(list.into()) @@ -60,7 +59,7 @@ impl ToJsonDict for (T, U) { impl ToJsonDict for (T, U, W) { fn to_json_dict(&self, py: Python<'_>) -> PyResult { - let list = PyList::empty(py); + let list = PyList::empty_bound(py); list.append(self.0.to_json_dict(py)?)?; list.append(self.1.to_json_dict(py)?)?; list.append(self.2.to_json_dict(py)?)?; diff --git a/package-lock.json b/package-lock.json index c171ba057..5c2560682 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5,14 +5,15 @@ "packages": { "": { "devDependencies": { - "prettier": "^3.4.2" + "prettier": "^3.4.1" } }, "node_modules/prettier": { - "version": "3.4.2", - "resolved": "https://registry.npmjs.org/prettier/-/prettier-3.4.2.tgz", - "integrity": "sha512-e9MewbtFo+Fevyuxn/4rrcDAaq0IYxPGLvObpQjiZBMAzB9IGmzlnG9RZy3FFas+eBMu2vA0CszMeduow5dIuQ==", + "version": "3.4.1", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-3.4.1.tgz", + "integrity": "sha512-G+YdqtITVZmOJje6QkXQWzl3fSfMxFwm1tjTyo9exhkmWSqC4Yhd1+lug++IlR2mvRVAxEDDWYkQdeSztajqgg==", "dev": true, + "license": "MIT", "bin": { "prettier": "bin/prettier.cjs" }, diff --git a/package.json b/package.json index 474b8042e..a13659367 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,6 @@ "fmt": "prettier --write ." }, "devDependencies": { - "prettier": "^3.4.2" + "prettier": "^3.4.1" } } diff --git a/wheel/src/api.rs b/wheel/src/api.rs index 6cc98f1f5..eaeca3210 100644 --- a/wheel/src/api.rs +++ b/wheel/src/api.rs @@ -90,7 +90,10 @@ pub fn compute_merkle_set_root<'p>( use pyo3::types::PyBytesMethods; buffer.push(b.as_bytes().try_into()?); } - Ok(PyBytes::new(py, &compute_merkle_root_impl(&mut buffer))) + Ok(PyBytes::new_bound( + py, + &compute_merkle_root_impl(&mut buffer), + )) } #[pyfunction] @@ -177,8 +180,8 @@ pub fn get_puzzle_and_solution_for_coin<'a>( }; */ Ok(( - PyBytes::new(py, &serialize(&allocator, puzzle)?), - PyBytes::new(py, &serialize(&allocator, solution)?), + PyBytes::new_bound(py, &serialize(&allocator, puzzle)?), + PyBytes::new_bound(py, &serialize(&allocator, solution)?), )) } @@ -237,7 +240,7 @@ type CoinSpendRef = (Coin, PyBackedBytes, PyBackedBytes); fn convert_list_of_tuples(spends: &Bound<'_, PyAny>) -> PyResult> { let mut native_spends = Vec::::new(); - for s in spends.try_iter()? { + for s in spends.iter()? { let s = s?; let tuple = s.downcast::()?; let coin = tuple.get_item(0)?.extract::()?; @@ -254,7 +257,7 @@ fn solution_generator<'p>( spends: &Bound<'_, PyAny>, ) -> PyResult> { let spends = convert_list_of_tuples(spends)?; - Ok(PyBytes::new(py, &native_solution_generator(spends)?)) + Ok(PyBytes::new_bound(py, &native_solution_generator(spends)?)) } #[pyfunction] @@ -263,7 +266,7 @@ fn solution_generator_backrefs<'p>( spends: &Bound<'_, PyAny>, ) -> PyResult> { let spends = convert_list_of_tuples(spends)?; - Ok(PyBytes::new( + Ok(PyBytes::new_bound( py, &native_solution_generator_backrefs(spends)?, )) @@ -399,7 +402,7 @@ fn fast_forward_singleton<'p>( let solution = node_from_bytes(&mut a, spend.solution.as_slice())?; let new_solution = native_ff(&mut a, puzzle, solution, &spend.coin, new_coin, new_parent)?; - Ok(PyBytes::new( + Ok(PyBytes::new_bound( py, node_to_bytes(&a, new_solution)?.as_slice(), ))