Skip to content

Commit 7b1f67e

Browse files
feat(starknet_os): os_logger: implement OsLogger::exit_tx
1 parent 09545d8 commit 7b1f67e

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

crates/starknet_os/src/hint_processor/os_logger.rs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ pub enum OsLoggerError {
4545
CallStackEmpty,
4646
#[error("SyscallTrace should be finalized only once.")]
4747
DoubleFinalize,
48+
#[error("No transaction should exit without entering.")]
49+
ExitBeforeEnter,
4850
#[error("Failed to fetch identifier data for struct {0}.")]
4951
InnerBuiltinPtrsIdentifierMissing(String),
5052
#[error("No transaction should call another transaction.")]
@@ -60,6 +62,8 @@ pub enum OsLoggerError {
6062
{self_ptr}, {enter_ptr}."
6163
)]
6264
RangeCheckNotInSameSegment { self_ptr: Relocatable, enter_ptr: Relocatable },
65+
#[error("All Syscalls should end when exiting a transaction.")]
66+
RemainingSyscalls,
6367
#[error("SyscallTrace should be finalized before accessing resources.")]
6468
ResourceAccessBeforeFinalize,
6569
#[error("The {0} syscall is not supposed to have an inner syscall.")]
@@ -180,10 +184,10 @@ impl ResourceFinalizer for OsTransactionTrace {
180184
}
181185
}
182186

183-
impl TryFrom<OsTransactionTrace> for String {
187+
impl TryFrom<&OsTransactionTrace> for String {
184188
type Error = OsLoggerError;
185189

186-
fn try_from(trace: OsTransactionTrace) -> OsLoggerResult<Self> {
190+
fn try_from(trace: &OsTransactionTrace) -> OsLoggerResult<Self> {
187191
let resources = trace.get_resources()?;
188192
let builtins = if !resources.builtin_instance_counter.is_empty() {
189193
format!("\n\tBuiltins: {:?}", resources.builtin_instance_counter)
@@ -487,4 +491,32 @@ impl OsLogger {
487491
self.log(&format!("Entering {tx_type:?}: {tx_hash}."), true);
488492
Ok(())
489493
}
494+
495+
pub fn exit_tx(
496+
&mut self,
497+
n_steps: usize,
498+
range_check_ptr: Relocatable,
499+
ids_data: &HashMap<String, HintReference>,
500+
vm: &VirtualMachine,
501+
ap_tracking: &ApTracking,
502+
os_program: &Program,
503+
) -> OsLoggerResult<()> {
504+
let mut current_tx = self.current_tx.take().ok_or(OsLoggerError::ExitBeforeEnter)?;
505+
506+
// Sanity check.
507+
if !self.syscall_stack.is_empty() {
508+
return Err(OsLoggerError::RemainingSyscalls);
509+
}
510+
511+
let enter_resources_counter =
512+
self.resource_counter_stack.pop().ok_or(OsLoggerError::CallStackEmpty)?;
513+
let exit_resources_counter =
514+
ResourceCounter::new(n_steps, range_check_ptr, ids_data, vm, ap_tracking, os_program)?;
515+
516+
current_tx
517+
.finalize_resources(exit_resources_counter.sub_counter(&enter_resources_counter)?)?;
518+
self.log(&format!("Exiting {}.", String::try_from(&current_tx)?), false);
519+
self.txs.push(current_tx);
520+
Ok(())
521+
}
490522
}

0 commit comments

Comments
 (0)