From 1efb99d52eafd44b6935ab12ad47e76be1c1571e Mon Sep 17 00:00:00 2001 From: Yair Bakalchuk Date: Mon, 8 Jul 2024 16:02:57 +0300 Subject: [PATCH] test(gateway): make sure we are using the latest block in the stateful validator --- crates/gateway/src/state_reader.rs | 3 +++ .../stateful_transaction_validator_test.rs | 22 ++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/crates/gateway/src/state_reader.rs b/crates/gateway/src/state_reader.rs index 66d5f1de1..0aa555b05 100644 --- a/crates/gateway/src/state_reader.rs +++ b/crates/gateway/src/state_reader.rs @@ -2,6 +2,8 @@ use blockifier::blockifier::block::BlockInfo; use blockifier::execution::contract_class::ContractClass; use blockifier::state::errors::StateError; use blockifier::state::state_api::{StateReader as BlockifierStateReader, StateResult}; +#[cfg(test)] +use mockall::automock; use starknet_api::block::BlockNumber; use starknet_api::core::{ClassHash, CompiledClassHash, ContractAddress, Nonce}; use starknet_api::state::StorageKey; @@ -11,6 +13,7 @@ pub trait MempoolStateReader: BlockifierStateReader + Send + Sync { fn get_block_info(&self) -> Result; } +#[cfg_attr(test, automock)] pub trait StateReaderFactory: Send + Sync { fn get_state_reader_from_latest_block(&self) -> Box; fn get_state_reader(&self, block_number: BlockNumber) -> Box; diff --git a/crates/gateway/src/stateful_transaction_validator_test.rs b/crates/gateway/src/stateful_transaction_validator_test.rs index 9d048ad74..187488230 100644 --- a/crates/gateway/src/stateful_transaction_validator_test.rs +++ b/crates/gateway/src/stateful_transaction_validator_test.rs @@ -9,6 +9,7 @@ use mempool_test_utils::starknet_api_test_utils::{ deploy_account_tx, external_invoke_tx, invoke_tx, TEST_SENDER_ADDRESS, VALID_L1_GAS_MAX_AMOUNT, VALID_L1_GAS_MAX_PRICE_PER_UNIT, }; +use mockall::predicate::eq; use num_bigint::BigUint; use pretty_assertions::assert_eq; use rstest::{fixture, rstest}; @@ -21,6 +22,7 @@ use starknet_types_core::felt::Felt; use crate::compilation::GatewayCompiler; use crate::config::{GatewayCompilerConfig, StatefulTransactionValidatorConfig}; use crate::errors::{StatefulTransactionValidatorError, StatefulTransactionValidatorResult}; +use crate::state_reader::{MockStateReaderFactory, StateReaderFactory}; use crate::state_reader_test_utils::{ local_test_state_reader_factory, TestStateReader, TestStateReaderFactory, }; @@ -93,6 +95,24 @@ fn test_stateful_tx_validator( #[test] fn test_instantiate_validator() { let state_reader_factory = local_test_state_reader_factory(CairoVersion::Cairo1, false); + + let mut mock_state_reader_factory = MockStateReaderFactory::new(); + + // Make sure stateful_validator uses the latest block in the initiall call. + let latest_state_reader = state_reader_factory.get_state_reader_from_latest_block(); + mock_state_reader_factory + .expect_get_state_reader_from_latest_block() + .return_once(|| latest_state_reader); + + // Make sure stateful_validator uses the latest block in the following calls to the + // state_reader. + let latest_block = state_reader_factory.state_reader.block_info.block_number; + let state_reader = state_reader_factory.get_state_reader(latest_block); + mock_state_reader_factory + .expect_get_state_reader() + .with(eq(latest_block)) + .return_once(move |_| state_reader); + let block_context = &BlockContext::create_for_testing(); let stateful_validator = StatefulTransactionValidator { config: StatefulTransactionValidatorConfig { @@ -102,7 +122,7 @@ fn test_instantiate_validator() { chain_info: block_context.chain_info().clone().into(), }, }; - let blockifier_validator = stateful_validator.instantiate_validator(&state_reader_factory); + let blockifier_validator = stateful_validator.instantiate_validator(&mock_state_reader_factory); assert!(blockifier_validator.is_ok()); }