11use crate :: gas:: stats:: GasStats ;
2+ use crate :: gas:: utils:: shorten_felt;
23use cheatnet:: trace_data:: { CallTrace , CallTraceNode } ;
34use comfy_table:: modifiers:: UTF8_ROUND_CORNERS ;
45use comfy_table:: { Attribute , Cell , Color , Table } ;
@@ -12,14 +13,20 @@ use std::fmt::Display;
1213type ContractName = String ;
1314type Selector = String ;
1415
16+ #[ derive( Debug , Clone , PartialOrd , PartialEq , Ord , Eq ) ]
17+ pub enum ContractId {
18+ TestContract ( ContractName ) ,
19+ ForkedContract ( ClassHash ) ,
20+ }
21+
1522#[ derive( Debug , Clone ) ]
1623pub struct SingleTestGasInfo {
1724 pub gas_used : GasVector ,
1825 pub report_data : Option < ReportData > ,
1926}
2027
2128#[ derive( Debug , Clone , Default ) ]
22- pub struct ReportData ( BTreeMap < ContractName , ContractInfo > ) ;
29+ pub struct ReportData ( BTreeMap < ContractId , ContractInfo > ) ;
2330
2431#[ derive( Debug , Clone , Default ) ]
2532pub struct ContractInfo {
@@ -58,15 +65,15 @@ impl SingleTestGasInfo {
5865 "class_hash should be set in `fn execute_call_entry_point` in cheatnet" ,
5966 ) ;
6067
61- let contract_name = get_contract_name ( contracts_data, class_hash) ;
68+ let contract_id = get_contract_id ( contracts_data, class_hash) ;
6269 let selector = get_selector ( contracts_data, call. entry_point . entry_point_selector ) ;
6370 let gas = call
6471 . gas_report_data
6572 . as_ref ( )
6673 . expect ( "Gas report data must be updated after test execution" )
6774 . get_gas ( ) ;
6875
69- report_data. update_entry ( contract_name , selector, gas) ;
76+ report_data. update_entry ( contract_id , selector, gas) ;
7077 stack. extend ( call. nested_calls . clone ( ) ) ;
7178 }
7279 }
@@ -80,13 +87,8 @@ impl SingleTestGasInfo {
8087}
8188
8289impl ReportData {
83- fn update_entry (
84- & mut self ,
85- contract_name : ContractName ,
86- selector : Selector ,
87- gas_used : GasVector ,
88- ) {
89- let contract_info = self . 0 . entry ( contract_name) . or_default ( ) ;
90+ fn update_entry ( & mut self , contract_id : ContractId , selector : Selector , gas_used : GasVector ) {
91+ let contract_info = self . 0 . entry ( contract_id) . or_default ( ) ;
9092
9193 let current_gas = contract_info. gas_used ;
9294 contract_info. gas_used = current_gas. checked_add ( gas_used) . unwrap_or_else ( || {
@@ -119,11 +121,26 @@ impl Display for ReportData {
119121 }
120122}
121123
122- fn get_contract_name ( contracts_data : & ContractsDataStore , class_hash : ClassHash ) -> ContractName {
123- contracts_data
124- . get_contract_name ( & class_hash)
125- . map_or ( "forked contract" , |name| name. 0 . as_str ( ) )
126- . to_string ( )
124+ impl Display for ContractId {
125+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
126+ let name = match self {
127+ ContractId :: TestContract ( name) => format ! ( "{name} Contract" ) ,
128+ ContractId :: ForkedContract ( class_hash) => {
129+ format ! (
130+ "forked contract\n (class hash: {})" ,
131+ shorten_felt( class_hash. 0 )
132+ )
133+ }
134+ } ;
135+ write ! ( f, "{name}" )
136+ }
137+ }
138+
139+ fn get_contract_id ( contracts_data : & ContractsDataStore , class_hash : ClassHash ) -> ContractId {
140+ match contracts_data. get_contract_name ( & class_hash) {
141+ Some ( name) => ContractId :: TestContract ( name. 0 . to_string ( ) ) ,
142+ None => ContractId :: ForkedContract ( class_hash) ,
143+ }
127144}
128145
129146fn get_selector ( contracts_data : & ContractsDataStore , selector : EntryPointSelector ) -> Selector {
@@ -134,13 +151,11 @@ fn get_selector(contracts_data: &ContractsDataStore, selector: EntryPointSelecto
134151 . clone ( )
135152}
136153
137- pub fn format_table_output ( contract_info : & ContractInfo , name : & ContractName ) -> Table {
154+ pub fn format_table_output ( contract_info : & ContractInfo , contract_id : & ContractId ) -> Table {
138155 let mut table = Table :: new ( ) ;
139156 table. apply_modifier ( UTF8_ROUND_CORNERS ) ;
140157
141- table. set_header ( vec ! [
142- Cell :: new( format!( "{name} Contract" ) ) . fg( Color :: Magenta ) ,
143- ] ) ;
158+ table. set_header ( vec ! [ Cell :: new( contract_id. to_string( ) ) . fg( Color :: Magenta ) ] ) ;
144159 table. add_row ( vec ! [
145160 Cell :: new( "Function Name" ) . add_attribute( Attribute :: Bold ) ,
146161 Cell :: new( "Min" ) . add_attribute( Attribute :: Bold ) ,
0 commit comments