Skip to content

Commit 9c121cc

Browse files
committed
Make report_data as Option
1 parent 12b712a commit 9c121cc

File tree

2 files changed

+26
-24
lines changed

2 files changed

+26
-24
lines changed

crates/forge-runner/src/gas/report.rs

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,44 +11,40 @@ type Selector = String;
1111
#[derive(Debug, Clone)]
1212
pub struct SingleTestGasInfo {
1313
pub gas_used: GasVector,
14-
pub report_data: ReportData,
14+
pub report_data: Option<ReportData>,
1515
}
1616

1717
#[derive(Debug, Clone, Default)]
1818
pub struct ReportData(BTreeMap<ContractName, ContractInfo>);
1919

2020
#[derive(Debug, Clone, Default)]
2121
pub struct ContractInfo {
22-
pub gas_used: GasVector,
23-
pub functions: BTreeMap<Selector, SelectorReportData>,
22+
pub(super) gas_used: GasVector,
23+
pub(super) functions: BTreeMap<Selector, SelectorReportData>,
2424
}
2525

2626
#[derive(Debug, Clone, Default)]
2727
pub struct SelectorReportData {
28-
pub gas_stats: GasStats,
29-
pub n_calls: u64,
30-
pub records: Vec<u64>,
28+
pub(super) gas_stats: GasStats,
29+
pub(super) n_calls: u64,
30+
pub(super) records: Vec<u64>,
3131
}
3232

3333
impl SingleTestGasInfo {
3434
#[must_use]
35-
pub fn new(gas_used: GasVector) -> Self {
35+
pub(crate) fn new(gas_used: GasVector) -> Self {
3636
Self {
3737
gas_used,
38-
report_data: ReportData::default(),
38+
report_data: None,
3939
}
4040
}
4141

42-
#[must_use]
43-
pub fn new_with_report(
44-
gas_used: GasVector,
45-
call_trace: &CallTrace,
42+
pub(crate) fn get_with_report_data(
43+
self,
44+
trace: &CallTrace,
4645
contracts_data: &ContractsDataStore,
4746
) -> Self {
48-
Self::new(gas_used).collect_gas_data(call_trace, contracts_data)
49-
}
50-
51-
fn collect_gas_data(mut self, trace: &CallTrace, contracts_data: &ContractsDataStore) -> Self {
47+
let mut report_data = ReportData::default();
5248
let mut stack = trace.nested_calls.clone();
5349

5450
while let Some(call_trace_node) = stack.pop() {
@@ -66,21 +62,27 @@ impl SingleTestGasInfo {
6662
.expect("Gas report data must be updated after test execution")
6763
.get_gas();
6864

69-
self.update_entry(contract_name, selector, gas);
65+
report_data.update_entry(contract_name, selector, gas);
7066
stack.extend(call.nested_calls.clone());
7167
}
7268
}
73-
self.finalize();
74-
self
69+
report_data.finalize();
70+
71+
Self {
72+
gas_used: self.gas_used,
73+
report_data: Some(report_data),
74+
}
7575
}
76+
}
7677

78+
impl ReportData {
7779
fn update_entry(
7880
&mut self,
7981
contract_name: ContractName,
8082
selector: Selector,
8183
gas_used: GasVector,
8284
) {
83-
let contract_info = self.report_data.0.entry(contract_name).or_default();
85+
let contract_info = self.0.entry(contract_name).or_default();
8486

8587
let current_gas = contract_info.gas_used;
8688
contract_info.gas_used = current_gas.checked_add(gas_used).unwrap_or_else(|| {
@@ -93,7 +95,7 @@ impl SingleTestGasInfo {
9395
}
9496

9597
fn finalize(&mut self) {
96-
for contract_info in self.report_data.0.values_mut() {
98+
for contract_info in self.0.values_mut() {
9799
for gas_info in contract_info.functions.values_mut() {
98100
gas_info.gas_stats = GasStats::new(&gas_info.records);
99101
}

crates/forge-runner/src/test_case_summary.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -302,14 +302,14 @@ impl TestCaseSummary<Single> {
302302
&fork_data,
303303
);
304304

305+
let gas_info = SingleTestGasInfo::new(gas_used);
305306
let gas_info = if gas_report_enabled {
306-
SingleTestGasInfo::new_with_report(
307-
gas_used,
307+
gas_info.get_with_report_data(
308308
&call_trace.borrow(),
309309
&ContractsDataStore::new(contracts_data, &fork_data),
310310
)
311311
} else {
312-
SingleTestGasInfo::new(gas_used)
312+
gas_info
313313
};
314314

315315
match status {

0 commit comments

Comments
 (0)