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 25, 2024
1 parent 7c93ecc commit d7aba7b
Show file tree
Hide file tree
Showing 8 changed files with 2,409 additions and 15 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 @@ -24,6 +24,7 @@ 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" }
test_utils = { path = "../test_utils", version = "0.0"}
thiserror.workspace = true
tokio.workspace = true
validator.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion crates/gateway/src/gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ fn process_tx(
/// Formats the contract class for compilation, compiles it, and returns the compiled contract class
/// wrapped in a [`ClassInfo`].
/// Assumes the contract class is of a Sierra program which is compiled to Casm.
fn compile_contract_class(declare_tx: &RPCDeclareTransaction) -> GatewayResult<ClassInfo> {
pub fn compile_contract_class(declare_tx: &RPCDeclareTransaction) -> GatewayResult<ClassInfo> {
let RPCDeclareTransaction::V3(tx) = declare_tx;
let starknet_api_contract_class = &tx.contract_class;
let cairo_lang_contract_class =
Expand Down
23 changes: 15 additions & 8 deletions crates/gateway/src/gateway_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,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::{deploy_account_tx, invoke_tx};
use crate::gateway::{add_tx, compile_contract_class, AppState, SharedMempoolClient};
use crate::starknet_api_test_utils::{declare_tx, deploy_account_tx, invoke_tx};
use crate::state_reader_test_utils::{
local_test_state_reader_factory, local_test_state_reader_factory_for_deploy_account,
TestStateReaderFactory,
Expand All @@ -43,6 +43,8 @@ pub fn app_state(
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 @@ -69,6 +71,7 @@ pub fn app_state(
deploy_account_tx(),
local_test_state_reader_factory_for_deploy_account(&tx)
)]
#[case::declare_tx(declare_tx(), local_test_state_reader_factory(CairoVersion::Cairo1, false))]
async fn test_add_tx(
#[case] tx: RPCTransaction,
#[case] state_reader_factory: TestStateReaderFactory,
Expand Down Expand Up @@ -102,12 +105,16 @@ async fn to_bytes(res: Response) -> Bytes {
}

fn calculate_hash(external_tx: &RPCTransaction) -> TransactionHash {
if matches!(external_tx, RPCTransaction::Declare(_)) {
panic!("Declare transaction is not supported yet.");
}
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, None, &ChainInfo::create_for_testing().chain_id)
.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)
}
27 changes: 26 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 @@ -88,6 +93,26 @@ 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 contract_class = serde_json::from_reader(File::open(json_file_path).unwrap()).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(),
nonce,
contract_class
))
}

// TODO(Ayelet, 28/5/2025): Try unifying the macros.
// TODO(Ayelet, 28/5/2025): Consider moving the macros StarkNet API.
#[macro_export]
Expand Down
28 changes: 23 additions & 5 deletions crates/gateway/src/stateful_transaction_validator_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ use starknet_api::transaction::TransactionHash;

use crate::config::StatefulTransactionValidatorConfig;
use crate::errors::{StatefulTransactionValidatorError, StatefulTransactionValidatorResult};
use crate::gateway::compile_contract_class;
use crate::starknet_api_test_utils::{
deploy_account_tx, invoke_tx, VALID_L1_GAS_MAX_AMOUNT, VALID_L1_GAS_MAX_PRICE_PER_UNIT,
declare_tx, deploy_account_tx, invoke_tx, VALID_L1_GAS_MAX_AMOUNT,
VALID_L1_GAS_MAX_PRICE_PER_UNIT,
};
use crate::state_reader_test_utils::{
local_test_state_reader_factory, local_test_state_reader_factory_for_deploy_account,
Expand All @@ -23,21 +25,28 @@ use crate::stateful_transaction_validator::StatefulTransactionValidator;
invoke_tx(CairoVersion::Cairo1),
local_test_state_reader_factory(CairoVersion::Cairo1, false),
Ok(TransactionHash(StarkFelt::try_from(
"0x07459d76bd7adec02c25cf7ab0dcb95e9197101d4ada41cae6b465fcb78c0e47"
"0x007d70505b4487a4e1c1a4b4e4342cb5aa9e73b86d031891170c45a57ad8b4e6"
).unwrap()))
)]
#[case::valid_invoke_tx_cairo0(
invoke_tx(CairoVersion::Cairo0),
local_test_state_reader_factory(CairoVersion::Cairo0, false),
Ok(TransactionHash(StarkFelt::try_from(
"0x052358755cb14da1c0fddcdb371470f3ecf5ffe4cfa017cdfcedda4bff92a02e"
"0x032e3a969a64027f15ce2b526d8dff47d47524c58ff0363f93ce4cbe7c280861"
).unwrap()))
)]
#[case::valid_deploy_account_tx(
deploy_account_tx(),
local_test_state_reader_factory_for_deploy_account(&external_tx),
Ok(TransactionHash(StarkFelt::try_from(
"0x07fb8387575c7f4daa5996a3bb4a3010f4f4af1009b393c73198b8bc5e788c8f"
"0x013287740b37dc112391de4ef0f7cd7aeca323537ca2a78a1108c6aee5a55d70"
).unwrap()))
)]
#[case::valid_declare_tx(
declare_tx(),
local_test_state_reader_factory(CairoVersion::Cairo1, false),
Ok(TransactionHash(StarkFelt::try_from(
"0x0278ed2700d5a30254a6b895d4e1140438d7d1a3b2b2ce0c096a9d5ee1c61f39"
).unwrap()))
)]
#[case::invalid_tx(
Expand Down Expand Up @@ -70,7 +79,16 @@ fn test_stateful_tx_validator(
chain_info: block_context.chain_info().clone().into(),
},
};
let optional_class_info = match &external_tx {
RPCTransaction::Declare(declare_tx) => Some(compile_contract_class(declare_tx).unwrap()),
_ => None,
};

let result = stateful_validator.run_validate(&state_reader_factory, &external_tx, None, None);
let result = stateful_validator.run_validate(
&state_reader_factory,
&external_tx,
optional_class_info,
None,
);
assert_eq!(format!("{:?}", result), format!("{:?}", expected_result));
}
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 d7aba7b

Please sign in to comment.