From 72a2b06a1db0179224374cfc72ce77bb166b9920 Mon Sep 17 00:00:00 2001 From: Arnon Hod Date: Thu, 11 Apr 2024 13:07:05 +0300 Subject: [PATCH] feat: rename file to stateless transaction validator (#40) --- crates/gateway/src/lib.rs | 2 +- ....rs => stateless_transaction_validator.rs} | 14 +++++------ ...> stateless_transaction_validator_test.rs} | 25 ++++++++++--------- 3 files changed, 21 insertions(+), 20 deletions(-) rename crates/gateway/src/{transaction_validator.rs => stateless_transaction_validator.rs} (87%) rename crates/gateway/src/{transaction_validator_test.rs => stateless_transaction_validator_test.rs} (79%) diff --git a/crates/gateway/src/lib.rs b/crates/gateway/src/lib.rs index 27ee44a2..d69c8278 100644 --- a/crates/gateway/src/lib.rs +++ b/crates/gateway/src/lib.rs @@ -1,7 +1,7 @@ pub mod errors; pub mod gateway; pub mod starknet_api_test_utils; -pub mod transaction_validator; +pub mod stateless_transaction_validator; use serde::{Deserialize, Serialize}; use std::collections::BTreeMap; diff --git a/crates/gateway/src/transaction_validator.rs b/crates/gateway/src/stateless_transaction_validator.rs similarity index 87% rename from crates/gateway/src/transaction_validator.rs rename to crates/gateway/src/stateless_transaction_validator.rs index 37ddf300..c2c86aa3 100644 --- a/crates/gateway/src/transaction_validator.rs +++ b/crates/gateway/src/stateless_transaction_validator.rs @@ -7,27 +7,27 @@ use starknet_api::transaction::Resource; use crate::errors::{TransactionValidatorError, TransactionValidatorResult}; #[cfg(test)] -#[path = "transaction_validator_test.rs"] +#[path = "stateless_transaction_validator_test.rs"] mod transaction_validator_test; #[derive(Default)] -pub struct TransactionValidatorConfig { +pub struct StatelessTransactionValidatorConfig { // If true, validates that the reousrce bounds are not zero. pub validate_non_zero_l1_gas_fee: bool, pub validate_non_zero_l2_gas_fee: bool, } -pub struct TransactionValidator { - pub config: TransactionValidatorConfig, +pub struct StatelessTransactionValidator { + pub config: StatelessTransactionValidatorConfig, } -impl TransactionValidator { - pub fn validate(&self, tx: ExternalTransaction) -> TransactionValidatorResult<()> { +impl StatelessTransactionValidator { + pub fn validate(&self, tx: &ExternalTransaction) -> TransactionValidatorResult<()> { // TODO(Arni, 1/5/2024): Add a mechanism that validate the sender address is not blocked. // TODO(Arni, 1/5/2024): Validate transaction version. // TODO(Arni, 4/4/2024): Validate tx signature and calldata are not too long. - self.validate_fee(&tx)?; + self.validate_fee(tx)?; Ok(()) } diff --git a/crates/gateway/src/transaction_validator_test.rs b/crates/gateway/src/stateless_transaction_validator_test.rs similarity index 79% rename from crates/gateway/src/transaction_validator_test.rs rename to crates/gateway/src/stateless_transaction_validator_test.rs index 8fda0d90..5d09d299 100644 --- a/crates/gateway/src/transaction_validator_test.rs +++ b/crates/gateway/src/stateless_transaction_validator_test.rs @@ -8,25 +8,26 @@ use crate::starknet_api_test_utils::{ create_external_invoke_tx_for_testing, non_zero_l1_resource_bounds_mapping, non_zero_l2_resource_bounds_mapping, zero_resource_bounds_mapping, }; -use crate::transaction_validator::{ - TransactionValidator, TransactionValidatorConfig, TransactionValidatorError, +use crate::stateless_transaction_validator::{ + StatelessTransactionValidator, StatelessTransactionValidatorConfig, TransactionValidatorError, TransactionValidatorResult, }; -const VALIDATOR_CONFIG_FOR_TESTING: TransactionValidatorConfig = TransactionValidatorConfig { - validate_non_zero_l1_gas_fee: true, - validate_non_zero_l2_gas_fee: false, -}; +const VALIDATOR_CONFIG_FOR_TESTING: StatelessTransactionValidatorConfig = + StatelessTransactionValidatorConfig { + validate_non_zero_l1_gas_fee: true, + validate_non_zero_l2_gas_fee: false, + }; -const VALIDATOR_CONFIG_FOR_L2_GAS_TESTING: TransactionValidatorConfig = - TransactionValidatorConfig { +const VALIDATOR_CONFIG_FOR_L2_GAS_TESTING: StatelessTransactionValidatorConfig = + StatelessTransactionValidatorConfig { validate_non_zero_l1_gas_fee: false, validate_non_zero_l2_gas_fee: true, }; #[rstest] #[case::ignore_resource_bounds( - TransactionValidatorConfig{ + StatelessTransactionValidatorConfig{ validate_non_zero_l1_gas_fee: false, validate_non_zero_l2_gas_fee: false, }, @@ -78,12 +79,12 @@ const VALIDATOR_CONFIG_FOR_L2_GAS_TESTING: TransactionValidatorConfig = Ok(()) )] fn test_transaction_validator( - #[case] config: TransactionValidatorConfig, + #[case] config: StatelessTransactionValidatorConfig, #[case] tx: ExternalTransaction, #[case] expected_result: TransactionValidatorResult<()>, ) { - let tx_validator = TransactionValidator { config }; - let result = tx_validator.validate(tx); + let tx_validator = StatelessTransactionValidator { config }; + let result = tx_validator.validate(&tx); assert_eq!(result, expected_result); }