Skip to content

Commit

Permalink
refactor(native_blockifier, starknet_consensus_orchestrator): share e…
Browse files Browse the repository at this point in the history
…xecution objects
  • Loading branch information
Yael-Starkware committed Feb 9, 2025
1 parent 4a0c186 commit 00aeb44
Show file tree
Hide file tree
Showing 13 changed files with 111 additions and 139 deletions.
14 changes: 12 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ members = [
"crates/papyrus_storage",
"crates/papyrus_sync",
"crates/papyrus_test_utils",
"crates/shared_execution_objects",
"crates/starknet_api",
"crates/starknet_batcher",
"crates/starknet_batcher_types",
Expand Down Expand Up @@ -218,6 +219,7 @@ serde_repr = "0.1.19"
serde_yaml = "0.9.16"
sha2 = "0.10.8"
sha3 = "0.10.8"
shared_execution_objects = { path = "crates/shared_execution_objects", version = "0.0.0" }
simple_logger = "4.0.0"
starknet-core = "0.6.0"
starknet-crypto = "0.7.1"
Expand Down
2 changes: 1 addition & 1 deletion crates/native_blockifier/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ papyrus_state_reader.workspace = true
papyrus_storage.workspace = true
pyo3 = { workspace = true, features = ["hashbrown", "num-bigint"] }
pyo3-log.workspace = true
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true, features = ["arbitrary_precision"] }
shared_execution_objects.workspace = true
starknet-types-core.workspace = true
starknet_api.workspace = true
starknet_sierra_multicompile.workspace = true
Expand Down
79 changes: 10 additions & 69 deletions crates/native_blockifier/src/py_block_executor.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
#![allow(non_local_definitions)]

use std::collections::HashMap;

