-
Notifications
You must be signed in to change notification settings - Fork 240
Add gas report logic #3841
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add gas report logic #3841
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9f9b94a
Add gas report logic
ddoktorski 04592cc
Panic on gas addition overflow
ddoktorski cb415bf
Address nit comments
ddoktorski 56c2a81
Remove redundant clone
ddoktorski 70ac89a
Make `report_data` as `Option`
ddoktorski d0f389f
Fix config tests
ddoktorski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| use crate::gas::stats::GasStats; | ||
| use cheatnet::trace_data::{CallTrace, CallTraceNode}; | ||
| use debugging::ContractsDataStore; | ||
| use starknet_api::core::{ClassHash, EntryPointSelector}; | ||
| use starknet_api::execution_resources::GasVector; | ||
| use std::collections::BTreeMap; | ||
|
|
||
| type ContractName = String; | ||
| type Selector = String; | ||
franciszekjob marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| #[derive(Debug, Clone)] | ||
| pub struct SingleTestGasInfo { | ||
| pub gas_used: GasVector, | ||
| pub report_data: Option<ReportData>, | ||
| } | ||
|
|
||
| #[derive(Debug, Clone, Default)] | ||
| pub struct ReportData(BTreeMap<ContractName, ContractInfo>); | ||
cptartur marked this conversation as resolved.
Show resolved
Hide resolved
ddoktorski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| #[derive(Debug, Clone, Default)] | ||
| pub struct ContractInfo { | ||
| pub(super) gas_used: GasVector, | ||
| pub(super) functions: BTreeMap<Selector, SelectorReportData>, | ||
| } | ||
|
|
||
| #[derive(Debug, Clone, Default)] | ||
| pub struct SelectorReportData { | ||
| pub(super) gas_stats: GasStats, | ||
| pub(super) n_calls: u64, | ||
| pub(super) records: Vec<u64>, | ||
| } | ||
|
|
||
| impl SingleTestGasInfo { | ||
| #[must_use] | ||
| pub(crate) fn new(gas_used: GasVector) -> Self { | ||
| Self { | ||
| gas_used, | ||
| report_data: None, | ||
| } | ||
| } | ||
|
|
||
| pub(crate) fn get_with_report_data( | ||
| self, | ||
| trace: &CallTrace, | ||
| contracts_data: &ContractsDataStore, | ||
| ) -> Self { | ||
| let mut report_data = ReportData::default(); | ||
| let mut stack = trace.nested_calls.clone(); | ||
|
|
||
| while let Some(call_trace_node) = stack.pop() { | ||
| if let CallTraceNode::EntryPointCall(call) = call_trace_node { | ||
| let call = call.borrow(); | ||
| let class_hash = call.entry_point.class_hash.expect( | ||
| "class_hash should be set in `fn execute_call_entry_point` in cheatnet", | ||
| ); | ||
|
|
||
| let contract_name = get_contract_name(contracts_data, class_hash); | ||
| let selector = get_selector(contracts_data, call.entry_point.entry_point_selector); | ||
| let gas = call | ||
| .gas_report_data | ||
| .as_ref() | ||
| .expect("Gas report data must be updated after test execution") | ||
| .get_gas(); | ||
|
|
||
| report_data.update_entry(contract_name, selector, gas); | ||
| stack.extend(call.nested_calls.clone()); | ||
| } | ||
| } | ||
| report_data.finalize(); | ||
|
|
||
| Self { | ||
| gas_used: self.gas_used, | ||
| report_data: Some(report_data), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl ReportData { | ||
| fn update_entry( | ||
| &mut self, | ||
| contract_name: ContractName, | ||
| selector: Selector, | ||
| gas_used: GasVector, | ||
| ) { | ||
| let contract_info = self.0.entry(contract_name).or_default(); | ||
|
|
||
| let current_gas = contract_info.gas_used; | ||
| contract_info.gas_used = current_gas.checked_add(gas_used).unwrap_or_else(|| { | ||
| panic!("Gas addition overflow when adding {gas_used:?} to {current_gas:?}.") | ||
| }); | ||
|
|
||
| let entry = contract_info.functions.entry(selector).or_default(); | ||
| entry.records.push(gas_used.l2_gas.0); | ||
| entry.n_calls += 1; | ||
| } | ||
|
|
||
| fn finalize(&mut self) { | ||
| for contract_info in self.0.values_mut() { | ||
| for gas_info in contract_info.functions.values_mut() { | ||
| gas_info.gas_stats = GasStats::new(&gas_info.records); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn get_contract_name(contracts_data: &ContractsDataStore, class_hash: ClassHash) -> ContractName { | ||
| contracts_data | ||
| .get_contract_name(&class_hash) | ||
| .map_or("forked contract", |name| name.0.as_str()) | ||
| .to_string() | ||
| } | ||
|
|
||
| fn get_selector(contracts_data: &ContractsDataStore, selector: EntryPointSelector) -> Selector { | ||
| contracts_data | ||
| .get_selector(&selector) | ||
| .expect("`Selector` should be present") | ||
| .0 | ||
| .clone() | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.