Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Yair/tmp #395

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
4 changes: 2 additions & 2 deletions crates/gateway/src/gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
57 changes: 44 additions & 13 deletions crates/gateway/src/stateful_transaction_validator.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use blockifier::blockifier::block::BlockInfo;
use blockifier::blockifier::stateful_validator::StatefulValidator as BlockifierStatefulValidator;
use blockifier::blockifier::stateful_validator::StatefulValidator as GenericBlockifierStatefulValidator;
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 @@ -21,14 +24,50 @@ pub struct StatefulTransactionValidator {
pub config: StatefulTransactionValidatorConfig,
}

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,
state_reader_factory: &dyn StateReaderFactory,
external_tx: &RPCTransaction,
optional_class_info: Option<ClassInfo>,
deploy_account_tx_hash: Option<TransactionHash>,
mut validator: TStatefulTransactionValidator,
) -> StatefulTransactionValidatorResult<TransactionHash> {
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<BlockifierStatefulValidator> {
// 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)?;
Expand All @@ -53,19 +92,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)
))
}
}

Expand Down
70 changes: 32 additions & 38 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,31 @@ fn test_stateful_tx_validator(
_ => None,
};

let result = stateful_validator.run_validate(
&state_reader_factory,
&external_tx,
optional_class_info,
None,
);
assert_eq!(format!("{:?}", result), format!("{:?}", expected_result));
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, mock_validator);
assert_eq!(format!("{:?}", result), expected_result_msg);
}

#[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());
}
Loading