|
| 1 | +use crate::gas::stats::GasStats; |
| 2 | +use cheatnet::trace_data::{CallTrace, CallTraceNode}; |
| 3 | +use debugging::ContractName as DebuggingContractName; |
| 4 | +use debugging::ContractsDataStore; |
| 5 | +use starknet_api::core::{ClassHash, EntryPointSelector}; |
| 6 | +use starknet_api::execution_resources::GasVector; |
| 7 | +use std::collections::BTreeMap; |
| 8 | + |
| 9 | +type ContractName = String; |
| 10 | +type Selector = String; |
| 11 | + |
| 12 | +#[derive(Debug, Clone)] |
| 13 | +pub struct GasSingleTestInfo { |
| 14 | + pub gas_used: GasVector, |
| 15 | + pub report_data: ReportData, |
| 16 | +} |
| 17 | + |
| 18 | +#[derive(Debug, Clone)] |
| 19 | +pub struct ReportData(BTreeMap<ContractName, ContractInfo>); |
| 20 | + |
| 21 | +#[derive(Debug, Clone, Default)] |
| 22 | +pub struct ContractInfo { |
| 23 | + pub gas_used: GasVector, |
| 24 | + pub functions: BTreeMap<Selector, SelectorReportData>, |
| 25 | +} |
| 26 | + |
| 27 | +#[derive(Debug, Clone, Default)] |
| 28 | +pub struct SelectorReportData { |
| 29 | + pub gas_stats: GasStats, |
| 30 | + pub n_calls: u64, |
| 31 | + pub records: Vec<u64>, |
| 32 | +} |
| 33 | + |
| 34 | +impl GasSingleTestInfo { |
| 35 | + #[must_use] |
| 36 | + pub fn new(gas_used: GasVector) -> Self { |
| 37 | + Self { |
| 38 | + gas_used, |
| 39 | + report_data: ReportData(BTreeMap::new()), |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + #[must_use] |
| 44 | + pub fn new_with_report( |
| 45 | + gas_used: GasVector, |
| 46 | + call_trace: &CallTrace, |
| 47 | + contracts_data: &ContractsDataStore, |
| 48 | + ) -> Self { |
| 49 | + Self::new(gas_used).collect_gas_data(call_trace, contracts_data) |
| 50 | + } |
| 51 | + |
| 52 | + fn collect_gas_data(mut self, trace: &CallTrace, contracts_data: &ContractsDataStore) -> Self { |
| 53 | + let mut stack = trace.nested_calls.clone(); |
| 54 | + |
| 55 | + while let Some(call_trace_node) = stack.pop() { |
| 56 | + if let CallTraceNode::EntryPointCall(call) = call_trace_node { |
| 57 | + let call = call.borrow(); |
| 58 | + let class_hash = call.entry_point.class_hash.expect( |
| 59 | + "class_hash should be set in `fn execute_call_entry_point` in cheatnet", |
| 60 | + ); |
| 61 | + |
| 62 | + let contract_name = get_contract_name(contracts_data, class_hash); |
| 63 | + let selector = get_selector(contracts_data, call.entry_point.entry_point_selector); |
| 64 | + let gas = call |
| 65 | + .gas_report_data |
| 66 | + .as_ref() |
| 67 | + .expect("Gas report data must be updated after test execution") |
| 68 | + .get_gas(); |
| 69 | + |
| 70 | + self.update_entry(contract_name, selector, gas); |
| 71 | + stack.extend(call.nested_calls.clone()); |
| 72 | + } |
| 73 | + } |
| 74 | + self.finalize(); |
| 75 | + self |
| 76 | + } |
| 77 | + |
| 78 | + fn update_entry( |
| 79 | + &mut self, |
| 80 | + contract_name: ContractName, |
| 81 | + selector: Selector, |
| 82 | + gas_used: GasVector, |
| 83 | + ) { |
| 84 | + let contract_info = self.report_data.0.entry(contract_name).or_default(); |
| 85 | + |
| 86 | + if let Some(gas) = contract_info.gas_used.checked_add(gas_used) { |
| 87 | + contract_info.gas_used = gas; |
| 88 | + } |
| 89 | + |
| 90 | + let entry = contract_info.functions.entry(selector).or_default(); |
| 91 | + entry.records.push(gas_used.l2_gas.0); |
| 92 | + entry.n_calls += 1; |
| 93 | + } |
| 94 | + |
| 95 | + fn finalize(&mut self) { |
| 96 | + for contract_info in self.report_data.0.values_mut() { |
| 97 | + for gas_info in contract_info.functions.values_mut() { |
| 98 | + gas_info.gas_stats = GasStats::new(&gas_info.records); |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +fn get_contract_name(contracts_data: &ContractsDataStore, class_hash: ClassHash) -> ContractName { |
| 105 | + contracts_data |
| 106 | + .get_contract_name(&class_hash) |
| 107 | + .cloned() |
| 108 | + .unwrap_or_else(|| DebuggingContractName("forked contract".to_string())) |
| 109 | + .0 |
| 110 | + .clone() |
| 111 | +} |
| 112 | + |
| 113 | +fn get_selector(contracts_data: &ContractsDataStore, selector: EntryPointSelector) -> Selector { |
| 114 | + contracts_data |
| 115 | + .get_selector(&selector) |
| 116 | + .expect("`Selector` should be present") |
| 117 | + .0 |
| 118 | + .clone() |
| 119 | +} |
0 commit comments