Skip to content

Commit 1d5a394

Browse files
authored
Merge pull request dmnd-pool#114 from jbesraa/2025-07-30/add-testnet3-support
add testnet3 env variable and server endpoint
2 parents 3eb66dd + 2a1722a commit 1d5a394

3 files changed

Lines changed: 52 additions & 16 deletions

File tree

src/config.rs

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ use std::{
88
};
99
use tracing::{debug, error, info, warn};
1010

11-
use crate::{shared::error::Error, HashUnit, DEFAULT_SV1_HASHPOWER, PRODUCTION_URL, STAGING_URL};
11+
use crate::{
12+
shared::error::Error, HashUnit, DEFAULT_SV1_HASHPOWER, PRODUCTION_URL, STAGING_URL,
13+
TESTNET3_URL,
14+
};
1215
lazy_static! {
1316
pub static ref CONFIG: Configuration = Configuration::load_config();
1417
}
@@ -17,6 +20,8 @@ struct Args {
1720
#[clap(long)]
1821
staging: bool,
1922
#[clap(long)]
23+
testnet3: bool,
24+
#[clap(long)]
2025
local: bool,
2126
#[clap(long = "d", short = 'd', value_parser = parse_hashrate)]
2227
downstream_hashrate: Option<f32>,
@@ -58,6 +63,7 @@ struct ConfigFile {
5863
sv1_log: Option<bool>,
5964
staging: Option<bool>,
6065
local: Option<bool>,
66+
testnet3: Option<bool>,
6167
listening_addr: Option<String>,
6268
api_server_port: Option<String>,
6369
monitor: Option<bool>,
@@ -76,6 +82,7 @@ impl ConfigFile {
7682
nc_loglevel: None,
7783
sv1_log: None,
7884
staging: None,
85+
testnet3: None,
7986
local: None,
8087
listening_addr: None,
8188
api_server_port: None,
@@ -95,6 +102,7 @@ pub struct Configuration {
95102
nc_loglevel: String,
96103
sv1_log: bool,
97104
staging: bool,
105+
testnet3: bool,
98106
local: bool,
99107
listening_addr: Option<String>,
100108
api_server_port: String,
@@ -177,6 +185,10 @@ impl Configuration {
177185
CONFIG.local
178186
}
179187

188+
pub fn testnet3() -> bool {
189+
CONFIG.testnet3
190+
}
191+
180192
/// Returns the environment based on the configuration.
181193
/// Possible values: "staging", "local", "production".
182194
/// If no environment is set, it defaults to "production".
@@ -185,6 +197,8 @@ impl Configuration {
185197
"staging".to_string()
186198
} else if CONFIG.local {
187199
"local".to_string()
200+
} else if CONFIG.testnet3 {
201+
"testnet3".to_string()
188202
} else {
189203
"production".to_string()
190204
}
@@ -291,6 +305,8 @@ impl Configuration {
291305

292306
let staging =
293307
args.staging || config.staging.unwrap_or(false) || std::env::var("STAGING").is_ok();
308+
let testnet3 =
309+
args.testnet3 || config.testnet3.unwrap_or(false) || std::env::var("TESTNET3").is_ok();
294310
let local = args.local || config.local.unwrap_or(false) || std::env::var("LOCAL").is_ok();
295311
let monitor =
296312
args.monitor || config.monitor.unwrap_or(false) || std::env::var("MONITOR").is_ok();
@@ -309,6 +325,7 @@ impl Configuration {
309325
nc_loglevel,
310326
sv1_log,
311327
staging,
328+
testnet3,
312329
local,
313330
listening_addr,
314331
api_server_port,
@@ -351,23 +368,36 @@ fn parse_hashrate(hashrate_str: &str) -> Result<f32, String> {
351368
Ok(hashrate)
352369
}
353370

354-
fn parse_address(addr: String) -> SocketAddr {
355-
addr.to_socket_addrs()
356-
.map_err(|e| error!("Invalid socket address: {}", e))
357-
.expect("Failed to parse socket address")
358-
.next()
359-
.expect("No socket address resolved")
371+
fn parse_address(addr: String) -> Option<SocketAddr> {
372+
match addr.to_socket_addrs() {
373+
Ok(mut addrs) => match addrs.next() {
374+
Some(socket_addr) => Some(socket_addr),
375+
None => {
376+
error!("Failed to parse address: {}", addr);
377+
None
378+
}
379+
},
380+
Err(e) => {
381+
error!("Failed to parse address '{}': {}", addr, e);
382+
None
383+
}
384+
}
360385
}
361386

362387
/// Fetches pool URLs from the server based on the environment.
363388
async fn fetch_pool_urls() -> Result<Vec<SocketAddr>, Error> {
364389
if CONFIG.local {
365-
return Ok(vec![parse_address("127.0.0.1:20000".to_string())]);
390+
return Ok(vec![
391+
parse_address("127.0.0.1:20000".to_string()).expect("Invalid local address")
392+
]);
366393
};
367394

368395
let url = if CONFIG.staging {
369396
info!("Fetching pool URLs from staging server: {}", STAGING_URL);
370397
STAGING_URL
398+
} else if CONFIG.testnet3 {
399+
info!("Fetching pool URLs from testnet server: {}", TESTNET3_URL);
400+
TESTNET3_URL
371401
} else {
372402
info!(
373403
"Fetching pool URLs from production server: {}",
@@ -403,10 +433,10 @@ async fn fetch_pool_urls() -> Result<Vec<SocketAddr>, Error> {
403433
// Parse the addresses into SocketAddr
404434
let socket_addrs: Vec<SocketAddr> = addresses
405435
.into_iter()
406-
.map(|addr| {
436+
.filter_map(|addr| {
407437
let address = format!("{}:{}", addr.host, addr.port);
408438
parse_address(address)
409-
})
439+
}) // Filter out any None values
410440
.collect();
411441
debug!("Pool addresses: {:?}", socket_addrs);
412442
Ok(socket_addrs)

src/main.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ const REPO_OWNER: &str = "demand-open-source";
4343
const REPO_NAME: &str = "demand-cli";
4444
const BIN_NAME: &str = "demand-cli";
4545
const STAGING_URL: &str = "https://staging-user-dashboard-server.dmnd.work";
46-
const LOCAL_URL: &str = "http://localhost:8787/api";
46+
const LOCAL_URL: &str = "http://localhost:8787";
47+
const TESTNET3_URL: &str = "https://testnet3-user-dashboard-server.dmnd.work";
4748
const PRODUCTION_URL: &str = "https://production-user-dashboard-server.dmnd.work";
4849

4950
lazy_static! {
@@ -61,7 +62,7 @@ lazy_static! {
6162

6263
// for staging and local environments, use the test auth public key
6364
// for production, use the main auth public key
64-
pub static ref AUTH_PUB_KEY: &'static str = if Configuration::staging() || Configuration::local() {
65+
pub static ref AUTH_PUB_KEY: &'static str = if Configuration::staging() || Configuration::local() || Configuration::testnet3() {
6566
TEST_AUTH_PUB_KEY
6667
} else {
6768
MAIN_AUTH_PUB_KEY
@@ -100,6 +101,9 @@ async fn main() {
100101
if Configuration::local() {
101102
info!("Package is running in local mode");
102103
}
104+
if Configuration::testnet3() {
105+
info!("Package is running in testnet3 mode");
106+
}
103107

104108
let auth_pub_k: Secp256k1PublicKey = AUTH_PUB_KEY.parse().expect("Invalid public key");
105109

@@ -108,6 +112,7 @@ async fn main() {
108112
.filter(|p| !p.is_empty())
109113
.unwrap_or_else(|| match Configuration::environment().as_str() {
110114
"staging" => panic!("Staging pool address is missing"),
115+
"testnet3" => panic!("Testnet3 pool address is missing"),
111116
"local" => panic!("Local pool address is missing"),
112117
"production" => panic!("Pool address is missing"),
113118
_ => unreachable!(),

src/shares_monitor.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,16 @@ use crate::{
99
config::Configuration,
1010
proxy_state::{DownstreamType, ProxyState},
1111
shared::error::Error,
12-
LOCAL_URL, PRODUCTION_URL, STAGING_URL,
12+
LOCAL_URL, PRODUCTION_URL, STAGING_URL, TESTNET3_URL,
1313
};
1414

1515
fn monitoring_server_url() -> String {
1616
// Determine the monitoring server URL based on the environment
1717
match Configuration::environment().as_str() {
18-
"staging" => format!("{}/share/save", STAGING_URL),
19-
"local" => format!("{}/share/save", LOCAL_URL),
20-
"production" => format!("{}/share/save", PRODUCTION_URL),
18+
"staging" => format!("{}/api/share/save", STAGING_URL),
19+
"testnet3" => format!("{}/api/share/save", TESTNET3_URL),
20+
"local" => format!("{}/api/share/save", LOCAL_URL),
21+
"production" => format!("{}/api/share/save", PRODUCTION_URL),
2122
_ => unreachable!(),
2223
}
2324
}

0 commit comments

Comments
 (0)