Skip to content

Commit

Permalink
Remove some of the unwraps in jd-client
Browse files Browse the repository at this point in the history
Added error handling around some of the connection setups.
..more is needed to be fixed in future commits.
  • Loading branch information
jbesraa committed Jun 21, 2024
1 parent f7d8378 commit bcbf6a0
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 18 deletions.
21 changes: 21 additions & 0 deletions roles/jd-client/src/lib/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ pub enum Error<'a> {
ChannelErrorSender(ChannelSendError<'a>),
Uint256Conversion(ParseLengthError),
Infallible(std::convert::Infallible),
/// Errors from `network_helpers_sv2` crate.
Network(network_helpers_sv2::Error),
/// Errors from prasing network address.
NetworkAddressParse(std::net::AddrParseError),
/// Errors from setting up connection.
ConnectionSetup,
}

impl<'a> fmt::Display for Error<'a> {
Expand All @@ -79,6 +85,9 @@ impl<'a> fmt::Display for Error<'a> {
Uint256Conversion(ref e) => write!(f, "U256 Conversion Error: `{:?}`", e),
VecToSlice32(ref e) => write!(f, "Standard Error: `{:?}`", e),
Infallible(ref e) => write!(f, "Infallible Error:`{:?}`", e),
Network(ref e) => write!(f, "Network Error: `{:?}`", e),
NetworkAddressParse(ref e) => write!(f, "Network Address Parse Error: `{:?}`", e),
ConnectionSetup => write!(f, "Setup Connection Error"),
}
}
}
Expand Down Expand Up @@ -137,6 +146,18 @@ impl<'a> From<tokio::sync::broadcast::error::RecvError> for Error<'a> {
}
}

impl<'a> From<network_helpers_sv2::Error> for Error<'a> {
fn from(e: network_helpers_sv2::Error) -> Self {
Error::Network(e)
}
}

impl<'a> From<std::net::AddrParseError> for Error<'a> {
fn from(e: std::net::AddrParseError) -> Self {
Error::NetworkAddressParse(e)
}
}

// *** LOCK ERRORS ***
// impl<'a> From<PoisonError<MutexGuard<'a, proxy::Bridge>>> for Error<'a> {
// fn from(e: PoisonError<MutexGuard<'a, proxy::Bridge>>) -> Self {
Expand Down
26 changes: 13 additions & 13 deletions roles/jd-client/src/lib/job_declarator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,25 +87,25 @@ impl JobDeclarator {
let stream = tokio::net::TcpStream::connect(address).await?;
let initiator = Initiator::from_raw_k(authority_public_key)?;
let (mut receiver, mut sender, _, _) =
Connection::new(stream, HandshakeRole::Initiator(initiator))
.await
.expect("impossible to connect");
Connection::new(stream, HandshakeRole::Initiator(initiator)).await?;

let proxy_address = SocketAddr::new(
IpAddr::from_str(&config.downstream_address).unwrap(),
config.downstream_port,
);
let downstream_address = IpAddr::from_str(&config.downstream_address)?;
let proxy_address = SocketAddr::new(downstream_address, config.downstream_port);

info!(
"JD proxy: setupconnection Proxy address: {:?}",
"Job Declarator trying to setup connection to: {:?}",
proxy_address
);

SetupConnectionHandler::setup(&mut receiver, &mut sender, proxy_address)
.await
.unwrap();

info!("JD CONNECTED");
match SetupConnectionHandler::setup(&mut receiver, &mut sender, proxy_address).await {
Ok(_) => {
info!("Job Declarator connection setup success")
}
Err(e) => {
error!("Error setting Job Declarator connection: {:?}", e);
return Err(Error::ConnectionSetup);
}
};

let min_extranonce_size = config.min_extranonce2_size;

Expand Down
8 changes: 8 additions & 0 deletions roles/jd-client/src/lib/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,5 +133,13 @@ pub async fn handle_error(
send_status(sender, e, error_handling::ErrorBranch::Break).await
}
Error::Infallible(_) => send_status(sender, e, error_handling::ErrorBranch::Break).await,
// Errors from `network_helpers_sv2` crate.
Error::Network(_) => send_status(sender, e, error_handling::ErrorBranch::Break).await,
// Errors from prasing network address.
Error::NetworkAddressParse(_) => {
send_status(sender, e, error_handling::ErrorBranch::Break).await
}
// Errors from setting up connection.
Error::ConnectionSetup => send_status(sender, e, error_handling::ErrorBranch::Break).await,
}
}
16 changes: 11 additions & 5 deletions roles/jd-client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,13 @@ async fn initialize_jd(
upstream_config: lib::proxy_config::Upstream,
timeout: Duration,
) {
let proxy_config = process_cli_args().unwrap();
let proxy_config = match process_cli_args() {
Ok(p) => p,
Err(e) => {
error!("Failed to read config file: {}", e);
return;
}
};
let test_only_do_not_send_solution_to_tp = proxy_config
.test_only_do_not_send_solution_to_tp
.unwrap_or(false);
Expand All @@ -274,7 +280,7 @@ async fn initialize_jd(
let port = parts
.next()
.and_then(|p| p.parse::<u16>().ok())
.unwrap_or_else(|| panic!("Invalid pool address {}", upstream_config.pool_address));
.unwrap_or_else(|| panic!("Invalid pool address port {}", upstream_config.pool_address));
let upstream_addr = SocketAddr::new(
IpAddr::from_str(address)
.unwrap_or_else(|_| panic!("Invalid pool address {}", upstream_config.pool_address)),
Expand All @@ -300,14 +306,14 @@ async fn initialize_jd(
Ok(upstream) => upstream,
Err(e) => {
error!("Failed to create upstream: {}", e);
panic!()
return;
}
};

// Start receiving messages from the SV2 Upstream role
if let Err(e) = lib::upstream_sv2::Upstream::parse_incoming(upstream.clone()) {
error!("failed to create sv2 parser: {}", e);
panic!()
return;
}

match lib::upstream_sv2::Upstream::setup_connection(
Expand All @@ -320,7 +326,7 @@ async fn initialize_jd(
Ok(_) => info!("Connected to Upstream!"),
Err(e) => {
error!("Failed to connect to Upstream EXITING! : {}", e);
panic!()
return;
}
}

Expand Down

0 comments on commit bcbf6a0

Please sign in to comment.