Flush ASIC work before recycling job IDs
next-build / build (push) Canceled after 0s

This commit is contained in:
2026-10-15 03:55:13 -06:00
parent 2d6233cdc7
commit a1d8e34390
4 changed files with 108 additions and 6 deletions
+4
View File
@@ -71,6 +71,10 @@ the connection so a caller can reconnect instead of filling the pending
queue indefinitely. Terminal pool errors request miner shutdown, and pool
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.
When the 32 hardware work IDs are exhausted, the client checks temperature,
resets the chain, discards old reports, and only then reuses an ID for the next
job. A reset failure requests PSU shutdown. A host test dispatches 33 jobs
and checks the reset boundary; sustained board operation is still unverified.
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,
+19
View File
@@ -146,6 +146,25 @@ impl<'a, B: HardwareBackend> Bm1366Miner<'a, B> {
self.power_off()
}
/// Flush the ASIC's in-flight work before reusing any work IDs. A reset
/// failure shuts the PSU down because old reports may still arrive.
pub fn reset_chain(&mut self) -> Result<(), MinerError<B::Error>> {
if !self.running {
return Err(MinerError::NotRunning);
}
self.work = [None; 32];
self.check_temperature()?;
if let Err(error) = self.backend.set_chain_reset(self.chain, true) {
let _ = self.power_off();
return Err(MinerError::Backend(error));
}
if let Err(error) = self.backend.set_chain_reset(self.chain, false) {
let _ = self.power_off();
return Err(MinerError::Backend(error));
}
Ok(())
}
pub fn submit(
&mut self,
header: [u8; 80],
+58 -6
View File
@@ -161,11 +161,19 @@ impl PoolConnection {
return Err(ClientError::Pool(PoolMessageError::WrongMessage));
}
match message.get("method").and_then(Value::as_str) {
Some("mining.notify") => self
.session
.issue_to_miner(miner, &line)
.map(PoolEvent::Work)
.map_err(ClientError::Dispatch),
Some("mining.notify") => {
let issued = match self.session.issue_to_miner(miner, &line) {
Err(DispatchError::Session(SessionError::WorkIdsExhausted)) => {
miner
.reset_chain()
.map_err(|error| ClientError::Dispatch(DispatchError::Miner(error)))?;
self.after_hardware_reset(miner);
self.session.issue_to_miner(miner, &line)
}
result => result,
};
issued.map(PoolEvent::Work).map_err(ClientError::Dispatch)
}
Some("mining.set_difficulty" | "mining.set_version_mask" | "mining.set_extranonce") => {
self.session
.control_with_miner(miner, &line)
@@ -263,6 +271,7 @@ mod tests {
sent: Vec<Vec<u8>>,
powered: Rc<Cell<bool>>,
reports_remaining: usize,
reset_assertions: usize,
}
impl HardwareBackend for Backend {
@@ -282,7 +291,10 @@ mod tests {
]);
Ok(11)
}
fn set_chain_reset(&mut self, _chain: u8, _asserted: bool) -> Result<(), Self::Error> {
fn set_chain_reset(&mut self, _chain: u8, asserted: bool) -> Result<(), Self::Error> {
if asserted {
self.reset_assertions += 1;
}
Ok(())
}
fn set_fan_target(&mut self, _fan: u8, _percent: u8) -> Result<(), Self::Error> {
@@ -356,6 +368,46 @@ mod tests {
server.join().unwrap();
}
#[test]
fn thirty_third_job_flushes_asic_before_reusing_work_id() {
let (mut link, server) = test_link(b"");
link.early.push_back(
b"{\"id\":null,\"method\":\"mining.set_difficulty\",\"params\":[1]}\n".to_vec(),
);
let notify = concat!(
"{\"id\":null,\"method\":\"mining.notify\",\"params\":[\"job\",",
"\"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f\",",
"\"616263\",\"78797a\",[],\"20000000\",\"1d00ffff\",\"66554433\",true]}\n"
);
for _ in 0..33 {
link.early.push_back(notify.as_bytes().to_vec());
}
let mut backend = Backend::default();
let powered = Rc::clone(&backend.powered);
let mut miner = Bm1366Miner::new(&mut backend, 0, 0, 0, 100, 80_000).unwrap();
miner.start().unwrap();
assert!(matches!(link.step(&mut miner), Ok(PoolEvent::Control)));
let mut first_header = None;
for index in 0..33 {
let PoolEvent::Work(work) = link.step(&mut miner).unwrap() else {
panic!("expected work");
};
if index == 0 {
first_header = Some(work.header);
}
if index == 32 {
assert_eq!(work.work_id, 0);
assert_ne!(Some(work.header), first_header);
}
}
assert!(powered.get());
miner.stop().unwrap();
drop(miner);
assert_eq!(backend.reset_assertions, 2);
assert_eq!(backend.sent.len(), 33);
server.join().unwrap();
}
#[test]
fn share_replies_match_pending_ids_and_count_accepts_separately() {
let (mut link, server) = test_link(
+27
View File
@@ -27,6 +27,8 @@ struct FakeBackend {
powered: bool,
fail_off_call: usize,
off_calls: usize,
fail_reset_call: usize,
reset_calls: usize,
report: Option<[u8; REPORT_BYTES]>,
additional_reports: Vec<[u8; REPORT_BYTES]>,
sent: Vec<u8>,
@@ -54,6 +56,10 @@ impl HardwareBackend for FakeBackend {
fn set_chain_reset(&mut self, _chain: u8, asserted: bool) -> Result<(), Self::Error> {
assert!(self.powered);
self.reset_calls += 1;
if self.reset_calls == self.fail_reset_call {
return Err("reset failed");
}
self.events
.push(if asserted { "reset-on" } else { "reset-off" });
Ok(())
@@ -218,3 +224,24 @@ fn failed_power_off_is_retried_on_drop() {
assert_eq!(backend.off_calls, 3);
assert!(!backend.powered);
}
#[test]
fn failed_chain_flush_powers_off_before_work_id_reuse() {
let mut backend = FakeBackend {
temperatures: vec![50_000, 50_000],
fail_reset_call: 3,
..Default::default()
};
let mut miner = Bm1366Miner::new(&mut backend, 0, 0, 0, 100, 80_000).unwrap();
miner.start().unwrap();
assert_eq!(
miner.reset_chain(),
Err(MinerError::Backend("reset failed"))
);
assert_eq!(
miner.submit(HEADER, diff_one(), 0),
Err(MinerError::NotRunning)
);
drop(miner);
assert!(!backend.powered);
}