@@ -16,25 +16,20 @@ use cairo_lang_sierra::{
1616 ids:: FunctionId ,
1717 program:: { Program , StatementIdx } ,
1818} ;
19- use cairo_lang_sierra_ap_change:: { ap_change_info:: ApChangeInfo , calc_ap_changes} ;
20- use cairo_lang_sierra_ap_change:: {
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 ;
2725
2826use crate :: { error:: Result as NativeResult , native_panic} ;
2927
30- use std:: collections:: BTreeMap ;
28+ use std:: { collections:: BTreeMap , fmt , ops :: Deref } ;
3129
3230/// Holds global gas info.
33- #[ derive( Debug , Default , PartialEq , Eq ) ]
34- pub struct GasMetadata {
35- pub ap_change_info : ApChangeInfo ,
36- pub gas_info : GasInfo ,
37- }
31+ #[ derive( Default ) ]
32+ pub struct GasMetadata ( pub CairoGasMetadata ) ;
3833
3934/// The gas cost associated to a determined sierra statement.
4035///
@@ -43,15 +38,6 @@ pub struct GasMetadata {
4338#[ derive( Debug , Clone , PartialEq , Eq , Hash ) ]
4439pub struct GasCost ( pub Vec < ( u64 , CostTokenType ) > ) ;
4540
46- /// Configuration for metadata computation.
47- #[ derive( Debug , Clone ) ]
48- pub struct MetadataComputationConfig {
49- pub function_set_costs : OrderedHashMap < FunctionId , OrderedHashMap < CostTokenType , i32 > > ,
50- // ignored, its always used
51- pub linear_gas_solver : bool ,
52- pub linear_ap_change_solver : bool ,
53- }
54-
5541/// Error for metadata calculations.
5642#[ derive( Debug , thiserror:: Error , Eq , PartialEq ) ]
5743pub enum GasMetadataError {
@@ -63,26 +49,18 @@ pub enum GasMetadataError {
6349 NotEnoughGas { gas : Box < ( u64 , u64 ) > } ,
6450}
6551
66- impl Default for MetadataComputationConfig {
67- fn default ( ) -> Self {
68- Self {
69- function_set_costs : Default :: default ( ) ,
70- linear_gas_solver : true ,
71- linear_ap_change_solver : true ,
72- }
73- }
74- }
75-
7652impl GasMetadata {
7753 pub fn new (
7854 sierra_program : & Program ,
7955 config : Option < MetadataComputationConfig > ,
8056 ) -> Result < GasMetadata , GasMetadataError > {
81- if let Some ( metadata_config) = config {
82- calc_metadata ( sierra_program, metadata_config)
57+ let cairo_gas_metadata = if let Some ( metadata_config) = config {
58+ calc_metadata ( sierra_program, metadata_config) ?
8359 } else {
84- calc_metadata_ap_change_only ( sierra_program)
85- }
60+ calc_metadata_ap_change_only ( sierra_program) ?
61+ } ;
62+
63+ Ok ( GasMetadata :: from ( cairo_gas_metadata) )
8664 }
8765
8866 /// Returns the initial value for the gas counter.
@@ -183,9 +161,32 @@ impl GasMetadata {
183161 }
184162}
185163
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+
186187impl Clone for GasMetadata {
187188 fn clone ( & self ) -> Self {
188- Self {
189+ Self ( CairoGasMetadata {
189190 ap_change_info : ApChangeInfo {
190191 variable_values : self . ap_change_info . variable_values . clone ( ) ,
191192 function_ap_change : self . ap_change_info . function_ap_change . clone ( ) ,
@@ -194,61 +195,15 @@ impl Clone for GasMetadata {
194195 variable_values : self . gas_info . variable_values . clone ( ) ,
195196 function_costs : self . gas_info . function_costs . clone ( ) ,
196197 } ,
197- }
198+ } )
198199 }
199200}
200201
201- // Methods from https://github.com/starkware-libs/cairo/blob/fbdbbe4c42a6808eccbff8436078f73d0710c772/crates/cairo-lang-sierra-to-casm/src/metadata.rs#L71
202-
203- /// Calculates the metadata for a Sierra program, with ap change info only.
204- fn calc_metadata_ap_change_only ( program : & Program ) -> Result < GasMetadata , GasMetadataError > {
205- Ok ( GasMetadata {
206- ap_change_info : calc_ap_changes ( program, |_, _| 0 ) ?,
207- gas_info : GasInfo {
208- variable_values : Default :: default ( ) ,
209- function_costs : Default :: default ( ) ,
210- } ,
211- } )
212- }
213-
214- /// Calculates the metadata for a Sierra program.
215- ///
216- /// `no_eq_solver` uses a linear-time algorithm for calculating the gas, instead of solving
217- /// equations.
218- fn calc_metadata (
219- program : & Program ,
220- config : MetadataComputationConfig ,
221- ) -> Result < GasMetadata , GasMetadataError > {
222- let pre_gas_info = compute_precost_info ( program) ?;
223-
224- let ap_change_info = if config. linear_ap_change_solver {
225- linear_calc_ap_changes
226- } else {
227- calc_ap_changes
228- } ( program, |idx, token_type| {
229- pre_gas_info. variable_values [ & ( idx, token_type) ] as usize
230- } ) ?;
231-
232- let enforced_function_costs: OrderedHashMap < FunctionId , i32 > = config
233- . function_set_costs
234- . iter ( )
235- . map ( |( func, costs) | ( func. clone ( ) , costs[ & CostTokenType :: Const ] ) )
236- . collect ( ) ;
237- let post_gas_info = compute_postcost_info (
238- program,
239- & |idx| {
240- ap_change_info
241- . variable_values
242- . get ( idx)
243- . copied ( )
244- . unwrap_or_default ( )
245- } ,
246- & pre_gas_info,
247- & enforced_function_costs,
248- ) ?;
249-
250- Ok ( GasMetadata {
251- ap_change_info,
252- gas_info : pre_gas_info. combine ( post_gas_info) ,
253- } )
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+ }
254209}
0 commit comments