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

Commit

Permalink
Replace PyBouncerInfo's additional_os_resources fiels to tx_weights f…
Browse files Browse the repository at this point in the history
…ield and _calculate_tx_weights in Rust instead of in Python
  • Loading branch information
ayeletstarkware committed Jan 22, 2024
1 parent 4a2a92d commit 6b0b81b
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 9 deletions.
46 changes: 46 additions & 0 deletions crates/blockifier/src/transaction/transaction_utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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 crate::abi::constants;
Expand Down Expand Up @@ -73,3 +74,48 @@ pub fn verify_contract_class_version(
}
}
}

// TODO(Ayelet, 01/02/2024): Move to VmExecutionResourcesWrapper when merged.
pub fn to_dict(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
}

fn add_counters(x: HashMap<String, usize>, y: HashMap<String, usize>) -> HashMap<String, usize> {
let mut result = HashMap::new();

// Insert values from x into result
for (key, value) in x {
result.insert(key.clone(), value);
}

// Add values from y to result
for (key, value) in y {
let entry = result.entry(key.clone()).or_insert(0);
*entry += value;
}

result
}

pub fn calculate_tx_weights(
additional_os_resources: VmExecutionResources,
actual_resources: HashMap<String, usize>,
message_segment_length: usize,
) -> HashMap<String, usize> {
let mut tx_weights: HashMap<String, usize> = HashMap::new();
let mut cairo_resource_usage: HashMap<String, usize> = actual_resources;
if let Some(&value) = cairo_resource_usage.get("l1_gas_usage") {
tx_weights.insert("gas_weight".to_string(), value);
cairo_resource_usage.remove("l1_gas_usage");
}
let os_cairo_usage: HashMap<String, usize> = to_dict(additional_os_resources);
let cairo_usage = add_counters(cairo_resource_usage, os_cairo_usage);
tx_weights.extend(cairo_usage);
tx_weights.insert("message_segment_length".to_string(), message_segment_length);
tx_weights
}
Original file line number Diff line number Diff line change
Expand Up @@ -189,5 +189,5 @@ pub struct PyBouncerInfo {
// The number of felts needed to store the state diff.
pub state_diff_size: usize,
#[pyo3(get)]
pub additional_os_resources: PyVmExecutionResources,
pub tx_weights: HashMap<String, usize>,
}
19 changes: 11 additions & 8 deletions crates/native_blockifier/src/transaction_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ 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 All @@ -22,9 +23,7 @@ use crate::errors::{NativeBlockifierError, NativeBlockifierResult};
use crate::py_block_executor::{into_block_context, PyGeneralConfig};
use crate::py_state_diff::{PyBlockInfo, PyStateDiff};
use crate::py_transaction::py_tx;
use crate::py_transaction_execution_info::{
PyBouncerInfo, PyTransactionExecutionInfo, PyVmExecutionResources,
};
use crate::py_transaction_execution_info::{PyBouncerInfo, PyTransactionExecutionInfo};
use crate::py_utils::PyFelt;

pub struct TransactionExecutor<S: StateReader> {
Expand Down Expand Up @@ -85,6 +84,7 @@ impl<S: StateReader> TransactionExecutor<S> {
.map_err(NativeBlockifierError::from);
match tx_execution_result {
Ok(tx_execution_info) => {
let actual_resources = tx_execution_info.actual_resources.0.clone();
// TODO(Elin, 01/06/2024): consider traversing the calls to collect data once.
tx_executed_class_hashes.extend(tx_execution_info.get_executed_class_hashes());
tx_visited_storage_entries.extend(tx_execution_info.get_visited_storage_entries());
Expand All @@ -100,11 +100,14 @@ impl<S: StateReader> TransactionExecutor<S> {
&self.visited_storage_entries,
&tx_visited_storage_entries,
)?;
let py_bouncer_info = PyBouncerInfo {
message_segment_length: 0,
state_diff_size: 0,
additional_os_resources: PyVmExecutionResources::from(additional_os_resources),
};
let message_segment_length = 0;
let tx_weights = calculate_tx_weights(
additional_os_resources.clone(),
actual_resources,
message_segment_length,
);
let py_bouncer_info =
PyBouncerInfo { message_segment_length, state_diff_size: 0, 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 6b0b81b

Please sign in to comment.