From 762177e59e39277244d9cc7975285119125f2904 Mon Sep 17 00:00:00 2001 From: Yair Bakalchuk Date: Thu, 4 Jul 2024 15:09:35 +0300 Subject: [PATCH] refactor(gateway): prepare run_validate for mocking --- crates/gateway/src/gateway.rs | 4 +-- .../src/stateful_transaction_validator.rs | 32 ++++++++++++------- .../stateful_transaction_validator_test.rs | 26 +++++++++++---- 3 files changed, 42 insertions(+), 20 deletions(-) diff --git a/crates/gateway/src/gateway.rs b/crates/gateway/src/gateway.rs index cd51fb77a..b9432381a 100644 --- a/crates/gateway/src/gateway.rs +++ b/crates/gateway/src/gateway.rs @@ -125,8 +125,8 @@ fn process_tx( }; // TODO(Yael, 19/5/2024): pass the relevant deploy_account_hash. - let tx_hash = - stateful_tx_validator.run_validate(state_reader_factory, &tx, optional_class_info, None)?; + let validator = stateful_tx_validator.instantiate_validator(state_reader_factory)?; + let tx_hash = stateful_tx_validator.run_validate(&tx, optional_class_info, None, validator)?; // TODO(Arni): Add the Sierra and the Casm to the mempool input. Ok(MempoolInput { diff --git a/crates/gateway/src/stateful_transaction_validator.rs b/crates/gateway/src/stateful_transaction_validator.rs index bce61a693..b09f85946 100644 --- a/crates/gateway/src/stateful_transaction_validator.rs +++ b/crates/gateway/src/stateful_transaction_validator.rs @@ -1,5 +1,5 @@ use blockifier::blockifier::block::BlockInfo; -use blockifier::blockifier::stateful_validator::StatefulValidator as BlockifierStatefulValidator; +use blockifier::blockifier::stateful_validator::StatefulValidator; use blockifier::bouncer::BouncerConfig; use blockifier::context::BlockContext; use blockifier::execution::contract_class::ClassInfo; @@ -21,14 +21,30 @@ pub struct StatefulTransactionValidator { pub config: StatefulTransactionValidatorConfig, } +type BlockifierStatefulValidator = StatefulValidator>; + impl StatefulTransactionValidator { pub fn run_validate( &self, - state_reader_factory: &dyn StateReaderFactory, external_tx: &RPCTransaction, optional_class_info: Option, deploy_account_tx_hash: Option, + mut validator: BlockifierStatefulValidator, ) -> StatefulTransactionValidatorResult { + let account_tx = external_tx_to_account_tx( + external_tx, + optional_class_info, + &self.config.chain_info.chain_id, + )?; + let tx_hash = get_tx_hash(&account_tx); + validator.perform_validations(account_tx, deploy_account_tx_hash)?; + Ok(tx_hash) + } + + pub fn instantiate_validator( + &self, + state_reader_factory: &dyn StateReaderFactory, + ) -> StatefulTransactionValidatorResult { // TODO(yael 6/5/2024): consider storing the block_info as part of the // StatefulTransactionValidator and update it only once a new block is created. let latest_block_info = get_latest_block_info(state_reader_factory)?; @@ -53,19 +69,11 @@ impl StatefulTransactionValidator { BouncerConfig::max(), ); - let mut validator = BlockifierStatefulValidator::create( + Ok(BlockifierStatefulValidator::create( state, block_context, self.config.max_nonce_for_validation_skip, - ); - let account_tx = external_tx_to_account_tx( - external_tx, - optional_class_info, - &self.config.chain_info.chain_id, - )?; - let tx_hash = get_tx_hash(&account_tx); - validator.perform_validations(account_tx, deploy_account_tx_hash)?; - Ok(tx_hash) + )) } } diff --git a/crates/gateway/src/stateful_transaction_validator_test.rs b/crates/gateway/src/stateful_transaction_validator_test.rs index 8eebdcf67..c86cb1a89 100644 --- a/crates/gateway/src/stateful_transaction_validator_test.rs +++ b/crates/gateway/src/stateful_transaction_validator_test.rs @@ -84,11 +84,25 @@ fn test_stateful_tx_validator( _ => None, }; - let result = stateful_validator.run_validate( - &state_reader_factory, - &external_tx, - optional_class_info, - None, - ); + let validator = stateful_validator.instantiate_validator(&state_reader_factory).unwrap(); + + let result = + stateful_validator.run_validate(&external_tx, optional_class_info, None, validator); assert_eq!(format!("{:?}", result), format!("{:?}", expected_result)); } + +#[test] +fn test_instantiate_validator() { + let state_reader_factory = local_test_state_reader_factory(CairoVersion::Cairo1, false); + let block_context = &BlockContext::create_for_testing(); + let stateful_validator = StatefulTransactionValidator { + config: StatefulTransactionValidatorConfig { + max_nonce_for_validation_skip: Default::default(), + validate_max_n_steps: block_context.versioned_constants().validate_max_n_steps, + max_recursion_depth: block_context.versioned_constants().max_recursion_depth, + chain_info: block_context.chain_info().clone().into(), + }, + }; + let blockifier_validator = stateful_validator.instantiate_validator(&state_reader_factory); + assert!(blockifier_validator.is_ok()); +}