summaryrefslogtreecommitdiffstats
path: root/powWorker.js
blob: fa668a1e3e30e5ed278cf7b6db5fdf5d0a033021 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
self.onmessage = async ({ data }) => {
  const { prefix, targetZeros, startNonce, step } = data;
  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 };
      }
    }
    
    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;
    }
  }
};