Skip to content

Commit

Permalink
chore(blockifier): Add charge fee flage to execute
Browse files Browse the repository at this point in the history
  • Loading branch information
meship-starkware committed Aug 7, 2024
1 parent c5c3259 commit 6bfc25d
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 20 deletions.
7 changes: 4 additions & 3 deletions crates/blockifier/src/blockifier/stateful_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,13 @@ impl<S: StateReader> StatefulValidator<S> {
&mut self,
tx: AccountTransaction,
skip_validate: bool,
charge_fee: bool,
) -> StatefulValidatorResult<()> {
// Deploy account transactions should be fully executed, since the constructor must run
// before `__validate_deploy__`. The execution already includes all necessary validations,
// so they are skipped here.
if let AccountTransaction::DeployAccount(_) = tx {
self.execute(tx)?;
self.execute(tx, charge_fee)?;
return Ok(());
}

Expand All @@ -83,8 +84,8 @@ impl<S: StateReader> StatefulValidator<S> {
Ok(())
}

fn execute(&mut self, tx: AccountTransaction) -> StatefulValidatorResult<()> {
self.tx_executor.execute(&Transaction::AccountTransaction(tx))?;
fn execute(&mut self, tx: AccountTransaction, charge_fee: bool) -> StatefulValidatorResult<()> {
self.tx_executor.execute(&Transaction::AccountTransaction(tx), charge_fee)?;
Ok(())
}

Expand Down
4 changes: 2 additions & 2 deletions crates/blockifier/src/blockifier/stateful_validator_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ fn test_transaction_validator(
// Test the stateful validator.
let mut stateful_validator = StatefulValidator::create(state, block_context);

let result = stateful_validator.perform_validations(tx, false);
let result = stateful_validator.perform_validations(tx, false, true);
assert!(result.is_ok(), "Validation failed: {:?}", result.unwrap_err());
}

Expand All @@ -92,6 +92,6 @@ fn test_transaction_validator_skip_validate() {

let mut stateful_validator = StatefulValidator::create(state, block_context);
// The transaction validations should be skipped and the function should return Ok.
let result = stateful_validator.perform_validations(tx, true);
let result = stateful_validator.perform_validations(tx, true, true);
assert_matches!(result, Ok(()));
}
9 changes: 6 additions & 3 deletions crates/blockifier/src/blockifier/transaction_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,14 @@ impl<S: StateReader> TransactionExecutor<S> {
pub fn execute(
&mut self,
tx: &Transaction,
charge_fee: bool,
) -> TransactionExecutorResult<TransactionExecutionInfo> {
let mut transactional_state = TransactionalState::create_transactional(
self.block_state.as_mut().expect(BLOCK_STATE_ACCESS_ERR),
);
// Executing a single transaction cannot be done in a concurrent mode.
let execution_flags =
ExecutionFlags { charge_fee: true, validate: true, concurrency_mode: false };
ExecutionFlags { charge_fee, validate: true, concurrency_mode: false };
let tx_execution_result =
tx.execute_raw(&mut transactional_state, &self.block_context, execution_flags);
match tx_execution_result {
Expand All @@ -118,10 +119,11 @@ impl<S: StateReader> TransactionExecutor<S> {
pub fn execute_txs_sequentially(
&mut self,
txs: &[Transaction],
charge_fee: bool,
) -> Vec<TransactionExecutorResult<TransactionExecutionInfo>> {
let mut results = Vec::new();
for tx in txs {
match self.execute(tx) {
match self.execute(tx, charge_fee) {
Ok(tx_execution_info) => results.push(Ok(tx_execution_info)),
Err(TransactionExecutorError::BlockFull) => break,
Err(error) => results.push(Err(error)),
Expand Down Expand Up @@ -179,10 +181,11 @@ impl<S: StateReader + Send + Sync> TransactionExecutor<S> {
pub fn execute_txs(
&mut self,
txs: &[Transaction],
charge_fee: bool,
) -> Vec<TransactionExecutorResult<TransactionExecutionInfo>> {
if !self.config.concurrency_config.enabled {
log::debug!("Executing transactions sequentially.");
self.execute_txs_sequentially(txs)
self.execute_txs_sequentially(txs, charge_fee)
} else {
log::debug!("Executing transactions concurrently.");
let chunk_size = self.config.concurrency_config.chunk_size;
Expand Down
10 changes: 5 additions & 5 deletions crates/blockifier/src/blockifier/transaction_executor_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ fn tx_executor_test_body<S: StateReader>(
// TODO(Arni, 30/03/2024): Consider adding a test for the transaction execution info. If A test
// should not be added, rename the test to `test_bouncer_info`.
// TODO(Arni, 30/03/2024): Test all bouncer weights.
let _tx_execution_info = tx_executor.execute(&tx).unwrap();
let _tx_execution_info = tx_executor.execute(&tx, true).unwrap();
let bouncer_weights = tx_executor.bouncer.get_accumulated_weights();
assert_eq!(bouncer_weights.state_diff_size, expected_bouncer_weights.state_diff_size);
assert_eq!(
Expand Down Expand Up @@ -269,7 +269,7 @@ fn test_bouncing(#[case] initial_bouncer_weights: BouncerWeights, #[case] n_even
account_address,
contract_address,
nonce_manager.next(account_address),
)))
)), true)
.map_err(|error| panic!("{error:?}: {error}"))
.unwrap();
}
Expand Down Expand Up @@ -305,7 +305,7 @@ fn test_execute_txs_bouncing() {
.collect();

// Run.
let results = tx_executor.execute_txs(&txs);
let results = tx_executor.execute_txs(&txs, true);

// Check execution results.
let expected_offset = 3;
Expand Down Expand Up @@ -333,12 +333,12 @@ fn test_execute_txs_bouncing() {

// Check idempotency: excess transactions should not be added.
let remaining_txs = &txs[expected_offset..];
let remaining_tx_results = tx_executor.execute_txs(remaining_txs);
let remaining_tx_results = tx_executor.execute_txs(remaining_txs, true);
assert_eq!(remaining_tx_results.len(), 0);

// Reset the bouncer and add the remaining transactions.
tx_executor.bouncer = Bouncer::new(tx_executor.block_context.bouncer_config.clone());
let remaining_tx_results = tx_executor.execute_txs(remaining_txs);
let remaining_tx_results = tx_executor.execute_txs(remaining_txs, true);

assert_eq!(remaining_tx_results.len(), 2);
assert!(remaining_tx_results[0].is_ok());
Expand Down
2 changes: 1 addition & 1 deletion crates/blockifier/src/test_utils/transfers_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ impl TransfersGenerator {
let account_tx = self.generate_transfer(sender_address, recipient_address);
txs.push(Transaction::AccountTransaction(account_tx));
}
let results = self.executor.execute_txs(&txs);
let results = self.executor.execute_txs(&txs, true);
assert_eq!(results.len(), self.config.n_txs);
for result in results {
assert!(!result.unwrap().is_reverted());
Expand Down
2 changes: 1 addition & 1 deletion crates/gateway/src/stateful_transaction_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl StatefulTransactionValidatorTrait for BlockifierStatefulValidator {
account_tx: AccountTransaction,
skip_validate: bool,
) -> BlockifierStatefulValidatorResult<()> {
self.perform_validations(account_tx, skip_validate)
self.perform_validations(account_tx, skip_validate, true)
}

fn get_nonce(
Expand Down
10 changes: 6 additions & 4 deletions crates/native_blockifier/src/py_block_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,15 @@ impl PyBlockExecutor {
self.tx_executor = None;
}

#[pyo3(signature = (tx, optional_py_class_info))]
#[pyo3(signature = (tx, charge_fee, optional_py_class_info))]
pub fn execute(
&mut self,
tx: &PyAny,
charge_fee: bool,
optional_py_class_info: Option<PyClassInfo>,
) -> NativeBlockifierResult<Py<PyBytes>> {
let tx: Transaction = py_tx(tx, optional_py_class_info).expect(PY_TX_PARSING_ERR);
let tx_execution_info = self.tx_executor().execute(&tx)?;
let tx_execution_info = self.tx_executor().execute(&tx, charge_fee)?;
let thin_tx_execution_info = ThinTransactionExecutionInfo::from_tx_execution_info(
&self.tx_executor().block_context,
tx_execution_info,
Expand All @@ -180,10 +181,11 @@ impl PyBlockExecutor {
/// Executes the given transactions on the Blockifier state.
/// Stops if and when there is no more room in the block, and returns the executed transactions'
/// results as a PyList of (success (bool), serialized result (bytes)) tuples.
#[pyo3(signature = (txs_with_class_infos))]
#[pyo3(signature = (txs_with_class_infos, charge_fee))]
pub fn execute_txs(
&mut self,
txs_with_class_infos: Vec<(&PyAny, Option<PyClassInfo>)>,
charge_fee: bool,
) -> Py<PyList> {
// Parse Py transactions.
let txs: Vec<Transaction> = txs_with_class_infos
Expand All @@ -195,7 +197,7 @@ impl PyBlockExecutor {

// Run.
let results =
Python::with_gil(|py| py.allow_threads(|| self.tx_executor().execute_txs(&txs)));
Python::with_gil(|py| py.allow_threads(|| self.tx_executor().execute_txs(&txs, charge_fee)));

// Process results.
// TODO(Yoni, 15/5/2024): serialize concurrently.
Expand Down
2 changes: 1 addition & 1 deletion crates/native_blockifier/src/py_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl PyValidator {
// processed.
let skip_validate = self
.skip_validate_due_to_unprocessed_deploy_account(&account_tx, deploy_account_tx_hash)?;
self.stateful_validator.perform_validations(account_tx, skip_validate)?;
self.stateful_validator.perform_validations(account_tx, skip_validate, true)?;

Ok(())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ impl<ReceiverT> NetworkReceiver<ReceiverT>
where
ReceiverT: Stream<Item = (Result<ConsensusMessage, ProtobufConversionError>, ReportSender)>,
{
// TODO(Meshi): remove this before merging this pr.
#[allow(dead_code)]
pub fn new(
receiver: ReceiverT,
cache_size: usize,
Expand Down

0 comments on commit 6bfc25d

Please sign in to comment.