Skip to content

Commit 2888f08

Browse files
chore(blockifier): can statediff be returned from execute?
1 parent ca983aa commit 2888f08

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

crates/blockifier/src/blockifier/transaction_executor.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::blockifier::config::TransactionExecutorConfig;
1111
use crate::bouncer::{Bouncer, BouncerWeights};
1212
use crate::concurrency::worker_logic::WorkerExecutor;
1313
use crate::context::BlockContext;
14-
use crate::state::cached_state::{CachedState, CommitmentStateDiff, TransactionalState};
14+
use crate::state::cached_state::{CachedState, CommitmentStateDiff, StateMaps, TransactionalState};
1515
use crate::state::errors::StateError;
1616
use crate::state::state_api::{StateReader, StateResult};
1717
use crate::state::stateful_compression::{allocate_aliases_in_storage, compress, CompressionError};
@@ -103,7 +103,7 @@ impl<S: StateReader> TransactionExecutor<S> {
103103
pub fn execute(
104104
&mut self,
105105
tx: &Transaction,
106-
) -> TransactionExecutorResult<TransactionExecutionInfo> {
106+
) -> TransactionExecutorResult<TransactionExecutionOutput> {
107107
let mut transactional_state = TransactionalState::create_transactional(
108108
self.block_state.as_mut().expect(BLOCK_STATE_ACCESS_ERR),
109109
);
@@ -116,14 +116,15 @@ impl<S: StateReader> TransactionExecutor<S> {
116116
Ok(tx_execution_info) => {
117117
let tx_state_changes_keys =
118118
transactional_state.get_actual_state_changes()?.state_maps.into_keys();
119+
let state_diff = transactional_state.to_state_diff()?.state_maps;
119120
self.bouncer.try_update(
120121
&transactional_state,
121122
&tx_state_changes_keys,
122123
&tx_execution_info.summarize(&self.block_context.versioned_constants),
123124
&tx_execution_info.receipt.resources,
124125
)?;
125126
transactional_state.commit();
126-
Ok(tx_execution_info)
127+
Ok((tx_execution_info, state_diff))
127128
}
128129
Err(error) => {
129130
transactional_state.abort();
@@ -135,11 +136,13 @@ impl<S: StateReader> TransactionExecutor<S> {
135136
pub fn execute_txs_sequentially_inner(
136137
&mut self,
137138
txs: &[Transaction],
138-
) -> Vec<TransactionExecutorResult<TransactionExecutionInfo>> {
139+
) -> Vec<TransactionExecutorResult<(TransactionExecutionInfo, StateMaps)>> {
139140
let mut results = Vec::new();
140141
for tx in txs {
141142
match self.execute(tx) {
142-
Ok(tx_execution_info) => results.push(Ok(tx_execution_info)),
143+
Ok((tx_execution_info, state_diff)) => {
144+
results.push(Ok((tx_execution_info, state_diff)))
145+
}
143146
Err(TransactionExecutorError::BlockFull) => break,
144147
Err(error) => results.push(Err(error)),
145148
}
@@ -233,14 +236,18 @@ impl<S: StateReader + Send + Sync> TransactionExecutor<S> {
233236
txs: &[Transaction],
234237
) -> Vec<TransactionExecutorResult<TransactionExecutionInfo>> {
235238
#[cfg(not(feature = "cairo_native"))]
236-
return self.execute_txs_sequentially_inner(txs);
239+
return self
240+
.execute_txs_sequentially_inner(txs)
241+
.into_iter()
242+
.map(|result| result.map(|(info, _)| info))
243+
.collect();
237244
#[cfg(feature = "cairo_native")]
238245
{
239246
// TODO meshi: find a way to access the contract class manager config from transaction
240247
// executor.
241248
let txs = txs.to_vec();
242249
std::thread::scope(|s| {
243-
std::thread::Builder::new()
250+
let execution_outputs = std::thread::Builder::new()
244251
// when running Cairo natively, the real stack is used and could get overflowed
245252
// (unlike the VM where the stack is simulated in the heap as a memory segment).
246253
//
@@ -256,7 +263,9 @@ impl<S: StateReader + Send + Sync> TransactionExecutor<S> {
256263
.spawn_scoped(s, || self.execute_txs_sequentially_inner(&txs))
257264
.expect("Failed to spawn thread")
258265
.join()
259-
.expect("Failed to join thread.")
266+
.expect("Failed to join thread.");
267+
268+
execution_outputs.into_iter().map(|result| result.map(|(info, _)| info)).collect()
260269
})
261270
}
262271
}

crates/blockifier/src/blockifier/transaction_executor_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ fn tx_executor_test_body<S: StateReader>(
5959
// TODO(Arni, 30/03/2024): Consider adding a test for the transaction execution info. If A test
6060
// should not be added, rename the test to `test_bouncer_info`.
6161
// TODO(Arni, 30/03/2024): Test all bouncer weights.
62-
let _tx_execution_info = tx_executor.execute(&tx).unwrap();
62+
let (_tx_execution_info, _state_diff) = tx_executor.execute(&tx).unwrap();
6363
let bouncer_weights = tx_executor.bouncer.get_accumulated_weights();
6464
assert_eq!(bouncer_weights.state_diff_size, expected_bouncer_weights.state_diff_size);
6565
assert_eq!(

crates/native_blockifier/src/py_block_executor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ impl PyBlockExecutor {
197197
optional_py_class_info: Option<PyClassInfo>,
198198
) -> NativeBlockifierResult<Py<PyBytes>> {
199199
let tx: Transaction = py_tx(tx, optional_py_class_info).expect(PY_TX_PARSING_ERR);
200-
let tx_execution_info = self.tx_executor().execute(&tx)?;
200+
let (tx_execution_info, _state_diff) = self.tx_executor().execute(&tx)?;
201201
let thin_tx_execution_info =
202202
ThinTransactionExecutionInfo::from_tx_execution_info(tx_execution_info);
203203

0 commit comments

Comments
 (0)