From a00b508f2dc7a4e2232f4ebf50c5b677d58c009f Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Thu, 20 Mar 2025 15:02:05 -0700 Subject: [PATCH] Create type to store execution details to retry on context loss. Add initial recovery statements. --- src/lib/gl/index.ts | 33 ++++++++++++++++++++++++++++++--- src/types.d.ts | 23 ++++++++++++++++++++--- 2 files changed, 50 insertions(+), 6 deletions(-) diff --git a/src/lib/gl/index.ts b/src/lib/gl/index.ts index a1d45ee..ed39a9e 100644 --- a/src/lib/gl/index.ts +++ b/src/lib/gl/index.ts @@ -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' diff --git a/src/types.d.ts b/src/types.d.ts index fc5aa59..49116ae 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -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. * -- 2.34.1