261 lines
8.2 KiB
Rust
261 lines
8.2 KiB
Rust
// SPDX-License-Identifier: MIT
|
|
use alkane_os::miner::{Bm1366Miner, MinerError, ValidShare};
|
|
use alkane_os::pow::VersionMask;
|
|
use alkane_os::stratum_session::{DispatchError, PoolSession, SessionError};
|
|
use alkane_os::stratum_v1::Subscription;
|
|
use alkane_os::HardwareBackend;
|
|
|
|
fn session() -> PoolSession {
|
|
PoolSession::new(
|
|
Subscription {
|
|
extranonce1: vec![1, 2, 3, 4],
|
|
extranonce2_size: 4,
|
|
},
|
|
Some(VersionMask::new(0x1fff_e000).unwrap()),
|
|
true,
|
|
)
|
|
.unwrap()
|
|
}
|
|
|
|
fn notify(id: &str, clean: bool) -> String {
|
|
serde_json::json!({
|
|
"id": null,
|
|
"method": "mining.notify",
|
|
"params": [
|
|
id,
|
|
"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",
|
|
"616263", "78797a", [], "20000000", "1d00ffff", "66554433", clean
|
|
]
|
|
})
|
|
.to_string()
|
|
}
|
|
|
|
#[test]
|
|
fn difficulty_changes_on_next_job_and_old_work_keeps_its_target() {
|
|
let mut pool = session();
|
|
assert_eq!(
|
|
pool.issue_notify(notify("early", true).as_bytes()).err(),
|
|
Some(SessionError::MissingDifficulty)
|
|
);
|
|
pool.control(br#"{"id":null,"method":"mining.set_difficulty","params":[1]}"#)
|
|
.unwrap();
|
|
let first = pool.issue_notify(notify("first", true).as_bytes()).unwrap();
|
|
assert!(first.clean_jobs);
|
|
assert_eq!(first.work_id, 0);
|
|
assert_eq!(&first.target_le[26..28], &[0xff, 0xff]);
|
|
pool.control(br#"{"id":null,"method":"mining.set_difficulty","params":[2]}"#)
|
|
.unwrap();
|
|
let second = pool
|
|
.issue_notify(notify("second", false).as_bytes())
|
|
.unwrap();
|
|
assert!(!second.clean_jobs);
|
|
assert_eq!(second.work_id, 8);
|
|
assert_eq!(&second.target_le[26..28], &[0xff, 0x7f]);
|
|
assert_ne!(first.header, second.header);
|
|
assert_eq!(
|
|
pool.control(br#"{"id":null,"method":"mining.set_version_mask","params":["00006000"]}"#),
|
|
Ok(true)
|
|
);
|
|
assert_eq!(
|
|
pool.submission(
|
|
ValidShare {
|
|
nonce: 0xedad_ff38,
|
|
work_id: first.work_id,
|
|
rolled_version: 0x2000_0000,
|
|
hash_le: [0; 32],
|
|
},
|
|
"worker",
|
|
10,
|
|
),
|
|
Err(SessionError::StaleShare)
|
|
);
|
|
assert_eq!(
|
|
pool.control(br#"{"id":null,"method":"mining.set_version_mask","params":["00006000"]}"#),
|
|
Ok(false)
|
|
);
|
|
let clean = pool.issue_notify(notify("third", true).as_bytes()).unwrap();
|
|
assert!(clean.clean_jobs);
|
|
assert_eq!(clean.work_id, 16);
|
|
let old = ValidShare {
|
|
nonce: 0xedad_ff38,
|
|
work_id: first.work_id,
|
|
rolled_version: 0x2000_0000,
|
|
hash_le: [0; 32],
|
|
};
|
|
assert_eq!(
|
|
pool.submission(old, "worker", 10),
|
|
Err(SessionError::StaleShare)
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn extranonce_change_invalidates_jobs_without_reusing_asic_ids() {
|
|
let mut pool = session();
|
|
pool.control(br#"{"id":null,"method":"mining.set_difficulty","params":[1]}"#)
|
|
.unwrap();
|
|
let old = pool.issue_notify(notify("old", true).as_bytes()).unwrap();
|
|
assert!(pool
|
|
.control(br#"{"id":null,"method":"mining.set_extranonce","params":["aabbccdd",4]}"#)
|
|
.unwrap());
|
|
let new = pool.issue_notify(notify("new", false).as_bytes()).unwrap();
|
|
assert_eq!(new.work_id, 8);
|
|
assert_ne!(old.header, new.header);
|
|
for index in 2..32 {
|
|
let work = pool.issue_notify(notify("more", false).as_bytes()).unwrap();
|
|
assert_eq!(work.work_id, (index as u8) << 3);
|
|
}
|
|
assert_eq!(
|
|
pool.issue_notify(notify("exhausted", false).as_bytes())
|
|
.err(),
|
|
Some(SessionError::WorkIdsExhausted)
|
|
);
|
|
pool.after_hardware_reset();
|
|
assert_eq!(
|
|
pool.issue_notify(notify("after-reset", false).as_bytes())
|
|
.unwrap()
|
|
.work_id,
|
|
0
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn zero_length_extranonce_requires_distinct_header_until_hardware_reset() {
|
|
let mut pool = PoolSession::new(
|
|
Subscription {
|
|
extranonce1: vec![1],
|
|
extranonce2_size: 0,
|
|
},
|
|
None,
|
|
false,
|
|
)
|
|
.unwrap();
|
|
pool.control(br#"{"id":null,"method":"mining.set_difficulty","params":[1]}"#)
|
|
.unwrap();
|
|
pool.issue_notify(notify("one", true).as_bytes()).unwrap();
|
|
assert_eq!(
|
|
pool.issue_notify(notify("two", false).as_bytes()).err(),
|
|
Some(SessionError::ExtranonceExhausted)
|
|
);
|
|
let distinct = notify("two", true).replace("66554433", "66554434");
|
|
pool.issue_notify(distinct.as_bytes()).unwrap();
|
|
pool.after_hardware_reset();
|
|
pool.issue_notify(notify("three", true).as_bytes()).unwrap();
|
|
}
|
|
|
|
#[test]
|
|
fn one_byte_extranonce_restarts_only_for_a_distinct_header_template() {
|
|
let mut pool = PoolSession::new(
|
|
Subscription {
|
|
extranonce1: vec![1],
|
|
extranonce2_size: 1,
|
|
},
|
|
None,
|
|
false,
|
|
)
|
|
.unwrap();
|
|
pool.control(br#"{"id":null,"method":"mining.set_difficulty","params":[1]}"#)
|
|
.unwrap();
|
|
for index in 0..256 {
|
|
if index != 0 && index % 32 == 0 {
|
|
pool.after_hardware_reset();
|
|
}
|
|
pool.issue_notify(notify("same", false).as_bytes()).unwrap();
|
|
}
|
|
pool.after_hardware_reset();
|
|
assert_eq!(
|
|
pool.issue_notify(notify("same", false).as_bytes()).err(),
|
|
Some(SessionError::ExtranonceExhausted)
|
|
);
|
|
let distinct = notify("new", false).replace("66554433", "66554434");
|
|
pool.issue_notify(distinct.as_bytes()).unwrap();
|
|
pool.after_hardware_reset();
|
|
for index in 0..256_u32 {
|
|
if index != 0 && index % 32 == 0 {
|
|
pool.after_hardware_reset();
|
|
}
|
|
let ntime = format!("{index:08x}");
|
|
let next = notify("fresh", false).replace("66554433", &ntime);
|
|
pool.issue_notify(next.as_bytes()).unwrap();
|
|
}
|
|
}
|
|
|
|
struct FakeBackend {
|
|
powered: bool,
|
|
sent: usize,
|
|
report: Option<[u8; 11]>,
|
|
}
|
|
|
|
impl HardwareBackend for FakeBackend {
|
|
type Error = &'static str;
|
|
|
|
fn send_frame(&mut self, _chain: u8, frame: &[u8]) -> Result<(), Self::Error> {
|
|
assert!(self.powered);
|
|
assert_eq!(&frame[..4], &[0x55, 0xaa, 0x21, 0x54]);
|
|
self.sent += 1;
|
|
Ok(())
|
|
}
|
|
fn receive_frame(&mut self, _chain: u8, frame: &mut [u8]) -> Result<usize, Self::Error> {
|
|
match self.report.take() {
|
|
Some(report) => {
|
|
frame[..11].copy_from_slice(&report);
|
|
Ok(11)
|
|
}
|
|
None => Ok(0),
|
|
}
|
|
}
|
|
fn set_chain_reset(&mut self, _chain: u8, _asserted: bool) -> Result<(), Self::Error> {
|
|
Ok(())
|
|
}
|
|
fn set_fan_target(&mut self, _fan: u8, _percent: u8) -> Result<(), Self::Error> {
|
|
Ok(())
|
|
}
|
|
fn read_temperature_mc(&mut self, _sensor: u8) -> Result<i32, Self::Error> {
|
|
Ok(50_000)
|
|
}
|
|
fn set_psu_enabled(&mut self, enabled: bool) -> Result<(), Self::Error> {
|
|
self.powered = enabled;
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn pool_job_dispatch_and_version_mask_invalidation_reach_the_miner() {
|
|
let mut backend = FakeBackend {
|
|
powered: false,
|
|
sent: 0,
|
|
report: Some([
|
|
0xaa, 0x55, 0x38, 0xff, 0xad, 0xed, 0, 0x18, 0x04, 0xb3, 0x94,
|
|
]),
|
|
};
|
|
{
|
|
let mut miner = Bm1366Miner::new(&mut backend, 0, 0, 0, 100, 80_000).unwrap();
|
|
miner.start().unwrap();
|
|
let mut pool = session();
|
|
pool.control_with_miner(
|
|
&mut miner,
|
|
br#"{"id":null,"method":"mining.set_difficulty","params":[1]}"#,
|
|
)
|
|
.unwrap();
|
|
for index in 0..4 {
|
|
let work = pool
|
|
.issue_to_miner(&mut miner, notify("job", index == 0).as_bytes())
|
|
.unwrap();
|
|
assert_eq!(work.work_id, (index as u8) << 3);
|
|
}
|
|
pool.control_with_miner(
|
|
&mut miner,
|
|
br#"{"id":null,"method":"mining.set_version_mask","params":["00006000"]}"#,
|
|
)
|
|
.unwrap();
|
|
assert_eq!(miner.poll(), Err(MinerError::StaleReport));
|
|
miner.stop().unwrap();
|
|
assert_eq!(
|
|
pool.issue_to_miner(&mut miner, notify("stopped", false).as_bytes())
|
|
.err(),
|
|
Some(DispatchError::Miner(MinerError::NotRunning))
|
|
);
|
|
}
|
|
assert!(!backend.powered);
|
|
assert_eq!(backend.sent, 4);
|
|
}
|