Skip to content

Commit

Permalink
Merge pull request stratum-mining#1087 from Shourya742/remove-toml-de…
Browse files Browse the repository at this point in the history
…pendency

Remove toml dependency
  • Loading branch information
plebhash authored Aug 9, 2024
2 parents c1d4cf3 + 4d7a93b commit 9bb8f15
Show file tree
Hide file tree
Showing 17 changed files with 136 additions and 75 deletions.
2 changes: 1 addition & 1 deletion roles/jd-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ roles_logic_sv2 = { version = "^1.0.0", path = "../../protocols/v2/roles-logic-s
serde = { version = "1.0.89", default-features = false, features = ["derive", "alloc"] }
futures = "0.3.25"
tokio = { version = "1", features = ["full"] }
toml = { version = "0.5.6", git = "https://github.com/diondokter/toml-rs", default-features = false, rev = "c4161aa" }
ext-config = { version = "0.14.0", features = ["toml"], package = "config" }
tracing = { version = "0.1" }
tracing-subscriber = { version = "0.3" }
error_handling = { version = "1.0.0", path = "../../utils/error-handling" }
Expand Down
19 changes: 7 additions & 12 deletions roles/jd-client/src/lib/error.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use ext_config::ConfigError;
use std::fmt;

use roles_logic_sv2::mining_sv2::{ExtendedExtranonce, NewExtendedMiningJob, SetCustomMiningJob};
Expand Down Expand Up @@ -30,8 +31,8 @@ pub enum Error<'a> {
VecToSlice32(Vec<u8>),
/// Errors on bad CLI argument input.
BadCliArgs,
/// Errors on bad `toml` deserialize.
BadTomlDeserialize(toml::de::Error),
/// Errors on bad `config` TOML deserialize.
BadConfigDeserialize(ConfigError),
/// Errors from `binary_sv2` crate.
BinarySv2(binary_sv2::Error),
/// Errors on bad noise handshake.
Expand Down Expand Up @@ -63,7 +64,7 @@ impl<'a> fmt::Display for Error<'a> {
use Error::*;
match self {
BadCliArgs => write!(f, "Bad CLI arg input"),
BadTomlDeserialize(ref e) => write!(f, "Bad `toml` deserialize: `{:?}`", e),
BadConfigDeserialize(ref e) => write!(f, "Bad `config` TOML deserialize: `{:?}`", e),
BinarySv2(ref e) => write!(f, "Binary SV2 error: `{:?}`", e),
CodecNoise(ref e) => write!(f, "Noise error: `{:?}", e),
FramingSv2(ref e) => write!(f, "Framing SV2 error: `{:?}`", e),
Expand Down Expand Up @@ -119,9 +120,9 @@ impl<'a> From<roles_logic_sv2::errors::Error> for Error<'a> {
}
}

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

Expand Down Expand Up @@ -209,12 +210,6 @@ impl<'a>
}
}

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

