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

Commit

Permalink
refactor(execution): clean PyBlockExecutor (#1850)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoni-Starkware authored May 2, 2024
1 parent 96e28cf commit 545f912
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 34 deletions.
36 changes: 5 additions & 31 deletions crates/native_blockifier/src/py_block_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,12 @@ use crate::errors::{
NativeBlockifierResult,
};
use crate::py_state_diff::{PyBlockInfo, PyStateDiff};
use crate::py_transaction::{py_tx, PyClassInfo};
use crate::py_transaction::{get_py_tx_type, py_tx, PyClassInfo, PY_TX_PARSING_ERR};
use crate::py_transaction_execution_info::PyBouncerConfig;
use crate::py_utils::{int_to_chain_id, PyFelt};
use crate::state_readers::papyrus_state::PapyrusReader;
use crate::storage::{PapyrusStorage, Storage, StorageConfig};

pub(crate) type RawTransactionExecutionInfo = Vec<u8>;
pub(crate) type PyVisitedSegmentsMapping = Vec<(PyFelt, Vec<usize>)>;

#[cfg(test)]
Expand Down Expand Up @@ -164,8 +163,8 @@ impl PyBlockExecutor {
optional_py_class_info: Option<PyClassInfo>,
) -> NativeBlockifierResult<Py<PyBytes>> {
let charge_fee = true;
let tx_type: String = tx.getattr("tx_type")?.getattr("name")?.extract()?;
let tx: Transaction = py_tx(tx, optional_py_class_info)?;
let tx_type: String = get_py_tx_type(tx).expect(PY_TX_PARSING_ERR).to_string();
let tx: Transaction = py_tx(tx, optional_py_class_info).expect(PY_TX_PARSING_ERR);
let tx_execution_info = self.tx_executor().execute(&tx, charge_fee)?;
let typed_tx_execution_info = TypedTransactionExecutionInfo {
info: ThinTransactionExecutionInfo::from_tx_execution_info(
Expand All @@ -177,39 +176,14 @@ impl PyBlockExecutor {

// Convert to PyBytes:
let raw_tx_execution_info = Python::with_gil(|py| {
let bytes_tx_execution_info = serde_json::to_vec(&typed_tx_execution_info).unwrap();
let bytes_tx_execution_info = serde_json::to_vec(&typed_tx_execution_info)
.expect("Failed serializing execution info.");
PyBytes::new(py, &bytes_tx_execution_info).into()
});

Ok(raw_tx_execution_info)
}

#[pyo3(signature = (txs_with_class_infos))]
pub fn execute_txs(
&mut self,
txs_with_class_infos: Vec<(&PyAny, Option<PyClassInfo>)>,
) -> NativeBlockifierResult<Vec<RawTransactionExecutionInfo>> {
let charge_fee = true;
let mut result_vec = Vec::new();

for (tx, optional_py_class_info) in txs_with_class_infos.into_iter() {
let tx_type: String = tx.getattr("tx_type")?.getattr("name")?.extract()?;
let tx: Transaction = py_tx(tx, optional_py_class_info)?;
let tx_execution_info = self.tx_executor().execute(&tx, charge_fee)?;
let typed_tx_execution_info = TypedTransactionExecutionInfo {
info: ThinTransactionExecutionInfo::from_tx_execution_info(
&self.tx_executor().block_context,
tx_execution_info,
),
tx_type,
};
let raw_tx_execution_info = serde_json::to_vec(&typed_tx_execution_info)?;
result_vec.push(raw_tx_execution_info);
}

Ok(result_vec)
}

/// 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)> {
Expand Down
9 changes: 8 additions & 1 deletion crates/native_blockifier/src/py_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ use crate::py_deploy_account::py_deploy_account;
use crate::py_invoke_function::py_invoke_function;
use crate::py_l1_handler::py_l1_handler;

pub(crate) const PY_TX_PARSING_ERR: &str = "Failed parsing Py transaction.";

// Structs.

#[derive(Clone, Eq, Ord, PartialEq, PartialOrd)]
Expand Down Expand Up @@ -122,7 +124,7 @@ pub fn py_tx(
tx: &PyAny,
optional_py_class_info: Option<PyClassInfo>,
) -> NativeBlockifierResult<Transaction> {
let tx_type: &str = tx.getattr("tx_type")?.getattr("name")?.extract()?;
let tx_type = get_py_tx_type(tx)?;
let tx_type: TransactionType =
tx_type.parse().map_err(NativeBlockifierInputError::ParseError)?;

Expand All @@ -141,6 +143,11 @@ pub fn py_tx(
TransactionType::L1Handler => py_l1_handler(tx)?.into(),
})
}

pub fn get_py_tx_type(tx: &PyAny) -> NativeBlockifierResult<&str> {
Ok(tx.getattr("tx_type")?.getattr("name")?.extract()?)
}

#[derive(FromPyObject)]
pub struct PyClassInfo {
raw_contract_class: String,
Expand Down
4 changes: 2 additions & 2 deletions crates/native_blockifier/src/py_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use starknet_api::transaction::TransactionHash;
use crate::errors::NativeBlockifierResult;
use crate::py_block_executor::{into_block_context_args, PyGeneralConfig};
use crate::py_state_diff::PyBlockInfo;
use crate::py_transaction::{py_account_tx, PyClassInfo};
use crate::py_transaction::{py_account_tx, PyClassInfo, PY_TX_PARSING_ERR};
use crate::py_utils::PyFelt;
use crate::state_readers::py_state_reader::PyStateReader;

Expand Down Expand Up @@ -65,7 +65,7 @@ impl PyValidator {
optional_py_class_info: Option<PyClassInfo>,
deploy_account_tx_hash: Option<PyFelt>,
) -> NativeBlockifierResult<()> {
let account_tx = py_account_tx(tx, optional_py_class_info)?;
let account_tx = py_account_tx(tx, optional_py_class_info).expect(PY_TX_PARSING_ERR);
let deploy_account_tx_hash = deploy_account_tx_hash.map(|hash| TransactionHash(hash.0));
self.stateful_validator.perform_validations(account_tx, deploy_account_tx_hash)?;

Expand Down

0 comments on commit 545f912

Please sign in to comment.