Skip to content

Commit

Permalink
Standard roles_logic_sv2 err handling
Browse files Browse the repository at this point in the history
  • Loading branch information
rrybarczyk committed Jun 22, 2024
1 parent 7f76ec4 commit 1c7a152
Show file tree
Hide file tree
Showing 21 changed files with 118 additions and 102 deletions.
30 changes: 15 additions & 15 deletions roles/jd-client/src/lib/downstream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use roles_logic_sv2::{
channel_logic::channel_factory::{OnNewShare, PoolChannelFactory, Share},
common_messages_sv2::{SetupConnection, SetupConnectionSuccess},
common_properties::{CommonDownstreamData, IsDownstream, IsMiningDownstream},
errors::Error,
handlers::{
common::{ParseDownstreamCommonMessages, SendTo as SendToCommon},
mining::{ParseDownstreamMiningMessages, SendTo, SupportedChannelTypes},
Expand All @@ -17,6 +16,7 @@ use roles_logic_sv2::{
parsers::{Mining, MiningDeviceMessages, PoolMessages},
template_distribution_sv2::{NewTemplate, SubmitSolution},
utils::Mutex,
Error as RolesLogicSv2Error,
};
use tracing::{debug, error, info, warn};

Expand Down Expand Up @@ -210,7 +210,7 @@ impl DownstreamMiningNode {
Self::next(self_mutex, incoming).await;
}
let tx_status = self_mutex.safe_lock(|s| s.tx_status.clone()).unwrap();
let err = Error::DownstreamDown;
let err = RolesLogicSv2Error::DownstreamDown;
let status = status::Status {
state: status::State::DownstreamShutdown(err.into()),
};
Expand Down Expand Up @@ -268,7 +268,7 @@ impl DownstreamMiningNode {
#[async_recursion::async_recursion]
async fn match_send_to(
self_mutex: Arc<Mutex<Self>>,
next_message_to_send: Result<SendTo<UpstreamMiningNode>, Error>,
next_message_to_send: Result<SendTo<UpstreamMiningNode>, RolesLogicSv2Error>,
incoming: Option<StdFrame>,
) {
match next_message_to_send {
Expand Down Expand Up @@ -354,7 +354,7 @@ impl DownstreamMiningNode {
self_mutex: &Arc<Mutex<Self>>,
mut new_template: NewTemplate<'static>,
pool_output: &[u8],
) -> Result<(), Error> {
) -> Result<(), RolesLogicSv2Error> {
if !self_mutex.safe_lock(|s| s.status.have_channel()).unwrap() {
IS_NEW_TEMPLATE_HANDLED.store(true, std::sync::atomic::Ordering::Release);
return Ok(());
Expand Down Expand Up @@ -398,7 +398,7 @@ impl DownstreamMiningNode {
pub async fn on_set_new_prev_hash(
self_mutex: &Arc<Mutex<Self>>,
new_prev_hash: roles_logic_sv2::template_distribution_sv2::SetNewPrevHash<'static>,
) -> Result<(), Error> {
) -> Result<(), RolesLogicSv2Error> {
if !self_mutex.safe_lock(|s| s.status.have_channel()).unwrap() {
return Ok(());
}
Expand Down Expand Up @@ -451,15 +451,15 @@ impl
&mut self,
_: OpenStandardMiningChannel,
_: Option<Arc<Mutex<UpstreamMiningNode>>>,
) -> Result<SendTo<UpstreamMiningNode>, Error> {
) -> Result<SendTo<UpstreamMiningNode>, RolesLogicSv2Error> {
warn!("Ignoring OpenStandardMiningChannel");
Ok(SendTo::None(None))
}

fn handle_open_extended_mining_channel(
&mut self,
m: OpenExtendedMiningChannel,
) -> Result<SendTo<UpstreamMiningNode>, Error> {
) -> Result<SendTo<UpstreamMiningNode>, RolesLogicSv2Error> {
if !self.status.is_solo_miner() {
// Safe unwrap alreay checked if it cointains upstream with is_solo_miner
Ok(SendTo::RelaySameMessageToRemote(
Expand Down Expand Up @@ -506,15 +506,15 @@ impl
let messages = messages.into_iter().map(SendTo::Respond).collect();
Ok(SendTo::Multiple(messages))
}
Err(_) => Err(roles_logic_sv2::Error::ChannelIsNeitherExtendedNeitherInAPool),
Err(_) => Err(RolesLogicSv2Error::ChannelIsNeitherExtendedNeitherInAPool),
}
}
}

fn handle_update_channel(
&mut self,
_: UpdateChannel,
) -> Result<SendTo<UpstreamMiningNode>, Error> {
) -> Result<SendTo<UpstreamMiningNode>, RolesLogicSv2Error> {
if !self.status.is_solo_miner() {
// Safe unwrap alreay checked if it cointains upstream with is_solo_miner
Ok(SendTo::RelaySameMessageToRemote(
Expand All @@ -528,15 +528,15 @@ impl
fn handle_submit_shares_standard(
&mut self,
_: SubmitSharesStandard,
) -> Result<SendTo<UpstreamMiningNode>, Error> {
) -> Result<SendTo<UpstreamMiningNode>, RolesLogicSv2Error> {
warn!("Ignoring SubmitSharesStandard");
Ok(SendTo::None(None))
}

fn handle_submit_shares_extended(
&mut self,
m: SubmitSharesExtended,
) -> Result<SendTo<UpstreamMiningNode>, Error> {
) -> Result<SendTo<UpstreamMiningNode>, RolesLogicSv2Error> {
match self
.status
.get_channel()
Expand Down Expand Up @@ -617,7 +617,7 @@ impl
fn handle_set_custom_mining_job(
&mut self,
_: SetCustomMiningJob,
) -> Result<SendTo<UpstreamMiningNode>, Error> {
) -> Result<SendTo<UpstreamMiningNode>, RolesLogicSv2Error> {
warn!("Ignoring SetCustomMiningJob");
Ok(SendTo::None(None))
}
Expand All @@ -629,8 +629,8 @@ impl ParseDownstreamCommonMessages<roles_logic_sv2::routing_logic::NoRouting>
fn handle_setup_connection(
&mut self,
_: SetupConnection,
_: Option<Result<(CommonDownstreamData, SetupConnectionSuccess), Error>>,
) -> Result<roles_logic_sv2::handlers::common::SendTo, Error> {
_: Option<Result<(CommonDownstreamData, SetupConnectionSuccess), RolesLogicSv2Error>>,
) -> Result<roles_logic_sv2::handlers::common::SendTo, RolesLogicSv2Error> {
let response = SetupConnectionSuccess {
used_version: 2,
// require extended channels
Expand Down Expand Up @@ -668,7 +668,7 @@ pub async fn listen_for_downstream_mining(
tx_status: status::Sender,
miner_coinbase_output: Vec<TxOut>,
jd: Option<Arc<Mutex<JobDeclarator>>>,
) -> Result<Arc<Mutex<DownstreamMiningNode>>, Error> {
) -> Result<Arc<Mutex<DownstreamMiningNode>>, RolesLogicSv2Error> {
info!("Listening for downstream mining connections on {}", address);
let listner = TcpListener::bind(address).await.unwrap();

Expand Down
12 changes: 6 additions & 6 deletions roles/jd-client/src/lib/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ pub enum JdcError<'a> {
/// Errors on bad `String` to `int` conversion.
ParseInt(std::num::ParseIntError),
/// Errors from `roles_logic_sv2` crate.
RolesSv2Logic(roles_logic_sv2::errors::Error),
UpstreamIncoming(roles_logic_sv2::errors::Error),
RolesLogicSv2(roles_logic_sv2::Error),
UpstreamIncoming(roles_logic_sv2::Error),
#[allow(dead_code)]
SubprotocolMining(String),
// Locking Errors
Expand All @@ -65,7 +65,7 @@ impl<'a> fmt::Display for JdcError<'a> {
FramingSv2(ref e) => write!(f, "Framing SV2 error: `{:?}`", e),
Io(ref e) => write!(f, "I/O error: `{:?}", e),
ParseInt(ref e) => write!(f, "Bad convert from `String` to `int`: `{:?}`", e),
RolesSv2Logic(ref e) => write!(f, "Roles SV2 Logic Error: `{:?}`", e),
RolesLogicSv2(ref e) => write!(f, "Roles SV2 Logic Error: `{:?}`", e),
SubprotocolMining(ref e) => write!(f, "Subprotocol Mining Error: `{:?}`", e),
UpstreamIncoming(ref e) => write!(f, "Upstream parse incoming error: `{:?}`", e),
PoisonLock => write!(f, "Poison Lock error"),
Expand Down Expand Up @@ -115,9 +115,9 @@ impl<'a> From<std::num::ParseIntError> for JdcError<'a> {
}
}

impl<'a> From<roles_logic_sv2::errors::Error> for JdcError<'a> {
fn from(e: roles_logic_sv2::errors::Error) -> Self {
JdcError::RolesSv2Logic(e)
impl<'a> From<roles_logic_sv2::Error> for JdcError<'a> {
fn from(e: roles_logic_sv2::Error) -> Self {
JdcError::RolesLogicSv2(e)
}
}

Expand Down
12 changes: 6 additions & 6 deletions roles/jd-client/src/lib/job_declarator/message_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ use roles_logic_sv2::{
ProvideMissingTransactionsSuccess,
},
parsers::JobDeclaration,
Error as RolesLogicSv2Error,
};
pub type SendTo = SendTo_<JobDeclaration<'static>, ()>;
use roles_logic_sv2::errors::Error;

impl ParseServerJobDeclarationMessages for JobDeclarator {
fn handle_allocate_mining_job_token_success(
&mut self,
message: AllocateMiningJobTokenSuccess,
) -> Result<SendTo, Error> {
) -> Result<SendTo, RolesLogicSv2Error> {
self.allocated_tokens.push(message.into_static());

Ok(SendTo::None(None))
Expand All @@ -24,22 +24,22 @@ impl ParseServerJobDeclarationMessages for JobDeclarator {
fn handle_declare_mining_job_success(
&mut self,
message: DeclareMiningJobSuccess,
) -> Result<SendTo, Error> {
) -> Result<SendTo, RolesLogicSv2Error> {
let message = JobDeclaration::DeclareMiningJobSuccess(message.into_static());
Ok(SendTo::None(Some(message)))
}

fn handle_declare_mining_job_error(
&mut self,
_message: DeclareMiningJobError,
) -> Result<SendTo, Error> {
) -> Result<SendTo, RolesLogicSv2Error> {
Ok(SendTo::None(None))
}

fn handle_identify_transactions(
&mut self,
message: IdentifyTransactions,
) -> Result<SendTo, Error> {
) -> Result<SendTo, RolesLogicSv2Error> {
let message_identify_transactions = IdentifyTransactionsSuccess {
request_id: message.request_id,
tx_data_hashes: Vec::new().into(),
Expand All @@ -52,7 +52,7 @@ impl ParseServerJobDeclarationMessages for JobDeclarator {
fn handle_provide_missing_transactions(
&mut self,
message: ProvideMissingTransactions,
) -> Result<SendTo, Error> {
) -> Result<SendTo, RolesLogicSv2Error> {
let tx_list = self
.last_declare_mining_jobs_sent
.get(&message.request_id)
Expand Down
7 changes: 4 additions & 3 deletions roles/jd-client/src/lib/job_declarator/setup_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use roles_logic_sv2::{
parsers::PoolMessages,
routing_logic::{CommonRoutingLogic, NoRouting},
utils::Mutex,
Error as RolesLogicSv2Error,
};
use std::{convert::TryInto, net::SocketAddr, sync::Arc};
pub type Message = PoolMessages<'static>;
Expand Down Expand Up @@ -74,21 +75,21 @@ impl ParseUpstreamCommonMessages<NoRouting> for SetupConnectionHandler {
fn handle_setup_connection_success(
&mut self,
_: roles_logic_sv2::common_messages_sv2::SetupConnectionSuccess,
) -> Result<roles_logic_sv2::handlers::common::SendTo, roles_logic_sv2::errors::Error> {
) -> Result<roles_logic_sv2::handlers::common::SendTo, RolesLogicSv2Error> {
Ok(SendTo::None(None))
}

fn handle_setup_connection_error(
&mut self,
_: roles_logic_sv2::common_messages_sv2::SetupConnectionError,
) -> Result<roles_logic_sv2::handlers::common::SendTo, roles_logic_sv2::errors::Error> {
) -> Result<roles_logic_sv2::handlers::common::SendTo, RolesLogicSv2Error> {
todo!()
}

fn handle_channel_endpoint_changed(
&mut self,
_: roles_logic_sv2::common_messages_sv2::ChannelEndpointChanged,
) -> Result<roles_logic_sv2::handlers::common::SendTo, roles_logic_sv2::errors::Error> {
) -> Result<roles_logic_sv2::handlers::common::SendTo, RolesLogicSv2Error> {
todo!()
}
}
2 changes: 1 addition & 1 deletion roles/jd-client/src/lib/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ pub async fn handle_error(sender: &Sender, e: JdcError<'static>) -> error_handli
// Errors on bad `String` to `int` conversion.
JdcError::ParseInt(_) => send_status(sender, e, error_handling::ErrorBranch::Break).await,
// Errors from `roles_logic_sv2` crate.
JdcError::RolesSv2Logic(_) => {
JdcError::RolesLogicSv2(_) => {
send_status(sender, e, error_handling::ErrorBranch::Break).await
}
JdcError::UpstreamIncoming(_) => {
Expand Down
7 changes: 4 additions & 3 deletions roles/jd-client/src/lib/template_receiver/setup_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use roles_logic_sv2::{
parsers::PoolMessages,
routing_logic::{CommonRoutingLogic, NoRouting},
utils::Mutex,
Error as RolesLogicSv2Error,
};
use std::{convert::TryInto, net::SocketAddr, sync::Arc};
pub type Message = PoolMessages<'static>;
Expand Down Expand Up @@ -70,21 +71,21 @@ impl ParseUpstreamCommonMessages<NoRouting> for SetupConnectionHandler {
fn handle_setup_connection_success(
&mut self,
_: roles_logic_sv2::common_messages_sv2::SetupConnectionSuccess,
) -> Result<roles_logic_sv2::handlers::common::SendTo, roles_logic_sv2::errors::Error> {
) -> Result<roles_logic_sv2::handlers::common::SendTo, RolesLogicSv2Error> {
Ok(SendTo::None(None))
}

fn handle_setup_connection_error(
&mut self,
_: roles_logic_sv2::common_messages_sv2::SetupConnectionError,
) -> Result<roles_logic_sv2::handlers::common::SendTo, roles_logic_sv2::errors::Error> {
) -> Result<roles_logic_sv2::handlers::common::SendTo, RolesLogicSv2Error> {
todo!()
}

fn handle_channel_endpoint_changed(
&mut self,
_: roles_logic_sv2::common_messages_sv2::ChannelEndpointChanged,
) -> Result<roles_logic_sv2::handlers::common::SendTo, roles_logic_sv2::errors::Error> {
) -> Result<roles_logic_sv2::handlers::common::SendTo, RolesLogicSv2Error> {
todo!()
}
}
6 changes: 3 additions & 3 deletions roles/jd-server/src/lib/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub enum JdsError {
BinarySv2(binary_sv2::Error),
Codec(codec_sv2::Error),
Noise(noise_sv2::Error),
RolesLogic(roles_logic_sv2::Error),
RolesLogicSv2(roles_logic_sv2::Error),
Framing(codec_sv2::framing_sv2::Error),
PoisonLock(String),
Custom(String),
Expand All @@ -39,7 +39,7 @@ impl std::fmt::Display for JdsError {
Codec(ref e) => write!(f, "Codec SV2 error: `{:?}", e),
Framing(ref e) => write!(f, "Framing SV2 error: `{:?}`", e),
Noise(ref e) => write!(f, "Noise SV2 error: `{:?}", e),
RolesLogic(ref e) => write!(f, "Roles Logic SV2 error: `{:?}`", e),
RolesLogicSv2(ref e) => write!(f, "Roles Logic SV2 error: `{:?}`", e),
PoisonLock(ref e) => write!(f, "Poison lock: {:?}", e),
Custom(ref e) => write!(f, "Custom SV2 error: `{:?}`", e),
Sv2ProtocolError(ref e) => {
Expand Down Expand Up @@ -92,7 +92,7 @@ impl From<noise_sv2::Error> for JdsError {

impl From<roles_logic_sv2::Error> for JdsError {
fn from(e: roles_logic_sv2::Error) -> JdsError {
JdsError::RolesLogic(e)
JdsError::RolesLogicSv2(e)
}
}

Expand Down
4 changes: 3 additions & 1 deletion roles/jd-server/src/lib/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ pub async fn handle_error(sender: &Sender, e: JdsError) -> error_handling::Error
JdsError::BinarySv2(_) => send_status(sender, e, error_handling::ErrorBranch::Break).await,
JdsError::Codec(_) => send_status(sender, e, error_handling::ErrorBranch::Break).await,
JdsError::Noise(_) => send_status(sender, e, error_handling::ErrorBranch::Continue).await,
JdsError::RolesLogic(_) => send_status(sender, e, error_handling::ErrorBranch::Break).await,
JdsError::RolesLogicSv2(_) => {
send_status(sender, e, error_handling::ErrorBranch::Break).await
}
JdsError::Custom(_) => send_status(sender, e, error_handling::ErrorBranch::Break).await,
JdsError::Framing(_) => send_status(sender, e, error_handling::ErrorBranch::Break).await,
JdsError::PoisonLock(_) => send_status(sender, e, error_handling::ErrorBranch::Break).await,
Expand Down
6 changes: 3 additions & 3 deletions roles/pool/src/lib/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub enum PoolError {
BinarySv2(binary_sv2::Error),
Codec(codec_sv2::Error),
Noise(noise_sv2::Error),
RolesLogic(roles_logic_sv2::Error),
RolesLogicSv2(roles_logic_sv2::Error),
Framing(codec_sv2::framing_sv2::Error),
PoisonLock(String),
ComponentShutdown(String),
Expand All @@ -41,7 +41,7 @@ impl std::fmt::Display for PoolError {
Codec(ref e) => write!(f, "Codec SV2 error: `{:?}", e),
Framing(ref e) => write!(f, "Framing SV2 error: `{:?}`", e),
Noise(ref e) => write!(f, "Noise SV2 error: `{:?}", e),
RolesLogic(ref e) => write!(f, "Roles Logic SV2 error: `{:?}`", e),
RolesLogicSv2(ref e) => write!(f, "Roles Logic SV2 error: `{:?}`", e),
PoisonLock(ref e) => write!(f, "Poison lock: {:?}", e),
ComponentShutdown(ref e) => write!(f, "Component shutdown: {:?}", e),
Custom(ref e) => write!(f, "Custom SV2 error: `{:?}`", e),
Expand Down Expand Up @@ -86,7 +86,7 @@ impl From<noise_sv2::Error> for PoolError {

impl From<roles_logic_sv2::Error> for PoolError {
fn from(e: roles_logic_sv2::Error) -> PoolError {
PoolError::RolesLogic(e)
PoolError::RolesLogicSv2(e)
}
}

Expand Down
Loading

0 comments on commit 1c7a152

Please sign in to comment.