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

Commit

Permalink
chore(native_blockifier): return the block weights to python (#1895)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoni-Starkware authored May 19, 2024
1 parent e2fd958 commit 4c760c7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 19 deletions.
11 changes: 6 additions & 5 deletions crates/blockifier/src/blockifier/transaction_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use starknet_api::core::ClassHash;
use thiserror::Error;

use crate::blockifier::config::TransactionExecutorConfig;
use crate::bouncer::{Bouncer, BouncerConfig};
use crate::bouncer::{Bouncer, BouncerConfig, BouncerWeights};
use crate::context::BlockContext;
use crate::execution::call_info::CallInfo;
use crate::fee::actual_cost::TransactionReceipt;
Expand Down Expand Up @@ -183,11 +183,12 @@ impl<S: StateReader> TransactionExecutor<S> {
Ok((validate_call_info, tx_receipt))
}

/// Returns the state diff and a list of contract class hash with the corresponding list of
/// visited segment values.
/// Returns the state diff, a list of contract class hash with the corresponding list of
/// visited segment values and the block weights.
pub fn finalize(
&mut self,
) -> TransactionExecutorResult<(CommitmentStateDiff, VisitedSegmentsMapping)> {
) -> TransactionExecutorResult<(CommitmentStateDiff, VisitedSegmentsMapping, BouncerWeights)>
{
// Get the visited segments of each contract class.
// This is done by taking all the visited PCs of each contract, and compress them to one
// representative for each visited segment.
Expand All @@ -202,6 +203,6 @@ impl<S: StateReader> TransactionExecutor<S> {
.collect::<TransactionExecutorResult<_>>()?;

log::debug!("Final block weights: {:?}.", self.bouncer.get_accumulated_weights());
Ok((self.state.to_state_diff(), visited_segments))
Ok((self.state.to_state_diff(), visited_segments, *self.bouncer.get_accumulated_weights()))
}
}
4 changes: 3 additions & 1 deletion crates/blockifier/src/bouncer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::collections::{HashMap, HashSet};
use cairo_vm::serde::deserialize_program::BuiltinName;
use cairo_vm::vm::runners::builtin_runner::HASH_BUILTIN_NAME;
use cairo_vm::vm::runners::cairo_runner::ExecutionResources;
use serde::Deserialize;
use serde::{Deserialize, Serialize};
use starknet_api::core::ClassHash;

use crate::blockifier::transaction_executor::{
Expand Down Expand Up @@ -61,6 +61,7 @@ impl BouncerConfig {
derive_more::Sub,
Deserialize,
PartialEq,
Serialize,
)]
/// Represents the execution resources counted throughout block creation.
pub struct BouncerWeights {
Expand Down Expand Up @@ -108,6 +109,7 @@ impl BouncerWeights {
derive_more::Sub,
Deserialize,
PartialEq,
Serialize,
)]
pub struct BuiltinCount {
pub bitwise: usize,
Expand Down
30 changes: 17 additions & 13 deletions crates/native_blockifier/src/py_block_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,9 @@ impl PyBlockExecutor {
tx_type,
);

// Convert to PyBytes:
let raw_tx_execution_info = Python::with_gil(|py| {
let bytes_tx_execution_info = typed_tx_execution_info.serialize();
PyBytes::new(py, &bytes_tx_execution_info).into()
});

Ok(raw_tx_execution_info)
// Serialize and convert to PyBytes.
let serialized_tx_execution_info = typed_tx_execution_info.serialize();
Ok(Python::with_gil(|py| PyBytes::new(py, &serialized_tx_execution_info).into()))
}

/// Executes the given transactions on the Blockifier state.
Expand Down Expand Up @@ -275,21 +271,29 @@ impl PyBlockExecutor {
})
}

/// Returns the state diff and a list of contract class hash with the corresponding list of
/// visited segment values.
pub fn finalize(&mut self) -> NativeBlockifierResult<(PyStateDiff, PyVisitedSegmentsMapping)> {
/// Returns the state diff, a list of contract class hash with the corresponding list of
/// visited segment values and the block weights.
pub fn finalize(
&mut self,
) -> NativeBlockifierResult<(PyStateDiff, PyVisitedSegmentsMapping, Py<PyBytes>)> {
log::debug!("Finalizing execution...");
let (commitment_state_diff, visited_pcs) = self.tx_executor().finalize()?;
let (commitment_state_diff, visited_pcs, block_weights) = self.tx_executor().finalize()?;
let visited_pcs = visited_pcs
.into_iter()
.map(|(class_hash, class_visited_pcs_vec)| {
(PyFelt::from(class_hash), class_visited_pcs_vec)
})
.collect();
let finalized_state = (PyStateDiff::from(commitment_state_diff), visited_pcs);
let py_state_diff = PyStateDiff::from(commitment_state_diff);

let serialized_block_weights =
serde_json::to_vec(&block_weights).expect("Failed serializing bouncer weights.");
let raw_block_weights =
Python::with_gil(|py| PyBytes::new(py, &serialized_block_weights).into());

log::debug!("Finalized execution.");

Ok(finalized_state)
Ok((py_state_diff, visited_pcs, raw_block_weights))
}

// Storage Alignment API.
Expand Down

0 comments on commit 4c760c7

Please sign in to comment.