Skip to content

Commit

Permalink
standardize roles CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
plebhash committed Mar 21, 2024
1 parent 230db35 commit 90572e9
Show file tree
Hide file tree
Showing 30 changed files with 648 additions and 577 deletions.
17 changes: 17 additions & 0 deletions roles/jd-client/src/args.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::path::PathBuf;
use tracing::error;
use crate::lib::error::{JdcError, JdcResult};
use crate::lib::jdc_config::JdcConfig;

#[derive(Debug)]
pub struct Args {
Expand Down Expand Up @@ -57,3 +60,17 @@ impl Args {
Ok(Self { config_path })
}
}

/// Process CLI args, if any.
#[allow(clippy::result_large_err)]
pub fn process_cli_args<'a>() -> JdcResult<'a, JdcConfig> {
let args = match Args::from_args() {
Ok(cfg) => cfg,
Err(help) => {
error!("{}", help);
return Err(JdcError::BadCliArgs);
}
};
let config_file = std::fs::read_to_string(args.config_path)?;
Ok(toml::from_str::<JdcConfig>(&config_file)?)
}
80 changes: 40 additions & 40 deletions roles/jd-client/src/lib/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::fmt;
use roles_logic_sv2::mining_sv2::{ExtendedExtranonce, NewExtendedMiningJob, SetCustomMiningJob};
use stratum_common::bitcoin::util::uint::ParseLengthError;

pub type ProxyResult<'a, T> = core::result::Result<T, Error<'a>>;
pub type JdcResult<'a, T> = core::result::Result<T, JdcError<'a>>;

#[derive(Debug)]
pub enum ChannelSendError<'a> {
Expand All @@ -26,7 +26,7 @@ pub enum ChannelSendError<'a> {
}

#[derive(Debug)]
pub enum Error<'a> {
pub enum JdcError<'a> {
VecToSlice32(Vec<u8>),
/// Errors on bad CLI argument input.
BadCliArgs,
Expand Down Expand Up @@ -58,9 +58,9 @@ pub enum Error<'a> {
Infallible(std::convert::Infallible),
}

