Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Commit

Permalink
feat(fee): adding a slope paramters to the os resources
Browse files Browse the repository at this point in the history
  • Loading branch information
noaov1 committed Jan 27, 2024
1 parent 9575e81 commit 8765e6f
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 36 deletions.
4 changes: 2 additions & 2 deletions crates/blockifier/src/execution/entry_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,14 +299,14 @@ impl EntryPointExecutionContext {
&mut self,
validate_call_info: &Option<CallInfo>,
tx_type: &TransactionType,
_calldata_length: usize,
calldata_length: usize,
) -> usize {
let validate_steps = validate_call_info
.as_ref()
.map(|call_info| call_info.vm_resources.n_steps)
.unwrap_or_default();

let overhead_steps = OS_RESOURCES.resources_for_tx_type(tx_type).n_steps;
let overhead_steps = OS_RESOURCES.resources_for_tx_type(tx_type, calldata_length).n_steps;
self.subtract_steps(validate_steps + overhead_steps)
}

Expand Down
3 changes: 2 additions & 1 deletion crates/blockifier/src/fee/gas_usage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,8 @@ pub fn estimate_minimal_l1_gas(
tx: &AccountTransaction,
) -> TransactionPreValidationResult<GasAndBlobGasUsages> {
// TODO(Dori, 1/8/2023): Give names to the constant VM step estimates and regression-test them.
let os_steps_for_type = OS_RESOURCES.resources_for_tx_type(&tx.tx_type()).n_steps;
let os_steps_for_type =
OS_RESOURCES.resources_for_tx_type(&tx.tx_type(), tx.calldata_length()).n_steps;
let gas_cost: usize = match tx {
// We consider the following state changes: sender balance update (storage update) + nonce
// increment (contract modification) (we exclude the sequencer balance update and the ERC20
Expand Down
83 changes: 61 additions & 22 deletions crates/blockifier/src/fee/os_resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,38 +229,77 @@ fn os_resources() -> serde_json::Value {
},
"execute_txs_inner": {
"Declare": {
"builtin_instance_counter": {
"pedersen_builtin": 15,
"range_check_builtin": 63
"Constant": {
"builtin_instance_counter": {
"pedersen_builtin": 15,
"range_check_builtin": 63
},
"n_memory_holes": 66,
"n_steps": 2843
},
"n_memory_holes": 66,
"n_steps": 2843
"Slope": {
"builtin_instance_counter": {
"pedersen_builtin": 0,
"range_check_builtin": 0
},
"n_memory_holes": 0,
"n_steps": 0
}
},
"DeployAccount": {
"builtin_instance_counter": {
"pedersen_builtin": 23,
"range_check_builtin": 83
"Constant": {
"builtin_instance_counter": {
"pedersen_builtin": 23,
"range_check_builtin": 83
},
"n_memory_holes": 82,
"n_steps": 3798
},
"Slope":{
"builtin_instance_counter": {
"pedersen_builtin": 0,
"range_check_builtin": 0
},
"n_memory_holes": 0,
"n_steps": 0
},
"n_memory_holes": 82,
"n_steps": 3798
},
"InvokeFunction": {
"builtin_instance_counter": {
"pedersen_builtin": 16,
"range_check_builtin": 80
"Constant": {
"builtin_instance_counter": {
"pedersen_builtin": 16,
"range_check_builtin": 80
},
"n_memory_holes": 68,
"n_steps": 3549
},
"n_memory_holes": 68,
"n_steps": 3549
"Slope": {
"builtin_instance_counter": {
"pedersen_builtin": 0,
"range_check_builtin": 0
},
"n_memory_holes": 0,
"n_steps": 0
}
},
"L1Handler": {
"builtin_instance_counter": {
"pedersen_builtin": 11,
"range_check_builtin": 17
"Constant": {
"builtin_instance_counter": {
"pedersen_builtin": 11,
"range_check_builtin": 17
},
"n_memory_holes": 0,
"n_steps": 1157
},
"n_memory_holes": 0,
"n_steps": 1157
"Slope": {
"builtin_instance_counter": {
"pedersen_builtin": 0,
"range_check_builtin": 0
},
"n_memory_holes": 0,
"n_steps": 0
}
}
}
}
)
})
}
51 changes: 44 additions & 7 deletions crates/blockifier/src/fee/os_usage.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,71 @@
use std::collections::HashMap;
use std::str::FromStr;

use cairo_vm::vm::runners::cairo_runner::ExecutionResources as VmExecutionResources;
use serde::Deserialize;
use strum_macros::EnumIter;

use crate::execution::deprecated_syscalls::hint_processor::SyscallCounter;
use crate::execution::deprecated_syscalls::DeprecatedSyscallSelector;
use crate::fee::os_resources::OS_RESOURCES;
use crate::transaction::errors::TransactionExecutionError;
use crate::transaction::errors::{ParseError, TransactionExecutionError};
use crate::transaction::transaction_types::TransactionType;

#[cfg(test)]
#[path = "os_usage_test.rs"]
pub mod test;

#[derive(Clone, Copy, Debug, Deserialize, EnumIter, Eq, Hash, PartialEq)]
pub enum ResourcesRole {
Constant,
Slope,
}

impl FromStr for ResourcesRole {
type Err = ParseError;

fn from_str(resources_role: &str) -> Result<Self, Self::Err> {
match resources_role {
"Constant" => Ok(ResourcesRole::Constant),
"Slope" => Ok(ResourcesRole::Slope),
unknown_resources_role => {
Err(ParseError::UnknownTransactionType(unknown_resources_role.to_string()))
}
}
}
}

#[derive(Debug, Deserialize)]
pub struct OsResources {
// Mapping from every syscall to its execution resources in the OS (e.g., amount of Cairo
// steps).
execute_syscalls: HashMap<DeprecatedSyscallSelector, VmExecutionResources>,
// Mapping from every transaction to its extra execution resources in the OS,
// i.e., resources that don't count during the execution itself.
execute_txs_inner: HashMap<TransactionType, VmExecutionResources>,
execute_txs_inner: HashMap<TransactionType, HashMap<ResourcesRole, VmExecutionResources>>,
}

impl OsResources {
pub fn resources_for_tx_type(&self, tx_type: &TransactionType) -> &VmExecutionResources {
fn resources_for_tx_type_by_resource_role(
&self,
tx_type: &TransactionType,
resources_role: &ResourcesRole,
) -> &VmExecutionResources {
self.execute_txs_inner
.get(tx_type)
.unwrap_or_else(|| panic!("should contain transaction type '{tx_type:?}'."))
.get(resources_role)
.unwrap_or_else(|| panic!("should contain constant resources '{resources_role:?}'."))
}

pub fn resources_for_tx_type(
&self,
tx_type: &TransactionType,
calldata_length: usize,
) -> VmExecutionResources {
self.resources_for_tx_type_by_resource_role(tx_type, &ResourcesRole::Constant)
+ &(OS_RESOURCES.resources_for_tx_type_by_resource_role(tx_type, &ResourcesRole::Slope)
* calldata_length)
}
}

Expand All @@ -36,9 +74,8 @@ impl OsResources {
pub fn get_additional_os_resources(
syscall_counter: &SyscallCounter,
tx_type: TransactionType,
_calldata_length: usize,
calldata_length: usize,
) -> Result<VmExecutionResources, TransactionExecutionError> {
// TODO(Noa, 21/01/24): Use calldata_length.
let mut os_additional_vm_resources = VmExecutionResources::default();
for (syscall_selector, count) in syscall_counter {
let syscall_resources =
Expand All @@ -52,6 +89,6 @@ pub fn get_additional_os_resources(
// i.e., the resources of the Starknet OS function `execute_transactions_inner`.
// Also adds the resources needed for the fee transfer execution, performed in the end·
// of every transaction.
let os_resources = OS_RESOURCES.resources_for_tx_type(&tx_type);
Ok(&os_additional_vm_resources + os_resources)
let os_resources = OS_RESOURCES.resources_for_tx_type(&tx_type, calldata_length);
Ok(&os_additional_vm_resources + &os_resources)
}
6 changes: 3 additions & 3 deletions crates/blockifier/src/fee/os_usage_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ fn test_resource_name_consistency() {
builtin_runner::POSEIDON_BUILTIN_NAME,
builtin_runner::SEGMENT_ARENA_BUILTIN_NAME,
]);
for resources in
OS_RESOURCES.execute_syscalls.values().chain(OS_RESOURCES.execute_txs_inner.values())
{
let tx_resources =
OS_RESOURCES.execute_txs_inner.values().flat_map(|resources| resources.values());
for resources in OS_RESOURCES.execute_syscalls.values().chain(tx_resources) {
for builtin_name in resources.builtin_instance_counter.keys() {
assert!(known_builtin_names.contains(builtin_name.as_str()));
}
Expand Down
2 changes: 1 addition & 1 deletion crates/blockifier/src/transaction/account_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ impl AccountTransaction {
}
}

fn calldata_length(&self) -> usize {
pub fn calldata_length(&self) -> usize {
match self {
Self::Declare(_tx) => 0,
Self::DeployAccount(tx) => tx.constructor_calldata().0.len(),
Expand Down
2 changes: 2 additions & 0 deletions crates/blockifier/src/transaction/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ pub enum TransactionPreValidationError {

#[derive(Debug, Error)]
pub enum ParseError {
#[error("Unsupported resources role: {0}")]
UnknownResourcesRole(String),
#[error("Unsupported transaction type: {0}")]
UnknownTransactionType(String),
}

0 comments on commit 8765e6f

Please sign in to comment.