diff --git a/README.md b/README.md index f94d3a5..01898b5 100644 --- a/README.md +++ b/README.md @@ -59,11 +59,12 @@ The pool connection applies difficulty and job messages in stream order, dispatches work to the BM1366 mining session, and tracks up to 32 verified share submissions by request ID. It counts accepted and rejected pool replies separately. Continuous scheduling and live pool acceptance remain unverified. -Its cycle helper reads one pool event, polls one ASIC report, and sends a -verified share when present. A share reply unanswered for 30 seconds closes +Its cycle helper reads one pool event, drains up to 256 ASIC reports, and sends +verified shares when present. A share reply unanswered for 30 seconds closes the connection so a caller can reconnect instead of filling the pending queue indefinitely. Terminal pool errors request miner shutdown, and pool -reads are capped at 500 ms after the handshake so ASIC polling continues. +reads use 1 ms slices after the handshake so ASIC polling continues. Partial +pool lines have a 2-second deadline and pool writes have a 100 ms deadline. The executable still lacks a board adapter and run loop. The Stratum V1 decoder bounds and validates `mining.subscribe` responses, nine-field Bitcoin `mining.notify` jobs, difficulty and extranonce changes, diff --git a/ci/CONFORMANCE.md b/ci/CONFORMANCE.md index 1d09766..0ddc4a9 100644 --- a/ci/CONFORMANCE.md +++ b/ci/CONFORMANCE.md @@ -20,7 +20,14 @@ The report references files under `ci/evidence/` with objects containing The checker opens and hashes each file. It loads the trace and capture JSON, checks the current run/image/cell identities, verifies SHA-256d nonces against a pool target at difficulty 1 or higher, and requires a matching -`mining.submit` nonce, ntime, and pool ACCEPT response ID. The trace binds +`mining.submit` and pool ACCEPT response ID. A SHA-256 capture must include +the matching `subscription` (`extranonce1`, `extranonce2_size`), full +`notify` message, exact decimal `difficulty`, and a `configure_response` +with an accepted BIP310 mask when version bits were submitted. A later +`version_mask_event` may supply the effective `mining.set_version_mask`. +The checker reconstructs +the coinbase, merkle root, and complete submitted header; its 80 bytes and +calculated difficulty target must match the capture. The trace binds the capture digests. Scrypt and Equihash share verifiers are pending, so those cells fail closed. Bench calibration names the physical control-board and chip devices, their diff --git a/ci/check-conformance.py b/ci/check-conformance.py index fa2f7ae..9acdd41 100644 --- a/ci/check-conformance.py +++ b/ci/check-conformance.py @@ -3,6 +3,7 @@ """Require the binary target matrix and traceable per-cell release evidence.""" import argparse +from decimal import Decimal, InvalidOperation import hashlib import json import os @@ -72,6 +73,82 @@ def artifact(root: pathlib.Path, value: object) -> pathlib.Path: return path +def hex_field(value: object, size: int | None = None) -> bytes: + if (not isinstance(value, str) or len(value) & 1 + or len(value) > 131_072 or not re.fullmatch(r"[0-9a-fA-F]*", value) + or (size is not None and len(value) != size * 2)): + raise ValueError("invalid Stratum hex field") + return bytes.fromhex(value) + + +def u32_hex(value: object) -> int: + return int.from_bytes(hex_field(value, 4), "big") + + +def reconstructed_header(capture: dict, params: list[str]) -> tuple[bytes, int]: + subscription = capture.get("subscription") + notify = capture.get("notify") + if not isinstance(subscription, dict) or not isinstance(notify, dict): + raise ValueError("subscription and notify are required to bind the share") + extra1 = hex_field(subscription.get("extranonce1")) + extra_size = subscription.get("extranonce2_size") + if (len(extra1) > 32 or type(extra_size) is not int + or not 0 <= extra_size <= 32): + raise ValueError("invalid subscription extranonce") + extra2 = hex_field(params[2], extra_size) + job = notify.get("params") + if (notify.get("method") != "mining.notify" or not isinstance(job, list) + or len(job) != 9 or job[0] != params[1] + or not isinstance(job[4], list) or len(job[4]) > 64 + or type(job[8]) is not bool): + raise ValueError("submission is not bound to a matching notify") + prevhash = hex_field(job[1], 32) + coinbase = hex_field(job[2]) + extra1 + extra2 + hex_field(job[3]) + if len(coinbase) > 65_536: + raise ValueError("coinbase exceeds supported size") + root = hashlib.sha256(hashlib.sha256(coinbase).digest()).digest() + for branch in job[4]: + pair = root + hex_field(branch, 32) + root = hashlib.sha256(hashlib.sha256(pair).digest()).digest() + base_version = u32_hex(job[5]) + nbits = u32_hex(job[6]) + u32_hex(job[7]) + submitted_ntime = u32_hex(params[3]) + submitted_nonce = u32_hex(params[4]) + if len(params) == 6: + configure = capture.get("configure_response") + result = configure.get("result") if isinstance(configure, dict) else None + if not isinstance(result, dict) or result.get("version-rolling") is not True: + raise ValueError("version rolling was not negotiated") + mask_text = result.get("version-rolling.mask") + mask_event = capture.get("version_mask_event") + if mask_event is not None: + fields = mask_event.get("params") if isinstance(mask_event, dict) else None + if (not isinstance(mask_event, dict) + or mask_event.get("method") != "mining.set_version_mask" + or not isinstance(fields, list) or len(fields) != 1): + raise ValueError("invalid effective version mask event") + mask_text = fields[0] + mask = u32_hex(mask_text) + bits = u32_hex(params[5]) + if bits & ~mask: + raise ValueError("submitted version bits exceed negotiated mask") + version = (base_version & ~mask) | bits + else: + if capture.get("configure_response") is not None or capture.get("version_mask_event") is not None: + raise ValueError("negotiated version mask requires submission bits") + version = base_version + header = bytearray() + header.extend(version.to_bytes(4, "little")) + for offset in range(0, 32, 4): + header.extend(prevhash[offset:offset + 4][::-1]) + header.extend(root) + header.extend(submitted_ntime.to_bytes(4, "little")) + header.extend(nbits.to_bytes(4, "little")) + header.extend(submitted_nonce.to_bytes(4, "little")) + return bytes(header), submitted_nonce + + def verify_accept(capture: dict, algorithm: str) -> None: if algorithm != "Sha256": raise ValueError(f"{algorithm} share verifier is not implemented") @@ -96,7 +173,6 @@ def verify_accept(capture: dict, algorithm: str) -> None: if (not isinstance(params, list) or len(params) not in (5, 6) or not all(isinstance(value, str) for value in params) or not params[0] or not params[1] - or not re.fullmatch(r"[0-9a-f]{0,64}", params[2].lower()) or params[3].lower() != f"{int.from_bytes(header[68:72], 'little'):08x}" or params[4].lower() != f"{nonce:08x}" or (len(params) == 6 and not re.fullmatch(r"[0-9a-f]{8}", params[5].lower())) @@ -110,6 +186,24 @@ def verify_accept(capture: dict, algorithm: str) -> None: target = int.from_bytes(bytes.fromhex(target_hex), "little") if not 0 < target <= DIFF_ONE_TARGET: raise ValueError("pool target is below difficulty one") + difficulty_text = capture.get("difficulty") + if not isinstance(difficulty_text, str) or len(difficulty_text) > 128: + raise ValueError("effective pool difficulty is missing") + try: + difficulty = Decimal(difficulty_text) + except InvalidOperation as error: + raise ValueError("invalid pool difficulty") from error + if not difficulty.is_finite() or difficulty < 1: + raise ValueError("pool difficulty is below one") + if abs(difficulty.as_tuple().exponent) > 64: + raise ValueError("pool difficulty exceeds supported range") + numerator, denominator = difficulty.as_integer_ratio() + expected_target = DIFF_ONE_TARGET * denominator // numerator + if target != expected_target: + raise ValueError("pool target differs from effective difficulty") + rebuilt, submitted_nonce = reconstructed_header(capture, params) + if submitted_nonce != nonce or rebuilt != header: + raise ValueError("submitted Stratum job differs from captured header") hash_bytes = hashlib.sha256(hashlib.sha256(header).digest()).digest() if int.from_bytes(hash_bytes, "little") > target: raise ValueError("captured nonce misses the pool target") diff --git a/ci/release.py b/ci/release.py index 343537d..f566284 100644 --- a/ci/release.py +++ b/ci/release.py @@ -73,6 +73,19 @@ def get_release(url): raise +def check_tag_commit(api, repo, tag, sha): + url = f"{api}/repos/{repo}/tags/{urllib.parse.quote(tag, safe='')}" + try: + remote_tag = request(url) + except urllib.error.HTTPError as error: + if error.code == 404: + return False + raise + if not isinstance(remote_tag, dict) or remote_tag.get("commit", {}).get("sha") != sha: + raise ValueError("Git tag points to a different or unknown commit") + return True + + def publish(tag, prerelease, paths, api, repo, sha): base = f"{api}/repos/{repo}/releases" tag_url = f"{base}/tags/{urllib.parse.quote(tag, safe='')}" @@ -83,6 +96,7 @@ def publish(tag, prerelease, paths, api, repo, sha): "body": f"Automated Alkane OS build from {sha}. Conformance results are in the repository.", "prerelease": prerelease, } + check_tag_commit(api, repo, tag, sha) release = get_release(tag_url) if release is None: release = json_request(base, "POST", {**metadata, "draft": True}) @@ -99,6 +113,7 @@ def publish(tag, prerelease, paths, api, repo, sha): and asset_digest(asset, api) == digests[name] for name, size in wanted.items() ): + check_tag_commit(api, repo, tag, sha) print(f"Release {tag} is already complete") return if not release.get("draft"): @@ -118,9 +133,12 @@ def publish(tag, prerelease, paths, api, repo, sha): or asset_digest(asset, api) != digests[name] for name, size in wanted.items()): raise ValueError("draft release asset verification failed") + check_tag_commit(api, repo, tag, sha) release = json_request(release_url, "PATCH", {**metadata, "draft": False}) if release.get("draft"): raise ValueError("release remained a draft after publishing") + if not check_tag_commit(api, repo, tag, sha): + raise ValueError("published release did not create the expected Git tag") print(f"Published {tag} with {len(paths)} assets") diff --git a/ci/test-conformance.py b/ci/test-conformance.py index 4c38808..d5eb2e5 100644 --- a/ci/test-conformance.py +++ b/ci/test-conformance.py @@ -70,6 +70,32 @@ class ConformanceGateTest(unittest.TestCase): with self.assertRaisesRegex(ValueError, "Equihash share verifier"): gate.verify_accept({}, "Equihash") + def test_submitted_header_reconstruction_rejects_version_and_extranonce_changes(self): + capture = { + "subscription": {"extranonce1": "aa", "extranonce2_size": 1}, + "notify": {"method": "mining.notify", "params": [ + "job", "11" * 32, "01", "02", [], + "20000000", "1d00ffff", "12345678", True]}, + "configure_response": {"result": { + "version-rolling": True, "version-rolling.mask": "1fffe000"}}, + } + params = ["worker", "job", "bb", "12345678", "00000001", "00002000"] + header, _ = gate.reconstructed_header(capture, params) + self.assertEqual(len(header), 80) + self.assertEqual(header[:4], (0x20002000).to_bytes(4, "little")) + self.assertEqual(header[4:8], b"\x11" * 4) + changed = params.copy() + changed[5] = "ffffffff" + with self.assertRaisesRegex(ValueError, "version bits"): + gate.reconstructed_header(capture, changed) + changed[5] = params[5] + changed[2] = "f" + with self.assertRaisesRegex(ValueError, "Stratum hex"): + gate.reconstructed_header(capture, changed) + changed[2] = "cc" + rebuilt, _ = gate.reconstructed_header(capture, changed) + self.assertNotEqual(rebuilt, header) + if __name__ == "__main__": unittest.main() diff --git a/ci/test-release.py b/ci/test-release.py index ffe0cba..0b999d2 100644 --- a/ci/test-release.py +++ b/ci/test-release.py @@ -4,6 +4,7 @@ import importlib.util import hashlib +import urllib.error from pathlib import Path from tempfile import TemporaryDirectory import unittest @@ -23,8 +24,13 @@ class FakeGitea: self.blobs = {} self.fail_second_upload = True self.uploads = 0 + self.tag_sha = None def request(self, url, method="GET", payload=None, content_type=None): + if "/tags/" in url and "/releases/" not in url: + if self.tag_sha is None: + raise urllib.error.HTTPError(url, 404, "missing", {}, None) + return {"commit": {"sha": self.tag_sha}} if "/assets" in url: if method == "GET": return list(self.assets) @@ -50,6 +56,8 @@ class FakeGitea: return dict(self.release) if method == "PATCH": self.release.update(release.json.loads(payload)) + if not self.release["draft"] and self.tag_sha is None: + self.tag_sha = self.release["target_commitish"] return dict(self.release) raise AssertionError((method, url)) @@ -91,6 +99,21 @@ class ReleaseTest(unittest.TestCase): with self.assertRaisesRegex(ValueError, "distinct"): release.files_to_publish([str(path), str(path)]) + def test_existing_git_tag_mismatch_blocks_release_without_changes(self): + with TemporaryDirectory() as directory: + path = Path(directory) / "image.tar.gz" + path.write_bytes(b"image") + fake = FakeGitea() + fake.tag_sha = "b" * 40 + fake.release = {"id": 7, "tag_name": "v1.2.3", + "target_commitish": "a" * 40, "draft": False} + with patch.object(release, "request", fake.request): + with self.assertRaisesRegex(ValueError, "Git tag points"): + release.publish("v1.2.3", False, [path], "https://api", + "org/repo", "a" * 40) + self.assertEqual(fake.uploads, 0) + self.assertFalse(fake.release["draft"]) + if __name__ == "__main__": unittest.main() diff --git a/src/hal_wire.rs b/src/hal_wire.rs index 3cfe1d3..5a55346 100644 --- a/src/hal_wire.rs +++ b/src/hal_wire.rs @@ -109,6 +109,11 @@ pub struct HalClient { } impl HalClient { + fn poison(&mut self) { + self.poisoned = true; + let _ = self.stream.shutdown(Shutdown::Both); + } + pub fn connect(path: impl AsRef) -> Result { let stream = connect_until(path.as_ref(), Instant::now() + IO_TIMEOUT)?; Self::from_stream(stream) @@ -134,10 +139,9 @@ impl HalClient { let result = self.exchange_inner(opcode, payload); if let Err(ref error) = result { if !matches!(error, HalError::Device(_)) { - self.poisoned = true; + self.poison(); // A backend power lease must expire on disconnect. Reusing a // desynchronized stream could silently discard an off request. - let _ = self.stream.shutdown(Shutdown::Both); } } result @@ -183,8 +187,7 @@ impl HalClient { fn empty_reply(&mut self, opcode: u8, payload: &[u8]) -> Result<(), HalError> { if !self.exchange(opcode, payload)?.is_empty() { - self.poisoned = true; - let _ = self.stream.shutdown(Shutdown::Both); + self.poison(); return Err(HalError::Protocol("unexpected response data")); } Ok(()) @@ -357,12 +360,12 @@ impl HardwareBackend for HalClient { request[1..].copy_from_slice(&(frame.len() as u16).to_le_bytes()); let body = self.exchange(RECEIVE_FRAME, &request)?; if body.len() < 2 { - self.poisoned = true; + self.poison(); return Err(HalError::Protocol("missing receive length")); } let count = u16::from_le_bytes([body[0], body[1]]) as usize; if count > frame.len() || body.len() != count + 2 { - self.poisoned = true; + self.poison(); return Err(HalError::Protocol("invalid received frame size")); } frame[..count].copy_from_slice(&body[2..]); @@ -385,7 +388,7 @@ impl HardwareBackend for HalClient { let bytes: [u8; 4] = match body.try_into() { Ok(bytes) => bytes, Err(_) => { - self.poisoned = true; + self.poison(); return Err(HalError::Protocol("invalid temperature response")); } }; @@ -395,8 +398,7 @@ impl HardwareBackend for HalClient { fn set_psu_enabled(&mut self, enabled: bool) -> Result<(), Self::Error> { let result = self.empty_reply(SET_PSU_ENABLED, &[u8::from(enabled)]); if !enabled && result.is_err() { - self.poisoned = true; - let _ = self.stream.shutdown(Shutdown::Both); + self.poison(); } result } diff --git a/src/miner.rs b/src/miner.rs index 6f55761..6735efc 100644 --- a/src/miner.rs +++ b/src/miner.rs @@ -224,6 +224,14 @@ impl<'a, B: HardwareBackend> Bm1366Miner<'a, B> { hash_le: check.hash_le, })) } + + /// Keep thermal supervision active while pool share replies are pending. + pub fn supervise(&mut self) -> Result<(), MinerError> { + if !self.running { + return Err(MinerError::NotRunning); + } + self.check_temperature() + } } impl Drop for Bm1366Miner<'_, B> { diff --git a/src/stratum_client.rs b/src/stratum_client.rs index 8a43343..912d69a 100644 --- a/src/stratum_client.rs +++ b/src/stratum_client.rs @@ -13,7 +13,8 @@ use std::time::{Duration, Instant}; const MAX_PENDING_SHARES: usize = 32; const SHARE_REPLY_TIMEOUT: Duration = Duration::from_secs(30); -const MINER_POOL_POLL_INTERVAL: Duration = Duration::from_millis(500); +const MAX_REPORTS_PER_CYCLE: usize = 256; +const POOL_WRITE_DEADLINE: Duration = Duration::from_millis(100); #[derive(Debug)] pub enum ClientError { @@ -39,10 +40,10 @@ pub enum PoolEvent { #[derive(Debug, Eq, PartialEq)] pub enum ReportEvent { - None, Stale, BelowTarget, Submitted(u64), + Backpressured, } #[derive(Debug)] @@ -94,8 +95,7 @@ impl PoolConnection { handshake.configure.version_mask, handshake.configure.subscribe_extranonce, )?; - // Pool I/O must not postpone the next ASIC sensor check indefinitely. - wire.set_timeout(MINER_POOL_POLL_INTERVAL); + wire.cap_write_timeout(POOL_WRITE_DEADLINE); Ok(Self { wire, session, @@ -136,18 +136,9 @@ impl PoolConnection { let line = if let Some(line) = self.early.pop_front() { line } else { - match self.wire.read_line() { - Ok(line) => line, - Err(WireError::Timeout) if !self.wire.is_closed() => { - if self - .pending - .first() - .is_some_and(|share| share.sent_at.elapsed() >= SHARE_REPLY_TIMEOUT) - { - return Err(ClientError::ShareReplyTimeout); - } - return Ok(PoolEvent::Idle); - } + match self.wire.try_read_line() { + Ok(Some(line)) => line, + Ok(None) => return Ok(PoolEvent::Idle), Err(error) => return Err(ClientError::Wire(error)), } }; @@ -212,31 +203,41 @@ impl PoolConnection { (self.accepted, self.rejected, self.pending.len()) } - /// Process one pool message or idle interval, then poll the ASIC once. - /// A reported nonce is verified by the miner and session before sending. + /// Process one pool message, then drain a bounded burst of ASIC reports. + /// A reported nonce is verified before sending. The caller must cycle + /// continuously; hardware still requires an independent power watchdog. pub fn cycle( &mut self, miner: &mut Bm1366Miner<'_, B>, - ) -> Result<(PoolEvent, ReportEvent), CycleError> { + ) -> Result<(PoolEvent, Vec), CycleError> { let pool = self.step(miner).map_err(CycleError::Pool)?; - let report = match miner.poll() { - Ok(None) => ReportEvent::None, - Ok(Some(share)) => { - let id = match self.submit_share(share) { - Ok(id) => id, - Err(error) => { - self.wire.close(); - miner.stop().map_err(CycleError::EmergencyShutdown)?; - return Err(CycleError::Submit(error)); - } - }; - ReportEvent::Submitted(id) + let mut reports = Vec::new(); + for _ in 0..MAX_REPORTS_PER_CYCLE { + if self.pending.len() >= MAX_PENDING_SHARES { + miner.supervise().map_err(CycleError::Miner)?; + reports.push(ReportEvent::Backpressured); + break; } - Err(MinerError::StaleReport) => ReportEvent::Stale, - Err(MinerError::BelowTarget) => ReportEvent::BelowTarget, - Err(error) => return Err(CycleError::Miner(error)), - }; - Ok((pool, report)) + let event = match miner.poll() { + Ok(None) => break, + Ok(Some(share)) => { + let id = match self.submit_share(share) { + Ok(id) => id, + Err(error) => { + self.wire.close(); + miner.stop().map_err(CycleError::EmergencyShutdown)?; + return Err(CycleError::Submit(error)); + } + }; + ReportEvent::Submitted(id) + } + Err(MinerError::StaleReport) => ReportEvent::Stale, + Err(MinerError::BelowTarget) => ReportEvent::BelowTarget, + Err(error) => return Err(CycleError::Miner(error)), + }; + reports.push(event); + } + Ok((pool, reports)) } /// Call only after an actual chain reset flushes old hardware reports. @@ -254,12 +255,14 @@ mod tests { use std::io::Write; use std::net::{TcpListener, TcpStream}; use std::rc::Rc; + use std::sync::mpsc; use std::thread; #[derive(Default)] struct Backend { sent: Vec>, powered: Rc>, + reports_remaining: usize, } impl HardwareBackend for Backend { @@ -269,8 +272,15 @@ mod tests { self.sent.push(frame.to_vec()); Ok(()) } - fn receive_frame(&mut self, _chain: u8, _frame: &mut [u8]) -> Result { - Ok(0) + fn receive_frame(&mut self, _chain: u8, frame: &mut [u8]) -> Result { + if self.reports_remaining == 0 { + return Ok(0); + } + self.reports_remaining -= 1; + frame[..11].copy_from_slice(&[ + 0xaa, 0x55, 0x38, 0xff, 0xad, 0xed, 0, 0x18, 0x04, 0xb3, 0x94, + ]); + Ok(11) } fn set_chain_reset(&mut self, _chain: u8, _asserted: bool) -> Result<(), Self::Error> { Ok(()) @@ -290,12 +300,15 @@ mod tests { fn test_link(input: &'static [u8]) -> (PoolConnection, thread::JoinHandle<()>) { let listener = TcpListener::bind("127.0.0.1:0").unwrap(); let address = listener.local_addr().unwrap(); + let (ready, received) = mpsc::channel(); let server = thread::spawn(move || { let (mut stream, _) = listener.accept().unwrap(); stream.write_all(input).unwrap(); + ready.send(()).unwrap(); }); let stream = TcpStream::connect(address).unwrap(); let wire = StratumWire::from_stream(stream, Duration::from_secs(2)).unwrap(); + received.recv_timeout(Duration::from_secs(2)).unwrap(); let handshake = PoolHandshake { subscription: Subscription { extranonce1: vec![1, 2, 3, 4], @@ -330,10 +343,12 @@ mod tests { miner.start().unwrap(); assert!(matches!( link.cycle(&mut miner).unwrap(), - (PoolEvent::Control, ReportEvent::None) + (PoolEvent::Control, reports) if reports.is_empty() )); match link.cycle(&mut miner).unwrap() { - (PoolEvent::Work(work), ReportEvent::None) => assert_eq!(work.work_id, 0), + (PoolEvent::Work(work), reports) if reports.is_empty() => { + assert_eq!(work.work_id, 0) + } _ => panic!("expected job"), } } @@ -419,4 +434,25 @@ mod tests { assert!(!powered.get()); server.join().unwrap(); } + + #[test] + fn idle_pool_does_not_limit_asic_report_burst_to_one() { + let (mut link, server) = + test_link(b"{\"id\":null,\"method\":\"mining.set_difficulty\",\"params\":[1]}\n"); + let mut backend = Backend { + reports_remaining: MAX_REPORTS_PER_CYCLE + 3, + ..Backend::default() + }; + let mut miner = Bm1366Miner::new(&mut backend, 0, 0, 0, 100, 80_000).unwrap(); + miner.start().unwrap(); + let (pool, reports) = link.cycle(&mut miner).unwrap(); + assert!(matches!(pool, PoolEvent::Control)); + assert_eq!(reports.len(), MAX_REPORTS_PER_CYCLE); + assert!(reports + .iter() + .all(|event| matches!(event, ReportEvent::Stale))); + drop(miner); + assert_eq!(backend.reports_remaining, 3); + server.join().unwrap(); + } } diff --git a/src/stratum_session.rs b/src/stratum_session.rs index 099d1d9..82770c8 100644 --- a/src/stratum_session.rs +++ b/src/stratum_session.rs @@ -48,14 +48,21 @@ struct TrackedWork { base_version: u32, } +struct NonceSpace { + template: [u8; 80], + next: [u8; 32], + exhausted: bool, +} + +const MAX_NONCE_SPACES: usize = 4096; + pub struct PoolSession { subscription: Subscription, version_mask: Option, subscribe_extranonce: bool, pending_difficulty: Option, active_difficulty: Option, - extranonce2: [u8; 32], - extranonce_exhausted: bool, + nonce_spaces: Vec, zero_extranonce_headers: Vec<[u8; 80]>, tracked: [Option; 32], next_slot: usize, @@ -76,8 +83,7 @@ impl PoolSession { subscribe_extranonce, pending_difficulty: None, active_difficulty: None, - extranonce2: [0; 32], - extranonce_exhausted: false, + nonce_spaces: Vec::new(), zero_extranonce_headers: Vec::new(), tracked: std::array::from_fn(|_| None), next_slot: 0, @@ -104,8 +110,7 @@ impl PoolSession { } PoolNotification::SetExtranonce(subscription) => { self.subscription = subscription; - self.extranonce2 = [0; 32]; - self.extranonce_exhausted = false; + self.nonce_spaces.clear(); self.zero_extranonce_headers.clear(); self.tracked = std::array::from_fn(|_| None); Ok(true) @@ -140,10 +145,30 @@ impl PoolSession { return Err(SessionError::WorkIdsExhausted); } let size = self.subscription.extranonce2_size; - if size != 0 && self.extranonce_exhausted { + let template = if size == 0 { + None + } else { + Some( + job.work + .nonce_zero_header(&vec![0; size], job.work.ntime) + .map_err(SessionError::Work)?, + ) + }; + let space_index = template.and_then(|header| { + self.nonce_spaces + .iter() + .position(|space| space.template == header) + }); + if size != 0 && space_index.is_none() && self.nonce_spaces.len() == MAX_NONCE_SPACES { return Err(SessionError::ExtranonceExhausted); } - let extranonce2 = self.extranonce2[..size].to_vec(); + let extranonce2 = match space_index { + Some(index) if self.nonce_spaces[index].exhausted => { + return Err(SessionError::ExtranonceExhausted) + } + Some(index) => self.nonce_spaces[index].next[..size].to_vec(), + None => vec![0; size], + }; let header = job .work .nonce_zero_header(&extranonce2, job.work.ntime) @@ -170,8 +195,19 @@ impl PoolSession { if size == 0 { self.zero_extranonce_headers.push(header); } else { + let space = match space_index { + Some(index) => &mut self.nonce_spaces[index], + None => { + self.nonce_spaces.push(NonceSpace { + template: template.expect("nonzero extranonce has a template"), + next: [0; 32], + exhausted: false, + }); + self.nonce_spaces.last_mut().expect("just pushed") + } + }; let mut carry = true; - for byte in self.extranonce2[..size].iter_mut().rev() { + for byte in space.next[..size].iter_mut().rev() { let (new, overflow) = byte.overflowing_add(u8::from(carry)); *byte = new; carry = overflow; @@ -179,7 +215,7 @@ impl PoolSession { break; } } - self.extranonce_exhausted = carry; + space.exhausted = carry; } Ok(IssuedWork { work_id: (slot as u8) << 3, diff --git a/src/stratum_wire.rs b/src/stratum_wire.rs index 98f93d8..a3d04fb 100644 --- a/src/stratum_wire.rs +++ b/src/stratum_wire.rs @@ -13,6 +13,8 @@ use std::time::{Duration, Instant}; const MAX_EARLY_NOTIFICATIONS: usize = 32; const MAX_EARLY_NOTIFICATION_BYTES: usize = 262_144; +const READ_SLICE: Duration = Duration::from_millis(1); +const MAX_PARTIAL_AGE: Duration = Duration::from_secs(2); #[derive(Debug)] pub enum WireError { @@ -42,14 +44,13 @@ pub struct PoolHandshake { pub struct StratumWire { stream: TcpStream, buffered: Vec, + partial_started: Option, timeout: Duration, + write_timeout: Duration, closed: bool, } impl StratumWire { - pub(crate) fn set_timeout(&mut self, timeout: Duration) { - self.timeout = timeout; - } pub fn connect(address: SocketAddr, timeout: Duration) -> Result { if timeout.is_zero() || Instant::now().checked_add(timeout).is_none() { return Err(WireError::InvalidRequest); @@ -59,7 +60,9 @@ impl StratumWire { Ok(Self { stream, buffered: Vec::new(), + partial_started: None, timeout, + write_timeout: timeout, closed: false, }) } @@ -72,7 +75,9 @@ impl StratumWire { Ok(Self { stream, buffered: Vec::new(), + partial_started: None, timeout, + write_timeout: timeout, closed: false, }) } @@ -86,6 +91,10 @@ impl StratumWire { self.closed } + pub(crate) fn cap_write_timeout(&mut self, limit: Duration) { + self.write_timeout = self.write_timeout.min(limit); + } + fn remaining(deadline: Instant) -> Result { deadline .checked_duration_since(Instant::now()) @@ -110,7 +119,9 @@ impl StratumWire { self.close(); return Err(WireError::LineTooLong); } - return Ok(self.buffered.drain(..=end).collect()); + let line = self.buffered.drain(..=end).collect(); + self.partial_started = (!self.buffered.is_empty()).then(Instant::now); + return Ok(line); } if self.buffered.len() >= MAX_POOL_LINE_BYTES { self.close(); @@ -135,7 +146,12 @@ impl StratumWire { self.close(); return Err(WireError::Eof); } - Ok(length) => self.buffered.extend_from_slice(&chunk[..length]), + Ok(length) => { + if self.buffered.is_empty() { + self.partial_started = Some(Instant::now()); + } + self.buffered.extend_from_slice(&chunk[..length]); + } Err(error) => { let error = Self::transport_error(error); if !matches!(error, WireError::Timeout) || !self.buffered.is_empty() { @@ -151,6 +167,70 @@ impl StratumWire { self.read_line_before(Instant::now() + self.timeout) } + /// Check one socket slice without coupling ASIC polling to pool silence. + /// An unfinished line has its own absolute deadline across calls. + pub fn try_read_line(&mut self) -> Result>, WireError> { + if self.closed { + return Err(WireError::Closed); + } + if let Some(line) = self.try_read_line_buffered()? { + return Ok(Some(line)); + } + if self + .partial_started + .is_some_and(|start| start.elapsed() >= MAX_PARTIAL_AGE) + { + self.close(); + return Err(WireError::Timeout); + } + self.stream + .set_read_timeout(Some(READ_SLICE)) + .map_err(WireError::Io)?; + let mut chunk = [0_u8; 4096]; + match self.stream.read(&mut chunk) { + Ok(0) => { + self.close(); + Err(WireError::Eof) + } + Ok(length) => { + if self.buffered.is_empty() { + self.partial_started = Some(Instant::now()); + } + self.buffered.extend_from_slice(&chunk[..length]); + self.try_read_line_buffered() + } + Err(error) + if matches!( + error.kind(), + io::ErrorKind::TimedOut | io::ErrorKind::WouldBlock + ) => + { + Ok(None) + } + Err(error) => { + self.close(); + Err(WireError::Io(error)) + } + } + } + + fn try_read_line_buffered(&mut self) -> Result>, WireError> { + if let Some(end) = self.buffered.iter().position(|byte| *byte == b'\n') { + if end + 1 > MAX_POOL_LINE_BYTES { + self.close(); + return Err(WireError::LineTooLong); + } + let line = self.buffered.drain(..=end).collect(); + self.partial_started = (!self.buffered.is_empty()).then(Instant::now); + return Ok(Some(line)); + } + if self.buffered.len() >= MAX_POOL_LINE_BYTES { + self.close(); + return Err(WireError::LineTooLong); + } + Ok(None) + } + fn send_line_before(&mut self, line: &[u8], deadline: Instant) -> Result<(), WireError> { if self.closed { return Err(WireError::Closed); @@ -191,7 +271,7 @@ impl StratumWire { } pub fn send_line(&mut self, line: &[u8]) -> Result<(), WireError> { - self.send_line_before(line, Instant::now() + self.timeout) + self.send_line_before(line, Instant::now() + self.write_timeout) } fn read_reply( diff --git a/tests/hal_wire.rs b/tests/hal_wire.rs index bbdcd85..5cf5afd 100644 --- a/tests/hal_wire.rs +++ b/tests/hal_wire.rs @@ -113,6 +113,26 @@ fn failed_power_off_ends_backend_power_lease() { worker.join().unwrap(); } +#[test] +fn malformed_temperature_response_disconnects_power_lease() { + let (client, mut server) = UnixStream::pair().unwrap(); + let worker = thread::spawn(move || { + let mut header = [0_u8; 16]; + server.read_exact(&mut header).unwrap(); + let mut request = [0_u8; 1]; + server.read_exact(&mut request).unwrap(); + assert_eq!(header[5], 5); + reply(&mut server, &header, &[0, 1]); + assert_eq!(server.read(&mut request).unwrap(), 0); + }); + let mut backend = HalClient::from_stream(client).unwrap(); + assert!(matches!( + backend.read_temperature_mc(1), + Err(HalError::Protocol("invalid temperature response")) + )); + worker.join().unwrap(); +} + #[test] fn rejects_malformed_error_response_and_poisoned_connection() { for body in [&[4_u8][..], &[3_u8, 99][..]] { diff --git a/tests/stratum_session.rs b/tests/stratum_session.rs index 7dbf6eb..051852f 100644 --- a/tests/stratum_session.rs +++ b/tests/stratum_session.rs @@ -124,6 +124,43 @@ fn zero_length_extranonce_requires_distinct_header_until_hardware_reset() { pool.issue_notify(notify("three", true).as_bytes()).unwrap(); } +#[test] +fn one_byte_extranonce_restarts_only_for_a_distinct_header_template() { + let mut pool = PoolSession::new( + Subscription { + extranonce1: vec![1], + extranonce2_size: 1, + }, + None, + false, + ) + .unwrap(); + pool.control(br#"{"id":null,"method":"mining.set_difficulty","params":[1]}"#) + .unwrap(); + for index in 0..256 { + if index != 0 && index % 32 == 0 { + pool.after_hardware_reset(); + } + pool.issue_notify(notify("same", false).as_bytes()).unwrap(); + } + pool.after_hardware_reset(); + assert_eq!( + pool.issue_notify(notify("same", false).as_bytes()).err(), + Some(SessionError::ExtranonceExhausted) + ); + let distinct = notify("new", false).replace("66554433", "66554434"); + pool.issue_notify(distinct.as_bytes()).unwrap(); + pool.after_hardware_reset(); + for index in 0..256_u32 { + if index != 0 && index % 32 == 0 { + pool.after_hardware_reset(); + } + let ntime = format!("{index:08x}"); + let next = notify("fresh", false).replace("66554433", &ntime); + pool.issue_notify(next.as_bytes()).unwrap(); + } +} + struct FakeBackend { powered: bool, sent: usize,