Skip to content

Commit

Permalink
feat: rename file to stateless transaction validator (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArniStarkware authored Apr 11, 2024
1 parent 7b06d43 commit 72a2b06
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
2 changes: 1 addition & 1 deletion crates/gateway/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down Expand Up @@ -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);
}

0 comments on commit 72a2b06

Please sign in to comment.