-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminer_worker.js
More file actions
46 lines (39 loc) · 964 Bytes
/
miner_worker.js
File metadata and controls
46 lines (39 loc) · 964 Bytes
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
// miner_worker.js
const { parentPort, workerData } = require('worker_threads');
const crypto = require('crypto');
// Ambil data dari workerData
const { minerAddress, difficultyPrefix } = workerData;
// Mining loop di worker thread
function mineLoop() {
const timestamp = Date.now();
let nonce = 0;
let hash;
while (true) {
hash = crypto.createHash('sha256')
.update(minerAddress + timestamp + nonce)
.digest('hex');
if (nonce % 100000 === 0) {
parentPort.postMessage({
event: 'progress',
miner: minerAddress,
nonce,
hash
});
}
if (hash.startsWith(difficultyPrefix)) {
parentPort.postMessage({
event: 'blockFound',
miner: minerAddress,
nonce,
hash,
timestamp
});
break;
}
nonce++;
}
// Loop berkelanjutan: setelah block ditemukan, lanjut mining lagi
setImmediate(mineLoop);
}
// Jalankan loop
mineLoop();