Skip to content

feat(starknet_os): os_logger: implement OsLogger::exit_tx #5284

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 34 additions & 3 deletions crates/starknet_os/src/hint_processor/os_logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ pub enum OsLoggerError {
CallStackEmpty,
#[error("SyscallTrace should be finalized only once.")]
DoubleFinalize,
#[error("No transaction should exit without entering.")]
ExitBeforeEnter,
#[error("Failed to fetch identifier data for struct {0}.")]
InnerBuiltinPtrsIdentifierMissing(String),
#[error("No transaction should call another transaction.")]
Expand All @@ -44,6 +46,8 @@ pub enum OsLoggerError {
{self_ptr}, {enter_ptr}."
)]
RangeCheckNotInSameSegment { self_ptr: Relocatable, enter_ptr: Relocatable },
#[error("All Syscalls should end when exiting a transaction.")]
RemainingSyscalls,
#[error("SyscallTrace should be finalized before accessing resources.")]
ResourceAccessBeforeFinalize,
#[error("The {0} syscall is not supposed to have an inner syscall.")]
Expand Down Expand Up @@ -163,10 +167,10 @@ impl ResourceFinalizer for OsTransactionTrace {
}
}

impl TryFrom<OsTransactionTrace> for String {
impl TryFrom<&OsTransactionTrace> for String {
type Error = OsLoggerError;

fn try_from(trace: OsTransactionTrace) -> OsLoggerResult<Self> {
fn try_from(trace: &OsTransactionTrace) -> OsLoggerResult<Self> {
let resources = trace.get_resources()?;
let builtins = if !resources.builtin_instance_counter.is_empty() {
format!("\n\tBuiltins: {:?}", resources.builtin_instance_counter)
Expand Down Expand Up @@ -329,7 +333,6 @@ pub struct OsLogger {
current_tx: Option<OsTransactionTrace>,
tab_count: usize,
syscall_stack: Vec<SyscallTrace>,
#[allow(dead_code)]
txs: Vec<OsTransactionTrace>,
resource_counter_stack: Vec<ResourceCounter>,
}
Expand Down Expand Up @@ -476,4 +479,32 @@ impl OsLogger {
self.log(&format!("Entering {tx_type:?}: {tx_hash}."), true);
Ok(())
}

pub fn exit_tx(
&mut self,
n_steps: usize,
range_check_ptr: Relocatable,
ids_data: &HashMap<String, HintReference>,
vm: &VirtualMachine,
ap_tracking: &ApTracking,
os_program: &Program,
) -> OsLoggerResult<()> {
let mut current_tx = self.current_tx.take().ok_or(OsLoggerError::ExitBeforeEnter)?;

// Sanity check.
if !self.syscall_stack.is_empty() {
return Err(OsLoggerError::RemainingSyscalls);
}

let enter_resources_counter =
self.resource_counter_stack.pop().ok_or(OsLoggerError::CallStackEmpty)?;
let exit_resources_counter =
ResourceCounter::new(n_steps, range_check_ptr, ids_data, vm, ap_tracking, os_program)?;

current_tx
.finalize_resources(exit_resources_counter.sub_counter(&enter_resources_counter)?)?;
self.log(&format!("Exiting {}.", String::try_from(&current_tx)?), false);
self.txs.push(current_tx);
Ok(())
}
}
Loading