diff --git a/crates/gateway/src/gateway.rs b/crates/gateway/src/gateway.rs index a6b0913f55f..ed96c4532f1 100644 --- a/crates/gateway/src/gateway.rs +++ b/crates/gateway/src/gateway.rs @@ -126,19 +126,19 @@ fn process_tx( } } - let validator = stateful_tx_validator.instantiate_validator(state_reader_factory)?; - // TODO(Yael 31/7/24): refactor after IntrnalTransaction is ready, delete validate_info and - // compute all the info outside of run_validate. - let validate_info = stateful_tx_validator.run_validate(&executable_tx, validator)?; + let mut validator = stateful_tx_validator.instantiate_validator(state_reader_factory)?; let sender_address = executable_tx.contract_address(); + let account_nonce = validator.get_nonce(sender_address).map_err(|e| { + error!("Failed to get nonce for sender address {}: {}", sender_address, e); + GatewaySpecError::UnexpectedError { data: "Internal server error.".to_owned() } + })?; + + stateful_tx_validator.run_validate(&executable_tx, account_nonce, validator)?; // TODO(Arni): Add the Sierra and the Casm to the mempool input. Ok(MempoolInput { tx: executable_tx, - account: Account { - sender_address, - state: AccountState { nonce: validate_info.account_nonce }, - }, + account: Account { sender_address, state: AccountState { nonce: account_nonce } }, }) } diff --git a/crates/gateway/src/stateful_transaction_validator.rs b/crates/gateway/src/stateful_transaction_validator.rs index 35449e41fdf..672fb2154bd 100644 --- a/crates/gateway/src/stateful_transaction_validator.rs +++ b/crates/gateway/src/stateful_transaction_validator.rs @@ -71,13 +71,9 @@ impl StatefulTransactionValidator { pub fn run_validate( &self, executable_tx: &ExecutableTransaction, + account_nonce: Nonce, mut validator: V, - ) -> StatefulTransactionValidatorResult { - let sender_address = executable_tx.contract_address(); - let account_nonce = validator.get_nonce(sender_address).map_err(|e| { - error!("Failed to get nonce for sender address {}: {}", sender_address, e); - GatewaySpecError::UnexpectedError { data: "Internal server error.".to_owned() } - })?; + ) -> StatefulTransactionValidatorResult<()> { let skip_validate = skip_stateful_validations(executable_tx, account_nonce); let account_tx = AccountTransaction::try_from( // TODO(Arni): create a try_from for &ExecutableTransaction. @@ -90,7 +86,7 @@ impl StatefulTransactionValidator { validator .validate(account_tx, skip_validate) .map_err(|err| GatewaySpecError::ValidationFailure { data: err.to_string() })?; - Ok(ValidateInfo { account_nonce }) + Ok(()) } pub fn instantiate_validator( @@ -145,10 +141,3 @@ pub fn get_latest_block_info( GatewaySpecError::UnexpectedError { data: "Internal server error.".to_owned() } }) } - -/// Holds members created by the stateful transaction validator, needed for -/// [`MempoolInput`](starknet_mempool_types::mempool_types::MempoolInput). -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub struct ValidateInfo { - pub account_nonce: Nonce, -} diff --git a/crates/gateway/src/stateful_transaction_validator_test.rs b/crates/gateway/src/stateful_transaction_validator_test.rs index 5f4024a4877..c5dd6ddf3bd 100644 --- a/crates/gateway/src/stateful_transaction_validator_test.rs +++ b/crates/gateway/src/stateful_transaction_validator_test.rs @@ -24,7 +24,6 @@ use starknet_api::{deploy_account_tx_args, invoke_tx_args}; use starknet_gateway_types::errors::GatewaySpecError; use starknet_types_core::felt::Felt; -use super::ValidateInfo; use crate::config::StatefulTransactionValidatorConfig; use crate::state_reader::{MockStateReaderFactory, StateReaderFactory}; use crate::state_reader_test_utils::local_test_state_reader_factory; @@ -66,7 +65,7 @@ fn stateful_validator(block_context: BlockContext) -> StatefulTransactionValidat #[rstest] #[case::valid_tx( create_executable_invoke_tx(CairoVersion::Cairo1), - Ok(ValidateInfo{account_nonce: Nonce::default()}) + Ok(()) )] #[case::invalid_tx( create_executable_invoke_tx(CairoVersion::Cairo1), @@ -74,19 +73,21 @@ fn stateful_validator(block_context: BlockContext) -> StatefulTransactionValidat )] fn test_stateful_tx_validator( #[case] executable_tx: Transaction, - #[case] expected_result: BlockifierStatefulValidatorResult, + #[case] expected_result: BlockifierStatefulValidatorResult<()>, stateful_validator: StatefulTransactionValidator, ) { - let expected_result_as_stateful_transaction_result = - expected_result.as_ref().map(|validate_info| *validate_info).map_err(|blockifier_error| { - GatewaySpecError::ValidationFailure { data: blockifier_error.to_string() } + let expected_result_as_stateful_transaction_result = expected_result + .as_ref() + .map(|validate_result| *validate_result) + .map_err(|blockifier_error| GatewaySpecError::ValidationFailure { + data: blockifier_error.to_string(), }); let mut mock_validator = MockStatefulTransactionValidatorTrait::new(); mock_validator.expect_validate().return_once(|_, _| expected_result.map(|_| ())); - mock_validator.expect_get_nonce().returning(|_| Ok(Nonce(Felt::ZERO))); - let result = stateful_validator.run_validate(&executable_tx, mock_validator); + let account_nonce = Nonce(Felt::ZERO); + let result = stateful_validator.run_validate(&executable_tx, account_nonce, mock_validator); assert_eq!(result, expected_result_as_stateful_transaction_result); } @@ -157,16 +158,10 @@ fn test_skip_stateful_validation( #[case] should_skip_validate: bool, stateful_validator: StatefulTransactionValidator, ) { - let sender_address = executable_tx.contract_address(); - let mut mock_validator = MockStatefulTransactionValidatorTrait::new(); - mock_validator - .expect_get_nonce() - .withf(move |contract_address| *contract_address == sender_address) - .returning(move |_| Ok(sender_nonce)); mock_validator .expect_validate() .withf(move |_, skip_validate| *skip_validate == should_skip_validate) .returning(|_, _| Ok(())); - let _ = stateful_validator.run_validate(&executable_tx, mock_validator); + let _ = stateful_validator.run_validate(&executable_tx, sender_nonce, mock_validator); }