Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "dmnd-client"
version = "0.3.14"
version = "0.3.15"
edition = "2021"

[lib]
Expand Down
62 changes: 46 additions & 16 deletions src/translator/downstream/diff_management.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ impl Downstream {
u.channel_nominal_hashrate -=
// Make sure that upstream channel hasrate never goes below 0
f32::min(estimated_downstream_hash_rate, u.channel_nominal_hashrate);
u.request_immediate_update();
})?;
Ok(())
}
Expand Down Expand Up @@ -318,6 +319,7 @@ impl Downstream {
} else {
c.channel_nominal_hashrate = 0.0;
}
c.request_immediate_update();
})?;
Ok(())
}
Expand Down Expand Up @@ -560,10 +562,8 @@ mod test {
initial_difficulty: latest_difficulty,
hard_minimum_difficulty: None,
};
let upstream_config = Arc::new(Mutex::new(UpstreamDifficultyConfig {
channel_diff_update_interval: 60,
channel_nominal_hashrate,
}));
let (upstream_config, _rx) = UpstreamDifficultyConfig::new(60, channel_nominal_hashrate);
let upstream_config = Arc::new(Mutex::new(upstream_config));
let (tx_sv1_submit, _rx_sv1_submit) = tokio::sync::mpsc::channel(10);
let (tx_outgoing, _rx_outgoing) = channel(10);
let (tx_update_token, _rx_update_token) = channel(10);
Expand Down Expand Up @@ -620,10 +620,8 @@ mod test {
initial_difficulty: latest_difficulty,
hard_minimum_difficulty: None,
};
let upstream_config = Arc::new(Mutex::new(UpstreamDifficultyConfig {
channel_diff_update_interval: 60,
channel_nominal_hashrate,
}));
let (upstream_config, _rx) = UpstreamDifficultyConfig::new(60, channel_nominal_hashrate);
let upstream_config = Arc::new(Mutex::new(upstream_config));
let (tx_sv1_submit, _rx_sv1_submit) = tokio::sync::mpsc::channel(10);
let (tx_outgoing, rx_outgoing) = channel(10);
let (tx_update_token, _rx_update_token) = channel(10);
Expand Down Expand Up @@ -683,8 +681,15 @@ mod test {
target_rate_submits(),
RAW_BOOTSTRAP_HASHRATE,
);
let mut update_rx = upstream_config
.safe_lock(|u| u.subscribe_updates())
.unwrap();

Downstream::update_difficulty_and_hashrate(&downstream).unwrap();
tokio::time::timeout(Duration::from_secs(1), update_rx.changed())
.await
.unwrap()
.unwrap();

let estimated_downstream_hash_rate = downstream
.safe_lock(|d| d.difficulty_mgmt.estimated_downstream_hash_rate)
Expand All @@ -697,6 +702,37 @@ mod test {
assert_hashrate_close(channel_nominal_hashrate, quantized_hashrate);
}

#[tokio::test]
async fn removing_downstream_hashrate_requests_immediate_upstream_retarget() {
let latest_difficulty = 1_024.0;
let estimated_hashrate = hashrate_for_diff(latest_difficulty);
let pid = Pid::new(*crate::SHARE_PER_MIN, latest_difficulty * 10.0);
let (downstream, upstream_config) = seeded_downstream(
estimated_hashrate,
latest_difficulty,
pid,
VecDeque::new(),
estimated_hashrate,
);
let mut update_rx = upstream_config
.safe_lock(|u| u.subscribe_updates())
.unwrap();

downstream
.safe_lock(|d| d.mark_channel_hashrate_registered())
.unwrap();
Downstream::remove_downstream_hashrate_from_channel(&downstream).unwrap();
tokio::time::timeout(Duration::from_secs(1), update_rx.changed())
.await
.unwrap()
.unwrap();

let channel_nominal_hashrate = upstream_config
.safe_lock(|u| u.channel_nominal_hashrate)
.unwrap();
assert_eq!(channel_nominal_hashrate, 0.0);
}