use blockifier::abi::constants as abi_constants;
use blockifier::blockifier::config::{ContractClassManagerConfig, TransactionExecutorConfig};
use blockifier::blockifier::transaction_executor::{
BlockExecutionSummary,
Expand All @@ -12,20 +9,15 @@ use blockifier::blockifier::transaction_executor::{
use blockifier::blockifier_versioned_constants::VersionedConstants;
use blockifier::bouncer::BouncerConfig;
use blockifier::context::{BlockContext, ChainInfo, FeeTokenAddresses};
use blockifier::execution::call_info::CallInfo;
use blockifier::fee::receipt::TransactionReceipt;
use blockifier::state::contract_class_manager::ContractClassManager;
use blockifier::transaction::objects::{ExecutionResourcesTraits, TransactionExecutionInfo};
use blockifier::transaction::transaction_execution::Transaction;
use papyrus_state_reader::papyrus_state::PapyrusReader;
use pyo3::prelude::*;
use pyo3::types::{PyBytes, PyList};
use pyo3::{FromPyObject, PyAny, Python};
use serde::Serialize;
use shared_execution_objects::objects::CentralTransactionExecutionInfo;
use starknet_api::block::BlockNumber;
use starknet_api::core::{ChainId, ContractAddress};
use starknet_api::execution_resources::GasVector;
use starknet_api::transaction::fields::Fee;
use starknet_types_core::felt::Felt;

use crate::errors::{NativeBlockifierError, NativeBlockifierResult};
Expand All @@ -47,65 +39,16 @@ use crate::storage::{
};

pub(crate) type RawTransactionExecutionResult = Vec<u8>;
const RESULT_SERIALIZE_ERR: &str = "Failed serializing execution info.";

#[cfg(test)]
#[path = "py_block_executor_test.rs"]
mod py_block_executor_test;

const RESULT_SERIALIZE_ERR: &str = "Failed serializing execution info.";

/// A mapping from a transaction execution resource to its actual usage.
#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize)]
pub struct ResourcesMapping(pub HashMap<String, usize>);

/// Stripped down `TransactionExecutionInfo` for Python serialization, containing only the required
/// fields.
#[derive(Debug, Serialize)]
pub(crate) struct ThinTransactionExecutionInfo {
pub validate_call_info: Option<CallInfo>,
pub execute_call_info: Option<CallInfo>,
pub fee_transfer_call_info: Option<CallInfo>,
pub actual_fee: Fee,
pub da_gas: GasVector,
pub actual_resources: ResourcesMapping,
pub revert_error: Option<String>,
pub total_gas: GasVector,
}

impl ThinTransactionExecutionInfo {
pub fn from_tx_execution_info(tx_execution_info: TransactionExecutionInfo) -> Self {
Self {
validate_call_info: tx_execution_info.validate_call_info,
execute_call_info: tx_execution_info.execute_call_info,
fee_transfer_call_info: tx_execution_info.fee_transfer_call_info,
actual_fee: tx_execution_info.receipt.fee,
da_gas: tx_execution_info.receipt.da_gas,
actual_resources: ThinTransactionExecutionInfo::receipt_to_resources_mapping(
&tx_execution_info.receipt,
),
revert_error: tx_execution_info.revert_error.map(|error| error.to_string()),
total_gas: tx_execution_info.receipt.gas,
}
}
pub fn serialize(self) -> RawTransactionExecutionResult {
serde_json::to_vec(&self).expect(RESULT_SERIALIZE_ERR)
}

pub fn receipt_to_resources_mapping(receipt: &TransactionReceipt) -> ResourcesMapping {
let vm_resources = &receipt.resources.computation.vm_resources;
let mut resources = HashMap::from([(
abi_constants::N_STEPS_RESOURCE.to_string(),
vm_resources.total_n_steps() + receipt.resources.computation.n_reverted_steps,
)]);
resources.extend(
vm_resources
.prover_builtins()
.iter()
.map(|(builtin, value)| (builtin.to_str_with_suffix().to_string(), *value)),
);

ResourcesMapping(resources)
}
fn serialize(
py_transaction_execution_info: CentralTransactionExecutionInfo,
) -> RawTransactionExecutionResult {
serde_json::to_vec(&py_transaction_execution_info).expect(RESULT_SERIALIZE_ERR)
}

#[pyclass]
Expand Down Expand Up @@ -198,11 +141,10 @@ impl PyBlockExecutor {
) -> NativeBlockifierResult<Py<PyBytes>> {
let tx: Transaction = py_tx(tx, optional_py_class_info).expect(PY_TX_PARSING_ERR);
let (tx_execution_info, _state_diff) = self.tx_executor().execute(&tx)?;
let thin_tx_execution_info =
ThinTransactionExecutionInfo::from_tx_execution_info(tx_execution_info);
let central_tx_execution_info = CentralTransactionExecutionInfo::from(tx_execution_info);

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

Expand Down Expand Up @@ -234,10 +176,9 @@ impl PyBlockExecutor {
.map(|result| match result {
Ok((tx_execution_info, _state_diff)) => (
true,
ThinTransactionExecutionInfo::from_tx_execution_info(
serialize(CentralTransactionExecutionInfo::from(
tx_execution_info,
)
.serialize(),
)),
),
Err(error) => (false, serialize_failure_reason(error)),
})
Expand Down
17 changes: 17 additions & 0 deletions crates/shared_execution_objects/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "shared_execution_objects"
version.workspace = true
edition.workspace = true
repository.workspace = true
license-file.workspace = true

[features]
deserialize = ["blockifier/transaction_serde"]

[dependencies]
blockifier.workspace = true
serde.workspace = true
starknet_api.workspace = true

[lints]
workspace = true
1 change: 1 addition & 0 deletions crates/shared_execution_objects/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod objects;
61 changes: 61 additions & 0 deletions crates/shared_execution_objects/src/objects.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use std::collections::HashMap;

use blockifier::abi::constants as abi_constants;
use blockifier::execution::call_info::CallInfo;
use blockifier::fee::receipt::TransactionReceipt;
use blockifier::transaction::objects::{ExecutionResourcesTraits, TransactionExecutionInfo};
use serde::Serialize;
use starknet_api::execution_resources::GasVector;
use starknet_api::transaction::fields::Fee;

/// A mapping from a transaction execution resource to its actual usage.
#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize)]
pub struct ResourcesMapping(pub HashMap<String, usize>);

impl From<TransactionReceipt> for ResourcesMapping {
fn from(receipt: TransactionReceipt) -> ResourcesMapping {
let vm_resources = &receipt.resources.computation.vm_resources;
let mut resources = HashMap::from([(
abi_constants::N_STEPS_RESOURCE.to_string(),
vm_resources.total_n_steps() + receipt.resources.computation.n_reverted_steps,
)]);
resources.extend(
vm_resources
.prover_builtins()
.iter()
.map(|(builtin, value)| (builtin.to_str_with_suffix().to_string(), *value)),
);

ResourcesMapping(resources)
}
}

/// The TransactionExecutionInfo object as used by the Python code.
#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
#[derive(Debug, Serialize)]
pub struct CentralTransactionExecutionInfo {
pub validate_call_info: Option<CallInfo>,
pub execute_call_info: Option<CallInfo>,
pub fee_transfer_call_info: Option<CallInfo>,
pub actual_fee: Fee,
pub da_gas: GasVector,
pub actual_resources: ResourcesMapping,
pub revert_error: Option<String>,
pub total_gas: GasVector,
}

impl From<TransactionExecutionInfo> for CentralTransactionExecutionInfo {
fn from(tx_execution_info: TransactionExecutionInfo) -> CentralTransactionExecutionInfo {
CentralTransactionExecutionInfo {
validate_call_info: tx_execution_info.validate_call_info,
execute_call_info: tx_execution_info.execute_call_info,
fee_transfer_call_info: tx_execution_info.fee_transfer_call_info,
actual_fee: tx_execution_info.receipt.fee,
da_gas: tx_execution_info.receipt.da_gas,
revert_error: tx_execution_info.revert_error.map(|error| error.to_string()),
total_gas: tx_execution_info.receipt.gas,
actual_resources: tx_execution_info.receipt.into(),
}
}
}
4 changes: 1 addition & 3 deletions crates/starknet_consensus_orchestrator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ repository.workspace = true
license-file.workspace = true
description = "Implements the consensus context and orchestrates the node's components accordingly"

[features]
deserialize = ["blockifier/transaction_serde"]

[dependencies]
assert_matches.workspace = true
async-trait.workspace = true
Expand All @@ -24,6 +21,7 @@ paste.workspace = true
reqwest = { workspace = true, features = ["json"] }
serde.workspace = true
serde_json = { workspace = true, features = ["arbitrary_precision"] }
shared_execution_objects.workspace = true
starknet-types-core.workspace = true
starknet_api.workspace = true
starknet_batcher_types.workspace = true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
use std::collections::HashMap;

use assert_matches::assert_matches;
use blockifier::abi::constants as abi_constants;
use blockifier::bouncer::BouncerWeights;
use blockifier::execution::call_info::CallInfo;
use blockifier::fee::receipt::TransactionReceipt;
use blockifier::state::cached_state::CommitmentStateDiff;
use blockifier::transaction::objects::{ExecutionResourcesTraits, TransactionExecutionInfo};
use cairo_lang_starknet_classes::casm_contract_class::CasmContractClass;
use cairo_lang_starknet_classes::NestedIntList;
use indexmap::{indexmap, IndexMap};
Expand Down Expand Up @@ -35,7 +29,6 @@ use starknet_api::executable_transaction::{
L1HandlerTransaction,
Transaction,
};
use starknet_api::execution_resources::GasVector;
use starknet_api::state::{StorageKey, ThinStateDiff};
use starknet_api::transaction::fields::{
AccountDeploymentData,
Expand Down Expand Up @@ -394,54 +387,3 @@ pub fn casm_contract_class_central_format(
..compiled_class_hash
}
}

/// A mapping from a transaction execution resource to its actual usage.
#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
#[derive(Debug, Eq, PartialEq, Serialize)]
pub struct ResourcesMapping(pub HashMap<String, usize>);

impl From<TransactionReceipt> for ResourcesMapping {
fn from(receipt: TransactionReceipt) -> ResourcesMapping {
let vm_resources = &receipt.resources.computation.vm_resources;
let mut resources = HashMap::from([(
abi_constants::N_STEPS_RESOURCE.to_string(),
vm_resources.total_n_steps() + receipt.resources.computation.n_reverted_steps,
)]);
resources.extend(
vm_resources
.prover_builtins()
.iter()
.map(|(builtin, value)| (builtin.to_str_with_suffix().to_string(), *value)),
);

ResourcesMapping(resources)
}
}

#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
#[derive(Debug, Serialize)]
pub struct CentralTransactionExecutionInfo {
pub validate_call_info: Option<CallInfo>,
pub execute_call_info: Option<CallInfo>,
pub fee_transfer_call_info: Option<CallInfo>,
pub actual_fee: Fee,
pub da_gas: GasVector,
pub actual_resources: ResourcesMapping,
pub revert_error: Option<String>,
pub total_gas: GasVector,
}

impl From<TransactionExecutionInfo> for CentralTransactionExecutionInfo {
fn from(tx_execution_info: TransactionExecutionInfo) -> CentralTransactionExecutionInfo {
CentralTransactionExecutionInfo {
validate_call_info: tx_execution_info.validate_call_info,
execute_call_info: tx_execution_info.execute_call_info,
fee_transfer_call_info: tx_execution_info.fee_transfer_call_info,
actual_fee: tx_execution_info.receipt.fee,
da_gas: tx_execution_info.receipt.da_gas,
revert_error: tx_execution_info.revert_error.map(|error| error.to_string()),
total_gas: tx_execution_info.receipt.gas,
actual_resources: tx_execution_info.receipt.into(),
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ use indexmap::indexmap;
use num_bigint::BigUint;
use rstest::rstest;
use serde_json::Value;
use shared_execution_objects::objects::CentralTransactionExecutionInfo;
use starknet_api::block::{
BlockHash,
BlockInfo,
Expand Down Expand Up @@ -111,7 +112,6 @@ use super::{
CentralInvokeTransaction,
CentralStateDiff,
CentralTransaction,
CentralTransactionExecutionInfo,
CentralTransactionWritten,
};
use crate::cende::central_objects::casm_contract_class_central_format;
Expand Down
Loading

0 comments on commit 00aeb44

Please sign in to comment.