Skip to content
Open
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
38 changes: 26 additions & 12 deletions tools/src/bin/run-clvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@ struct Args {

/// Arguments to pass to the program as a list. Integers are created as
/// number atoms, other strings are used as raw byte atoms.
#[arg(long, num_args = 1..)]
#[arg(long, num_args = 1.., conflicts_with = "envfile")]
env: Vec<String>,

/// Path to file containing hex-encoded CLVM environment
#[arg(long, conflicts_with = "env")]
envfile: Option<String>,

/// CLVM dialect flags to enable
#[arg(long, num_args = 1..)]
flags: Vec<String>,
Expand All @@ -33,15 +37,23 @@ pub fn main() {
let mut a = Allocator::new();
let program = node_from_bytes_backrefs(&mut a, &program_bytes).expect("invalid CLVM");

let mut env = NodePtr::NIL;
for val in args.env.into_iter().rev() {
let atom = if let Ok(num) = val.parse::<i64>() {
a.new_number(num.into()).expect("new_number")
} else {
a.new_atom(val.as_bytes()).expect("new_atom")
};
env = a.new_pair(atom, env).expect("new_pair");
}
let env = if let Some(envfile) = &args.envfile {
let env_hex = std::fs::read_to_string(envfile)
.unwrap_or_else(|e| panic!("failed to read {envfile}: {e}"));
let env_bytes = hex::decode(env_hex.trim()).expect("invalid hex in envfile");
node_from_bytes_backrefs(&mut a, &env_bytes).expect("invalid CLVM in envfile")
} else {
let mut env = NodePtr::NIL;
for val in args.env.into_iter().rev() {
let atom = if let Ok(num) = val.parse::<i64>() {
a.new_number(num.into()).expect("new_number")
} else {
a.new_atom(val.as_bytes()).expect("new_atom")
};
env = a.new_pair(atom, env).expect("new_pair");
}
env
};

let mut flags = ClvmFlags::empty();
for f in &args.flags {
Expand All @@ -66,13 +78,15 @@ pub fn main() {
match result {
Ok(Reduction(cost, _result)) => {
println!("cost: {cost}");
println!("execution time: {duration:.3?}");
let ns_per_cost = duration.as_nanos() as f64 / cost as f64;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Division by zero when cost is zero

Low Severity

The ns_per_cost computation divides by cost as f64, which produces inf or NaN when cost is zero. While Rust's f64 division by zero won't panic, the printed output (ns/cost: inf or ns/cost: NaN) would be confusing. A guard checking for zero cost before computing and printing this metric would make the tool's output cleaner.

Fix in Cursor Fix in Web

println!("ns/cost: {ns_per_cost:.3}");
}
Err(e) => {
println!("execution FAILED: {e:?}");
println!("execution time: {duration:.3?}");
}
}

println!("execution time: {duration:.3?}");
println!("atom_count: {}", counters.atom_count);
println!("pair_count: {}", counters.pair_count);
println!("heap_size: {}", counters.heap_size);
Expand Down
Loading