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

Fix js interface error identified by fcoleman #91

Merged
merged 8 commits into from
May 21, 2024
Merged
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
45 changes: 44 additions & 1 deletion src/compiler/clvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,11 @@ pub fn convert_from_clvm_rs(
// Ensure that atom values that don't evaluate equal to integers
// are represented faithfully as atoms.
if u8_from_number(integer.clone()) == atom_data {
Ok(Rc::new(SExp::Integer(loc, integer)))
if atom_data == [0] {
Ok(Rc::new(SExp::QuotedString(loc, b'x', atom_data.to_vec())))
} else {
Ok(Rc::new(SExp::Integer(loc, integer)))
}
} else {
Ok(Rc::new(SExp::Atom(loc, atom_data.to_vec())))
}
Expand All @@ -287,6 +291,45 @@ pub fn convert_from_clvm_rs(
}
}

#[test]
fn test_convert_from_clvm_rs_00_byte() {
let mut allocator = Allocator::new();
let allocator_atom = allocator.new_atom(&[0]).expect("should convert");
let srcloc = Srcloc::start("*test*");
let result = convert_from_clvm_rs(&mut allocator, srcloc.clone(), allocator_atom)
.expect("should convert to mod");
assert_eq!(result, Rc::new(SExp::Atom(srcloc, vec![0])));
assert_eq!(
sha256tree(result.clone()),
&[
0x47, 0xdc, 0x54, 0x0c, 0x94, 0xce, 0xb7, 0x04, 0xa2, 0x38, 0x75, 0xc1, 0x12, 0x73,
0xe1, 0x6b, 0xb0, 0xb8, 0xa8, 0x7a, 0xed, 0x84, 0xde, 0x91, 0x1f, 0x21, 0x33, 0x56,
0x81, 0x15, 0xf2, 0x54
]
);

let node = convert_to_clvm_rs(&mut allocator, result).expect("should convert from mod");
assert_eq!(allocator.atom(node), &[0]);
}

#[test]
fn test_convert_to_clvm_rs_m129() {
let mut allocator = Allocator::new();
let srcloc = Srcloc::start("*test*");
let result = Rc::new(SExp::Integer(srcloc, -129_i32.to_bigint().unwrap()));
assert_eq!(
sha256tree(result.clone()),
&[
0x5a, 0x0c, 0x1f, 0xec, 0x64, 0x75, 0x1e, 0x82, 0xc0, 0xd4, 0x86, 0x1d, 0x0b, 0xc1,
0x9c, 0x75, 0x80, 0x52, 0x5d, 0x2f, 0x47, 0x66, 0x79, 0x56, 0xbb, 0xd9, 0xd7, 0x9e,
0x26, 0x0a, 0xae, 0x00
]
);

let node = convert_to_clvm_rs(&mut allocator, result).expect("should convert from mod");
assert_eq!(allocator.atom(node), &[255, 127]);
}

