Skip to content

Commit 58e6e1c

Browse files
feat(starknet_os): os_logger: implement OsLogger::exit_tx
1 parent be7fe86 commit 58e6e1c

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
@@ -29,6 +29,8 @@ pub enum OsLoggerError {
2929
CallStackEmpty,
3030
#[error("SyscallTrace should be finalized only once.")]
3131
DoubleFinalize,
32+
#[error("No transaction should exit without entering.")]
33+
ExitBeforeEnter,
3234
#[error("Failed to fetch identifier data for struct {0}.")]
3335
InnerBuiltinPtrsIdentifierMissing(String),
3436
#[error("No transaction should call another transaction.")]
@@ -44,6 +46,8 @@ pub enum OsLoggerError {
4446
{self_ptr}, {enter_ptr}."
4547
)]
4648
RangeCheckNotInSameSegment { self_ptr: Relocatable, enter_ptr: Relocatable },
49+
#[error("All Syscalls should end when exiting a transaction.")]
50+
RemainingSyscalls,
4751
#[error("SyscallTrace should be finalized before accessing resources.")]
4852
ResourceAccessBeforeFinalize,
4953
#[error("The {0} syscall is not supposed to have an inner syscall.")]
@@ -163,10 +167,10 @@ impl ResourceFinalizer for OsTransactionTrace {
163167
}
164168
}
165169

166-
impl TryFrom<OsTransactionTrace> for String {
170+
impl TryFrom<&OsTransactionTrace> for String {
167171
type Error = OsLoggerError;
168172

169-
fn try_from(trace: OsTransactionTrace) -> OsLoggerResult<Self> {
173+
fn try_from(trace: &OsTransactionTrace) -> OsLoggerResult<Self> {
170174
let resources = trace.get_resources()?;
171175
let builtins = if !resources.builtin_instance_counter.is_empty() {
172176
format!("\n\tBuiltins: {:?}", resources.builtin_instance_counter)
@@ -329,7 +333,6 @@ pub struct OsLogger {
329333
current_tx: Option<OsTransactionTrace>,
330334
tab_count: usize,
331335
syscall_stack: Vec<SyscallTrace>,
332-
#[allow(dead_code)]
333336
txs: Vec<OsTransactionTrace>,
334337
resource_counter_stack: Vec<ResourceCounter>,
335338
}
@@ -476,4 +479,32 @@ impl OsLogger {
476479
self.log(&format!("Entering {tx_type:?}: {tx_hash}."), true);
477480
Ok(())
478481
}
482+
483+
pub fn exit_tx(
484+
&mut self,
485+
n_steps: usize,
486+
range_check_ptr: Relocatable,
487+
ids_data: &HashMap<String, HintReference>,
488+
vm: &VirtualMachine,
489+
ap_tracking: &ApTracking,
490+
os_program: &Program,
491+
) -> OsLoggerResult<()> {
492+
let mut current_tx = self.current_tx.take().ok_or(OsLoggerError::ExitBeforeEnter)?;
493+
494+
// Sanity check.
495+
if !self.syscall_stack.is_empty() {
496+
return Err(OsLoggerError::RemainingSyscalls);
497+
}
498+
499+
let enter_resources_counter =
500+
self.resource_counter_stack.pop().ok_or(OsLoggerError::CallStackEmpty)?;
501+
let exit_resources_counter =
502+
ResourceCounter::new(n_steps, range_check_ptr, ids_data, vm, ap_tracking, os_program)?;
503+
504+
current_tx
505+
.finalize_resources(exit_resources_counter.sub_counter(&enter_resources_counter)?)?;
506+
self.log(&format!("Exiting {}.", String::try_from(&current_tx)?), false);
507+
self.txs.push(current_tx);
508+
Ok(())
509+
}
479510
}

0 commit comments

Comments
 (0)