@@ -2,15 +2,33 @@ use std::collections::HashMap;
2
2
3
3
use blockifier:: execution:: syscalls:: SyscallSelector ;
4
4
use blockifier:: transaction:: transaction_types:: TransactionType ;
5
+ use cairo_vm:: hint_processor:: hint_processor_definition:: HintReference ;
6
+ use cairo_vm:: serde:: deserialize_program:: ApTracking ;
7
+ use cairo_vm:: types:: builtin_name:: BuiltinName ;
8
+ use cairo_vm:: types:: program:: Program ;
9
+ use cairo_vm:: types:: relocatable:: Relocatable ;
5
10
use cairo_vm:: vm:: runners:: cairo_runner:: ExecutionResources ;
11
+ use cairo_vm:: vm:: vm_core:: VirtualMachine ;
6
12
use starknet_api:: transaction:: TransactionHash ;
7
13
14
+ use crate :: hints:: error:: OsHintError ;
15
+ use crate :: hints:: vars:: { CairoStruct , Ids } ;
16
+ use crate :: vm_utils:: get_address_of_nested_fields;
17
+
8
18
#[ derive( Debug , thiserror:: Error ) ]
9
19
pub enum OsLoggerError {
20
+ #[ error( "Failed to build builtin pointer map: {0}." ) ]
21
+ BuiltinPtrs ( OsHintError ) ,
10
22
#[ error( "SyscallTrace should be finalized only once." ) ]
11
23
DoubleFinalize ,
24
+ #[ error( "Failed to fetch identifier data for struct {0}." ) ]
25
+ InnerBuiltinPtrsIdentifierMissing ( String ) ,
26
+ #[ error( "The `members` field is None in identifier data for struct {0}." ) ]
27
+ MissingMembers ( String ) ,
12
28
#[ error( "SyscallTrace should be finalized before accessing resources." ) ]
13
29
ResourceAccessBeforeFinalize ,
30
+ #[ error( "{0}" ) ]
31
+ UnknownBuiltin ( String ) ,
14
32
}
15
33
16
34
pub type OsLoggerResult < T > = Result < T , OsLoggerError > ;
@@ -129,3 +147,101 @@ impl TryFrom<OsTransactionTrace> for String {
129
147
) )
130
148
}
131
149
}
150
+
151
+ #[ allow( dead_code) ]
152
+ pub struct ResourceCounter {
153
+ n_steps : usize ,
154
+ range_check_ptr : Relocatable ,
155
+ builtin_ptrs_dict : HashMap < BuiltinName , Relocatable > ,
156
+ }
157
+
158
+ impl ResourceCounter {
159
+ #[ allow( dead_code) ]
160
+ pub ( crate ) fn new (
161
+ n_steps : usize ,
162
+ range_check_ptr : Relocatable ,
163
+ ids_data : & HashMap < String , HintReference > ,
164
+ vm : & VirtualMachine ,
165
+ ap_tracking : & ApTracking ,
166
+ os_program : & Program ,
167
+ ) -> OsLoggerResult < Self > {
168
+ Ok ( Self {
169
+ n_steps,
170
+ range_check_ptr,
171
+ builtin_ptrs_dict : Self :: build_builtin_ptrs_dict (
172
+ ids_data,
173
+ vm,
174
+ ap_tracking,
175
+ os_program,
176
+ ) ?,
177
+ } )
178
+ }
179
+
180
+ fn build_builtin_ptrs_dict (
181
+ ids_data : & HashMap < String , HintReference > ,
182
+ vm : & VirtualMachine ,
183
+ ap_tracking : & ApTracking ,
184
+ os_program : & Program ,
185
+ ) -> OsLoggerResult < HashMap < BuiltinName , Relocatable > > {
186
+ let mut builtin_ptrs_dict: HashMap < BuiltinName , Relocatable > = HashMap :: new ( ) ;
187
+
188
+ // The `BuiltinPointers` struct has two fields: selectable and non-selectable builtins.
189
+ Self :: insert_builtins ( true , & mut builtin_ptrs_dict, ids_data, vm, ap_tracking, os_program) ?;
190
+ Self :: insert_builtins (
191
+ false ,
192
+ & mut builtin_ptrs_dict,
193
+ ids_data,
194
+ vm,
195
+ ap_tracking,
196
+ os_program,
197
+ ) ?;
198
+
199
+ Ok ( builtin_ptrs_dict)
200
+ }
201
+
202
+ fn insert_builtins (
203
+ selectable : bool ,
204
+ builtin_ptrs_dict : & mut HashMap < BuiltinName , Relocatable > ,
205
+ ids_data : & HashMap < String , HintReference > ,
206
+ vm : & VirtualMachine ,
207
+ ap_tracking : & ApTracking ,
208
+ os_program : & Program ,
209
+ ) -> OsLoggerResult < ( ) > {
210
+ let ( inner_field_name, inner_struct) = if selectable {
211
+ ( "selectable" , CairoStruct :: SelectableBuiltins )
212
+ } else {
213
+ ( "non_selectable" , CairoStruct :: NonSelectableBuiltins )
214
+ } ;
215
+
216
+ // We want all pointers except `segment_arena` and `sha256`.
217
+ let inner_struct_name: & str = inner_struct. into ( ) ;
218
+ let inner_members = os_program
219
+ . get_identifier ( inner_struct_name)
220
+ . ok_or ( OsLoggerError :: InnerBuiltinPtrsIdentifierMissing ( inner_struct_name. into ( ) ) ) ?
221
+ . members
222
+ . as_ref ( )
223
+ . ok_or ( OsLoggerError :: MissingMembers ( inner_struct_name. into ( ) ) ) ?;
224
+
225
+ for member_name in inner_members. keys ( ) {
226
+ if member_name == "segment_arena" || member_name == "sha256" {
227
+ continue ;
228
+ }
229
+ let member_ptr = get_address_of_nested_fields (
230
+ ids_data,
231
+ Ids :: BuiltinPtrs ,
232
+ CairoStruct :: BuiltinPointersPtr ,
233
+ vm,
234
+ ap_tracking,
235
+ & [ inner_field_name, member_name. as_str ( ) ] ,
236
+ os_program,
237
+ )
238
+ . map_err ( OsLoggerError :: BuiltinPtrs ) ?;
239
+ builtin_ptrs_dict. insert (
240
+ BuiltinName :: from_str ( member_name)
241
+ . ok_or ( OsLoggerError :: UnknownBuiltin ( member_name. clone ( ) ) ) ?,
242
+ member_ptr,
243
+ ) ;
244
+ }
245
+ Ok ( ( ) )
246
+ }
247
+ }
0 commit comments