Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ impl ConfigFile {
#[derive(Debug)]
pub struct Configuration {
token: Option<String>,
tp_address: Option<String>,
tp_address: Option<SocketAddr>,
interval: u64,
delay: u64,
downstream_hashrate: f32,
Expand All @@ -129,7 +129,7 @@ impl Configuration {
#[allow(clippy::too_many_arguments)]
pub fn new(
token: Option<String>,
tp_address: Option<String>,
tp_address: Option<SocketAddr>,
interval: u64,
delay: u64,
downstream_hashrate: f32,
Expand Down Expand Up @@ -217,8 +217,8 @@ impl Configuration {
Self::cfg().token.clone()
}

pub fn tp_address() -> Option<String> {
Self::cfg().tp_address.clone()
pub fn tp_address() -> Option<SocketAddr> {
Self::cfg().tp_address
}

pub async fn pool_address() -> Option<Vec<SocketAddr>> {
Expand Down Expand Up @@ -369,6 +369,17 @@ impl Configuration {
.tp_address
.or(config.tp_address)
.or_else(|| std::env::var("TP_ADDRESS").ok());
let tp_address = tp_address.map(|tp| {
let addr = tp.parse::<std::net::SocketAddr>().unwrap_or_else(|e| {
error!("Invalid TP address '{tp}': {e}. Expected format: 'ip:port' (e.g., '127.0.0.1:8442')");
std::process::exit(1);
});
if let Err(e) = std::net::TcpStream::connect_timeout(&addr, std::time::Duration::from_secs(3)) {
error!("Error: TP address '{addr}' is not reachable: {e}");
std::process::exit(1);
}
addr
});

let miner_name = args
.miner_name
Expand Down
23 changes: 5 additions & 18 deletions src/jd_client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,7 @@ pub static IS_NEW_PHASH_ARRIVED: AtomicBool = AtomicBool::new(false);

use crate::proxy_state::{DownstreamType, ProxyState, TpState};
use roles_logic_sv2::{parsers::Mining, utils::Mutex};
use std::{
net::{IpAddr, SocketAddr},
str::FromStr,
sync::Arc,
};
use std::{net::SocketAddr, sync::Arc};

use crate::shared::utils::AbortOnDrop;

Expand Down Expand Up @@ -96,7 +92,7 @@ async fn initialize_jd(
};

// Initialize JD part
let tp_address = match crate::TP_ADDRESS.safe_lock(|tp| tp.clone()) {
let tp_address = match crate::TP_ADDRESS.safe_lock(|tp| *tp) {
Ok(tp_address) => tp_address
.expect("Unreachable code, jdc is not instantiated when TP_ADDRESS not present"),
Err(e) => {
Expand All @@ -106,10 +102,6 @@ async fn initialize_jd(
}
};

let mut parts = tp_address.split(':');
let ip_tp = parts.next().expect("The passed value for TP address is not valid. Terminating.... TP_ADDRESS should be in this format `127.0.0.1:8442`").to_string();
let port_tp = parts.next().expect("The passed value for TP address is not valid. Terminating.... TP_ADDRESS should be in this format `127.0.0.1:8442`").parse::<u16>().expect("This operation should not fail because a valid port_tp should always be converted to U16");

let auth_pub_k: Secp256k1PublicKey = crate::AUTH_PUB_KEY.parse().expect("Invalid public key");
let address = match crate::ACTIVE_POOL_ADDRESS.safe_lock(|address| *address) {
Ok(Some(address)) => address,
Expand Down Expand Up @@ -205,10 +197,8 @@ async fn initialize_jd(
drop(abortable); // drop all tasks initailzed upto this point
return None;
};
let ip = IpAddr::from_str(ip_tp.as_str())
.expect("Infallable Operation: Failed tp can always be converted into IpAddr");
let tp_abortable = match TemplateRx::connect(
SocketAddr::new(ip, port_tp),
tp_address,
recv_solution,
Some(jd.clone()),
donwstream.clone(),
Expand Down Expand Up @@ -249,15 +239,12 @@ async fn initialize_jd(
}

// Used when tp is down or connection was unsuccessful to retry connection.
async fn retry_connection(address: String) {
async fn retry_connection(address: SocketAddr) {
let mut interval = tokio::time::interval(tokio::time::Duration::from_secs(5));
loop {
info!("TP Retrying connection....");
interval.tick().await;
if tokio::net::TcpStream::connect(address.clone())
.await
.is_ok()
{
if tokio::net::TcpStream::connect(address).await.is_ok() {
info!("Successfully reconnected to TP: Restarting Proxy...");
if crate::TP_ADDRESS
.safe_lock(|tp| *tp = Some(address))
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const TESTNET3_URL: &str = "https://testnet3-user-dashboard-server.dmnd.work";
const PRODUCTION_URL: &str = "https://production-user-dashboard-server.dmnd.work";

lazy_static! {
static ref TP_ADDRESS: roles_logic_sv2::utils::Mutex<Option<String>> =
static ref TP_ADDRESS: roles_logic_sv2::utils::Mutex<Option<SocketAddr>> =
roles_logic_sv2::utils::Mutex::new(Configuration::tp_address());
static ref ACTIVE_POOL_ADDRESS: roles_logic_sv2::utils::Mutex<Option<SocketAddr>> =
roles_logic_sv2::utils::Mutex::new(None); // Connected pool address
Expand Down Expand Up @@ -234,7 +234,7 @@ async fn initialize_proxy(

let jdc_abortable: Option<AbortOnDrop>;
let share_accounter_abortable;
let tp = match TP_ADDRESS.safe_lock(|tp| tp.clone()) {
let tp = match TP_ADDRESS.safe_lock(|tp| *tp) {
Ok(tp) => tp,
Err(e) => {
error!("TP_ADDRESS Mutex Corrupted: {e}");
Expand Down
2 changes: 1 addition & 1 deletion tests/library_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ async fn library_init_sv2_setup_connection() {

let config = dmnd_client::Configuration::new(
Some("test_token".to_string()),
Some(tp_sniffer_addr.to_string()),
Some(tp_sniffer_addr),
120_000,
0,
100_000_000_000_000.0,
Expand Down