@@ -8,7 +8,10 @@ use std::{
88} ;
99use 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+ } ;
1215lazy_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.
363388async 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)
0 commit comments