impl<'a> From<ParseLengthError> for Error<'a> {
fn from(e: ParseLengthError) -> Self {
Error::Uint256Conversion(e)
Expand Down
6 changes: 3 additions & 3 deletions roles/jd-client/src/lib/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ async fn send_status(
outcome
}

// this is called by `error_handling::handle_result!`
// This is called by `error_handling::handle_result!`
pub async fn handle_error(
sender: &Sender,
e: error::Error<'static>,
Expand All @@ -94,8 +94,8 @@ pub async fn handle_error(
Error::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,
// Errors on bad `toml` deserialize.
Error::BadTomlDeserialize(_) => {
// Errors on bad `config` TOML deserialize.
Error::BadConfigDeserialize(_) => {
send_status(sender, e, error_handling::ErrorBranch::Break).await
}
// Errors from `binary_sv2` crate.
Expand Down
31 changes: 21 additions & 10 deletions roles/jd-client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,31 @@ use lib::{
};

use args::Args;
use ext_config::{Config, File, FileFormat};
use tracing::error;

/// Process CLI args, if any.
/// Process CLI args and load configuration.
#[allow(clippy::result_large_err)]
fn process_cli_args<'a>() -> ProxyResult<'a, ProxyConfig> {
let args = match Args::from_args() {
Ok(cfg) => cfg,
Err(help) => {
error!("{}", help);
return Err(Error::BadCliArgs);
}
};
let config_file = std::fs::read_to_string(args.config_path)?;
Ok(toml::from_str::<ProxyConfig>(&config_file)?)
// Parse CLI arguments
let args = Args::from_args().map_err(|help| {
error!("{}", help);
Error::BadCliArgs
})?;

// Build configuration from the provided file path
let config_path = args.config_path.to_str().ok_or_else(|| {
error!("Invalid configuration path.");
Error::BadCliArgs
})?;

let settings = Config::builder()
.add_source(File::new(config_path, FileFormat::Toml))
.build()?;

// Deserialize settings into ProxyConfig
let config = settings.try_deserialize::<ProxyConfig>()?;
Ok(config)
}

/// TODO on the setup phase JDC must send a random nonce to bitcoind and JDS used for the tx
Expand Down
4 changes: 2 additions & 2 deletions roles/jd-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ noise_sv2 = { version = "1.1.0", path = "../../protocols/v2/noise-sv2" }
rand = "0.8.4"
roles_logic_sv2 = { version = "^1.0.0", path = "../../protocols/v2/roles-logic-sv2" }
tokio = { version = "1", features = ["full"] }
toml = { version = "0.5.6", git = "https://github.com/diondokter/toml-rs", default-features = false, rev = "c4161aa" }
ext-config = { version = "0.14.0", features = ["toml"], package = "config" }
tracing = { version = "0.1" }
tracing-subscriber = "0.3"
error_handling = { version = "1.0.0", path = "../../utils/error-handling" }
Expand All @@ -31,4 +31,4 @@ serde = { version = "1.0.89", features = ["derive", "alloc"], default-features =
hashbrown = { version = "0.11", default-features = false, features = ["ahash", "serde"] }
key-utils = { version = "^1.0.0", path = "../../utils/key-utils" }
rpc_sv2 = { version = "1.0.0", path = "../roles-utils/rpc" }
hex = "0.4.3"
hex = "0.4.3"
13 changes: 9 additions & 4 deletions roles/jd-server/src/lib/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ where
_ => Err(serde::de::Error::custom("Unsupported duration unit")),
}
}

#[cfg(test)]
mod tests {
use ext_config::{Config, File, FileFormat};
use std::path::PathBuf;

use super::*;
Expand All @@ -119,9 +119,14 @@ mod tests {
config_path
);

let config_string =
std::fs::read_to_string(config_path).expect("Failed to read the config file");
toml::from_str(&config_string).expect("Failed to parse config")
let config_path = config_path.to_str().unwrap();

let settings = Config::builder()
.add_source(File::new(&config_path, FileFormat::Toml))
.build()
.expect("Failed to build config");

settings.try_deserialize().expect("Failed to parse config")
}

#[test]
Expand Down
14 changes: 10 additions & 4 deletions roles/jd-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use tokio::{select, task};
use tracing::{error, info, warn};
mod lib;

use ext_config::{Config, File, FileFormat};
use lib::job_declarator::JobDeclarator;

mod args {
Expand Down Expand Up @@ -87,17 +88,22 @@ async fn main() {
}
};

let config_path = args.config_path.to_str().expect("Invalid config path");

// Load config
let config: Configuration = match std::fs::read_to_string(&args.config_path) {
Ok(c) => match toml::from_str(&c) {
let config: Configuration = match Config::builder()
.add_source(File::new(config_path, FileFormat::Toml))
.build()
{
Ok(settings) => match settings.try_deserialize::<Configuration>() {
Ok(c) => c,
Err(e) => {
error!("Failed to parse config: {}", e);
error!("Failed to deserialize config: {}", e);
return;
}
},
Err(e) => {
error!("Failed to read config: {}", e);
error!("Failed to build config: {}", e);
return;
}
};
Expand Down
2 changes: 1 addition & 1 deletion roles/mining-proxy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ once_cell = "1.12.0"
roles_logic_sv2 = { version = "^1.0.0", path = "../../protocols/v2/roles-logic-sv2" }
serde = { version = "1.0.89", features = ["derive", "alloc"], default-features = false }
tokio = { version = "1", features = ["full"] }
toml = { version = "0.5.6", git = "https://github.com/diondokter/toml-rs", default-features = false, rev = "c4161aa" }
ext-config = { version = "0.14.0", features = ["toml"], package = "config" }
tracing = {version = "0.1"}
tracing-subscriber = {version = "0.3"}
nohash-hasher = "0.2.0"
Expand Down
4 changes: 2 additions & 2 deletions roles/mining-proxy/src/lib/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ pub enum ChannelKind {
}

#[derive(Debug, Deserialize, Clone)]
pub struct Config {
pub struct Configuration {
pub upstreams: Vec<UpstreamMiningValues>,
pub listen_address: String,
pub listen_mining_port: u16,
Expand All @@ -110,7 +110,7 @@ pub struct Config {
pub async fn initialize_r_logic(
upstreams: &[UpstreamMiningValues],
group_id: Arc<Mutex<GroupId>>,
config: Config,
config: Configuration,
) -> RLogic {
let channel_ids = Arc::new(Mutex::new(Id::new()));
let mut upstream_mining_nodes = Vec::with_capacity(upstreams.len());
Expand Down
23 changes: 16 additions & 7 deletions roles/mining-proxy/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ use std::{net::SocketAddr, sync::Arc};
use tokio::{net::TcpListener, sync::oneshot};
use tracing::{error, info};

use lib::Config;
use ext_config::{Config, File, FileFormat};
use lib::Configuration;
use roles_logic_sv2::utils::{GroupId, Mutex};

mod lib;
Expand Down Expand Up @@ -112,13 +113,21 @@ async fn main() {
}
};

// Scan all the upstreams and map them
let config_file = std::fs::read_to_string(args.config_path.clone())
.unwrap_or_else(|_| panic!("Can not open {:?}", args.config_path));
let config = match toml::from_str::<Config>(&config_file) {
Ok(cfg) => cfg,
let config_path = args.config_path.to_str().expect("Invalid config path");

let config: Configuration = match Config::builder()
.add_source(File::new(config_path, FileFormat::Toml))
.build()
{
Ok(settings) => match settings.try_deserialize::<Configuration>() {
Ok(c) => c,
Err(e) => {
error!("Failed to deserialize config: {}", e);
return;
}
},
Err(e) => {
error!("Failed to parse config file: {}", e);
error!("Failed to build config: {}", e);
return;
}
};
Expand Down
2 changes: 1 addition & 1 deletion roles/pool/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ rand = "0.8.4"
roles_logic_sv2 = { version = "^1.0.0", path = "../../protocols/v2/roles-logic-sv2" }
serde = { version = "1.0.89", features = ["derive", "alloc"], default-features = false }
tokio = { version = "1", features = ["full"] }
toml = { version = "0.5.6", git = "https://github.com/diondokter/toml-rs", default-features = false, rev = "c4161aa" }
ext-config = { version = "0.14.0", features = ["toml"], package = "config" }
tracing = { version = "0.1" }
tracing-subscriber = "0.3"
async-recursion = "1.0.0"
Expand Down
28 changes: 23 additions & 5 deletions roles/pool/src/lib/mining_pool/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -657,23 +657,41 @@ impl Pool {
#[cfg(test)]
mod test {
use binary_sv2::{B0255, B064K};
use ext_config::{Config, File, FileFormat};
use std::convert::TryInto;
use tracing::error;

use stratum_common::{
bitcoin,
bitcoin::{util::psbt::serialize::Serialize, Transaction, Witness},
};

use super::Configuration;

// this test is used to verify the `coinbase_tx_prefix` and `coinbase_tx_suffix` values tested against in
// message generator `stratum/test/message-generator/test/pool-sri-test-extended.json`
#[test]
fn test_coinbase_outputs_from_config() {
let config_path = "./config-examples/pool-config-local-tp-example.toml";

// Load config
let config: super::Configuration = toml::from_str(
&std::fs::read_to_string("./config-examples/pool-config-local-tp-example.toml")
.unwrap(),
)
.unwrap();
let config: Configuration = match Config::builder()
.add_source(File::new(&config_path, FileFormat::Toml))
.build()
{
Ok(settings) => match settings.try_deserialize::<Configuration>() {
Ok(c) => c,
Err(e) => {
error!("Failed to deserialize config: {}", e);
return;
}
},
Err(e) => {
error!("Failed to build config: {}", e);
return;
}
};

// template from message generator test (mock TP template)
let _extranonce_len = 3;
let coinbase_prefix = vec![3, 76, 163, 38, 0];
Expand Down
14 changes: 10 additions & 4 deletions roles/pool/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use lib::{
template_receiver::TemplateRx,
};

use ext_config::{Config, File, FileFormat};
use tokio::select;

mod args {
Expand Down Expand Up @@ -86,17 +87,22 @@ async fn main() {
}
};

let config_path = args.config_path.to_str().expect("Invalid config path");

// Load config
let config: Configuration = match std::fs::read_to_string(&args.config_path) {
Ok(c) => match toml::from_str(&c) {
let config: Configuration = match Config::builder()
.add_source(File::new(config_path, FileFormat::Toml))
.build()
{
Ok(settings) => match settings.try_deserialize::<Configuration>() {
Ok(c) => c,
Err(e) => {
error!("Failed to parse config: {}", e);
error!("Failed to deserialize config: {}", e);
return;
}
},
Err(e) => {
error!("Failed to read config: {}", e);
error!("Failed to build config: {}", e);
return;
}
};
Expand Down
3 changes: 1 addition & 2 deletions roles/translator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ serde = { version = "1.0.89", default-features = false, features = ["derive", "a
serde_json = { version = "1.0.64", default-features = false, features = ["alloc"] }
futures = "0.3.25"
tokio = { version = "1", features = ["full"] }
toml = { version = "0.5.6", git = "https://github.com/diondokter/toml-rs", default-features = false, rev = "c4161aa" }
ext-config = { version = "0.14.0", features = ["toml"], package = "config" }
tracing = { version = "0.1" }
tracing-subscriber = { version = "0.3" }
v1 = { version = "^1.0.0", path = "../../protocols/v1", package="sv1_api" }
Expand All @@ -36,7 +36,6 @@ tokio-util = { version = "0.7.10", features = ["codec"] }
async-compat = "0.2.1"



[dev-dependencies]
rand = "0.8.4"
sha2 = "0.10.6"
Expand Down
Loading

0 comments on commit 9bb8f15

Please sign in to comment.