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

Commit

Permalink
refactor(native_blockifier): flat fields in bouncerinfo
Browse files Browse the repository at this point in the history
  • Loading branch information
ayeletstarkware committed Jan 29, 2024
1 parent 0a5416b commit c5f4376
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 64 deletions.
38 changes: 0 additions & 38 deletions crates/blockifier/src/transaction/transaction_utils.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::collections::HashMap;

use cairo_vm::vm::runners::builtin_runner::SEGMENT_ARENA_BUILTIN_NAME;
use cairo_vm::vm::runners::cairo_runner::ExecutionResources as VmExecutionResources;
use starknet_api::transaction::TransactionVersion;

use super::objects::GasAndBlobGasUsages;
Expand All @@ -13,7 +12,6 @@ use crate::fee::os_usage::get_additional_os_resources;
use crate::transaction::errors::TransactionExecutionError;
use crate::transaction::objects::{ResourcesMapping, TransactionExecutionResult};
use crate::transaction::transaction_types::TransactionType;
use crate::utils::merge_hashmaps;

/// Calculates the total resources needed to include the transaction in a Starknet block as
/// most-recent (recent w.r.t. application on the given state).
Expand Down Expand Up @@ -82,39 +80,3 @@ pub fn verify_contract_class_version(
}
}
}

// TODO(Ayelet, 01/02/2024): Move to VmExecutionResourcesWrapper when merged.
pub fn vm_execution_resources_to_hash_map(
execution_resources: VmExecutionResources,
) -> HashMap<String, usize> {
let mut result = execution_resources.builtin_instance_counter.clone();
result.insert(
String::from("n_steps"),
execution_resources.n_steps + execution_resources.n_memory_holes,
);
result
}

type ResourcesMap = HashMap<String, usize>;

// TODO(Arni, 24/01/2024): Add state_diff_size to tx_weights
pub fn calculate_tx_weights(
additional_os_resources: VmExecutionResources,
actual_resources: ResourcesMap,
message_segment_length: usize,
) -> Result<ResourcesMap, TransactionExecutionError> {
let mut tx_weights: HashMap<String, usize> = HashMap::new();
let mut cairo_resource_usage: HashMap<String, usize> = actual_resources;
let value = cairo_resource_usage.remove("l1_gas_usage").ok_or(
TransactionExecutionError::InvalidTransactionExecutionInfo {
field: "l1_gas_usage".to_string(),
},
)?;
tx_weights.insert("gas_weight".to_string(), value);
let os_cairo_usage: HashMap<String, usize> =
vm_execution_resources_to_hash_map(additional_os_resources);
let cairo_usage = merge_hashmaps(&cairo_resource_usage, &os_cairo_usage);
tx_weights.extend(cairo_usage);
tx_weights.insert("message_segment_length".to_string(), message_segment_length);
Ok(tx_weights)
}
17 changes: 0 additions & 17 deletions crates/blockifier/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use std::collections::HashMap;
use std::hash::Hash;
use std::ops::{Add, AddAssign};

#[cfg(test)]
#[path = "utils_test.rs"]
Expand All @@ -21,18 +19,3 @@ where
pub const fn const_max(a: u128, b: u128) -> u128 {
[a, b][(a < b) as usize]
}

pub fn merge_hashmaps<K, V>(x: &HashMap<K, V>, y: &HashMap<K, V>) -> HashMap<K, V>
where
K: Hash + Eq + Clone,
V: Add<Output = V> + AddAssign + Default + Clone,
{
let mut result = x.clone();
for (key, value) in y {
result
.entry(key.clone())
.and_modify(|v| v.add_assign(value.clone()))
.or_insert(value.clone());
}
result
}
63 changes: 59 additions & 4 deletions crates/native_blockifier/src/py_transaction_execution_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use std::collections::{HashMap, HashSet};

use blockifier::execution::call_info::{CallInfo, OrderedEvent, OrderedL2ToL1Message};
use blockifier::execution::entry_point::CallType;
use blockifier::transaction::objects::TransactionExecutionInfo;
use blockifier::transaction::errors::TransactionExecutionError;
use blockifier::transaction::objects::{TransactionExecutionInfo, TransactionExecutionResult};
use cairo_vm::vm::runners::cairo_runner::ExecutionResources as VmExecutionResources;
use pyo3::prelude::*;
use starknet_api::deprecated_contract_class::EntryPointType;
Expand Down Expand Up @@ -215,16 +216,70 @@ impl From<VmExecutionResources> for PyVmExecutionResources {
}
}

