-
Notifications
You must be signed in to change notification settings - Fork 177
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added resource usage estimation (#605)
<!-- Reference any GitHub issues resolved by this PR --> Closes #249 Closes #362 ## Introduced changes <!-- A brief description of the changes --> - added gas estimation ## Breaking changes <!-- List of all breaking changes, if applicable --> - `call_contract`, `deploy`, and `deploy_at` Cheatnet API changed ## Checklist <!-- Make sure all of these are complete --> - [x] Linked relevant issue - [ ] Updated relevant documentation - [x] Added relevant tests - [x] Performed self-review of the code - [ ] Added changes to `CHANGELOG.md` --------- Co-authored-by: Arcticae <tomekgsd@gmail.com>
- Loading branch information
Showing
19 changed files
with
288 additions
and
72 deletions.
There are no files selected for viewing
This file contains 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 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 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,30 @@ | ||
use std::collections::HashMap; | ||
|
||
use blockifier::{ | ||
abi::constants, block_context::BlockContext, execution::entry_point::ExecutionResources, | ||
fee::fee_utils::calculate_l1_gas_by_vm_usage, transaction::objects::ResourcesMapping, | ||
}; | ||
use cairo_vm::vm::runners::cairo_runner::ExecutionResources as VmExecutionResources; | ||
|
||
#[allow(clippy::module_name_repetitions)] | ||
#[must_use] | ||
pub fn gas_from_execution_resources( | ||
block_context: &BlockContext, | ||
resources: &ExecutionResources, | ||
) -> f64 { | ||
let resource_mapping = vm_execution_resources_to_resource_mapping(&resources.vm_resources); | ||
calculate_l1_gas_by_vm_usage(block_context, &resource_mapping) | ||
.expect("Calculating gas failed, some resources were not included.") | ||
} | ||
|
||
#[must_use] | ||
fn vm_execution_resources_to_resource_mapping( | ||
execution_resources: &VmExecutionResources, | ||
) -> ResourcesMapping { | ||
let mut map = HashMap::from([( | ||
constants::N_STEPS_RESOURCE.to_string(), | ||
execution_resources.n_steps, | ||
)]); | ||
map.extend(execution_resources.builtin_instance_counter.clone()); | ||
ResourcesMapping(map) | ||
} |
This file contains 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 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 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 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.