impl<'a> fmt::Display for Error<'a> {
impl<'a> fmt::Display for JdcError<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use Error::*;
use JdcError::*;
match self {
BadCliArgs => write!(f, "Bad CLI arg input"),
BadTomlDeserialize(ref e) => write!(f, "Bad `toml` deserialize: `{:?}`", e),
Expand All @@ -83,57 +83,57 @@ impl<'a> fmt::Display for Error<'a> {
}
}

impl<'a> From<binary_sv2::Error> for Error<'a> {
impl<'a> From<binary_sv2::Error> for JdcError<'a> {
fn from(e: binary_sv2::Error) -> Self {
Error::BinarySv2(e)
JdcError::BinarySv2(e)
}
}

impl<'a> From<codec_sv2::noise_sv2::Error> for Error<'a> {
impl<'a> From<codec_sv2::noise_sv2::Error> for JdcError<'a> {
fn from(e: codec_sv2::noise_sv2::Error) -> Self {
Error::CodecNoise(e)
JdcError::CodecNoise(e)
}
}

impl<'a> From<framing_sv2::Error> for Error<'a> {
impl<'a> From<framing_sv2::Error> for JdcError<'a> {
fn from(e: framing_sv2::Error) -> Self {
Error::FramingSv2(e)
JdcError::FramingSv2(e)
}
}

impl<'a> From<std::io::Error> for Error<'a> {
impl<'a> From<std::io::Error> for JdcError<'a> {
fn from(e: std::io::Error) -> Self {
Error::Io(e)
JdcError::Io(e)
}
}

impl<'a> From<std::num::ParseIntError> for Error<'a> {
impl<'a> From<std::num::ParseIntError> for JdcError<'a> {
fn from(e: std::num::ParseIntError) -> Self {
Error::ParseInt(e)
JdcError::ParseInt(e)
}
}

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

impl<'a> From<toml::de::Error> for Error<'a> {
impl<'a> From<toml::de::Error> for JdcError<'a> {
fn from(e: toml::de::Error) -> Self {
Error::BadTomlDeserialize(e)
JdcError::BadTomlDeserialize(e)
}
}

impl<'a> From<async_channel::RecvError> for Error<'a> {
impl<'a> From<async_channel::RecvError> for JdcError<'a> {
fn from(e: async_channel::RecvError) -> Self {
Error::ChannelErrorReceiver(e)
JdcError::ChannelErrorReceiver(e)
}
}

impl<'a> From<tokio::sync::broadcast::error::RecvError> for Error<'a> {
impl<'a> From<tokio::sync::broadcast::error::RecvError> for JdcError<'a> {
fn from(e: tokio::sync::broadcast::error::RecvError) -> Self {
Error::TokioChannelErrorRecv(e)
JdcError::TokioChannelErrorRecv(e)
}
}

Expand All @@ -156,38 +156,38 @@ impl<'a> From<tokio::sync::broadcast::error::RecvError> for Error<'a> {

// *** CHANNEL SENDER ERRORS ***
impl<'a> From<async_channel::SendError<roles_logic_sv2::mining_sv2::SubmitSharesExtended<'a>>>
for Error<'a>
for JdcError<'a>
{
fn from(
e: async_channel::SendError<roles_logic_sv2::mining_sv2::SubmitSharesExtended<'a>>,
) -> Self {
Error::ChannelErrorSender(ChannelSendError::SubmitSharesExtended(e))
JdcError::ChannelErrorSender(ChannelSendError::SubmitSharesExtended(e))
}
}

impl<'a> From<async_channel::SendError<roles_logic_sv2::mining_sv2::SetNewPrevHash<'a>>>
for Error<'a>
for JdcError<'a>
{
fn from(e: async_channel::SendError<roles_logic_sv2::mining_sv2::SetNewPrevHash<'a>>) -> Self {
Error::ChannelErrorSender(ChannelSendError::SetNewPrevHash(e))
JdcError::ChannelErrorSender(ChannelSendError::SetNewPrevHash(e))
}
}

impl<'a> From<async_channel::SendError<(ExtendedExtranonce, u32)>> for Error<'a> {
impl<'a> From<async_channel::SendError<(ExtendedExtranonce, u32)>> for JdcError<'a> {
fn from(e: async_channel::SendError<(ExtendedExtranonce, u32)>) -> Self {
Error::ChannelErrorSender(ChannelSendError::Extranonce(e))
JdcError::ChannelErrorSender(ChannelSendError::Extranonce(e))
}
}

impl<'a> From<async_channel::SendError<NewExtendedMiningJob<'a>>> for Error<'a> {
impl<'a> From<async_channel::SendError<NewExtendedMiningJob<'a>>> for JdcError<'a> {
fn from(e: async_channel::SendError<NewExtendedMiningJob<'a>>) -> Self {
Error::ChannelErrorSender(ChannelSendError::NewExtendedMiningJob(e))
JdcError::ChannelErrorSender(ChannelSendError::NewExtendedMiningJob(e))
}
}

impl<'a> From<async_channel::SendError<SetCustomMiningJob<'a>>> for Error<'a> {
impl<'a> From<async_channel::SendError<SetCustomMiningJob<'a>>> for JdcError<'a> {
fn from(e: async_channel::SendError<SetCustomMiningJob<'a>>) -> Self {
Error::ChannelErrorSender(ChannelSendError::SetCustomMiningJob(e))
JdcError::ChannelErrorSender(ChannelSendError::SetCustomMiningJob(e))
}
}

Expand All @@ -197,32 +197,32 @@ impl<'a>
roles_logic_sv2::template_distribution_sv2::SetNewPrevHash<'a>,
Vec<u8>,
)>,
> for Error<'a>
> for JdcError<'a>
{
fn from(
e: async_channel::SendError<(
roles_logic_sv2::template_distribution_sv2::SetNewPrevHash<'a>,
Vec<u8>,
)>,
) -> Self {
Error::ChannelErrorSender(ChannelSendError::NewTemplate(e))
JdcError::ChannelErrorSender(ChannelSendError::NewTemplate(e))
}
}

impl<'a> From<Vec<u8>> for Error<'a> {
impl<'a> From<Vec<u8>> for JdcError<'a> {
fn from(e: Vec<u8>) -> Self {
Error::VecToSlice32(e)
JdcError::VecToSlice32(e)
}
}

impl<'a> From<ParseLengthError> for Error<'a> {
impl<'a> From<ParseLengthError> for JdcError<'a> {
fn from(e: ParseLengthError) -> Self {
Error::Uint256Conversion(e)
JdcError::Uint256Conversion(e)
}
}

impl<'a> From<std::convert::Infallible> for Error<'a> {
impl<'a> From<std::convert::Infallible> for JdcError<'a> {
fn from(e: std::convert::Infallible) -> Self {
Error::Infallible(e)
JdcError::Infallible(e)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl TryFrom<&CoinbaseOutput> for CoinbaseOutput_ {
}

#[derive(Debug, Deserialize, Clone)]
pub struct ProxyConfig {
pub struct JdcConfig {
pub downstream_address: String,
pub downstream_port: u16,
pub max_supported_version: u16,
Expand Down Expand Up @@ -82,7 +82,7 @@ where
}
}

pub fn get_coinbase_output(config: &ProxyConfig) -> Result<Vec<TxOut>, Error> {
pub fn get_coinbase_output(config: &JdcConfig) -> Result<Vec<TxOut>, Error> {
let mut result = Vec::new();
for coinbase_output_pool in &config.coinbase_outputs {
let coinbase_output: CoinbaseOutput_ = coinbase_output_pool.try_into()?;
Expand Down
6 changes: 3 additions & 3 deletions roles/jd-client/src/lib/job_declarator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub type StdFrame = StandardSv2Frame<Message>;
mod setup_connection;
use setup_connection::SetupConnectionHandler;

use super::{error::Error, proxy_config::ProxyConfig, upstream_sv2::Upstream};
use super::{error::JdcError, jdc_config::JdcConfig, upstream_sv2::Upstream};

#[derive(Debug, Clone)]
pub struct LastDeclareJob {
Expand Down Expand Up @@ -79,10 +79,10 @@ impl JobDeclarator {
pub async fn new(
address: SocketAddr,
authority_public_key: [u8; 32],
config: ProxyConfig,
config: JdcConfig,
up: Arc<Mutex<Upstream>>,
task_collector: Arc<Mutex<Vec<AbortHandle>>>,
) -> Result<Arc<Mutex<Self>>, Error<'static>> {
) -> Result<Arc<Mutex<Self>>, JdcError<'static>> {
let stream = tokio::net::TcpStream::connect(address).await?;
let initiator = Initiator::from_raw_k(authority_public_key)?;
let (mut receiver, mut sender, _, _) =
Expand Down
2 changes: 1 addition & 1 deletion roles/jd-client/src/lib/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pub mod downstream;
pub mod error;
pub mod job_declarator;
pub mod proxy_config;
pub mod jdc_config;
pub mod status;
pub mod template_receiver;
pub mod upstream_sv2;
Expand Down
44 changes: 22 additions & 22 deletions roles/jd-client/src/lib/status.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::error::{self, Error};
use super::error::{self, JdcError};

#[derive(Debug)]
pub enum Sender {
Expand Down Expand Up @@ -35,8 +35,8 @@ impl Clone for Sender {

#[derive(Debug)]
pub enum State<'a> {
DownstreamShutdown(Error<'a>),
UpstreamShutdown(Error<'a>),
DownstreamShutdown(JdcError<'a>),
UpstreamShutdown(JdcError<'a>),
UpstreamRogue,
Healthy(String),
}
Expand All @@ -48,7 +48,7 @@ pub struct Status<'a> {

async fn send_status(
sender: &Sender,
e: error::Error<'static>,
e: error::JdcError<'static>,
outcome: error_handling::ErrorBranch,
) -> error_handling::ErrorBranch {
match sender {
Expand Down Expand Up @@ -87,51 +87,51 @@ async fn send_status(
// this is called by `error_handling::handle_result!`
pub async fn handle_error(
sender: &Sender,
e: error::Error<'static>,
e: error::JdcError<'static>,
) -> error_handling::ErrorBranch {
tracing::error!("Error: {:?}", &e);
match e {
Error::VecToSlice32(_) => send_status(sender, e, error_handling::ErrorBranch::Break).await,
JdcError::VecToSlice32(_) => send_status(sender, e, error_handling::ErrorBranch::Break).await,
// Errors on bad CLI argument input.
Error::BadCliArgs => send_status(sender, e, error_handling::ErrorBranch::Break).await,
JdcError::BadCliArgs => send_status(sender, e, error_handling::ErrorBranch::Break).await,
// Errors on bad `toml` deserialize.
Error::BadTomlDeserialize(_) => {
JdcError::BadTomlDeserialize(_) => {
send_status(sender, e, error_handling::ErrorBranch::Break).await
}
// Errors from `binary_sv2` crate.
Error::BinarySv2(_) => send_status(sender, e, error_handling::ErrorBranch::Break).await,
JdcError::BinarySv2(_) => send_status(sender, e, error_handling::ErrorBranch::Break).await,
// Errors on bad noise handshake.
Error::CodecNoise(_) => send_status(sender, e, error_handling::ErrorBranch::Break).await,
JdcError::CodecNoise(_) => send_status(sender, e, error_handling::ErrorBranch::Break).await,
// Errors from `framing_sv2` crate.
Error::FramingSv2(_) => send_status(sender, e, error_handling::ErrorBranch::Break).await,
JdcError::FramingSv2(_) => send_status(sender, e, error_handling::ErrorBranch::Break).await,
// Errors on bad `TcpStream` connection.
Error::Io(_) => send_status(sender, e, error_handling::ErrorBranch::Break).await,
JdcError::Io(_) => send_status(sender, e, error_handling::ErrorBranch::Break).await,
// Errors on bad `String` to `int` conversion.
Error::ParseInt(_) => send_status(sender, e, error_handling::ErrorBranch::Break).await,
JdcError::ParseInt(_) => send_status(sender, e, error_handling::ErrorBranch::Break).await,
// Errors from `roles_logic_sv2` crate.
Error::RolesSv2Logic(_) => send_status(sender, e, error_handling::ErrorBranch::Break).await,
Error::UpstreamIncoming(_) => {
JdcError::RolesSv2Logic(_) => send_status(sender, e, error_handling::ErrorBranch::Break).await,
JdcError::UpstreamIncoming(_) => {
send_status(sender, e, error_handling::ErrorBranch::Break).await
}
Error::SubprotocolMining(_) => {
JdcError::SubprotocolMining(_) => {
send_status(sender, e, error_handling::ErrorBranch::Break).await
}
// Locking Errors
Error::PoisonLock => send_status(sender, e, error_handling::ErrorBranch::Break).await,
JdcError::PoisonLock => send_status(sender, e, error_handling::ErrorBranch::Break).await,
// Channel Receiver Error
Error::ChannelErrorReceiver(_) => {
JdcError::ChannelErrorReceiver(_) => {
send_status(sender, e, error_handling::ErrorBranch::Break).await
}
Error::TokioChannelErrorRecv(_) => {
JdcError::TokioChannelErrorRecv(_) => {
send_status(sender, e, error_handling::ErrorBranch::Break).await
}
// Channel Sender Errors
Error::ChannelErrorSender(_) => {
JdcError::ChannelErrorSender(_) => {
send_status(sender, e, error_handling::ErrorBranch::Break).await
}
Error::Uint256Conversion(_) => {
JdcError::Uint256Conversion(_) => {
send_status(sender, e, error_handling::ErrorBranch::Break).await
}
Error::Infallible(_) => send_status(sender, e, error_handling::ErrorBranch::Break).await,
JdcError::Infallible(_) => send_status(sender, e, error_handling::ErrorBranch::Break).await,
}
}
Loading

0 comments on commit 90572e9

Please sign in to comment.