From 9d8999b9d1e7529abb53572f831cbf37223e8f1a Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Fri, 24 Jan 2025 23:11:43 -0800 Subject: [PATCH] Validate input prior to setting busy flag to avoid perpetual busy status. --- src/classes/gl.ts | 14 +++++++------- src/classes/gpu.ts | 16 +++++++++++----- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/classes/gl.ts b/src/classes/gl.ts index 871fa2d..fe3d56c 100644 --- a/src/classes/gl.ts +++ b/src/classes/gl.ts @@ -218,6 +218,9 @@ export class NanoPowGl { * @param {number} [threshold=0xfffffff8] - Difficulty of proof-of-work calculation */ static async search (hash: string, options?: NanoPowOptions): Promise { + if (NanoPowGl.#gl == null) throw new Error('WebGL 2 is required') + if (this.#gl == null) throw new Error('WebGL 2 is required') + if (!/^[A-Fa-f0-9]{64}$/.test(hash)) throw new Error(`Invalid hash ${hash}`) if (this.#busy) { return new Promise(resolve => { setTimeout(async (): Promise => { @@ -227,9 +230,6 @@ export class NanoPowGl { }) } this.#busy = true - if (NanoPowGl.#gl == null) throw new Error('WebGL 2 is required') - if (this.#gl == null) throw new Error('WebGL 2 is required') - if (!/^[A-Fa-f0-9]{64}$/.test(hash)) throw new Error(`Invalid hash ${hash}`) const threshold = (typeof options?.threshold !== 'number' || options.threshold < 0x0 || options.threshold > 0xffffffff) ? 0xfffffff8 : options.threshold @@ -284,6 +284,10 @@ export class NanoPowGl { * @param {number} [threshold=0xfffffff8] - Difficulty of proof-of-work calculation */ static async validate (work: string, hash: string, options?: NanoPowOptions): Promise { + if (NanoPowGl.#gl == null) throw new Error('WebGL 2 is required') + if (this.#gl == null) throw new Error('WebGL 2 is required') + if (!/^[A-Fa-f0-9]{16}$/.test(work)) throw new Error(`Invalid work ${work}`) + if (!/^[A-Fa-f0-9]{64}$/.test(hash)) throw new Error(`Invalid hash ${hash}`) if (this.#busy) { return new Promise(resolve => { setTimeout(async (): Promise => { @@ -293,10 +297,6 @@ export class NanoPowGl { }) } this.#busy = true - if (NanoPowGl.#gl == null) throw new Error('WebGL 2 is required') - if (this.#gl == null) throw new Error('WebGL 2 is required') - if (!/^[A-Fa-f0-9]{16}$/.test(work)) throw new Error(`Invalid work ${work}`) - if (!/^[A-Fa-f0-9]{64}$/.test(hash)) throw new Error(`Invalid hash ${hash}`) const threshold = (typeof options?.threshold !== 'number' || options.threshold < 0x0 || options.threshold > 0xffffffff) ? 0xfffffff8 : options.threshold diff --git a/src/classes/gpu.ts b/src/classes/gpu.ts index 5a3ba04..61688af 100644 --- a/src/classes/gpu.ts +++ b/src/classes/gpu.ts @@ -205,6 +205,7 @@ export class NanoPowGpu { * @param {NanoPowOptions} options - Used to configure search execution */ static async search (hash: string, options?: NanoPowOptions): Promise { + if (!/^[A-Fa-f0-9]{64}$/.test(hash)) throw new TypeError(`Invalid hash ${hash}`) if (this.#busy) { return new Promise(resolve => { setTimeout(async (): Promise => { @@ -214,7 +215,6 @@ export class NanoPowGpu { }) } this.#busy = true - if (!/^[A-Fa-f0-9]{64}$/.test(hash)) throw new TypeError(`Invalid hash ${hash}`) const threshold = (typeof options?.threshold !== 'number' || options.threshold < 0x0 || options.threshold > 0xffffffff) ? 0xfffffff8 : options.threshold @@ -230,7 +230,10 @@ export class NanoPowGpu { setTimeout(resolve, 500) }) } - if (this.#device == null) throw new Error(`WebGPU device failed to load.`) + if (this.#device == null) { + this.#busy = false + throw new Error(`WebGPU device failed to load.`) + } let times = [] let start = performance.now() @@ -256,6 +259,8 @@ export class NanoPowGpu { * @param {NanoPowOptions} options - Options used to configure search execution */ static async validate (work: string, hash: string, options?: NanoPowOptions): Promise { + if (!/^[A-Fa-f0-9]{16}$/.test(work)) throw new TypeError(`Invalid work ${work}`) + if (!/^[A-Fa-f0-9]{64}$/.test(hash)) throw new TypeError(`Invalid hash ${hash}`) if (this.#busy) { return new Promise(resolve => { setTimeout(async (): Promise => { @@ -265,8 +270,6 @@ export class NanoPowGpu { }) } this.#busy = true - if (!/^[A-Fa-f0-9]{16}$/.test(work)) throw new TypeError(`Invalid work ${work}`) - if (!/^[A-Fa-f0-9]{64}$/.test(hash)) throw new TypeError(`Invalid hash ${hash}`) const debug = !!(options?.debug) const threshold = (typeof options?.threshold !== 'number' || options.threshold < 0x0 || options.threshold > 0xffffffff) ? 0xfffffff8 @@ -279,7 +282,10 @@ export class NanoPowGpu { setTimeout(resolve, 500) }) } - if (this.#device == null) throw new Error(`WebGPU device failed to load.`) + if (this.#device == null) { + this.#busy = false + throw new Error(`WebGPU device failed to load.`) + } const seed = BigInt(`0x${work}`) const data = await this.#dispatch(this.#validatePipeline, seed, hash, threshold, 1) -- 2.47.3