diff --git a/Cargo.toml b/Cargo.toml index bcddea685..c7702acd8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [workspace] resolver = "2" -members = ["core", "mock"] # TODO: Add "circuits" back later +members = ["core"] # TODO: Add "circuits" back later [workspace.dependencies] bitcoin = "0.32.2" diff --git a/core/Cargo.toml b/core/Cargo.toml index 954256d11..f90a30a53 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -29,9 +29,6 @@ sqlx = { workspace = true, features = ["runtime-tokio", "postgres", "macros"] } bitcoin-mock-rpc = { workspace = true } musig2 = { workspace = true } -[dev-dependencies] -mock = { path = "../mock" } - [features] default = [] mock_rpc = [] diff --git a/core/src/database/common.rs b/core/src/database/common.rs index 61c40d6aa..fdc6a2ee2 100644 --- a/core/src/database/common.rs +++ b/core/src/database/common.rs @@ -588,7 +588,6 @@ impl Database { mod tests { use super::Database; use crate::{ - mock::database::create_test_config_with_thread_name, musig2::{nonce_pair, MuSigAggNonce, MuSigPubNonce, MuSigSecNonce}, ByteArray32, EVMAddress, UTXO, }; diff --git a/core/src/database/mod.rs b/core/src/database/mod.rs index 59546c9ee..2ef30c5c6 100644 --- a/core/src/database/mod.rs +++ b/core/src/database/mod.rs @@ -92,10 +92,7 @@ impl Database { #[cfg(test)] mod tests { - use crate::{ - config::BridgeConfig, database::Database, - mock::database::create_test_config_with_thread_name, - }; + use crate::{config::BridgeConfig, database::Database}; #[tokio::test] async fn valid_database_connection() { diff --git a/core/src/operator.rs b/core/src/operator.rs index 840e88b51..065f53e7d 100644 --- a/core/src/operator.rs +++ b/core/src/operator.rs @@ -702,7 +702,6 @@ where #[cfg(test)] mod tests { use crate::{ - create_extended_rpc, extended_rpc::ExtendedRpc, mock::database::create_test_config, operator::Operator, servers::create_operator_server, traits::rpc::OperatorRpcClient, UTXO, }; use bitcoin::{hashes::Hash, Amount, OutPoint, ScriptBuf, TxOut, Txid}; diff --git a/core/src/servers.rs b/core/src/servers.rs index 0d46572c3..144a98880 100644 --- a/core/src/servers.rs +++ b/core/src/servers.rs @@ -105,9 +105,3 @@ pub async fn create_aggregator_server( Ok((client, handle, addr)) } - -#[tracing::instrument(ret(level = tracing::Level::TRACE))] -fn is_test_env() -> bool { - // if thread name is not main then it is a test - thread::current().name().unwrap_or_default() != "main" -} diff --git a/core/src/user.rs b/core/src/user.rs index 092cb9330..980a3dd0f 100644 --- a/core/src/user.rs +++ b/core/src/user.rs @@ -119,7 +119,6 @@ mod tests { use crate::extended_rpc::ExtendedRpc; use crate::user::User; use crate::EVMAddress; - use crate::{create_extended_rpc, mock::database::create_test_config}; use secp256k1::{rand, SecretKey}; #[tokio::test] diff --git a/core/src/verifier.rs b/core/src/verifier.rs index aea007cef..e9842ad79 100644 --- a/core/src/verifier.rs +++ b/core/src/verifier.rs @@ -585,12 +585,10 @@ where mod tests { use crate::actor::Actor; use crate::errors::BridgeError; - use crate::extended_rpc::ExtendedRpc; use crate::musig2::nonce_pair; use crate::user::User; use crate::verifier::Verifier; use crate::EVMAddress; - use crate::{create_extended_rpc, mock::database::create_test_config}; use secp256k1::rand; #[tokio::test] diff --git a/core/tests/common/common.rs b/core/tests/common/common.rs new file mode 100644 index 000000000..717742c7b --- /dev/null +++ b/core/tests/common/common.rs @@ -0,0 +1,99 @@ +//! # Common Test Utilities +//! +//! This file includes common functions/variables for tests. + +use clementine_core::{config::BridgeConfig, errors::BridgeError}; +use std::{env, net::TcpListener}; + +pub const ENV_CONF_FILE: &str = "TEST_CONFIG"; + +/// Returns configuration, read from configuration file which is specified from +/// either an environment variable or the function argument. Environment +/// variable is priotirized over the function argument `configuration_file`. +pub fn get_test_config(configuration_file: &str) -> Result { + let env_config: Option = if let Ok(config_file_path) = env::var(ENV_CONF_FILE) { + Some(BridgeConfig::try_parse_file(config_file_path.into())?) + } else { + None + }; + + // Read specified configuration file from `tests/data` directory. + let mut config = match BridgeConfig::try_parse_file( + format!( + "{}/tests/data/{}", + env!("CARGO_MANIFEST_DIR"), + configuration_file + ) + .into(), + ) { + Ok(c) => c, + Err(e) => return Err(e), + }; + + // Overwrite user's environment to test's hard coded data if environment + // file is specified. + if let Some(env_config) = env_config { + config.db_host = env_config.db_host; + config.db_port = env_config.db_port; + config.db_user = env_config.db_user; + config.db_password = env_config.db_password; + config.db_name = env_config.db_name; + }; + + #[cfg(test)] + { + config.port = 0; + } + + Ok(config) +} + +/// Finds consecutive idle ports starting from the given port, up to count num. +pub fn find_consecutive_idle_ports(port: u16, num: usize) -> Result { + let mut idle_ports = Vec::new(); + let mut current_port = port; + + while current_port < 65535 { + match TcpListener::bind(("0.0.0.0", current_port)) { + Ok(_) => { + idle_ports.push(current_port); + current_port += 1; + if idle_ports.len() == num + 1 { + break; + } + tracing::debug!( + "Ports {:?}-{:?} are available.", + current_port, + current_port + num as u16 + ); + } + Err(_e) => { + idle_ports.clear(); + if current_port < port + num as u16 { + tracing::debug!( + "Ports {:?}-{:?} are not available. Searching for new ports...", + current_port, + current_port + num as u16 + ); + } + current_port += 1; + } + } + } + + if idle_ports.len() == num + 1 { + Ok(idle_ports[0]) + } else { + Err(BridgeError::PortError( + "No consecutive idle ports found".to_string(), + )) + } +} + +mod tests { + #[tokio::test] + async fn ports() { + let res = super::find_consecutive_idle_ports(0, 5).unwrap(); + println!("{:?}", res); + } +} diff --git a/mock/src/database.rs b/core/tests/common/database.rs similarity index 89% rename from mock/src/database.rs rename to core/tests/common/database.rs index 068611bb0..417d8f2b1 100644 --- a/mock/src/database.rs +++ b/core/tests/common/database.rs @@ -2,6 +2,10 @@ //! //! This module provides mock database interfaces, for testing. +use clementine_core::{ + config::BridgeConfig, database::Database, errors::BridgeError, utils::initialize_logger, +}; + use super::common; use std::thread; @@ -113,10 +117,17 @@ async fn drop_database(config: &BridgeConfig) -> Result<(), BridgeError> { Ok(()) } +#[cfg(test)] mod tests { + use crate::common::{ + common::get_test_config, + database::{self, create_database, drop_database}, + }; + use clementine_core::database::Database; + #[tokio::test] async fn create_drop_database() { - let mut config = common::get_test_config("test_config.toml").unwrap(); + let mut config = get_test_config("test_config.toml").unwrap(); config.db_name = "create_drop_database".to_string(); // Drop database (clear previous test run artifacts) and check that @@ -135,8 +146,8 @@ mod tests { } #[tokio::test] - async fn test_initialize_database() { - let mut config = common::get_test_config("test_config.toml").unwrap(); + async fn initialize_database() { + let mut config = get_test_config("test_config.toml").unwrap(); config.db_name = "initialize_database".to_string(); // Drop database (clear previous test run artifacts) and check that @@ -145,9 +156,7 @@ mod tests { assert!(Database::new(&config).await.is_err()); // It should be possible to initialize and connect to the new database. - crate::mock::database::initialize_database(&config) - .await - .unwrap(); + database::initialize_database(&config).await.unwrap(); Database::new(&config).await.unwrap(); // Dropping database again should result connection to not be diff --git a/core/tests/common/deposit.rs b/core/tests/common/deposit.rs index 5d750cb24..e944fc76a 100644 --- a/core/tests/common/deposit.rs +++ b/core/tests/common/deposit.rs @@ -1,6 +1,7 @@ //! # Deposit Related Utilities -use crate::common::mock::database::create_test_config_with_thread_name; +use crate::common::create_verifiers_and_operators; +use crate::common::database::create_test_config_with_thread_name; use crate::create_extended_rpc; use bitcoin::consensus::encode::deserialize_hex; use bitcoin::Address; @@ -11,7 +12,9 @@ use clementine_core::actor::Actor; use clementine_core::config::BridgeConfig; use clementine_core::errors::BridgeError; use clementine_core::musig2::MuSigPartialSignature; -use clementine_core::servers::create_verifiers_and_operators; +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; diff --git a/mock/src/env.rs b/core/tests/common/env.rs similarity index 100% rename from mock/src/env.rs rename to core/tests/common/env.rs diff --git a/core/tests/common/mod.rs b/core/tests/common/mod.rs index 061cc0855..03a98fb1c 100644 --- a/core/tests/common/mod.rs +++ b/core/tests/common/mod.rs @@ -1,5 +1,15 @@ //! # Common Utilities for Integration Tests +mod common; +mod database; mod deposit; +mod env; +mod rpc; +mod server; +pub use common::*; +pub use database::*; pub use deposit::*; +pub use env::*; +pub use rpc::*; +pub use server::*; diff --git a/mock/src/rpc.rs b/core/tests/common/rpc.rs similarity index 90% rename from mock/src/rpc.rs rename to core/tests/common/rpc.rs index 1e9cfcf53..8f4b28165 100644 --- a/mock/src/rpc.rs +++ b/core/tests/common/rpc.rs @@ -25,7 +25,7 @@ macro_rules! create_extended_rpc { $config.bitcoin_rpc_url = handle.to_string(); - ExtendedRpc::::new( + clementine_core::extended_rpc::ExtendedRpc::::new( $config.bitcoin_rpc_url.clone(), $config.bitcoin_rpc_user.clone(), $config.bitcoin_rpc_password.clone(), @@ -49,7 +49,7 @@ macro_rules! create_extended_rpc { // Mutation for consistency with above defined macro $config.bitcoin_rpc_url = $config.bitcoin_rpc_url.clone(); - ExtendedRpc::::new( + clementine_core::extended_rpc::ExtendedRpc::::new( $config.bitcoin_rpc_url.clone(), $config.bitcoin_rpc_user.clone(), $config.bitcoin_rpc_password.clone(), diff --git a/mock/src/common.rs b/core/tests/common/server.rs similarity index 55% rename from mock/src/common.rs rename to core/tests/common/server.rs index 079629647..8b83e109b 100644 --- a/mock/src/common.rs +++ b/core/tests/common/server.rs @@ -1,13 +1,12 @@ -//! # Common Test Utilities -//! -//! This file includes common functions/variables for tests. - -use super::database::create_test_config_with_thread_name; -use clementine_core::servers::create_verifier_server; +use std::thread; + +use crate::{common::database::create_test_config_with_thread_name, create_extended_rpc}; +use clementine_core::{ + config::BridgeConfig, + errors::BridgeError, + servers::{create_aggregator_server, create_operator_server, create_verifier_server}, +}; use jsonrpsee::{http_client::HttpClient, server::ServerHandle}; -use std::{env, net::TcpListener, path}; - -pub const ENV_CONF_FILE: &str = "TEST_CONFIG"; /// Starts operators and verifiers servers. This function's intended use is for /// tests. @@ -30,7 +29,7 @@ pub async fn create_verifiers_and_operators( ) { let mut config = create_test_config_with_thread_name(config_name, None).await; let start_port = config.port; - let rpc = crate::create_extended_rpc!(config); + let rpc = create_extended_rpc!(config); let all_verifiers_secret_keys = config.all_verifiers_secret_keys.clone().unwrap_or_else(|| { panic!("All secret keys of the verifiers are required for testing"); }); @@ -119,93 +118,7 @@ pub async fn create_verifiers_and_operators( (verifier_endpoints, operator_endpoints, aggregator) } -/// Returns configuration, read from configuration file which is specified from -/// either an environment variable or the function argument. Environment -/// variable is priotirized over the function argument `configuration_file`. -pub fn get_test_config(configuration_file: &str) -> Result { - let env_config: Option = if let Ok(config_file_path) = env::var(ENV_CONF_FILE) { - Some(BridgeConfig::try_parse_file(config_file_path.into())?) - } else { - None - }; - - // Read specified configuration file from `tests/data` directory. - let mut config = match BridgeConfig::try_parse_file( - format!( - "{}/tests/data/{}", - env!("CARGO_MANIFEST_DIR"), - configuration_file - ) - .into(), - ) { - Ok(c) => c, - Err(e) => return Err(e), - }; - - // Overwrite user's environment to test's hard coded data if environment - // file is specified. - if let Some(env_config) = env_config { - config.db_host = env_config.db_host; - config.db_port = env_config.db_port; - config.db_user = env_config.db_user; - config.db_password = env_config.db_password; - config.db_name = env_config.db_name; - }; - - #[cfg(test)] - { - config.port = 0; - } - - Ok(config) -} - -/// Finds consecutive idle ports starting from the given port, up to count num. -pub fn find_consecutive_idle_ports(port: u16, num: usize) -> Result { - let mut idle_ports = Vec::new(); - let mut current_port = port; - - while current_port < 65535 { - match TcpListener::bind(("0.0.0.0", current_port)) { - Ok(_) => { - idle_ports.push(current_port); - current_port += 1; - if idle_ports.len() == num + 1 { - break; - } - tracing::debug!( - "Ports {:?}-{:?} are available.", - current_port, - current_port + num as u16 - ); - } - Err(_e) => { - idle_ports.clear(); - if current_port < port + num as u16 { - tracing::debug!( - "Ports {:?}-{:?} are not available. Searching for new ports...", - current_port, - current_port + num as u16 - ); - } - current_port += 1; - } - } - } - - if idle_ports.len() == num + 1 { - Ok(idle_ports[0]) - } else { - Err(BridgeError::PortError( - "No consecutive idle ports found".to_string(), - )) - } -} - -mod tests { - #[tokio::test] - async fn ports() { - let res = super::find_consecutive_idle_ports(0, 5).unwrap(); - println!("{:?}", res); - } +fn is_test_env() -> bool { + // if thread name is not main then it is a test + thread::current().name().unwrap_or_default() != "main" } diff --git a/core/tests/deposit.rs b/core/tests/deposit.rs index c43a796ca..a12a5e06b 100644 --- a/core/tests/deposit.rs +++ b/core/tests/deposit.rs @@ -1,18 +1,15 @@ //! # Deposit Tests +mod common; + use bitcoin::consensus::encode::deserialize_hex; use bitcoin::Transaction; -use clementine_core::actor::Actor; -use clementine_core::create_extended_rpc; -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::traits::rpc::{AggregatorClient, OperatorRpcClient}; use clementine_core::user::User; use clementine_core::EVMAddress; +use clementine_core::{actor::Actor, traits::rpc::VerifierRpcClient}; +use common::{create_test_config_with_thread_name, create_verifiers_and_operators}; #[tokio::test] async fn deposit_with_retry_checks() { diff --git a/core/tests/flow.rs b/core/tests/flow.rs index 4b2c446fa..461f4bf4a 100644 --- a/core/tests/flow.rs +++ b/core/tests/flow.rs @@ -2,16 +2,15 @@ //! //! This tests checks if typical flows works or not. +mod common; + use bitcoin::{Address, Amount}; use clementine_core::errors::BridgeError; -use clementine_core::extended_rpc::ExtendedRpc; use clementine_core::utils::SECP; -use clementine_core::{create_extended_rpc, traits::rpc::OperatorRpcClient, user::User}; +use clementine_core::{traits::rpc::OperatorRpcClient, user::User}; use common::run_single_deposit; use secp256k1::SecretKey; -mod common; - #[tokio::test] async fn honest_operator_takes_refund() { let (_verifiers, operators, mut config, deposit_outpoint) = diff --git a/core/tests/musig2.rs b/core/tests/musig2.rs index d93655238..de451d693 100644 --- a/core/tests/musig2.rs +++ b/core/tests/musig2.rs @@ -1,8 +1,8 @@ +mod common; + use bitcoin::opcodes::all::OP_CHECKSIG; use bitcoin::{hashes::Hash, script, Amount, ScriptBuf}; use clementine_core::builder::transaction::TxHandler; -use clementine_core::create_extended_rpc; -use clementine_core::mock::database::create_test_config_with_thread_name; use clementine_core::musig2::{ aggregate_nonces, aggregate_partial_signatures, MuSigPartialSignature, MuSigPubNonce, }; @@ -12,10 +12,10 @@ use clementine_core::{ actor::Actor, builder::{self}, config::BridgeConfig, - extended_rpc::ExtendedRpc, musig2::{create_key_agg_ctx, nonce_pair, partial_sign, MuSigNoncePair}, utils, ByteArray66, }; +use common::create_test_config_with_thread_name; use secp256k1::{Keypair, Message, PublicKey}; fn get_verifiers_keys( diff --git a/core/tests/taproot.rs b/core/tests/taproot.rs index f7e86a394..5d2976eaf 100644 --- a/core/tests/taproot.rs +++ b/core/tests/taproot.rs @@ -1,3 +1,5 @@ +mod common; + use bitcoin::hashes::{Hash, HashEngine}; use bitcoin::opcodes::all::OP_CHECKSIG; use bitcoin::script::Builder; @@ -5,10 +7,8 @@ use bitcoin::{Address, Amount, TapTweakHash, TxOut, XOnlyPublicKey}; use clementine_core::actor::Actor; use clementine_core::builder::transaction::TxHandler; use clementine_core::builder::{self}; -use clementine_core::create_extended_rpc; -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 common::create_test_config_with_thread_name; #[tokio::test] async fn create_address_and_transaction_then_sign_transaction() { diff --git a/mock/Cargo.toml b/mock/Cargo.toml deleted file mode 100644 index eabf4c9e3..000000000 --- a/mock/Cargo.toml +++ /dev/null @@ -1,5 +0,0 @@ -[package] -name = "mock" -description = "Mock interfaces for testing" -version = "0.0.0" -edition = "2021" diff --git a/mock/src/lib.rs b/mock/src/lib.rs deleted file mode 100644 index 3d1f6bf8b..000000000 --- a/mock/src/lib.rs +++ /dev/null @@ -1,9 +0,0 @@ -//! # Mock Interfaces -//! -//! This module includes mock interfaces for tests. There are also some common -//! elements for unit and integration tests. - -pub mod common; -pub mod database; -pub mod env; -pub mod rpc;