ironfish
ironfish copied to clipboard
Fix overlapping values for mining randomness
Summary
Closes https://github.com/iron-fish/ironfish/issues/2185
Previously, the miner would check values at the batch size boundaries more than once, leading to wasted work. This change prevents that from happening by making the end value less than the start of the next batch size
Testing Plan
Breaking Change
Is this a breaking change? If yes, add notes below on why this is breaking and what additional work is required, if any.
[ ] Yes
I made a quick spreadsheet to demonstrate the issue + this fix. In the current code, we:
- Initialize step_size to the number of threads
- Initialize batch_size to some large number
- Give each thread a unique starting value from 0 to (step_size - 1)
- Each thread hashes values from start to start + batch_size - 1, in increments of step_size
- When a thread completes a batch, increase its start by batch_size
The below example demonstrates this. You can see that some values are hashed twice:
The change here is to update the following:
- Each thread hashes values from start to start + batch_size - step_size, in increments of step_size
This addresses the duplicate hashing, but does skip some values in the process.
I believe an alternative that would include all values would be the following:
- Each thread hashes values from start to start + batch_size (inclusive), in increments of step_size
- When a thread completes a batch, increase its start by batch_size + step_size - (batch_size % step_size)
This is a bit less easy to test, however. For example: