Skip to content

Commit b445c22

Browse files
chore(blockifier): fn execute_txs_sequentially return state_diff +
change privacy pub fn to fn
1 parent 91889fd commit b445c22

File tree

4 files changed

+31
-13
lines changed

4 files changed

+31
-13
lines changed

crates/blockifier/src/blockifier/transaction_executor.rs

Lines changed: 19 additions & 11 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};
@@ -27,6 +27,8 @@ pub mod transaction_executor_test;
2727
pub const BLOCK_STATE_ACCESS_ERR: &str = "Error: The block state should be `Some`.";
2828
pub const DEFAULT_STACK_SIZE: usize = 60 * 1024 * 1024;
2929

30+
pub type TransactionExecutionOutput = (TransactionExecutionInfo, StateMaps);
31+
3032
#[derive(Debug, Error)]
3133
pub enum TransactionExecutorError {
3234
#[error("Transaction cannot be added to the current block, block capacity reached.")]
@@ -103,7 +105,7 @@ impl<S: StateReader> TransactionExecutor<S> {
103105
pub fn execute(
104106
&mut self,
105107
tx: &Transaction,
106-
) -> TransactionExecutorResult<TransactionExecutionInfo> {
108+
) -> TransactionExecutorResult<TransactionExecutionOutput> {
107109
let mut transactional_state = TransactionalState::create_transactional(
108110
self.block_state.as_mut().expect(BLOCK_STATE_ACCESS_ERR),
109111
);
@@ -114,16 +116,17 @@ impl<S: StateReader> TransactionExecutor<S> {
114116
tx.execute_raw(&mut transactional_state, &self.block_context, concurrency_mode);
115117
match tx_execution_result {
116118
Ok(tx_execution_info) => {
117-
let tx_state_changes_keys =
118-
transactional_state.get_actual_state_changes()?.state_maps.into_keys();
119+
let state_diff = transactional_state.to_state_diff()?.state_maps;
120+
let tx_state_changes_keys = state_diff.keys();
119121
self.bouncer.try_update(
120122
&transactional_state,
121123
&tx_state_changes_keys,
122124
&tx_execution_info.summarize(&self.block_context.versioned_constants),
123125
&tx_execution_info.receipt.resources,
124126
)?;
125127
transactional_state.commit();
126-
Ok(tx_execution_info)
128+
129+
Ok((tx_execution_info, state_diff))
127130
}
128131
Err(error) => {
129132
transactional_state.abort();
@@ -132,14 +135,16 @@ impl<S: StateReader> TransactionExecutor<S> {
132135
}
133136
}
134137

135-
pub fn execute_txs_sequentially_inner(
138+
fn execute_txs_sequentially_inner(
136139
&mut self,
137140
txs: &[Transaction],
138-
) -> Vec<TransactionExecutorResult<TransactionExecutionInfo>> {
141+
) -> Vec<TransactionExecutorResult<TransactionExecutionOutput>> {
139142
let mut results = Vec::new();
140143
for tx in txs {
141144
match self.execute(tx) {
142-
Ok(tx_execution_info) => results.push(Ok(tx_execution_info)),
145+
Ok((tx_execution_info, state_diff)) => {
146+
results.push(Ok((tx_execution_info, state_diff)))
147+
}
143148
Err(TransactionExecutorError::BlockFull) => break,
144149
Err(error) => results.push(Err(error)),
145150
}
@@ -196,6 +201,9 @@ impl<S: StateReader + Send + Sync> TransactionExecutor<S> {
196201
if !self.config.concurrency_config.enabled {
197202
log::debug!("Executing transactions sequentially.");
198203
self.execute_txs_sequentially(txs)
204+
.into_iter()
205+
.map(|res| res.map(|(tx_execution_info, _state_diff)| tx_execution_info))
206+
.collect()
199207
} else {
200208
log::debug!("Executing transactions concurrently.");
201209
let chunk_size = self.config.concurrency_config.chunk_size;
@@ -228,10 +236,10 @@ impl<S: StateReader + Send + Sync> TransactionExecutor<S> {
228236
}
229237
}
230238

231-
pub fn execute_txs_sequentially(
239+
fn execute_txs_sequentially(
232240
&mut self,
233241
txs: &[Transaction],
234-
) -> Vec<TransactionExecutorResult<TransactionExecutionInfo>> {
242+
) -> Vec<TransactionExecutorResult<TransactionExecutionOutput>> {
235243
#[cfg(not(feature = "cairo_native"))]
236244
return self.execute_txs_sequentially_inner(txs);
237245
#[cfg(feature = "cairo_native")]
@@ -261,7 +269,7 @@ impl<S: StateReader + Send + Sync> TransactionExecutor<S> {
261269
}
262270
}
263271

264-
pub fn execute_chunk(
272+
fn execute_chunk(
265273
&mut self,
266274
chunk: &[Transaction],
267275
) -> Vec<TransactionExecutorResult<TransactionExecutionInfo>> {

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_output = 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/blockifier/src/state/cached_state.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,16 @@ impl StateMaps {
364364
compiled_class_hash_keys: self.compiled_class_hashes.into_keys().collect(),
365365
}
366366
}
367+
368+
pub fn keys(&self) -> StateChangesKeys {
369+
StateChangesKeys {
370+
modified_contracts: self.get_contract_addresses(),
371+
nonce_keys: self.nonces.keys().cloned().collect(),
372+
class_hash_keys: self.class_hashes.keys().cloned().collect(),
373+
storage_keys: self.storage.keys().cloned().collect(),
374+
compiled_class_hash_keys: self.compiled_class_hashes.keys().cloned().collect(),
375+
}
376+
}
367377
}
368378
/// Caches read and write requests.
369379
/// The tracked changes are needed for block state commitment.

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)