type ResourcesMap = HashMap<String, usize>;

#[pyclass]
#[derive(Clone, Default)]
// TODO(Ayelet, 24/01/2024): Consider remove message_segment_length, state_diff_size.
pub struct PyBouncerInfo {
#[pyo3(get)]
// The number of felts needed to store the state diff.
pub state_diff_size: usize,
#[pyo3(get)]
pub gas_weight: usize,
#[pyo3(get)]
pub range_check_builtin: usize,
#[pyo3(get)]
// The number of felts needed to store L1<>L2 messages.
pub message_segment_length: usize,
#[pyo3(get)]
// The number of felts needed to store the state diff.
pub state_diff_size: usize,
pub n_steps: usize,
#[pyo3(get)]
pub tx_weights: HashMap<String, usize>,
pub pedersen_builtin: usize,
}

impl PyBouncerInfo {
pub fn calculate(
additional_os_resources: VmExecutionResources,
actual_resources: ResourcesMap,
message_segment_length: usize,
state_diff_size: usize,
) -> TransactionExecutionResult<Self> {
let gas_weight = actual_resources.get("l1_gas_usage").ok_or(
TransactionExecutionError::InvalidTransactionExecutionInfo {
field: "l1_gas_usage".to_string(),
},
)?;
let n_steps = additional_os_resources.n_steps
+ additional_os_resources.n_memory_holes
+ actual_resources.get("n_steps").ok_or(
TransactionExecutionError::InvalidTransactionExecutionInfo {
field: "n_steps".to_string(),
},
)?;
let pedersen_builtin = actual_resources.get("pedersen_builtin").ok_or(
TransactionExecutionError::InvalidTransactionExecutionInfo {
field: "pedersen_builtin".to_string(),
},
)? + additional_os_resources
.builtin_instance_counter
.get("pedersen_builtin")
.ok_or(TransactionExecutionError::InvalidTransactionExecutionInfo {
field: "pedersen_builtin".to_string(),
})?;

let range_check_builtin = actual_resources.get("range_check_builtin").ok_or(
TransactionExecutionError::InvalidTransactionExecutionInfo {
field: "range_check_builtin".to_string(),
},
)?;
Ok(Self {
state_diff_size,
gas_weight: *gas_weight,
range_check_builtin: *range_check_builtin,
message_segment_length,
n_steps,
pedersen_builtin,
})
}
}
8 changes: 3 additions & 5 deletions crates/native_blockifier/src/transaction_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use blockifier::state::cached_state::{
use blockifier::state::state_api::{State, StateReader};
use blockifier::transaction::account_transaction::AccountTransaction;
use blockifier::transaction::transaction_execution::Transaction;
use blockifier::transaction::transaction_utils::calculate_tx_weights;
use blockifier::transaction::transactions::{ExecutableTransaction, ValidatableTransaction};
use cairo_vm::vm::runners::builtin_runner::HASH_BUILTIN_NAME;
use cairo_vm::vm::runners::cairo_runner::ExecutionResources as VmExecutionResources;
Expand Down Expand Up @@ -112,15 +111,14 @@ impl<S: StateReader> TransactionExecutor<S> {
&self.visited_storage_entries,
&tx_visited_storage_entries,
)?;
let state_diff_size = 0;
let actual_resources = tx_execution_info.actual_resources.0.clone();
let tx_weights = calculate_tx_weights(
let state_diff_size = 0;
let py_bouncer_info = PyBouncerInfo::calculate(
additional_os_resources,
actual_resources,
message_segment_length,
state_diff_size,
)?;
let py_bouncer_info =
PyBouncerInfo { message_segment_length, state_diff_size, tx_weights };
self.staged_for_commit_state = Some(
transactional_state.stage(tx_executed_class_hashes, tx_visited_storage_entries),
);
Expand Down

0 comments on commit c5f4376

Please sign in to comment.