@@ -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,106 @@ 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 (
190
+ "selectable" ,
191
+ CairoStruct :: SelectableBuiltins ,
192
+ & mut builtin_ptrs_dict,
193
+ ids_data,
194
+ vm,
195
+ ap_tracking,
196
+ os_program,
197
+ ) ?;
198
+ Self :: insert_builtins (
199
+ "non_selectable" ,
200
+ CairoStruct :: NonSelectableBuiltins ,
201
+ & mut builtin_ptrs_dict,
202
+ ids_data,
203
+ vm,
204
+ ap_tracking,
205
+ os_program,
206
+ ) ?;
207
+
208
+ Ok ( builtin_ptrs_dict)
209
+ }
210
+
211
+ fn insert_builtins (
212
+ inner_field_name : & str ,
213
+ inner_field_type : CairoStruct ,
214
+ builtin_ptrs_dict : & mut HashMap < BuiltinName , Relocatable > ,
215
+ ids_data : & HashMap < String , HintReference > ,
216
+ vm : & VirtualMachine ,
217
+ ap_tracking : & ApTracking ,
218
+ os_program : & Program ,
219
+ ) -> OsLoggerResult < ( ) > {
220
+ // We want all pointers except `segment_arena` and `sha256`.
221
+ let excluded_builtins = [ "segment_arena" , "sha256" ] ;
222
+ let inner_struct_name: & str = inner_field_type. into ( ) ;
223
+ let inner_members = os_program
224
+ . get_identifier ( inner_struct_name)
225
+ . ok_or ( OsLoggerError :: InnerBuiltinPtrsIdentifierMissing ( inner_struct_name. into ( ) ) ) ?
226
+ . members
227
+ . as_ref ( )
228
+ . ok_or ( OsLoggerError :: MissingMembers ( inner_struct_name. into ( ) ) ) ?;
229
+
230
+ for member_name in inner_members. keys ( ) {
231
+ if excluded_builtins. contains ( & member_name. as_str ( ) ) {
232
+ continue ;
233
+ }
234
+ let member_ptr = get_address_of_nested_fields (
235
+ ids_data,
236
+ Ids :: BuiltinPtrs ,
237
+ CairoStruct :: BuiltinPointersPtr ,
238
+ vm,
239
+ ap_tracking,
240
+ & [ inner_field_name, member_name. as_str ( ) ] ,
241
+ os_program,
242
+ )
243
+ . map_err ( OsLoggerError :: BuiltinPtrs ) ?;
244
+ builtin_ptrs_dict. insert (
245
+ BuiltinName :: from_str ( member_name)
246
+ . ok_or_else ( || OsLoggerError :: UnknownBuiltin ( member_name. clone ( ) ) ) ?,
247
+ member_ptr,
248
+ ) ;
249
+ }
250
+ Ok ( ( ) )
251
+ }
252
+ }
0 commit comments