Skip to content

Commit

Permalink
mock: Convert create_test_config macros to functions and initialize l…
Browse files Browse the repository at this point in the history
…ogger there.
  • Loading branch information
ceyhunsen committed Sep 6, 2024
1 parent a1b38c7 commit 2cfff4a
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 96 deletions.
41 changes: 21 additions & 20 deletions core/src/database/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ pub struct Database {
impl Database {
/// Returns a `Database` after establishing a connection to database.
/// Returns error if database is not available.
///
/// TODO: Pass the reference &BridgeConfig instead of copying BridgeConfig.
pub async fn new(config: BridgeConfig) -> Result<Self, BridgeError> {
let url = "postgresql://".to_owned()
+ config.db_host.as_str()
Expand Down Expand Up @@ -663,8 +665,7 @@ mod tests {
use super::Database;
use crate::{
config::BridgeConfig,
create_test_config, create_test_config_with_thread_name,
mock::common,
mock::{common, database::create_test_config_with_thread_name},
musig2::{nonce_pair, MuSigAggNonce, MuSigPubNonce, MuSigSecNonce},
EVMAddress, UTXO,
};
Expand All @@ -678,7 +679,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");
let config = create_test_config_with_thread_name("test_config.toml", None).await;
let database = Database::new(config).await.unwrap();

let deposit_outpoint = OutPoint::null();
Expand Down Expand Up @@ -729,7 +730,7 @@ mod tests {

#[tokio::test]
async fn test_valid_connection() {
let config = create_test_config_with_thread_name!("test_config.toml");
let config = create_test_config_with_thread_name("test_config.toml", None).await;

Database::new(config).await.unwrap();
}
Expand All @@ -754,7 +755,7 @@ mod tests {

#[tokio::test]
async fn test_save_and_get_deposit_info() {
let config = create_test_config_with_thread_name!("test_config.toml");
let config = create_test_config_with_thread_name("test_config.toml", None).await;
let database = Database::new(config.clone()).await.unwrap();

let secp = Secp256k1::new();
Expand Down Expand Up @@ -786,7 +787,7 @@ mod tests {

#[tokio::test]
async fn test_nonces_1() {
let config = create_test_config_with_thread_name!("test_config.toml");
let config = create_test_config_with_thread_name("test_config.toml", None).await;
let db = Database::new(config).await.unwrap();
let secp = Secp256k1::new();

Expand Down Expand Up @@ -829,7 +830,7 @@ mod tests {

#[tokio::test]
async fn test_nonces_2() {
let config = create_test_config_with_thread_name!("test_config.toml");
let config = create_test_config_with_thread_name("test_config.toml", None).await;
let db = Database::new(config).await.unwrap();
let secp = Secp256k1::new();

Expand Down Expand Up @@ -871,7 +872,7 @@ mod tests {

#[tokio::test]
async fn test_nonces_3() {
let config = create_test_config_with_thread_name!("test_config.toml");
let config = create_test_config_with_thread_name("test_config.toml", None).await;
let db = Database::new(config).await.unwrap();
let secp = Secp256k1::new();

Expand Down Expand Up @@ -917,7 +918,7 @@ mod tests {

#[tokio::test]
async fn test_get_pub_nonces_1() {
let config = create_test_config_with_thread_name!("test_config.toml");
let config = create_test_config_with_thread_name("test_config.toml", None).await;
let db = Database::new(config).await.unwrap();
let secp = Secp256k1::new();

Expand Down Expand Up @@ -950,7 +951,7 @@ mod tests {

#[tokio::test]
async fn test_get_pub_nonces_2() {
let config = create_test_config_with_thread_name!("test_config.toml");
let config = create_test_config_with_thread_name("test_config.toml", None).await;
let db = Database::new(config).await.unwrap();
let outpoint = OutPoint {
txid: Txid::from_byte_array([1u8; 32]),
Expand All @@ -962,7 +963,7 @@ mod tests {

#[tokio::test]
async fn test_operators_kickoff_utxo_1() {
let config = create_test_config_with_thread_name!("test_config.toml");
let config = create_test_config_with_thread_name("test_config.toml", None).await;
let db = Database::new(config).await.unwrap();

let outpoint = OutPoint {
Expand All @@ -987,7 +988,7 @@ mod tests {

#[tokio::test]
async fn test_operators_kickoff_utxo_2() {
let config = create_test_config_with_thread_name!("test_config.toml");
let config = create_test_config_with_thread_name("test_config.toml", None).await;
let db = Database::new(config).await.unwrap();

let outpoint = OutPoint {
Expand All @@ -1000,7 +1001,7 @@ mod tests {

#[tokio::test]
async fn test_verifiers_kickoff_utxos_1() {
let config = create_test_config_with_thread_name!("test_config.toml");
let config = create_test_config_with_thread_name("test_config.toml", None).await;
let db = Database::new(config).await.unwrap();

let outpoint = OutPoint {
Expand Down Expand Up @@ -1037,7 +1038,7 @@ mod tests {

#[tokio::test]
async fn test_verifiers_kickoff_utxos_2() {
let config = create_test_config_with_thread_name!("test_config.toml");
let config = create_test_config_with_thread_name("test_config.toml", None).await;
let db = Database::new(config).await.unwrap();

let outpoint = OutPoint {
Expand All @@ -1050,7 +1051,7 @@ mod tests {

#[tokio::test]
async fn test_operators_funding_utxo_1() {
let config = create_test_config_with_thread_name!("test_config.toml");
let config = create_test_config_with_thread_name("test_config.toml", None).await;
let db = Database::new(config).await.unwrap();

let utxo = UTXO {
Expand All @@ -1072,7 +1073,7 @@ mod tests {

#[tokio::test]
async fn test_operators_funding_utxo_2() {
let config = create_test_config_with_thread_name!("test_config.toml");
let config = create_test_config_with_thread_name("test_config.toml", None).await;
let db = Database::new(config).await.unwrap();

let db_utxo = db.get_funding_utxo().await.unwrap();
Expand All @@ -1082,7 +1083,7 @@ mod tests {

#[tokio::test]
async fn test_deposit_kickoff_generator_tx_1() {
let config = create_test_config_with_thread_name!("test_config.toml");
let config = create_test_config_with_thread_name("test_config.toml", None).await;
let db = Database::new(config).await.unwrap();

let raw_hex = "01000000000101308d840c736eefd114a8fad04cb0d8338b4a3034a2b517250e5498701b25eb360100000000fdffffff02401f00000000000022512024985a1ab5724a5164ae5e0026b3e7e22031e83948eedf99d438b866857946b81f7e000000000000225120f7298da2a2be5b6e02a076ff7d35a1fe6b54a2bc7938c1c86bede23cadb7d9650140ad2fdb01ec5e2772f682867c8c6f30697c63f622e338f7390d3abc6c905b9fd7e96496fdc34cb9e872387758a6a334ec1307b3505b73121e0264fe2ba546d78ad11b0d00".to_string();
Expand Down Expand Up @@ -1123,7 +1124,7 @@ mod tests {

#[tokio::test]
async fn test_deposit_kickoff_generator_tx_2() {
let config = create_test_config_with_thread_name!("test_config.toml");
let config = create_test_config_with_thread_name("test_config.toml", None).await;
let db = Database::new(config).await.unwrap();

let txid = Txid::from_byte_array([1u8; 32]);
Expand All @@ -1133,7 +1134,7 @@ mod tests {

// #[tokio::test]
// async fn test_kickoff_root_1() {
// let config = create_test_config_with_thread_name!("test_config.toml");
// let config = create_test_config_with_thread_name("test_config.toml", None).await;
// let db = Database::new(config).await.unwrap();

// let outpoint = OutPoint {
Expand All @@ -1150,7 +1151,7 @@ mod tests {

// #[tokio::test]
// async fn test_kickoff_root_2() {
// let config = create_test_config_with_thread_name!("test_config.toml");
// let config = create_test_config_with_thread_name("test_config.toml", None).await;
// let db = Database::new(config).await.unwrap();

// let outpoint = OutPoint {
Expand Down
71 changes: 38 additions & 33 deletions core/src/mock/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,59 @@
//!
//! This module provides mock database interfaces, for testing.
use super::common;
use crate::{config::BridgeConfig, database::common::Database, utils::initialize_logger};
use std::thread;

/// Creates a temporary database for testing.
///
/// Parameters:
/// # 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`.
#[macro_export]
macro_rules! create_test_config {
($db_name:expr, $config_file:expr) => {{
let config = common::get_test_config($config_file).unwrap();
let config = Database::create_database(config, &$db_name).await.unwrap();

let database = Database::new(config.clone()).await.unwrap();
database.init_from_schema().await.unwrap();
database.close().await;

config
}};
pub async fn create_test_config(db_name: &str, config_file: &str) -> BridgeConfig {
// Use `RUST_LOG` env var's value.
initialize_logger(0).unwrap();

let config = common::get_test_config(config_file).unwrap();
let config = Database::create_database(config, db_name).await.unwrap();

let database = Database::new(config.clone()).await.unwrap();
database.init_from_schema().await.unwrap();
database.close().await;

config
}

/// Creates a temporary database for testing, using current thread's name as the
/// database name.
///
/// Parameters:
/// # 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 new `BridgeConfig`.
#[macro_export]
macro_rules! create_test_config_with_thread_name {
($config_file:expr, $suffix:expr) => {{
let suffix: String = $suffix.unwrap_or(&String::default()).to_string();
let handle = thread::current()
.name()
.unwrap()
.split(':')
.last()
.unwrap()
.to_owned()
+ &suffix;

create_test_config!(handle, $config_file)
}};
($config_file:expr) => {{
create_test_config_with_thread_name!($config_file, Option::<&str>::None)
}};
/// # 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;

create_test_config(&handle, config_file).await
}
14 changes: 6 additions & 8 deletions core/src/servers.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
//! # Servers
//!
//! Utilities for operator and verifier servers.
use crate::mock::common;
use crate::traits::rpc::AggregatorServer;
use crate::traits::rpc::OperatorRpcClient;
use crate::mock::database::create_test_config_with_thread_name;
use crate::traits::rpc::{AggregatorServer, OperatorRpcClient};
use crate::{aggregator, create_extended_rpc, UTXO};
use crate::{
config::BridgeConfig,
create_test_config, create_test_config_with_thread_name,
database::common::Database,
errors,
extended_rpc::ExtendedRpc,
operator,
Expand Down Expand Up @@ -135,7 +133,7 @@ pub async fn create_verifiers_and_operators(
Vec<(HttpClient, ServerHandle, std::net::SocketAddr)>, // Operator clients
(HttpClient, ServerHandle, std::net::SocketAddr), // Aggregator client
) {
let mut config = create_test_config_with_thread_name!(config_name);
let mut config = create_test_config_with_thread_name(config_name, None).await;
let start_port = config.port;
let rpc = create_extended_rpc!(config);
let all_verifiers_secret_keys = config.all_verifiers_secret_keys.clone().unwrap_or_else(|| {
Expand All @@ -151,7 +149,7 @@ pub async fn create_verifiers_and_operators(
let rpc = rpc.clone();
async move {
let config_with_new_db =
create_test_config_with_thread_name!(config_name, Some(&i.to_string()));
create_test_config_with_thread_name(config_name, Some(&i.to_string())).await;
let verifier = create_verifier_server(
BridgeConfig {
secret_key: *sk,
Expand Down Expand Up @@ -235,7 +233,7 @@ pub async fn create_verifiers_and_operators(
.await
.unwrap();
}
let config = create_test_config_with_thread_name!(config_name);
let config = create_test_config_with_thread_name(config_name, None).await;
println!("Port: {}", start_port);
let port = start_port
+ all_verifiers_secret_keys.len() as u16
Expand Down
6 changes: 3 additions & 3 deletions core/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ pub fn get_claim_reveal_indices(depth: usize, count: u32) -> Vec<(usize, usize)>
/// # Parameters
///
/// - `level`: Level ranges from 0 to 5. 0 defaults to no logs but can be
/// overwritten with `RUST_LOG` env var. While other numbers sets log level from
/// lowest level (1) to highest level (5). Is is advised to use 0 on tests and
/// other values for binaries (get value from user).
/// overwritten with `RUST_LOG` env var. While other numbers sets log level from
/// lowest level (1) to highest level (5). Is is advised to use 0 on tests and
/// other values for binaries (get value from user).
///
/// # Returns
///
Expand Down
20 changes: 6 additions & 14 deletions core/tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,20 @@ use bitcoin::OutPoint;
use bitcoin::Transaction;
use clementine_core::actor::Actor;
use clementine_core::config::BridgeConfig;
use clementine_core::database::common::Database;
use clementine_core::create_extended_rpc;
use clementine_core::errors::BridgeError;
use clementine_core::extended_rpc::ExtendedRpc;
use clementine_core::mock::common;
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 clementine_core::{
create_extended_rpc, create_test_config, create_test_config_with_thread_name,
};
use jsonrpsee::http_client::HttpClient;
use jsonrpsee::server::ServerHandle;
use std::net::SocketAddr;
use std::thread;

pub async fn run_single_deposit(
test_config_name: &str,
Expand All @@ -35,7 +31,7 @@ pub async fn run_single_deposit(
),
BridgeError,
> {
let mut config = create_test_config_with_thread_name!(test_config_name);
let mut config = create_test_config_with_thread_name(test_config_name, None).await;
let rpc = create_extended_rpc!(config);

let (verifiers, operators, aggregator) =
Expand Down Expand Up @@ -187,24 +183,20 @@ mod tests {
use bitcoin::consensus::encode::deserialize_hex;
use bitcoin::Transaction;
use clementine_core::actor::Actor;
use clementine_core::database::common::Database;
use clementine_core::create_extended_rpc;
use clementine_core::extended_rpc::ExtendedRpc;
use clementine_core::mock::common;
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 clementine_core::{
create_extended_rpc, create_test_config, create_test_config_with_thread_name,
};
use std::thread;

#[tokio::test]
async fn test_deposit_retry() {
let mut config = create_test_config_with_thread_name!("test_config.toml");
let mut config = create_test_config_with_thread_name("test_config.toml", None).await;
let rpc = create_extended_rpc!(config);

let (verifiers, operators, aggregator) =
Expand Down
Loading

0 comments on commit 2cfff4a

Please sign in to comment.