Skip to content

Commit

Permalink
Add skip_tx hint and test
Browse files Browse the repository at this point in the history
  • Loading branch information
notlesh committed Feb 6, 2024
1 parent a3e1eb3 commit 4576f99
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 5 deletions.
22 changes: 20 additions & 2 deletions src/hints/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type HintImpl = fn(
&HashMap<String, Felt252>,
) -> Result<(), HintError>;

static HINTS: [(&str, HintImpl); 53] = [
static HINTS: [(&str, HintImpl); 54] = [
// (BREAKPOINT, breakpoint),
(STARKNET_OS_INPUT, starknet_os_input),
(INITIALIZE_STATE_CHANGES, initialize_state_changes),
Expand Down Expand Up @@ -93,6 +93,7 @@ static HINTS: [(&str, HintImpl); 53] = [
(IS_ON_CURVE, is_on_curve),
(IS_N_GE_TWO, is_n_ge_two),
(START_TX, start_tx),
(SKIP_TX, skip_tx),
];

/// Hint Extensions extend the current map of hints used by the VM.
Expand Down Expand Up @@ -411,8 +412,25 @@ pub fn start_tx(
let deprecated_tx_info_ptr =
get_relocatable_from_var_name(vars::ids::DEPRECATED_TX_INFO, vm, ids_data, ap_tracking)?;

let execution_helper = exec_scopes.get::<ExecutionHelperWrapper>("execution_helper").unwrap();
let execution_helper = exec_scopes.get::<ExecutionHelperWrapper>(vars::ids::EXECUTION_HELPER)
.unwrap();
execution_helper.start_tx(Some(deprecated_tx_info_ptr));

Ok(())
}

const SKIP_TX: &str = "execution_helper.skip_tx()";
pub fn skip_tx(
vm: &mut VirtualMachine,
exec_scopes: &mut ExecutionScopes,
ids_data: &HashMap<String, HintReference>,
ap_tracking: &ApTracking,
_constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
let execution_helper = exec_scopes.get::<ExecutionHelperWrapper>(vars::ids::EXECUTION_HELPER)
.unwrap();
execution_helper.skip_tx();

Ok(())
}

35 changes: 35 additions & 0 deletions src/hints/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,4 +191,39 @@ pub(crate) mod tests {
// after starting tx, tx_execution_info should be some
assert!(exec_helper_box.execution_helper.borrow().tx_execution_info.is_some());
}

#[rstest]
fn test_skip_tx(
block_context: BlockContext,
transaction_execution_info: TransactionExecutionInfo,
) {
let mut vm = VirtualMachine::new(false);
vm.set_fp(1);
vm.add_memory_segment();
vm.add_memory_segment();

let ids_data = ids_data![vars::ids::DEPRECATED_TX_INFO];
let ap_tracking = ApTracking::default();

let mut exec_scopes = ExecutionScopes::new();

// skipping a tx is the same as starting and immediately ending it, so we need one
// execution info to chew through
let execution_infos = vec![transaction_execution_info];
let exec_helper = ExecutionHelperWrapper::new(execution_infos, &block_context);
let exec_helper_box = Box::new(exec_helper);
exec_scopes.insert_box(vars::ids::EXECUTION_HELPER, exec_helper_box.clone());

// before starting tx, tx_execution_info should be none and iter should have a next()
assert!(exec_helper_box.execution_helper.borrow().tx_execution_info.is_none());
assert!(exec_helper_box.execution_helper.borrow().tx_execution_info_iter.clone().peekable().peek().is_some());

start_tx(&mut vm, &mut exec_scopes, &ids_data, &ap_tracking, &Default::default())
.expect("start_tx");

// after starting tx, tx_execution_info should be some and iter should not have a next()
assert!(exec_helper_box.execution_helper.borrow().tx_execution_info.is_some());
assert!(exec_helper_box.execution_helper.borrow().tx_execution_info_iter.clone().peekable().peek().is_none());
}
}

3 changes: 0 additions & 3 deletions src/hints/unimplemented.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,6 @@ const SET_TX_INFO_PTR: &str = indoc! {r#"
#[allow(unused)]
const SPLIT_INPUTS_12: &str = "ids.high12, ids.low12 = divmod(memory[ids.inputs + 12], 256 ** 4)";

#[allow(unused)]
const SKIP_TX: &str = "execution_helper.skip_tx()";

#[allow(unused)]
const SET_AP_TO_IS_REVERTED: &str =
"memory[ap] = to_felt_or_relocatable(execution_helper.tx_execution_info.is_reverted)";
Expand Down

0 comments on commit 4576f99

Please sign in to comment.