From 289ad40d86b7253eedff21a85fe4279156939edc Mon Sep 17 00:00:00 2001 From: noah Date: Fri, 16 Oct 2026 08:11:46 -0600 Subject: [PATCH] Select BM1362 work encoding for captured replay --- README.md | 5 ++-- examples/simulator_replay_probe.rs | 14 ++++----- src/bm1362.rs | 20 ++++++++++++- src/bm1366.rs | 10 +------ src/bm13xx.rs | 9 ++++++ src/main.rs | 11 +++++-- src/miner.rs | 46 ++++++++++++++++++++++++++---- tests/miner.rs | 9 ++++-- 8 files changed, 92 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index b38a69f..2a1d03c 100644 --- a/README.md +++ b/README.md @@ -39,8 +39,9 @@ matches Litecoin's BM1489/BM1491 report handling and L7/L9 mining remain open. For a simulator HAL socket and a Stratum V1 pool, set `ALKANE_POOL_PASSWORD` -and run `alkane-os --mine-simulator HAL_SOCKET POOL_ADDRESS WORKER`. The -optional fourth argument stops after that many accepted shares. The command +and run `alkane-os --mine-simulator HAL_SOCKET POOL_ADDRESS WORKER BM1366`. +Select `BM1362` for that chip's captured job format. The optional argument +after the chip stops after that many accepted shares. The command uses the simulator's 80 C temperature limit and exits on a pool or hardware fault. The SDK HAL server must cut power when its client stalls or disconnects. diff --git a/examples/simulator_replay_probe.rs b/examples/simulator_replay_probe.rs index ff17b83..859422e 100644 --- a/examples/simulator_replay_probe.rs +++ b/examples/simulator_replay_probe.rs @@ -2,7 +2,7 @@ //! Exercise the SDK's host-only BM13xx HAL simulator with a captured job. use alkane_os::hal_wire::HalClient; -use alkane_os::miner::Bm1366Miner; +use alkane_os::miner::{Bm1366Miner, ChipModel}; use std::error::Error; use std::io; @@ -38,16 +38,14 @@ fn main() -> Result<(), Box> { if args.next().is_some() { return Err("unexpected argument".into()); } - let (header, nonce) = match chip.as_str() { - "BM1366" => (HEADER, 0xedad_ff38), - "BM1362" => (bm1362_header()?, 0x630b_2533), + let (header, nonce, model) = match chip.as_str() { + "BM1366" => (HEADER, 0xedad_ff38, ChipModel::Bm1366), + "BM1362" => (bm1362_header()?, 0x630b_2533, ChipModel::Bm1362), _ => return Err("unsupported captured chip".into()), }; let mut backend = HalClient::connect(path)?; - // BM1362 and BM1366 share the observed 88-byte job and 11-byte report - // framing exercised by this miner. Chip initialization is outside this - // host-only check. - let mut miner = Bm1366Miner::new(&mut backend, 0, 0, 0, 100, 80_000) + // Chip initialization is outside this host-only check. + let mut miner = Bm1366Miner::new_for_chip(&mut backend, model, 0, 0, 0, 100, 80_000) .ok_or("invalid simulator probe configuration")?; miner.start()?; let mut target = [0_u8; 32]; diff --git a/src/bm1362.rs b/src/bm1362.rs index c802c40..79406f6 100644 --- a/src/bm1362.rs +++ b/src/bm1362.rs @@ -4,7 +4,7 @@ //! These pure encoders do not initialize a chain. PLL fields, voltage, and //! model-specific operating points still require silicon-backed validation. -use crate::bm13xx::{crc16, crc5}; +use crate::bm13xx::{crc16, crc5, reverse_words}; pub const JOB_BYTES: usize = 88; pub const CHIP_ADDRESS_STEP: u8 = 2; @@ -120,6 +120,24 @@ pub fn encode_job(job: &Job) -> [u8; JOB_BYTES] { frame } +/// Encode one nonce-zero pool header in the observed raw-header job layout. +/// This covers work delivery; chain initialization still needs board evidence. +pub fn encode_work(header: &[u8; 80], work_id: u8) -> Option<[u8; JOB_BYTES]> { + if header[76..80] != [0; 4] || work_id & 7 != 0 { + return None; + } + Some(encode_job(&Job { + work_id, + num_midstates: 1, + start_nonce: 0, + nbits: u32::from_le_bytes(header[72..76].try_into().expect("nbits")), + ntime: u32::from_le_bytes(header[68..72].try_into().expect("ntime")), + merkle_root: reverse_words(&header[36..68]), + previous_hash: reverse_words(&header[4..36]), + version: u32::from_le_bytes(header[..4].try_into().expect("version")), + })) +} + /// BT8D for a 25 MHz input clock with BCLK_SEL=0. A model must still choose /// the actual target baud and confirm switching on silicon. pub fn bt8d_for_baud(baud: u32) -> Option { diff --git a/src/bm1366.rs b/src/bm1366.rs index ab27598..2f01afe 100644 --- a/src/bm1366.rs +++ b/src/bm1366.rs @@ -5,7 +5,7 @@ pub const WORK_BYTES: usize = 88; pub const REPORT_BYTES: usize = 11; pub use crate::bm13xx::crc16; -use crate::bm13xx::crc5; +use crate::bm13xx::{crc5, reverse_words}; #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub struct NonceReport { @@ -23,14 +23,6 @@ pub enum ReportError { Checksum, } -fn reverse_words(input: &[u8]) -> [u8; 32] { - let mut output = [0_u8; 32]; - for index in 0..8 { - output[index * 4..index * 4 + 4].copy_from_slice(&input[(7 - index) * 4..(8 - index) * 4]); - } - output -} - /// Encode a nonce-zero Bitcoin header for the observed 88-byte SEND_JOB form. /// The low three work-ID bits are reserved by the BM1366 response format. pub fn encode_work(header: &[u8; 80], work_id: u8) -> Option<[u8; WORK_BYTES]> { diff --git a/src/bm13xx.rs b/src/bm13xx.rs index c07d1ec..012469c 100644 --- a/src/bm13xx.rs +++ b/src/bm13xx.rs @@ -1,6 +1,15 @@ // SPDX-License-Identifier: MIT //! Shared BM13xx wire checksums. This module never generates mining nonces. +/// Reverse the eight 32-bit header words for the observed SEND_JOB layout. +pub(crate) fn reverse_words(input: &[u8]) -> [u8; 32] { + let mut output = [0_u8; 32]; + for index in 0..8 { + output[index * 4..index * 4 + 4].copy_from_slice(&input[(7 - index) * 4..(8 - index) * 4]); + } + output +} + /// CRC5 used by command and nonce-report frames, initial value 0x1f. pub fn crc5(bytes: &[u8]) -> u8 { let mut state = 0x1f_u8; diff --git a/src/main.rs b/src/main.rs index 78a2d36..7752c81 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ // SPDX-License-Identifier: MIT use alkane_os::hal_wire::HalClient; -use alkane_os::miner::Bm1366Miner; +use alkane_os::miner::{Bm1366Miner, ChipModel}; use alkane_os::stratum_client::{PoolConnection, PoolEvent}; use alkane_os::target_matrix; use std::error::Error; @@ -12,6 +12,11 @@ fn sim_mine(mut args: impl Iterator) -> Result<(), Box let socket = args.next().ok_or("missing HAL socket")?; let address: SocketAddr = args.next().ok_or("missing pool address")?.parse()?; let worker = args.next().ok_or("missing pool worker")?; + let chip = match args.next().as_deref() { + Some("BM1362") => ChipModel::Bm1362, + Some("BM1366") => ChipModel::Bm1366, + _ => return Err("select BM1362 or BM1366".into()), + }; let max_accepts: Option = args.next().map(|value| value.parse()).transpose()?; if args.next().is_some() || worker.is_empty() || max_accepts == Some(0) { return Err("invalid simulator mining arguments".into()); @@ -31,7 +36,7 @@ fn sim_mine(mut args: impl Iterator) -> Result<(), Box ) .map_err(|error| io::Error::other(format!("pool handshake: {error:?}")))?; let mut backend = HalClient::connect(socket)?; - let mut miner = Bm1366Miner::new(&mut backend, 0, 0, 0, 100, 80_000) + let mut miner = Bm1366Miner::new_for_chip(&mut backend, chip, 0, 0, 0, 100, 80_000) .ok_or("invalid simulator safety configuration")?; miner.start()?; let mut accepted = 0_u64; @@ -77,7 +82,7 @@ fn main() { } } _ => { - eprintln!("usage: alkane-os --list-targets | --version | --mine-simulator HAL_SOCKET POOL_ADDRESS WORKER [MAX_ACCEPTS]"); + eprintln!("usage: alkane-os --list-targets | --version | --mine-simulator HAL_SOCKET POOL_ADDRESS WORKER BM1362|BM1366 [MAX_ACCEPTS]"); std::process::exit(2); } } diff --git a/src/miner.rs b/src/miner.rs index c6e04a4..81b8a1d 100644 --- a/src/miner.rs +++ b/src/miner.rs @@ -1,12 +1,19 @@ // SPDX-License-Identifier: MIT -//! One-chain BM1366 mining control built on the SDK hardware backend. +//! One-chain BM13xx mining control built on the SDK hardware backend. //! This module never searches for nonces or chooses board safety thresholds. +use crate::bm1362; use crate::bm1366::{decode_nonce_report, encode_work, ReportError, REPORT_BYTES}; use crate::pow::verify_sha256_report; use crate::{power_decision, HardwareBackend, PowerDecision}; use std::fmt; +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub enum ChipModel { + Bm1362, + Bm1366, +} + #[derive(Debug, Eq, PartialEq)] pub enum MinerError { Backend(E), @@ -28,11 +35,11 @@ impl fmt::Display for MinerError { Self::AlreadyRunning => f.write_str("miner is already running"), Self::NotRunning => f.write_str("miner is not running"), Self::Faulted => f.write_str("miner shutdown is unconfirmed"), - Self::WorkIdInUse(id) => write!(f, "BM1366 work ID {id} is still in use"), + Self::WorkIdInUse(id) => write!(f, "BM13xx work ID {id} is still in use"), Self::UnsafeTemperature(value) => write!(f, "unsafe temperature: {value} mC"), - Self::InvalidWork => f.write_str("invalid BM1366 work"), - Self::InvalidReport(error) => write!(f, "invalid BM1366 report: {error:?}"), - Self::StaleReport => f.write_str("stale BM1366 work ID"), + Self::InvalidWork => f.write_str("invalid BM13xx work"), + Self::InvalidReport(error) => write!(f, "invalid BM13xx report: {error:?}"), + Self::StaleReport => f.write_str("stale BM13xx work ID"), Self::BelowTarget => f.write_str("nonce is below the pool target"), } } @@ -57,6 +64,7 @@ struct Work { pub struct Bm1366Miner<'a, B: HardwareBackend> { backend: &'a mut B, + chip: ChipModel, chain: u8, sensor: u8, fan: u8, @@ -77,12 +85,34 @@ impl<'a, B: HardwareBackend> Bm1366Miner<'a, B> { fan: u8, fan_percent: u8, max_temperature_mc: i32, + ) -> Option { + Self::new_for_chip( + backend, + ChipModel::Bm1366, + chain, + sensor, + fan, + fan_percent, + max_temperature_mc, + ) + } + + /// Select the captured BM1362 or BM1366 work format explicitly. + pub fn new_for_chip( + backend: &'a mut B, + chip: ChipModel, + chain: u8, + sensor: u8, + fan: u8, + fan_percent: u8, + max_temperature_mc: i32, ) -> Option { if fan_percent == 0 || fan_percent > 100 || max_temperature_mc <= 0 { return None; } Some(Self { backend, + chip, chain, sensor, fan, @@ -193,7 +223,11 @@ impl<'a, B: HardwareBackend> Bm1366Miner<'a, B> { return Err(MinerError::Faulted); } self.check_temperature()?; - let frame = encode_work(&header, work_id).ok_or(MinerError::InvalidWork)?; + let frame = match self.chip { + ChipModel::Bm1362 => bm1362::encode_work(&header, work_id), + ChipModel::Bm1366 => encode_work(&header, work_id), + } + .ok_or(MinerError::InvalidWork)?; let slot = usize::from(work_id >> 3); if self.used_work_ids[slot] { return Err(MinerError::WorkIdInUse(work_id)); diff --git a/tests/miner.rs b/tests/miner.rs index 0233db7..644fbf4 100644 --- a/tests/miner.rs +++ b/tests/miner.rs @@ -1,6 +1,6 @@ // SPDX-License-Identifier: MIT use alkane_os::bm1366::REPORT_BYTES; -use alkane_os::miner::{Bm1366Miner, MinerError}; +use alkane_os::miner::{Bm1366Miner, ChipModel, MinerError}; use alkane_os::HardwareBackend; const HEADER: [u8; 80] = [ @@ -125,7 +125,7 @@ fn captured_nonce_passes_a_started_miner() { } #[test] -fn real_bm1362_capture_uses_shared_bm13xx_work_and_report_frames() { +fn real_bm1362_capture_uses_its_job_format_and_shared_report() { // SDK capture bm1362-real-nonces.txt: one real-silicon pool-accepted share. // This checks host framing and proof only; board initialization is separate. let header_hex = concat!( @@ -146,7 +146,9 @@ fn real_bm1362_capture_uses_shared_bm13xx_work_and_report_frames() { ..Default::default() }; { - let mut miner = Bm1366Miner::new(&mut backend, 0, 0, 0, 100, 80_000).unwrap(); + let mut miner = + Bm1366Miner::new_for_chip(&mut backend, ChipModel::Bm1362, 0, 0, 0, 100, 80_000) + .unwrap(); miner.start().unwrap(); miner.submit(header, diff_one(), 0x18).unwrap(); let share = miner.poll().unwrap().unwrap(); @@ -156,6 +158,7 @@ fn real_bm1362_capture_uses_shared_bm13xx_work_and_report_frames() { miner.stop().unwrap(); } assert!(!backend.powered); + assert_eq!(&backend.sent[..6], &[0x55, 0xaa, 0x21, 0x56, 0x18, 1]); assert_eq!(&backend.sent[10..14], &header[72..76]); }