From a51997335b51db7853d166e4eb6503b1c0ecbb50 Mon Sep 17 00:00:00 2001 From: Gab <24553253+gabrix73@users.noreply.github.com> Date: Mon, 21 Apr 2025 15:12:48 +0200 Subject: Update powWorker.js --- powWorker.js | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 51 insertions(+), 10 deletions(-) diff --git a/powWorker.js b/powWorker.js index aba22ff..fa668a1 100644 --- a/powWorker.js +++ b/powWorker.js @@ -1,16 +1,57 @@ -// /var/www/mail2usenet/powWorker.js self.onmessage = async ({ data }) => { const { prefix, targetZeros, startNonce, step } = data; - let nonce = startNonce, checked = 0, zeroStr = '0'.repeat(targetZeros); - while (true) { - const buf = await crypto.subtle.digest('SHA-1', new TextEncoder().encode(prefix + nonce)); - const hex = Array.from(new Uint8Array(buf)).map(b=>b.toString(16).padStart(2,'0')).join(''); - if (hex.startsWith(zeroStr)) { - return self.postMessage({ type:'found', nonce }); + const zeroStr = '0'.repeat(targetZeros); + let nonce = startNonce; + let checked = 0; + + // Batch size: controllare più nonce per ogni aggiornamento di progresso + const BATCH_SIZE = 500; + + // Parallelizzazione del lavoro utilizzando più promise contemporaneamente + const CONCURRENT_PROMISES = 12; + + // Funzione per verificare un singolo nonce + const checkNonce = async (nonceToCheck) => { + const buf = await crypto.subtle.digest('SHA-1', new TextEncoder().encode(prefix + nonceToCheck)); + const hex = Array.from(new Uint8Array(buf)).map(b => b.toString(16).padStart(2, '0')).join(''); + return { nonce: nonceToCheck, hex }; + }; + + // Funzione per verificare un batch di nonce in parallelo + const processBatch = async () => { + const promises = []; + for (let i = 0; i < CONCURRENT_PROMISES; i++) { + let batchPromises = []; + for (let j = 0; j < BATCH_SIZE; j++) { + const currentNonce = nonce; + batchPromises.push(checkNonce(currentNonce)); + nonce += step; + } + promises.push(Promise.all(batchPromises)); + } + + // Attende il completamento di tutti i batch + const results = await Promise.all(promises); + + // Appiattisce i risultati e controlla se c'è una corrispondenza + const allResults = results.flat(); + for (const result of allResults) { + if (result.hex.startsWith(zeroStr)) { + return { found: true, nonce: result.nonce }; + } } - nonce += step; - if (++checked % 1000 === 0) { - self.postMessage({ type:'progress', checked }); + + checked += CONCURRENT_PROMISES * BATCH_SIZE; + self.postMessage({ type: 'progress', checked }); + return { found: false }; + }; + + // Loop principale + while (true) { + const { found, nonce: foundNonce } = await processBatch(); + if (found) { + self.postMessage({ type: 'found', nonce: foundNonce }); + break; } } }; -- cgit v1.2.3