224 lines
6.4 KiB
Rust
Raw Normal View History

2025-08-06 14:56:58 +03:00
use std::{path::PathBuf, time::Duration};
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-08-07 14:07:34 +03:00
use common::sequencer_client::SequencerClient;
2025-07-28 16:31:43 +03:00
use log::info;
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-08-07 14:07:34 +03:00
use wallet::{Command, helperfunctions::fetch_config};
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-07-30 14:01:40 +03:00
pub const ACC_SENDER: &str = "0d96dfcc414019380c9dde0cd3dce5aac90fb5443bf871108741aeafde552ad7";
pub const ACC_RECEIVER: &str = "974870e9be8d0ac08aa83b3fc7a7a686291d8732508aba98b36080f39c2cf364";
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
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;
//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,
}
2025-08-06 14:56:58 +03:00
pub async fn test_success() {
let command = Command::SendNativeTokenTransfer {
from: ACC_SENDER.to_string(),
to: ACC_RECEIVER.to_string(),
amount: 100,
};
2025-07-30 14:01:40 +03:00
2025-08-06 14:56:58 +03:00
let node_config = fetch_config().unwrap();
2025-07-30 14:01:40 +03:00
2025-08-07 14:07:34 +03:00
let seq_client = SequencerClient::new(node_config.sequencer_addr.clone()).unwrap();
2025-08-06 14:56:58 +03:00
2025-08-07 14:07:34 +03:00
wallet::execute_subcommand(command).await.unwrap();
2025-07-30 14:01:40 +03:00
2025-07-31 14:40:23 +03:00
info!("Waiting for next block creation");
tokio::time::sleep(Duration::from_secs(TIME_TO_WAIT_FOR_BLOCK_SECONDS)).await;
2025-07-30 14:01:40 +03:00
2025-07-31 14:40:23 +03:00
info!("Checking correct balance move");
2025-08-06 14:56:58 +03:00
let acc_1_balance = seq_client
2025-07-31 14:40:23 +03:00
.get_account_balance(ACC_SENDER.to_string())
.await
.unwrap();
2025-08-06 14:56:58 +03:00
let acc_2_balance = seq_client
2025-07-31 14:40:23 +03:00
.get_account_balance(ACC_RECEIVER.to_string())
.await
.unwrap();
2025-07-30 14:01:40 +03:00
2025-07-31 14:40:23 +03:00
info!("Balance of sender : {acc_1_balance:#?}");
info!("Balance of receiver : {acc_2_balance:#?}");
2025-07-30 14:01:40 +03:00
2025-07-31 14:40:23 +03:00
assert_eq!(acc_1_balance.balance, 9900);
assert_eq!(acc_2_balance.balance, 20100);
2025-07-30 14:01:40 +03:00
info!("Success!");
2025-07-31 14:40:23 +03:00
}
2025-07-30 14:01:40 +03:00
2025-08-06 14:56:58 +03:00
pub async fn test_success_move_to_another_account() {
let hex_acc_receiver_new_acc = hex::encode([42; 32]);
2025-07-30 14:01:40 +03:00
2025-08-06 14:56:58 +03:00
let command = Command::SendNativeTokenTransfer {
from: ACC_SENDER.to_string(),
to: hex_acc_receiver_new_acc.clone(),
amount: 100,
};
2025-07-31 14:40:23 +03:00
2025-08-06 14:56:58 +03:00
let node_config = fetch_config().unwrap();
2025-07-31 14:40:23 +03:00
2025-08-07 14:07:34 +03:00
let seq_client = SequencerClient::new(node_config.sequencer_addr.clone()).unwrap();
2025-08-06 14:56:58 +03:00
2025-08-07 14:07:34 +03:00
wallet::execute_subcommand(command).await.unwrap();
2025-07-31 14:40:23 +03:00
info!("Waiting for next block creation");
tokio::time::sleep(Duration::from_secs(TIME_TO_WAIT_FOR_BLOCK_SECONDS)).await;
info!("Checking correct balance move");
2025-08-06 14:56:58 +03:00
let acc_1_balance = seq_client
2025-07-31 14:40:23 +03:00
.get_account_balance(ACC_SENDER.to_string())
.await
.unwrap();
2025-08-06 14:56:58 +03:00
let acc_2_balance = seq_client
2025-08-01 18:32:30 +03:00
.get_account_balance(hex_acc_receiver_new_acc)
2025-07-31 14:40:23 +03:00
.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!");
}
2025-08-06 14:56:58 +03:00
pub async fn test_failure() {
let command = Command::SendNativeTokenTransfer {
from: ACC_SENDER.to_string(),
to: ACC_RECEIVER.to_string(),
amount: 1000000,
};
2025-07-31 14:40:23 +03:00
2025-08-06 14:56:58 +03:00
let node_config = fetch_config().unwrap();
2025-07-31 14:40:23 +03:00
2025-08-07 14:07:34 +03:00
let seq_client = SequencerClient::new(node_config.sequencer_addr.clone()).unwrap();
2025-08-06 14:56:58 +03:00
2025-08-07 14:07:34 +03:00
wallet::execute_subcommand(command).await.unwrap();
2025-07-31 14:40:23 +03:00
info!("Waiting for next block creation");
tokio::time::sleep(Duration::from_secs(TIME_TO_WAIT_FOR_BLOCK_SECONDS)).await;
info!("Checking correct balance move");
2025-08-06 14:56:58 +03:00
let acc_1_balance = seq_client
2025-07-31 14:40:23 +03:00
.get_account_balance(ACC_SENDER.to_string())
.await
.unwrap();
2025-08-06 14:56:58 +03:00
let acc_2_balance = seq_client
2025-07-31 14:40:23 +03:00
.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!");
}
2025-07-31 15:41:36 +03:00
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;
2025-08-06 14:56:58 +03:00
$test_func().await;
2025-07-31 15:41:36 +03:00
post_test(res).await;
}};
}
2025-07-31 14:40:23 +03:00
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" => {
2025-07-31 15:41:36 +03:00
test_cleanup_wrap!(home_dir, test_success_move_to_another_account);
2025-07-31 14:40:23 +03:00
}
"test_success" => {
2025-07-31 15:41:36 +03:00
test_cleanup_wrap!(home_dir, test_success);
2025-07-31 14:40:23 +03:00
}
"test_failure" => {
2025-07-31 15:41:36 +03:00
test_cleanup_wrap!(home_dir, test_failure);
2025-07-31 14:40:23 +03:00
}
"all" => {
2025-07-31 15:41:36 +03:00
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);
2025-07-31 14:40:23 +03:00
}
_ => {
anyhow::bail!("Unknown test name");
}
}
2025-07-30 14:01:40 +03:00
Ok(())
2025-07-25 19:42:29 +03:00
}