Skip to content

Commit

Permalink
refactor: add account nonce as input of run_validate and remove valid…
Browse files Browse the repository at this point in the history
…ation output
  • Loading branch information
ArniStarkware committed Sep 19, 2024
1 parent 7e19e82 commit 2866c29
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 37 deletions.
16 changes: 8 additions & 8 deletions crates/gateway/src/gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 } },
})
}

Expand Down
17 changes: 3 additions & 14 deletions crates/gateway/src/stateful_transaction_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,9 @@ impl StatefulTransactionValidator {
pub fn run_validate<V: StatefulTransactionValidatorTrait>(
&self,
executable_tx: &ExecutableTransaction,
account_nonce: Nonce,
mut validator: V,
) -> StatefulTransactionValidatorResult<ValidateInfo> {
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.
Expand All @@ -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(
Expand Down Expand Up @@ -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,
}
25 changes: 10 additions & 15 deletions crates/gateway/src/stateful_transaction_validator_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -66,27 +65,29 @@ 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),
Err(STATEFUL_VALIDATOR_FEE_ERROR)
)]
fn test_stateful_tx_validator(
#[case] executable_tx: Transaction,
#[case] expected_result: BlockifierStatefulValidatorResult<ValidateInfo>,
#[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);
}

Expand Down Expand Up @@ -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);
}

0 comments on commit 2866c29

Please sign in to comment.