This commit is contained in:
@@ -7,6 +7,7 @@ pub mod bm1366;
|
||||
pub mod hal_wire;
|
||||
pub mod miner;
|
||||
pub mod pow;
|
||||
pub mod stratum_work;
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub enum Vendor {
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
//! Construct SHA-256 Stratum V1 work after a protocol client decodes notify.
|
||||
//! This module assembles headers; it never searches for nonces.
|
||||
|
||||
use crate::pow::sha256d;
|
||||
|
||||
const MAX_COINBASE_BYTES: usize = 65_536;
|
||||
const MAX_MERKLE_BRANCHES: usize = 64;
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub enum JobError {
|
||||
InvalidExtranonceSize,
|
||||
CoinbaseTooLarge,
|
||||
TooManyMerkleBranches,
|
||||
ExtranonceLength,
|
||||
}
|
||||
|
||||
/// Binary fields from `mining.notify`, plus `mining.subscribe` extranonce data.
|
||||
/// `prevhash_stratum` is the eight word-swapped 32-bit words sent on the wire.
|
||||
pub struct Sha256Job {
|
||||
pub prevhash_stratum: [u8; 32],
|
||||
pub coinb1: Vec<u8>,
|
||||
pub coinb2: Vec<u8>,
|
||||
pub merkle_branches: Vec<[u8; 32]>,
|
||||
pub version: u32,
|
||||
pub nbits: u32,
|
||||
pub ntime: u32,
|
||||
pub extranonce1: Vec<u8>,
|
||||
pub extranonce2_size: usize,
|
||||
}
|
||||
|
||||
impl Sha256Job {
|
||||
pub fn validate(&self) -> Result<(), JobError> {
|
||||
if self.extranonce1.len() > 32 || self.extranonce2_size > 32 {
|
||||
return Err(JobError::InvalidExtranonceSize);
|
||||
}
|
||||
let total = self
|
||||
.coinb1
|
||||
.len()
|
||||
.checked_add(self.extranonce1.len())
|
||||
.and_then(|sum| sum.checked_add(self.extranonce2_size))
|
||||
.and_then(|sum| sum.checked_add(self.coinb2.len()));
|
||||
if total.is_none_or(|sum| sum > MAX_COINBASE_BYTES) {
|
||||
return Err(JobError::CoinbaseTooLarge);
|
||||
}
|
||||
if self.merkle_branches.len() > MAX_MERKLE_BRANCHES {
|
||||
return Err(JobError::TooManyMerkleBranches);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn nonce_zero_header(&self, extranonce2: &[u8], ntime: u32) -> Result<[u8; 80], JobError> {
|
||||
self.validate()?;
|
||||
if extranonce2.len() != self.extranonce2_size {
|
||||
return Err(JobError::ExtranonceLength);
|
||||
}
|
||||
let mut coinbase = Vec::with_capacity(
|
||||
self.coinb1.len() + self.extranonce1.len() + extranonce2.len() + self.coinb2.len(),
|
||||
);
|
||||
coinbase.extend_from_slice(&self.coinb1);
|
||||
coinbase.extend_from_slice(&self.extranonce1);
|
||||
coinbase.extend_from_slice(extranonce2);
|
||||
coinbase.extend_from_slice(&self.coinb2);
|
||||
let mut root = sha256d(&coinbase);
|
||||
for branch in &self.merkle_branches {
|
||||
let mut pair = [0_u8; 64];
|
||||
pair[..32].copy_from_slice(&root);
|
||||
pair[32..].copy_from_slice(branch);
|
||||
root = sha256d(&pair);
|
||||
}
|
||||
let mut header = [0_u8; 80];
|
||||
header[..4].copy_from_slice(&self.version.to_le_bytes());
|
||||
for (index, word) in self.prevhash_stratum.chunks_exact(4).enumerate() {
|
||||
for (offset, byte) in word.iter().rev().enumerate() {
|
||||
header[4 + index * 4 + offset] = *byte;
|
||||
}
|
||||
}
|
||||
header[36..68].copy_from_slice(&root);
|
||||
header[68..72].copy_from_slice(&ntime.to_le_bytes());
|
||||
header[72..76].copy_from_slice(&self.nbits.to_le_bytes());
|
||||
Ok(header)
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
use alkane_os::bm1366::{decode_nonce_report, encode_work};
|
||||
use alkane_os::miner::Bm1366Miner;
|
||||
use alkane_os::pow::verify_sha256_report;
|
||||
use alkane_os::stratum_work::Sha256Job;
|
||||
use alkane_os::{power_decision, target_matrix, HardwareBackend};
|
||||
use std::hint::black_box;
|
||||
use std::time::Instant;
|
||||
@@ -69,6 +70,28 @@ fn bench_bm1366_report_decode() {
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "microbenchmark"]
|
||||
fn bench_stratum_header() {
|
||||
let job = Sha256Job {
|
||||
prevhash_stratum: [0; 32],
|
||||
coinb1: vec![0; 48],
|
||||
coinb2: vec![0; 64],
|
||||
merkle_branches: vec![[0; 32]; 4],
|
||||
version: 0x2000_0000,
|
||||
nbits: 0x1d00_ffff,
|
||||
ntime: 0x6655_4433,
|
||||
extranonce1: vec![0; 4],
|
||||
extranonce2_size: 8,
|
||||
};
|
||||
measure("stratum_header", 5_000, || {
|
||||
black_box(
|
||||
job.nonce_zero_header(black_box(&[0; 8]), job.ntime)
|
||||
.unwrap(),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
struct BenchBackend;
|
||||
|
||||
impl HardwareBackend for BenchBackend {
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
use alkane_os::bm1366::encode_work;
|
||||
use alkane_os::stratum_work::{JobError, Sha256Job};
|
||||
|
||||
fn fixture() -> Sha256Job {
|
||||
let mut prevhash_stratum = [0_u8; 32];
|
||||
let mut branch = [0_u8; 32];
|
||||
for (index, byte) in prevhash_stratum.iter_mut().enumerate() {
|
||||
*byte = index as u8;
|
||||
}
|
||||
for (index, byte) in branch.iter_mut().enumerate() {
|
||||
*byte = (index + 32) as u8;
|
||||
}
|
||||
Sha256Job {
|
||||
prevhash_stratum,
|
||||
coinb1: b"abc".to_vec(),
|
||||
coinb2: b"xyz".to_vec(),
|
||||
merkle_branches: vec![branch],
|
||||
version: 0x2000_0000,
|
||||
nbits: 0x1d00_ffff,
|
||||
ntime: 0x6655_4433,
|
||||
extranonce1: vec![1, 2, 3, 4],
|
||||
extranonce2_size: 4,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn builds_cross_checked_header_and_asic_work() {
|
||||
let job = fixture();
|
||||
let header = job.nonce_zero_header(&[5, 6, 7, 8], job.ntime).unwrap();
|
||||
let expected = concat!(
|
||||
"0000002003020100070605040b0a09080f0e0d0c13121110171615141b1a19181f1e1d1c",
|
||||
"9b9bc91f078af83ff63d00c6da09b5bc4dd26e2d8c0df8ab16b933a259a05881",
|
||||
"33445566ffff001d00000000"
|
||||
);
|
||||
let actual: String = header.iter().map(|byte| format!("{byte:02x}")).collect();
|
||||
assert_eq!(actual, expected);
|
||||
let frame = encode_work(&header, 0x18).unwrap();
|
||||
assert_eq!(&frame[10..14], &header[72..76]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_invalid_pool_work_before_hashing() {
|
||||
let mut job = fixture();
|
||||
assert_eq!(
|
||||
job.nonce_zero_header(&[5, 6], job.ntime),
|
||||
Err(JobError::ExtranonceLength)
|
||||
);
|
||||
job.extranonce2_size = 33;
|
||||
assert_eq!(job.validate(), Err(JobError::InvalidExtranonceSize));
|
||||
job.extranonce2_size = 4;
|
||||
job.coinb1.resize(65_536, 0);
|
||||
assert_eq!(job.validate(), Err(JobError::CoinbaseTooLarge));
|
||||
job.coinb1 = b"abc".to_vec();
|
||||
job.merkle_branches.resize(65, [0; 32]);
|
||||
assert_eq!(job.validate(), Err(JobError::TooManyMerkleBranches));
|
||||
}
|
||||
Reference in New Issue
Block a user