fn generate_argument_refs(start: Number, sexp: Rc<SExp>) -> Rc<SExp> {
match sexp.borrow() {
SExp::Cons(l, a, b) => {
Expand Down
8 changes: 6 additions & 2 deletions wasm/src/jsval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::convert::TryFrom;
use std::rc::Rc;
use std::str::FromStr;

use clvm_tools_rs::classic::clvm::__type_compatibility__::{Bytes, BytesFromType};
use clvm_tools_rs::classic::clvm::__type_compatibility__::{bi_zero, Bytes, BytesFromType};
use clvm_tools_rs::compiler::sexp::SExp;
use clvm_tools_rs::compiler::srcloc::{Srcloc, Until};
use clvm_tools_rs::util::Number;
Expand Down Expand Up @@ -168,7 +168,11 @@ pub fn sexp_from_js_object(sstart: Srcloc, v: &JsValue) -> Option<Rc<SExp>> {
} else if let Some(fval) = v.as_f64() {
(fval as i64)
.to_bigint()
.map(|x| Rc::new(SExp::Integer(sstart.clone(), x)))
.map(|x| if x == bi_zero() {
Rc::new(SExp::Nil(sstart.clone()))
} else {
Rc::new(SExp::Integer(sstart.clone(), x))
})
} else if let Some(g1_bytes) = detect_serializable(&sstart, v) {
Some(g1_bytes)
} else if let Ok(converted) = detect_convertible(v) {
Expand Down
9 changes: 3 additions & 6 deletions wasm/src/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use clvm_tools_rs::classic::clvm::serialize::{
sexp_from_stream, sexp_to_stream, SimpleCreateCLVMObject,
};
use clvm_tools_rs::classic::clvm_tools::stages::stage_0::{DefaultProgramRunner, TRunProgram};
use clvm_tools_rs::compiler::clvm::{convert_from_clvm_rs, convert_to_clvm_rs, sha256tree, truthy};
use clvm_tools_rs::compiler::clvm::{convert_from_clvm_rs, convert_to_clvm_rs, sha256tree};
use clvm_tools_rs::compiler::prims::{primapply, primcons, primquote};
use clvm_tools_rs::compiler::sexp::SExp;
use clvm_tools_rs::compiler::srcloc::Srcloc;
Expand Down Expand Up @@ -246,9 +246,6 @@ thread_local! {
}

fn create_cached_sexp(id: i32, sexp: Rc<SExp>) -> Result<String, JsValue> {
if !truthy(sexp.clone()) {
return Ok("80".to_string());
}
OBJECT_CACHE.with(|ocache| {
let mut mut_object_cache_ref: RefMut<ObjectCache> = ocache.borrow_mut();
mut_object_cache_ref.create_entry_from_sexp(id, sexp)
Expand Down Expand Up @@ -495,8 +492,8 @@ impl Program {
#[wasm_bindgen]
pub fn from_hex(input: &str) -> Result<IProgram, JsValue> {
let new_id = get_next_id();
let obj = finish_new_object(new_id, input)?;
Program::to_internal(&obj).map(to_iprogram)
let _ = find_cached_sexp(new_id, input)?;
finish_new_object(new_id, input).map(to_iprogram)
}

#[wasm_bindgen]
Expand Down
3 changes: 3 additions & 0 deletions wasm/tests/clvm-tools-interface/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@
"file-loader": "^6.2.0",
"html-webpack-plugin": "^5.5.3",
"jest": "^29.6.2",
"jssha": "^3.3.1",
"mini-css-extract-plugin": "^2.7.6",
"mersenne-twister": "1.1.0",
"sha2": "^1.0.2",
"style-loader": "^3.3.2",
"terser-webpack-plugin": "^5.3.9",
"typescript": "^4.9.5",
Expand Down
19 changes: 19 additions & 0 deletions wasm/tests/clvm-tools-interface/src/lib/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,3 +228,22 @@ it('can uncurry an example program', async () => {
assert.equal(uncurried[1][1].toString(), 'a06d95dae356e32a71db5ddcb42224754a02524c615c5fc35f568c2af04774e589');
assert.equal(uncurried[1][2].toString(), cat2_curried_program);
});

// Thanks: fcoleman
describe('Program', () => {
it('preserves the representation across js and hex representations', () => {
const puzzle1 = Program.to([0]);
const puzzleHash1 = Buffer.from(puzzle1.sha256tree()).toString('hex');

const puzzle2 = Program.from_hex(puzzle1.toString());
const puzzleHash2 = Buffer.from(puzzle2.sha256tree()).toString('hex');

const puzzle3 = Program.from_hex('ff8080');
const puzzleHash3 = Buffer.from(puzzle3.sha256tree()).toString('hex');

expect(puzzle1.toString()).toEqual(puzzle2.toString());
expect(puzzle1.toString()).toEqual(puzzle3.toString());
expect(puzzleHash1).toEqual(puzzleHash2);
expect(puzzleHash1).toEqual(puzzleHash3);
});
})
Loading
Loading