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 TransactionExecutor - move validate to Sta…
Browse files Browse the repository at this point in the history
…tefulValidator (#1961)
  • Loading branch information
Yoni-Starkware authored Jun 6, 2024
1 parent c35c254 commit a34e203
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 47 deletions.
29 changes: 27 additions & 2 deletions crates/blockifier/src/blockifier/stateful_validator.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use std::sync::Arc;

use cairo_vm::vm::runners::cairo_runner::ExecutionResources;
use starknet_api::core::Nonce;
use starknet_api::hash::StarkFelt;
use starknet_api::transaction::TransactionHash;
Expand All @@ -16,6 +19,7 @@ use crate::transaction::account_transaction::AccountTransaction;
use crate::transaction::errors::{TransactionExecutionError, TransactionPreValidationError};
use crate::transaction::objects::TransactionInfo;
use crate::transaction::transaction_execution::Transaction;
use crate::transaction::transactions::ValidatableTransaction;

#[cfg(test)]
#[path = "stateful_validator_test.rs"]
Expand Down Expand Up @@ -140,8 +144,29 @@ impl<S: StateReader> StatefulValidator<S> {
fn validate(
&mut self,
tx: &AccountTransaction,
remaining_gas: u64,
mut remaining_gas: u64,
) -> StatefulValidatorResult<(Option<CallInfo>, TransactionReceipt)> {
Ok(self.tx_executor.validate(tx, remaining_gas)?)
let mut execution_resources = ExecutionResources::default();
let tx_context = Arc::new(self.tx_executor.block_context.to_tx_context(tx));

let limit_steps_by_resources = true;
let validate_call_info = tx.validate_tx(
&mut self.tx_executor.state,
&mut execution_resources,
tx_context.clone(),
&mut remaining_gas,
limit_steps_by_resources,
)?;

let tx_receipt = TransactionReceipt::from_account_tx(
tx,
&tx_context,
&self.tx_executor.state.get_actual_state_changes()?,
&execution_resources,
validate_call_info.iter(),
0,
)?;

Ok((validate_call_info, tx_receipt))
}
}
47 changes: 2 additions & 45 deletions crates/blockifier/src/blockifier/transaction_executor.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
use std::sync::Arc;

use cairo_vm::vm::runners::cairo_runner::ExecutionResources;
use itertools::FoldWhile::{Continue, Done};
use itertools::Itertools;
use starknet_api::core::ClassHash;
Expand All @@ -9,16 +6,13 @@ use thiserror::Error;
use crate::blockifier::config::TransactionExecutorConfig;
use crate::bouncer::{Bouncer, BouncerWeights};
use crate::context::BlockContext;
use crate::execution::call_info::CallInfo;
use crate::fee::actual_cost::TransactionReceipt;
use crate::state::cached_state::{CachedState, CommitmentStateDiff, TransactionalState};
use crate::state::errors::StateError;
use crate::state::state_api::{State, StateReader};
use crate::transaction::account_transaction::AccountTransaction;
use crate::state::state_api::StateReader;
use crate::transaction::errors::TransactionExecutionError;
use crate::transaction::objects::TransactionExecutionInfo;
use crate::transaction::transaction_execution::Transaction;
use crate::transaction::transactions::{ExecutableTransaction, ValidatableTransaction};
use crate::transaction::transactions::ExecutableTransaction;

#[cfg(test)]
#[path = "transaction_executor_test.rs"]
Expand Down Expand Up @@ -149,43 +143,6 @@ impl<S: StateReader> TransactionExecutor<S> {
results
}

pub fn validate(
&mut self,
account_tx: &AccountTransaction,
mut remaining_gas: u64,
) -> TransactionExecutorResult<(Option<CallInfo>, TransactionReceipt)> {
let mut execution_resources = ExecutionResources::default();
let tx_context = Arc::new(self.block_context.to_tx_context(account_tx));
let tx_info = &tx_context.tx_info;

// TODO(Amos, 01/12/2023): Delete this once deprecated txs call
// PyValidator.perform_validations().
// For fee charging purposes, the nonce-increment cost is taken into consideration when
// calculating the fees for validation.
// Note: This assumes that the state is reset between calls to validate.
self.state.increment_nonce(tx_info.sender_address())?;

let limit_steps_by_resources = true;
let validate_call_info = account_tx.validate_tx(
&mut self.state,
&mut execution_resources,
tx_context.clone(),
&mut remaining_gas,
limit_steps_by_resources,
)?;

let tx_receipt = TransactionReceipt::from_account_tx(
account_tx,
&tx_context,
&self.state.get_actual_state_changes()?,
&execution_resources,
validate_call_info.iter(),
0,
)?;

Ok((validate_call_info, tx_receipt))
}

/// Returns the state diff, a list of contract class hash with the corresponding list of
/// visited segment values and the block weights.
pub fn finalize(
Expand Down

0 comments on commit a34e203

Please sign in to comment.