diff --git a/Cargo.lock b/Cargo.lock index 85e9567e..9d98a536 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -125,6 +125,23 @@ dependencies = [ "tiny-bip39", ] +[[package]] +name = "chia-bls" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1cbbce0107fa3911890f1a204a147c6d07116687658a6af95690e64fc21ff782" +dependencies = [ + "anyhow", + "arbitrary", + "blst", + "chia-traits", + "hex", + "hkdf", + "sha2", + "thiserror", + "tiny-bip39", +] + [[package]] name = "chia-traits" version = "0.3.3" @@ -217,7 +234,7 @@ dependencies = [ name = "clvm-rs-test-tools" version = "0.1.0" dependencies = [ - "chia-bls", + "chia-bls 0.3.3", "clap", "clvmr", "hex", @@ -230,16 +247,6 @@ dependencies = [ "sha1", ] -[[package]] -name = "clvm-traits" -version = "0.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "02b527f38a1a9ef23aafd982a89be3881fa5f0a0ba0a642fa94244c72a74b8c8" -dependencies = [ - "num-bigint", - "thiserror", -] - [[package]] name = "clvm_rs" version = "0.3.2" @@ -271,8 +278,7 @@ dependencies = [ name = "clvmr" version = "0.4.0" dependencies = [ - "chia-bls", - "clvm-traits", + "chia-bls 0.4.0", "criterion", "hex", "k256", diff --git a/Cargo.toml b/Cargo.toml index f57326f7..ad346377 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,8 +33,7 @@ lazy_static = "1.4.0" num-bigint = "0.4.3" num-traits = "0.2.15" num-integer = "0.1.45" -clvm-traits = "0.3.2" -chia-bls = "0.3.3" +chia-bls = "0.4.0" sha2 = "0.10.8" openssl = { version = "=0.10.55", features = ["vendored"], optional = true } # for secp sigs diff --git a/src/allocator.rs b/src/allocator.rs index 98b32ef7..1ed42a7a 100644 --- a/src/allocator.rs +++ b/src/allocator.rs @@ -2,7 +2,6 @@ use crate::err_utils::err; use crate::number::{node_from_number, number_from_u8, Number}; use crate::reduction::EvalErr; use chia_bls::{G1Element, G2Element}; -use clvm_traits::{ClvmDecoder, ClvmEncoder, FromClvmError, ToClvmError}; const MAX_NUM_ATOMS: usize = 62500000; const MAX_NUM_PAIRS: usize = 62500000; @@ -375,42 +374,6 @@ impl Allocator { } } -impl ClvmEncoder for Allocator { - type Node = NodePtr; - - fn encode_atom(&mut self, bytes: &[u8]) -> Result { - self.new_atom(bytes).or(Err(ToClvmError::OutOfMemory)) - } - - fn encode_pair( - &mut self, - first: Self::Node, - rest: Self::Node, - ) -> Result { - self.new_pair(first, rest).or(Err(ToClvmError::OutOfMemory)) - } -} - -impl ClvmDecoder for Allocator { - type Node = NodePtr; - - fn decode_atom(&self, node: &Self::Node) -> Result<&[u8], FromClvmError> { - if let SExp::Atom = self.sexp(*node) { - Ok(self.atom(*node)) - } else { - Err(FromClvmError::ExpectedAtom) - } - } - - fn decode_pair(&self, node: &Self::Node) -> Result<(Self::Node, Self::Node), FromClvmError> { - if let SExp::Pair(first, rest) = self.sexp(*node) { - Ok((first, rest)) - } else { - Err(FromClvmError::ExpectedPair) - } - } -} - #[test] fn test_node_as_index() { assert_eq!(NodePtr::new(ObjectType::Pair, 0).as_index(), 0); diff --git a/src/from_node_ptr.rs b/src/from_node_ptr.rs deleted file mode 100644 index 58daf71b..00000000 --- a/src/from_node_ptr.rs +++ /dev/null @@ -1,115 +0,0 @@ -use crate::{allocator::NodePtr, Allocator}; - -use clvm_traits::{ClvmDecoder, FromClvm, FromClvmError}; - -pub trait FromNodePtr { - fn from_node_ptr(a: &Allocator, node: NodePtr) -> Result - where - Self: Sized; -} - -impl FromNodePtr for T -where - T: FromClvm, -{ - fn from_node_ptr(a: &Allocator, node: NodePtr) -> Result - where - Self: Sized, - { - T::from_clvm(a, node) - } -} - -impl FromClvm for NodePtr { - fn from_clvm( - _decoder: &impl ClvmDecoder, - node: NodePtr, - ) -> Result { - Ok(node) - } -} - -#[cfg(test)] -mod tests { - use crate::{allocator::NodePtr, serde::node_from_bytes, Allocator}; - - use super::*; - - fn decode(a: &mut Allocator, hex: &str) -> Result - where - T: FromClvm, - { - let bytes = hex::decode(hex).unwrap(); - let actual = node_from_bytes(a, &bytes).unwrap(); - T::from_clvm(a, actual) - } - - #[test] - fn test_nodeptr() { - let a = &mut Allocator::new(); - let ptr = a.one(); - assert_eq!(NodePtr::from_clvm(a, ptr).unwrap(), ptr); - } - - #[test] - fn test_primitives() { - let a = &mut Allocator::new(); - assert_eq!(decode(a, "80"), Ok(0u8)); - assert_eq!(decode(a, "80"), Ok(0i8)); - assert_eq!(decode(a, "05"), Ok(5u8)); - assert_eq!(decode(a, "05"), Ok(5u32)); - assert_eq!(decode(a, "05"), Ok(5i32)); - assert_eq!(decode(a, "81e5"), Ok(-27i32)); - assert_eq!(decode(a, "80"), Ok(-0)); - assert_eq!(decode(a, "8180"), Ok(-128i8)); - } - - #[test] - fn test_pair() { - let a = &mut Allocator::new(); - assert_eq!(decode(a, "ff0502"), Ok((5, 2))); - assert_eq!(decode(a, "ff81b8ff8301600980"), Ok((-72, (90121, ())))); - assert_eq!( - decode(a, "ffff80ff80ff80ffff80ff80ff80808080"), - Ok((((), ((), ((), (((), ((), ((), ()))), ())))), ())) - ); - } - - #[test] - fn test_nil() { - let a = &mut Allocator::new(); - assert_eq!(decode(a, "80"), Ok(())); - } - - #[test] - fn test_array() { - let a = &mut Allocator::new(); - assert_eq!(decode(a, "ff01ff02ff03ff0480"), Ok([1, 2, 3, 4])); - assert_eq!(decode(a, "80"), Ok([] as [i32; 0])); - } - - #[test] - fn test_vec() { - let a = &mut Allocator::new(); - assert_eq!(decode(a, "ff01ff02ff03ff0480"), Ok(vec![1, 2, 3, 4])); - assert_eq!(decode(a, "80"), Ok(Vec::::new())); - } - - #[test] - fn test_option() { - let a = &mut Allocator::new(); - assert_eq!(decode(a, "8568656c6c6f"), Ok(Some("hello".to_string()))); - assert_eq!(decode(a, "80"), Ok(None::)); - - // Empty strings get decoded as None instead, since both values are represented by nil bytes. - // This could be considered either intended behavior or not, depending on the way it's used. - assert_ne!(decode(a, "80"), Ok(Some("".to_string()))); - } - - #[test] - fn test_string() { - let a = &mut Allocator::new(); - assert_eq!(decode(a, "8568656c6c6f"), Ok("hello".to_string())); - assert_eq!(decode(a, "80"), Ok("".to_string())); - } -} diff --git a/src/lib.rs b/src/lib.rs index 5008b64b..8d449ced 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -17,12 +17,6 @@ pub mod serde; pub mod sha2; pub mod traverse_path; -pub mod from_node_ptr; -pub mod to_node_ptr; - -pub use from_node_ptr::FromNodePtr; -pub use to_node_ptr::ToNodePtr; - pub use allocator::{Allocator, NodePtr}; pub use chia_dialect::ChiaDialect; pub use run_program::run_program; diff --git a/src/to_node_ptr.rs b/src/to_node_ptr.rs deleted file mode 100644 index 17d49c1c..00000000 --- a/src/to_node_ptr.rs +++ /dev/null @@ -1,143 +0,0 @@ -use crate::{allocator::NodePtr, Allocator}; - -use clvm_traits::{ClvmEncoder, ToClvm, ToClvmError}; - -pub trait ToNodePtr { - fn to_node_ptr(&self, a: &mut Allocator) -> Result; -} - -impl ToNodePtr for T -where - T: ToClvm, -{ - fn to_node_ptr(&self, a: &mut Allocator) -> Result { - self.to_clvm(a) - } -} - -impl ToClvm for NodePtr { - fn to_clvm( - &self, - _encoder: &mut impl ClvmEncoder, - ) -> Result { - Ok(*self) - } -} - -#[cfg(test)] -mod tests { - use crate::{serde::node_to_bytes, Allocator}; - use hex::ToHex; - - use super::*; - - fn encode(a: &mut Allocator, value: T) -> Result - where - T: ToClvm, - { - let actual = value.to_clvm(a).unwrap(); - let actual_bytes = node_to_bytes(a, actual).unwrap(); - Ok(actual_bytes.encode_hex()) - } - - #[test] - fn test_nodeptr() { - let a = &mut Allocator::new(); - let ptr = a.one(); - assert_eq!(ptr.to_clvm(a).unwrap(), ptr); - } - - #[test] - fn test_primitives() { - let a = &mut Allocator::new(); - assert_eq!(encode(a, 0u8), Ok("80".to_owned())); - assert_eq!(encode(a, 0i8), Ok("80".to_owned())); - assert_eq!(encode(a, 5u8), Ok("05".to_owned())); - assert_eq!(encode(a, 5u32), Ok("05".to_owned())); - assert_eq!(encode(a, 5i32), Ok("05".to_owned())); - assert_eq!(encode(a, -27i32), Ok("81e5".to_owned())); - assert_eq!(encode(a, -0), Ok("80".to_owned())); - assert_eq!(encode(a, -128i8), Ok("8180".to_owned())); - } - - #[test] - fn test_reference() { - let a = &mut Allocator::new(); - assert_eq!(encode(a, [1, 2, 3]), encode(a, [1, 2, 3])); - assert_eq!(encode(a, Some(42)), encode(a, Some(42))); - assert_eq!(encode(a, Some(&42)), encode(a, Some(42))); - assert_eq!(encode(a, Some(&42)), encode(a, Some(42))); - } - - #[test] - fn test_pair() { - let a = &mut Allocator::new(); - assert_eq!(encode(a, (5, 2)), Ok("ff0502".to_owned())); - assert_eq!( - encode(a, (-72, (90121, ()))), - Ok("ff81b8ff8301600980".to_owned()) - ); - assert_eq!( - encode(a, (((), ((), ((), (((), ((), ((), ()))), ())))), ())), - Ok("ffff80ff80ff80ffff80ff80ff80808080".to_owned()) - ); - } - - #[test] - fn test_nil() { - let a = &mut Allocator::new(); - assert_eq!(encode(a, ()), Ok("80".to_owned())); - } - - #[test] - fn test_slice() { - let a = &mut Allocator::new(); - assert_eq!( - encode(a, [1, 2, 3, 4].as_slice()), - Ok("ff01ff02ff03ff0480".to_owned()) - ); - assert_eq!(encode(a, [0; 0].as_slice()), Ok("80".to_owned())); - } - - #[test] - fn test_array() { - let a = &mut Allocator::new(); - assert_eq!(encode(a, [1, 2, 3, 4]), Ok("ff01ff02ff03ff0480".to_owned())); - assert_eq!(encode(a, [0; 0]), Ok("80".to_owned())); - } - - #[test] - fn test_vec() { - let a = &mut Allocator::new(); - assert_eq!( - encode(a, vec![1, 2, 3, 4]), - Ok("ff01ff02ff03ff0480".to_owned()) - ); - assert_eq!(encode(a, vec![0; 0]), Ok("80".to_owned())); - } - - #[test] - fn test_option() { - let a = &mut Allocator::new(); - assert_eq!(encode(a, Some("hello")), Ok("8568656c6c6f".to_owned())); - assert_eq!(encode(a, None::<&str>), Ok("80".to_owned())); - assert_eq!(encode(a, Some("")), Ok("80".to_owned())); - } - - #[test] - fn test_str() { - let a = &mut Allocator::new(); - assert_eq!(encode(a, "hello"), Ok("8568656c6c6f".to_owned())); - assert_eq!(encode(a, ""), Ok("80".to_owned())); - } - - #[test] - fn test_string() { - let a = &mut Allocator::new(); - assert_eq!( - encode(a, "hello".to_string()), - Ok("8568656c6c6f".to_owned()) - ); - assert_eq!(encode(a, "".to_string()), Ok("80".to_owned())); - } -}