use std::{path::PathBuf, time::Duration}; use actix_web::dev::ServerHandle; use anyhow::Result; use clap::Parser; use common::sequencer_client::SequencerClient; use log::info; use sequencer_core::config::SequencerConfig; use sequencer_runner::startup_sequencer; use tempfile::TempDir; use tokio::task::JoinHandle; use wallet::{Command, helperfunctions::fetch_config}; #[derive(Parser, Debug)] #[clap(version)] struct Args { /// Path to configs home_dir: PathBuf, /// Test name test_name: String, } pub const ACC_SENDER: &str = "0d96dfcc414019380c9dde0cd3dce5aac90fb5443bf871108741aeafde552ad7"; pub const ACC_RECEIVER: &str = "974870e9be8d0ac08aa83b3fc7a7a686291d8732508aba98b36080f39c2cf364"; pub const TIME_TO_WAIT_FOR_BLOCK_SECONDS: u64 = 12; #[allow(clippy::type_complexity)] pub async fn pre_test( home_dir: PathBuf, ) -> Result<(ServerHandle, JoinHandle>, TempDir)> { let home_dir_sequencer = home_dir.join("sequencer"); let mut sequencer_config = sequencer_runner::config::from_file(home_dir_sequencer.join("sequencer_config.json")) .unwrap(); let temp_dir_sequencer = replace_home_dir_with_temp_dir_in_configs(&mut sequencer_config); let (seq_http_server_handle, sequencer_loop_handle) = startup_sequencer(sequencer_config).await?; 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, ) -> TempDir { let temp_dir_sequencer = tempfile::tempdir().unwrap(); sequencer_config.home = temp_dir_sequencer.path().to_path_buf(); temp_dir_sequencer } #[allow(clippy::type_complexity)] pub async fn post_test(residual: (ServerHandle, JoinHandle>, TempDir)) { let (seq_http_server_handle, sequencer_loop_handle, _) = residual; info!("Cleanup"); sequencer_loop_handle.abort(); seq_http_server_handle.stop(true).await; //At this point all of the references to node_core and sequencer_core must be lost. //So they are dropped and tempdirs will be dropped too, } pub async fn test_success() { let command = Command::SendNativeTokenTransfer { from: ACC_SENDER.to_string(), to: ACC_RECEIVER.to_string(), amount: 100, }; let node_config = fetch_config().unwrap(); let seq_client = SequencerClient::new(node_config.sequencer_addr.clone()).unwrap(); wallet::execute_subcommand(command).await.unwrap(); info!("Waiting for next block creation"); tokio::time::sleep(Duration::from_secs(TIME_TO_WAIT_FOR_BLOCK_SECONDS)).await; info!("Checking correct balance move"); let acc_1_balance = seq_client .get_account_balance(ACC_SENDER.to_string()) .await .unwrap(); let acc_2_balance = seq_client .get_account_balance(ACC_RECEIVER.to_string()) .await .unwrap(); info!("Balance of sender : {acc_1_balance:#?}"); info!("Balance of receiver : {acc_2_balance:#?}"); assert_eq!(acc_1_balance.balance, 9900); assert_eq!(acc_2_balance.balance, 20100); info!("Success!"); } pub async fn test_success_move_to_another_account() { let hex_acc_receiver_new_acc = hex::encode([42; 32]); let command = Command::SendNativeTokenTransfer { from: ACC_SENDER.to_string(), to: hex_acc_receiver_new_acc.clone(), amount: 100, }; let node_config = fetch_config().unwrap(); let seq_client = SequencerClient::new(node_config.sequencer_addr.clone()).unwrap(); wallet::execute_subcommand(command).await.unwrap(); info!("Waiting for next block creation"); tokio::time::sleep(Duration::from_secs(TIME_TO_WAIT_FOR_BLOCK_SECONDS)).await; info!("Checking correct balance move"); let acc_1_balance = seq_client .get_account_balance(ACC_SENDER.to_string()) .await .unwrap(); let acc_2_balance = seq_client .get_account_balance(hex_acc_receiver_new_acc) .await .unwrap(); info!("Balance of sender : {acc_1_balance:#?}"); info!("Balance of receiver : {acc_2_balance:#?}"); assert_eq!(acc_1_balance.balance, 9900); assert_eq!(acc_2_balance.balance, 100); info!("Success!"); } pub async fn test_failure() { let command = Command::SendNativeTokenTransfer { from: ACC_SENDER.to_string(), to: ACC_RECEIVER.to_string(), amount: 1000000, }; let node_config = fetch_config().unwrap(); let seq_client = SequencerClient::new(node_config.sequencer_addr.clone()).unwrap(); wallet::execute_subcommand(command).await.unwrap(); info!("Waiting for next block creation"); tokio::time::sleep(Duration::from_secs(TIME_TO_WAIT_FOR_BLOCK_SECONDS)).await; info!("Checking correct balance move"); let acc_1_balance = seq_client .get_account_balance(ACC_SENDER.to_string()) .await .unwrap(); let acc_2_balance = seq_client .get_account_balance(ACC_RECEIVER.to_string()) .await .unwrap(); info!("Balance of sender : {acc_1_balance:#?}"); info!("Balance of receiver : {acc_2_balance:#?}"); assert_eq!(acc_1_balance.balance, 10000); assert_eq!(acc_2_balance.balance, 20000); info!("Success!"); } macro_rules! test_cleanup_wrap { ($home_dir:ident, $test_func:ident) => {{ let res = pre_test($home_dir.clone()).await.unwrap(); info!("Waiting for first block creation"); tokio::time::sleep(Duration::from_secs(TIME_TO_WAIT_FOR_BLOCK_SECONDS)).await; $test_func().await; post_test(res).await; }}; } pub async fn main_tests_runner() -> Result<()> { env_logger::init(); let args = Args::parse(); let Args { home_dir, test_name, } = args; match test_name.as_str() { "test_success_move_to_another_account" => { test_cleanup_wrap!(home_dir, test_success_move_to_another_account); } "test_success" => { test_cleanup_wrap!(home_dir, test_success); } "test_failure" => { test_cleanup_wrap!(home_dir, test_failure); } "all" => { test_cleanup_wrap!(home_dir, test_success_move_to_another_account); test_cleanup_wrap!(home_dir, test_success); test_cleanup_wrap!(home_dir, test_failure); } _ => { anyhow::bail!("Unknown test name"); } } Ok(()) }