90 lines
3.4 KiB
Rust
90 lines
3.4 KiB
Rust
// SPDX-License-Identifier: MIT
|
|
use alkane_os::hal_wire::HalClient;
|
|
use alkane_os::miner::{Bm1366Miner, ChipModel};
|
|
use alkane_os::stratum_client::{PoolConnection, PoolEvent};
|
|
use alkane_os::target_matrix;
|
|
use std::error::Error;
|
|
use std::io;
|
|
use std::net::SocketAddr;
|
|
use std::time::Duration;
|
|
|
|
fn sim_mine(mut args: impl Iterator<Item = String>) -> Result<(), Box<dyn Error>> {
|
|
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<u64> = 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());
|
|
}
|
|
let password = std::env::var("ALKANE_POOL_PASSWORD")
|
|
.map_err(|_| "set ALKANE_POOL_PASSWORD in the environment")?;
|
|
|
|
// Connect to the pool before enabling power. The SDK HAL server owns the
|
|
// power lease and cuts power if this process stalls or disconnects.
|
|
let mut pool = PoolConnection::connect(
|
|
address,
|
|
concat!("alkane-os/", env!("CARGO_PKG_VERSION")),
|
|
&worker,
|
|
&password,
|
|
Duration::from_secs(2),
|
|
Duration::from_secs(5),
|
|
)
|
|
.map_err(|error| io::Error::other(format!("pool handshake: {error:?}")))?;
|
|
let mut backend = HalClient::connect(socket)?;
|
|
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;
|
|
loop {
|
|
let (event, reports) = pool
|
|
.cycle(&mut miner)
|
|
.map_err(|error| io::Error::other(format!("mining cycle: {error:?}")))?;
|
|
match event {
|
|
PoolEvent::ShareAccepted(_) => {
|
|
accepted += 1;
|
|
if max_accepts == Some(accepted) {
|
|
miner.stop()?;
|
|
println!("simulator shares accepted: {accepted}");
|
|
return Ok(());
|
|
}
|
|
}
|
|
PoolEvent::ShareRejected(id) => eprintln!("pool rejected share request {id}"),
|
|
_ => {}
|
|
}
|
|
if matches!(event, PoolEvent::Idle) && reports.is_empty() {
|
|
std::thread::sleep(Duration::from_millis(10));
|
|
}
|
|
}
|
|
}
|
|
|
|
fn main() {
|
|
let mut args = std::env::args().skip(1);
|
|
match args.next().as_deref() {
|
|
Some("--list-targets") => {
|
|
println!("control_board,hash_chip,algorithm");
|
|
for target in target_matrix() {
|
|
println!(
|
|
"{},{},{:?}",
|
|
target.control.id, target.chip.id, target.chip.algorithm
|
|
);
|
|
}
|
|
}
|
|
Some("--version") => println!("alkane-os {}", env!("CARGO_PKG_VERSION")),
|
|
Some("--mine-simulator") => {
|
|
if let Err(error) = sim_mine(args) {
|
|
eprintln!("simulator mining failed: {error}");
|
|
std::process::exit(1);
|
|
}
|
|
}
|
|
_ => {
|
|
eprintln!("usage: alkane-os --list-targets | --version | --mine-simulator HAL_SOCKET POOL_ADDRESS WORKER BM1362|BM1366 [MAX_ACCEPTS]");
|
|
std::process::exit(2);
|
|
}
|
|
}
|
|
}
|