summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGab <24553253+gabrix73@users.noreply.github.com>2025-04-21 15:12:48 +0200
committerGitHub <noreply@github.com>2025-04-21 15:12:48 +0200
commita51997335b51db7853d166e4eb6503b1c0ecbb50 (patch)
treee518a733c8986c3b21cac4450c2b4c17d4c5b7c4
parent46777cd7016b63225ada5ef74d55798c85d0171b (diff)
downloadm2usenet-and-mail2news-a51997335b51db7853d166e4eb6503b1c0ecbb50.tar.gz
m2usenet-and-mail2news-a51997335b51db7853d166e4eb6503b1c0ecbb50.tar.xz
m2usenet-and-mail2news-a51997335b51db7853d166e4eb6503b1c0ecbb50.zip
Update powWorker.js
-rw-r--r--powWorker.js61
1 files 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;
}
}
};