use std::{ net::IpAddr, sync::LazyLock, time::{Duration, SystemTime, UNIX_EPOCH}, }; use reqwest::{StatusCode, header::HeaderMap}; use serde_json::json; use tokio::{ io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt}, net::{TcpStream, ToSocketAddrs}, }; use crate::errors::{ModelSelectionError, RPCError}; /// Default read timeout for RPC stream responses. pub const DEFAULT_RPC_TIMEOUT: Duration = Duration::from_secs(5); pub fn unix_timestamp_secs() -> u64 { SystemTime::now().duration_since(UNIX_EPOCH).map_or_else( |error| { tracing::error!(?error, "failed to get system time"); 0 }, |duration| duration.as_secs(), ) } /// Build an HTTP client with bounded connection and total-request deadlines /// for firmware discovery. pub fn build_discovery_client() -> Result { build_discovery_client_with_timeout(DEFAULT_RPC_TIMEOUT) } fn build_discovery_client_with_timeout( timeout: Duration, ) -> Result { reqwest::Client::builder() .connect_timeout(timeout) .timeout(timeout) .pool_max_idle_per_host(0) .build() .map_err(|_| ModelSelectionError::NoModelResponse) } /// Returns true if the error is expected after a privileged command was sent: /// the miner did not reply or closed the connection while applying it. pub fn is_expected_write_error(err: &anyhow::Error) -> bool { err.downcast_ref::() .is_some_and(|e| e.is_transient()) } /// Shared HTTP client for discovery and utility requests. /// Reused across all calls to avoid per-request client construction overhead. static HTTP_CLIENT: LazyLock> = LazyLock::new(|| { reqwest::Client::builder() .redirect(reqwest::redirect::Policy::none()) .danger_accept_invalid_certs(true) .gzip(true) .connect_timeout(DEFAULT_RPC_TIMEOUT) .timeout(DEFAULT_RPC_TIMEOUT) .pool_max_idle_per_host(0) .build() }); fn http_client() -> Option<&'static reqwest::Client> { match HTTP_CLIENT.as_ref() { Ok(client) => Some(client), Err(err) => { tracing::error!("failed to initialize shared HTTP client: {err}"); None } } } /// Connect to a miner TCP endpoint with a bounded timeout. pub async fn connect_tcp_stream(addr: A, timeout: Duration) -> anyhow::Result where A: ToSocketAddrs, { tokio::time::timeout(timeout, TcpStream::connect(addr)) .await .map_err(|_| RPCError::ConnectionFailed)? .map_err(RPCError::from) .map_err(Into::into) } /// Read a complete RPC response from a stream. /// /// Miners typically terminate responses with `\0` or `\n` but keep the TCP /// connection open, so `read_to_end` would block forever. This reads in /// chunks and stops when a terminator is found, the stream closes, or the /// timeout expires (e.g. when a miner reboots mid-response). pub async fn read_stream_response( stream: &mut (impl AsyncRead + Unpin), timeout: Duration, ) -> anyhow::Result { tokio::time::timeout(timeout, async { let mut response = String::new(); let mut buffer = [0u8; 8192]; loop { let bytes_read = stream.read(&mut buffer).await.map_err(RPCError::from)?; if bytes_read == 0 { break; } let chunk = String::from_utf8_lossy(&buffer[..bytes_read]); response.push_str(&chunk); if response.contains('\0') || response.ends_with('\n') { break; } } Ok(response.trim_end_matches(['\0', '\n']).to_owned()) }) .await .map_err(|_| RPCError::ReadTimeout)? } /// Read exactly `buf.len()` bytes from a stream with a timeout. pub async fn read_exact_with_timeout( stream: &mut (impl AsyncRead + Unpin), buf: &mut [u8], timeout: Duration, ) -> anyhow::Result<()> { tokio::time::timeout(timeout, stream.read_exact(buf)) .await .map_err(|_| RPCError::ReadTimeout)? .map_err(RPCError::from)?; Ok(()) } /// Write a complete RPC request with a timeout. pub async fn write_all_with_timeout( stream: &mut (impl AsyncWrite + Unpin), buf: &[u8], timeout: Duration, ) -> anyhow::Result<()> { tokio::time::timeout(timeout, stream.write_all(buf)) .await .map_err(|_| RPCError::WriteTimeout)? .map_err(RPCError::from)?; Ok(()) } #[tracing::instrument(level = "debug")] pub async fn send_rpc_command(ip: &IpAddr, command: &'static str) -> Option { let response = { let mut stream = connect_tcp_stream((*ip, 4028), DEFAULT_RPC_TIMEOUT) .await .map_err(|_| tracing::debug!("failed to connect to {ip} rpc")) .ok()?; let command = format!("{{\"command\":\"{command}\"}}"); if let Err(err) = write_all_with_timeout(&mut stream, command.as_bytes(), DEFAULT_RPC_TIMEOUT).await { tracing::debug!("failed to write command to {ip}: {err:?}"); return None; } read_stream_response(&mut stream, DEFAULT_RPC_TIMEOUT).await }; let response = match response { Ok(r) => r, Err(err) => { tracing::debug!("failed to read response from {ip}: {err:?}"); return None; } }; tracing::trace!("got response from miner: {response}"); parse_rpc_result(&response) } #[tracing::instrument(level = "debug")] pub async fn send_web_command( ip: &IpAddr, command: &'static str, ) -> Option<(String, HeaderMap, StatusCode)> { let data = http_client()? .get(format!("http://{ip}{command}")) .send() .await .map_err(|_| tracing::debug!("failed to connect to {ip} web")) .ok()?; let headers = data.headers().clone(); let status = data.status(); let text = data .text() .await .map_err(|_| tracing::debug!("received no response data from miner")) .ok()?; tracing::trace!("got response from miner: {text}"); Some((text, headers, status)) } #[tracing::instrument(level = "debug")] pub async fn send_graphql_command(ip: &IpAddr, command: &'static str) -> Option { let query = json!({ "query": command }); let response = http_client()? .post(format!("http://{}/graphql", ip)) .header("Content-Type", "application/json") .json(&query) .send() .await .ok()?; response.json().await.ok()? } #[tracing::instrument(level = "debug")] fn parse_rpc_result(response: &str) -> Option { // Fix for WM V1, can have newlines in version which breaks the json parser let response = response.replace('\n', ""); let parsed: Result = serde_json::from_str(&response); let success_codes = ["S", "I"]; match parsed.ok() { Some(data) => { let command_status_generic = data["STATUS"][0]["STATUS"].as_str(); let command_status_whatsminer = data["STATUS"].as_str(); let command_status = command_status_generic.or(command_status_whatsminer); match command_status { Some(status) => { if success_codes.contains(&status) { tracing::trace!("found success code from miner: {status}"); Some(data) } else { tracing::debug!("got error status from miner: {status}"); None } } None => { tracing::debug!("could not find result status"); None } } } None => { tracing::debug!("failed to parse response"); None } } } #[cfg(test)] mod tests { use super::*; use crate::errors::RPCError; use tokio::{io::AsyncWriteExt, net::TcpListener}; #[tokio::test] async fn discovery_http_client_times_out_on_stalled_response() -> anyhow::Result<()> { let listener = TcpListener::bind((std::net::Ipv4Addr::LOCALHOST, 0)).await?; let address = listener.local_addr()?; let server = tokio::spawn(async move { let (_socket, _) = listener.accept().await?; tokio::time::sleep(Duration::from_secs(2)).await; std::io::Result::Ok(()) }); let client = build_discovery_client_with_timeout(Duration::from_millis(50))?; let started = tokio::time::Instant::now(); let response = client.get(format!("http://{address}")).send().await; assert!(response.is_err_and(|error| error.is_timeout())); assert!(started.elapsed() < Duration::from_secs(1)); server.abort(); Ok(()) } #[tokio::test] async fn null_terminated_response() { // Arrange let (mut writer, mut reader) = tokio::io::duplex(8192); tokio::spawn(async move { writer.write_all(b"{\"STATUS\":\"S\"}\0").await.unwrap(); }); // Act let result = read_stream_response(&mut reader, Duration::from_secs(5)) .await .unwrap(); // Assert assert_eq!(result, "{\"STATUS\":\"S\"}"); } #[tokio::test] async fn newline_terminated_response() { // Arrange let (mut writer, mut reader) = tokio::io::duplex(8192); tokio::spawn(async move { writer.write_all(b"{\"STATUS\":\"S\"}\n").await.unwrap(); }); // Act let result = read_stream_response(&mut reader, Duration::from_secs(5)) .await .unwrap(); // Assert assert_eq!(result, "{\"STATUS\":\"S\"}"); } #[tokio::test] async fn multi_chunk_response() { // Arrange let (mut writer, mut reader) = tokio::io::duplex(64); tokio::spawn(async move { writer.write_all(b"{\"STATUS\":").await.unwrap(); writer.write_all(b"\"S\"}\0").await.unwrap(); }); // Act let result = read_stream_response(&mut reader, Duration::from_secs(5)) .await .unwrap(); // Assert assert_eq!(result, "{\"STATUS\":\"S\"}"); } #[tokio::test] async fn empty_response_on_stream_close() { // Arrange let mut reader = { let (_writer, reader) = tokio::io::duplex(8192); reader }; // Act let result = read_stream_response(&mut reader, Duration::from_secs(5)) .await .unwrap(); // Assert assert_eq!(result, ""); } #[tokio::test] async fn both_terminators_trimmed() { // Arrange let (mut writer, mut reader) = tokio::io::duplex(8192); tokio::spawn(async move { writer.write_all(b"{\"STATUS\":\"S\"}\0\n").await.unwrap(); }); // Act let result = read_stream_response(&mut reader, Duration::from_secs(5)) .await .unwrap(); // Assert assert_eq!(result, "{\"STATUS\":\"S\"}"); } #[tokio::test] async fn read_timeout_fires() { // Arrange — duplex with no data written, simulating a miner that rebooted let (_writer, mut reader) = tokio::io::duplex(8192); // Act let result = read_stream_response(&mut reader, Duration::from_millis(100)).await; // Assert let err = result.unwrap_err(); assert!( err.downcast_ref::() .is_some_and(|e| matches!(e, RPCError::ReadTimeout)) ); } #[tokio::test] async fn write_timeout_fires() { // Arrange - keep the read half open but unread so the tiny buffer fills. let (mut writer, _reader) = tokio::io::duplex(1); // Act let result = write_all_with_timeout(&mut writer, &[0; 1024], Duration::from_millis(100)).await; // Assert let err = result.unwrap_err(); assert!( err.downcast_ref::() .is_some_and(|e| matches!(e, RPCError::WriteTimeout)) ); } #[test] fn expected_write_errors_require_command_delivery() { let read_timeout = anyhow::Error::new(RPCError::ReadTimeout); let write_timeout = anyhow::Error::new(RPCError::WriteTimeout); assert!(is_expected_write_error(&read_timeout)); assert!(!is_expected_write_error(&write_timeout)); } }