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 18, 2024
1 parent 0032f8c commit 46fcafe
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
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
}
2 changes: 2 additions & 0 deletions crates/native_blockifier/src/py_transaction_execution_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,6 @@ pub struct PyBouncerInfo {
pub state_diff_size: usize,
#[pyo3(get)]
pub additional_os_resources: PyVmExecutionResources,
#[pyo3(get)]
pub tx_weights: HashMap<String, usize>,
}
11 changes: 10 additions & 1 deletion 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 Down Expand Up @@ -85,6 +86,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,10 +102,17 @@ impl<S: StateReader> TransactionExecutor<S> {
&self.visited_storage_entries,
&tx_visited_storage_entries,
)?;
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: 0,
message_segment_length,
state_diff_size: 0,
additional_os_resources: PyVmExecutionResources::from(additional_os_resources),
tx_weights,
};

self.staged_for_commit_state = Some(
Expand Down

0 comments on commit 46fcafe

Please sign in to comment.