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 31, 2024
1 parent 9575e81 commit f3b879e
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 35 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
85 changes: 63 additions & 22 deletions crates/blockifier/src/fee/os_resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,40 +227,81 @@ fn os_resources() -> serde_json::Value {
"n_steps": 89
}
},
// For each transaction the OS uses a constant amount of VM resources, and an
// additional variable amount that depends on the calldata length.
"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
"calldata_factor": {
"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
},
"calldata_factor":{
"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
"calldata_factor": {
"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
"calldata_factor": {
"builtin_instance_counter": {
"pedersen_builtin": 0,
"range_check_builtin": 0
},
"n_memory_holes": 0,
"n_steps": 0
}
}
}
}
)
})
}
26 changes: 20 additions & 6 deletions crates/blockifier/src/fee/os_usage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,46 @@ use crate::transaction::transaction_types::TransactionType;
#[path = "os_usage_test.rs"]
pub mod test;

#[derive(Debug, Deserialize)]
pub struct ResourcesVector {
pub constant: VmExecutionResources,
pub calldata_factor: VmExecutionResources,
}

#[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, ResourcesVector>,
}

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

pub fn resources_for_tx_type(
&self,
tx_type: &TransactionType,
calldata_length: usize,
) -> VmExecutionResources {
let resources_vector = self.resources_vector_for_tx_type(tx_type);
&resources_vector.constant + &(&(resources_vector.calldata_factor) * calldata_length)
}
}

/// Calculates the additional resources needed for the OS to run the given syscalls;
/// i.e., the resources of the Starknet OS function `execute_syscalls`.
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 +66,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)
}
14 changes: 11 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,17 @@ 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 mut os_resources = OS_RESOURCES
.execute_txs_inner
.values()
.flat_map(|resources_vector| {
vec![&resources_vector.constant, &resources_vector.calldata_factor]
})
.collect::<Vec<_>>();
os_resources.extend(OS_RESOURCES.execute_syscalls.values());

for resources in os_resources.iter() {
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

0 comments on commit f3b879e

Please sign in to comment.