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

Support backref for wasm #395

Merged
merged 12 commits into from
Apr 25, 2024
3 changes: 3 additions & 0 deletions src/flags.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// when this flag is set, the block generator serialization is allowed to
// contain back-references
pub const ALLOW_BACKREFS: u32 = 0x2000000;
arvidn marked this conversation as resolved.
Show resolved Hide resolved
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub mod cost;
pub mod dialect;
pub mod err_utils;
pub mod f_table;
pub mod flags;
pub mod more_ops;
pub mod number;
pub mod op_utils;
Expand All @@ -25,6 +26,8 @@ pub use chia_dialect::{
ENABLE_BLS_OPS_OUTSIDE_GUARD, ENABLE_FIXED_DIV, LIMIT_HEAP, MEMPOOL_MODE, NO_UNKNOWN_OPS,
};

pub use flags::ALLOW_BACKREFS;

#[cfg(feature = "counters")]
pub use run_program::run_program_with_counters;

Expand Down
15 changes: 15 additions & 0 deletions wasm/src/lazy_node.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use clvmr::allocator::{Allocator, NodePtr, SExp};
use std::rc::Rc;

use clvmr::serde::{node_to_bytes, node_to_bytes_backrefs};
use clvmr::ALLOW_BACKREFS;
use js_sys::Array;
use wasm_bindgen::prelude::*;

Expand Down Expand Up @@ -35,6 +37,19 @@ impl LazyNode {
_ => None,
}
}

#[wasm_bindgen]
pub fn to_bytes(&self, flag: u32) -> Option<Vec<u8>> {
let serializer = if (flag & ALLOW_BACKREFS) != 0 {
node_to_bytes_backrefs
} else {
node_to_bytes
arvidn marked this conversation as resolved.
Show resolved Hide resolved
};
match serializer(&self.allocator, self.node) {
Ok(b) => Some(b),
Err(_) => None,
}
ChiaMineJP marked this conversation as resolved.
Show resolved Hide resolved
}
}

impl LazyNode {
Expand Down
3 changes: 2 additions & 1 deletion wasm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod api;
pub mod lazy_node;
pub mod run_program;
pub mod serialize;

#[cfg(test)]
pub mod tests;
41 changes: 22 additions & 19 deletions wasm/src/api.rs → wasm/src/run_program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ use clvmr::chia_dialect::ChiaDialect;
use clvmr::chia_dialect::NO_UNKNOWN_OPS as _no_unknown_ops;
use clvmr::cost::Cost;
use clvmr::run_program::run_program;
use clvmr::serde::{node_from_bytes, node_to_bytes, serialized_length_from_bytes};
use clvmr::serde::{node_from_bytes, node_from_bytes_backrefs, node_to_bytes};
use clvmr::ALLOW_BACKREFS;

// When the `wee_alloc` feature is enabled, use `wee_alloc` as the global
// allocator.
Expand All @@ -25,31 +26,28 @@ impl Flag {
pub fn no_unknown_ops() -> u32 {
_no_unknown_ops
}
}

#[wasm_bindgen]
pub fn serialized_length(program: &[u8]) -> Result<u64, String> {
match serialized_length_from_bytes(program) {
Ok(length) => Ok(length),
Err(err) => Err(err.to_string()),
#[wasm_bindgen]
pub fn allow_backrefs() -> u32 {
ALLOW_BACKREFS
}
}

#[wasm_bindgen]
pub fn run_clvm(program: &[u8], args: &[u8]) -> Vec<u8> {
pub fn run_clvm(program: &[u8], args: &[u8], flag: u32) -> Vec<u8> {
let max_cost: Cost = 1_000_000_000_000_000;

let mut allocator = Allocator::new();
let program = node_from_bytes(&mut allocator, program).unwrap();
let args = node_from_bytes(&mut allocator, args).unwrap();
let deserializer = if (flag & ALLOW_BACKREFS) != 0 {
node_from_bytes_backrefs
} else {
node_from_bytes
};
let program = deserializer(&mut allocator, program).unwrap();
let args = deserializer(&mut allocator, args).unwrap();
let dialect = ChiaDialect::new(flag);

let r = run_program(
&mut allocator,
&ChiaDialect::new(0),
program,
args,
max_cost,
);
let r = run_program(&mut allocator, &dialect, program, args, max_cost);
match r {
Ok(reduction) => node_to_bytes(&allocator, reduction.1).unwrap(),
Err(_eval_err) => format!("{:?}", _eval_err).into(),
Expand All @@ -64,8 +62,13 @@ pub fn run_chia_program(
flag: u32,
) -> Result<Array, String> {
let mut allocator = Allocator::new();
let program = node_from_bytes(&mut allocator, program).unwrap();
let args = node_from_bytes(&mut allocator, args).unwrap();
let deserializer = if (flag & ALLOW_BACKREFS) != 0 {
node_from_bytes_backrefs
} else {
node_from_bytes
};
let program = deserializer(&mut allocator, program).unwrap();
let args = deserializer(&mut allocator, args).unwrap();
let dialect = ChiaDialect::new(flag);

let r = run_program(&mut allocator, &dialect, program, args, max_cost);
Expand Down
27 changes: 27 additions & 0 deletions wasm/src/serialize.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use crate::lazy_node::LazyNode;
use clvmr::serde::{node_from_bytes, node_from_bytes_backrefs, serialized_length_from_bytes};
use clvmr::{Allocator, ALLOW_BACKREFS};
use std::rc::Rc;
use wasm_bindgen::prelude::wasm_bindgen;

#[wasm_bindgen]
pub fn serialized_length(program: &[u8]) -> Result<u64, String> {
match serialized_length_from_bytes(program) {
Ok(length) => Ok(length),
Err(err) => Err(err.to_string()),
}
arvidn marked this conversation as resolved.
Show resolved Hide resolved
}

#[wasm_bindgen]
pub fn sexp_from_bytes(b: &[u8], flag: u32) -> Result<LazyNode, String> {
arvidn marked this conversation as resolved.
Show resolved Hide resolved
let mut allocator = Allocator::new();
let deserializer = if (flag & ALLOW_BACKREFS) != 0 {
node_from_bytes_backrefs
} else {
node_from_bytes
};
match deserializer(&mut allocator, b) {
Ok(node) => Ok(LazyNode::new(Rc::new(allocator), node)),
Err(err) => Err(err.to_string()),
}
arvidn marked this conversation as resolved.
Show resolved Hide resolved
}
Loading