Pure Rust framing; chain, work attribution, and pool acceptance remain open.
This commit was merged in pull request #6.
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
name: source-pr
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
branches: [next, main]
|
||||
workflow_dispatch:
|
||||
|
||||
concurrency:
|
||||
group: alkane-os-source-pr-${{ github.ref }}
|
||||
cancel-in-progress: true
|
||||
|
||||
jobs:
|
||||
source:
|
||||
runs-on: alkane-ci
|
||||
env:
|
||||
CARGO_TARGET_DIR: target
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Install source formatter
|
||||
run: rustup component add rustfmt
|
||||
- name: Check source formatting
|
||||
run: cargo fmt --check
|
||||
- name: Build source
|
||||
run: cargo build --release --locked
|
||||
- name: Clean generated build
|
||||
if: always()
|
||||
run: python3 ci/cleanup-generated.py
|
||||
@@ -21,6 +21,9 @@ Stratum V1. Tests have replayed 33 captured BM1366 shares and one BM1362
|
||||
share to a loopback pool with matching ACCEPT responses. Production board
|
||||
control is still under development.
|
||||
|
||||
The BM1370 module encodes a captured S21 Pro job and parses captured nonce
|
||||
frames. Work-ID mapping, chain control, and pool acceptance remain open.
|
||||
|
||||
The HAL client requests PSU shutdown on sensor faults and reserves work IDs
|
||||
until a confirmed reset or shutdown. A production backend still needs a
|
||||
power lease, watchdog, and validated temperature limits. SHA-256d and
|
||||
|
||||
+186
@@ -0,0 +1,186 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
//! BM1370 wire frames observed on an S21 Pro. This module does not control power.
|
||||
|
||||
use crate::bm13xx::{crc16, crc5};
|
||||
|
||||
pub use crate::bm1362::Job;
|
||||
|
||||
pub const JOB_BYTES: usize = 88;
|
||||
pub const REPORT_BYTES: usize = 11;
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub struct NonceReport {
|
||||
pub nonce: u32,
|
||||
pub byte6: u8,
|
||||
pub byte7: u8,
|
||||
pub byte8: u8,
|
||||
pub byte9: u8,
|
||||
/// Raw trailer: bit 7 marks a nonce response; bits 4:0 carry CRC5.
|
||||
/// No chip identity is inferred from this byte.
|
||||
pub trailer: u8,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub enum ReportError {
|
||||
Length,
|
||||
Preamble,
|
||||
RegisterResponse,
|
||||
Checksum,
|
||||
}
|
||||
|
||||
/// Encode only the job shape observed in the S21 Pro capture. `Job` fields
|
||||
/// are in wire order; this does not validate a pool header or start a chain.
|
||||
pub fn encode_job(job: &Job) -> Option<[u8; JOB_BYTES]> {
|
||||
if job.work_id & 7 != 0 || job.num_midstates != 1 || job.start_nonce != 0 {
|
||||
return None;
|
||||
}
|
||||
let mut frame = crate::bm1362::encode_job(job);
|
||||
frame[3] = 0x36;
|
||||
let checksum = crc16(&frame[2..86]);
|
||||
frame[86..88].copy_from_slice(&checksum.to_be_bytes());
|
||||
Some(frame)
|
||||
}
|
||||
|
||||
/// Keep the five trailing bytes raw until work-ID and version-roll mapping is
|
||||
/// validated against silicon. The marker bit distinguishes nonce reports from
|
||||
/// register replies in the observed capture.
|
||||
pub fn decode_nonce_report(frame: &[u8]) -> Result<NonceReport, ReportError> {
|
||||
if frame.len() != REPORT_BYTES {
|
||||
return Err(ReportError::Length);
|
||||
}
|
||||
if frame[..2] != [0xaa, 0x55] {
|
||||
return Err(ReportError::Preamble);
|
||||
}
|
||||
if frame[10] & 0x80 == 0 {
|
||||
return Err(ReportError::RegisterResponse);
|
||||
}
|
||||
if crc5(&frame[2..]) != 0 {
|
||||
return Err(ReportError::Checksum);
|
||||
}
|
||||
Ok(NonceReport {
|
||||
nonce: u32::from_le_bytes(frame[2..6].try_into().expect("four-byte nonce")),
|
||||
byte6: frame[6],
|
||||
byte7: frame[7],
|
||||
byte8: frame[8],
|
||||
byte9: frame[9],
|
||||
trailer: frame[10],
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
// firmware-analysis/prong-b/s21-192/c2-steady.strace, SHA-256
|
||||
// c0aac1222e05e95bdcbd1de5a8ee244a581256e8f28e3dadf5300df6110229c7.
|
||||
const CAPTURED_JOB: &str = concat!(
|
||||
"55aa21366001000000008f060217834f276af0f09aafc588165b7fbfc56f8f09a55d3d0de5",
|
||||
"798bd87460278af180c4696fc200000000000000008af701003acc10cdebe5bc3c90f095e5",
|
||||
"03c6b7de69ca8b5d00000020e769"
|
||||
);
|
||||
|
||||
fn captured_bytes<const N: usize>(hex: &str) -> [u8; N] {
|
||||
assert_eq!(hex.len(), N * 2);
|
||||
let mut bytes = [0; N];
|
||||
for (index, pair) in hex.as_bytes().chunks_exact(2).enumerate() {
|
||||
bytes[index] = u8::from_str_radix(std::str::from_utf8(pair).unwrap(), 16).unwrap();
|
||||
}
|
||||
bytes
|
||||
}
|
||||
|
||||
fn with_valid_crc(mut frame: [u8; REPORT_BYTES]) -> [u8; REPORT_BYTES] {
|
||||
for checksum in 0..32 {
|
||||
frame[10] = (frame[10] & 0xe0) | checksum;
|
||||
if crc5(&frame[2..]) == 0 {
|
||||
return frame;
|
||||
}
|
||||
}
|
||||
panic!("no valid CRC5 trailer");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn live_job_matches_all_88_bytes() {
|
||||
let captured = captured_bytes::<JOB_BYTES>(CAPTURED_JOB);
|
||||
let mut merkle_root = [0; 32];
|
||||
merkle_root.copy_from_slice(&captured[18..50]);
|
||||
let mut previous_hash = [0; 32];
|
||||
previous_hash.copy_from_slice(&captured[50..82]);
|
||||
let job = Job {
|
||||
work_id: captured[4],
|
||||
num_midstates: captured[5],
|
||||
start_nonce: u32::from_le_bytes(captured[6..10].try_into().unwrap()),
|
||||
nbits: u32::from_le_bytes(captured[10..14].try_into().unwrap()),
|
||||
ntime: u32::from_le_bytes(captured[14..18].try_into().unwrap()),
|
||||
merkle_root,
|
||||
previous_hash,
|
||||
version: u32::from_le_bytes(captured[82..86].try_into().unwrap()),
|
||||
};
|
||||
assert_eq!(encode_job(&job), Some(captured));
|
||||
let mut invalid = job;
|
||||
invalid.work_id |= 1;
|
||||
assert_eq!(encode_job(&invalid), None);
|
||||
invalid = job;
|
||||
invalid.num_midstates = 2;
|
||||
assert_eq!(encode_job(&invalid), None);
|
||||
invalid = job;
|
||||
invalid.start_nonce = 1;
|
||||
assert_eq!(encode_job(&invalid), None);
|
||||
assert_eq!(
|
||||
crc16(&captured[2..86]),
|
||||
u16::from_be_bytes([captured[86], captured[87]])
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn live_nonce_reports_preserve_raw_tags() {
|
||||
for hex in [
|
||||
"aa555804a92a041e133e86",
|
||||
"aa55771def23001c16fc82",
|
||||
"aa5574480ae6021417b492",
|
||||
] {
|
||||
let frame = captured_bytes::<REPORT_BYTES>(hex);
|
||||
let report = decode_nonce_report(&frame).unwrap();
|
||||
assert_eq!(
|
||||
report.nonce,
|
||||
u32::from_le_bytes(frame[2..6].try_into().unwrap())
|
||||
);
|
||||
assert_eq!(report.byte6, frame[6]);
|
||||
assert_eq!(report.byte7, frame[7]);
|
||||
assert_eq!(report.byte8, frame[8]);
|
||||
assert_eq!(report.byte9, frame[9]);
|
||||
assert_eq!(report.trailer, frame[10]);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalid_report_fails_closed() {
|
||||
let mut frame = captured_bytes::<REPORT_BYTES>("aa555804a92a041e133e86");
|
||||
assert_eq!(decode_nonce_report(&frame[..10]), Err(ReportError::Length));
|
||||
frame[0] = 0;
|
||||
assert_eq!(decode_nonce_report(&frame), Err(ReportError::Preamble));
|
||||
frame[0] = 0xaa;
|
||||
frame[10] &= 0x60;
|
||||
frame = with_valid_crc(frame);
|
||||
assert_eq!(crc5(&frame[2..]), 0);
|
||||
assert_eq!(
|
||||
decode_nonce_report(&frame),
|
||||
Err(ReportError::RegisterResponse)
|
||||
);
|
||||
frame[10] |= 0x80;
|
||||
frame = with_valid_crc(frame);
|
||||
frame[4] ^= 1;
|
||||
assert_eq!(decode_nonce_report(&frame), Err(ReportError::Checksum));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn trailer_low_bits_are_checksum_not_chip_identity() {
|
||||
let original = captured_bytes::<REPORT_BYTES>("aa555804a92a041e133e86");
|
||||
let mut changed_nonce = original;
|
||||
changed_nonce[2] ^= 1;
|
||||
changed_nonce = with_valid_crc(changed_nonce);
|
||||
assert_eq!(crc5(&changed_nonce[2..]), 0);
|
||||
assert_ne!(changed_nonce[10] & 0x1f, original[10] & 0x1f);
|
||||
assert_eq!(changed_nonce[10] & 0xe0, original[10] & 0xe0);
|
||||
assert!(decode_nonce_report(&changed_nonce).is_ok());
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@
|
||||
|
||||
pub mod bm1362;
|
||||
pub mod bm1366;
|
||||
pub mod bm1370;
|
||||
pub mod bm13xx;
|
||||
pub mod difficulty;
|
||||
pub mod hal_wire;
|
||||
|
||||
Reference in New Issue
Block a user