From 190d07130fc315ff446e989649f64ff17d2180f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ceyhun=20=C5=9Een?= Date: Thu, 12 Dec 2024 12:43:04 +0300 Subject: [PATCH] mock_macro: Use create_test_config_with_thread_name in everywhere. --- core/src/bin/all_servers.rs | 49 --- core/src/database/common.rs | 52 +-- core/src/database/mod.rs | 13 +- core/src/header_chain_prover/blockgazer.rs | 20 +- core/src/header_chain_prover/mod.rs | 14 +- core/src/header_chain_prover/prover.rs | 14 +- core/src/mock/database.rs | 100 ++--- core/src/mock_macro.rs | 4 +- core/src/operator.rs | 14 +- core/src/rpc/aggregator.rs | 8 +- core/src/rpc/watchtower.rs | 6 +- core/src/servers.rs | 11 +- core/src/user.rs | 9 +- core/src/verifier.rs | 11 +- core/src/watchtower.rs | 10 +- core/tests/common/deposit.rs | 415 -------------------- core/tests/common/mod.rs | 421 ++++++++++++++++++++- core/tests/taproot.rs | 7 +- 18 files changed, 580 insertions(+), 598 deletions(-) delete mode 100644 core/src/bin/all_servers.rs diff --git a/core/src/bin/all_servers.rs b/core/src/bin/all_servers.rs deleted file mode 100644 index 1b9f0151..00000000 --- a/core/src/bin/all_servers.rs +++ /dev/null @@ -1,49 +0,0 @@ -use clementine_core::{ - musig2::AggregateFromPublicKeys, servers::create_verifiers_and_operators, - utils::get_configuration_for_binaries, -}; - -#[tokio::main] -async fn main() { - let (config, _) = get_configuration_for_binaries(); - - let (verifier_clients, operator_clients, aggregator) = - create_verifiers_and_operators("test_config.toml").await; - - println!( - "OPERATOR_URLS={}", - operator_clients - .iter() - .map(|(_, _, addr)| format!("http://127.0.0.1:{}", addr.port())) - .collect::>() - .join(",") - ); - println!( - "VERIFIER_URLS={}", - verifier_clients - .iter() - .map(|(_, _, addr)| format!("http://127.0.0.1:{}", addr.port())) - .collect::>() - .join(",") - ); - let xonly = - secp256k1::XOnlyPublicKey::from_musig2_pks(config.verifiers_public_keys, None, false); - println!( - "AGGREGATOR_URL={}", - format!("http://127.0.0.1:{}", aggregator.2.port()) - ); - println!("VERIFIER_PKS={}", xonly.to_string()); - - // Stop all servers - for (_, handle, _) in operator_clients { - handle.clone().stopped().await; - } - - for (_, handle, _) in verifier_clients { - handle.clone().stopped().await; - } - - aggregator.1.stopped().await; - - println!("All servers stopped"); -} diff --git a/core/src/database/common.rs b/core/src/database/common.rs index 780a40be..058daa71 100644 --- a/core/src/database/common.rs +++ b/core/src/database/common.rs @@ -976,8 +976,9 @@ impl Database { #[cfg(test)] mod tests { use super::Database; + use crate::{config::BridgeConfig, initialize_database, utils::initialize_logger}; use crate::{ - mock::database::create_test_config_with_thread_name, + create_test_config_with_thread_name, musig2::{nonce_pair, MuSigAggNonce, MuSigPubNonce, MuSigSecNonce}, ByteArray32, EVMAddress, UTXO, }; @@ -996,10 +997,11 @@ mod tests { use risc0_zkvm::Receipt; use secp256k1::{constants::SCHNORR_SIGNATURE_SIZE, rand::rngs::OsRng}; use secp256k1::{schnorr, Secp256k1}; + use std::{env, thread}; #[tokio::test] async fn save_get_timeout_tx_sigs() { - let config = create_test_config_with_thread_name("test_config.toml", None).await; + let config = create_test_config_with_thread_name!("test_config.toml", None); let database = Database::new(&config).await.unwrap(); let signatures: Vec = (0..0x45) @@ -1018,7 +1020,7 @@ mod tests { #[tokio::test] async fn test_database_gets_previously_saved_operator_take_signature() { - let config = create_test_config_with_thread_name("test_config.toml", None).await; + let config = create_test_config_with_thread_name!("test_config.toml", None); let database = Database::new(&config).await.unwrap(); let deposit_outpoint = OutPoint::null(); @@ -1056,7 +1058,7 @@ mod tests { #[tokio::test] async fn test_save_and_get_deposit_info() { - let config = create_test_config_with_thread_name("test_config.toml", None).await; + let config = create_test_config_with_thread_name!("test_config.toml", None); let database = Database::new(&config).await.unwrap(); let secp = Secp256k1::new(); @@ -1089,7 +1091,7 @@ mod tests { #[tokio::test] async fn test_nonces_1() { - let config = create_test_config_with_thread_name("test_config.toml", None).await; + let config = create_test_config_with_thread_name!("test_config.toml", None); let db = Database::new(&config).await.unwrap(); let secp = Secp256k1::new(); @@ -1134,7 +1136,7 @@ mod tests { #[tokio::test] async fn test_nonces_2() { - let config = create_test_config_with_thread_name("test_config.toml", None).await; + let config = create_test_config_with_thread_name!("test_config.toml", None); let db = Database::new(&config).await.unwrap(); let secp = Secp256k1::new(); @@ -1178,7 +1180,7 @@ mod tests { #[tokio::test] async fn test_nonces_3() { - let config = create_test_config_with_thread_name("test_config.toml", None).await; + let config = create_test_config_with_thread_name!("test_config.toml", None); let db = Database::new(&config).await.unwrap(); let secp = Secp256k1::new(); @@ -1226,7 +1228,7 @@ mod tests { #[tokio::test] async fn test_get_pub_nonces_1() { - let config = create_test_config_with_thread_name("test_config.toml", None).await; + let config = create_test_config_with_thread_name!("test_config.toml", None); let db = Database::new(&config).await.unwrap(); let secp = Secp256k1::new(); @@ -1259,7 +1261,7 @@ mod tests { #[tokio::test] async fn test_get_pub_nonces_2() { - let config = create_test_config_with_thread_name("test_config.toml", None).await; + let config = create_test_config_with_thread_name!("test_config.toml", None); let db = Database::new(&config).await.unwrap(); let outpoint = OutPoint { txid: Txid::from_byte_array([1u8; 32]), @@ -1271,7 +1273,7 @@ mod tests { #[tokio::test] async fn test_operators_kickoff_utxo_1() { - let config = create_test_config_with_thread_name("test_config.toml", None).await; + let config = create_test_config_with_thread_name!("test_config.toml", None); let db = Database::new(&config).await.unwrap(); let outpoint = OutPoint { @@ -1296,7 +1298,7 @@ mod tests { #[tokio::test] async fn test_operators_kickoff_utxo_2() { - let config = create_test_config_with_thread_name("test_config.toml", None).await; + let config = create_test_config_with_thread_name!("test_config.toml", None); let db = Database::new(&config).await.unwrap(); let outpoint = OutPoint { @@ -1309,7 +1311,7 @@ mod tests { #[tokio::test] async fn test_verifiers_kickoff_utxos_1() { - let config = create_test_config_with_thread_name("test_config.toml", None).await; + let config = create_test_config_with_thread_name!("test_config.toml", None); let db = Database::new(&config).await.unwrap(); let outpoint = OutPoint { @@ -1346,7 +1348,7 @@ mod tests { #[tokio::test] async fn test_verifiers_kickoff_utxos_2() { - let config = create_test_config_with_thread_name("test_config.toml", None).await; + let config = create_test_config_with_thread_name!("test_config.toml", None); let db = Database::new(&config).await.unwrap(); let outpoint = OutPoint { @@ -1359,7 +1361,7 @@ mod tests { #[tokio::test] async fn test_operators_funding_utxo_1() { - let config = create_test_config_with_thread_name("test_config.toml", None).await; + let config = create_test_config_with_thread_name!("test_config.toml", None); let db = Database::new(&config).await.unwrap(); let utxo = UTXO { @@ -1381,7 +1383,7 @@ mod tests { #[tokio::test] async fn test_operators_funding_utxo_2() { - let config = create_test_config_with_thread_name("test_config.toml", None).await; + let config = create_test_config_with_thread_name!("test_config.toml", None); let db = Database::new(&config).await.unwrap(); let db_utxo = db.get_funding_utxo(None).await.unwrap(); @@ -1391,7 +1393,7 @@ mod tests { #[tokio::test] async fn test_deposit_kickoff_generator_tx_0() { - let config = create_test_config_with_thread_name("test_config.toml", None).await; + let config = create_test_config_with_thread_name!("test_config.toml", None); let db = Database::new(&config).await.unwrap(); let raw_hex = "02000000000101eb87b1a80d47b7f5bd5082b77653f5ca37e566951742b80c361875ba0e5c478f0a00000000fdffffff0ca086010000000000225120b23da6d2e0390018b953f7d74e3582da4da30fd0fd157cc84a2d2753003d1ca3a086010000000000225120b23da6d2e0390018b953f7d74e3582da4da30fd0fd157cc84a2d2753003d1ca3a086010000000000225120b23da6d2e0390018b953f7d74e3582da4da30fd0fd157cc84a2d2753003d1ca3a086010000000000225120b23da6d2e0390018b953f7d74e3582da4da30fd0fd157cc84a2d2753003d1ca3a086010000000000225120b23da6d2e0390018b953f7d74e3582da4da30fd0fd157cc84a2d2753003d1ca3a086010000000000225120b23da6d2e0390018b953f7d74e3582da4da30fd0fd157cc84a2d2753003d1ca3a086010000000000225120b23da6d2e0390018b953f7d74e3582da4da30fd0fd157cc84a2d2753003d1ca3a086010000000000225120b23da6d2e0390018b953f7d74e3582da4da30fd0fd157cc84a2d2753003d1ca3a086010000000000225120b23da6d2e0390018b953f7d74e3582da4da30fd0fd157cc84a2d2753003d1ca3a086010000000000225120b23da6d2e0390018b953f7d74e3582da4da30fd0fd157cc84a2d2753003d1ca35c081777000000002251202a64b1ee3375f3bb4b367b8cb8384a47f73cf231717f827c6c6fbbf5aecf0c364a010000000000002200204ae81572f06e1b88fd5ced7a1a000945432e83e1551e6f721ee9c00b8cc33260014005a41e6f4a4bcfcc5cd3ef602687215f97c18949019a491df56af7413c5dce9292ba3966edc4564a39d9bc0d6c0faae19030f1cedf4d931a6cdc57cc5b83c8ef00000000".to_string(); @@ -1437,7 +1439,7 @@ mod tests { #[tokio::test] async fn test_deposit_kickoff_generator_tx_2() { - let config = create_test_config_with_thread_name("test_config.toml", None).await; + let config = create_test_config_with_thread_name!("test_config.toml", None); let db = Database::new(&config).await.unwrap(); let txid = Txid::from_byte_array([1u8; 32]); @@ -1447,7 +1449,7 @@ mod tests { #[tokio::test] async fn test_deposit_kickoff_generator_tx_1() { - let config = create_test_config_with_thread_name("test_config.toml", None).await; + let config = create_test_config_with_thread_name!("test_config.toml", None); let db = Database::new(&config).await.unwrap(); let raw_hex = "01000000000101308d840c736eefd114a8fad04cb0d8338b4a3034a2b517250e5498701b25eb360100000000fdffffff02401f00000000000022512024985a1ab5724a5164ae5e0026b3e7e22031e83948eedf99d438b866857946b81f7e000000000000225120f7298da2a2be5b6e02a076ff7d35a1fe6b54a2bc7938c1c86bede23cadb7d9650140ad2fdb01ec5e2772f682867c8c6f30697c63f622e338f7390d3abc6c905b9fd7e96496fdc34cb9e872387758a6a334ec1307b3505b73121e0264fe2ba546d78ad11b0d00".to_string(); @@ -1497,7 +1499,7 @@ mod tests { #[tokio::test] async fn test_deposit_kickoff_generator_tx_3() { - let config = create_test_config_with_thread_name("test_config.toml", None).await; + let config = create_test_config_with_thread_name!("test_config.toml", None); let db = Database::new(&config).await.unwrap(); let txid = Txid::from_byte_array([1u8; 32]); @@ -1507,7 +1509,7 @@ mod tests { #[tokio::test] async fn save_get_new_block() { - let config = create_test_config_with_thread_name("test_config.toml", None).await; + let config = create_test_config_with_thread_name!("test_config.toml", None); let db = Database::new(&config).await.unwrap(); let block = block::Block { @@ -1536,7 +1538,7 @@ mod tests { #[tokio::test] async fn get_block_header() { - let config = create_test_config_with_thread_name("test_config.toml", None).await; + let config = create_test_config_with_thread_name!("test_config.toml", None); let db = Database::new(&config).await.unwrap(); let block = block::Block { @@ -1573,7 +1575,7 @@ mod tests { #[tokio::test] pub async fn get_latest_chain_proof_height() { - let config = create_test_config_with_thread_name("test_config.toml", None).await; + let config = create_test_config_with_thread_name!("test_config.toml", None); let db = Database::new(&config).await.unwrap(); let mut block = block::Block { @@ -1629,7 +1631,7 @@ mod tests { #[tokio::test] pub async fn save_get_block_proof() { - let config = create_test_config_with_thread_name("test_config.toml", None).await; + let config = create_test_config_with_thread_name!("test_config.toml", None); let db = Database::new(&config).await.unwrap(); // Save dummy block. @@ -1673,7 +1675,7 @@ mod tests { #[tokio::test] pub async fn get_non_proven_block() { - let config = create_test_config_with_thread_name("test_config.toml", None).await; + let config = create_test_config_with_thread_name!("test_config.toml", None); let db = Database::new(&config).await.unwrap(); assert!(db.get_non_proven_block(None).await.is_err()); @@ -1749,7 +1751,7 @@ mod tests { #[tokio::test] async fn save_get_winternitz_public_key() { - let config = create_test_config_with_thread_name("test_config.toml", None).await; + let config = create_test_config_with_thread_name!("test_config.toml", None); let database = Database::new(&config).await.unwrap(); let wpk: winternitz::PublicKey = vec![[0x45; 20]]; diff --git a/core/src/database/mod.rs b/core/src/database/mod.rs index e84c70bd..03daa9a1 100644 --- a/core/src/database/mod.rs +++ b/core/src/database/mod.rs @@ -131,15 +131,14 @@ impl Database { #[cfg(test)] mod tests { - use crate::{ - config::BridgeConfig, - database::Database, - mock::{common, database::create_test_config_with_thread_name}, - }; + use crate::create_test_config_with_thread_name; + use crate::{config::BridgeConfig, database::Database}; + use crate::{initialize_database, utils::initialize_logger}; + use std::{env, thread}; #[tokio::test] async fn valid_database_connection() { - let config = create_test_config_with_thread_name("test_config.toml", None).await; + let config = create_test_config_with_thread_name!("test_config.toml", None); Database::new(&config).await.unwrap(); } @@ -159,7 +158,7 @@ mod tests { #[tokio::test] async fn create_drop_database() { - let mut config = common::get_test_config("test_config.toml").unwrap(); + let mut config = create_test_config_with_thread_name!("test_config.toml", None); config.db_name = "create_drop_database".to_string(); // Drop database (clear previous test run artifacts) and check that diff --git a/core/src/header_chain_prover/blockgazer.rs b/core/src/header_chain_prover/blockgazer.rs index 56776c38..a1706ce3 100644 --- a/core/src/header_chain_prover/blockgazer.rs +++ b/core/src/header_chain_prover/blockgazer.rs @@ -203,15 +203,19 @@ impl HeaderChainProver { #[cfg(test)] mod tests { use crate::{ + config::BridgeConfig, database::Database, initialize_database, utils::initialize_logger, + }; + use crate::{ + create_test_config_with_thread_name, extended_rpc::ExtendedRpc, header_chain_prover::{ blockgazer::{BlockFetchStatus, BATCH_DEEPNESS, BATCH_DEEPNESS_SAFETY_BARRIER}, HeaderChainProver, }, - mock::database::create_test_config_with_thread_name, }; use bitcoin::BlockHash; use bitcoincore_rpc::RpcApi; + use std::{env, thread}; async fn mine_and_save_blocks(prover: &HeaderChainProver, height: u64) -> Vec { let mut fork_block_hashes = Vec::new(); @@ -253,7 +257,7 @@ mod tests { #[tokio::test] #[serial_test::serial] async fn check_for_new_blocks_uptodate() { - let config = create_test_config_with_thread_name("test_config.toml", None).await; + let config = create_test_config_with_thread_name!("test_config.toml", None); let rpc = ExtendedRpc::new( config.bitcoin_rpc_url.clone(), config.bitcoin_rpc_user.clone(), @@ -287,7 +291,7 @@ mod tests { #[tokio::test] #[serial_test::serial] async fn check_for_new_blocks_fallen_behind_single() { - let config = create_test_config_with_thread_name("test_config.toml", None).await; + let config = create_test_config_with_thread_name!("test_config.toml", None); let rpc = ExtendedRpc::new( config.bitcoin_rpc_url.clone(), config.bitcoin_rpc_user.clone(), @@ -328,7 +332,7 @@ mod tests { #[tokio::test] #[serial_test::serial] async fn check_for_new_blocks_fallen_behind_multiple() { - let config = create_test_config_with_thread_name("test_config.toml", None).await; + let config = create_test_config_with_thread_name!("test_config.toml", None); let rpc = ExtendedRpc::new( config.bitcoin_rpc_url.clone(), config.bitcoin_rpc_user.clone(), @@ -359,7 +363,7 @@ mod tests { #[tokio::test] #[serial_test::serial] async fn check_for_new_blocks_fork_and_mine_new() { - let config = create_test_config_with_thread_name("test_config.toml", None).await; + let config = create_test_config_with_thread_name!("test_config.toml", None); let rpc = ExtendedRpc::new( config.bitcoin_rpc_url.clone(), config.bitcoin_rpc_user.clone(), @@ -391,7 +395,7 @@ mod tests { #[tokio::test] #[serial_test::serial] async fn sync_blockchain_single_block() { - let config = create_test_config_with_thread_name("test_config.toml", None).await; + let config = create_test_config_with_thread_name!("test_config.toml", None); let rpc = ExtendedRpc::new( config.bitcoin_rpc_url.clone(), config.bitcoin_rpc_user.clone(), @@ -425,7 +429,7 @@ mod tests { #[tokio::test] #[serial_test::serial] async fn sync_blockchain_multiple_blocks() { - let config = create_test_config_with_thread_name("test_config.toml", None).await; + let config = create_test_config_with_thread_name!("test_config.toml", None); let rpc = ExtendedRpc::new( config.bitcoin_rpc_url.clone(), config.bitcoin_rpc_user.clone(), @@ -459,7 +463,7 @@ mod tests { #[tokio::test] #[serial_test::serial] async fn sync_blockchain_multiple_blocks_with_fork() { - let config = create_test_config_with_thread_name("test_config.toml", None).await; + let config = create_test_config_with_thread_name!("test_config.toml", None); let rpc = ExtendedRpc::new( config.bitcoin_rpc_url.clone(), config.bitcoin_rpc_user.clone(), diff --git a/core/src/header_chain_prover/mod.rs b/core/src/header_chain_prover/mod.rs index 6752c138..1c8797f2 100644 --- a/core/src/header_chain_prover/mod.rs +++ b/core/src/header_chain_prover/mod.rs @@ -99,19 +99,23 @@ impl HeaderChainProver { #[cfg(test)] mod tests { use crate::{ - extended_rpc::ExtendedRpc, header_chain_prover::HeaderChainProver, - mock::database::create_test_config_with_thread_name, + config::BridgeConfig, database::Database, initialize_database, utils::initialize_logger, + }; + use crate::{ + create_test_config_with_thread_name, extended_rpc::ExtendedRpc, + header_chain_prover::HeaderChainProver, }; use bitcoin::{hashes::Hash, BlockHash}; use bitcoincore_rpc::RpcApi; use borsh::BorshDeserialize; use risc0_zkvm::Receipt; use std::time::Duration; + use std::{env, thread}; use tokio::time::sleep; #[tokio::test] async fn new() { - let config = create_test_config_with_thread_name("test_config.toml", None).await; + let config = create_test_config_with_thread_name!("test_config.toml", None); let rpc = ExtendedRpc::new( config.bitcoin_rpc_url.clone(), config.bitcoin_rpc_user.clone(), @@ -125,7 +129,7 @@ mod tests { #[tokio::test] #[serial_test::serial] async fn new_with_proof_assumption() { - let config = create_test_config_with_thread_name("test_config.toml", None).await; + let config = create_test_config_with_thread_name!("test_config.toml", None); let rpc = ExtendedRpc::new( config.bitcoin_rpc_url.clone(), config.bitcoin_rpc_user.clone(), @@ -152,7 +156,7 @@ mod tests { #[serial_test::serial] #[ignore = "This test is very host dependent and needs a human observer"] async fn start_header_chain_prover() { - let config = create_test_config_with_thread_name("test_config.toml", None).await; + let config = create_test_config_with_thread_name!("test_config.toml", None); let rpc = ExtendedRpc::new( config.bitcoin_rpc_url.clone(), config.bitcoin_rpc_user.clone(), diff --git a/core/src/header_chain_prover/prover.rs b/core/src/header_chain_prover/prover.rs index f96abac2..3bb0786d 100644 --- a/core/src/header_chain_prover/prover.rs +++ b/core/src/header_chain_prover/prover.rs @@ -137,8 +137,11 @@ impl HeaderChainProver { #[cfg(test)] mod tests { use crate::{ - extended_rpc::ExtendedRpc, header_chain_prover::HeaderChainProver, - mock::database::create_test_config_with_thread_name, + config::BridgeConfig, database::Database, initialize_database, utils::initialize_logger, + }; + use crate::{ + create_test_config_with_thread_name, extended_rpc::ExtendedRpc, + header_chain_prover::HeaderChainProver, }; use bitcoin::{ block::{Header, Version}, @@ -147,6 +150,7 @@ mod tests { }; use borsh::BorshDeserialize; use circuits::header_chain::{BlockHeader, BlockHeaderCircuitOutput}; + use std::{env, thread}; fn get_headers() -> Vec { let headers = include_bytes!("../../../scripts/headers.bin"); @@ -160,7 +164,7 @@ mod tests { #[tokio::test] #[serial_test::serial] async fn prove_block_headers_genesis() { - let config = create_test_config_with_thread_name("test_config.toml", None).await; + let config = create_test_config_with_thread_name!("test_config.toml", None); let rpc = ExtendedRpc::new( config.bitcoin_rpc_url.clone(), config.bitcoin_rpc_user.clone(), @@ -184,7 +188,7 @@ mod tests { #[tokio::test] #[serial_test::serial] async fn prove_block_headers_second() { - let config = create_test_config_with_thread_name("test_config.toml", None).await; + let config = create_test_config_with_thread_name!("test_config.toml", None); let rpc = ExtendedRpc::new( config.bitcoin_rpc_url.clone(), config.bitcoin_rpc_user.clone(), @@ -211,7 +215,7 @@ mod tests { #[tokio::test] #[serial_test::serial] async fn save_and_get_proof() { - let config = create_test_config_with_thread_name("test_config.toml", None).await; + let config = create_test_config_with_thread_name!("test_config.toml", None); let rpc = ExtendedRpc::new( config.bitcoin_rpc_url.clone(), config.bitcoin_rpc_user.clone(), diff --git a/core/src/mock/database.rs b/core/src/mock/database.rs index 7bd567cb..d43a6b3b 100644 --- a/core/src/mock/database.rs +++ b/core/src/mock/database.rs @@ -1,57 +1,57 @@ -//! # Database Mock Interface -//! -//! This module provides mock database interfaces, for testing. +// ! # Database Mock Interface +// ! +// ! This module provides mock database interfaces, for testing. -use super::common; -use crate::{config::BridgeConfig, utils::initialize_logger}; -use std::thread; +// use super::common; +// use crate::{config::BridgeConfig, utils::initialize_logger}; +// use std::thread; -/// Creates a temporary database for testing. -/// -/// # Parameters -/// -/// - db_name: New database's name. -/// - config_file: Test configuration file. Rest of the config will be read from -/// here and only `db_name` will be overwritten. -/// -/// Returns new `BridgeConfig`. -async fn create_test_config(db_name: &str, config_file: &str) -> BridgeConfig { - // Use maximum log level for tests. - initialize_logger(5).unwrap(); +// / Creates a temporary database for testing. +// / +// / # Parameters +// / +// / - db_name: New database's name. +// / - config_file: Test configuration file. Rest of the config will be read from +// / here and only `db_name` will be overwritten. +// / +// / Returns new `BridgeConfig`. +// async fn create_test_config(db_name: &str, config_file: &str) -> BridgeConfig { +// // Use maximum log level for tests. +// initialize_logger(5).unwrap(); - let mut config = common::get_test_config(config_file).unwrap(); - config.db_name = db_name.to_string(); - // Database::initialize_database(&config).await.unwrap(); +// let mut config = common::get_test_config(config_file).unwrap(); +// config.db_name = db_name.to_string(); +// // Database::initialize_database(&config).await.unwrap(); - config -} +// config +// } -/// Creates a temporary database for testing, using current thread's name as the -/// database name. -/// -/// # Parameters -/// -/// - config_file: Test configuration file. Rest of the config will be read from -/// here and only `db_name` will be overwritten. -/// - suffix: Optional suffix added to the thread handle. -/// -/// # Returns -/// -/// `BridgeConfig` -pub async fn create_test_config_with_thread_name( - config_file: &str, - suffix: Option<&str>, -) -> BridgeConfig { - let suffix: String = suffix.unwrap_or(&String::default()).to_string(); +// / Creates a temporary database for testing, using current thread's name as the +// / database name. +// / +// / # Parameters +// / +// / - config_file: Test configuration file. Rest of the config will be read from +// / here and only `db_name` will be overwritten. +// / - suffix: Optional suffix added to the thread handle. +// / +// / # Returns +// / +// / `BridgeConfig` +// pub async fn create_test_config_with_thread_name( +// config_file: &str, +// suffix: Option<&str>, +// ) -> BridgeConfig { +// let suffix: String = suffix.unwrap_or(&String::default()).to_string(); - let handle = thread::current() - .name() - .unwrap() - .split(':') - .last() - .unwrap() - .to_owned() - + &suffix; +// let handle = thread::current() +// .name() +// .unwrap() +// .split(':') +// .last() +// .unwrap() +// .to_owned() +// + &suffix; - create_test_config(&handle, config_file).await -} +// create_test_config(&handle, config_file).await +// } diff --git a/core/src/mock_macro.rs b/core/src/mock_macro.rs index ecb30c6f..3753df77 100644 --- a/core/src/mock_macro.rs +++ b/core/src/mock_macro.rs @@ -19,7 +19,9 @@ /// ## Unit Tests /// /// ```rust -/// use crate::{config::BridgeConfig, initialize_database, utils::initialize_logger}; +/// use crate::{ +/// config::BridgeConfig, database::Database, initialize_database, utils::initialize_logger, +/// }; /// use std::{env, thread}; /// ``` /// diff --git a/core/src/operator.rs b/core/src/operator.rs index 2b216dac..1831d702 100644 --- a/core/src/operator.rs +++ b/core/src/operator.rs @@ -739,14 +739,18 @@ impl OperatorRpcServer for Operator { #[cfg(test)] mod tests { use crate::{ - extended_rpc::ExtendedRpc, mock::database::create_test_config_with_thread_name, - operator::Operator, servers::create_operator_server, traits::rpc::OperatorRpcClient, UTXO, + config::BridgeConfig, database::Database, initialize_database, utils::initialize_logger, + }; + use crate::{ + create_test_config_with_thread_name, extended_rpc::ExtendedRpc, operator::Operator, + servers::create_operator_server, traits::rpc::OperatorRpcClient, UTXO, }; use bitcoin::{hashes::Hash, Amount, OutPoint, ScriptBuf, TxOut, Txid}; + use std::{env, thread}; #[tokio::test] async fn set_funding_utxo() { - let config = create_test_config_with_thread_name("test_config.toml", None).await; + let config = create_test_config_with_thread_name!("test_config.toml", None); let rpc = ExtendedRpc::new( config.bitcoin_rpc_url.clone(), config.bitcoin_rpc_user.clone(), @@ -779,7 +783,7 @@ mod tests { #[tokio::test] async fn set_funding_utxo_rpc() { - let config = create_test_config_with_thread_name("test_config.toml", None).await; + let config = create_test_config_with_thread_name!("test_config.toml", None); let rpc = ExtendedRpc::new( config.bitcoin_rpc_url.clone(), config.bitcoin_rpc_user.clone(), @@ -807,7 +811,7 @@ mod tests { #[tokio::test] async fn is_profitable() { - let mut config = create_test_config_with_thread_name("test_config.toml", None).await; + let mut config = create_test_config_with_thread_name!("test_config.toml", None); let rpc = ExtendedRpc::new( config.bitcoin_rpc_url.clone(), config.bitcoin_rpc_user.clone(), diff --git a/core/src/rpc/aggregator.rs b/core/src/rpc/aggregator.rs index c28b811f..1166a962 100644 --- a/core/src/rpc/aggregator.rs +++ b/core/src/rpc/aggregator.rs @@ -405,6 +405,7 @@ impl ClementineAggregator for Aggregator { mod tests { use crate::{ config::BridgeConfig, + create_test_config_with_thread_name, database::Database, errors::BridgeError, initialize_database, @@ -412,20 +413,21 @@ mod tests { create_aggregator_grpc_server, create_operator_grpc_server, create_verifier_grpc_server, create_watchtower_grpc_server, }, + utils::initialize_logger, }; use crate::{ create_actors, extended_rpc::ExtendedRpc, - mock::database::create_test_config_with_thread_name, rpc::clementine::{self, clementine_aggregator_client::ClementineAggregatorClient}, verifier::Verifier, watchtower::Watchtower, }; + use std::{env, thread}; #[tokio::test] #[serial_test::serial] async fn aggregator_double_setup_fail() { - let config = create_test_config_with_thread_name("test_config.toml", None).await; + let config = create_test_config_with_thread_name!("test_config.toml", None); let (_, _, aggregator, _) = create_actors!(config, 0); let mut aggregator_client = @@ -447,7 +449,7 @@ mod tests { #[tokio::test] #[serial_test::serial] async fn aggregator_setup_winternitz_public_keys() { - let mut config = create_test_config_with_thread_name("test_config.toml", None).await; + let mut config = create_test_config_with_thread_name!("test_config.toml", None); let (_verifiers, _operators, aggregator, _watchtowers) = create_actors!(config.clone(), 1); let mut aggregator_client = diff --git a/core/src/rpc/watchtower.rs b/core/src/rpc/watchtower.rs index 5f2766e5..ea250cb5 100644 --- a/core/src/rpc/watchtower.rs +++ b/core/src/rpc/watchtower.rs @@ -32,6 +32,7 @@ impl ClementineWatchtower for Watchtower { mod tests { use crate::{ config::BridgeConfig, + create_test_config_with_thread_name, database::Database, errors::BridgeError, extended_rpc::ExtendedRpc, @@ -40,19 +41,20 @@ mod tests { create_aggregator_grpc_server, create_operator_grpc_server, create_verifier_grpc_server, create_watchtower_grpc_server, }, + utils::initialize_logger, }; use crate::{ create_actors, - mock::database::create_test_config_with_thread_name, rpc::clementine::{clementine_watchtower_server::ClementineWatchtower, Empty}, watchtower::Watchtower, }; + use std::{env, thread}; use tonic::Request; #[tokio::test] #[serial_test::serial] async fn watchtower_get_params() { - let mut config = create_test_config_with_thread_name("test_config.toml", None).await; + let mut config = create_test_config_with_thread_name!("test_config.toml", None); let (verifiers, operators, _, _watchtowers) = create_actors!(config.clone(), 2); config.verifier_endpoints = Some( diff --git a/core/src/servers.rs b/core/src/servers.rs index 57e2421a..6bc0753b 100644 --- a/core/src/servers.rs +++ b/core/src/servers.rs @@ -3,7 +3,6 @@ //! Utilities for operator and verifier servers. use crate::aggregator; use crate::aggregator::Aggregator; -use crate::mock::database::create_test_config_with_thread_name; use crate::rpc::clementine::clementine_aggregator_server::ClementineAggregatorServer; use crate::rpc::clementine::clementine_operator_server::ClementineOperatorServer; use crate::rpc::clementine::clementine_verifier_server::ClementineVerifierServer; @@ -135,7 +134,8 @@ pub async fn create_verifiers_and_operators( Vec<(HttpClient, ServerHandle, std::net::SocketAddr)>, // Operator clients (HttpClient, ServerHandle, std::net::SocketAddr), // Aggregator client ) { - let config = create_test_config_with_thread_name(config_name, None).await; + // let config = create_test_config_with_thread_name!(config_name, None); + let config = BridgeConfig::default(); let start_port = config.port; let rpc = ExtendedRpc::new( config.bitcoin_rpc_url, @@ -152,11 +152,10 @@ pub async fn create_verifiers_and_operators( .map(|(i, sk)| { let port = start_port + i as u16; // println!("Port: {}", port); - let i = i.to_string(); + let _i = i.to_string(); let rpc = rpc.clone(); async move { - let config_with_new_db = - create_test_config_with_thread_name(config_name, Some(&i.to_string())).await; + let config_with_new_db = BridgeConfig::default(); let verifier = create_verifier_server( BridgeConfig { secret_key: *sk, @@ -216,7 +215,7 @@ pub async fn create_verifiers_and_operators( .await .unwrap(); - let config = create_test_config_with_thread_name(config_name, None).await; + let config = BridgeConfig::default(); println!("Port: {}", start_port); let port = start_port + all_verifiers_secret_keys.len() as u16 diff --git a/core/src/user.rs b/core/src/user.rs index a4a5c8c8..360bb5d2 100644 --- a/core/src/user.rs +++ b/core/src/user.rs @@ -114,17 +114,20 @@ impl User { #[cfg(test)] mod tests { - use crate::extended_rpc::ExtendedRpc; - use crate::mock::database::create_test_config_with_thread_name; use crate::user::User; use crate::EVMAddress; + use crate::{ + config::BridgeConfig, database::Database, initialize_database, utils::initialize_logger, + }; + use crate::{create_test_config_with_thread_name, extended_rpc::ExtendedRpc}; use bitcoincore_rpc::RpcApi; use secp256k1::{rand, SecretKey}; + use std::{env, thread}; #[tokio::test] #[serial_test::parallel] async fn deposit_tx() { - let config = create_test_config_with_thread_name("test_config.toml", None).await; + let config = create_test_config_with_thread_name!("test_config.toml", None); let rpc = ExtendedRpc::new( config.bitcoin_rpc_url.clone(), config.bitcoin_rpc_user.clone(), diff --git a/core/src/verifier.rs b/core/src/verifier.rs index 12309d14..a1f09957 100644 --- a/core/src/verifier.rs +++ b/core/src/verifier.rs @@ -637,19 +637,22 @@ impl VerifierRpcServer for Verifier { #[cfg(test)] mod tests { - use crate::actor::Actor; use crate::errors::BridgeError; use crate::extended_rpc::ExtendedRpc; - use crate::mock::database::create_test_config_with_thread_name; use crate::musig2::nonce_pair; use crate::user::User; use crate::verifier::Verifier; use crate::EVMAddress; + use crate::{actor::Actor, create_test_config_with_thread_name}; + use crate::{ + config::BridgeConfig, database::Database, initialize_database, utils::initialize_logger, + }; use secp256k1::rand; + use std::{env, thread}; #[tokio::test] async fn verifier_new_public_key_check() { - let mut config = create_test_config_with_thread_name("test_config.toml", None).await; + let mut config = create_test_config_with_thread_name!("test_config.toml", None); let rpc = ExtendedRpc::new( config.bitcoin_rpc_url.clone(), config.bitcoin_rpc_user.clone(), @@ -668,7 +671,7 @@ mod tests { #[tokio::test] #[serial_test::serial] async fn new_deposit_nonce_checks() { - let config = create_test_config_with_thread_name("test_config.toml", None).await; + let config = create_test_config_with_thread_name!("test_config.toml", None); let rpc = ExtendedRpc::new( config.bitcoin_rpc_url.clone(), config.bitcoin_rpc_user.clone(), diff --git a/core/src/watchtower.rs b/core/src/watchtower.rs index 966b4fc5..9b522616 100644 --- a/core/src/watchtower.rs +++ b/core/src/watchtower.rs @@ -72,7 +72,8 @@ impl Watchtower { #[cfg(test)] mod tests { - use crate::create_actors; + use crate::utils::initialize_logger; + use crate::watchtower::Watchtower; use crate::{ config::BridgeConfig, database::Database, @@ -84,12 +85,13 @@ mod tests { create_verifier_grpc_server, create_watchtower_grpc_server, }, }; - use crate::{mock::database::create_test_config_with_thread_name, watchtower::Watchtower}; + use crate::{create_actors, create_test_config_with_thread_name}; + use std::{env, thread}; #[tokio::test] #[serial_test::serial] async fn new_watchtower() { - let mut config = create_test_config_with_thread_name("test_config.toml", None).await; + let mut config = create_test_config_with_thread_name!("test_config.toml", None); let (verifiers, operators, _, _should_not_panic) = create_actors!(config.clone(), 1); config.verifier_endpoints = Some( @@ -111,7 +113,7 @@ mod tests { #[tokio::test] #[serial_test::serial] async fn get_winternitz_public_keys() { - let mut config = create_test_config_with_thread_name("test_config.toml", None).await; + let mut config = create_test_config_with_thread_name!("test_config.toml", None); let (verifiers, operators, _, _watchtowers) = create_actors!(config.clone(), 2); config.verifier_endpoints = Some( diff --git a/core/tests/common/deposit.rs b/core/tests/common/deposit.rs index f4e50358..e69de29b 100644 --- a/core/tests/common/deposit.rs +++ b/core/tests/common/deposit.rs @@ -1,415 +0,0 @@ -//! # Deposit Related Utilities - -use bitcoin::consensus::encode::deserialize_hex; -use bitcoin::Address; -use bitcoin::Amount; -use bitcoin::OutPoint; -use bitcoin::Transaction; -use bitcoincore_rpc::RpcApi; -use clementine_core::actor::Actor; -use clementine_core::config::BridgeConfig; -use clementine_core::errors::BridgeError; -use clementine_core::extended_rpc::ExtendedRpc; -use clementine_core::mock::database::create_test_config_with_thread_name; -use clementine_core::musig2::MuSigPartialSignature; -use clementine_core::servers::*; -use clementine_core::traits::rpc::AggregatorClient; -use clementine_core::traits::rpc::OperatorRpcClient; -use clementine_core::traits::rpc::VerifierRpcClient; -use clementine_core::user::User; -use clementine_core::EVMAddress; -use jsonrpsee::http_client::HttpClient; -use jsonrpsee::server::ServerHandle; -use std::net::SocketAddr; - -pub async fn run_multiple_deposits(test_config_name: &str) { - let config = create_test_config_with_thread_name(test_config_name, None).await; - let rpc = ExtendedRpc::new( - config.bitcoin_rpc_url.clone(), - config.bitcoin_rpc_user.clone(), - config.bitcoin_rpc_password.clone(), - ) - .await; - let secp = secp256k1::Secp256k1::new(); - let (verifiers, operators, aggregator) = - create_verifiers_and_operators("test_config.toml").await; - - // println!("Operators: {:#?}", operators); - // println!("Verifiers: {:#?}", verifiers); - - let secret_key = secp256k1::SecretKey::new(&mut secp256k1::rand::thread_rng()); - - let signer_address = Actor::new(secret_key, config.winternitz_secret_key, config.network) - .address - .as_unchecked() - .clone(); - let user = User::new(rpc.clone(), secret_key, config.clone()); - - let evm_address = EVMAddress([1u8; 20]); - let deposit_address = user.get_deposit_address(evm_address).unwrap(); - let mut deposit_outpoints = Vec::new(); - for _ in 0..config.operator_num_kickoff_utxos_per_tx + 1 { - let deposit_outpoint = rpc - .send_to_address(&deposit_address, config.bridge_amount_sats) - .await - .unwrap(); - - rpc.mine_blocks(18).await.unwrap(); - - let mut pub_nonces = Vec::new(); - - for (client, _, _) in verifiers.iter() { - let musig_pub_nonces = client - .verifier_new_deposit_rpc(deposit_outpoint, signer_address.clone(), evm_address) - .await - .unwrap(); - - pub_nonces.push(musig_pub_nonces); - } - - let agg_nonces = aggregator - .0 - .aggregate_pub_nonces_rpc(pub_nonces) - .await - .unwrap(); - let mut kickoff_utxos = Vec::new(); - let mut signatures = Vec::new(); - - for (client, _, _) in operators.iter() { - let (kickoff_utxo, signature) = client - .new_deposit_rpc(deposit_outpoint, signer_address.clone(), evm_address) - .await - .unwrap(); - - kickoff_utxos.push(kickoff_utxo); - signatures.push(signature); - } - - println!("Now the verifiers sequence starts"); - let mut slash_or_take_partial_sigs = Vec::new(); - - for (client, ..) in verifiers.iter() { - let (partial_sigs, _) = client - .operator_kickoffs_generated_rpc( - deposit_outpoint, - kickoff_utxos.clone(), - signatures.clone(), - agg_nonces.clone(), - ) - .await - .unwrap(); - - slash_or_take_partial_sigs.push(partial_sigs); - } - - let slash_or_take_sigs = aggregator - .0 - .aggregate_slash_or_take_sigs_rpc( - deposit_outpoint, - kickoff_utxos.clone(), - agg_nonces[config.num_operators + 1..2 * config.num_operators + 1].to_vec(), - slash_or_take_partial_sigs, - ) - .await - .unwrap(); - let mut operator_take_partial_sigs: Vec> = Vec::new(); - for (client, ..) in verifiers.iter() { - let partial_sigs = client - .burn_txs_signed_rpc(deposit_outpoint, vec![], slash_or_take_sigs.clone()) - .await - .unwrap(); - operator_take_partial_sigs.push(partial_sigs); - } - - let operator_take_sigs = aggregator - .0 - .aggregate_operator_take_sigs_rpc( - deposit_outpoint, - kickoff_utxos.clone(), - agg_nonces[1..config.num_operators + 1].to_vec(), - operator_take_partial_sigs, - ) - .await - .unwrap(); - - let mut move_tx_partial_sigs = Vec::new(); - for (client, _, _) in verifiers.iter() { - let move_tx_partial_sig = client - .operator_take_txs_signed_rpc(deposit_outpoint, operator_take_sigs.clone()) - .await - .unwrap(); - move_tx_partial_sigs.push(move_tx_partial_sig); - } - - let (move_tx, _) = aggregator - .0 - .aggregate_move_tx_sigs_rpc( - deposit_outpoint, - signer_address.clone(), - evm_address, - agg_nonces[0], - move_tx_partial_sigs, - ) - .await - .unwrap(); - let move_tx: Transaction = deserialize_hex(&move_tx).unwrap(); - - println!("Move tx weight: {:?}", move_tx.weight()); - let move_txid = rpc.client.send_raw_transaction(&move_tx).await.unwrap(); - println!("Move txid: {:?}", move_txid); - deposit_outpoints.push(deposit_outpoint); - } - let withdrawal_address = Address::p2tr( - &secp, - secret_key.x_only_public_key(&secp).0, - None, - config.network, - ); - let (user_utxo, user_txout, user_sig) = user - .generate_withdrawal_transaction_and_signature( - withdrawal_address.clone(), - Amount::from_sat( - config.bridge_amount_sats.to_sat() - - 2 * config.operator_withdrawal_fee_sats.unwrap().to_sat(), - ), - ) - .await - .unwrap(); - let withdrawal_provide_txid = operators[0] - .0 - .new_withdrawal_sig_rpc(0, user_sig, user_utxo, user_txout) - .await - .unwrap(); - println!("Withdrawal provide txid: {:?}", withdrawal_provide_txid); - let txs_to_be_sent_0 = operators[0] - .0 - .withdrawal_proved_on_citrea_rpc(0, deposit_outpoints[0]) - .await - .unwrap(); - assert!(txs_to_be_sent_0.len() == 3); - let (user_utxo, user_txout, user_sig) = user - .generate_withdrawal_transaction_and_signature( - withdrawal_address.clone(), - Amount::from_sat( - config.bridge_amount_sats.to_sat() - - 2 * config.operator_withdrawal_fee_sats.unwrap().to_sat(), - ), - ) - .await - .unwrap(); - let withdrawal_provide_txid = operators[1] - .0 - .new_withdrawal_sig_rpc( - config.operator_num_kickoff_utxos_per_tx as u32 - 1, - user_sig, - user_utxo, - user_txout, - ) - .await - .unwrap(); - println!("Withdrawal provide txid: {:?}", withdrawal_provide_txid); - let txs_to_be_sent_penultimate = operators[1] - .0 - .withdrawal_proved_on_citrea_rpc( - config.operator_num_kickoff_utxos_per_tx as u32 - 1, - deposit_outpoints[config.operator_num_kickoff_utxos_per_tx - 1], - ) - .await - .unwrap(); - assert!(txs_to_be_sent_penultimate.len() == 3); - let (user_utxo, user_txout, user_sig) = user - .generate_withdrawal_transaction_and_signature( - withdrawal_address.clone(), - Amount::from_sat( - config.bridge_amount_sats.to_sat() - - 2 * config.operator_withdrawal_fee_sats.unwrap().to_sat(), - ), - ) - .await - .unwrap(); - let withdrawal_provide_txid = operators[0] - .0 - .new_withdrawal_sig_rpc(2, user_sig, user_utxo, user_txout) - .await - .unwrap(); - println!("Withdrawal provide txid: {:?}", withdrawal_provide_txid); - let txs_to_be_sent_last = operators[2] - .0 - .withdrawal_proved_on_citrea_rpc( - config.operator_num_kickoff_utxos_per_tx as u32, - deposit_outpoints[config.operator_num_kickoff_utxos_per_tx], - ) - .await - .unwrap(); - assert!(txs_to_be_sent_last.len() == 4); -} - -pub async fn run_single_deposit( - test_config_name: &str, -) -> Result< - ( - Vec<(HttpClient, ServerHandle, SocketAddr)>, - Vec<(HttpClient, ServerHandle, SocketAddr)>, - BridgeConfig, - OutPoint, - ), - BridgeError, -> { - let config = create_test_config_with_thread_name(test_config_name, None).await; - let rpc = ExtendedRpc::new( - config.bitcoin_rpc_url.clone(), - config.bitcoin_rpc_user.clone(), - config.bitcoin_rpc_password.clone(), - ) - .await; - - let secret_key = secp256k1::SecretKey::new(&mut secp256k1::rand::thread_rng()); - let signer_address = Actor::new(secret_key, config.winternitz_secret_key, config.network) - .address - .as_unchecked() - .clone(); - - let user = User::new(rpc.clone(), secret_key, config.clone()); - - let evm_address = EVMAddress([1u8; 20]); - let deposit_address = user.get_deposit_address(evm_address).unwrap(); - - let (verifiers, operators, aggregator) = - create_verifiers_and_operators("test_config.toml").await; - - let deposit_outpoint = rpc - .send_to_address(&deposit_address, config.bridge_amount_sats) - .await - .unwrap(); - rpc.mine_blocks(18).await.unwrap(); - - // for every verifier, we call new_deposit - // aggregate nonces - let mut pub_nonces = Vec::new(); - for (client, _, _) in verifiers.iter() { - let musig_pub_nonces = client - .verifier_new_deposit_rpc(deposit_outpoint, signer_address.clone(), evm_address) - .await - .unwrap(); - - pub_nonces.push(musig_pub_nonces); - } - - let agg_nonces = aggregator - .0 - .aggregate_pub_nonces_rpc(pub_nonces) - .await - .unwrap(); - - // call operators' new_deposit - let mut kickoff_utxos = Vec::new(); - let mut signatures = Vec::new(); - for (client, _, _) in operators.iter() { - // Create deposit kickoff transaction - let (kickoff_utxo, signature) = client - .new_deposit_rpc(deposit_outpoint, signer_address.clone(), evm_address) - .await - .unwrap(); - - kickoff_utxos.push(kickoff_utxo); - signatures.push(signature); - } - - // Verifiers part starts here. - let mut slash_or_take_partial_sigs = Vec::new(); - for (client, ..) in verifiers.iter() { - let (partial_sigs, _) = client - .operator_kickoffs_generated_rpc( - deposit_outpoint, - kickoff_utxos.clone(), - signatures.clone(), - agg_nonces.clone(), - ) - .await - .unwrap(); - - slash_or_take_partial_sigs.push(partial_sigs); - } - - let slash_or_take_sigs = aggregator - .0 - .aggregate_slash_or_take_sigs_rpc( - deposit_outpoint, - kickoff_utxos.clone(), - agg_nonces[config.num_operators + 1..2 * config.num_operators + 1].to_vec(), - slash_or_take_partial_sigs, - ) - .await - .unwrap(); - - // call burn_txs_signed_rpc - let mut operator_take_partial_sigs: Vec> = Vec::new(); - for (client, ..) in verifiers.iter() { - let partial_sigs = client - .burn_txs_signed_rpc(deposit_outpoint, vec![], slash_or_take_sigs.clone()) - .await - .unwrap(); - - operator_take_partial_sigs.push(partial_sigs); - } - - let operator_take_sigs = aggregator - .0 - .aggregate_operator_take_sigs_rpc( - deposit_outpoint, - kickoff_utxos.clone(), - agg_nonces[1..config.num_operators + 1].to_vec(), - operator_take_partial_sigs, - ) - .await - .unwrap(); - - // Call operator_take_txs_signed_rpc - let mut move_tx_partial_sigs = Vec::new(); - for (client, _, _) in verifiers.iter() { - let move_tx_partial_sig = client - .operator_take_txs_signed_rpc(deposit_outpoint, operator_take_sigs.clone()) - .await - .unwrap(); - - move_tx_partial_sigs.push(move_tx_partial_sig); - } - - // Aggregate move_tx_partial_sigs - let (move_tx, _) = aggregator - .0 - .aggregate_move_tx_sigs_rpc( - deposit_outpoint, - signer_address, - evm_address, - agg_nonces[0], - move_tx_partial_sigs, - ) - .await - .unwrap(); - - let move_tx: Transaction = deserialize_hex(&move_tx).unwrap(); - println!("Move tx weight: {:?}", move_tx.weight()); - - let move_txid = rpc.client.send_raw_transaction(&move_tx).await.unwrap(); - println!("Move txid: {:?}", move_txid); - - Ok((verifiers, operators, config, deposit_outpoint)) -} - -#[cfg(test)] -mod tests { - use crate::common::{run_multiple_deposits, run_single_deposit}; - - #[ignore = "We are switching to gRPC"] - #[tokio::test] - async fn test_deposit() { - run_single_deposit("test_config.toml").await.unwrap(); - } - - #[ignore = "We are switching to gRPC"] - #[tokio::test] - async fn multiple_deposits_for_operator() { - run_multiple_deposits("test_config.toml").await; - } -} diff --git a/core/tests/common/mod.rs b/core/tests/common/mod.rs index 5e03b736..0f5e4649 100644 --- a/core/tests/common/mod.rs +++ b/core/tests/common/mod.rs @@ -1,8 +1,421 @@ //! # Common Utilities for Integration Tests -mod deposit; - -pub use deposit::*; - #[path = "../../src/mock_macro.rs"] mod mock_macro; + +use crate::{create_test_config_with_thread_name, initialize_database}; +use bitcoin::consensus::encode::deserialize_hex; +use bitcoin::Address; +use bitcoin::Amount; +use bitcoin::OutPoint; +use bitcoin::Transaction; +use bitcoincore_rpc::RpcApi; +use clementine_core::actor::Actor; +use clementine_core::config::BridgeConfig; +use clementine_core::database::Database; +use clementine_core::errors::BridgeError; +use clementine_core::extended_rpc::ExtendedRpc; +use clementine_core::musig2::MuSigPartialSignature; +use clementine_core::servers::*; +use clementine_core::traits::rpc::AggregatorClient; +use clementine_core::traits::rpc::OperatorRpcClient; +use clementine_core::traits::rpc::VerifierRpcClient; +use clementine_core::user::User; +use clementine_core::utils::initialize_logger; +use clementine_core::EVMAddress; +use jsonrpsee::http_client::HttpClient; +use jsonrpsee::server::ServerHandle; +use std::net::SocketAddr; +use std::{env, thread}; + +pub async fn run_multiple_deposits(test_config_name: &str) { + let config = create_test_config_with_thread_name!(test_config_name, None); + let rpc = ExtendedRpc::new( + config.bitcoin_rpc_url.clone(), + config.bitcoin_rpc_user.clone(), + config.bitcoin_rpc_password.clone(), + ) + .await; + let secp = secp256k1::Secp256k1::new(); + let (verifiers, operators, aggregator) = + create_verifiers_and_operators("test_config.toml").await; + + // println!("Operators: {:#?}", operators); + // println!("Verifiers: {:#?}", verifiers); + + let secret_key = secp256k1::SecretKey::new(&mut secp256k1::rand::thread_rng()); + + let signer_address = Actor::new(secret_key, config.winternitz_secret_key, config.network) + .address + .as_unchecked() + .clone(); + let user = User::new(rpc.clone(), secret_key, config.clone()); + + let evm_address = EVMAddress([1u8; 20]); + let deposit_address = user.get_deposit_address(evm_address).unwrap(); + let mut deposit_outpoints = Vec::new(); + for _ in 0..config.operator_num_kickoff_utxos_per_tx + 1 { + let deposit_outpoint = rpc + .send_to_address(&deposit_address, config.bridge_amount_sats) + .await + .unwrap(); + + rpc.mine_blocks(18).await.unwrap(); + + let mut pub_nonces = Vec::new(); + + for (client, _, _) in verifiers.iter() { + let musig_pub_nonces = client + .verifier_new_deposit_rpc(deposit_outpoint, signer_address.clone(), evm_address) + .await + .unwrap(); + + pub_nonces.push(musig_pub_nonces); + } + + let agg_nonces = aggregator + .0 + .aggregate_pub_nonces_rpc(pub_nonces) + .await + .unwrap(); + let mut kickoff_utxos = Vec::new(); + let mut signatures = Vec::new(); + + for (client, _, _) in operators.iter() { + let (kickoff_utxo, signature) = client + .new_deposit_rpc(deposit_outpoint, signer_address.clone(), evm_address) + .await + .unwrap(); + + kickoff_utxos.push(kickoff_utxo); + signatures.push(signature); + } + + println!("Now the verifiers sequence starts"); + let mut slash_or_take_partial_sigs = Vec::new(); + + for (client, ..) in verifiers.iter() { + let (partial_sigs, _) = client + .operator_kickoffs_generated_rpc( + deposit_outpoint, + kickoff_utxos.clone(), + signatures.clone(), + agg_nonces.clone(), + ) + .await + .unwrap(); + + slash_or_take_partial_sigs.push(partial_sigs); + } + + let slash_or_take_sigs = aggregator + .0 + .aggregate_slash_or_take_sigs_rpc( + deposit_outpoint, + kickoff_utxos.clone(), + agg_nonces[config.num_operators + 1..2 * config.num_operators + 1].to_vec(), + slash_or_take_partial_sigs, + ) + .await + .unwrap(); + let mut operator_take_partial_sigs: Vec> = Vec::new(); + for (client, ..) in verifiers.iter() { + let partial_sigs = client + .burn_txs_signed_rpc(deposit_outpoint, vec![], slash_or_take_sigs.clone()) + .await + .unwrap(); + operator_take_partial_sigs.push(partial_sigs); + } + + let operator_take_sigs = aggregator + .0 + .aggregate_operator_take_sigs_rpc( + deposit_outpoint, + kickoff_utxos.clone(), + agg_nonces[1..config.num_operators + 1].to_vec(), + operator_take_partial_sigs, + ) + .await + .unwrap(); + + let mut move_tx_partial_sigs = Vec::new(); + for (client, _, _) in verifiers.iter() { + let move_tx_partial_sig = client + .operator_take_txs_signed_rpc(deposit_outpoint, operator_take_sigs.clone()) + .await + .unwrap(); + move_tx_partial_sigs.push(move_tx_partial_sig); + } + + let (move_tx, _) = aggregator + .0 + .aggregate_move_tx_sigs_rpc( + deposit_outpoint, + signer_address.clone(), + evm_address, + agg_nonces[0], + move_tx_partial_sigs, + ) + .await + .unwrap(); + let move_tx: Transaction = deserialize_hex(&move_tx).unwrap(); + + println!("Move tx weight: {:?}", move_tx.weight()); + let move_txid = rpc.client.send_raw_transaction(&move_tx).await.unwrap(); + println!("Move txid: {:?}", move_txid); + deposit_outpoints.push(deposit_outpoint); + } + let withdrawal_address = Address::p2tr( + &secp, + secret_key.x_only_public_key(&secp).0, + None, + config.network, + ); + let (user_utxo, user_txout, user_sig) = user + .generate_withdrawal_transaction_and_signature( + withdrawal_address.clone(), + Amount::from_sat( + config.bridge_amount_sats.to_sat() + - 2 * config.operator_withdrawal_fee_sats.unwrap().to_sat(), + ), + ) + .await + .unwrap(); + let withdrawal_provide_txid = operators[0] + .0 + .new_withdrawal_sig_rpc(0, user_sig, user_utxo, user_txout) + .await + .unwrap(); + println!("Withdrawal provide txid: {:?}", withdrawal_provide_txid); + let txs_to_be_sent_0 = operators[0] + .0 + .withdrawal_proved_on_citrea_rpc(0, deposit_outpoints[0]) + .await + .unwrap(); + assert!(txs_to_be_sent_0.len() == 3); + let (user_utxo, user_txout, user_sig) = user + .generate_withdrawal_transaction_and_signature( + withdrawal_address.clone(), + Amount::from_sat( + config.bridge_amount_sats.to_sat() + - 2 * config.operator_withdrawal_fee_sats.unwrap().to_sat(), + ), + ) + .await + .unwrap(); + let withdrawal_provide_txid = operators[1] + .0 + .new_withdrawal_sig_rpc( + config.operator_num_kickoff_utxos_per_tx as u32 - 1, + user_sig, + user_utxo, + user_txout, + ) + .await + .unwrap(); + println!("Withdrawal provide txid: {:?}", withdrawal_provide_txid); + let txs_to_be_sent_penultimate = operators[1] + .0 + .withdrawal_proved_on_citrea_rpc( + config.operator_num_kickoff_utxos_per_tx as u32 - 1, + deposit_outpoints[config.operator_num_kickoff_utxos_per_tx - 1], + ) + .await + .unwrap(); + assert!(txs_to_be_sent_penultimate.len() == 3); + let (user_utxo, user_txout, user_sig) = user + .generate_withdrawal_transaction_and_signature( + withdrawal_address.clone(), + Amount::from_sat( + config.bridge_amount_sats.to_sat() + - 2 * config.operator_withdrawal_fee_sats.unwrap().to_sat(), + ), + ) + .await + .unwrap(); + let withdrawal_provide_txid = operators[0] + .0 + .new_withdrawal_sig_rpc(2, user_sig, user_utxo, user_txout) + .await + .unwrap(); + println!("Withdrawal provide txid: {:?}", withdrawal_provide_txid); + let txs_to_be_sent_last = operators[2] + .0 + .withdrawal_proved_on_citrea_rpc( + config.operator_num_kickoff_utxos_per_tx as u32, + deposit_outpoints[config.operator_num_kickoff_utxos_per_tx], + ) + .await + .unwrap(); + assert!(txs_to_be_sent_last.len() == 4); +} + +pub async fn run_single_deposit( + test_config_name: &str, +) -> Result< + ( + Vec<(HttpClient, ServerHandle, SocketAddr)>, + Vec<(HttpClient, ServerHandle, SocketAddr)>, + BridgeConfig, + OutPoint, + ), + BridgeError, +> { + let config = create_test_config_with_thread_name!(test_config_name, None); + let rpc = ExtendedRpc::new( + config.bitcoin_rpc_url.clone(), + config.bitcoin_rpc_user.clone(), + config.bitcoin_rpc_password.clone(), + ) + .await; + + let secret_key = secp256k1::SecretKey::new(&mut secp256k1::rand::thread_rng()); + let signer_address = Actor::new(secret_key, config.winternitz_secret_key, config.network) + .address + .as_unchecked() + .clone(); + + let user = User::new(rpc.clone(), secret_key, config.clone()); + + let evm_address = EVMAddress([1u8; 20]); + let deposit_address = user.get_deposit_address(evm_address).unwrap(); + + let (verifiers, operators, aggregator) = + create_verifiers_and_operators("test_config.toml").await; + + let deposit_outpoint = rpc + .send_to_address(&deposit_address, config.bridge_amount_sats) + .await + .unwrap(); + rpc.mine_blocks(18).await.unwrap(); + + // for every verifier, we call new_deposit + // aggregate nonces + let mut pub_nonces = Vec::new(); + for (client, _, _) in verifiers.iter() { + let musig_pub_nonces = client + .verifier_new_deposit_rpc(deposit_outpoint, signer_address.clone(), evm_address) + .await + .unwrap(); + + pub_nonces.push(musig_pub_nonces); + } + + let agg_nonces = aggregator + .0 + .aggregate_pub_nonces_rpc(pub_nonces) + .await + .unwrap(); + + // call operators' new_deposit + let mut kickoff_utxos = Vec::new(); + let mut signatures = Vec::new(); + for (client, _, _) in operators.iter() { + // Create deposit kickoff transaction + let (kickoff_utxo, signature) = client + .new_deposit_rpc(deposit_outpoint, signer_address.clone(), evm_address) + .await + .unwrap(); + + kickoff_utxos.push(kickoff_utxo); + signatures.push(signature); + } + + // Verifiers part starts here. + let mut slash_or_take_partial_sigs = Vec::new(); + for (client, ..) in verifiers.iter() { + let (partial_sigs, _) = client + .operator_kickoffs_generated_rpc( + deposit_outpoint, + kickoff_utxos.clone(), + signatures.clone(), + agg_nonces.clone(), + ) + .await + .unwrap(); + + slash_or_take_partial_sigs.push(partial_sigs); + } + + let slash_or_take_sigs = aggregator + .0 + .aggregate_slash_or_take_sigs_rpc( + deposit_outpoint, + kickoff_utxos.clone(), + agg_nonces[config.num_operators + 1..2 * config.num_operators + 1].to_vec(), + slash_or_take_partial_sigs, + ) + .await + .unwrap(); + + // call burn_txs_signed_rpc + let mut operator_take_partial_sigs: Vec> = Vec::new(); + for (client, ..) in verifiers.iter() { + let partial_sigs = client + .burn_txs_signed_rpc(deposit_outpoint, vec![], slash_or_take_sigs.clone()) + .await + .unwrap(); + + operator_take_partial_sigs.push(partial_sigs); + } + + let operator_take_sigs = aggregator + .0 + .aggregate_operator_take_sigs_rpc( + deposit_outpoint, + kickoff_utxos.clone(), + agg_nonces[1..config.num_operators + 1].to_vec(), + operator_take_partial_sigs, + ) + .await + .unwrap(); + + // Call operator_take_txs_signed_rpc + let mut move_tx_partial_sigs = Vec::new(); + for (client, _, _) in verifiers.iter() { + let move_tx_partial_sig = client + .operator_take_txs_signed_rpc(deposit_outpoint, operator_take_sigs.clone()) + .await + .unwrap(); + + move_tx_partial_sigs.push(move_tx_partial_sig); + } + + // Aggregate move_tx_partial_sigs + let (move_tx, _) = aggregator + .0 + .aggregate_move_tx_sigs_rpc( + deposit_outpoint, + signer_address, + evm_address, + agg_nonces[0], + move_tx_partial_sigs, + ) + .await + .unwrap(); + + let move_tx: Transaction = deserialize_hex(&move_tx).unwrap(); + println!("Move tx weight: {:?}", move_tx.weight()); + + let move_txid = rpc.client.send_raw_transaction(&move_tx).await.unwrap(); + println!("Move txid: {:?}", move_txid); + + Ok((verifiers, operators, config, deposit_outpoint)) +} + +#[cfg(test)] +mod tests { + use crate::common::{run_multiple_deposits, run_single_deposit}; + + #[ignore = "We are switching to gRPC"] + #[tokio::test] + async fn test_deposit() { + run_single_deposit("test_config.toml").await.unwrap(); + } + + #[ignore = "We are switching to gRPC"] + #[tokio::test] + async fn multiple_deposits_for_operator() { + run_multiple_deposits("test_config.toml").await; + } +} diff --git a/core/tests/taproot.rs b/core/tests/taproot.rs index 15e57bc0..938cefbf 100644 --- a/core/tests/taproot.rs +++ b/core/tests/taproot.rs @@ -7,13 +7,16 @@ use clementine_core::actor::Actor; use clementine_core::builder::transaction::TxHandler; use clementine_core::builder::{self}; use clementine_core::extended_rpc::ExtendedRpc; -use clementine_core::mock::database::create_test_config_with_thread_name; use clementine_core::utils::{handle_taproot_witness_new, SECP}; +use clementine_core::{config::BridgeConfig, database::Database, utils::initialize_logger}; +use std::{env, thread}; + +mod common; #[tokio::test] #[serial_test::serial] async fn create_address_and_transaction_then_sign_transaction() { - let config = create_test_config_with_thread_name("test_config.toml", None).await; + let config = create_test_config_with_thread_name!("test_config.toml", None); let rpc = ExtendedRpc::new( config.bitcoin_rpc_url, config.bitcoin_rpc_user,