Skip to content

Commit

Permalink
test: Add tests for add tx on declare tx
Browse files Browse the repository at this point in the history
  • Loading branch information
ArniStarkware committed Jun 16, 2024
1 parent af43060 commit f57c7fc
Show file tree
Hide file tree
Showing 6 changed files with 2,403 additions and 13 deletions.
1 change: 1 addition & 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 crates/gateway/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ tempfile.workspace = true
thiserror.workspace = true
tokio.workspace = true
validator.workspace = true
test_utils = { path = "../test_utils", version = "0.0"}

[dev-dependencies]
assert_matches.workspace = true
Expand Down
33 changes: 20 additions & 13 deletions crates/gateway/src/gateway_test.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::sync::Arc;

use assert_matches::assert_matches;
use axum::body::{Bytes, HttpBody};
use axum::extract::State;
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use blockifier::context::ChainInfo;
use rstest::rstest;
use starknet_api::external_transaction::ExternalTransaction;
use starknet_api::transaction::TransactionHash;
use starknet_mempool::mempool::{create_mempool_server, Mempool};
Expand All @@ -14,8 +14,8 @@ use tokio::sync::mpsc::channel;
use tokio::task;

use crate::config::{StatefulTransactionValidatorConfig, StatelessTransactionValidatorConfig};
use crate::gateway::{add_tx, AppState, SharedMempoolClient};
use crate::starknet_api_test_utils::invoke_tx;
use crate::gateway::{add_tx, get_optional_class_info, AppState, SharedMempoolClient};
use crate::starknet_api_test_utils::{declare_tx, invoke_tx};
use crate::state_reader_test_utils::local_test_state_reader_factory;
use crate::stateful_transaction_validator::StatefulTransactionValidator;
use crate::stateless_transaction_validator::StatelessTransactionValidator;
Expand Down Expand Up @@ -43,7 +43,8 @@ pub fn app_state(mempool_client: SharedMempoolClient) -> AppState {

// TODO(Ayelet): add test cases for declare and deploy account transactions.
#[tokio::test]
async fn test_add_tx() {
#[rstest]
async fn test_add_tx(#[values(declare_tx(), invoke_tx())] tx: ExternalTransaction) {
// TODO: Add fixture.

let mempool = Mempool::new([]);
Expand All @@ -60,7 +61,7 @@ async fn test_add_tx() {

let app_state = app_state(mempool_client);

let tx = invoke_tx();
// Scenario based test.
let tx_hash = calculate_hash(&tx);
let response = add_tx(State(app_state), tx.into()).await.into_response();

Expand All @@ -76,14 +77,20 @@ async fn to_bytes(res: Response) -> Bytes {
}

fn calculate_hash(external_tx: &ExternalTransaction) -> TransactionHash {
assert_matches!(
external_tx,
ExternalTransaction::Invoke(_),
"Only Invoke supported for now, extend as needed."
);
match external_tx {
ExternalTransaction::Invoke(_) | ExternalTransaction::Declare(_) => {}
_ => {
panic!("Only Declare and Invoke are supported for now, extend as needed.");
}
}

let account_tx =
external_tx_to_account_tx(external_tx, None, &ChainInfo::create_for_testing().chain_id)
.unwrap();
let optional_class_info = get_optional_class_info(external_tx).unwrap();

let account_tx = external_tx_to_account_tx(
external_tx,
optional_class_info,
&ChainInfo::create_for_testing().chain_id,
)
.unwrap();
get_tx_hash(&account_tx)
}
13 changes: 13 additions & 0 deletions crates/gateway/src/starknet_api_test_utils.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
use std::env;
use std::fs::File;
use std::path::Path;

use blockifier::test_utils::contracts::FeatureContract;
use blockifier::test_utils::{create_trivial_calldata, CairoVersion, NonceManager};
use serde_json::to_string_pretty;
Expand All @@ -15,6 +19,7 @@ use starknet_api::transaction::{
AccountDeploymentData, Calldata, ContractAddressSalt, PaymasterData, ResourceBounds, Tip,
TransactionSignature, TransactionVersion,
};
use test_utils::{get_absolute_path, DECLARE_V3_TX_FILE, TEST_FILES_FOLDER};

use crate::{declare_tx_args, deploy_account_tx_args, invoke_tx_args};

Expand Down Expand Up @@ -82,6 +87,14 @@ pub fn executable_resource_bounds_mapping() -> ResourceBoundsMapping {
)
}

pub fn declare_tx() -> ExternalTransaction {
env::set_current_dir(get_absolute_path(TEST_FILES_FOLDER)).expect("Couldn't set working dir.");
let json_file_path = Path::new(DECLARE_V3_TX_FILE);
let json_file = File::open(json_file_path).unwrap();
let tx: ExternalTransaction = serde_json::from_reader(json_file).unwrap();
tx
}

pub fn invoke_tx() -> ExternalTransaction {
let cairo_version = CairoVersion::Cairo1;
let account_contract = FeatureContract::AccountWithoutValidations(cairo_version);
Expand Down
3 changes: 3 additions & 0 deletions crates/test_utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::env;
use std::path::{Path, PathBuf};

pub const TEST_FILES_FOLDER: &str = "crates/test_utils/test_files";
pub const DECLARE_V3_TX_FILE: &str = "declare_v3.json";

/// Returns the absolute path from the project root.
pub fn get_absolute_path(relative_path: &str) -> PathBuf {
Path::new(&env::var("CARGO_MANIFEST_DIR").unwrap()).join("../..").join(relative_path)
Expand Down
Loading

0 comments on commit f57c7fc

Please sign in to comment.