Skip to content

Instantly share code, notes, and snippets.

@khaosdoctor khaosdoctor/index.js
Created Sep 17, 2019

Embed
What would you like to do?
Simple bitcoin-like proof of work using worker threads
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)
}
{
"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"
]
}
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
You can’t perform that action at this time.