Skip to content

Commit 9973ebf

Browse files
committed
update diff when rate_limit_hit_count >= 5
1 parent 804d899 commit 9973ebf

4 files changed

Lines changed: 43 additions & 21 deletions

File tree

src/jd_client/mining_downstream/mod.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -613,10 +613,11 @@ impl
613613
header_nonce: share.nonce,
614614
coinbase_tx: coinbase.try_into()?,
615615
};
616-
// The below channel should never be full is ok to block
617-
solution_sender
618-
.blocking_send(solution)
619-
.map_err(|_| Error::DownstreamDown)?; // Better Error to return here?
616+
tokio::spawn(async move {
617+
if solution_sender.send(solution).await.is_err() {
618+
error!("Downstream channel closed, couldn't send solution");
619+
}
620+
});
620621
if !self.status.is_solo_miner() {
621622
{
622623
let jd = self.jd.clone();

src/translator/downstream/diff_management.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ impl Downstream {
4747
});
4848
stats_sender.update_diff(connection_id, diff);
4949
stats_sender.update_hashrate(connection_id, estimated_hashrate);
50-
tokio::spawn(crate::translator::utils::check_share_rate_limit());
50+
let downstream = self_.clone();
51+
tokio::spawn(crate::translator::utils::check_share_rate_limit(downstream));
5152

5253
Ok(())
5354
}

src/translator/proxy/bridge.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -263,25 +263,22 @@ impl Bridge {
263263
let mut upstream_target: Target = upstream_target.into();
264264
let res = self_
265265
.safe_lock(|s| {
266-
let job_id = share.share.job_id.parse::<u32>().expect("Invalid job_id format");
266+
let job_id = share.share.job_id.parse::<u32>().expect("Invalid job_id");
267267
if !s.valid_job_ids.contains(&job_id) {
268268
info!("Share rejected: job_id {} not in last two jobs", job_id);
269269
return Err(roles_logic_sv2::Error::ShareDoNotMatchAnyJob); // rejected
270270
}
271271
s.channel_factory.set_target(&mut upstream_target);
272-
match s.translate_submit(
273-
share.channel_id,
274-
share.share,
275-
share.version_rolling_mask,
276-
) {
272+
match s.translate_submit(share.channel_id, share.share, share.version_rolling_mask)
273+
{
277274
Ok(submit_shares_extended) => {
278-
// Ordering::Relaxed is safe here because we only need simple counter updates.
275+
// Ordering::Relaxed is safe here because we only need simple counter updates.
279276
// No need for strict ordering since it just tracks failures.
280277
SUBMIT_FAIL_COUNTER.store(0, Ordering::Relaxed); // Reset on success
281-
s.channel_factory.on_submit_shares_extended(submit_shares_extended)
282-
},
283-
Err(e) => {
284-
error!("Failed to Translates SV1 mining.submit message to SV2 SubmitSharesExtended message: {e}");
278+
s.channel_factory
279+
.on_submit_shares_extended(submit_shares_extended)
280+
}
281+
Err(_) => {
285282
Err(roles_logic_sv2::Error::NoValidJob) // Error will be handled by the caller
286283
}
287284
}
@@ -537,7 +534,7 @@ impl Bridge {
537534
self_
538535
.safe_lock(|s| {
539536
s.last_notify = Some(notify);
540-
s.valid_job_ids.push_back(j_id); // Add new job_id
537+
s.valid_job_ids.push_back(j_id);
541538
if s.valid_job_ids.len() > 2 {
542539
s.valid_job_ids.pop_front(); // Remove oldest if more than 2
543540
}

src/translator/utils.rs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use bitcoin::{
1717
use lazy_static::lazy_static;
1818
use roles_logic_sv2::utils::Mutex;
1919
use sv1_api::{client_to_server, server_to_client::Notify};
20-
use tracing::error;
20+
use tracing::{debug, error};
2121

2222
use super::downstream::Downstream;
2323
lazy_static! {
@@ -30,8 +30,11 @@ lazy_static! {
3030

3131
/// Checks if a share can be sent upstream based on a rate limit of 70 shares per minute.
3232
/// Returns `true` if the share can be sent, `false` if the limit is exceeded.
33-
pub async fn check_share_rate_limit() {
33+
pub async fn check_share_rate_limit(downstream: Arc<Mutex<Downstream>>) {
3434
let mut interval = tokio::time::interval(std::time::Duration::from_secs(1));
35+
let mut last_update = tokio::time::Instant::now(); // Track last difficulty update
36+
let mut rate_limit_hit_count = 0;
37+
3538
loop {
3639
interval.tick().await;
3740
let now = tokio::time::Instant::now();
@@ -52,7 +55,24 @@ pub async fn check_share_rate_limit() {
5255
0
5356
});
5457

55-
IS_RATE_LIMITED.store(count >= 70, std::sync::atomic::Ordering::SeqCst);
58+
let is_limited = count >= 70;
59+
IS_RATE_LIMITED.store(is_limited, std::sync::atomic::Ordering::SeqCst);
60+
61+
if is_limited {
62+
rate_limit_hit_count += 1;
63+
} else {
64+
rate_limit_hit_count = 0;
65+
}
66+
67+
if rate_limit_hit_count >= 5 && now.duration_since(last_update).as_secs() >= 2 {
68+
debug!("Rate limited. Updating difficulty");
69+
if let Err(e) = Downstream::try_update_difficulty_settings(&downstream).await {
70+
error!("Failed to update difficulty: {e}");
71+
}
72+
last_update = now;
73+
IS_RATE_LIMITED.store(false, std::sync::atomic::Ordering::SeqCst);
74+
rate_limit_hit_count = 0;
75+
}
5676
}
5777
}
5878

@@ -92,7 +112,10 @@ pub fn validate_share(
92112
let job = match matching_job {
93113
Some(job) => job,
94114
None => {
95-
error!("Share rejected: Invalid Job ID {}", request.job_id);
115+
error!(
116+
"Share rejected: Job ID {} not found in recent notify msgs",
117+
request.job_id
118+
);
96119
return false;
97120
}
98121
};

0 commit comments

Comments
 (0)