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+ LocalContract ( 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 ( || {
@@ -124,11 +126,26 @@ impl Display for ReportData {
124126 }
125127}
126128
127- fn get_contract_name ( contracts_data : & ContractsDataStore , class_hash : ClassHash ) -> ContractName {
128- contracts_data
129- . get_contract_name ( & class_hash)
130- . map_or ( "forked contract" , |name| name. 0 . as_str ( ) )
131- . to_string ( )
129+ impl Display for ContractId {
130+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
131+ let name = match self {
132+ ContractId :: LocalContract ( name) => format ! ( "{name} Contract" ) ,
133+ ContractId :: ForkedContract ( class_hash) => {
134+ format ! (
135+ "forked contract\n (class hash: {})" ,
136+ shorten_felt( class_hash. 0 )
137+ )
138+ }
139+ } ;
140+ write ! ( f, "{name}" )
141+ }
142+ }
143+
144+ fn get_contract_id ( contracts_data : & ContractsDataStore , class_hash : ClassHash ) -> ContractId {
145+ match contracts_data. get_contract_name ( & class_hash) {
146+ Some ( name) => ContractId :: LocalContract ( name. 0 . clone ( ) ) ,
147+ None => ContractId :: ForkedContract ( class_hash) ,
148+ }
132149}
133150
134151fn get_selector ( contracts_data : & ContractsDataStore , selector : EntryPointSelector ) -> Selector {
@@ -139,13 +156,11 @@ fn get_selector(contracts_data: &ContractsDataStore, selector: EntryPointSelecto
139156 . clone ( )
140157}
141158
142- pub fn format_table_output ( contract_info : & ContractInfo , name : & ContractName ) -> Table {
159+ pub fn format_table_output ( contract_info : & ContractInfo , contract_id : & ContractId ) -> Table {
143160 let mut table = Table :: new ( ) ;
144161 table. apply_modifier ( UTF8_ROUND_CORNERS ) ;
145162
146- table. set_header ( vec ! [
147- Cell :: new( format!( "{name} Contract" ) ) . fg( Color :: Magenta ) ,
148- ] ) ;
163+ table. set_header ( vec ! [ Cell :: new( contract_id. to_string( ) ) . fg( Color :: Magenta ) ] ) ;
149164 table. add_row ( vec ! [
150165 Cell :: new( "Function Name" ) . add_attribute( Attribute :: Bold ) ,
151166 Cell :: new( "Min" ) . add_attribute( Attribute :: Bold ) ,
0 commit comments