]
for (let i = 0; i < 6; i++) {
const start = performance.now()
- const work = await PowGpu.find(hashes[i])
+ const work = await PowGpu.search(hashes[i])
const end = performance.now()
times.push(end - start)
console.log(`${work} (${end - start} ms) ${hashes[i]}`)
* @param {string} hash - Hexadecimal hash of previous block, or public key for new accounts
* @param {number} [threshold=0xfffffff8] - Difficulty of proof-of-work calculation
*/
- static async search (hashHex: string, callback: (nonce: string | PromiseLike<string>) => any, threshold: number = 0xfffffff8): Promise<void> {
+ static async search (hashHex: string, threshold: number = 0xfffffff8): Promise<string> {
if (!/^[A-Fa-f0-9]{64}$/.test(hashHex)) throw new TypeError(`Invalid hash ${hashHex}`)
if (typeof threshold !== 'number') throw new TypeError(`Invalid threshold ${threshold}`)
if (performance.now() > 8000) {
throw new Error(`WebGPU device failed to load in time.`)
}
- setTimeout(async () => { await this.search(hashHex, callback, threshold) }, 100)
+ setTimeout(async () => { await this.search(hashHex, threshold) }, 100)
return
}
if (found) {
const hex = nonce.toString(16).padStart(16, '0')
- typeof callback === 'function' && callback(hex)
- return
+ return hex
} else {
- setTimeout(async () => { await this.search(hashHex, callback, threshold) })
+ setTimeout(async () => { return await this.search(hashHex, threshold) })
}
}
}