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

Commit

Permalink
Make TRansactionExecutor.execute() get tx of type Transaction and not…
Browse files Browse the repository at this point in the history
… PyAny.
  • Loading branch information
barak-b-starkware committed Feb 1, 2024
1 parent 7a5c141 commit 634cedb
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 28 deletions.
20 changes: 19 additions & 1 deletion crates/native_blockifier/src/py_block_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@ use std::sync::Arc;
use blockifier::block::{BlockInfo, GasPrices};
use blockifier::context::{BlockContext, ChainInfo, FeeTokenAddresses};
use blockifier::state::cached_state::{GlobalContractCache, GLOBAL_CONTRACT_CACHE_SIZE_FOR_TEST};
use blockifier::transaction::objects::TransactionExecutionInfo;
use blockifier::transaction::transaction_execution::Transaction;
use blockifier::versioned_constants::VersionedConstants;
use pyo3::prelude::*;
use serde::Serialize;
use starknet_api::block::{BlockNumber, BlockTimestamp};
use starknet_api::core::{ChainId, ContractAddress};
use starknet_api::hash::StarkFelt;

use crate::errors::{NativeBlockifierError, NativeBlockifierResult};
use crate::py_state_diff::{PyBlockInfo, PyStateDiff};
use crate::py_transaction::py_tx;
use crate::py_transaction_execution_info::PyBouncerInfo;
use crate::py_utils::{int_to_chain_id, py_attr, versioned_constants_with_overrides, PyFelt};
use crate::state_readers::papyrus_state::PapyrusReader;
Expand All @@ -22,6 +26,14 @@ use crate::transaction_executor::{RawTransactionExecutionInfo, TransactionExecut
#[path = "py_block_executor_test.rs"]
mod py_block_executor_test;

#[pyclass]
#[derive(Debug, Serialize)]
pub(crate) struct TypedTransactionExecutionInfo {
#[serde(flatten)]
pub info: TransactionExecutionInfo,
pub tx_type: String,
}

#[pyclass]
pub struct PyBlockExecutor {
pub general_config: PyGeneralConfig,
Expand Down Expand Up @@ -90,7 +102,13 @@ impl PyBlockExecutor {
raw_contract_class: Option<&str>,
) -> NativeBlockifierResult<(RawTransactionExecutionInfo, PyBouncerInfo)> {
let charge_fee = true;
self.tx_executor().execute(tx, raw_contract_class, charge_fee)
let tx_type: &str = tx.getattr("tx_type")?.getattr("name")?.extract()?;
let tx: Transaction = py_tx(tx, raw_contract_class)?;
let (tx_execution_info, py_bouncer_info) = self.tx_executor().execute(tx, charge_fee)?;
let typed_tx_execution_info =
TypedTransactionExecutionInfo { info: tx_execution_info, tx_type: tx_type.to_string() };
let raw_tx_execution_info = serde_json::to_vec(&typed_tx_execution_info)?;
Ok((raw_tx_execution_info, py_bouncer_info))
}

/// Returns the state diff and a list of contract class hash with the corresponding list of
Expand Down
13 changes: 10 additions & 3 deletions crates/native_blockifier/src/py_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ use starknet_api::core::Nonce;
use starknet_api::hash::StarkFelt;

use crate::errors::NativeBlockifierResult;
use crate::py_block_executor::PyGeneralConfig;
use crate::py_block_executor::{PyGeneralConfig, TypedTransactionExecutionInfo};
use crate::py_state_diff::PyBlockInfo;
use crate::py_transaction::py_account_tx;
use crate::py_transaction::{py_account_tx, py_tx};
use crate::py_transaction_execution_info::PyBouncerInfo;
use crate::py_utils::{versioned_constants_with_overrides, PyFelt};
use crate::state_readers::py_state_reader::PyStateReader;
Expand Down Expand Up @@ -129,7 +129,14 @@ impl PyValidator {
raw_contract_class: Option<&str>,
) -> NativeBlockifierResult<(RawTransactionExecutionInfo, PyBouncerInfo)> {
let limit_execution_steps_by_resource_bounds = true;
self.tx_executor.execute(tx, raw_contract_class, limit_execution_steps_by_resource_bounds)
let tx_type: &str = tx.getattr("tx_type")?.getattr("name")?.extract()?;
let tx: Transaction = py_tx(tx, raw_contract_class)?;
let (tx_execution_info, py_bouncer_info) =
self.tx_executor.execute(tx, limit_execution_steps_by_resource_bounds)?;
let typed_tx_execution_info =
TypedTransactionExecutionInfo { info: tx_execution_info, tx_type: tx_type.to_string() };
let raw_tx_execution_info = serde_json::to_vec(&typed_tx_execution_info)?;
Ok((raw_tx_execution_info, py_bouncer_info))
}
}

Expand Down
28 changes: 4 additions & 24 deletions crates/native_blockifier/src/transaction_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,16 @@ use blockifier::transaction::transactions::{ExecutableTransaction, ValidatableTr
use blockifier::versioned_constants::VersionedConstants;
use cairo_vm::vm::runners::builtin_runner::HASH_BUILTIN_NAME;
use cairo_vm::vm::runners::cairo_runner::ExecutionResources as VmExecutionResources;
use pyo3::prelude::*;
use serde::Serialize;
use starknet_api::core::ClassHash;

use crate::errors::{NativeBlockifierError, NativeBlockifierResult};
use crate::py_block_executor::{into_block_context, PyGeneralConfig};
use crate::py_state_diff::{PyBlockInfo, PyStateDiff};
use crate::py_transaction::py_tx;
use crate::py_transaction_execution_info::PyBouncerInfo;
use crate::py_utils::PyFelt;

pub(crate) type RawTransactionExecutionInfo = Vec<u8>;

#[pyclass]
#[derive(Debug, Serialize)]
pub(crate) struct TypedTransactionExecutionInfo {
#[serde(flatten)]
info: TransactionExecutionInfo,
tx_type: String,
}

pub struct TransactionExecutor<S: StateReader> {
pub block_context: BlockContext,

Expand Down Expand Up @@ -81,12 +70,10 @@ impl<S: StateReader> TransactionExecutor<S> {
/// (used for counting purposes).
pub fn execute(
&mut self,
tx: &PyAny,
raw_contract_class: Option<&str>,
// tx: &PyAny,
tx: Transaction,
charge_fee: bool,
) -> NativeBlockifierResult<(RawTransactionExecutionInfo, PyBouncerInfo)> {
let tx_type: &str = tx.getattr("tx_type")?.getattr("name")?.extract()?;
let tx: Transaction = py_tx(tx, raw_contract_class)?;
) -> NativeBlockifierResult<(TransactionExecutionInfo, PyBouncerInfo)> {
let l1_handler_payload_size: usize =
if let Transaction::L1HandlerTransaction(l1_handler_tx) = &tx {
l1_handler_tx.payload_size()
Expand Down Expand Up @@ -144,14 +131,7 @@ impl<S: StateReader> TransactionExecutor<S> {
self.staged_for_commit_state = Some(
transactional_state.stage(tx_executed_class_hashes, tx_visited_storage_entries),
);

let typed_tx_execution_info = TypedTransactionExecutionInfo {
info: tx_execution_info,
tx_type: tx_type.to_string(),
};
let raw_tx_execution_info = serde_json::to_vec(&typed_tx_execution_info)?;

Ok((raw_tx_execution_info, py_bouncer_info))
Ok((tx_execution_info, py_bouncer_info))
}
Err(error) => {
transactional_state.abort();
Expand Down

0 comments on commit 634cedb

Please sign in to comment.