// SPDX-License-Identifier: MIT //! Bounded host-only check: captured BM13xx report through HAL to a strict pool. use alkane_os::hal_wire::HalClient; use alkane_os::miner::Bm1366Miner; use alkane_os::stratum_client::{PoolConnection, PoolEvent, ReportEvent}; use std::error::Error; use std::io; use std::net::SocketAddr; use std::time::{Duration, Instant}; const CAPTURED_HEADER: [u8; 80] = [ 0x00, 0x00, 0x00, 0x20, 0xfc, 0x3a, 0xb8, 0xd4, 0x30, 0x21, 0xf4, 0x5e, 0xb6, 0x62, 0x38, 0x9d, 0x0f, 0x79, 0x1f, 0x65, 0x7d, 0xbf, 0x75, 0xa5, 0xcb, 0x93, 0x67, 0x0b, 0x57, 0xb6, 0xc2, 0x25, 0x03, 0xb4, 0x21, 0x3a, 0x43, 0xae, 0x10, 0x52, 0xeb, 0xd4, 0x44, 0x43, 0x06, 0x8f, 0xe1, 0x78, 0xe2, 0xc6, 0x1c, 0xa4, 0x99, 0x48, 0x46, 0xfd, 0x13, 0x3a, 0xdd, 0xb0, 0x4a, 0xab, 0x2b, 0x38, 0x2c, 0x5b, 0x4c, 0x74, 0xc1, 0x01, 0x2e, 0x6a, 0x8f, 0x06, 0x02, 0x17, 0x00, 0x00, 0x00, 0x00, ]; const BM1362_HEADER_HEX: &str = concat!( "00000020fc3ab8d43021f45eb662389d0f791f657dbf75a5cb93670b57b6c22503", "b4213a0503969b7384e1215aace8cfc65dae22d8493ad48fec0b7ac3b0ad7b4e7", "fb0c5c0012e6a8f06021700000000" ); fn bm1362_header() -> Result<[u8; 80], io::Error> { let mut header = [0_u8; 80]; for (index, pair) in BM1362_HEADER_HEX.as_bytes().chunks_exact(2).enumerate() { let digits = std::str::from_utf8(pair).map_err(io::Error::other)?; header[index] = u8::from_str_radix(digits, 16).map_err(io::Error::other)?; } Ok(header) } fn failure(message: impl Into) -> io::Error { io::Error::other(message.into()) } fn main() -> Result<(), Box> { let mut args = std::env::args().skip(1); let socket = args.next().ok_or_else(|| { failure("usage: simulator_pool_replay_probe HAL_SOCKET POOL_ADDRESS [SHARES] [CHIP]") })?; let address: SocketAddr = args .next() .ok_or_else(|| failure("missing pool address"))? .parse()?; let expected_accepts: usize = args.next().map_or(Ok(1), |value| value.parse())?; if !(1..=64).contains(&expected_accepts) { return Err(failure("SHARES must be between 1 and 64").into()); } let chip = args.next().unwrap_or_else(|| "BM1366".to_owned()); let first_header = match chip.as_str() { "BM1366" => CAPTURED_HEADER, "BM1362" if expected_accepts == 1 => bm1362_header()?, "BM1362" => return Err(failure("BM1362 has one captured header").into()), _ => return Err(failure("unsupported captured chip").into()), }; if args.next().is_some() { return Err(failure("unexpected argument").into()); } if !address.ip().is_loopback() { return Err(failure("probe pool must be on loopback").into()); } let mut backend = HalClient::connect(socket)?; let mut miner = Bm1366Miner::new(&mut backend, 0, 0, 0, 100, 80_000) .ok_or_else(|| failure("invalid simulator configuration"))?; let mut pool = PoolConnection::connect( address, "alkane-simulator-probe", "alkane.test", "x", Duration::from_secs(2), Duration::from_secs(5), ) .map_err(|error| failure(format!("pool handshake: {error:?}")))?; miner.start()?; let deadline = Instant::now() + Duration::from_secs(10 + 2 * expected_accepts as u64); let mut issued = 0; let mut submitted = 0; let mut accepted = 0; while Instant::now() < deadline { let (event, reports) = pool .cycle(&mut miner) .map_err(|error| failure(format!("mining cycle: {error:?}")))?; submitted += reports .iter() .filter(|report| matches!(report, ReportEvent::Submitted(_))) .count(); match event { PoolEvent::Work(work) => { if issued == 0 && work.header != first_header { return Err(failure("pool job differs from real-silicon capture").into()); } issued += 1; } PoolEvent::ShareAccepted(_) => { if accepted >= submitted || accepted >= issued { return Err(failure("unattributed pool acceptance").into()); } accepted += 1; if accepted == expected_accepts { miner.stop()?; println!( "{chip} real captured nonce accepted by strict loopback pool ({accepted} shares)" ); return Ok(()); } } PoolEvent::ShareRejected(_) => return Err(failure("pool rejected share").into()), _ => {} } } Err(failure("captured share was not accepted before deadline").into()) }