From 49bca60de2bf6bb81bae527d0cea77f28f043705 Mon Sep 17 00:00:00 2001 From: jbesraa Date: Tue, 3 Sep 2024 12:39:08 +0300 Subject: [PATCH] Add error handling to `PoolSv2::start` 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. --- roles/pool/src/lib/mod.rs | 31 +++++++++++-------------------- roles/pool/src/main.rs | 2 +- 2 files changed, 12 insertions(+), 21 deletions(-) diff --git a/roles/pool/src/lib/mod.rs b/roles/pool/src/lib/mod.rs index d7d483a5c1..2603695f88 100644 --- a/roles/pool/src/lib/mod.rs +++ b/roles/pool/src/lib/mod.rs @@ -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, } @@ -19,7 +21,8 @@ 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); @@ -27,15 +30,9 @@ impl PoolSv2 { 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, @@ -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, @@ -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(); @@ -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); @@ -103,7 +94,7 @@ impl PoolSv2 { .safe_lock(|p| p.remove_downstream(downstream_id)) .is_err() { - break; + break Ok(()); } } } diff --git a/roles/pool/src/main.rs b/roles/pool/src/main.rs index 5129684396..34f5983e59 100644 --- a/roles/pool/src/main.rs +++ b/roles/pool/src/main.rs @@ -99,5 +99,5 @@ async fn main() { return; } }; - PoolSv2::new(config).start().await; + let _ = PoolSv2::new(config).start().await; }