Skip to content

Commit

Permalink
Fix issue stratum-mining#730
Browse files Browse the repository at this point in the history
  • Loading branch information
GitGab19 committed Jan 23, 2024
1 parent 764ae99 commit 4100000
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
15 changes: 14 additions & 1 deletion roles/jd-server/src/lib/mempool/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use serde::{Deserialize, Serialize};
use std::{convert::TryInto, sync::Arc};
use stratum_common::{bitcoin, bitcoin::hash_types::Txid};

use self::rpc_client::BitcoincoreRpcError;

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Hash([u8; 32]);

Expand Down Expand Up @@ -57,7 +59,11 @@ impl JDsMempool {
.ok_or(JdsMempoolError::NoClient)?;
let new_mempool: Result<Vec<TransacrtionWithHash>, JdsMempoolError> =
tokio::task::spawn(async move {
let mempool: Vec<String> = client.get_raw_mempool_verbose().unwrap();
let mempool: Result<Vec<String>, BitcoincoreRpcError> = client.get_raw_mempool_verbose();
let mempool = mempool.map_err(|e| {
println!("Error fetching mempool: {:?}\nUnable to connect to Template Provider (possible reasons: not fully synced, down)", e);
JdsMempoolError::BitcoinCoreRpcError(e)
})?;
for id in &mempool {
let tx: Result<Transaction, _> = client.get_raw_transaction(id, None);
if let Ok(tx) = tx {
Expand Down Expand Up @@ -106,4 +112,11 @@ impl JDsMempool {
pub enum JdsMempoolError {
EmptyMempool,
NoClient,
BitcoinCoreRpcError(BitcoincoreRpcError),
}

impl From<BitcoincoreRpcError> for JdsMempoolError {
fn from(error: BitcoincoreRpcError) -> Self {
JdsMempoolError::BitcoinCoreRpcError(error)
}
}
35 changes: 33 additions & 2 deletions roles/jd-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ pub struct Configuration {
pub core_rpc_port: u16,
pub core_rpc_user: String,
pub core_rpc_pass: String,
#[serde(deserialize_with = "duration_from_toml")]
pub mempool_update_timeout: Duration,
}

mod args {
Expand Down Expand Up @@ -140,6 +142,35 @@ mod args {
}
}

fn duration_from_toml<'de, D>(deserializer: D) -> Result<Duration, D::Error>
where
D: serde::Deserializer<'de>,
{
#[derive(Deserialize)]
struct Helper {
unit: String,
value: u64,
}

let helper = Helper::deserialize(deserializer)?;
match helper.unit.as_str() {
"seconds" => Ok(Duration::from_secs(helper.value)),
"secs" => Ok(Duration::from_secs(helper.value)),
"s" => Ok(Duration::from_secs(helper.value)),
"milliseconds" => Ok(Duration::from_millis(helper.value)),
"millis" => Ok(Duration::from_millis(helper.value)),
"ms" => Ok(Duration::from_millis(helper.value)),
"microseconds" => Ok(Duration::from_micros(helper.value)),
"micros" => Ok(Duration::from_micros(helper.value)),
"us" => Ok(Duration::from_micros(helper.value)),
"nanoseconds" => Ok(Duration::from_nanos(helper.value)),
"nanos" => Ok(Duration::from_nanos(helper.value)),
"ns" => Ok(Duration::from_nanos(helper.value)),
// ... add other units as needed
_ => Err(serde::de::Error::custom("Unsupported duration unit")),
}
}

#[tokio::main]
async fn main() {
tracing_subscriber::fmt::init();
Expand Down Expand Up @@ -174,13 +205,13 @@ async fn main() {
username,
password,
)));
let mempool_update_timeout = config.mempool_update_timeout.clone();
let mempool_cloned_ = mempool.clone();
if url.contains("http") {
task::spawn(async move {
loop {
let _ = mempool::JDsMempool::update_mempool(mempool_cloned_.clone()).await;
// TODO this should be configurable by the user
tokio::time::sleep(Duration::from_millis(10000)).await;
tokio::time::sleep(mempool_update_timeout).await;
}
});
};
Expand Down

0 comments on commit 4100000

Please sign in to comment.