166 lines
4.6 KiB
Rust
Raw Normal View History

2025-10-03 15:59:27 -03:00
use base64::{Engine, engine::general_purpose::STANDARD as BASE64};
2025-10-22 13:55:35 +03:00
use std::path::PathBuf;
2025-07-28 16:31:43 +03:00
2025-07-31 14:40:23 +03:00
use actix_web::dev::ServerHandle;
2025-07-25 19:42:29 +03:00
use anyhow::Result;
2025-07-28 16:31:43 +03:00
use clap::Parser;
2025-10-03 15:59:27 -03:00
use common::{
sequencer_client::SequencerClient,
transaction::{EncodedTransaction, NSSATransaction},
};
2025-08-22 15:58:43 +03:00
use log::{info, warn};
2025-10-22 13:55:35 +03:00
use nssa::PrivacyPreservingTransaction;
use nssa_core::Commitment;
2025-08-01 18:32:30 +03:00
use sequencer_core::config::SequencerConfig;
use sequencer_runner::startup_sequencer;
2025-07-31 14:40:23 +03:00
use tempfile::TempDir;
2025-08-06 14:56:58 +03:00
use tokio::task::JoinHandle;
2025-10-22 13:55:35 +03:00
use crate::test_suite_map::prepare_function_map;
#[macro_use]
extern crate proc_macro_test_attribute;
pub mod test_suite_map;
2025-07-28 16:31:43 +03:00
#[derive(Parser, Debug)]
#[clap(version)]
struct Args {
/// Path to configs
home_dir: PathBuf,
2025-07-31 14:40:23 +03:00
/// Test name
test_name: String,
2025-07-28 16:31:43 +03:00
}
2025-07-25 19:42:29 +03:00
2025-10-24 11:12:32 +03:00
pub const ACC_SENDER: &str = "BLgCRDXYdQPMMWVHYRFGQZbgeHx9frkipa8GtpG2Syqy";
pub const ACC_RECEIVER: &str = "Gj1mJy5W7J5pfmLRujmQaLfLMWidNxQ6uwnhb666ZwHw";
2025-07-30 14:01:40 +03:00
2025-10-24 11:12:32 +03:00
pub const ACC_SENDER_PRIVATE: &str = "3oCG8gqdKLMegw4rRfyaMQvuPHpcASt7xwttsmnZLSkw";
pub const ACC_RECEIVER_PRIVATE: &str = "AKTcXgJ1xoynta1Ec7y6Jso1z1JQtHqd7aPQ1h9er6xX";
2025-07-31 14:40:23 +03:00
pub const TIME_TO_WAIT_FOR_BLOCK_SECONDS: u64 = 12;
2025-07-28 16:31:43 +03:00
pub const NSSA_PROGRAM_FOR_TEST_DATA_CHANGER: &[u8] = include_bytes!("data_changer.bin");
2025-10-15 20:14:19 -03:00
2025-10-27 14:32:28 +02:00
fn make_public_account_input_from_str(addr: &str) -> String {
format!("Public/{addr:?}")
}
fn make_private_account_input_from_str(addr: &str) -> String {
format!("Private/{addr:?}")
}
2025-07-31 14:40:23 +03:00
#[allow(clippy::type_complexity)]
pub async fn pre_test(
home_dir: PathBuf,
2025-08-06 14:56:58 +03:00
) -> Result<(ServerHandle, JoinHandle<Result<()>>, TempDir)> {
2025-07-30 14:01:40 +03:00
let home_dir_sequencer = home_dir.join("sequencer");
2025-07-31 14:40:23 +03:00
let mut sequencer_config =
2025-07-30 14:01:40 +03:00
sequencer_runner::config::from_file(home_dir_sequencer.join("sequencer_config.json"))
.unwrap();
2025-07-28 16:31:43 +03:00
2025-08-06 14:56:58 +03:00
let temp_dir_sequencer = replace_home_dir_with_temp_dir_in_configs(&mut sequencer_config);
2025-07-31 14:40:23 +03:00
2025-08-01 18:32:30 +03:00
let (seq_http_server_handle, sequencer_loop_handle) =
startup_sequencer(sequencer_config).await?;
2025-07-28 16:31:43 +03:00
2025-07-31 14:40:23 +03:00
Ok((
seq_http_server_handle,
sequencer_loop_handle,
temp_dir_sequencer,
))
}
pub fn replace_home_dir_with_temp_dir_in_configs(
sequencer_config: &mut SequencerConfig,
2025-08-06 14:56:58 +03:00
) -> TempDir {
2025-07-31 14:40:23 +03:00
let temp_dir_sequencer = tempfile::tempdir().unwrap();
sequencer_config.home = temp_dir_sequencer.path().to_path_buf();
2025-08-06 14:56:58 +03:00
temp_dir_sequencer
2025-07-31 14:40:23 +03:00
}
#[allow(clippy::type_complexity)]
2025-08-06 14:56:58 +03:00
pub async fn post_test(residual: (ServerHandle, JoinHandle<Result<()>>, TempDir)) {
let (seq_http_server_handle, sequencer_loop_handle, _) = residual;
2025-07-31 14:40:23 +03:00
info!("Cleanup");
2025-07-30 14:01:40 +03:00
2025-07-31 14:40:23 +03:00
sequencer_loop_handle.abort();
seq_http_server_handle.stop(true).await;
2025-08-22 15:58:43 +03:00
let wallet_home = wallet::helperfunctions::get_home().unwrap();
let persistent_data_home = wallet_home.join("curr_accounts.json");
//Removing persistent accounts after run to not affect other executions
//Not necessary an error, if fails as there is tests for failure scenario
let _ = std::fs::remove_file(persistent_data_home)
.inspect_err(|err| warn!("Failed to remove persistent data with err {err:#?}"));
2025-08-11 08:55:08 +03:00
//At this point all of the references to sequencer_core must be lost.
2025-07-31 14:40:23 +03:00
//So they are dropped and tempdirs will be dropped too,
}
pub async fn main_tests_runner() -> Result<()> {
env_logger::init();
let args = Args::parse();
let Args {
home_dir,
test_name,
} = args;
2025-10-22 13:55:35 +03:00
let function_map = prepare_function_map();
2025-07-31 14:40:23 +03:00
match test_name.as_str() {
"all" => {
2025-10-22 13:55:35 +03:00
for (_, fn_pointer) in function_map {
fn_pointer(home_dir.clone()).await;
}
2025-07-31 14:40:23 +03:00
}
_ => {
2025-10-22 13:55:35 +03:00
let fn_pointer = function_map.get(&test_name).expect("Unknown test name");
fn_pointer(home_dir.clone()).await;
2025-07-31 14:40:23 +03:00
}
}
2025-07-30 14:01:40 +03:00
Ok(())
2025-07-25 19:42:29 +03:00
}
2025-10-03 15:59:27 -03:00
async fn fetch_privacy_preserving_tx(
seq_client: &SequencerClient,
tx_hash: String,
) -> PrivacyPreservingTransaction {
let transaction_encoded = seq_client
.get_transaction_by_hash(tx_hash.clone())
.await
.unwrap()
.transaction
.unwrap();
let tx_base64_decode = BASE64.decode(transaction_encoded).unwrap();
match NSSATransaction::try_from(
&borsh::from_slice::<EncodedTransaction>(&tx_base64_decode).unwrap(),
)
.unwrap()
{
2025-10-03 15:59:27 -03:00
NSSATransaction::PrivacyPreserving(privacy_preserving_transaction) => {
privacy_preserving_transaction
}
_ => panic!("Invalid tx type"),
}
}
async fn verify_commitment_is_in_state(
commitment: Commitment,
seq_client: &SequencerClient,
) -> bool {
2025-10-03 17:06:45 -03:00
matches!(
seq_client.get_proof_for_commitment(commitment).await,
Ok(Some(_))
)
2025-10-03 15:59:27 -03:00
}