#[tokio::test]
#[ignore = "non-zero delay is blocked until the stale bootstrap difficulty replay bug is fixed"]
async fn positive_delay_does_not_replay_stale_bootstrap_difficulty_after_retarget() {
Expand Down Expand Up @@ -840,10 +876,7 @@ mod test {
initial_difficulty: 10_000.0,
hard_minimum_difficulty: Some(NON_LOCAL_DOWNSTREAM_MIN_DIFFICULTY),
};
let upstream_config = UpstreamDifficultyConfig {
channel_diff_update_interval: 60,
channel_nominal_hashrate: 0.0,
};
let (upstream_config, _rx) = UpstreamDifficultyConfig::new(60, 0.0);
let (tx_sv1_submit, _rx_sv1_submit) = tokio::sync::mpsc::channel(10);
let (tx_outgoing, _rx_outgoing) = channel(10);
let (tx_update_token, _rx_update_token) = channel(10);
Expand Down Expand Up @@ -902,10 +935,7 @@ mod test {
initial_difficulty: 10_000_000_000.0,
hard_minimum_difficulty: None,
};
let upstream_config = UpstreamDifficultyConfig {
channel_diff_update_interval: 60,
channel_nominal_hashrate: 0.0,
};
let (upstream_config, _rx) = UpstreamDifficultyConfig::new(60, 0.0);
let (tx_sv1_submit, _rx_sv1_submit) = tokio::sync::mpsc::channel(10);
let (tx_outgoing, _rx_outgoing) = channel(10);
let (tx_update_token, _rx_update_token) = channel(10);
Expand Down
6 changes: 2 additions & 4 deletions src/translator/downstream/downstream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1236,10 +1236,8 @@ mod tests {
initial_difficulty: 1.0,
hard_minimum_difficulty: None,
};
let upstream_config = UpstreamDifficultyConfig {
channel_diff_update_interval: crate::CHANNEL_DIFF_UPDTATE_INTERVAL,
channel_nominal_hashrate: 0.0,
};
let (upstream_config, _rx) =
UpstreamDifficultyConfig::new(crate::CHANNEL_DIFF_UPDTATE_INTERVAL, 0.0);
let (tx_sv1_submit, _rx_sv1_submit) = channel::<DownstreamMessages>(8);
let (tx_outgoing, rx_outgoing) = channel(8);
let (tx_update_token, _rx_update_token) = channel(8);
Expand Down
65 changes: 56 additions & 9 deletions src/translator/downstream/notify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ pub async fn start_notify(
d.difficulty_mgmt.estimated_downstream_hash_rate,
)
})?;
upstream_difficulty_config
.safe_lock(|c| c.channel_nominal_hashrate += registered_hashrate)?;
upstream_difficulty_config.safe_lock(|c| {
c.channel_nominal_hashrate += registered_hashrate;
c.request_immediate_update();
})?;
downstream.safe_lock(|d| d.mark_channel_hashrate_registered())?;
if let Err(e) = stats_sender.setup_stats_reliable(connection_id).await {
error!("Failed to register downstream stats {connection_id}: {e}");
Expand All @@ -66,6 +68,7 @@ pub async fn start_notify(
upstream_difficulty_config.safe_lock(|u| {
u.channel_nominal_hashrate -=
f32::min(registered_hashrate, u.channel_nominal_hashrate);
u.request_immediate_update();
})?;
}
return Ok(());
Expand Down Expand Up @@ -275,10 +278,8 @@ mod tests {
initial_difficulty: 1.0,
hard_minimum_difficulty: None,
};
let upstream_config = UpstreamDifficultyConfig {
channel_diff_update_interval: crate::CHANNEL_DIFF_UPDTATE_INTERVAL,
channel_nominal_hashrate: 0.0,
};
let (upstream_config, _rx) =
UpstreamDifficultyConfig::new(crate::CHANNEL_DIFF_UPDTATE_INTERVAL, 0.0);
let (tx_sv1_submit, _rx_sv1_submit) = channel::<DownstreamMessages>(8);
let (tx_outgoing, rx_outgoing) = channel(8);
let (tx_update_token, _rx_update_token) = channel(8);
Expand Down Expand Up @@ -342,10 +343,11 @@ mod tests {
initial_difficulty: latest_difficulty,
hard_minimum_difficulty: None,
};
let upstream_config = Arc::new(Mutex::new(UpstreamDifficultyConfig {
channel_diff_update_interval: crate::CHANNEL_DIFF_UPDTATE_INTERVAL,
let (upstream_config, _rx) = UpstreamDifficultyConfig::new(
crate::CHANNEL_DIFF_UPDTATE_INTERVAL,
channel_nominal_hashrate,
}));
);
let upstream_config = Arc::new(Mutex::new(upstream_config));
let (tx_sv1_submit, _rx_sv1_submit) = channel::<DownstreamMessages>(8);
let (tx_outgoing, _rx_outgoing) = channel(8);
let (tx_update_token, _rx_update_token) = channel(8);
Expand Down Expand Up @@ -421,6 +423,51 @@ mod tests {
}
}

#[tokio::test]
async fn start_notify_requests_immediate_upstream_retarget_on_register() {
let first_job = first_job("91");
let (downstream, _rx_outgoing) =
downstream_with_first_job(first_job, vec!["worker".to_string()]);
let downstream = Arc::new(Mutex::new(downstream));
let mut update_rx = downstream
.safe_lock(|d| {
d.upstream_difficulty_config
.safe_lock(|c| c.subscribe_updates())
.unwrap()
})
.unwrap();
let task_manager = TaskManager::initialize();
let (_tx_notify, rx_notify) = broadcast::channel(8);

start_notify(
task_manager.clone(),
downstream.clone(),
rx_notify,
"127.0.0.1".to_string(),
1,
)
.await
.unwrap();

tokio::time::timeout(Duration::from_secs(1), update_rx.changed())
.await
.unwrap()
.unwrap();

let channel_nominal_hashrate = downstream
.safe_lock(|d| {
d.upstream_difficulty_config
.safe_lock(|c| c.channel_nominal_hashrate)
.unwrap()
})
.unwrap();
assert_eq!(channel_nominal_hashrate, 1.0);

if let Some(aborter) = task_manager.safe_lock(|t| t.get_aborter()).unwrap() {
drop(aborter);
}
}

#[tokio::test]
async fn first_retarget_waits_full_adjustment_interval_before_fixing_upstream_hashrate() {
let quantized_difficulty = quantized_difficulty_for_hashrate(RAW_BOOTSTRAP_HASHRATE);
Expand Down
7 changes: 4 additions & 3 deletions src/translator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ pub async fn start(

let channel_nominal_hashrate = 0.0;

let upstream_diff = UpstreamDifficultyConfig {
channel_diff_update_interval: crate::CHANNEL_DIFF_UPDTATE_INTERVAL,
let (upstream_diff, diff_update_rx) = UpstreamDifficultyConfig::new(
crate::CHANNEL_DIFF_UPDTATE_INTERVAL,
channel_nominal_hashrate,
};
);
let diff_config = Arc::new(Mutex::new(upstream_diff));

// Instantiate a new `Upstream` (SV2 Pool)
Expand All @@ -113,6 +113,7 @@ pub async fn start(
recv_from_up,
rx_sv2_submit_shares_ext,
rx_update_token,
diff_update_rx,
)
.await?;
TaskManager::add_upstream(task_manager.clone(), upstream_abortable)
Expand Down
Loading
Loading