Skip to content

Commit

Permalink
feat: mock blockifier
Browse files Browse the repository at this point in the history
  • Loading branch information
yair-starkware committed Jul 8, 2024
1 parent 4ae18d9 commit ff15ad6
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 36 deletions.
72 changes: 72 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ hyper = { version = "0.14", features = ["client", "server", "http1", "http2", "t
indexmap = "2.1.0"
itertools = "0.13.0"
lazy_static = "1.4.0"
mockall = "0.12.1"
num-traits = "0.2"
num-bigint = { version = "0.4.5", default-features = false }
# TODO(YaelD, 28/5/2024): The special Papyrus version is needed in order to be aligned with the
Expand Down
1 change: 1 addition & 0 deletions crates/gateway/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ blockifier= { workspace = true , features = ["testing"] }
cairo-lang-starknet-classes.workspace = true
cairo-vm.workspace = true
hyper.workspace = true
mockall.workspace = true
num-traits.workspace = true
papyrus_config.workspace = true
papyrus_rpc.workspace = true
Expand Down
27 changes: 25 additions & 2 deletions crates/gateway/src/stateful_transaction_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ use blockifier::bouncer::BouncerConfig;
use blockifier::context::BlockContext;
use blockifier::execution::contract_class::ClassInfo;
use blockifier::state::cached_state::CachedState;
use blockifier::transaction::account_transaction::AccountTransaction;
use blockifier::versioned_constants::VersionedConstants;
use mockall::predicate::*;
use mockall::*;
use starknet_api::rpc_transaction::RPCTransaction;
use starknet_api::transaction::TransactionHash;

Expand All @@ -23,13 +26,33 @@ pub struct StatefulTransactionValidator {

type BlockifierStatefulValidator = GenericBlockifierStatefulValidator<Box<dyn MempoolStateReader>>;

#[automock]
pub trait StatefulTransactionValidatorTrait {
fn perform_validations(
&mut self,
account_tx: AccountTransaction,
deploy_account_tx_hash: Option<TransactionHash>,
) -> StatefulTransactionValidatorResult<()>;
}


impl StatefulTransactionValidatorTrait for BlockifierStatefulValidator {
fn perform_validations(
&mut self,
account_tx: AccountTransaction,
deploy_account_tx_hash: Option<TransactionHash>,
) -> StatefulTransactionValidatorResult<()> {
Ok(self.perform_validations(account_tx, deploy_account_tx_hash)?)
}
}

impl StatefulTransactionValidator {
pub fn run_validate(
pub fn run_validate<TStatefulTransactionValidator: StatefulTransactionValidatorTrait>(
&self,
external_tx: &RPCTransaction,
optional_class_info: Option<ClassInfo>,
deploy_account_tx_hash: Option<TransactionHash>,
mut validator: BlockifierStatefulValidator,
mut validator: TStatefulTransactionValidator,
) -> StatefulTransactionValidatorResult<TransactionHash> {
let account_tx = external_tx_to_account_tx(
external_tx,
Expand Down
48 changes: 14 additions & 34 deletions crates/gateway/src/stateful_transaction_validator_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,51 +8,26 @@ use starknet_api::felt;
use starknet_api::rpc_transaction::RPCTransaction;
use starknet_api::transaction::TransactionHash;
use test_utils::starknet_api_test_utils::{
declare_tx, deploy_account_tx, invoke_tx, VALID_L1_GAS_MAX_AMOUNT,
VALID_L1_GAS_MAX_PRICE_PER_UNIT,
invoke_tx, VALID_L1_GAS_MAX_AMOUNT, VALID_L1_GAS_MAX_PRICE_PER_UNIT,
};

use crate::compilation::compile_contract_class;
use crate::config::StatefulTransactionValidatorConfig;
use crate::errors::{StatefulTransactionValidatorError, StatefulTransactionValidatorResult};
use crate::state_reader_test_utils::{
local_test_state_reader_factory, local_test_state_reader_factory_for_deploy_account,
TestStateReaderFactory,
use crate::state_reader_test_utils::local_test_state_reader_factory;
use crate::stateful_transaction_validator::{
MockStatefulTransactionValidatorTrait, StatefulTransactionValidator,
};
use crate::stateful_transaction_validator::StatefulTransactionValidator;

#[rstest]
#[case::valid_invoke_tx_cairo1(
#[case::valid_tx(
invoke_tx(CairoVersion::Cairo1),
local_test_state_reader_factory(CairoVersion::Cairo1, false),
Ok(TransactionHash(felt!(
"0x007d70505b4487a4e1c1a4b4e4342cb5aa9e73b86d031891170c45a57ad8b4e6"
)))
)]
#[case::valid_invoke_tx_cairo0(
invoke_tx(CairoVersion::Cairo0),
local_test_state_reader_factory(CairoVersion::Cairo0, false),
Ok(TransactionHash(felt!(
"0x032e3a969a64027f15ce2b526d8dff47d47524c58ff0363f93ce4cbe7c280861"
)))
)]
#[case::valid_deploy_account_tx(
deploy_account_tx(),
local_test_state_reader_factory_for_deploy_account(&external_tx),
Ok(TransactionHash(felt!(
"0x013287740b37dc112391de4ef0f7cd7aeca323537ca2a78a1108c6aee5a55d70"
)))
)]
#[case::valid_declare_tx(
declare_tx(),
local_test_state_reader_factory(CairoVersion::Cairo1, false),
Ok(TransactionHash(felt!(
"0x02da54b89e00d2e201f8e3ed2bcc715a69e89aefdce88aff2d2facb8dec55c0a"
)))
)]
#[case::invalid_tx(
invoke_tx(CairoVersion::Cairo1),
local_test_state_reader_factory(CairoVersion::Cairo1, true),
Err(StatefulTransactionValidatorError::StatefulValidatorError(
StatefulValidatorError::TransactionPreValidationError(
TransactionPreValidationError::TransactionFeeError(
Expand All @@ -67,7 +42,6 @@ use crate::stateful_transaction_validator::StatefulTransactionValidator;
)]
fn test_stateful_tx_validator(
#[case] external_tx: RPCTransaction,
#[case] state_reader_factory: TestStateReaderFactory,
#[case] expected_result: StatefulTransactionValidatorResult<TransactionHash>,
) {
let block_context = &BlockContext::create_for_testing();
Expand All @@ -84,11 +58,17 @@ fn test_stateful_tx_validator(
_ => None,
};

let validator = stateful_validator.instantiate_validator(&state_reader_factory).unwrap();
let expected_result_msg = format!("{:?}", expected_result);

let mut mock_validator = MockStatefulTransactionValidatorTrait::new();
mock_validator.expect_perform_validations().return_once(|_, _| match expected_result {
Ok(..) => Ok(()),
Err(e) => Err(e),
});

let result =
stateful_validator.run_validate(&external_tx, optional_class_info, None, validator);
assert_eq!(format!("{:?}", result), format!("{:?}", expected_result));
stateful_validator.run_validate(&external_tx, optional_class_info, None, mock_validator);
assert_eq!(format!("{:?}", result), expected_result_msg);
}

#[test]
Expand Down

0 comments on commit ff15ad6

Please sign in to comment.