Skip to content

Commit

Permalink
test(gateway): make sure we are using the latest block in the statefu…
Browse files Browse the repository at this point in the history
…l validator (#401)
  • Loading branch information
yair-starkware authored Jul 23, 2024
1 parent 7f2356e commit ef2a585
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
3 changes: 3 additions & 0 deletions crates/gateway/src/state_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -11,6 +13,7 @@ pub trait MempoolStateReader: BlockifierStateReader + Send + Sync {
fn get_block_info(&self) -> Result<BlockInfo, StateError>;
}

#[cfg_attr(test, automock)]
pub trait StateReaderFactory: Send + Sync {
fn get_state_reader_from_latest_block(&self) -> Box<dyn MempoolStateReader>;
fn get_state_reader(&self, block_number: BlockNumber) -> Box<dyn MempoolStateReader>;
Expand Down
22 changes: 21 additions & 1 deletion crates/gateway/src/stateful_transaction_validator_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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,
};
Expand Down Expand Up @@ -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 {
Expand All @@ -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());
}

Expand Down

0 comments on commit ef2a585

Please sign in to comment.