Skip to content

Commit

Permalink
chore: rename external_tx to rpc tx (#607)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArniStarkware authored Aug 27, 2024
1 parent e184ed1 commit f78c88f
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 36 deletions.
10 changes: 5 additions & 5 deletions crates/gateway/src/gateway_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::gateway::{add_tx, AppState, SharedMempoolClient};
use crate::state_reader_test_utils::{local_test_state_reader_factory, TestStateReaderFactory};
use crate::stateful_transaction_validator::StatefulTransactionValidator;
use crate::stateless_transaction_validator::StatelessTransactionValidator;
use crate::utils::external_tx_to_account_tx;
use crate::utils::rpc_tx_to_account_tx;

pub fn app_state(
mempool_client: SharedMempoolClient,
Expand Down Expand Up @@ -93,16 +93,16 @@ async fn to_bytes(res: Response) -> Bytes {
res.into_body().collect().await.unwrap().to_bytes()
}

fn calculate_hash(external_tx: &RpcTransaction) -> TransactionHash {
let optional_class_info = match &external_tx {
fn calculate_hash(rpc_tx: &RpcTransaction) -> TransactionHash {
let optional_class_info = match &rpc_tx {
RpcTransaction::Declare(_declare_tx) => {
panic!("Declare transactions are not supported in this test")
}
_ => None,
};

let account_tx = external_tx_to_account_tx(
external_tx,
let account_tx = rpc_tx_to_account_tx(
rpc_tx,
optional_class_info,
&ChainInfo::create_for_testing().chain_id,
)
Expand Down
13 changes: 5 additions & 8 deletions crates/gateway/src/stateful_transaction_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use tracing::error;
use crate::config::StatefulTransactionValidatorConfig;
use crate::errors::{GatewaySpecError, StatefulTransactionValidatorResult};
use crate::state_reader::{MempoolStateReader, StateReaderFactory};
use crate::utils::{external_tx_to_account_tx, get_sender_address};
use crate::utils::{get_sender_address, rpc_tx_to_account_tx};

#[cfg(test)]
#[path = "stateful_transaction_validator_test.rs"]
Expand Down Expand Up @@ -69,22 +69,19 @@ impl StatefulTransactionValidator {
// conversion is also relevant for the Mempool.
pub fn run_validate<V: StatefulTransactionValidatorTrait>(
&self,
external_tx: &RpcTransaction,
rpc_tx: &RpcTransaction,
optional_class_info: Option<ClassInfo>,
mut validator: V,
) -> StatefulTransactionValidatorResult<ValidateInfo> {
let account_tx = external_tx_to_account_tx(
external_tx,
optional_class_info,
&self.config.chain_info.chain_id,
)?;
let account_tx =
rpc_tx_to_account_tx(rpc_tx, optional_class_info, &self.config.chain_info.chain_id)?;
let tx_hash = account_tx.tx_hash();
let sender_address = get_sender_address(&account_tx);
let account_nonce = validator.get_nonce(sender_address).map_err(|e| {
error!("Failed to get nonce for sender address {}: {}", sender_address, e);
GatewaySpecError::UnexpectedError { data: "Internal server error.".to_owned() }
})?;
let skip_validate = skip_stateful_validations(external_tx, account_nonce);
let skip_validate = skip_stateful_validations(rpc_tx, account_nonce);
validator
.validate(account_tx, skip_validate)
.map_err(|err| GatewaySpecError::ValidationFailure { data: err.to_string() })?;
Expand Down
12 changes: 6 additions & 6 deletions crates/gateway/src/stateful_transaction_validator_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ fn stateful_validator(block_context: BlockContext) -> StatefulTransactionValidat
)]
#[case::invalid_tx(invoke_tx(CairoVersion::Cairo1), Err(STATEFUL_VALIDATOR_FEE_ERROR))]
fn test_stateful_tx_validator(
#[case] external_tx: RpcTransaction,
#[case] rpc_tx: RpcTransaction,
#[case] expected_result: BlockifierStatefulValidatorResult<ValidateInfo>,
stateful_validator: StatefulTransactionValidator,
) {
let optional_class_info = match &external_tx {
let optional_class_info = match &rpc_tx {
RpcTransaction::Declare(declare_tx) => Some(
ClassInfo::try_from(
GatewayCompiler::new_cairo_lang_compiler(SierraToCasmCompilationConfig::default())
Expand All @@ -103,7 +103,7 @@ fn test_stateful_tx_validator(
mock_validator.expect_validate().return_once(|_, _| expected_result.map(|_| ()));
mock_validator.expect_get_nonce().returning(|_| Ok(Nonce(Felt::ZERO)));

let result = stateful_validator.run_validate(&external_tx, optional_class_info, mock_validator);
let result = stateful_validator.run_validate(&rpc_tx, optional_class_info, mock_validator);
assert_eq!(result, expected_result_as_stateful_transaction_result);
}

Expand Down Expand Up @@ -159,12 +159,12 @@ fn test_instantiate_validator() {
false
)]
fn test_skip_stateful_validation(
#[case] external_tx: RpcTransaction,
#[case] rpc_tx: RpcTransaction,
#[case] sender_nonce: Nonce,
#[case] should_skip_validate: bool,
stateful_validator: StatefulTransactionValidator,
) {
let sender_address = external_tx.calculate_sender_address().unwrap();
let sender_address = rpc_tx.calculate_sender_address().unwrap();
let mut mock_validator = MockStatefulTransactionValidatorTrait::new();
mock_validator
.expect_get_nonce()
Expand All @@ -174,5 +174,5 @@ fn test_skip_stateful_validation(
.expect_validate()
.withf(move |_, skip_validate| *skip_validate == should_skip_validate)
.returning(|_, _| Ok(()));
let _ = stateful_validator.run_validate(&external_tx, None, mock_validator);
let _ = stateful_validator.run_validate(&rpc_tx, None, mock_validator);
}
16 changes: 6 additions & 10 deletions crates/gateway/src/stateless_transaction_validator_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use mempool_test_utils::declare_tx_args;
use mempool_test_utils::starknet_api_test_utils::{
create_resource_bounds_mapping,
external_declare_tx,
external_tx_for_testing,
rpc_tx_for_testing,
zero_resource_bounds_mapping,
TransactionType,
NON_EMPTY_RESOURCE_BOUNDS,
Expand Down Expand Up @@ -118,7 +118,7 @@ fn test_positive_flow(
tx_type: TransactionType,
) {
let tx_validator = StatelessTransactionValidator { config };
let tx = external_tx_for_testing(tx_type, resource_bounds, tx_calldata, signature);
let tx = rpc_tx_for_testing(tx_type, resource_bounds, tx_calldata, signature);

assert_matches!(tx_validator.validate(&tx), Ok(()));
}
Expand Down Expand Up @@ -154,12 +154,8 @@ fn test_invalid_resource_bounds(
tx_type: TransactionType,
) {
let tx_validator = StatelessTransactionValidator { config };
let tx = external_tx_for_testing(
tx_type,
resource_bounds,
calldata![],
TransactionSignature::default(),
);
let tx =
rpc_tx_for_testing(tx_type, resource_bounds, calldata![], TransactionSignature::default());

assert_eq!(tx_validator.validate(&tx).unwrap_err(), expected_error);
}
Expand All @@ -170,7 +166,7 @@ fn test_calldata_too_long(
) {
let tx_validator =
StatelessTransactionValidator { config: default_validator_config_for_testing().clone() };
let tx = external_tx_for_testing(
let tx = rpc_tx_for_testing(
tx_type,
zero_resource_bounds_mapping(),
calldata![Felt::ONE, Felt::TWO],
Expand All @@ -193,7 +189,7 @@ fn test_signature_too_long(
) {
let tx_validator =
StatelessTransactionValidator { config: default_validator_config_for_testing().clone() };
let tx = external_tx_for_testing(
let tx = rpc_tx_for_testing(
tx_type,
zero_resource_bounds_mapping(),
calldata![],
Expand Down
6 changes: 3 additions & 3 deletions crates/gateway/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ use tracing::error;

use crate::errors::{GatewaySpecError, StatefulTransactionValidatorResult};

pub fn external_tx_to_account_tx(
external_tx: &RpcTransaction,
pub fn rpc_tx_to_account_tx(
rpc_tx: &RpcTransaction,
// FIXME(yael 15/4/24): calculate class_info inside the function once compilation code is ready
optional_class_info: Option<ClassInfo>,
chain_id: &ChainId,
) -> StatefulTransactionValidatorResult<AccountTransaction> {
match external_tx {
match rpc_tx {
RpcTransaction::Declare(RpcDeclareTransaction::V3(tx)) => {
let declare_tx = DeclareTransaction::V3(DeclareTransactionV3 {
class_hash: ClassHash::default(), /* FIXME(yael 15/4/24): call the starknet-api
Expand Down
4 changes: 2 additions & 2 deletions crates/mempool_test_utils/src/starknet_api_test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub enum TransactionType {
Invoke,
}

pub fn external_tx_for_testing(
pub fn rpc_tx_for_testing(
tx_type: TransactionType,
resource_bounds: ResourceBoundsMapping,
calldata: Calldata,
Expand Down Expand Up @@ -530,7 +530,7 @@ pub fn external_declare_tx(declare_tx_args: DeclareTxArgs) -> RpcTransaction {
)
}

pub fn external_tx_to_json(tx: &RpcTransaction) -> String {
pub fn rpc_tx_to_json(tx: &RpcTransaction) -> String {
let mut tx_json = serde_json::to_value(tx)
.unwrap_or_else(|tx| panic!("Failed to serialize transaction: {tx:?}"));

Expand Down
4 changes: 2 additions & 2 deletions crates/tests-integration/src/integration_test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::net::SocketAddr;
use axum::body::Body;
use blockifier::test_utils::contracts::FeatureContract;
use mempool_test_utils::starknet_api_test_utils::{
external_tx_to_json,
rpc_tx_to_json,
MultiAccountTransactionGenerator,
};
use reqwest::{Client, Response};
Expand Down Expand Up @@ -70,7 +70,7 @@ impl GatewayClient {
// Prefer using assert_add_tx_success or other higher level methods of this client, to ensure
// tests are boilerplate and implementation-detail free.
pub async fn add_tx(&self, tx: &RpcTransaction) -> Response {
let tx_json = external_tx_to_json(tx);
let tx_json = rpc_tx_to_json(tx);
self.client
.post(format!("http://{}/add_tx", self.socket))
.header("content-type", "application/json")
Expand Down

0 comments on commit f78c88f

Please sign in to comment.