@@ -29,6 +29,8 @@ pub enum OsLoggerError {
29
29
CallStackEmpty ,
30
30
#[ error( "SyscallTrace should be finalized only once." ) ]
31
31
DoubleFinalize ,
32
+ #[ error( "No transaction should exit without entering." ) ]
33
+ ExitBeforeEnter ,
32
34
#[ error( "Failed to fetch identifier data for struct {0}." ) ]
33
35
InnerBuiltinPtrsIdentifierMissing ( String ) ,
34
36
#[ error( "No transaction should call another transaction." ) ]
@@ -44,6 +46,8 @@ pub enum OsLoggerError {
44
46
{self_ptr}, {enter_ptr}."
45
47
) ]
46
48
RangeCheckNotInSameSegment { self_ptr : Relocatable , enter_ptr : Relocatable } ,
49
+ #[ error( "All Syscalls should end when exiting a transaction." ) ]
50
+ RemainingSyscalls ,
47
51
#[ error( "SyscallTrace should be finalized before accessing resources." ) ]
48
52
ResourceAccessBeforeFinalize ,
49
53
#[ error( "The {0} syscall is not supposed to have an inner syscall." ) ]
@@ -163,10 +167,10 @@ impl ResourceFinalizer for OsTransactionTrace {
163
167
}
164
168
}
165
169
166
- impl TryFrom < OsTransactionTrace > for String {
170
+ impl TryFrom < & OsTransactionTrace > for String {
167
171
type Error = OsLoggerError ;
168
172
169
- fn try_from ( trace : OsTransactionTrace ) -> OsLoggerResult < Self > {
173
+ fn try_from ( trace : & OsTransactionTrace ) -> OsLoggerResult < Self > {
170
174
let resources = trace. get_resources ( ) ?;
171
175
let builtins = if !resources. builtin_instance_counter . is_empty ( ) {
172
176
format ! ( "\n \t Builtins: {:?}" , resources. builtin_instance_counter)
@@ -329,7 +333,6 @@ pub struct OsLogger {
329
333
current_tx : Option < OsTransactionTrace > ,
330
334
tab_count : usize ,
331
335
syscall_stack : Vec < SyscallTrace > ,
332
- #[ allow( dead_code) ]
333
336
txs : Vec < OsTransactionTrace > ,
334
337
resource_counter_stack : Vec < ResourceCounter > ,
335
338
}
@@ -476,4 +479,32 @@ impl OsLogger {
476
479
self . log ( & format ! ( "Entering {tx_type:?}: {tx_hash}." ) , true ) ;
477
480
Ok ( ( ) )
478
481
}
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
+ }
479
510
}
0 commit comments