Skip to content

Commit

Permalink
general: Initial impl for using server for mock_rpc.
Browse files Browse the repository at this point in the history
  • Loading branch information
ceyhunsen committed Sep 5, 2024
1 parent e1d6ee8 commit c44415b
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 70 deletions.
46 changes: 31 additions & 15 deletions core/src/database/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,7 @@ impl Database {
mod tests {
use super::Database;
use crate::{
check_and_start_mock_rpc,
config::BridgeConfig,
create_test_config, create_test_config_with_thread_name,
mock::common,
Expand Down Expand Up @@ -678,7 +679,8 @@ mod tests {

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

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

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

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

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

Expand Down Expand Up @@ -778,7 +782,8 @@ mod tests {

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

Expand Down Expand Up @@ -820,7 +825,8 @@ mod tests {

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

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

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

Expand Down Expand Up @@ -899,7 +906,8 @@ mod tests {

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

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

let outpoint = OutPoint {
Expand All @@ -936,7 +945,8 @@ mod tests {

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

let outpoint = OutPoint {
Expand All @@ -949,7 +959,8 @@ mod tests {

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

let outpoint = OutPoint {
Expand Down Expand Up @@ -986,7 +997,8 @@ mod tests {

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

let outpoint = OutPoint {
Expand All @@ -999,7 +1011,8 @@ mod tests {

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

let utxo = UTXO {
Expand All @@ -1021,7 +1034,8 @@ mod tests {

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

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

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

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

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

let txid = Txid::from_byte_array([1u8; 32]);
Expand Down
16 changes: 10 additions & 6 deletions core/src/mock/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@
/// 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();
($db_name:expr, $config_file:expr, $rpc_server_url:expr) => {{
let mut config = common::get_test_config($config_file).unwrap();
if let Some(url) = $rpc_server_url {
config.bitcoin_rpc_url = url.to_string();
}

let config = Database::create_database(config, &$db_name).await.unwrap();

let database = Database::new(config.clone()).await.unwrap();
Expand All @@ -36,7 +40,7 @@ macro_rules! create_test_config {
/// Returns new `BridgeConfig`.
#[macro_export]
macro_rules! create_test_config_with_thread_name {
($config_file:expr, $suffix:expr) => {{
($config_file:expr, $rpc_server_url:expr, $suffix:expr) => {{
let suffix: String = $suffix.unwrap_or(&String::default()).to_string();
let handle = thread::current()
.name()
Expand All @@ -47,9 +51,9 @@ macro_rules! create_test_config_with_thread_name {
.to_owned()
+ &suffix;

create_test_config!(handle, $config_file)
create_test_config!(handle, $config_file, $rpc_server_url)
}};
($config_file:expr) => {{
create_test_config_with_thread_name!($config_file, Option::<&str>::None)
($config_file:expr, $rpc_server_url:expr) => {{
create_test_config_with_thread_name!($config_file, $rpc_server_url, Option::<&str>::None)
}};
}
50 changes: 18 additions & 32 deletions core/src/mock/rpc.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,26 @@
//! # RPC Related Mocks and Test Utilities
// #[cfg_attr(feature = "mock_rpc", mock_rpc)]

/// Creates an [`ExtendedRpc`] struct from either the real Bitcoin RPC or mock
/// RPC.
///
/// # Parameters
///
/// - `config`: Mutable `BridgeConfig` structure.
/// - `db_name`: If mock is used, this will be it's database name. If not used
/// can be dummy value.
#[cfg(feature = "mock_rpc")]
/// This macro checks if mock_rpc feature is enabled or not. If enabled, starts
/// the server and returns it's address and url. If not enabled, returns `None`.
#[macro_export]
macro_rules! create_extended_rpc {
($config:expr) => {{
println!("Using Mock RPC for testing...");
let handle = std::thread::current()
.name()
.unwrap()
.split(":")
.last()
.unwrap()
.to_owned();

$config.bitcoin_rpc_url = handle.to_string();
macro_rules! check_and_start_mock_rpc {
() => {{
#[cfg(feature = "mock_rpc")]
{
let ret = bitcoin_mock_rpc::rpc::spawn_rpc_server(None, None).unwrap();
(Some(ret.0), Some(ret.1))
}

ExtendedRpc::<bitcoin_mock_rpc::Client>::new(
$config.bitcoin_rpc_url.clone(),
$config.bitcoin_rpc_user.clone(),
$config.bitcoin_rpc_password.clone(),
)
#[cfg(not(feature = "mock_rpc"))]
{
(
Option::<std::net::SocketAddr>::None,
Option::<std::thread::JoinHandle<()>>::None,
)
}
}};
}

/// Creates an [`ExtendedRpc`] struct from either the real Bitcoin RPC or mock
/// RPC.
///
Expand All @@ -40,14 +29,11 @@ macro_rules! create_extended_rpc {
/// - `config`: Mutable `BridgeConfig` structure.
/// - `db_name`: If mock is used, this will be it's database name. If not used
/// can be dummy value.
#[cfg(not(feature = "mock_rpc"))]
#[macro_export]
macro_rules! create_extended_rpc {
($config:expr) => {{
println!("Using Bitcoin regtest for testing...");

// Mutation for consistency with above defined macro
$config.bitcoin_rpc_url = $config.bitcoin_rpc_url.clone();
// $config.bitcoin_rpc_url = $config.bitcoin_rpc_url.clone();

ExtendedRpc::<bitcoincore_rpc::Client>::new(
$config.bitcoin_rpc_url.clone(),
Expand Down
8 changes: 4 additions & 4 deletions core/src/servers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,13 @@ fn is_test_env() -> bool {
/// Panics if there was an error while creating any of the servers.
pub async fn create_verifiers_and_operators(
config_name: &str,
// rpc: ExtendedRpc<R>,
server: Option<std::net::SocketAddr>, // rpc: ExtendedRpc<R>,
) -> (
Vec<(HttpClient, ServerHandle, std::net::SocketAddr)>, // Verifier clients
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 config = create_test_config_with_thread_name!(config_name, server);
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 +151,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, server, Some(&i.to_string()));
let verifier = create_verifier_server(
BridgeConfig {
secret_key: *sk,
Expand Down Expand Up @@ -237,7 +237,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, server);
println!("Port: {}", start_port);
let port = start_port
+ all_verifiers_secret_keys.len() as u16
Expand Down
5 changes: 3 additions & 2 deletions core/tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use std::thread;

pub async fn run_single_deposit(
test_config_name: &str,
server: Option<std::net::SocketAddr>,
) -> Result<
(
Vec<(HttpClient, ServerHandle, SocketAddr)>,
Expand All @@ -32,11 +33,11 @@ pub async fn run_single_deposit(
),
BridgeError,
> {
let mut config = create_test_config_with_thread_name!(test_config_name);
let config = create_test_config_with_thread_name!(test_config_name, server);
let rpc = create_extended_rpc!(config);

let (verifiers, operators, aggregator) =
create_verifiers_and_operators("test_config.toml").await;
create_verifiers_and_operators("test_config.toml", server).await;

// println!("Operators: {:#?}", operators);
// println!("Verifiers: {:#?}", verifiers);
Expand Down
11 changes: 8 additions & 3 deletions core/tests/flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// //! This testss checks if basic deposit and withdraw operations are OK or not.

use bitcoin::Address;
use clementine_core::check_and_start_mock_rpc;
use clementine_core::extended_rpc::ExtendedRpc;
use clementine_core::{
create_extended_rpc, errors::BridgeError, traits::rpc::OperatorRpcClient, user::User,
Expand All @@ -14,7 +15,8 @@ mod common;

#[tokio::test]
async fn test_deposit() -> Result<(), BridgeError> {
match run_single_deposit("test_config.toml").await {
let server = check_and_start_mock_rpc!();
match run_single_deposit("test_config.toml", server.0).await {
Ok((_, _, _, deposit_outpoint)) => {
// tracing::debug!("Verifiers: {:#?}", verifiers);
// tracing::debug!("Operators: {:#?}", operators);
Expand All @@ -30,9 +32,12 @@ async fn test_deposit() -> Result<(), BridgeError> {

#[tokio::test]
async fn test_honest_operator_takes_refund() {
let server = check_and_start_mock_rpc!();
// let mut config = create_test_config_with_thread_name!("test_config_flow.toml");
let (_verifiers, operators, mut config, deposit_outpoint) =
run_single_deposit("test_config.toml").await.unwrap();
let (_verifiers, operators, config, deposit_outpoint) =
run_single_deposit("test_config.toml", server.0)
.await
.unwrap();
let rpc = create_extended_rpc!(config);

let secp = bitcoin::secp256k1::Secp256k1::new();
Expand Down
12 changes: 8 additions & 4 deletions core/tests/musig2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,18 @@ use clementine_core::{
utils, ByteArray66,
};
use clementine_core::{
create_extended_rpc, create_test_config, create_test_config_with_thread_name,
check_and_start_mock_rpc, create_extended_rpc, create_test_config,
create_test_config_with_thread_name,
};
use secp256k1::{Keypair, Message};
use std::thread;

#[tokio::test]
async fn test_musig2_key_spend() {
let server = check_and_start_mock_rpc!();
let secp = bitcoin::secp256k1::Secp256k1::new();

let mut config: BridgeConfig = create_test_config_with_thread_name!("test_config.toml");
let config: BridgeConfig = create_test_config_with_thread_name!("test_config.toml", server.0);
let rpc: ExtendedRpc<_> = create_extended_rpc!(config);
let sks = config.all_verifiers_secret_keys.unwrap();
let kp_vec: Vec<Keypair> = sks
Expand Down Expand Up @@ -124,9 +126,10 @@ async fn test_musig2_key_spend() {

#[tokio::test]
async fn test_musig2_key_spend_with_script() {
let server = check_and_start_mock_rpc!();
let secp = bitcoin::secp256k1::Secp256k1::new();

let mut config: BridgeConfig = create_test_config_with_thread_name!("test_config.toml");
let config: BridgeConfig = create_test_config_with_thread_name!("test_config.toml", server.0);
let rpc: ExtendedRpc<_> = create_extended_rpc!(config);
let sks = config.all_verifiers_secret_keys.unwrap();
let kp_vec: Vec<Keypair> = sks
Expand Down Expand Up @@ -228,9 +231,10 @@ async fn test_musig2_key_spend_with_script() {

#[tokio::test]
async fn test_musig2_script_spend() {
let server = check_and_start_mock_rpc!();
let secp = bitcoin::secp256k1::Secp256k1::new();

let mut config: BridgeConfig = create_test_config_with_thread_name!("test_config.toml");
let config: BridgeConfig = create_test_config_with_thread_name!("test_config.toml", server.0);
let rpc: ExtendedRpc<_> = create_extended_rpc!(config);
let sks = config.all_verifiers_secret_keys.unwrap();
let kp_vec: Vec<Keypair> = sks
Expand Down
Loading

0 comments on commit c44415b

Please sign in to comment.