Skip to content

Commit c5f1ecb

Browse files
feat(starknet_os): os_logger: implement OsLogger::enter_syscall
1 parent c4b8c52 commit c5f1ecb

File tree

2 files changed

+97
-0
lines changed
  • crates
    • blockifier/src/execution/deprecated_syscalls
    • starknet_os/src/hint_processor

2 files changed

+97
-0
lines changed

crates/blockifier/src/execution/deprecated_syscalls/mod.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,21 @@ pub enum DeprecatedSyscallSelector {
8787
StorageWrite,
8888
}
8989

90+
impl DeprecatedSyscallSelector {
91+
pub fn is_calling_syscall(&self) -> bool {
92+
match self {
93+
Self::CallContract
94+
| Self::DelegateCall
95+
| Self::DelegateL1Handler
96+
| Self::Deploy
97+
| Self::LibraryCall
98+
| Self::LibraryCallL1Handler
99+
| Self::MetaTxV0 => true,
100+
_ => false,
101+
}
102+
}
103+
}
104+
90105
impl TryFrom<Felt> for DeprecatedSyscallSelector {
91106
type Error = DeprecatedSyscallExecutionError;
92107
fn try_from(raw_selector: Felt) -> Result<Self, Self::Error> {

crates/starknet_os/src/hint_processor/os_logger.rs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,17 @@ pub enum OsLoggerError {
4949
MissingBuiltinPtr(String),
5050
#[error("The `members` field is None in identifier data for struct {0}.")]
5151
MissingMembers(String),
52+
#[error("All syscalls should be called inside a transaction.")]
53+
NotInTxContext,
5254
#[error(
5355
"Range check in self and in the enter call counter are not in the same segment: \
5456
{self_ptr}, {enter_ptr}."
5557
)]
5658
RangeCheckNotInSameSegment { self_ptr: Relocatable, enter_ptr: Relocatable },
5759
#[error("SyscallTrace should be finalized before accessing resources.")]
5860
ResourceAccessBeforeFinalize,
61+
#[error("The {0} syscall is not supposed to have an inner syscall.")]
62+
UnexpectedParentSyscall(String),
5963
#[error("{0}")]
6064
UnknownBuiltin(String),
6165
#[error("Builtin {0} is not in the known sizes mapping {:?}.", BUILTIN_INSTANCE_SIZES)]
@@ -319,3 +323,81 @@ impl ResourceCounter {
319323
Ok(())
320324
}
321325
}
326+
327+
pub struct OsLogger {
328+
debug: bool,
329+
current_tx: Option<OsTransactionTrace>,
330+
tab_count: usize,
331+
syscall_stack: Vec<SyscallTrace>,
332+
#[allow(dead_code)]
333+
txs: Vec<OsTransactionTrace>,
334+
resource_counter_stack: Vec<ResourceCounter>,
335+
}
336+
337+
impl OsLogger {
338+
pub fn new(debug: bool) -> Self {
339+
Self {
340+
debug,
341+
current_tx: None,
342+
tab_count: 0,
343+
syscall_stack: Vec::new(),
344+
txs: Vec::new(),
345+
resource_counter_stack: Vec::new(),
346+
}
347+
}
348+
349+
pub fn log(&mut self, msg: &str, enter: bool) {
350+
if self.debug {
351+
if enter {
352+
self.tab_count += 1;
353+
}
354+
let indentation = " ".repeat(self.tab_count);
355+
log::debug!("{indentation}{msg}");
356+
if !enter {
357+
self.tab_count -= 1;
358+
}
359+
}
360+
}
361+
362+
pub fn enter_syscall(
363+
&mut self,
364+
selector: SyscallSelector,
365+
is_deprecated: bool,
366+
n_steps: usize,
367+
range_check_ptr: Relocatable,
368+
ids_data: &HashMap<String, HintReference>,
369+
vm: &VirtualMachine,
370+
ap_tracking: &ApTracking,
371+
os_program: &Program,
372+
) -> OsLoggerResult<()> {
373+
if self.current_tx.is_none() {
374+
return Err(OsLoggerError::NotInTxContext);
375+
}
376+
377+
if let Some(last_call) = self.syscall_stack.last() {
378+
if !last_call.selector.is_calling_syscall() {
379+
return Err(OsLoggerError::UnexpectedParentSyscall(format!(
380+
"{:?}",
381+
last_call.selector
382+
)));
383+
}
384+
}
385+
386+
self.resource_counter_stack.push(ResourceCounter::new(
387+
n_steps,
388+
range_check_ptr,
389+
ids_data,
390+
vm,
391+
ap_tracking,
392+
os_program,
393+
)?);
394+
self.syscall_stack.push(SyscallTrace::new(selector, is_deprecated, self.tab_count));
395+
396+
if selector.is_calling_syscall() {
397+
let deprecated_str = if is_deprecated { "deprecated " } else { "" };
398+
self.log(&format!("Entering {deprecated_str}{:?}.", selector), true);
399+
}
400+
401+
Ok(())
402+
}
403+
}

0 commit comments

Comments
 (0)