Skip to content

Commit

Permalink
Add error handling to PoolSv2::start
Browse files Browse the repository at this point in the history
Handle errors in `start` function for better user experience and to be
able to catch errors in test environment, for example without
introducing error handling, we do not get a proper response if the
provided `coinbase_output` in the config is valid.
  • Loading branch information
jbesraa committed Sep 26, 2024
1 parent 87d6c30 commit 49bca60
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 21 deletions.
31 changes: 11 additions & 20 deletions roles/pool/src/lib/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ pub mod template_receiver;

use async_channel::{bounded, unbounded};

use error::PoolError;
use mining_pool::{get_coinbase_output, Configuration, Pool};
use template_receiver::TemplateRx;
use tracing::{error, info, warn};

use tokio::select;

#[derive(Debug, Clone)]
pub struct PoolSv2 {
config: Configuration,
}
Expand All @@ -19,23 +21,18 @@ impl PoolSv2 {
pub fn new(config: Configuration) -> PoolSv2 {
PoolSv2 { config }
}
pub async fn start(self) {

pub async fn start(&self) -> Result<(), PoolError> {
let config = self.config.clone();
let (status_tx, status_rx) = unbounded();
let (s_new_t, r_new_t) = bounded(10);
let (s_prev_hash, r_prev_hash) = bounded(10);
let (s_solution, r_solution) = bounded(10);
let (s_message_recv_signal, r_message_recv_signal) = bounded(10);
let coinbase_output_result = get_coinbase_output(&config);
let coinbase_output_len = match coinbase_output_result {
Ok(coinbase_output) => coinbase_output.len() as u32,
Err(err) => {
error!("Failed to get Coinbase output: {:?}", err);
return;
}
};
let coinbase_output_len = coinbase_output_result?.len() as u32;
let tp_authority_public_key = config.tp_authority_public_key;
let template_rx_res = TemplateRx::connect(
TemplateRx::connect(
config.tp_address.parse().unwrap(),
s_new_t,
s_prev_hash,
Expand All @@ -45,13 +42,7 @@ impl PoolSv2 {
coinbase_output_len,
tp_authority_public_key,
)
.await;

if let Err(e) = template_rx_res {
error!("Could not connect to Template Provider: {}", e);
return;
}

.await?;
let pool = Pool::start(
config.clone(),
r_new_t,
Expand All @@ -76,7 +67,7 @@ impl PoolSv2 {
// we also shut down in case of error
},
}
break;
break Ok(());
}
};
let task_status: status::Status = task_status.unwrap();
Expand All @@ -88,11 +79,11 @@ impl PoolSv2 {
"SHUTDOWN from Downstream: {}\nTry to restart the downstream listener",
err
);
break;
break Ok(());
}
status::State::TemplateProviderShutdown(err) => {
error!("SHUTDOWN from Upstream: {}\nTry to reconnecting or connecting to a new upstream", err);
break;
break Ok(());
}
status::State::Healthy(msg) => {
info!("HEALTHY message: {}", msg);
Expand All @@ -103,7 +94,7 @@ impl PoolSv2 {
.safe_lock(|p| p.remove_downstream(downstream_id))
.is_err()
{
break;
break Ok(());
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion roles/pool/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,5 @@ async fn main() {
return;
}
};
PoolSv2::new(config).start().await;
let _ = PoolSv2::new(config).start().await;
}

0 comments on commit 49bca60

Please sign in to comment.