Skip to content

Conversation

@mutatrum
Copy link
Collaborator

@mutatrum mutatrum commented Nov 4, 2025

This PR eliminates several inefficient codepaths in constructing new jobs:

  • Limit back and forth reversing of byte or word orders;
  • Remove merkle_root_be and prev_block_hash_be from bm_job;
  • calculate_coinbase_tx_hash and calculate_merkle_root_hash return hashes instead of hex strings;
  • All parameters are converted to binary once;
  • Stack allocation instead of malloc;
  • Remove unused functions;
  • Unroll endiannness and word reversal functions.

Verified on BM1397 and BM1370.

This PR eliminates several inefficient codepaths
 * Limit back and forth reversing of byte or word orders;
 * Remove merkle_root_be and prev_block_hash_be from bm_job;
 * All parameters are converted to binary once;
 * Stack allocation instead of malloc;
 * Remove unused functions;
 * Unroll byte and word reversal functions.
@github-actions
Copy link

github-actions bot commented Nov 4, 2025

Test Results

22 tests  +3   22 ✅ +3   0s ⏱️ ±0s
 1 suites ±0    0 💤 ±0 
 1 files   ±0    0 ❌ ±0 

Results for commit bb51b7e. ± Comparison against base commit 2535ad4.

♻️ This comment has been updated with latest results.

@WantClue WantClue requested a review from Georges760 November 4, 2025 19:48
Copy link
Collaborator

@Georges760 Georges760 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A very GOOD PR, GG Mutatrum!

Didn't test it on HW but since you did it on both 97 and 70, should be fine, this kind of change easily brake mining, so as long as it mine well, it is fine.

@mutatrum
Copy link
Collaborator Author

mutatrum commented Nov 5, 2025

A very GOOD PR, GG Mutatrum!

Thanks, it messes with your sanity this stuff.

Didn't test it on HW but since you did it on both 97 and 70, should be fine, this kind of change easily brake mining, so as long as it mine well, it is fine.

Indeed, if something is wrong, there's three options:

  • ASIC won't return any shares;
  • Diff verification in firmware fails (everything is diff 0.0);
  • Pool rejects the share (on ckpool all rejected with 'Above target').

@mutatrum mutatrum added hashing This PR impacts hashing performance cleanup Code cleanup labels Nov 5, 2025
@Georges760
Copy link
Collaborator

A very GOOD PR, GG Mutatrum!

Thanks, it messes with your sanity this stuff.
Very true, only those who messed with endiannes in bitcoin structures knows...

@skot
Copy link
Collaborator

skot commented Nov 5, 2025

bold PR; thanks for risking your sanity to improve the mining job construction. I'll give this a try on my BM1368 Supra today.

@johnny9 you might be interested in seeing this.

TEST_ASSERT_EQUAL_UINT8_ARRAY(expected_midstate_bin, job.midstate, 32);
uint8_t expected_midstate_bin_reversed[32];
reverse_32bit_words(expected_midstate_bin, expected_midstate_bin_reversed);
reverse_endianness_per_word(expected_midstate_bin_reversed);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like this, my suspicion is that the hex string above is not in the same order as it would actually come out of the midstate. In code, the endianness isn't changed, only word order.

@mutatrum
Copy link
Collaborator Author

mutatrum commented Nov 5, 2025

bold PR; thanks for risking your sanity to improve the mining job construction. I'll give this a try on my BM1368 Supra today.

Paving the way for #420 and dropping the job queues, slowly getting there.

@johnny9
Copy link
Collaborator

johnny9 commented Nov 5, 2025

Looks like good cleanup at first glance. I need to take a closer look at the test case changes to be sure it's functionally working well.

@mutatrum
Copy link
Collaborator Author

mutatrum commented Nov 5, 2025

Looks like good cleanup at first glance. I need to take a closer look at the test case changes to be sure it's functionally working well.

Only the setup of test cases has changed, or expected values are converted to binary, all the assertions in the test cases are the same. The only one I'm not happy with is this one: #1321 (comment)

One other thing I'm considering is to move out all the reverse_32bit_words for prev_block_hash, merkle_root and mid_states out from mining.c and only do that in the bm13##.c code, as that's a Bitmain specific thing AFAIK.

The only manipulation happening then is the word endianness swap of prev_block_hash, but that is an actual bitcoin vs stratum difference.

@WantClue WantClue added this to the 2.12.0 milestone Nov 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cleanup Code cleanup hashing This PR impacts hashing performance

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants