]> zoso.dev Git - nano-pow.git/commitdiff
Create type to store execution details to retry on context loss. Add initial recovery...
authorChris Duncan <chris@zoso.dev>
Thu, 20 Mar 2025 22:02:05 +0000 (15:02 -0700)
committerChris Duncan <chris@zoso.dev>
Thu, 20 Mar 2025 22:02:05 +0000 (15:02 -0700)
src/lib/gl/index.ts
src/types.d.ts

index a1d45eefde84a87ddd9689bb9c6e30e2ae584092..ed39a9ed86be0ac2a26b138be45ca5e0c10e60bb 100644 (file)
@@ -5,7 +5,7 @@
 import { default as NanoPowGlDownsampleShader } from './gl-downsample.frag'
 import { default as NanoPowGlDrawShader } from './gl-draw.frag'
 import { default as NanoPowGlVertexShader } from './gl-vertex.vert'
-import type { FBO, NanoPowOptions, WorkGenerateRequest, WorkGenerateResponse, WorkValidateRequest, WorkValidateResponse } from '../../types.d.ts'
+import type { FBO, NanoPowExecution, NanoPowOptions, WorkGenerateRequest, WorkGenerateResponse, WorkValidateRequest, WorkValidateResponse } from '../../types.d.ts'
 
 /**
 * Nano proof-of-work using WebGL 2.0.
@@ -15,6 +15,7 @@ export class NanoPowGl {
        static #RECEIVE: bigint = 0xfffffe0000000000n
 
        static #busy: boolean = false
+       static #call: NanoPowExecution | null = null
        static #debug: boolean = false
        static #raf: number = 0
        /** Used to set canvas size. */
@@ -221,6 +222,7 @@ export class NanoPowGl {
                NanoPowGl.#gl = null
                NanoPowGl.#busy = false
                NanoPowGl.init()
+               if (this.#call) console.log('call')
        }
 
        static #logAverages (times: number[]): void {
@@ -359,7 +361,17 @@ export class NanoPowGl {
                if (options?.threshold != null) {
                        options.threshold = BigInt(`0x${options.threshold.toString(16) ?? '0'}00000000`)
                }
-               const result = await this.work_generate(hash, options)
+               this.#call = {
+                       method: 'search',
+                       hash,
+                       options
+               }
+               let result
+               try {
+                       result = await this.work_generate(hash, options)
+               } finally {
+                       this.#call = null
+               }
                return result.work
        }
 
@@ -380,6 +392,11 @@ export class NanoPowGl {
                        })
                }
                this.#busy = true
+               this.#call ??= {
+                       method: 'work_generate',
+                       hash,
+                       options
+               }
 
                /** Process user input */
                if (!/^[A-Fa-f0-9]{64}$/.test(hash)) throw new Error(`Invalid hash ${hash}`)
@@ -465,7 +482,17 @@ export class NanoPowGl {
                if (options?.threshold != null) {
                        options.threshold = BigInt(`0x${options.threshold.toString(16) ?? '0'}00000000`)
                }
-               const result = await this.work_validate(work, hash, options)
+               this.#call = {
+                       method: 'search',
+                       hash,
+                       options
+               }
+               let result
+               try {
+                       result = await this.work_validate(work, hash, options)
+               } finally {
+                       this.#call = null
+               }
                return (options?.threshold != null)
                        ? result.valid === '1'
                        : result.valid_all === '1'
index fc5aa59f2c28bce58ff8eed93083d2d3bcbb12d3..49116ae30b174c2c3bedb30415dde5dd5c3d217a 100644 (file)
@@ -73,9 +73,11 @@ declare const NanoPow: typeof NanoPowGl | typeof NanoPowGpu | null
 /**
 * Used to create WebGL framebuffer objects.
 *
-* @param {WebGLTexture} - Defines storage size
-* @param {WebGLFramebuffer} - Holds texture data
-* @param {size} - 2D lengths of texture
+* @param {WebGLTexture} texture - Defines storage size
+* @param {WebGLFramebuffer} framebuffer - Holds texture data
+* @param {Object} size - 2D lengths of texture
+* @param {number} size.x - length of texture on X-axis
+* @param {number} size.y - length of texture on Y-axis
 */
 export type FBO = {
        texture: WebGLTexture
@@ -86,6 +88,21 @@ export type FBO = {
        }
 }
 
+/**
+* Used to retry a NanoPow generate or validation execution when context is lost.
+*
+* @param {string} method - NanoPow method that was called
+* @param {string} hash - Block hash passed to method
+* @param {number} [work] - Work value passed to `validate` or `work_validate` method
+* @param {number} [options] - Options passed to method
+*/
+export type NanoPowExecution = {
+       method: 'search' | 'validate' | 'work_generate' | 'work_validate'
+       hash: string
+       work?: string
+       options?: NanoPowOptions
+}
+
 /**
 * Used to configure NanoPow.
 *