From ad4c8ae74a3d425153109be7371bb135b50a6e96 Mon Sep 17 00:00:00 2001 From: arty Date: Thu, 25 Apr 2024 13:38:17 -0700 Subject: [PATCH 1/5] Upgrade clvm_rs and make capture of atom bufs more brief --- Cargo.lock | 27 ++++---- Cargo.toml | 2 +- src/classic/clvm/__type_compatibility__.rs | 4 +- src/classic/clvm/casts.rs | 23 ++++++- src/classic/clvm/serialize.rs | 20 +++--- src/classic/clvm/sexp.rs | 27 ++++---- src/classic/clvm_tools/binutils.rs | 7 ++- src/classic/clvm_tools/clvmc.rs | 4 +- src/classic/clvm_tools/cmds.rs | 4 +- src/classic/clvm_tools/debug.rs | 6 +- src/classic/clvm_tools/pattern_match.rs | 24 ++++--- src/classic/clvm_tools/sha256tree.rs | 20 +++--- .../clvm_tools/stages/stage_2/compile.rs | 49 ++++++++------- .../clvm_tools/stages/stage_2/defaults.rs | 2 +- .../clvm_tools/stages/stage_2/inline.rs | 13 ++-- .../clvm_tools/stages/stage_2/module.rs | 37 ++++++----- .../clvm_tools/stages/stage_2/operators.rs | 62 +++++++++---------- .../clvm_tools/stages/stage_2/optimize.rs | 43 +++++++------ .../clvm_tools/stages/stage_2/reader.rs | 27 ++++---- src/compiler/cldb.rs | 2 +- src/compiler/clvm.rs | 13 ++-- src/compiler/debug.rs | 6 +- src/compiler/dialect.rs | 5 +- src/tests/classic/smoke.rs | 34 +++++----- 24 files changed, 253 insertions(+), 208 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 51bba9667..65d2b89a3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -150,9 +150,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "chia-bls" -version = "0.3.3" +version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ba003303bda3019f2fe94e5478555a96addff1d6a1ea84bfe207b3f075cd8420" +checksum = "1cbbce0107fa3911890f1a204a147c6d07116687658a6af95690e64fc21ff782" dependencies = [ "anyhow", "arbitrary", @@ -189,16 +189,6 @@ dependencies = [ "syn 2.0.28", ] -[[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_tools_rs" version = "0.1.41" @@ -237,13 +227,12 @@ dependencies = [ [[package]] name = "clvmr" -version = "0.3.2" +version = "0.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dede49a536ac5ad83753cf241f674d0c7b251a2a58ae681577457be9c5ea7bc3" +checksum = "deb235bb5ce94e1e64e54cede97ca5a4766590d3584dea1b4285499d8e7f56f6" dependencies = [ "chia-bls", - "clvm-traits", - "hex", + "hex-literal", "k256", "lazy_static", "num-bigint", @@ -510,6 +499,12 @@ version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" +[[package]] +name = "hex-literal" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6fe2267d4ed49bc07b63801559be28c718ea06c4738b7a03c94df7386d2cde46" + [[package]] name = "hkdf" version = "0.12.4" diff --git a/Cargo.toml b/Cargo.toml index cfd47ea75..efb87b9b0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,7 +32,7 @@ do-notation = "0.1.3" serde_json = "1.0" sha2 = "0.9.5" tempfile = "3.3.0" -clvmr = { version = "0.3.2", features = ["pre-eval"] } +clvmr = { version = "0.6.1", features = ["pre-eval"] } binascii = "0.1.4" yaml-rust = "0.4" linked-hash-map = "0.5.6" diff --git a/src/classic/clvm/__type_compatibility__.rs b/src/classic/clvm/__type_compatibility__.rs index d5432af0a..51e055dd0 100644 --- a/src/classic/clvm/__type_compatibility__.rs +++ b/src/classic/clvm/__type_compatibility__.rs @@ -214,8 +214,8 @@ impl Display for Bytes { } } -pub fn sha256(value: Bytes) -> Bytes { - let hashed = Sha256::digest(&value.data()[..]); +pub fn sha256(value: &[u8]) -> Bytes { + let hashed = Sha256::digest(value); let hashed_iter = hashed.into_iter(); let newvec: Vec = hashed_iter.collect(); Bytes::new(Some(BytesFromType::Raw(newvec))) diff --git a/src/classic/clvm/casts.rs b/src/classic/clvm/casts.rs index de10c7190..ac893f26b 100644 --- a/src/classic/clvm/casts.rs +++ b/src/classic/clvm/casts.rs @@ -1,7 +1,9 @@ use num_bigint::ToBigInt; +use std::borrow::Borrow; use clvm_rs::allocator::Allocator; use clvm_rs::reduction::EvalErr; +use clvm_rs::{Atom, NodePtr}; use crate::classic::clvm::__type_compatibility__::{ bi_one, bi_zero, get_u32, Bytes, BytesFromType, @@ -21,7 +23,7 @@ pub fn int_from_bytes( return Ok(0); } else if b.length() * 8 > 64 { return Err(EvalErr( - allocator.null(), + allocator.nil(), "Cannot convert Bytes to Integer larger than 64bit. Use bigint_from_bytes instead." .to_string(), )); @@ -148,3 +150,22 @@ pub fn bigint_to_bytes_clvm(v: &Number) -> Bytes { // export function limbs_for_int(v: number|bigint): number { // return ((v >= 0 ? v : -v).toString(2).length + 7) >> 3; // } + +pub struct By<'a> { atom: Atom<'a> } +impl<'a> By<'a> { + pub fn new(allocator: &'a Allocator, node: NodePtr) -> Self { + By { atom: allocator.atom(node) } + } + pub fn u8(&self) -> &[u8] { + self.atom.borrow() + } + pub fn to_vec(&self) -> Vec { + let borrowed: &[u8] = self.atom.borrow(); + borrowed.to_vec() + } +} +impl<'a> Borrow<[u8]> for By<'a> { + fn borrow(&self) -> &[u8] { + self.u8() + } +} diff --git a/src/classic/clvm/serialize.rs b/src/classic/clvm/serialize.rs index 6deb9dd7c..f17221a27 100644 --- a/src/classic/clvm/serialize.rs +++ b/src/classic/clvm/serialize.rs @@ -17,7 +17,7 @@ use std::rc::Rc; use crate::classic::clvm::__type_compatibility__::{Bytes, BytesFromType, Stream}; use crate::classic::clvm::as_rust::{TToSexpF, TValStack}; -use crate::classic::clvm::casts::int_from_bytes; +use crate::classic::clvm::casts::{By, int_from_bytes}; use crate::classic::clvm::sexp::{to_sexp_type, CastableType}; use clvm_rs::allocator::{Allocator, NodePtr, SExp}; use clvm_rs::reduction::{EvalErr, Reduction, Response}; @@ -100,12 +100,12 @@ impl<'a> Iterator for SExpToBytesIterator<'a> { SExp::Atom => { // The only node we have in scope is x, so this atom // capture is trivial. - let buf = self.allocator.atom(x).to_vec(); - let bytes = Bytes::new(Some(BytesFromType::Raw(buf.clone()))); + let buf = By::new(&mut self.allocator, x); + let bytes = Bytes::new(Some(BytesFromType::Raw(buf.to_vec()))); match atom_size_blob(&bytes) { Ok((original, b)) => { if original { - self.state.push(SExpToByteOp::Blob(buf)); + self.state.push(SExpToByteOp::Blob(buf.to_vec())); } Some(b) } @@ -184,7 +184,7 @@ impl OpStackEntry for OpReadSexp { ) -> Option { let blob = f.read(1); if blob.length() == 0 { - return Some(EvalErr(allocator.null(), "bad encoding".to_string())); + return Some(EvalErr(allocator.nil(), "bad encoding".to_string())); } let b = blob.at(0); @@ -236,7 +236,7 @@ pub fn sexp_from_stream<'a>( } Err(EvalErr( - allocator.null(), + allocator.nil(), "No value left after conversion".to_string(), )) } @@ -250,7 +250,7 @@ pub fn atom_from_stream<'a>( let mut b = b_; if b == 0x80 { - return Ok(allocator.null()); + return Ok(allocator.nil()); } else if b <= MAX_SINGLE_BYTE as u8 { return allocator.new_atom(&[b]); } @@ -268,17 +268,17 @@ pub fn atom_from_stream<'a>( if bit_count > 1 { let bin = f.read(bit_count - 1); if bin.length() != bit_count - 1 { - return Err(EvalErr(allocator.null(), "bad encoding".to_string())); + return Err(EvalErr(allocator.nil(), "bad encoding".to_string())); } size_blob = size_blob.concat(&bin); } int_from_bytes(allocator, size_blob, None).and_then(|size| { if size >= 0x400000000 { - return Err(EvalErr(allocator.null(), "blob too large".to_string())); + return Err(EvalErr(allocator.nil(), "blob too large".to_string())); } let blob = f.read(size as usize); if blob.length() != size as usize { - return Err(EvalErr(allocator.null(), "bad encoding".to_string())); + return Err(EvalErr(allocator.nil(), "bad encoding".to_string())); } return allocator.new_atom(blob.data()); }) diff --git a/src/classic/clvm/sexp.rs b/src/classic/clvm/sexp.rs index bf8b1fd30..fabed8590 100644 --- a/src/classic/clvm/sexp.rs +++ b/src/classic/clvm/sexp.rs @@ -8,6 +8,7 @@ use clvm_rs::reduction::EvalErr; use bls12_381::G1Affine; use crate::classic::clvm::__type_compatibility__::{Bytes, BytesFromType, Stream}; +use crate::classic::clvm::casts::By; use crate::classic::clvm::serialize::sexp_to_stream; use crate::util::{u8_from_number, Number}; @@ -43,7 +44,7 @@ pub fn to_sexp_type(allocator: &mut Allocator, value: CastableType) -> Result { - return Err(EvalErr(allocator.null(), "empty value stack".to_string())); + return Err(EvalErr(allocator.nil(), "empty value stack".to_string())); } Some(rc) => rc, }; @@ -57,7 +58,7 @@ pub fn to_sexp_type(allocator: &mut Allocator, value: CastableType) -> Result { let target_index = stack.len(); - match allocator.new_pair(allocator.null(), allocator.null()) { + match allocator.new_pair(allocator.nil(), allocator.nil()) { Ok(pair) => { stack.push(Rc::new(CastableType::CLVMObject(pair))); } @@ -75,7 +76,7 @@ pub fn to_sexp_type(allocator: &mut Allocator, value: CastableType) -> Result { let target_index = stack.len(); - stack.push(Rc::new(CastableType::CLVMObject(allocator.null()))); + stack.push(Rc::new(CastableType::CLVMObject(allocator.nil()))); for vi in v.iter().take(v.len() - 1) { stack.push(vi.clone()); ops.push(SexpStackOp::OpPrepend(target_index)); @@ -158,14 +159,14 @@ pub fn to_sexp_type(allocator: &mut Allocator, value: CastableType) -> Result { return Err(EvalErr( - allocator.null(), + allocator.nil(), format!("Setting wing of non pair {:?}", stack[target]), )); } }, _ => { return Err(EvalErr( - allocator.null(), + allocator.nil(), format!("op_set_pair on atom item {target:?} in vec {stack:?} ops {ops:?}"), )); } @@ -183,14 +184,14 @@ pub fn to_sexp_type(allocator: &mut Allocator, value: CastableType) -> Result { return Err(EvalErr( - allocator.null(), + allocator.nil(), format!("unrealized pair prepended {:?}", stack[target]), )); } }, _ => { return Err(EvalErr( - allocator.null(), + allocator.nil(), format!("unrealized prepend {top:?}"), )); } @@ -200,17 +201,17 @@ pub fn to_sexp_type(allocator: &mut Allocator, value: CastableType) -> Result Err(EvalErr(allocator.null(), "stack empty".to_string())), + None => Err(EvalErr(allocator.nil(), "stack empty".to_string())), Some(top) => match top.borrow() { CastableType::CLVMObject(o) => Ok(*o), _ => Err(EvalErr( - allocator.null(), + allocator.nil(), format!("unimplemented {:?}", stack[0]), )), }, @@ -227,7 +228,7 @@ pub fn bool_sexp(allocator: &Allocator, b: bool) -> NodePtr { if b { allocator.one() } else { - allocator.null() + allocator.nil() } } @@ -355,7 +356,7 @@ pub fn rest(allocator: &Allocator, sexp: NodePtr) -> Result { pub fn atom(allocator: &Allocator, sexp: NodePtr) -> Result, EvalErr> { match allocator.sexp(sexp) { - SExp::Atom => Ok(allocator.atom(sexp).to_vec()), // only sexp in scope + SExp::Atom => Ok(By::new(allocator, sexp).to_vec()), // only sexp in scope _ => Err(EvalErr(sexp, "not an atom".to_string())), } } @@ -383,7 +384,7 @@ pub fn proper_list(allocator: &Allocator, sexp: NodePtr, store: bool) -> Option< } pub fn enlist(allocator: &mut Allocator, vec: &[NodePtr]) -> Result { - let mut built = allocator.null(); + let mut built = allocator.nil(); for i_reverse in 0..vec.len() { let i = vec.len() - i_reverse - 1; diff --git a/src/classic/clvm_tools/binutils.rs b/src/classic/clvm_tools/binutils.rs index dbb2afc46..c28ba985e 100644 --- a/src/classic/clvm_tools/binutils.rs +++ b/src/classic/clvm_tools/binutils.rs @@ -8,6 +8,7 @@ use clvm_rs::reduction::EvalErr; use crate::classic::clvm::__type_compatibility__::{Bytes, BytesFromType, Record, Stream}; use crate::classic::clvm::OPERATORS_LATEST_VERSION; use crate::classic::clvm::{keyword_from_atom, keyword_to_atom}; +use crate::classic::clvm::casts::By; use crate::classic::clvm_tools::ir::r#type::IRRepr; use crate::classic::clvm_tools::ir::reader::IRReader; use crate::classic::clvm_tools::ir::writer::write_ir; @@ -25,7 +26,7 @@ pub fn assemble_from_ir( ir_sexp: Rc, ) -> Result { match ir_sexp.borrow() { - IRRepr::Null => Ok(allocator.null()), + IRRepr::Null => Ok(allocator.nil()), IRRepr::Quotes(b) => allocator.new_atom(b.data()), IRRepr::Int(b, _signed) => allocator.new_atom(b.data()), IRRepr::Hex(b) => allocator.new_atom(b.data()), @@ -127,7 +128,7 @@ pub fn disassemble_to_ir_with_kw( SExp::Atom => { // sexp is the only node in scope. - let bytes = Bytes::new(Some(BytesFromType::Raw(allocator.atom(sexp).to_vec()))); + let bytes = Bytes::new(Some(BytesFromType::Raw(By::new(allocator, sexp).to_vec()))); ir_for_atom(&bytes, allow_keyword, keyword_from_atom) } } @@ -157,6 +158,6 @@ pub fn assemble(allocator: &mut Allocator, s: &str) -> Result let mut reader = IRReader::new(stream); reader .read_expr() - .map_err(|e| EvalErr(allocator.null(), e.to_string())) + .map_err(|e| EvalErr(allocator.nil(), e.to_string())) .and_then(|ir| assemble_from_ir(allocator, Rc::new(ir))) } diff --git a/src/classic/clvm_tools/clvmc.rs b/src/classic/clvm_tools/clvmc.rs index 9db074e35..0f1327ea3 100644 --- a/src/classic/clvm_tools/clvmc.rs +++ b/src/classic/clvm_tools/clvmc.rs @@ -90,7 +90,7 @@ pub fn compile_clvm_text_maybe_opt( input_path: &str, classic_with_opts: bool, ) -> Result { - let ir_src = read_ir(text).map_err(|s| EvalErr(allocator.null(), s.to_string()))?; + let ir_src = read_ir(text).map_err(|s| EvalErr(allocator.nil(), s.to_string()))?; let assembled_sexp = assemble_from_ir(allocator, Rc::new(ir_src))?; let dialect = detect_modern(allocator, assembled_sexp); @@ -118,7 +118,7 @@ pub fn compile_clvm_text_maybe_opt( Ok(convert_to_clvm_rs(allocator, res)?) } else { let compile_invoke_code = run(allocator); - let input_sexp = allocator.new_pair(assembled_sexp, allocator.null())?; + let input_sexp = allocator.new_pair(assembled_sexp, allocator.nil())?; let run_program = run_program_for_search_paths(input_path, &opts.get_search_paths(), false); if classic_with_opts { run_program.set_compiler_opts(Some(opts)); diff --git a/src/classic/clvm_tools/cmds.rs b/src/classic/clvm_tools/cmds.rs index 9bf9771e3..883ce5875 100644 --- a/src/classic/clvm_tools/cmds.rs +++ b/src/classic/clvm_tools/cmds.rs @@ -241,7 +241,7 @@ impl TConversion for OpcConversion { .and_then(|ir_sexp| assemble_from_ir(allocator, Rc::new(ir_sexp)).map_err(|e| e.1)) .map(|sexp| t(sexp, sexp_as_bin(allocator, sexp).hex())) .map(Ok) // Flatten result type to Ok - .unwrap_or_else(|err| Ok(t(allocator.null(), err))) // Original code printed error messages on stdout, ret 0 on CLVM error + .unwrap_or_else(|err| Ok(t(allocator.nil(), err))) // Original code printed error messages on stdout, ret 0 on CLVM error } } @@ -812,7 +812,7 @@ fn calculate_cost_offset( This is a hack and need to go away, probably when we do dialects for real, and then the dialect can have a `run_program` API. */ - let almost_empty_list = enlist(allocator, &[allocator.null()]).unwrap(); + let almost_empty_list = enlist(allocator, &[allocator.nil()]).unwrap(); let cost = run_program .run_program(allocator, run_script, almost_empty_list, None) .map(|x| x.0) diff --git a/src/classic/clvm_tools/debug.rs b/src/classic/clvm_tools/debug.rs index 15bd5142a..8e8f85859 100644 --- a/src/classic/clvm_tools/debug.rs +++ b/src/classic/clvm_tools/debug.rs @@ -102,7 +102,7 @@ pub fn build_symbol_dump( let mut map_result: Vec = Vec::new(); for (k, v) in constants_lookup.iter() { - let run_result = run_program.run_program(allocator, *v, allocator.null(), None)?; + let run_result = run_program.run_program(allocator, *v, allocator.nil(), None)?; let sha256 = sha256tree(allocator, run_result.1).hex(); let sha_atom = allocator.new_atom(sha256.as_bytes())?; @@ -152,7 +152,7 @@ fn text_trace( let mut env = env_; match symbol { Some(sym) => { - env = rest(allocator, env).unwrap_or_else(|_| allocator.null()); + env = rest(allocator, env).unwrap_or_else(|_| allocator.nil()); let symbol_atom = allocator.new_atom(sym.as_bytes()).unwrap(); let symbol_list = allocator.new_pair(symbol_atom, env).unwrap(); symbol_val = disassemble_f(allocator, symbol_list); @@ -180,7 +180,7 @@ fn table_trace( ) { let (sexp, args) = match allocator.sexp(form) { SExp::Pair(sexp, args) => (sexp, args), - SExp::Atom => (form, allocator.null()), + SExp::Atom => (form, allocator.nil()), }; stdout.write_str(&format!("exp: {}\n", disassemble_f(allocator, sexp))); diff --git a/src/classic/clvm_tools/pattern_match.rs b/src/classic/clvm_tools/pattern_match.rs index 02e894b4e..0ab6f31cc 100644 --- a/src/classic/clvm_tools/pattern_match.rs +++ b/src/classic/clvm_tools/pattern_match.rs @@ -1,4 +1,5 @@ use crate::classic::clvm::__type_compatibility__::{Bytes, BytesFromType}; +use crate::classic::clvm::casts::By; use crate::classic::clvm::sexp::equal_to; use clvm_rs::allocator::{Allocator, NodePtr, SExp}; use std::collections::HashMap; @@ -61,14 +62,18 @@ pub fn match_sexp( } (SExp::Pair(pleft, pright), _) => match (allocator.sexp(pleft), allocator.sexp(pright)) { (SExp::Atom, SExp::Atom) => { - let pright_atom = allocator.atom(pright).to_vec(); + // We need to avoid a double borrow for unify bindings below. + let pright_atom = By::new(allocator, pright).to_vec(); match allocator.sexp(sexp) { SExp::Atom => { + let sexp_atom = By::new(allocator, sexp).to_vec(); + // Expression is ($ . $), sexp is '$', result: no capture. // Avoid double borrow. - if allocator.atom(pleft) == ATOM_MATCH { - if allocator.atom(pright) == ATOM_MATCH { - if allocator.atom(sexp) == ATOM_MATCH { + let pleft_atom = By::new(allocator, pleft).to_vec(); + if pleft_atom == ATOM_MATCH { + if pright_atom == ATOM_MATCH { + if sexp_atom == ATOM_MATCH { return Some(HashMap::new()); } return None; @@ -76,9 +81,9 @@ pub fn match_sexp( return unify_bindings(allocator, known_bindings, &pright_atom, sexp); } - if allocator.atom(pleft) == SEXP_MATCH { - if allocator.atom(pright) == SEXP_MATCH - && allocator.atom(sexp) == SEXP_MATCH + if pleft_atom == SEXP_MATCH { + if pright_atom == SEXP_MATCH + && sexp_atom == SEXP_MATCH { return Some(HashMap::new()); } @@ -95,8 +100,9 @@ pub fn match_sexp( None } SExp::Pair(sleft, sright) => { - if allocator.atom(pleft) == SEXP_MATCH - && allocator.atom(pright) != SEXP_MATCH + let pleft_atom = By::new(allocator, pleft).to_vec(); + if pleft_atom == SEXP_MATCH + && pright_atom != SEXP_MATCH { return unify_bindings( allocator, diff --git a/src/classic/clvm_tools/sha256tree.rs b/src/classic/clvm_tools/sha256tree.rs index ebbc3197c..3e5e353f5 100644 --- a/src/classic/clvm_tools/sha256tree.rs +++ b/src/classic/clvm_tools/sha256tree.rs @@ -3,6 +3,7 @@ use std::fmt::Display; use clvm_rs::allocator::{Allocator, NodePtr, SExp}; use crate::classic::clvm::__type_compatibility__::{sha256, Bytes, BytesFromType}; +use crate::classic::clvm::casts::By; #[derive(Clone, Debug, PartialEq, Eq, Hash)] pub struct TreeHash { @@ -29,16 +30,19 @@ pub fn sha256tree(allocator: &mut Allocator, v: NodePtr) -> Bytes { let left = sha256tree(allocator, l); let right = sha256tree(allocator, r); sha256( - Bytes::new(Some(BytesFromType::Raw(vec![2]))) + &Bytes::new(Some(BytesFromType::Raw(vec![2]))) .concat(&left) - .concat(&right), + .concat(&right).data(), + ) + } + SExp::Atom => { + let v_atom = By::new(allocator, v); + sha256( + &Bytes::new(Some(BytesFromType::Raw(vec![1]))).concat(&Bytes::new(Some( + // only v in scope. + BytesFromType::Raw(v_atom.to_vec()) + ))).data(), ) } - SExp::Atom => sha256( - Bytes::new(Some(BytesFromType::Raw(vec![1]))).concat(&Bytes::new(Some( - // only v in scope. - BytesFromType::Raw(allocator.atom(v).to_vec()), - ))), - ), } } diff --git a/src/classic/clvm_tools/stages/stage_2/compile.rs b/src/classic/clvm_tools/stages/stage_2/compile.rs index 27d8c3f04..088eb4f1b 100644 --- a/src/classic/clvm_tools/stages/stage_2/compile.rs +++ b/src/classic/clvm_tools/stages/stage_2/compile.rs @@ -1,3 +1,4 @@ +use std::borrow::Borrow; use std::collections::{HashMap, HashSet}; use std::rc::Rc; @@ -5,6 +6,7 @@ use clvm_rs::allocator::{Allocator, NodePtr, SExp}; use clvm_rs::reduction::{EvalErr, Reduction, Response}; use crate::classic::clvm::__type_compatibility__::{Bytes, BytesFromType}; +use crate::classic::clvm::casts::By; use crate::classic::clvm::sexp::{enlist, first, map_m, non_nil, proper_list, rest}; use crate::classic::clvm::OPERATORS_LATEST_VERSION; use crate::classic::clvm::{keyword_from_atom, keyword_to_atom}; @@ -129,17 +131,18 @@ pub fn compile_qq( SExp::Pair(op, sexp_rest) => { if let SExp::Atom = allocator.sexp(op) { // opbuf => op - if allocator.atom(op).to_vec() == qq_atom() { + let opbuf = By::new(allocator, op); + if opbuf.borrow() == qq_atom() { return m! { cons_atom <- allocator.new_atom(&[4]); subexp <- compile_qq(allocator, sexp_rest, macro_lookup, symbol_table, runner.clone(), level+1); - quoted_null <- quote(allocator, allocator.null()); + quoted_null <- quote(allocator, allocator.nil()); consed <- enlist(allocator, &[cons_atom, subexp, quoted_null]); run_list <- enlist(allocator, &[cons_atom, op, consed]); com_qq(allocator, "qq sexp pair".to_string(), macro_lookup, symbol_table, runner, run_list) }; - } else if allocator.atom(op).to_vec() == unquote_atom() { + } else if opbuf.borrow() == unquote_atom() { // opbuf if level == 1 { // (qq (unquote X)) => X @@ -153,7 +156,7 @@ pub fn compile_qq( cons_atom <- allocator.new_atom(&[4]); subexp <- compile_qq(allocator, sexp_rest, macro_lookup, symbol_table, runner.clone(), level-1); - quoted_null <- quote(allocator, allocator.null()); + quoted_null <- quote(allocator, allocator.nil()); consed_subexp <- enlist(allocator, &[cons_atom, subexp, quoted_null]); run_list <- enlist(allocator, &[cons_atom, op, consed_subexp]); com_qq(allocator, "qq pair general".to_string(), macro_lookup, symbol_table, runner, run_list) @@ -212,7 +215,8 @@ fn lower_quote_(allocator: &mut Allocator, prog: NodePtr) -> Result 1 { mp_list[1] } else { - allocator.null() + allocator.nil() }; match allocator.sexp(mp_list[0]) { SExp::Atom => { // was macro_name, but it's singular and probably // not useful to rename. - if allocator.atom(mp_list[0]) == operator { + if By::new(allocator, mp_list[0]).u8() == operator { return Ok(Some(value)); } } @@ -382,13 +386,13 @@ fn transform_program_atom( continue; } - let value = if v.len() > 1 { v[1] } else { allocator.null() }; + let value = if v.len() > 1 { v[1] } else { allocator.nil() }; match allocator.sexp(v[0]) { SExp::Atom => { // v[0] is close by, and probably not useful to // rename here. - if allocator.atom(v[0]) == a { + if By::new(allocator, v[0]).u8() == a { return Ok(Reduction(1, value)); } } @@ -465,15 +469,15 @@ fn find_symbol_match( SExp::Atom => { let symbol = symdef[0]; let value = if symdef.len() == 1 { - allocator.null() + allocator.nil() } else { symdef[1] }; - let symbuf = allocator.atom(symdef[0]); - if b"*" == symbuf { + let symbuf = By::new(allocator, symdef[0]); + if b"*" == symbuf.u8() { return Ok(Some(SymbolResult::Direct(r))); - } else if opname == symbuf { + } else if opname == symbuf.u8() { return Ok(Some(SymbolResult::Matched(symbol, value))); } } @@ -640,7 +644,7 @@ fn do_com_prog_( match allocator.sexp(prog) { SExp::Atom => { // Note: can't co-borrow with allocator below. - let prog_bytes = allocator.atom(prog).to_vec(); + let prog_bytes = By::new(allocator, prog).to_vec(); transform_program_atom( allocator, prog, @@ -652,7 +656,7 @@ fn do_com_prog_( match allocator.sexp(operator) { SExp::Atom => { // Note: can't co-borrow with allocator below. - let opbuf = allocator.atom(operator).to_vec(); + let opbuf = By::new(allocator, operator).to_vec(); get_macro_program(allocator, &opbuf, macro_lookup). and_then(|x| match x { Some(value) => { @@ -723,7 +727,7 @@ pub fn do_com_prog_for_dialect( ) -> Response { match allocator.sexp(sexp) { SExp::Pair(prog, extras) => { - let mut symbol_table = allocator.null(); + let mut symbol_table = allocator.nil(); let macro_lookup; let mut elist = Vec::new(); @@ -777,20 +781,19 @@ pub fn get_compile_filename( let cvt_prog = assemble(allocator, "(_get_compile_filename)")?; let Reduction(_, cvt_prog_result) = - runner.run_program(allocator, cvt_prog, allocator.null(), None)?; + runner.run_program(allocator, cvt_prog, allocator.nil(), None)?; - if cvt_prog_result == allocator.null() { + if cvt_prog_result == allocator.nil() { return Ok(None); } if let SExp::Atom = allocator.sexp(cvt_prog_result) { // only cvt_prog_result in scope. - let abuf = allocator.atom(cvt_prog_result).to_vec(); - return Ok(Some(Bytes::new(Some(BytesFromType::Raw(abuf))).decode())); + return Ok(Some(Bytes::new(Some(BytesFromType::Raw(By::new(allocator, cvt_prog_result).to_vec()))).decode())); } Err(EvalErr( - allocator.null(), + allocator.nil(), "Couldn't decode result filename".to_string(), )) } @@ -801,7 +804,7 @@ pub fn get_search_paths( ) -> Result, EvalErr> { let search_paths_prog = assemble(allocator, "(_get_include_paths)")?; let search_path_result = - runner.run_program(allocator, search_paths_prog, allocator.null(), None)?; + runner.run_program(allocator, search_paths_prog, allocator.nil(), None)?; let mut res = Vec::new(); if let Some(l) = proper_list(allocator, search_path_result.1, true) { @@ -809,7 +812,7 @@ pub fn get_search_paths( if let SExp::Atom = allocator.sexp(elt) { // Only elt in scope. res.push( - Bytes::new(Some(BytesFromType::Raw(allocator.atom(elt).to_vec()))).decode(), + Bytes::new(Some(BytesFromType::Raw(By::new(allocator, elt).to_vec()))).decode(), ); } } diff --git a/src/classic/clvm_tools/stages/stage_2/defaults.rs b/src/classic/clvm_tools/stages/stage_2/defaults.rs index 5c09c13d1..d6a1b173c 100644 --- a/src/classic/clvm_tools/stages/stage_2/defaults.rs +++ b/src/classic/clvm_tools/stages/stage_2/defaults.rs @@ -106,7 +106,7 @@ fn build_default_macro_lookup( macros_src: &[String], ) -> NodePtr { let run = assemble(allocator, "(a (com 2 3) 1)").unwrap(); - let mut default_macro_lookup: NodePtr = allocator.null(); + let mut default_macro_lookup: NodePtr = allocator.nil(); for macro_src in macros_src { let macro_sexp = assemble(allocator, macro_src).unwrap(); let env = allocator diff --git a/src/classic/clvm_tools/stages/stage_2/inline.rs b/src/classic/clvm_tools/stages/stage_2/inline.rs index 215c3679d..58f632e11 100644 --- a/src/classic/clvm_tools/stages/stage_2/inline.rs +++ b/src/classic/clvm_tools/stages/stage_2/inline.rs @@ -1,4 +1,5 @@ use crate::classic::clvm::__type_compatibility__::{bi_one, bi_zero}; +use crate::classic::clvm::casts::By; use crate::classic::clvm::sexp::{enlist, proper_list}; use crate::compiler::gensym::gensym; use crate::util::Number; @@ -19,7 +20,8 @@ pub fn is_at_capture( allocator.sexp(tree_first), proper_list(allocator, tree_rest, true), ) { - if allocator.atom(tree_first) == b"@" && spec.len() == 2 { + let tree_first_atom = By::new(allocator, tree_first); + if tree_first_atom.u8() == b"@" && spec.len() == 2 { return Some((spec[0], spec[1])); } } @@ -100,7 +102,7 @@ fn formulate_path_selections_for_destructuring_arg( }; // Was cbuf from capture. - selections.insert(allocator.atom(capture).to_vec(), tail); + selections.insert(By::new(allocator, capture).to_vec(), tail); return formulate_path_selections_for_destructuring_arg( allocator, @@ -149,11 +151,11 @@ fn formulate_path_selections_for_destructuring_arg( } SExp::Atom => { // Note: can't co-borrow with allocator below. - let buf = allocator.atom(arg_sexp).to_vec(); + let buf = By::new(allocator, arg_sexp).to_vec(); if !buf.is_empty() { if let Some(capture) = referenced_from { let tail = wrap_path_selection(allocator, arg_path + arg_depth, capture)?; - selections.insert(buf, tail); + selections.insert(buf.to_vec(), tail); return Ok(arg_sexp); } } @@ -229,8 +231,7 @@ pub fn formulate_path_selections_for_destructuring( let quoted_arg_list = wrap_in_unquote(allocator, capture)?; let tail = wrap_in_compile_time_list(allocator, quoted_arg_list)?; // Was: cbuf from capture. - let buf = allocator.atom(capture); - selections.insert(buf.to_vec(), tail); + selections.insert(By::new(allocator, capture).to_vec(), tail); let newsub = formulate_path_selections_for_destructuring_arg( allocator, substructure, diff --git a/src/classic/clvm_tools/stages/stage_2/module.rs b/src/classic/clvm_tools/stages/stage_2/module.rs index 74083616a..0affe6dd9 100644 --- a/src/classic/clvm_tools/stages/stage_2/module.rs +++ b/src/classic/clvm_tools/stages/stage_2/module.rs @@ -6,6 +6,7 @@ use clvm_rs::allocator::{Allocator, NodePtr, SExp}; use clvm_rs::reduction::EvalErr; use crate::classic::clvm::__type_compatibility__::{Bytes, BytesFromType}; +use crate::classic::clvm::casts::By; use crate::classic::clvm::sexp::{ enlist, first, flatten, fold_m, map_m, non_nil, nonempty_last, proper_list, rest, First, NodeSel, Rest, SelectNode, ThisNode, @@ -52,7 +53,7 @@ impl CompileOutput { // export type TBuildTree = Bytes | Tuple | []; fn build_tree(allocator: &mut Allocator, items: &[Vec]) -> Result { if items.is_empty() { - Ok(allocator.null()) + Ok(allocator.nil()) } else if items.len() == 1 { allocator.new_atom(&items[0]) } else { @@ -72,7 +73,7 @@ fn build_tree_program(allocator: &mut Allocator, items: &[NodePtr]) -> Result { // Only code in scope. - let code_atom = allocator.atom(code); + let code_atom = By::new(allocator, code); let matching_args = args .iter() - .filter(|arg| *arg == code_atom) + .filter(|arg| *arg == code_atom.u8()) .cloned() .collect::>>(); if !matching_args.is_empty() { @@ -287,14 +288,16 @@ fn defun_inline_to_macro( .iter() .filter_map(|x| { if let SExp::Atom = allocator.sexp(*x) { - // only x usefully in scope. - Some(allocator.atom(*x)) + let x_buf = By::new(allocator, *x); + if x_buf.u8().is_empty() { + None + } else { + Some(x_buf.to_vec()) + } } else { None } }) - .filter(|x| !x.is_empty()) - .map(|v| v.to_vec()) .collect::>>(); let unquoted_code = unquote_args(allocator, code, &arg_name_list, &destructure_matches)?; @@ -326,12 +329,12 @@ fn parse_mod_sexp( let op = match allocator.sexp(op_node) { // op_node in use. - SExp::Atom => allocator.atom(op_node).to_vec(), + SExp::Atom => By::new(allocator, op_node).to_vec(), _ => Vec::new(), }; let name = match allocator.sexp(name_node) { // name_node in use. - SExp::Atom => allocator.atom(name_node).to_vec(), + SExp::Atom => By::new(allocator, name_node).to_vec(), _ => Vec::new(), }; @@ -462,7 +465,7 @@ fn compile_mod_stage_1( let main_list = enlist( allocator, - &[allocator.null(), *delayed_body] + &[allocator.nil(), *delayed_body] )?; result_collection.functions.insert( @@ -499,7 +502,7 @@ fn compile_mod_stage_1( run_program.run_program( allocator, compiled, - allocator.null(), + allocator.nil(), None )?; @@ -507,7 +510,7 @@ fn compile_mod_stage_1( run_program.run_program( allocator, compilation_result.1, - allocator.null(), + allocator.nil(), None )?; @@ -518,11 +521,11 @@ fn compile_mod_stage_1( } if !processed { - return Err(EvalErr(allocator.null(), "got stuck untangling defconst dependencies".to_string())); + return Err(EvalErr(allocator.nil(), "got stuck untangling defconst dependencies".to_string())); } } - let uncompiled_main = nonempty_last(allocator.null(), &alist)?; + let uncompiled_main = nonempty_last(allocator.nil(), &alist)?; let main_list = enlist( allocator, @@ -835,7 +838,7 @@ pub fn compile_mod( ) -> Result { // Deal with the "mod" keyword. let produce_extra_info_prog = assemble(allocator, "(_symbols_extra_info)")?; - let produce_extra_info_null = allocator.null(); + let produce_extra_info_null = allocator.nil(); let extra_info_res = run_program.run_program( allocator, produce_extra_info_prog, diff --git a/src/classic/clvm_tools/stages/stage_2/operators.rs b/src/classic/clvm_tools/stages/stage_2/operators.rs index 5b433395f..a4b225bb6 100644 --- a/src/classic/clvm_tools/stages/stage_2/operators.rs +++ b/src/classic/clvm_tools/stages/stage_2/operators.rs @@ -12,6 +12,7 @@ use clvm_rs::reduction::{EvalErr, Reduction, Response}; use clvm_rs::run_program::run_program_with_pre_eval; use crate::classic::clvm::__type_compatibility__::{Bytes, BytesFromType, Stream}; +use crate::classic::clvm::casts::By; use crate::classic::clvm::OPERATORS_LATEST_VERSION; use crate::classic::clvm::keyword_from_atom; @@ -146,7 +147,7 @@ impl CompilerOperatorsInternal { if self.symbols_extra_info { Ok(Reduction(1, allocator.new_atom(&[1])?)) } else { - Ok(Reduction(1, allocator.null())) + Ok(Reduction(1, allocator.nil())) } } @@ -195,7 +196,7 @@ impl CompilerOperatorsInternal { // return EvalErr. let parse_file_content = |allocator: &mut Allocator, content: &String| { read_ir(content) - .map_err(|e| EvalErr(allocator.null(), e.to_string())) + .map_err(|e| EvalErr(allocator.nil(), e.to_string())) .and_then(|ir| { assemble_from_ir(allocator, Rc::new(ir)).map(|ir_sexp| Reduction(1, ir_sexp)) }) @@ -205,7 +206,7 @@ impl CompilerOperatorsInternal { SExp::Pair(f, _) => match allocator.sexp(f) { SExp::Atom => { let filename = - Bytes::new(Some(BytesFromType::Raw(allocator.atom(f).to_vec()))).decode(); + Bytes::new(Some(BytesFromType::Raw(By::new(allocator, f).to_vec()))).decode(); // Use the read interface in CompilerOpts if we have one. if let Some(opts) = self.get_compiler_opts() { if let Ok((_, content)) = @@ -218,16 +219,16 @@ impl CompilerOperatorsInternal { // Use the filesystem like normal if the opts couldn't find // the file. fs::read_to_string(filename) - .map_err(|_| EvalErr(allocator.null(), "Failed to read file".to_string())) + .map_err(|_| EvalErr(allocator.nil(), "Failed to read file".to_string())) .and_then(|content| parse_file_content(allocator, &content)) } _ => Err(EvalErr( - allocator.null(), + allocator.nil(), "filename is not an atom".to_string(), )), }, _ => Err(EvalErr( - allocator.null(), + allocator.nil(), "given a program that is an atom".to_string(), )), } @@ -237,9 +238,8 @@ impl CompilerOperatorsInternal { if let SExp::Pair(filename_sexp, r) = allocator.sexp(sexp) { if let SExp::Pair(data, _) = allocator.sexp(r) { if let SExp::Atom = allocator.sexp(filename_sexp) { - let filename_buf = allocator.atom(filename_sexp); let filename_bytes = - Bytes::new(Some(BytesFromType::Raw(filename_buf.to_vec()))); + Bytes::new(Some(BytesFromType::Raw(By::new(allocator, filename_sexp).to_vec()))); let ir = disassemble_to_ir_with_kw( allocator, data, @@ -252,7 +252,7 @@ impl CompilerOperatorsInternal { .map_err(|_| { EvalErr(sexp, format!("failed to write {}", filename_bytes.decode())) }) - .map(|_| Reduction(1, allocator.null())); + .map(|_| Reduction(1, allocator.nil())); } } } @@ -266,7 +266,7 @@ impl CompilerOperatorsInternal { } fn get_include_paths(&self, allocator: &mut Allocator) -> Response { - let mut converted_search_paths = allocator.null(); + let mut converted_search_paths = allocator.nil(); for s in self.search_paths.iter().rev() { let search_path_string = allocator.new_atom(s.as_bytes())?; converted_search_paths = @@ -286,7 +286,7 @@ impl CompilerOperatorsInternal { if let SExp::Atom = allocator.sexp(l) { // l most relevant in scope. let filename = - Bytes::new(Some(BytesFromType::Raw(allocator.atom(l).to_vec()))).decode(); + Bytes::new(Some(BytesFromType::Raw(By::new(allocator, l).to_vec()))).decode(); // If we have a compiler opts injected, let that handle reading // files. The name will bubble up to the _read function. if self.get_compiler_opts().is_some() { @@ -320,10 +320,10 @@ impl CompilerOperatorsInternal { if let (SExp::Atom, SExp::Atom) = (allocator.sexp(hash), allocator.sexp(name)) { // hash and name in scope. let hash_text = - Bytes::new(Some(BytesFromType::Raw(allocator.atom(hash).to_vec()))) + Bytes::new(Some(BytesFromType::Raw(By::new(allocator, hash).to_vec()))) .decode(); let name_text = - Bytes::new(Some(BytesFromType::Raw(allocator.atom(name).to_vec()))) + Bytes::new(Some(BytesFromType::Raw(By::new(allocator, name).to_vec()))) .decode(); self.compile_outcomes.replace_with(|co| { @@ -336,7 +336,7 @@ impl CompilerOperatorsInternal { } } - Ok(Reduction(1, allocator.null())) + Ok(Reduction(1, allocator.nil())) } fn get_disassembly_ver(&self) -> usize { @@ -347,16 +347,16 @@ impl CompilerOperatorsInternal { } impl Dialect for CompilerOperatorsInternal { - fn quote_kw(&self) -> &[u8] { - &[1] + fn quote_kw(&self) -> u32 { + 1 } - fn apply_kw(&self) -> &[u8] { - &[2] + fn apply_kw(&self) -> u32 { + 2 } - fn softfork_kw(&self) -> &[u8] { - &[36] + fn softfork_kw(&self) -> u32 { + 36 } // The softfork operator comes with an extension argument. @@ -387,26 +387,26 @@ impl Dialect for CompilerOperatorsInternal { match allocator.sexp(op) { SExp::Atom => { // use of op obvious. - let opbuf = allocator.atom(op); - if opbuf == "_read".as_bytes() { + let opbuf = By::new(allocator, op); + if opbuf.u8() == b"_read" { self.read(allocator, sexp) - } else if opbuf == "_write".as_bytes() { + } else if opbuf.u8() == b"_write" { self.write(allocator, sexp) - } else if opbuf == "com".as_bytes() { + } else if opbuf.u8() == b"com" { do_com_prog_for_dialect(self.get_runner(), allocator, sexp) - } else if opbuf == "opt".as_bytes() { + } else if opbuf.u8() == b"opt" { do_optimize(self.get_runner(), allocator, &self.opt_memo, sexp) - } else if opbuf == "_set_symbol_table".as_bytes() { + } else if opbuf.u8() == b"_set_symbol_table" { self.set_symbol_table(allocator, sexp) - } else if opbuf == "_get_compile_filename".as_bytes() { + } else if opbuf.u8() == b"_get_compile_filename" { self.get_compile_filename(allocator) - } else if opbuf == "_get_include_paths".as_bytes() { + } else if opbuf.u8() == b"_get_include_paths" { self.get_include_paths(allocator) - } else if opbuf == "_full_path_for_name".as_bytes() { + } else if opbuf.u8() == b"_full_path_for_name" { self.get_full_path_for_filename(allocator, sexp) - } else if opbuf == "_symbols_extra_info".as_bytes() { + } else if opbuf.u8() == b"_symbols_extra_info" { self.symbols_extra_info(allocator) - } else if opbuf == "_get_source_file".as_bytes() { + } else if opbuf.u8() == b"_get_source_file" { self.get_source_file(allocator) } else { self.base_dialect.op( diff --git a/src/classic/clvm_tools/stages/stage_2/optimize.rs b/src/classic/clvm_tools/stages/stage_2/optimize.rs index 9aef8e77c..591b889d1 100644 --- a/src/classic/clvm_tools/stages/stage_2/optimize.rs +++ b/src/classic/clvm_tools/stages/stage_2/optimize.rs @@ -10,6 +10,7 @@ use clvm_rs::cost::Cost; use clvm_rs::reduction::{EvalErr, Reduction, Response}; use crate::classic::clvm::__type_compatibility__::{bi_one, bi_zero}; +use crate::classic::clvm::casts::By; use crate::classic::clvm::sexp::{ atom, enlist, equal_to, first, fold_m, map_m, non_nil, proper_list, }; @@ -42,7 +43,7 @@ pub fn seems_constant_tail(allocator: &mut Allocator, sexp_: NodePtr) -> bool { sexp = r; } SExp::Atom => { - return sexp == allocator.null(); + return sexp == allocator.nil(); } } } @@ -51,16 +52,16 @@ pub fn seems_constant_tail(allocator: &mut Allocator, sexp_: NodePtr) -> bool { pub fn seems_constant(allocator: &mut Allocator, sexp: NodePtr) -> bool { match allocator.sexp(sexp) { SExp::Atom => { - return sexp == allocator.null(); + return sexp == allocator.nil(); } SExp::Pair(operator, r) => { match allocator.sexp(operator) { SExp::Atom => { // Was buf of operator. - let atom = allocator.atom(operator); - if atom.len() == 1 && atom[0] == 1 { + let atom = By::new(allocator, operator); + if atom.u8().len() == 1 && atom.u8()[0] == 1 { return true; - } else if atom.len() == 1 && atom[0] == 8 { + } else if atom.u8().len() == 1 && atom.u8()[0] == 8 { return false; } } @@ -94,8 +95,8 @@ pub fn constant_optimizer( if let SExp::Pair(first, _) = allocator.sexp(r) { // first relevant in scope. if let SExp::Atom = allocator.sexp(first) { - let buf = allocator.atom(first); - if buf.len() == 1 && buf[0] == 1 { + let buf = By::new(allocator, first); + if buf.u8().len() == 1 && buf.u8()[0] == 1 { // Short circuit already quoted expression. return Ok(r); } @@ -117,7 +118,7 @@ pub fn constant_optimizer( res <- runner.run_program( allocator, r, - allocator.null(), + allocator.nil(), None ); let r1 = res.1; @@ -139,8 +140,8 @@ pub fn constant_optimizer( pub fn is_args_call(allocator: &Allocator, r: NodePtr) -> bool { if let SExp::Atom = allocator.sexp(r) { // Only r in scope. - let buf = allocator.atom(r); - buf.len() == 1 && buf[0] == 1 + let buf = By::new(allocator, r); + buf.u8().len() == 1 && buf.u8()[0] == 1 } else { false } @@ -192,7 +193,7 @@ fn cons_f(allocator: &mut Allocator, args: NodePtr) -> Result } else { m! { first_atom <- allocator.new_atom(&[5]); - tail <- allocator.new_pair(args, allocator.null()); + tail <- allocator.new_pair(args, allocator.nil()); allocator.new_pair(first_atom, tail) } } @@ -207,7 +208,7 @@ fn cons_r(allocator: &mut Allocator, args: NodePtr) -> Result } else { m! { rest_atom <- allocator.new_atom(&[6]); - tail <- allocator.new_pair(args, allocator.null()); + tail <- allocator.new_pair(args, allocator.nil()); allocator.new_pair(rest_atom, tail) } } @@ -222,7 +223,8 @@ fn path_from_args( match allocator.sexp(sexp) { SExp::Atom => { // Only sexp in scope. - let v = number_from_u8(allocator.atom(sexp)); + let sexp_atom = By::new(allocator, sexp); + let v = number_from_u8(sexp_atom.u8()); if v <= bi_one() { Ok(new_args) } else { @@ -256,8 +258,8 @@ pub fn sub_args( } SExp::Atom => { // Atom is a reflection of first_pre. - let atom = allocator.atom(first_pre); - if atom.len() == 1 && atom[0] == 1 { + let atom = By::new(allocator, first_pre); + if atom.u8().len() == 1 && atom.u8()[0] == 1 { return Ok(sexp); } else { first = first_pre; @@ -376,8 +378,8 @@ pub fn var_change_optimizer_cons_eval( SExp::Pair(val_first, _) => match allocator.sexp(val_first) { SExp::Atom => { // Atom reflects val_first. - let vf_buf = allocator.atom(val_first); - (vf_buf.len() != 1 || vf_buf[0] != 1) as i32 + let vf_buf = By::new(allocator, val_first); + (vf_buf.u8().len() != 1 || vf_buf.u8()[0] != 1) as i32 } _ => 0, }, @@ -420,7 +422,8 @@ pub fn children_optimizer( return Ok(r); } if let SExp::Atom = allocator.sexp(list[0]) { - if allocator.atom(list[0]).to_vec() == vec![1] { + let list0_atom = By::new(allocator, list[0]); + if list0_atom.u8() == &[1] { return Ok(r); } } @@ -569,7 +572,7 @@ fn quote_null_optimizer( // This applies the transform `(q . 0)` => `0` let t1 = match_sexp(allocator, quote_pattern_1, r, HashMap::new()); - Ok(t1.map(|_| allocator.null()).unwrap_or_else(|| r)) + Ok(t1.map(|_| allocator.nil()).unwrap_or_else(|| r)) } fn apply_null_pattern_1(allocator: &mut Allocator) -> NodePtr { @@ -586,7 +589,7 @@ fn apply_null_optimizer( // This applies the transform `(a 0 ARGS)` => `0` let t1 = match_sexp(allocator, apply_null_pattern_1, r, HashMap::new()); - Ok(t1.map(|_| allocator.null()).unwrap_or_else(|| r)) + Ok(t1.map(|_| allocator.nil()).unwrap_or_else(|| r)) } struct OptimizerRunner<'a> { diff --git a/src/classic/clvm_tools/stages/stage_2/reader.rs b/src/classic/clvm_tools/stages/stage_2/reader.rs index 716988f75..8b1f21c18 100644 --- a/src/classic/clvm_tools/stages/stage_2/reader.rs +++ b/src/classic/clvm_tools/stages/stage_2/reader.rs @@ -5,6 +5,7 @@ use clvmr::allocator::{Allocator, NodePtr, SExp}; use clvmr::reduction::EvalErr; use crate::classic::clvm::__type_compatibility__::{Bytes, Stream, UnvalidatedBytesFromType}; +use crate::classic::clvm::casts::By; use crate::classic::clvm::serialize::{sexp_from_stream, SimpleCreateCLVMObject}; use crate::classic::clvm::sexp::{proper_list, rest}; use crate::classic::clvm_tools::stages::assemble; @@ -32,7 +33,7 @@ pub fn convert_hex_to_sexp( let content_bytes = Bytes::new_validated(Some(UnvalidatedBytesFromType::Hex(decode_string( file_data, )))) - .map_err(|e| EvalErr(allocator.null(), e.to_string()))?; + .map_err(|e| EvalErr(allocator.nil(), e.to_string()))?; let mut reader_stream = Stream::new(Some(content_bytes)); Ok(sexp_from_stream( allocator, @@ -97,38 +98,42 @@ pub fn process_embed_file( ) { // Note: we don't want to keep borrowing here because we // need the mutable borrow below. - let name_buf = allocator.atom(l[0]).to_vec(); - let kind_buf = allocator.atom(l[1]); - let filename_buf = allocator.atom(l[2]).to_vec(); - let file_data = if kind_buf == b"bin" { + let (name, kind, filename) = + { + (By::new(allocator, l[0]).to_vec(), + By::new(allocator, l[1]).to_vec(), + By::new(allocator, l[2]).to_vec() + ) + }; + let file_data = if &kind == b"bin" { let file = read_file( runner, allocator, declaration_sexp, - &decode_string(&filename_buf), + &decode_string(&filename), )?; allocator.new_atom(&file.data)? - } else if kind_buf == b"hex" { + } else if &kind == b"hex" { let file = read_file( runner, allocator, declaration_sexp, - &decode_string(&filename_buf), + &decode_string(&filename), )?; convert_hex_to_sexp(allocator, &file.data)? - } else if kind_buf == b"sexp" { + } else if &kind == b"sexp" { let file = read_file( runner, allocator, declaration_sexp, - &decode_string(&filename_buf), + &decode_string(&filename), )?; assemble(allocator, &decode_string(&file.data))? } else { return Err(EvalErr(declaration_sexp, "no such embed kind".to_string())); }; - Ok((name_buf.to_vec(), quote(allocator, file_data)?)) + Ok((name, quote(allocator, file_data)?)) } else { Err(EvalErr( declaration_sexp, diff --git a/src/compiler/cldb.rs b/src/compiler/cldb.rs index d8bb942cf..0c7b8e9ab 100644 --- a/src/compiler/cldb.rs +++ b/src/compiler/cldb.rs @@ -563,7 +563,7 @@ fn hex_to_modern_sexp_inner( ))), _ => convert_from_clvm_rs(allocator, srcloc, program).map_err(|_| { EvalErr( - Allocator::null(allocator), + allocator.nil(), "clvm_rs allocator failed".to_string(), ) }), diff --git a/src/compiler/clvm.rs b/src/compiler/clvm.rs index 7246aae96..400748b33 100644 --- a/src/compiler/clvm.rs +++ b/src/compiler/clvm.rs @@ -11,6 +11,7 @@ use sha2::Digest; use sha2::Sha256; use crate::classic::clvm::__type_compatibility__::{bi_one, bi_zero}; +use crate::classic::clvm::casts::By; use crate::classic::clvm_tools::stages::stage_0::TRunProgram; use crate::compiler::prims; @@ -228,7 +229,7 @@ pub fn convert_to_clvm_rs( head: Rc, ) -> Result { match head.borrow() { - SExp::Nil(_) => Ok(allocator.null()), + SExp::Nil(_) => Ok(allocator.nil()), SExp::Atom(_l, x) => allocator .new_atom(x) .map_err(|_e| RunFailure::RunErr(head.loc(), format!("failed to alloc atom {head}"))), @@ -237,7 +238,7 @@ pub fn convert_to_clvm_rs( .map_err(|_e| RunFailure::RunErr(head.loc(), format!("failed to alloc string {head}"))), SExp::Integer(_, i) => { if *i == bi_zero() { - Ok(allocator.null()) + Ok(allocator.nil()) } else { allocator .new_atom(&u8_from_number(i.clone())) @@ -264,14 +265,14 @@ pub fn convert_from_clvm_rs( ) -> Result, RunFailure> { match allocator.sexp(head) { allocator::SExp::Atom => { - let atom_data = allocator.atom(head); - if atom_data.is_empty() { + let atom_data = By::new(allocator, head); + if atom_data.u8().is_empty() { Ok(Rc::new(SExp::Nil(loc))) } else { - let integer = number_from_u8(atom_data); + let integer = number_from_u8(atom_data.u8()); // Ensure that atom values that don't evaluate equal to integers // are represented faithfully as atoms. - if u8_from_number(integer.clone()) == atom_data { + if u8_from_number(integer.clone()) == atom_data.u8() { Ok(Rc::new(SExp::Integer(loc, integer))) } else { Ok(Rc::new(SExp::Atom(loc, atom_data.to_vec()))) diff --git a/src/compiler/debug.rs b/src/compiler/debug.rs index 0ba0f40ea..78a98e77d 100644 --- a/src/compiler/debug.rs +++ b/src/compiler/debug.rs @@ -61,7 +61,8 @@ pub fn build_table_mut( let treehash = sha256( Bytes::new(Some(BytesFromType::Raw(vec![2]))) .concat(&left) - .concat(&right), + .concat(&right) + .data(), ); code_map.entry(treehash.hex()).or_insert_with(|| tx(code)); treehash @@ -69,7 +70,8 @@ pub fn build_table_mut( SExp::Atom(_, a) => { let treehash = sha256( Bytes::new(Some(BytesFromType::Raw(vec![1]))) - .concat(&Bytes::new(Some(BytesFromType::Raw(a.clone())))), + .concat(&Bytes::new(Some(BytesFromType::Raw(a.clone())))) + .data(), ); code_map.insert(treehash.hex(), tx(code)); treehash diff --git a/src/compiler/dialect.rs b/src/compiler/dialect.rs index e3f876e93..857b1cf0f 100644 --- a/src/compiler/dialect.rs +++ b/src/compiler/dialect.rs @@ -2,6 +2,7 @@ use std::collections::HashMap; use clvmr::allocator::{Allocator, NodePtr, SExp}; +use crate::classic::clvm::casts::By; use crate::classic::clvm::sexp::proper_list; use crate::compiler::sexp::decode_string; @@ -92,8 +93,8 @@ fn include_dialect(allocator: &Allocator, e: &[NodePtr]) -> Option(allocator: &'a mut Allocator, src: String) -> NodePtr { let ir = read_ir(&src).unwrap(); let assembled = assemble_from_ir(allocator, Rc::new(ir)).unwrap(); let runner = DefaultProgramRunner::new(); - let null = allocator.null(); + let null = allocator.nil(); let res = runner .run_program(allocator, assembled, null, None) .unwrap(); @@ -253,7 +253,7 @@ fn compile_program<'a>( let runner = run_program_for_search_paths("*test*", &vec![include_path], false); let input_ir = read_ir(&src); let input_program = assemble_from_ir(allocator, Rc::new(input_ir.unwrap())).unwrap(); - let input_sexp = allocator.new_pair(input_program, allocator.null()).unwrap(); + let input_sexp = allocator.new_pair(input_program, allocator.nil()).unwrap(); let res = runner.run_program(allocator, run_script, input_sexp, None); return res.map(|x| disassemble(allocator, x.1, Some(0))); @@ -289,8 +289,7 @@ fn can_run_from_source_nil() { let res = run_from_source(&mut allocator, "()".to_string()); match allocator.sexp(res) { SExp::Atom => { - let res_bytes = allocator.atom(res); - assert_eq!(res_bytes.len(), 0); + assert_eq!(By::new(&allocator, res).u8().len(), 0); } _ => { assert_eq!("expected atom", ""); @@ -304,8 +303,7 @@ fn can_echo_quoted_nil() { let res = run_from_source(&mut allocator, "(1)".to_string()); match allocator.sexp(res) { SExp::Atom => { - let res_bytes = allocator.atom(res); - assert_eq!(res_bytes.len(), 0); + assert_eq!(By::new(&allocator, res).u8().len(), 0); } _ => { assert_eq!("expected atom", ""); @@ -316,7 +314,7 @@ fn can_echo_quoted_nil() { #[test] fn can_echo_quoted() { let mut allocator = Allocator::new(); - let null = allocator.null(); + let null = allocator.nil(); let res = run_from_source(&mut allocator, "(1 ())".to_string()); match allocator.sexp(res) { SExp::Pair(l, r) => { @@ -335,9 +333,9 @@ fn can_echo_quoted_atom() { let res = run_from_source(&mut allocator, "(1 . 3)".to_string()); match allocator.sexp(res) { SExp::Atom => { - let res_bytes = allocator.atom(res); - assert_eq!(res_bytes.len(), 1); - assert_eq!(res_bytes[0], 3); + let res_bytes = By::new(&allocator, res); + assert_eq!(res_bytes.u8().len(), 1); + assert_eq!(res_bytes.u8()[0], 3); } _ => { assert_eq!("expected atom", ""); @@ -351,9 +349,9 @@ fn can_do_operations() { let res = run_from_source(&mut allocator, "(16 (1 . 3) (1 . 5))".to_string()); match allocator.sexp(res) { SExp::Atom => { - let res_bytes = allocator.atom(res); - assert_eq!(res_bytes.len(), 1); - assert_eq!(res_bytes[0], 8); + let res_bytes = By::new(&allocator, res); + assert_eq!(res_bytes.u8().len(), 1); + assert_eq!(res_bytes.u8()[0], 8); } _ => { assert_eq!("expected atom", ""); @@ -367,9 +365,9 @@ fn can_do_operations_kw() { let res = run_from_source(&mut allocator, "(+ (q . 3) (q . 5))".to_string()); match allocator.sexp(res) { SExp::Atom => { - let res_bytes = allocator.atom(res); - assert_eq!(res_bytes.len(), 1); - assert_eq!(res_bytes[0], 8); + let res_bytes = By::new(&allocator, res); + assert_eq!(res_bytes.u8().len(), 1); + assert_eq!(res_bytes.u8()[0], 8); } _ => { assert_eq!("expected atom", ""); From fd399290ee70dd1081021ce18bd4407f7062b811 Mon Sep 17 00:00:00 2001 From: arty Date: Thu, 25 Apr 2024 13:41:57 -0700 Subject: [PATCH 2/5] fmt --- src/classic/clvm/casts.rs | 8 ++++++-- src/classic/clvm/serialize.rs | 2 +- src/classic/clvm_tools/binutils.rs | 2 +- src/classic/clvm_tools/pattern_match.rs | 8 ++------ src/classic/clvm_tools/sha256tree.rs | 13 ++++++++----- src/classic/clvm_tools/stages/stage_2/compile.rs | 7 ++++++- src/classic/clvm_tools/stages/stage_2/operators.rs | 8 +++++--- src/classic/clvm_tools/stages/stage_2/reader.rs | 10 +++++----- src/compiler/cldb.rs | 8 ++------ src/compiler/dialect.rs | 4 +++- 10 files changed, 39 insertions(+), 31 deletions(-) diff --git a/src/classic/clvm/casts.rs b/src/classic/clvm/casts.rs index ac893f26b..5120befff 100644 --- a/src/classic/clvm/casts.rs +++ b/src/classic/clvm/casts.rs @@ -151,10 +151,14 @@ pub fn bigint_to_bytes_clvm(v: &Number) -> Bytes { // return ((v >= 0 ? v : -v).toString(2).length + 7) >> 3; // } -pub struct By<'a> { atom: Atom<'a> } +pub struct By<'a> { + atom: Atom<'a>, +} impl<'a> By<'a> { pub fn new(allocator: &'a Allocator, node: NodePtr) -> Self { - By { atom: allocator.atom(node) } + By { + atom: allocator.atom(node), + } } pub fn u8(&self) -> &[u8] { self.atom.borrow() diff --git a/src/classic/clvm/serialize.rs b/src/classic/clvm/serialize.rs index f17221a27..40af71621 100644 --- a/src/classic/clvm/serialize.rs +++ b/src/classic/clvm/serialize.rs @@ -17,7 +17,7 @@ use std::rc::Rc; use crate::classic::clvm::__type_compatibility__::{Bytes, BytesFromType, Stream}; use crate::classic::clvm::as_rust::{TToSexpF, TValStack}; -use crate::classic::clvm::casts::{By, int_from_bytes}; +use crate::classic::clvm::casts::{int_from_bytes, By}; use crate::classic::clvm::sexp::{to_sexp_type, CastableType}; use clvm_rs::allocator::{Allocator, NodePtr, SExp}; use clvm_rs::reduction::{EvalErr, Reduction, Response}; diff --git a/src/classic/clvm_tools/binutils.rs b/src/classic/clvm_tools/binutils.rs index c28ba985e..94a50c047 100644 --- a/src/classic/clvm_tools/binutils.rs +++ b/src/classic/clvm_tools/binutils.rs @@ -6,9 +6,9 @@ use clvm_rs::allocator::{Allocator, NodePtr, SExp}; use clvm_rs::reduction::EvalErr; use crate::classic::clvm::__type_compatibility__::{Bytes, BytesFromType, Record, Stream}; +use crate::classic::clvm::casts::By; use crate::classic::clvm::OPERATORS_LATEST_VERSION; use crate::classic::clvm::{keyword_from_atom, keyword_to_atom}; -use crate::classic::clvm::casts::By; use crate::classic::clvm_tools::ir::r#type::IRRepr; use crate::classic::clvm_tools::ir::reader::IRReader; use crate::classic::clvm_tools::ir::writer::write_ir; diff --git a/src/classic/clvm_tools/pattern_match.rs b/src/classic/clvm_tools/pattern_match.rs index 0ab6f31cc..b3508cdea 100644 --- a/src/classic/clvm_tools/pattern_match.rs +++ b/src/classic/clvm_tools/pattern_match.rs @@ -82,9 +82,7 @@ pub fn match_sexp( return unify_bindings(allocator, known_bindings, &pright_atom, sexp); } if pleft_atom == SEXP_MATCH { - if pright_atom == SEXP_MATCH - && sexp_atom == SEXP_MATCH - { + if pright_atom == SEXP_MATCH && sexp_atom == SEXP_MATCH { return Some(HashMap::new()); } @@ -101,9 +99,7 @@ pub fn match_sexp( } SExp::Pair(sleft, sright) => { let pleft_atom = By::new(allocator, pleft).to_vec(); - if pleft_atom == SEXP_MATCH - && pright_atom != SEXP_MATCH - { + if pleft_atom == SEXP_MATCH && pright_atom != SEXP_MATCH { return unify_bindings( allocator, known_bindings, diff --git a/src/classic/clvm_tools/sha256tree.rs b/src/classic/clvm_tools/sha256tree.rs index 3e5e353f5..5c6b6a813 100644 --- a/src/classic/clvm_tools/sha256tree.rs +++ b/src/classic/clvm_tools/sha256tree.rs @@ -32,16 +32,19 @@ pub fn sha256tree(allocator: &mut Allocator, v: NodePtr) -> Bytes { sha256( &Bytes::new(Some(BytesFromType::Raw(vec![2]))) .concat(&left) - .concat(&right).data(), + .concat(&right) + .data(), ) } SExp::Atom => { let v_atom = By::new(allocator, v); sha256( - &Bytes::new(Some(BytesFromType::Raw(vec![1]))).concat(&Bytes::new(Some( - // only v in scope. - BytesFromType::Raw(v_atom.to_vec()) - ))).data(), + &Bytes::new(Some(BytesFromType::Raw(vec![1]))) + .concat(&Bytes::new(Some( + // only v in scope. + BytesFromType::Raw(v_atom.to_vec()), + ))) + .data(), ) } } diff --git a/src/classic/clvm_tools/stages/stage_2/compile.rs b/src/classic/clvm_tools/stages/stage_2/compile.rs index 088eb4f1b..6429b0d96 100644 --- a/src/classic/clvm_tools/stages/stage_2/compile.rs +++ b/src/classic/clvm_tools/stages/stage_2/compile.rs @@ -789,7 +789,12 @@ pub fn get_compile_filename( if let SExp::Atom = allocator.sexp(cvt_prog_result) { // only cvt_prog_result in scope. - return Ok(Some(Bytes::new(Some(BytesFromType::Raw(By::new(allocator, cvt_prog_result).to_vec()))).decode())); + return Ok(Some( + Bytes::new(Some(BytesFromType::Raw( + By::new(allocator, cvt_prog_result).to_vec(), + ))) + .decode(), + )); } Err(EvalErr( diff --git a/src/classic/clvm_tools/stages/stage_2/operators.rs b/src/classic/clvm_tools/stages/stage_2/operators.rs index a4b225bb6..2ab6532d3 100644 --- a/src/classic/clvm_tools/stages/stage_2/operators.rs +++ b/src/classic/clvm_tools/stages/stage_2/operators.rs @@ -206,7 +206,8 @@ impl CompilerOperatorsInternal { SExp::Pair(f, _) => match allocator.sexp(f) { SExp::Atom => { let filename = - Bytes::new(Some(BytesFromType::Raw(By::new(allocator, f).to_vec()))).decode(); + Bytes::new(Some(BytesFromType::Raw(By::new(allocator, f).to_vec()))) + .decode(); // Use the read interface in CompilerOpts if we have one. if let Some(opts) = self.get_compiler_opts() { if let Ok((_, content)) = @@ -238,8 +239,9 @@ impl CompilerOperatorsInternal { if let SExp::Pair(filename_sexp, r) = allocator.sexp(sexp) { if let SExp::Pair(data, _) = allocator.sexp(r) { if let SExp::Atom = allocator.sexp(filename_sexp) { - let filename_bytes = - Bytes::new(Some(BytesFromType::Raw(By::new(allocator, filename_sexp).to_vec()))); + let filename_bytes = Bytes::new(Some(BytesFromType::Raw( + By::new(allocator, filename_sexp).to_vec(), + ))); let ir = disassemble_to_ir_with_kw( allocator, data, diff --git a/src/classic/clvm_tools/stages/stage_2/reader.rs b/src/classic/clvm_tools/stages/stage_2/reader.rs index 8b1f21c18..ca072479a 100644 --- a/src/classic/clvm_tools/stages/stage_2/reader.rs +++ b/src/classic/clvm_tools/stages/stage_2/reader.rs @@ -98,11 +98,11 @@ pub fn process_embed_file( ) { // Note: we don't want to keep borrowing here because we // need the mutable borrow below. - let (name, kind, filename) = - { - (By::new(allocator, l[0]).to_vec(), - By::new(allocator, l[1]).to_vec(), - By::new(allocator, l[2]).to_vec() + let (name, kind, filename) = { + ( + By::new(allocator, l[0]).to_vec(), + By::new(allocator, l[1]).to_vec(), + By::new(allocator, l[2]).to_vec(), ) }; let file_data = if &kind == b"bin" { diff --git a/src/compiler/cldb.rs b/src/compiler/cldb.rs index 0c7b8e9ab..0e56ce0c9 100644 --- a/src/compiler/cldb.rs +++ b/src/compiler/cldb.rs @@ -561,12 +561,8 @@ fn hex_to_modern_sexp_inner( hex_to_modern_sexp_inner(allocator, symbol_table, srcloc.clone(), a)?, hex_to_modern_sexp_inner(allocator, symbol_table, srcloc, b)?, ))), - _ => convert_from_clvm_rs(allocator, srcloc, program).map_err(|_| { - EvalErr( - allocator.nil(), - "clvm_rs allocator failed".to_string(), - ) - }), + _ => convert_from_clvm_rs(allocator, srcloc, program) + .map_err(|_| EvalErr(allocator.nil(), "clvm_rs allocator failed".to_string())), } } diff --git a/src/compiler/dialect.rs b/src/compiler/dialect.rs index 857b1cf0f..6079af279 100644 --- a/src/compiler/dialect.rs +++ b/src/compiler/dialect.rs @@ -94,7 +94,9 @@ fn include_dialect(allocator: &Allocator, e: &[NodePtr]) -> Option Date: Thu, 25 Apr 2024 13:49:03 -0700 Subject: [PATCH 3/5] Upgrade python bindings --- src/py/binutils.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/py/binutils.rs b/src/py/binutils.rs index 4ee4a0e64..53bec1724 100644 --- a/src/py/binutils.rs +++ b/src/py/binutils.rs @@ -1,6 +1,7 @@ use std::collections::HashMap; use crate::classic::clvm::__type_compatibility__::{Bytes, BytesFromType, Stream}; +use crate::classic::clvm::casts::By; use crate::classic::clvm::serialize::{sexp_from_stream, SimpleCreateCLVMObject}; use clvm_rs::allocator::{Allocator, NodePtr, SExp}; @@ -54,7 +55,7 @@ fn convert_to_external( if !finished.contains_key(&node) { let converted: PyObject = Python::with_gil(|py| { - let bytes = PyBytes::new(py, allocator.atom(node)); + let bytes = PyBytes::new(py, By::new(allocator, node).u8()); let args = PyTuple::new(py, &[bytes]); from_bytes.call1(args) })? From f228d75714815b91ef0cb9e42de444f712521a91 Mon Sep 17 00:00:00 2001 From: arty Date: Thu, 25 Apr 2024 14:03:11 -0700 Subject: [PATCH 4/5] Upgrade in wasm --- wasm/Cargo.lock | 27 +++++++++++---------------- wasm/Cargo.toml | 2 +- 2 files changed, 12 insertions(+), 17 deletions(-) diff --git a/wasm/Cargo.lock b/wasm/Cargo.lock index 3bcc6037c..8bb87903b 100644 --- a/wasm/Cargo.lock +++ b/wasm/Cargo.lock @@ -150,9 +150,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "chia-bls" -version = "0.3.3" +version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ba003303bda3019f2fe94e5478555a96addff1d6a1ea84bfe207b3f075cd8420" +checksum = "1cbbce0107fa3911890f1a204a147c6d07116687658a6af95690e64fc21ff782" dependencies = [ "anyhow", "arbitrary", @@ -189,16 +189,6 @@ dependencies = [ "syn 2.0.28", ] -[[package]] -name = "clvm-traits" -version = "0.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5548b7de9921c68b2ca1101c08dd416bfa9afdf319f5b45183ecb3574ee177a3" -dependencies = [ - "num-bigint", - "thiserror", -] - [[package]] name = "clvm_tools_rs" version = "0.1.41" @@ -246,13 +236,12 @@ dependencies = [ [[package]] name = "clvmr" -version = "0.3.3" +version = "0.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "deabd4e4163efb64486937a7c80996e5bdfb88d21a6baa9c0f802e6b45906109" +checksum = "deb235bb5ce94e1e64e54cede97ca5a4766590d3584dea1b4285499d8e7f56f6" dependencies = [ "chia-bls", - "clvm-traits", - "hex", + "hex-literal", "k256", "lazy_static", "num-bigint", @@ -507,6 +496,12 @@ version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" +[[package]] +name = "hex-literal" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6fe2267d4ed49bc07b63801559be28c718ea06c4738b7a03c94df7386d2cde46" + [[package]] name = "hkdf" version = "0.12.4" diff --git a/wasm/Cargo.toml b/wasm/Cargo.toml index 6b8516e4f..ef37028e3 100644 --- a/wasm/Cargo.toml +++ b/wasm/Cargo.toml @@ -18,7 +18,7 @@ path = "src/mod.rs" [dependencies] clvm_tools_rs = { path= "..", features = [] } -clvmr = { version = "0.3.0", features = ["pre-eval"] } +clvmr = { version = "0.6.1", features = ["pre-eval"] } wasm-bindgen = "=0.2.83" wasm-bindgen-test = "=0.3.25" js-sys = "0.3.60" From 7d92c2da513db1d1a877be4fbcb496021c4933e1 Mon Sep 17 00:00:00 2001 From: arty Date: Thu, 25 Apr 2024 14:13:25 -0700 Subject: [PATCH 5/5] new clippy traffic --- src/classic/clvm/serialize.rs | 2 +- src/classic/clvm_tools/cmds.rs | 16 ++++++++-------- src/classic/clvm_tools/sha256tree.rs | 4 ++-- .../clvm_tools/stages/stage_2/optimize.rs | 4 ++-- src/classic/platform/argparse.rs | 5 +---- src/compiler/codegen.rs | 6 ++++-- src/compiler/compiler.rs | 2 +- src/compiler/comptypes.rs | 2 +- 8 files changed, 20 insertions(+), 21 deletions(-) diff --git a/src/classic/clvm/serialize.rs b/src/classic/clvm/serialize.rs index 40af71621..a719e729c 100644 --- a/src/classic/clvm/serialize.rs +++ b/src/classic/clvm/serialize.rs @@ -100,7 +100,7 @@ impl<'a> Iterator for SExpToBytesIterator<'a> { SExp::Atom => { // The only node we have in scope is x, so this atom // capture is trivial. - let buf = By::new(&mut self.allocator, x); + let buf = By::new(self.allocator, x); let bytes = Bytes::new(Some(BytesFromType::Raw(buf.to_vec()))); match atom_size_blob(&bytes) { Ok((original, b)) => { diff --git a/src/classic/clvm_tools/cmds.rs b/src/classic/clvm_tools/cmds.rs index 883ce5875..7b81a558b 100644 --- a/src/classic/clvm_tools/cmds.rs +++ b/src/classic/clvm_tools/cmds.rs @@ -584,8 +584,8 @@ pub fn cldb(args: &[String]) { } if let Some(ArgumentValue::ArgString(file, path_or_code)) = parsed_args.get("path_or_code") { - input_file = file.clone(); - input_program = path_or_code.to_string(); + input_file.clone_from(file); + input_program.clone_from(path_or_code); } if let Some(ArgumentValue::ArgString(_, s)) = parsed_args.get("env") { @@ -1117,8 +1117,8 @@ pub fn launch_tool(stdout: &mut Stream, args: &[String], tool_name: &str, defaul let mut input_program = "()".to_string(); if let Some(ArgumentValue::ArgString(file, path_or_code)) = parsed_args.get("path_or_code") { - input_file = file.clone(); - input_program = path_or_code.to_string(); + input_file.clone_from(file); + input_program.clone_from(path_or_code); } let reported_input_file = input_file @@ -1179,8 +1179,8 @@ pub fn launch_tool(stdout: &mut Stream, args: &[String], tool_name: &str, defaul } if let Some(ArgumentValue::ArgString(file, path_or_code)) = parsed_args.get("env") { - input_file = file.clone(); - input_args = path_or_code.to_string(); + input_file.clone_from(file); + input_args.clone_from(path_or_code); } let special_runner = @@ -1244,8 +1244,8 @@ pub fn launch_tool(stdout: &mut Stream, args: &[String], tool_name: &str, defaul if let Some(ArgumentValue::ArgString(f, content)) = parsed_args.get("path_or_code") { match read_ir(content) { Ok(s) => { - input_program = content.clone(); - input_file = f.clone(); + input_program.clone_from(content); + input_file.clone_from(f); src_sexp = s; } Err(e) => { diff --git a/src/classic/clvm_tools/sha256tree.rs b/src/classic/clvm_tools/sha256tree.rs index 5c6b6a813..7301617a6 100644 --- a/src/classic/clvm_tools/sha256tree.rs +++ b/src/classic/clvm_tools/sha256tree.rs @@ -30,7 +30,7 @@ pub fn sha256tree(allocator: &mut Allocator, v: NodePtr) -> Bytes { let left = sha256tree(allocator, l); let right = sha256tree(allocator, r); sha256( - &Bytes::new(Some(BytesFromType::Raw(vec![2]))) + Bytes::new(Some(BytesFromType::Raw(vec![2]))) .concat(&left) .concat(&right) .data(), @@ -39,7 +39,7 @@ pub fn sha256tree(allocator: &mut Allocator, v: NodePtr) -> Bytes { SExp::Atom => { let v_atom = By::new(allocator, v); sha256( - &Bytes::new(Some(BytesFromType::Raw(vec![1]))) + Bytes::new(Some(BytesFromType::Raw(vec![1]))) .concat(&Bytes::new(Some( // only v in scope. BytesFromType::Raw(v_atom.to_vec()), diff --git a/src/classic/clvm_tools/stages/stage_2/optimize.rs b/src/classic/clvm_tools/stages/stage_2/optimize.rs index 591b889d1..4f9d938d9 100644 --- a/src/classic/clvm_tools/stages/stage_2/optimize.rs +++ b/src/classic/clvm_tools/stages/stage_2/optimize.rs @@ -423,7 +423,7 @@ pub fn children_optimizer( } if let SExp::Atom = allocator.sexp(list[0]) { let list0_atom = By::new(allocator, list[0]); - if list0_atom.u8() == &[1] { + if list0_atom.u8() == [1] { return Ok(r); } } @@ -691,7 +691,7 @@ pub fn optimize_sexp_( } SExp::Pair(_, _) => { for opt in optimizers.iter() { - name = opt.name.clone(); + name.clone_from(&opt.name); match opt.invoke(allocator, memo, r, eval_f.clone()) { Err(e) => { return Err(e); diff --git a/src/classic/platform/argparse.rs b/src/classic/platform/argparse.rs index 916d1343f..eea96c176 100644 --- a/src/classic/platform/argparse.rs +++ b/src/classic/platform/argparse.rs @@ -491,10 +491,7 @@ impl ArgumentParser { } let name = &self.optional_args[k].names[index as usize]; - let index2 = match arg.find(name) { - Some(i) => i, - _ => 0, - } + name.len(); + let index2 = arg.find(name).unwrap_or_default() + name.len(); let value = &arg[index2..].to_string(); norm.push(name.to_string()); diff --git a/src/compiler/codegen.rs b/src/compiler/codegen.rs index af3fcb4c9..08d695827 100644 --- a/src/compiler/codegen.rs +++ b/src/compiler/codegen.rs @@ -1520,7 +1520,7 @@ fn start_codegen( )), }; - code_generator.to_process = program.helpers.clone(); + code_generator.to_process.clone_from(&program.helpers); // Ensure that we have the synthesis of the previous codegen's helpers and // The ones provided with the new form if any. let mut combined_helpers_for_codegen = program.helpers.clone(); @@ -1716,7 +1716,9 @@ pub fn codegen( // If stepping 23 or greater, we support no-env mode. enable_nil_env_mode_for_stepping_23_or_greater(opts.clone(), &mut code_generator); - *context.symbols() = code_generator.function_symbols.clone(); + context + .symbols() + .clone_from(&code_generator.function_symbols); context .symbols() .insert("source_file".to_string(), opts.filename()); diff --git a/src/compiler/compiler.rs b/src/compiler/compiler.rs index e02c2f5cd..92b27ca5d 100644 --- a/src/compiler/compiler.rs +++ b/src/compiler/compiler.rs @@ -216,7 +216,7 @@ impl CompilerOpts for DefaultCompilerOpts { } fn set_search_paths(&self, dirs: &[String]) -> Rc { let mut copy = self.clone(); - copy.include_dirs = dirs.to_owned(); + dirs.clone_into(&mut copy.include_dirs); Rc::new(copy) } fn set_disassembly_ver(&self, ver: Option) -> Rc { diff --git a/src/compiler/comptypes.rs b/src/compiler/comptypes.rs index 1f446a05f..ed25ba2ff 100644 --- a/src/compiler/comptypes.rs +++ b/src/compiler/comptypes.rs @@ -1210,7 +1210,7 @@ pub fn join_vecs_to_string(sep: Vec, vecs: &[Vec]) -> String { s.append(&mut comma.clone()); s.append(&mut elt.to_vec()); if comma.is_empty() { - comma = sep.clone(); + comma.clone_from(&sep); } }