Skip to content

Commit bb489cc

Browse files
feat(starknet_os): os_logger: implement OsLogger::exit_tx
1 parent 040fd61 commit bb489cc

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

crates/starknet_os/src/hint_processor/os_logger.rs

Lines changed: 34 additions & 3 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.")]
@@ -179,10 +183,10 @@ impl ResourceFinalizer for OsTransactionTrace {
179183
}
180184
}
181185

182-
impl TryFrom<OsTransactionTrace> for String {
186+
impl TryFrom<&OsTransactionTrace> for String {
183187
type Error = OsLoggerError;
184188

185-
fn try_from(trace: OsTransactionTrace) -> OsLoggerResult<Self> {
189+
fn try_from(trace: &OsTransactionTrace) -> OsLoggerResult<Self> {
186190
let resources = trace.get_resources()?;
187191
let builtins = if !resources.builtin_instance_counter.is_empty() {
188192
format!("\n\tBuiltins: {:?}", resources.builtin_instance_counter)
@@ -345,7 +349,6 @@ pub struct OsLogger {
345349
current_tx: Option<OsTransactionTrace>,
346350
tab_count: usize,
347351
syscall_stack: Vec<SyscallTrace>,
348-
#[allow(dead_code)]
349352
txs: Vec<OsTransactionTrace>,
350353
resource_counter_stack: Vec<ResourceCounter>,
351354
}
@@ -492,4 +495,32 @@ impl OsLogger {
492495
self.log(&format!("Entering {tx_type:?}: {tx_hash}."), true);
493496
Ok(())
494497
}
498+
499+
pub fn exit_tx(
500+
&mut self,
501+
n_steps: usize,
502+
range_check_ptr: Relocatable,
503+
ids_data: &HashMap<String, HintReference>,
504+
vm: &VirtualMachine,
505+
ap_tracking: &ApTracking,
506+
os_program: &Program,
507+
) -> OsLoggerResult<()> {
508+
let mut current_tx = self.current_tx.take().ok_or(OsLoggerError::ExitBeforeEnter)?;
509+
510+
// Sanity check.
511+
if !self.syscall_stack.is_empty() {
512+
return Err(OsLoggerError::RemainingSyscalls);
513+
}
514+
515+
let enter_resources_counter =
516+
self.resource_counter_stack.pop().ok_or(OsLoggerError::CallStackEmpty)?;
517+
let exit_resources_counter =
518+
ResourceCounter::new(n_steps, range_check_ptr, ids_data, vm, ap_tracking, os_program)?;
519+
520+
current_tx
521+
.finalize_resources(exit_resources_counter.sub_counter(&enter_resources_counter)?)?;
522+
self.log(&format!("Exiting {}.", String::try_from(&current_tx)?), false);
523+
self.txs.push(current_tx);
524+
Ok(())
525+
}
495526
}

0 commit comments

Comments
 (0)