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.
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. */
NanoPowGl.#gl = null
NanoPowGl.#busy = false
NanoPowGl.init()
+ if (this.#call) console.log('call')
}
static #logAverages (times: number[]): void {
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
}
})
}
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}`)
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'
/**
* 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
}
}
+/**
+* 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.
*