Skip to content

Commit

Permalink
Merge pull request #257 from pow-lab/avoid-sleep-0
Browse files Browse the repository at this point in the history
[PoW Lab] Enhance hash rate 5x by reducing the use of  `await sleep(0)`.
  • Loading branch information
atomicals authored Jan 27, 2024
2 parents 77379b7 + 34a2491 commit 75b53c8
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions lib/utils/miner-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,12 @@ if (parentPort) {
};
let finalCopyData, finalPrelimTx, finalSequence;

let lastGenerated = 0;
let generated = 0;
let lastTime = Date.now();

// Start mining loop, terminates when a valid proof of work is found or stopped manually
do {
// Introduce a minor delay to avoid overloading the CPU
await sleep(0);

// This worker has tried all assigned sequence range but it did not find solution.
if (sequence > seqEnd) {
Expand Down Expand Up @@ -196,6 +198,19 @@ if (parentPort) {
}

sequence++;
generated++;

if (generated % 10000 === 0) {
const hashRate = ((generated - lastGenerated) / (Date.now() - lastTime)) * 1000;
console.log(
'Hash rate:',
hashRate.toFixed(2),
'Op/s ',
);
lastTime = Date.now();
lastGenerated = generated;
await sleep(0);
}
} while (workerPerformBitworkForCommitTx);

if (finalSequence && finalSequence != -1) {
Expand Down

0 comments on commit 75b53c8

Please sign in to comment.