diff --git a/roles/tests-integration/lib/mod.rs b/roles/tests-integration/lib/mod.rs index 7de630dda..c6cb7354c 100644 --- a/roles/tests-integration/lib/mod.rs +++ b/roles/tests-integration/lib/mod.rs @@ -5,21 +5,17 @@ use key_utils::{Secp256k1PublicKey, Secp256k1SecretKey}; use pool_sv2::PoolSv2; use translator_sv2::TranslatorSv2; -use once_cell::sync::Lazy; use rand::{thread_rng, Rng}; use std::{ - collections::HashSet, convert::{TryFrom, TryInto}, - net::{SocketAddr, TcpListener}, + net::SocketAddr, str::FromStr, - sync::Mutex, }; +use utils::get_available_address; pub mod sniffer; pub mod template_provider; - -// prevents get_available_port from ever returning the same port twice -static UNIQUE_PORTS: Lazy>> = Lazy::new(|| Mutex::new(HashSet::new())); +mod utils; pub async fn start_sniffer( identifier: String, @@ -49,11 +45,11 @@ pub async fn start_pool(template_provider_address: Option) -> (PoolS let authority_public_key = Secp256k1PublicKey::try_from( "9auqWEzQDVyd2oe1JVGFLMLHZtCo2FFqZwtKA5gd9xbuEu7PH72".to_string(), ) - .expect("failed"); + .expect("failed"); let authority_secret_key = Secp256k1SecretKey::try_from( "mkDLTBBRxdBv998612qipDYoTK3YUrqLe8uWw7gu3iXbSrn2n".to_string(), ) - .expect("failed"); + .expect("failed"); let cert_validity_sec = 3600; let coinbase_outputs = vec![CoinbaseOutput::new( "P2WPKH".to_string(), @@ -319,24 +315,3 @@ pub async fn start_mining_sv2_proxy(upstream: SocketAddr) -> SocketAddr { }); mining_proxy_listening_address } - -fn get_available_port() -> u16 { - let mut unique_ports = UNIQUE_PORTS.lock().unwrap(); - - loop { - let port = TcpListener::bind("127.0.0.1:0") - .unwrap() - .local_addr() - .unwrap() - .port(); - if !unique_ports.contains(&port) { - unique_ports.insert(port); - return port; - } - } -} - -fn get_available_address() -> SocketAddr { - let port = get_available_port(); - SocketAddr::from(([127, 0, 0, 1], port)) -} diff --git a/roles/tests-integration/lib/utils.rs b/roles/tests-integration/lib/utils.rs new file mode 100644 index 000000000..76ae7945e --- /dev/null +++ b/roles/tests-integration/lib/utils.rs @@ -0,0 +1,30 @@ +use once_cell::sync::Lazy; +use std::{ + collections::HashSet, + net::{SocketAddr, TcpListener}, + sync::Mutex, +}; + +// prevents get_available_port from ever returning the same port twice +static UNIQUE_PORTS: Lazy>> = Lazy::new(|| Mutex::new(HashSet::new())); + +pub fn get_available_address() -> SocketAddr { + let port = get_available_port(); + SocketAddr::from(([127, 0, 0, 1], port)) +} + +fn get_available_port() -> u16 { + let mut unique_ports = UNIQUE_PORTS.lock().unwrap(); + + loop { + let port = TcpListener::bind("127.0.0.1:0") + .unwrap() + .local_addr() + .unwrap() + .port(); + if !unique_ports.contains(&port) { + unique_ports.insert(port); + return port; + } + } +}