Skip to content

Commit fb2aa4d

Browse files
feat(starknet_os): os_logger: implement TransactionTrace
1 parent 923933c commit fb2aa4d

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

crates/starknet_os/src/hint_processor/os_logger.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use std::collections::HashMap;
22

33
use blockifier::execution::syscalls::SyscallSelector;
4+
use blockifier::transaction::transaction_types::TransactionType;
45
use cairo_vm::vm::runners::cairo_runner::ExecutionResources;
6+
use starknet_api::transaction::TransactionHash;
57

68
#[derive(Debug, thiserror::Error)]
79
pub enum OsLoggerError {
@@ -86,3 +88,44 @@ impl TryFrom<SyscallTrace> for String {
8688
))
8789
}
8890
}
91+
92+
pub struct OsTransactionTrace {
93+
tx_type: TransactionType,
94+
tx_hash: TransactionHash,
95+
#[allow(dead_code)]
96+
syscalls: Vec<SyscallTrace>,
97+
resources: Option<ExecutionResources>,
98+
}
99+
100+
impl OsTransactionTrace {
101+
pub fn new(tx_type: TransactionType, tx_hash: TransactionHash) -> Self {
102+
Self { tx_type, tx_hash, syscalls: Vec::new(), resources: None }
103+
}
104+
}
105+
106+
impl ResourceFinalizer for OsTransactionTrace {
107+
fn get_optional_resources(&self) -> Option<&ExecutionResources> {
108+
self.resources.as_ref()
109+
}
110+
111+
fn set_resources(&mut self, resources: ExecutionResources) {
112+
self.resources = Some(resources);
113+
}
114+
}
115+
116+
impl TryFrom<OsTransactionTrace> for String {
117+
type Error = OsLoggerError;
118+
119+
fn try_from(trace: OsTransactionTrace) -> OsLoggerResult<Self> {
120+
let resources = trace.get_resources()?;
121+
let builtins = if !resources.builtin_instance_counter.is_empty() {
122+
format!("\n\tBuiltins: {:?}", resources.builtin_instance_counter)
123+
} else {
124+
"".to_string()
125+
};
126+
Ok(format!(
127+
"Transaction: {:?}\n\tHash: {}\n\tSteps: {}{builtins}",
128+
trace.tx_type, trace.tx_hash, resources.n_steps
129+
))
130+
}
131+
}

0 commit comments

Comments
 (0)