-
-
Save SonPatrick/954b8eadbb970ac9d358567aaa04b862 to your computer and use it in GitHub Desktop.
Simple bitcoin-like proof of work using worker threads
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { payloads } = require('./payloads.json') | |
const { Worker } = require('worker_threads') | |
const LEADING_ZEROES = 4 | |
const final = [] | |
let finishedWorkers = 0 | |
for (let payload of payloads) { | |
const worker = new Worker('./worker.js', { env: { LEADING_ZEROES } }) | |
worker.once('message', (message) => { | |
final.push(message) | |
finishedWorkers++ | |
if (finishedWorkers === payloads.length) console.log(final) | |
}) | |
worker.on('error', console.error) | |
console.log(`Iniciando worker de ID ${worker.threadId} e enviando o payload "${payload}"`) | |
worker.postMessage(payload) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"payloads": [ | |
"romi3927273277nevraefiass2103342375ohodkotbug", | |
"gaze443157697niscipibgiw1642589262efihjejlos", | |
"ocug1626862968veswukguwcu2592116772osoreteriu", | |
"iwos2708201740eprihvogmos3764344965agadupiote", | |
"nemu3084996143vimwudtanek1213613753ofefuecnuf", | |
"isaj4116570015biwidaposci4241495684hezenlatdu", | |
"huct2915543159amviwihweuv2600946750jeeduzerah", | |
"razk2100190742afatmuclolo997646306routaotahe", | |
"gihu2518634792boafiehucef2128481441gecrepirse", | |
"zoup2063415799niuwictihal1459892629arrogdekin", | |
"arsa41137640wetildedimt3297232485uvebascaen", | |
"uccu3568686497mucuminulaf2528880605ueduemosob", | |
"luro3950339148zogazeivnac3529592063makevtabic", | |
"ijoj2400035192akikodpuhet1846390304oesadipkor", | |
"zuhe24553937pjicathesej1889936403voskolrowu", | |
"ulog2145473488ewkiusalakm3891503344isosfidulw", | |
"mevi4172405508maosocicacn218089igamvugusu", | |
"vepi1035082870ratewivneln4027185780ugahidiwuz", | |
"toze3177065179demjukuzjab3619747835epocilopin", | |
"wefj1958553370osnoputrila406622169tonuseotij", | |
"jitm4273326712uulfafoafda3083365269donecwaguu", | |
"deiz284958975sonalkedrob3724713613ejlumkuvwu", | |
"ekoa329701548nopofarejep3799522462ewergilual", | |
"wogi4052082081dsimjugovbe1235427965ifvigesuma", | |
"vost153242693ocukuwinhad3925312540celudnawhe", | |
"luzz1422123026uzpihwausvi3711685905wocbewenub", | |
"opup2085811647ugorwadtekj1040859347askiwosuma", | |
"pisg3729559558ucucikufava940175451bwalidhuri", | |
"diur2228207618koungurzawt1535270119ihullepfil", | |
"guew1198852206uerehozvecd1741914790obbesikves", | |
"duba1193434831nepwajmajim3638815420ivekemunjo", | |
"bolm1143531649etsocopzijc2871885729ijpuptazsu", | |
"jazo2509464204resunouduma364075720lepbamwegj" | |
] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { parentPort } = require('worker_threads') | |
const crypto = require('crypto') | |
parentPort.once('message', (message) => { | |
const payload = message | |
let nonce = 0 | |
let generatedHash = '' | |
do { | |
generatedHash = crypto.createHash('sha256').update(payload + nonce).digest('hex') | |
nonce++ | |
} while (generatedHash.slice(0, process.env.LEADING_ZEROES) !== '0'.repeat(process.env.LEADING_ZEROES)) | |
parentPort.postMessage({ payload: message, nonce, hash: generatedHash }) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment