Skip to content

Commit

Permalink
protocol: fix endianness
Browse files Browse the repository at this point in the history
  • Loading branch information
Georges760 committed Jan 26, 2025
1 parent e2feb6d commit 224dac2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
26 changes: 18 additions & 8 deletions bm13xx-protocol/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,12 +241,14 @@ impl Command {
/// ```
/// use bm13xx_protocol::command::Command;
///
/// let merkle_root = [0x2d, 0x19, 0x75, 0x74, 0x66, 0x63, 0x21, 0x46, 0xb8, 0x71, 0x7a, 0x7e,
/// 0xfe, 0x83, 0xec, 0x35, 0xc0, 0x96, 0xf3, 0xa4, 0xc0, 0xd8, 0x86, 0xda, 0xa8, 0x0e,
/// 0x70, 0x2e, 0xed, 0xe9, 0x96, 0x71];
/// let prev_hash = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x86, 0x02, 0x00,
/// 0x5b, 0xa4, 0xa5, 0x0e, 0x55, 0xd3, 0x00, 0xfc, 0xae, 0x0e, 0xd5, 0x56, 0xd7, 0x76,
/// 0xd8, 0x1a, 0x38, 0xe1, 0x99, 0x1f];
/// let merkle_root = [
/// 0x74, 0x75, 0x19, 0x2d, 0x46, 0x21, 0x63, 0x66, 0x7e, 0x7a, 0x71, 0xb8,
/// 0x35, 0xec, 0x83, 0xfe, 0xa4, 0xf3, 0x96, 0xc0, 0xda, 0x86, 0xd8, 0xc0,
/// 0x2e, 0x70, 0x0e, 0xa8, 0x71, 0x96, 0xe9, 0xed];
/// let prev_hash = [
/// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x86, 0xff,
/// 0x0e, 0xa5, 0xa4, 0x5b, 0xfc, 0x00, 0xd3, 0x55, 0x56, 0xd5, 0x0e, 0xae,
/// 0x1a, 0xd8, 0x76, 0xd7, 0x1f, 0x99, 0xe1, 0x38];
/// let header = Command::job_header(168, 0x1704_2450, 0x6570_de83, merkle_root, prev_hash, 0x2000_0000);
/// assert_eq!(
/// header,
Expand All @@ -264,8 +266,8 @@ impl Command {
job_id: u8,
n_bits: u32,
n_time: u32,
full_merkle_root: [u8; 32],
prev_block_header_hash: [u8; 32],
mut full_merkle_root: [u8; 32],
mut prev_block_header_hash: [u8; 32],
version: u32,
) -> [u8; 88] {
let mut data = [0; 88];
Expand All @@ -279,7 +281,15 @@ impl Command {
// data[6..].clone_from_slice(&0u32.to_le_bytes()); // starting_nonce ?
data[10..14].clone_from_slice(&n_bits.to_le_bytes());
data[14..18].clone_from_slice(&n_time.to_le_bytes());
full_merkle_root.chunks_exact_mut(4).for_each(|chunk| {
chunk.reverse();
});
data[18..50].clone_from_slice(&full_merkle_root);
prev_block_header_hash
.chunks_exact_mut(4)
.for_each(|chunk| {
chunk.reverse();
});
data[50..82].clone_from_slice(&prev_block_header_hash);
data[82..86].clone_from_slice(&version.to_le_bytes());
let crc = crc16(&data[2..86]);
Expand Down
20 changes: 10 additions & 10 deletions bm13xx-protocol/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ pub struct JobResponse {
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
pub struct JobVersionResponse {
pub nonce: u32,
pub job_id: u8,
pub midstate_id: u8,
pub unknown: u8,
pub job_id_small_core_id: u8,
pub version_bit: u32,
}

Expand Down Expand Up @@ -96,7 +96,7 @@ impl Response {
/// assert!(resp.is_ok());
/// match resp.unwrap() {
/// ResponseType::Job(j) => {
/// assert_eq!(j.nonce, 0x97C3_28B6);
/// assert_eq!(j.nonce, 0xB628_C397);
/// assert_eq!(j.midstate_id, 1);
/// assert_eq!(j.job_id, 0x63);
/// },
Expand All @@ -115,7 +115,7 @@ impl Response {
}
if data[8] & 0x80 == 0x80 {
return Ok(ResponseType::Job(JobResponse {
nonce: u32::from_be_bytes(data[2..6].try_into().unwrap()),
nonce: u32::from_le_bytes(data[2..6].try_into().unwrap()),
midstate_id: data[6],
job_id: data[7],
}));
Expand Down Expand Up @@ -179,9 +179,9 @@ impl Response {
/// assert!(resp.is_ok());
/// match resp.unwrap() {
/// ResponseType::JobVer(j) => {
/// assert_eq!(j.nonce, 0x2FD5_96CE);
/// assert_eq!(j.midstate_id, 2);
/// assert_eq!(j.job_id, 0x93);
/// assert_eq!(j.nonce, 0xCE96_D52F);
/// assert_eq!(j.unknown, 2);
/// assert_eq!(j.job_id_small_core_id, 0x93);
/// assert_eq!(j.version_bit, 0x129F_6000);
/// },
/// _ => panic!(),
Expand All @@ -199,9 +199,9 @@ impl Response {
}
if data[10] & 0x80 == 0x80 {
return Ok(ResponseType::JobVer(JobVersionResponse {
nonce: u32::from_be_bytes(data[2..6].try_into().unwrap()),
midstate_id: data[6],
job_id: data[7],
nonce: u32::from_le_bytes(data[2..6].try_into().unwrap()),
unknown: data[6],
job_id_small_core_id: data[7],
version_bit: (u16::from_be_bytes(data[8..10].try_into().unwrap()) as u32) << 13,
}));
}
Expand Down

0 comments on commit 224dac2

Please sign in to comment.