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 19, 2024
1 parent 71b97d2 commit ee53252
Show file tree
Hide file tree
Showing 7 changed files with 2,397 additions and 16 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.

3 changes: 2 additions & 1 deletion crates/gateway/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ serde.workspace = true
serde_json.workspace = true
starknet_api.workspace = true
starknet_mempool_types = { path = "../mempool_types", version = "0.0" }
starknet_sierra_compile ={ path = "../starknet_sierra_compile", version = "0.0" }
starknet_sierra_compile = { path = "../starknet_sierra_compile", version = "0.0" }
test_utils = { path = "../test_utils", version = "0.0"}
thiserror.workspace = true
tokio.workspace = true
validator.workspace = true
Expand Down
37 changes: 24 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::rpc_transaction::RPCTransaction;
use starknet_api::transaction::TransactionHash;
use starknet_mempool::communication::create_mempool_server;
Expand All @@ -15,8 +15,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, compile_contract_class, 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 All @@ -31,6 +31,8 @@ pub fn app_state(mempool_client: SharedMempoolClient) -> AppState {
validate_non_zero_l1_gas_fee: true,
max_calldata_length: 10,
max_signature_length: 2,
max_bytecode_size: 10000,
max_raw_class_size: 1000000,
..Default::default()
},
},
Expand All @@ -44,7 +46,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: RPCTransaction) {
// TODO: Add fixture.

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

let app_state = app_state(mempool_client);

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

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

fn calculate_hash(external_tx: &RPCTransaction) -> TransactionHash {
assert_matches!(
external_tx,
RPCTransaction::Invoke(_),
"Only Invoke supported for now, extend as needed."
);
match external_tx {
RPCTransaction::Invoke(_) | RPCTransaction::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 = match &external_tx {
RPCTransaction::Declare(declare_tx) => Some(compile_contract_class(declare_tx).unwrap()),
_ => None,
};

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)
}
28 changes: 27 additions & 1 deletion 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 @@ -14,10 +18,11 @@ use starknet_api::transaction::{
TransactionSignature, TransactionVersion,
};
use starknet_api::{calldata, stark_felt};
use test_utils::{get_absolute_path, CONTRACT_CLASS_FILE, TEST_FILES_FOLDER};

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

pub const VALID_L1_GAS_MAX_AMOUNT: u64 = 2214;
pub const VALID_L1_GAS_MAX_AMOUNT: u64 = 203483;
pub const VALID_L1_GAS_MAX_PRICE_PER_UNIT: u128 = 100000000000;

// Utils.
Expand Down Expand Up @@ -85,6 +90,27 @@ pub fn executable_resource_bounds_mapping() -> ResourceBoundsMapping {
)
}

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

let cairo_version = CairoVersion::Cairo1;
let account_contract = FeatureContract::AccountWithoutValidations(cairo_version);
let account_address = account_contract.get_instance_address(0);
let mut nonce_manager = NonceManager::default();
let nonce = nonce_manager.next(account_address);

external_declare_tx(declare_tx_args!(
signature: TransactionSignature(vec![StarkFelt::ZERO]),
sender_address: account_address,
resource_bounds: executable_resource_bounds_mapping(),
contract_class,
nonce
))
}

pub fn invoke_tx() -> RPCTransaction {
let cairo_version = CairoVersion::Cairo1;
let account_contract = FeatureContract::AccountWithoutValidations(cairo_version);
Expand Down
2 changes: 1 addition & 1 deletion crates/gateway/src/stateful_transaction_validator_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::stateful_transaction_validator::StatefulTransactionValidator;
#[case::valid_invoke_tx(
100000000000000000,
Ok(TransactionHash(StarkFelt::try_from(
"0x07459d76bd7adec02c25cf7ab0dcb95e9197101d4ada41cae6b465fcb78c0e47"
"0x007d70505b4487a4e1c1a4b4e4342cb5aa9e73b86d031891170c45a57ad8b4e6"
).unwrap()))
)]
#[case::invalid_invoke_tx(
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 CONTRACT_CLASS_FILE: &str = "contract_class.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 ee53252

Please sign in to comment.