use {
super::*,
stratum::client::{Client, ClientError, Event},
};
pub(crate) const NOTIFY_TIMEOUT: Duration = Duration::from_secs(600);
pub(crate) struct UpstreamSubmit {
pub(crate) job_id: JobId,
pub(crate) enonce2: Extranonce,
pub(crate) nonce: Nonce,
pub(crate) ntime: Ntime,
pub(crate) version_bits: Option<Version>,
pub(crate) share_diff: Difficulty,
}
pub(crate) struct Upstream {
id: u32,
client: Client,
endpoint: String,
enonce1: Extranonce,
enonce2_size: usize,
version_mask: Option<Version>,
metatron: Arc<Metatron>,
connected: watch::Sender<bool>,
ping: Arc<RwLock<Duration>>,
difficulty: Arc<RwLock<Difficulty>>,
workbase_rx: watch::Receiver<Arc<Notify>>,
tasks: TaskTracker,
}
struct Disconnect(watch::Sender<bool>);
impl Drop for Disconnect {
fn drop(&mut self) {
self.0.send_if_modified(|connected| {
if !*connected {
return false;
}
*connected = false;
true
});
}
}
impl Upstream {
pub(crate) async fn connect(
id: u32,
target: &UpstreamTarget,
timeout: Duration,
notify_timeout: Duration,
cancel: CancellationToken,
tasks: &TaskTracker,
metatron: Arc<Metatron>,
) -> Result<Arc<Self>> {
info!(
"Connecting to upstream {} as {}",
target.endpoint(),
target.username()
);
let client = Client::with_cancel(
target.endpoint().into(),
target.username().clone(),
target.password().map(String::from),
USER_AGENT.into(),
timeout,
cancel.clone(),
);
let mut events = client
.connect()
.await
.context("failed to connect to upstream")?;
let version_mask = match client
.configure(
vec!["version-rolling".to_string()],
Some(Version::from_str("1fffe000").expect("valid hex")),
)
.await
{
Ok((response, ..)) => {
if response.version_rolling {
if let Some(mask) = response.version_rolling_mask {
info!("Upstream supports version rolling: mask={mask}",);
}
response.version_rolling_mask
} else {
info!("Upstream does not support version rolling");
None
}
}
Err(e) => {
warn!("Failed to negotiate version rolling with upstream: {e}");
None
}
};
let (subscribe, ..) = client
.subscribe()
.await
.context("failed to subscribe to upstream")?;
info!(
"Subscribed to upstream: enonce1={}, enonce2_size={}",
subscribe.enonce1, subscribe.enonce2_size
);
let (ping, _) = client
.authorize()
.await
.context("failed to authorize with upstream")?;
info!(
"Authorized to upstream {} with {}",
client.address(),
client.username()
);
let (initial_difficulty, first_notify) = tokio::time::timeout(timeout, async {
let mut initial_difficulty: Option<Difficulty> = None;
let mut first_notify: Option<Notify> = None;
Ok(loop {
match events.recv().await {
Ok(Event::SetDifficulty(diff)) => {
info!("Received initial difficulty: {}", diff);
initial_difficulty = Some(diff);
}
Ok(Event::Notify(notify)) => {
info!(
"Received job: job_id={}, clean_jobs={}",
notify.job_id, notify.clean_jobs
);
first_notify = Some(notify);
}
Ok(Event::Reconnect(_)) | Ok(Event::Disconnected) => {
bail!("Disconnected from upstream before initialization complete");
}
Err(e) => {
bail!("Upstream error during initialization: {e}");
}
}
if let Some(diff) = initial_difficulty
&& let Some(notify) = first_notify.take()
{
break (diff, notify);
}
})
})
.await
.context("timed out waiting for initial difficulty and job from upstream")??;
let difficulty = Arc::new(RwLock::new(initial_difficulty));
let (connected, _) = watch::channel(true);
let (workbase_tx, workbase_rx) = watch::channel(Arc::new(first_notify));
let difficulty_clone = difficulty.clone();
let disconnect = Disconnect(connected.clone());
let mut notify_deadline = tokio::time::Instant::now() + notify_timeout;
tasks.spawn(async move {
let _disconnect = disconnect;
loop {
tokio::select! {
biased;
_ = cancel.cancelled() => {
break;
}
_ = tokio::time::sleep_until(notify_deadline) => {
warn!(
"No work received from upstream for {}s, disconnecting",
notify_timeout.as_secs()
);
break;
}
event = events.recv() => {
match event {
Ok(Event::Notify(notify)) => {
debug!(
"Received notify: job_id={}, clean_jobs={}",
notify.job_id, notify.clean_jobs
);
notify_deadline = tokio::time::Instant::now() + notify_timeout;
workbase_tx.send_replace(Arc::new(notify));
}
Ok(Event::SetDifficulty(diff)) => {
info!("Received set_difficulty: {}", diff);
*difficulty_clone.write() = diff;
}
Ok(Event::Reconnect(_)) | Ok(Event::Disconnected) => {
warn!("Disconnected from upstream");
break;
}
Err(err) => {
error!("Upstream event error: {}", err);
break;
}
}
}
}
}
});
Ok(Arc::new(Self {
id,
client,
endpoint: target.endpoint().to_string(),
enonce1: subscribe.enonce1,
enonce2_size: subscribe.enonce2_size,
connected,
ping: Arc::new(RwLock::new(ping)),
difficulty,
metatron,
version_mask,
workbase_rx,
tasks: tasks.clone(),
}))
}
pub(crate) fn workbase_rx(&self) -> watch::Receiver<Arc<Notify>> {
self.workbase_rx.clone()
}
pub(crate) async fn submit_share(&self, submit: UpstreamSubmit) {
let upstream_diff = *self.difficulty.read();
if submit.share_diff < upstream_diff {
debug!(
"Share below upstream difficulty: share_diff={} < upstream_diff={}",
submit.share_diff, upstream_diff
);
return;
}
debug!(
"Submitting share to upstream: job_id={}, share_diff={}, upstream_diff={}",
submit.job_id, submit.share_diff, upstream_diff
);
let client = self.client.clone();
let metatron = self.metatron.clone();
let id = self.id;
let share_diff = submit.share_diff;
let ping = self.ping.clone();
self.tasks.spawn(async move {
match client
.submit(
submit.job_id,
submit.enonce2,
submit.ntime,
submit.nonce,
submit.version_bits,
)
.await
{
Ok(duration) => {
metatron.record_order_accepted(id, upstream_diff, share_diff);
*ping.write() = duration;
debug!("Upstream accepted share");
}
Err(err) => {
metatron.record_order_rejected(id, upstream_diff);
match err {
ClientError::SubmitFalse => warn!("Upstream rejected share"),
ClientError::Rejected { reason, .. } => {
warn!("Upstream rejected share: {reason}")
}
err => warn!("Upstream submit error: {err}"),
}
}
}
});
}
pub(crate) fn id(&self) -> u32 {
self.id
}
pub(crate) fn enonce1(&self) -> &Extranonce {
&self.enonce1
}
pub(crate) fn enonce2_size(&self) -> usize {
self.enonce2_size
}
pub(crate) fn difficulty(&self) -> Difficulty {
*self.difficulty.read()
}
pub(crate) fn is_connected(&self) -> bool {
*self.connected.borrow()
}
pub(crate) async fn disconnected(&self) {
let mut rx = self.connected.subscribe();
while *rx.borrow_and_update() {
if rx.changed().await.is_err() {
return;
}
}
}
pub(crate) fn endpoint(&self) -> &str {
&self.endpoint
}
pub(crate) fn username(&self) -> &Username {
self.client.username()
}
pub(crate) fn version_mask(&self) -> Option<Version> {
self.version_mask
}
pub(crate) fn ping_ms(&self) -> u128 {
self.ping.read().as_millis()
}
#[cfg(test)]
pub(crate) fn set_connected(&self, value: bool) {
self.connected.send_replace(value);
}
#[cfg(test)]
pub(crate) fn test(id: u32, metatron: Arc<Metatron>) -> Arc<Self> {
fn runtime() -> &'static tokio::runtime::Runtime {
static RUNTIME: std::sync::OnceLock<tokio::runtime::Runtime> =
std::sync::OnceLock::new();
RUNTIME.get_or_init(|| tokio::runtime::Runtime::new().unwrap())
}
let notify = Notify {
job_id: "bf".parse().unwrap(),
prevhash: "4d16b6f85af6e2198f44ae2a6de67f78487ae5611b77c6c0440b921e00000000"
.parse()
.unwrap(),
coinb1: "aa".into(),
coinb2: "bb".into(),
merkle_branches: Vec::new(),
version: Version(block::Version::TWO),
nbits: "1c2ac4af".parse().unwrap(),
ntime: "504e86b9".parse().unwrap(),
clean_jobs: false,
};
let (_, workbase_rx) = watch::channel(Arc::new(notify));
let _guard = runtime().enter();
Arc::new(Self {
id,
client: Client::new(
"foo:3333".into(),
"tb1qkrrl75qekv9ree0g2qt49j8vdynsvlc4kuctrc.worker"
.parse()
.unwrap(),
None,
"baz".into(),
Duration::from_secs(1),
),
endpoint: "foo:3333".into(),
enonce1: Extranonce::random(4),
enonce2_size: 4,
connected: watch::channel(true).0,
ping: Arc::new(RwLock::new(Duration::ZERO)),
difficulty: Arc::new(RwLock::new(Difficulty::from(1u64))),
metatron,
version_mask: None,
workbase_rx,
tasks: TaskTracker::new(),
})
}
}
#[cfg(test)]
mod tests {
use {
super::*,
std::net::SocketAddr,
tokio::{
io::{AsyncBufReadExt, AsyncWriteExt, BufReader},
net::TcpListener,
},
};
async fn mock_upstream(with_work: bool) -> SocketAddr {
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
tokio::spawn(async move {
let (socket, _) = listener.accept().await.unwrap();
let (read_half, mut write_half) = socket.into_split();
let mut reader = BufReader::new(read_half);
let mut line = String::new();
loop {
line.clear();
if reader.read_line(&mut line).await.unwrap_or(0) == 0 {
break;
}
let Ok(request) = serde_json::from_str::<serde_json::Value>(line.trim()) else {
continue;
};
let id = request["id"].clone();
let response = match request["method"].as_str() {
Some("mining.configure") => {
serde_json::json!({"id": id, "result": {}, "error": null})
}
Some("mining.subscribe") => serde_json::json!({
"id": id,
"result": [[["mining.notify", "ae6812eb4cd7735a302a8a9dd95cf71f"]], "08000002", 4],
"error": null,
}),
Some("mining.authorize") => {
write_half
.write_all(
format!(
"{}\n",
serde_json::json!({"id": id, "result": true, "error": null})
)
.as_bytes(),
)
.await
.unwrap();
if with_work {
write_half
.write_all(b"{\"id\":null,\"method\":\"mining.set_difficulty\",\"params\":[2]}\n")
.await
.unwrap();
write_half
.write_all(
b"{\"id\":null,\"method\":\"mining.notify\",\"params\":[\"bf\",\"4d16b6f85af6e2198f44ae2a6de67f78487ae5611b77c6c0440b921e00000000\",\"01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff20020862062f503253482f04b8864e5008\",\"072f736c7573682f000000000100f2052a010000001976a914d23fcdf86f7e756a64a7a9688ef9903327048ed988ac00000000\",[],\"00000002\",\"1c2ac4af\",\"504e86b9\",false]}\n",
)
.await
.unwrap();
}
break;
}
_ => continue,
};
write_half
.write_all(format!("{response}\n").as_bytes())
.await
.unwrap();
}
tokio::time::sleep(Duration::from_secs(60)).await;
});
addr
}
fn test_target(addr: SocketAddr) -> UpstreamTarget {
format!("tb1qkrrl75qekv9ree0g2qt49j8vdynsvlc4kuctrc.worker@{addr}")
.parse()
.unwrap()
}
#[tokio::test]
async fn connect_times_out_without_initial_job() {
let (metatron, _dir) = Metatron::test();
let addr = mock_upstream(false).await;
let tasks = TaskTracker::new();
let err = match Upstream::connect(
0,
&test_target(addr),
Duration::from_millis(300),
Duration::from_millis(300),
CancellationToken::new(),
&tasks,
Arc::new(metatron),
)
.await
{
Ok(_) => panic!("connect must time out without an initial job"),
Err(err) => err,
};
assert!(err.to_string().contains("timed out"), "{err:#}");
}
#[tokio::test]
async fn upstream_disconnects_when_notifies_stall() {
let (metatron, _dir) = Metatron::test();
let addr = mock_upstream(true).await;
let tasks = TaskTracker::new();
let upstream = Upstream::connect(
0,
&test_target(addr),
Duration::from_secs(5),
Duration::from_millis(200),
CancellationToken::new(),
&tasks,
Arc::new(metatron),
)
.await
.unwrap();
timeout(Duration::from_secs(2), upstream.disconnected())
.await
.expect("upstream must disconnect when notifies stall");
}
#[tokio::test]
async fn disconnected_returns_immediately_when_already_disconnected() {
let (metatron, _dir) = Metatron::test();
let upstream = Upstream::test(0, Arc::new(metatron));
upstream.set_connected(false);
timeout(Duration::from_millis(100), upstream.disconnected())
.await
.expect("disconnected() must return immediately when not connected");
}
#[tokio::test]
async fn disconnected_resolves_when_set_connected_flips_to_false() {
let (metatron, _dir) = Metatron::test();
let upstream = Upstream::test(0, Arc::new(metatron));
let flipper = upstream.clone();
tokio::spawn(async move {
tokio::time::sleep(Duration::from_millis(20)).await;
flipper.set_connected(false);
});
timeout(Duration::from_millis(500), upstream.disconnected())
.await
.expect("disconnected() must resolve once set_connected flips to false");
}
}