Skip to content

Commit

Permalink
macro cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
raychu86 committed Mar 27, 2024
1 parent 25e03ff commit 0f47547
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 32 deletions.
34 changes: 14 additions & 20 deletions synthesizer/process/src/finalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
use super::*;
use console::program::{FinalizeType, Future, Register};
use synthesizer_program::{Await, FinalizeRegistersState, Operand};
use utilities::handle_halting;
use utilities::try_vm_runtime;

use std::{collections::HashSet, panic::AssertUnwindSafe};
use std::collections::HashSet;

impl<N: Network> Process<N> {
/// Finalizes the deployment and fee.
Expand Down Expand Up @@ -227,9 +227,7 @@ fn finalize_transition<N: Network, P: FinalizeStorage<N>>(
// Finalize the command.
match &command {
Command::BranchEq(branch_eq) => {
let result = handle_halting!(AssertUnwindSafe(|| {
branch_to(counter, branch_eq, finalize, stack, &registers)
}));
let result = try_vm_runtime!(|| branch_to(counter, branch_eq, finalize, stack, &registers));
match result {
Ok(Ok(new_counter)) => {
counter = new_counter;
Expand All @@ -241,9 +239,7 @@ fn finalize_transition<N: Network, P: FinalizeStorage<N>>(
}
}
Command::BranchNeq(branch_neq) => {
let result = handle_halting!(AssertUnwindSafe(|| {
branch_to(counter, branch_neq, finalize, stack, &registers)
}));
let result = try_vm_runtime!(|| branch_to(counter, branch_neq, finalize, stack, &registers));
match result {
Ok(Ok(new_counter)) => {
counter = new_counter;
Expand Down Expand Up @@ -277,16 +273,15 @@ fn finalize_transition<N: Network, P: FinalizeStorage<N>>(
None => bail!("Transition ID '{transition_id}' not found in call graph"),
};

let callee_state = match handle_halting!(AssertUnwindSafe(|| {
// Set up the finalize state for the await.
setup_await(state, await_, stack, &registers, child_transition_id)
})) {
Ok(Ok(callee_state)) => callee_state,
// If the evaluation fails, bail and return the error.
Ok(Err(error)) => bail!("'finalize' failed to evaluate command ({command}): {error}"),
// If the evaluation fails, bail and return the error.
Err(_) => bail!("'finalize' failed to evaluate command ({command})"),
};
// Set up the finalize state for the await.
let callee_state =
match try_vm_runtime!(|| setup_await(state, await_, stack, &registers, child_transition_id)) {
Ok(Ok(callee_state)) => callee_state,
// If the evaluation fails, bail and return the error.
Ok(Err(error)) => bail!("'finalize' failed to evaluate command ({command}): {error}"),
// If the evaluation fails, bail and return the error.
Err(_) => bail!("'finalize' failed to evaluate command ({command})"),
};

// Increment the call counter.
call_counter += 1;
Expand All @@ -306,8 +301,7 @@ fn finalize_transition<N: Network, P: FinalizeStorage<N>>(
continue 'outer;
}
_ => {
let result =
handle_halting!(AssertUnwindSafe(|| { command.finalize(stack, store, &mut registers) }));
let result = try_vm_runtime!(|| command.finalize(stack, store, &mut registers));
match result {
// If the evaluation succeeds with an operation, add it to the list.
Ok(Ok(Some(finalize_operation))) => finalize_operations.push(finalize_operation),
Expand Down
4 changes: 2 additions & 2 deletions synthesizer/src/vm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ use ledger_store::{
};
use synthesizer_process::{deployment_cost, execution_cost, Authorization, Process, Trace};
use synthesizer_program::{FinalizeGlobalState, FinalizeOperation, FinalizeStoreTrait, Program};
use utilities::handle_halting;
use utilities::try_vm_runtime;

use aleo_std::prelude::{finish, lap, timer};
use indexmap::{IndexMap, IndexSet};
use itertools::Either;
use lru::LruCache;
use parking_lot::{Mutex, RwLock};
use rand::{rngs::StdRng, SeedableRng};
use std::{collections::HashSet, num::NonZeroUsize, panic::AssertUnwindSafe, sync::Arc};
use std::{collections::HashSet, num::NonZeroUsize, sync::Arc};

#[cfg(not(feature = "serial"))]
use rayon::prelude::*;
Expand Down
6 changes: 2 additions & 4 deletions synthesizer/src/vm/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ impl<N: Network, C: ConsensusStorage<N>> VM<N, C> {
// Verify the deployment if it has not been verified before.
if !is_partially_verified {
// Verify the deployment.
match handle_halting!(AssertUnwindSafe(|| { self.check_deployment_internal(deployment, rng) })) {
match try_vm_runtime!(|| self.check_deployment_internal(deployment, rng)) {
Ok(result) => result?,
Err(_) => bail!("VM safely halted transaction '{id}' during verification"),
}
Expand All @@ -170,9 +170,7 @@ impl<N: Network, C: ConsensusStorage<N>> VM<N, C> {
bail!("Transaction '{id}' contains a previously rejected execution")
}
// Verify the execution.
match handle_halting!(AssertUnwindSafe(|| {
self.check_execution_internal(execution, is_partially_verified)
})) {
match try_vm_runtime!(|| self.check_execution_internal(execution, is_partially_verified)) {
Ok(result) => result?,
Err(_) => bail!("VM safely halted transaction '{id}' during verification"),
}
Expand Down
12 changes: 6 additions & 6 deletions utilities/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ impl Error for crate::String {}
#[cfg(not(feature = "std"))]
impl Error for crate::io::Error {}

/// This purpose of this macro is to catch the instances of halting
/// without producing logs looking like unexpected panics. It prints
/// to stderr using the format: "Halted at <location>: <halt message>".
/// This macro provides a VM runtime environment which will safely halt
/// without producing logs that look like unexpected behavior.
/// It prints to stderr using the format: "VM safely halted at <location>: <halt message>".
#[macro_export]
macro_rules! handle_halting {
macro_rules! try_vm_runtime {
($e:expr) => {{
use std::panic;

Expand All @@ -52,12 +52,12 @@ macro_rules! handle_halting {
let msg = e.to_string();
let msg = msg.split_ascii_whitespace().skip_while(|&word| word != "panicked").collect::<Vec<&str>>();
let mut msg = msg.join(" ");
msg = msg.replacen("panicked", "Halted", 1);
msg = msg.replacen("panicked", "VM safely halted", 1);
eprintln!("{msg}");
}));

// Perform the operation that may panic.
let result = panic::catch_unwind($e);
let result = panic::catch_unwind(panic::AssertUnwindSafe($e));

// Restore the standard panic hook.
let _ = panic::take_hook();
Expand Down

0 comments on commit 0f47547

Please sign in to comment.