]> git.codecow.com Git - nano-pow.git/commitdiff
Validate input prior to setting busy flag to avoid perpetual busy status.
authorChris Duncan <chris@zoso.dev>
Sat, 25 Jan 2025 07:11:43 +0000 (23:11 -0800)
committerChris Duncan <chris@zoso.dev>
Sat, 25 Jan 2025 07:11:43 +0000 (23:11 -0800)
src/classes/gl.ts
src/classes/gpu.ts

index 871fa2d26f7627a8614635e690bc698d4850c16c..fe3d56cda6cea54378c5569f421387a134cce177 100644 (file)
@@ -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<string> {
+               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<void> => {
@@ -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<boolean> {
+               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<void> => {
@@ -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
index 5a3ba0418993f17cd0ad61cdccf782e7eed87bbd..61688af434e288287810ed4b8a1ad21eef3f23df 100644 (file)
@@ -205,6 +205,7 @@ export class NanoPowGpu {
        * @param {NanoPowOptions} options - Used to configure search execution
        */
        static async search (hash: string, options?: NanoPowOptions): Promise<string> {
+               if (!/^[A-Fa-f0-9]{64}$/.test(hash)) throw new TypeError(`Invalid hash ${hash}`)
                if (this.#busy) {
                        return new Promise(resolve => {
                                setTimeout(async (): Promise<void> => {
@@ -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<boolean> {
+               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<void> => {
@@ -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)