1
1
use crate :: trace:: types:: { ContractName , Selector } ;
2
- use cairo_lang_sierra:: program:: ProgramArtifact ;
2
+ use cairo_lang_sierra:: program:: { Program , ProgramArtifact } ;
3
+ use cairo_lang_sierra_to_casm:: compiler:: {
4
+ CairoProgram , CairoProgramDebugInfo , SierraToCasmConfig ,
5
+ } ;
6
+ use cairo_lang_sierra_to_casm:: metadata:: { MetadataComputationConfig , calc_metadata} ;
3
7
use cairo_lang_starknet_classes:: contract_class:: ContractClass ;
4
8
use cheatnet:: forking:: data:: ForkData ;
5
9
use cheatnet:: runtime_extensions:: forge_runtime_extension:: contracts_data:: ContractsData ;
@@ -16,6 +20,8 @@ pub struct ContractsDataStore {
16
20
contract_names : HashMap < ClassHash , ContractName > ,
17
21
selectors : HashMap < EntryPointSelector , Selector > ,
18
22
programs : HashMap < ClassHash , ProgramArtifact > ,
23
+ // FIXME(https://github.com/software-mansion/universal-sierra-compiler/issues/98): Use CASM debug info from USC once it provides it.
24
+ casm_debug_infos : HashMap < ClassHash , CairoProgramDebugInfo > ,
19
25
}
20
26
21
27
impl ContractsDataStore {
@@ -46,7 +52,7 @@ impl ContractsDataStore {
46
52
. chain ( fork_data. abi . clone ( ) )
47
53
. collect ( ) ;
48
54
49
- let programs = contracts_data
55
+ let programs: HashMap < _ , _ > = contracts_data
50
56
. contracts
51
57
. par_iter ( )
52
58
. map ( |( _, contract_data) | {
@@ -69,11 +75,20 @@ impl ContractsDataStore {
69
75
} )
70
76
. collect ( ) ;
71
77
78
+ let casm_debug_infos = programs
79
+ . iter ( )
80
+ . map ( |( class_hash, program_artifact) | {
81
+ let casm = compile ( & program_artifact. program ) ;
82
+ ( * class_hash, casm. debug_info )
83
+ } )
84
+ . collect ( ) ;
85
+
72
86
Self {
73
87
abi,
74
88
contract_names,
75
89
selectors,
76
90
programs,
91
+ casm_debug_infos,
77
92
}
78
93
}
79
94
@@ -99,4 +114,24 @@ impl ContractsDataStore {
99
114
pub fn get_program_artifact ( & self , class_hash : & ClassHash ) -> Option < & ProgramArtifact > {
100
115
self . programs . get ( class_hash)
101
116
}
117
+
118
+ /// Gets the [`CairoProgramDebugInfo`] for a given contract [`ClassHash`].
119
+ #[ must_use]
120
+ pub fn get_casm_debug_info ( & self , class_hash : & ClassHash ) -> Option < & CairoProgramDebugInfo > {
121
+ self . casm_debug_infos . get ( class_hash)
122
+ }
123
+ }
124
+
125
+ /// Compile the given [`Program`] to `casm`.
126
+ fn compile ( program : & Program ) -> CairoProgram {
127
+ let metadata = calc_metadata ( program, MetadataComputationConfig :: default ( ) )
128
+ . expect ( "metadata calculation should not fail" ) ;
129
+
130
+ let config = SierraToCasmConfig {
131
+ gas_usage_check : false ,
132
+ max_bytecode_size : usize:: MAX ,
133
+ } ;
134
+
135
+ cairo_lang_sierra_to_casm:: compiler:: compile ( program, & metadata, config)
136
+ . expect ( "compilation should not fail" )
102
137
}
0 commit comments