From 4afc7994d94f41634d0435930f0e9ef9da46e6b3 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Wed, 11 Dec 2024 14:07:10 -0800 Subject: [PATCH] Implement once-called function inline, and move validation to top of calculate function. --- src/lib/workers/pow.ts | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/src/lib/workers/pow.ts b/src/lib/workers/pow.ts index 78ba3b2..4cdf7b0 100644 --- a/src/lib/workers/pow.ts +++ b/src/lib/workers/pow.ts @@ -40,16 +40,13 @@ export class Pow { return out } - static hex_reverse (hex: string): string { - let out = '' - for (let i = hex.length; i > 0; i -= 2) { - out += hex.slice(i - 2, i) - } - return out - } - static calculate (hashHex: string, threshold: number | string = '0xFFFFFFF8', callback: (nonce: string | PromiseLike) => any): void { if (typeof threshold === 'number') threshold = '0x' + threshold.toString(16) + if (!/^[A-F-a-f0-9]{64}$/.test(hashHex)) throw new Error(`invalid_hash ${hashHex}`) + let reverseHex = '' + for (let i = hashHex.length; i > 0; i -= 2) { + reverseHex += hashHex.slice(i - 2, i) + } const canvas = new OffscreenCanvas(this.webglWidth, this.webglHeight) const gl = canvas.getContext('webgl2') @@ -57,12 +54,8 @@ export class Pow { if (!gl) throw new Error('webgl2_required') - if (!/^[A-F-a-f0-9]{64}$/.test(hashHex)) - throw new Error(`invalid_hash ${hashHex}`) - gl.clearColor(0, 0, 0, 1) - const reverseHex = this.hex_reverse(hashHex) // Vertext Shader const vsSource = `#version 300 es -- 2.34.1