Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(native_blockifier, starknet_consensus_orchestrator): share execution objects #3877

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -27,6 +27,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 @@ -212,6 +213,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"
simple_logger = "4.0.0"
starknet-core = "0.6.0"
starknet-crypto = "0.7.1"
Expand Down
1 change: 1 addition & 0 deletions commitlint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const Configuration = {
'papyrus_sync',
'papyrus_test_utils',
'release',
'shared_execution_objects',
'starknet_api',
'starknet_batcher',
'starknet_batcher_types',
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
80 changes: 11 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::central_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_block_execution_info(
py_tx_execution_info: CentralTransactionExecutionInfo,
) -> RawTransactionExecutionResult {
serde_json::to_vec(&py_tx_execution_info).expect(RESULT_SERIALIZE_ERR)
}

#[pyclass]
Expand Down Expand Up @@ -198,11 +141,11 @@ 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_block_execution_info(central_tx_execution_info);
Ok(Python::with_gil(|py| PyBytes::new(py, &serialized_tx_execution_info).into()))
}

Expand Down Expand Up @@ -234,10 +177,9 @@ impl PyBlockExecutor {
.map(|result| match result {
Ok((tx_execution_info, _state_diff)) => (
true,
ThinTransactionExecutionInfo::from_tx_execution_info(
serialize_block_execution_info(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
61 changes: 61 additions & 0 deletions crates/shared_execution_objects/src/central_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(),
}
}
}
7 changes: 7 additions & 0 deletions crates/shared_execution_objects/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//! The `shared_execution_objects` crate contains execution objects shared between different crates.

/// Contains a rust version of objects on the centralized Python side. These objects are used when
/// interacting with the centralized Python.
pub mod central_objects;
// TODO(Yael): add the execution objects from the blockifier: execution_info, bouncer_weights,
// commitment_state_diff.
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]
async-trait.workspace = true
cairo-lang-starknet-classes.workspace = true
Expand All @@ -23,6 +20,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,7 @@
use std::collections::HashMap;
use std::str::FromStr;

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 All @@ -29,7 +24,6 @@ use starknet_api::core::{
};
use starknet_api::data_availability::DataAvailabilityMode;
use starknet_api::executable_transaction::L1HandlerTransaction;
use starknet_api::execution_resources::GasVector;
use starknet_api::rpc_transaction::{
InternalRpcDeclareTransactionV3,
InternalRpcDeployAccountTransaction,
Expand Down Expand Up @@ -440,58 +434,6 @@ pub(crate) 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)]
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 {
validate_call_info: Option<CallInfo>,
execute_call_info: Option<CallInfo>,
fee_transfer_call_info: Option<CallInfo>,
actual_fee: Fee,
da_gas: GasVector,
actual_resources: ResourcesMapping,
revert_error: Option<String>,
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(),
}
}
}

async fn get_contract_classes_if_declare(
class_manager: SharedClassManagerClient,
tx: &InternalConsensusTransaction,
Expand Down
Loading
Loading