Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade clvm_rs and make capture of atom bufs more brief #86

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 11 additions & 16 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions src/classic/clvm/__type_compatibility__.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8> = hashed_iter.collect();
Bytes::new(Some(BytesFromType::Raw(newvec)))
Expand Down
27 changes: 26 additions & 1 deletion src/classic/clvm/casts.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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(),
));
Expand Down Expand Up @@ -148,3 +150,26 @@ 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<u8> {
let borrowed: &[u8] = self.atom.borrow();
borrowed.to_vec()
}
}
impl<'a> Borrow<[u8]> for By<'a> {
fn borrow(&self) -> &[u8] {
self.u8()
}
}
20 changes: 10 additions & 10 deletions src/classic/clvm/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{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};
Expand Down Expand Up @@ -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(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)
}
Expand Down Expand Up @@ -184,7 +184,7 @@ impl OpStackEntry for OpReadSexp {
) -> Option<EvalErr> {
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);
Expand Down Expand Up @@ -236,7 +236,7 @@ pub fn sexp_from_stream<'a>(
}

Err(EvalErr(
allocator.null(),
allocator.nil(),
"No value left after conversion".to_string(),
))
}
Expand All @@ -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]);
}
Expand All @@ -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());
})
Expand Down
27 changes: 14 additions & 13 deletions src/classic/clvm/sexp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -43,7 +44,7 @@ pub fn to_sexp_type(allocator: &mut Allocator, value: CastableType) -> Result<No

let top = match stack.pop() {
None => {
return Err(EvalErr(allocator.null(), "empty value stack".to_string()));
return Err(EvalErr(allocator.nil(), "empty value stack".to_string()));
}
Some(rc) => rc,
};
Expand All @@ -57,7 +58,7 @@ pub fn to_sexp_type(allocator: &mut Allocator, value: CastableType) -> Result<No
}
CastableType::TupleOf(left, right) => {
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)));
}
Expand All @@ -75,7 +76,7 @@ pub fn to_sexp_type(allocator: &mut Allocator, value: CastableType) -> Result<No
}
CastableType::ListOf(_sel, v) => {
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));
Expand Down Expand Up @@ -158,14 +159,14 @@ pub fn to_sexp_type(allocator: &mut Allocator, value: CastableType) -> Result<No
},
_ => {
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:?}"),
));
}
Expand All @@ -183,14 +184,14 @@ pub fn to_sexp_type(allocator: &mut Allocator, value: CastableType) -> Result<No
},
_ => {
return Err(EvalErr(
allocator.null(),
allocator.nil(),
format!("unrealized pair prepended {:?}", stack[target]),
));
}
},
_ => {
return Err(EvalErr(
allocator.null(),
allocator.nil(),
format!("unrealized prepend {top:?}"),
));
}
Expand All @@ -200,17 +201,17 @@ pub fn to_sexp_type(allocator: &mut Allocator, value: CastableType) -> Result<No

if stack.len() != 1 {
return Err(EvalErr(
allocator.null(),
allocator.nil(),
format!("too many values left on op stack {stack:?}"),
));
}

return match stack.pop() {
None => 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]),
)),
},
Expand All @@ -227,7 +228,7 @@ pub fn bool_sexp(allocator: &Allocator, b: bool) -> NodePtr {
if b {
allocator.one()
} else {
allocator.null()
allocator.nil()
}
}

Expand Down Expand Up @@ -355,7 +356,7 @@ pub fn rest(allocator: &Allocator, sexp: NodePtr) -> Result<NodePtr, EvalErr> {

pub fn atom(allocator: &Allocator, sexp: NodePtr) -> Result<Vec<u8>, 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())),
}
}
Expand Down Expand Up @@ -383,7 +384,7 @@ pub fn proper_list(allocator: &Allocator, sexp: NodePtr, store: bool) -> Option<
}

pub fn enlist(allocator: &mut Allocator, vec: &[NodePtr]) -> Result<NodePtr, EvalErr> {
let mut built = allocator.null();
let mut built = allocator.nil();

for i_reverse in 0..vec.len() {
let i = vec.len() - i_reverse - 1;
Expand Down
7 changes: 4 additions & 3 deletions src/classic/clvm_tools/binutils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ 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_tools::ir::r#type::IRRepr;
Expand All @@ -25,7 +26,7 @@ pub fn assemble_from_ir(
ir_sexp: Rc<IRRepr>,
) -> Result<NodePtr, EvalErr> {
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()),
Expand Down Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -157,6 +158,6 @@ pub fn assemble(allocator: &mut Allocator, s: &str) -> Result<NodePtr, EvalErr>
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)))
}
4 changes: 2 additions & 2 deletions src/classic/clvm_tools/clvmc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ pub fn compile_clvm_text_maybe_opt(
input_path: &str,
classic_with_opts: bool,
) -> Result<NodePtr, CompileError> {
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);
Expand Down Expand Up @@ -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));
Expand Down
Loading
Loading