@@ -16,22 +16,20 @@ use cairo_lang_sierra::{
1616 ids:: FunctionId ,
1717 program:: { Program , StatementIdx } ,
1818} ;
19- use cairo_lang_sierra_ap_change:: {
20- ap_change_info:: ApChangeInfo , calc_ap_changes,
21- compute:: calc_ap_changes as linear_calc_ap_changes, ApChangeError ,
19+ use cairo_lang_sierra_ap_change:: { ap_change_info:: ApChangeInfo , ApChangeError } ;
20+ use cairo_lang_sierra_gas:: { gas_info:: GasInfo , CostError } ;
21+ use cairo_lang_sierra_to_casm:: metadata:: {
22+ calc_metadata, calc_metadata_ap_change_only, Metadata as CairoGasMetadata ,
23+ MetadataComputationConfig , MetadataError as CairoGasMetadataError ,
2224} ;
23- use cairo_lang_sierra_gas:: {
24- compute_postcost_info, compute_precost_info, gas_info:: GasInfo , CostError ,
25- } ;
26- use cairo_lang_utils:: ordered_hash_map:: OrderedHashMap ;
27- use std:: collections:: BTreeMap ;
25+
26+ use crate :: { error:: Result as NativeResult , native_panic} ;
27+
28+ use std:: { collections:: BTreeMap , fmt, ops:: Deref } ;
2829
2930/// Holds global gas info.
30- #[ derive( Debug , Default , PartialEq , Eq ) ]
31- pub struct GasMetadata {
32- pub ap_change_info : ApChangeInfo ,
33- pub gas_info : GasInfo ,
34- }
31+ #[ derive( Default ) ]
32+ pub struct GasMetadata ( pub CairoGasMetadata ) ;
3533
3634/// The gas cost associated to a determined sierra statement.
3735///
@@ -40,15 +38,6 @@ pub struct GasMetadata {
4038#[ derive( Debug , Clone , PartialEq , Eq , Hash ) ]
4139pub struct GasCost ( pub Vec < ( u64 , CostTokenType ) > ) ;
4240
43- /// Configuration for metadata computation.
44- #[ derive( Debug , Clone ) ]
45- pub struct MetadataComputationConfig {
46- pub function_set_costs : OrderedHashMap < FunctionId , OrderedHashMap < CostTokenType , i32 > > ,
47- // ignored, its always used
48- pub linear_gas_solver : bool ,
49- pub linear_ap_change_solver : bool ,
50- }
51-
5241/// Error for metadata calculations.
5342#[ derive( Debug , thiserror:: Error , Eq , PartialEq ) ]
5443pub enum GasMetadataError {
@@ -60,26 +49,18 @@ pub enum GasMetadataError {
6049 NotEnoughGas { gas : Box < ( u64 , u64 ) > } ,
6150}
6251
63- impl Default for MetadataComputationConfig {
64- fn default ( ) -> Self {
65- Self {
66- function_set_costs : Default :: default ( ) ,
67- linear_gas_solver : true ,
68- linear_ap_change_solver : true ,
69- }
70- }
71- }
72-
7352impl GasMetadata {
7453 pub fn new (
7554 sierra_program : & Program ,
7655 config : Option < MetadataComputationConfig > ,
7756 ) -> Result < GasMetadata , GasMetadataError > {
78- if let Some ( metadata_config) = config {
79- calc_metadata ( sierra_program, metadata_config)
57+ let cairo_gas_metadata = if let Some ( metadata_config) = config {
58+ calc_metadata ( sierra_program, metadata_config) ?
8059 } else {
81- calc_metadata_ap_change_only ( sierra_program)
82- }
60+ calc_metadata_ap_change_only ( sierra_program) ?
61+ } ;
62+
63+ Ok ( GasMetadata :: from ( cairo_gas_metadata) )
8364 }
8465
8566 /// Returns the initial value for the gas counter.
@@ -180,9 +161,32 @@ impl GasMetadata {
180161 }
181162}
182163
164+ impl From < CairoGasMetadata > for GasMetadata {
165+ fn from ( value : CairoGasMetadata ) -> Self {
166+ Self ( value)
167+ }
168+ }
169+
170+ impl Deref for GasMetadata {
171+ type Target = CairoGasMetadata ;
172+
173+ fn deref ( & self ) -> & Self :: Target {
174+ & self . 0
175+ }
176+ }
177+
178+ impl fmt:: Debug for GasMetadata {
179+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
180+ f. debug_struct ( "GasMetadata" )
181+ . field ( "ap_change_info" , & self . ap_change_info )
182+ . field ( "gas_info" , & self . gas_info )
183+ . finish ( )
184+ }
185+ }
186+
183187impl Clone for GasMetadata {
184188 fn clone ( & self ) -> Self {
185- Self {
189+ Self ( CairoGasMetadata {
186190 ap_change_info : ApChangeInfo {
187191 variable_values : self . ap_change_info . variable_values . clone ( ) ,
188192 function_ap_change : self . ap_change_info . function_ap_change . clone ( ) ,
@@ -191,61 +195,15 @@ impl Clone for GasMetadata {
191195 variable_values : self . gas_info . variable_values . clone ( ) ,
192196 function_costs : self . gas_info . function_costs . clone ( ) ,
193197 } ,
194- }
198+ } )
195199 }
196200}
197201
198- // Methods from https://github.com/starkware-libs/cairo/blob/fbdbbe4c42a6808eccbff8436078f73d0710c772/crates/cairo-lang-sierra-to-casm/src/metadata.rs#L71
199-
200- /// Calculates the metadata for a Sierra program, with ap change info only.
201- fn calc_metadata_ap_change_only ( program : & Program ) -> Result < GasMetadata , GasMetadataError > {
202- Ok ( GasMetadata {
203- ap_change_info : calc_ap_changes ( program, |_, _| 0 ) ?,
204- gas_info : GasInfo {
205- variable_values : Default :: default ( ) ,
206- function_costs : Default :: default ( ) ,
207- } ,
208- } )
209- }
210-
211- /// Calculates the metadata for a Sierra program.
212- ///
213- /// `no_eq_solver` uses a linear-time algorithm for calculating the gas, instead of solving
214- /// equations.
215- fn calc_metadata (
216- program : & Program ,
217- config : MetadataComputationConfig ,
218- ) -> Result < GasMetadata , GasMetadataError > {
219- let pre_gas_info = compute_precost_info ( program) ?;
220-
221- let ap_change_info = if config. linear_ap_change_solver {
222- linear_calc_ap_changes
223- } else {
224- calc_ap_changes
225- } ( program, |idx, token_type| {
226- pre_gas_info. variable_values [ & ( idx, token_type) ] as usize
227- } ) ?;
228-
229- let enforced_function_costs: OrderedHashMap < FunctionId , i32 > = config
230- . function_set_costs
231- . iter ( )
232- . map ( |( func, costs) | ( func. clone ( ) , costs[ & CostTokenType :: Const ] ) )
233- . collect ( ) ;
234- let post_gas_info = compute_postcost_info (
235- program,
236- & |idx| {
237- ap_change_info
238- . variable_values
239- . get ( idx)
240- . copied ( )
241- . unwrap_or_default ( )
242- } ,
243- & pre_gas_info,
244- & enforced_function_costs,
245- ) ?;
246-
247- Ok ( GasMetadata {
248- ap_change_info,
249- gas_info : pre_gas_info. combine ( post_gas_info) ,
250- } )
202+ impl From < CairoGasMetadataError > for GasMetadataError {
203+ fn from ( value : CairoGasMetadataError ) -> Self {
204+ match value {
205+ CairoGasMetadataError :: ApChangeError ( x) => GasMetadataError :: ApChangeError ( x) ,
206+ CairoGasMetadataError :: CostError ( x) => GasMetadataError :: CostError ( x) ,
207+ }
208+ }
251209}
0 commit comments