Skip to content

Commit

Permalink
chore: simplify the use of rpc_tx_args (#1898)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArniStarkware authored Nov 10, 2024
1 parent 7122d01 commit 6df6962
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 34 deletions.
45 changes: 28 additions & 17 deletions crates/gateway/src/stateless_transaction_validator_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ use std::sync::LazyLock;
use std::vec;

use assert_matches::assert_matches;
use mempool_test_utils::declare_tx_args;
use mempool_test_utils::starknet_api_test_utils::{
rpc_declare_tx,
rpc_tx_for_testing,
RpcTransactionArgs,
TransactionType,
NON_EMPTY_RESOURCE_BOUNDS,
};
use mempool_test_utils::{declare_tx_args, rpc_tx_args};
use rstest::rstest;
use starknet_api::core::{ContractAddress, EntryPointSelector};
use starknet_api::rpc_transaction::{ContractClass, EntryPointByType};
Expand Down Expand Up @@ -53,55 +53,58 @@ static DEFAULT_VALIDATOR_CONFIG_FOR_TESTING: LazyLock<StatelessTransactionValida
validate_non_zero_l2_gas_fee: false,
..*DEFAULT_VALIDATOR_CONFIG_FOR_TESTING
},
rpc_tx_args!()
RpcTransactionArgs::default()
)]
#[case::valid_l1_gas(
StatelessTransactionValidatorConfig {
validate_non_zero_l1_gas_fee: true,
validate_non_zero_l2_gas_fee: false,
..*DEFAULT_VALIDATOR_CONFIG_FOR_TESTING
},
rpc_tx_args!(
RpcTransactionArgs{
resource_bounds: AllResourceBounds {
l1_gas: NON_EMPTY_RESOURCE_BOUNDS,
..Default::default()
}
)
},
..Default::default()
}
)]
#[case::valid_l2_gas(
StatelessTransactionValidatorConfig {
validate_non_zero_l1_gas_fee: false,
validate_non_zero_l2_gas_fee: true,
..*DEFAULT_VALIDATOR_CONFIG_FOR_TESTING
},
rpc_tx_args!(
RpcTransactionArgs {
resource_bounds: AllResourceBounds {
l2_gas: NON_EMPTY_RESOURCE_BOUNDS,
..Default::default()
}
)
},
..Default::default()
}
)]
#[case::valid_l1_and_l2_gas(
StatelessTransactionValidatorConfig {
validate_non_zero_l1_gas_fee: true,
validate_non_zero_l2_gas_fee: true,
..*DEFAULT_VALIDATOR_CONFIG_FOR_TESTING
},
rpc_tx_args!(
RpcTransactionArgs {
resource_bounds: AllResourceBounds {
l1_gas: NON_EMPTY_RESOURCE_BOUNDS,
l2_gas: NON_EMPTY_RESOURCE_BOUNDS,
..Default::default()
}
)
},
..Default::default()
}
)]
#[case::non_empty_valid_calldata(
DEFAULT_VALIDATOR_CONFIG_FOR_TESTING.clone(),
rpc_tx_args!( calldata: calldata![Felt::ONE])
RpcTransactionArgs { calldata: calldata![Felt::ONE], ..Default::default()}
)]
#[case::non_empty_valid_signature(
DEFAULT_VALIDATOR_CONFIG_FOR_TESTING.clone(),
rpc_tx_args!( signature: TransactionSignature(vec![Felt::ONE]))
RpcTransactionArgs { signature: TransactionSignature(vec![Felt::ONE]), ..Default::default()}
)]
#[case::valid_tx(DEFAULT_VALIDATOR_CONFIG_FOR_TESTING.clone(), RpcTransactionArgs::default())]
fn test_positive_flow(
Expand Down Expand Up @@ -153,7 +156,8 @@ fn test_invalid_resource_bounds(
) {
let tx_validator = StatelessTransactionValidator { config };

let tx = rpc_tx_for_testing(tx_type, rpc_tx_args!(resource_bounds));
let tx =
rpc_tx_for_testing(tx_type, RpcTransactionArgs { resource_bounds, ..Default::default() });

assert_eq!(tx_validator.validate(&tx).unwrap_err(), expected_error);
}
Expand All @@ -164,7 +168,10 @@ fn test_calldata_too_long(
) {
let tx_validator =
StatelessTransactionValidator { config: DEFAULT_VALIDATOR_CONFIG_FOR_TESTING.clone() };
let tx = rpc_tx_for_testing(tx_type, rpc_tx_args!(calldata: calldata![Felt::ONE, Felt::TWO]));
let tx = rpc_tx_for_testing(
tx_type,
RpcTransactionArgs { calldata: calldata![Felt::ONE, Felt::TWO], ..Default::default() },
);

assert_eq!(
tx_validator.validate(&tx).unwrap_err(),
Expand All @@ -184,7 +191,10 @@ fn test_signature_too_long(
StatelessTransactionValidator { config: DEFAULT_VALIDATOR_CONFIG_FOR_TESTING.clone() };
let tx = rpc_tx_for_testing(
tx_type,
rpc_tx_args!(signature: TransactionSignature(vec![Felt::ONE, Felt::TWO])),
RpcTransactionArgs {
signature: TransactionSignature(vec![Felt::ONE, Felt::TWO]),
..Default::default()
},
);

assert_eq!(
Expand Down Expand Up @@ -421,7 +431,8 @@ fn test_invalid_contract_address(
#[case] sender_address: ContractAddress,
#[values(TransactionType::Declare, TransactionType::Invoke)] tx_type: TransactionType,
) {
let tx = rpc_tx_for_testing(tx_type, rpc_tx_args!(sender_address));
let tx =
rpc_tx_for_testing(tx_type, RpcTransactionArgs { sender_address, ..Default::default() });

let tx_validator =
StatelessTransactionValidator { config: DEFAULT_VALIDATOR_CONFIG_FOR_TESTING.clone() };
Expand Down
18 changes: 1 addition & 17 deletions crates/mempool_test_utils/src/starknet_api_test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ pub enum TransactionType {
Invoke,
}

/// Transaction arguments used for the function [rpc_tx_for_testing].
#[derive(Clone)]
pub struct RpcTransactionArgs {
pub sender_address: ContractAddress,
Expand All @@ -83,23 +84,6 @@ impl Default for RpcTransactionArgs {
}
}

/// Utility macro for creating `RpcTransactionArgs` to reduce boilerplate.
#[macro_export]
macro_rules! rpc_tx_args {
($($field:ident $(: $value:expr)?),* $(,)?) => {
$crate::starknet_api_test_utils::RpcTransactionArgs {
$($field $(: $value)?,)*
..Default::default()
}
};
($($field:ident $(: $value:expr)?),* , ..$defaults:expr) => {
$crate::starknet_api_test_utils::RpcTransactionArgs {
$($field $(: $value)?,)*
..$defaults
}
};
}

pub fn rpc_tx_for_testing(
tx_type: TransactionType,
rpc_tx_args: RpcTransactionArgs,
Expand Down

0 comments on commit 6df6962

Please sign in to comment.