this.#query = this.#gl.createQuery()
}
- /**
- * Finds a nonce that satisfies the Nano proof-of-work requirements.
- *
- * @param {string} hash - Hexadecimal hash of previous block, or public key for new accounts
- * @param {number} [threshold=0xfffffff8] - Difficulty of proof-of-work calculation
- */
- static async search (hash: string, threshold: number = 0xfffffff8): Promise<string> {
- if (NanoPowGl.#gl == null) throw new Error('WebGL 2 is required')
- if (!/^[A-F-a-f0-9]{64}$/.test(hash)) throw new Error(`invalid_hash ${hash}`)
- if (typeof threshold !== 'number') throw new TypeError(`Invalid threshold ${threshold}`)
- if (this.#gl == null) throw new Error('WebGL 2 is required')
-
- /** Set up uniform buffer object */
- const uboView = new DataView(new ArrayBuffer(144))
- for (let i = 0; i < 64; i += 8) {
- const uint32 = hash.slice(i, i + 8)
- uboView.setUint32(i * 2, parseInt(uint32, 16))
- }
- uboView.setUint32(128, threshold, true)
- uboView.setFloat32(132, NanoPowGl.#WORKLOAD - 1, true)
- NanoPowGl.#gl.bindBuffer(NanoPowGl.#gl.UNIFORM_BUFFER, NanoPowGl.#uboBuffer)
- NanoPowGl.#gl.bufferSubData(NanoPowGl.#gl.UNIFORM_BUFFER, 0, uboView)
- NanoPowGl.#gl.bindBuffer(NanoPowGl.#gl.UNIFORM_BUFFER, null)
-
- /** Start drawing to calculate one nonce per pixel */
- let nonce = null
- const work = new Uint8Array(8)
- while (nonce == null) {
- this.#draw(work)
- const found = await this.#checkQueryResult()
- if (found) {
- nonce = this.#readResult(work)
- }
- }
- return nonce
- }
-
static #draw (work: Uint8Array): void {
if (this.#gl == null || this.#query == null) throw new Error('WebGL 2 is required to draw and query pixels')
if (this.#workBuffer == null) throw new Error('Work buffer is required to draw')
}
throw new Error('Query reported result but nonce value not found')
}
+
+ /**
+ * Finds a nonce that satisfies the Nano proof-of-work requirements.
+ *
+ * @param {string} hash - Hexadecimal hash of previous block, or public key for new accounts
+ * @param {number} [threshold=0xfffffff8] - Difficulty of proof-of-work calculation
+ */
+ static async search (hash: string, threshold: number = 0xfffffff8): Promise<string> {
+ if (NanoPowGl.#gl == null) throw new Error('WebGL 2 is required')
+ if (!/^[A-F-a-f0-9]{64}$/.test(hash)) throw new Error(`invalid_hash ${hash}`)
+ if (typeof threshold !== 'number') throw new TypeError(`Invalid threshold ${threshold}`)
+ if (this.#gl == null) throw new Error('WebGL 2 is required')
+
+ /** Set up uniform buffer object */
+ const uboView = new DataView(new ArrayBuffer(144))
+ for (let i = 0; i < 64; i += 8) {
+ const uint32 = hash.slice(i, i + 8)
+ uboView.setUint32(i * 2, parseInt(uint32, 16))
+ }
+ uboView.setUint32(128, threshold, true)
+ uboView.setFloat32(132, NanoPowGl.#WORKLOAD - 1, true)
+ NanoPowGl.#gl.bindBuffer(NanoPowGl.#gl.UNIFORM_BUFFER, NanoPowGl.#uboBuffer)
+ NanoPowGl.#gl.bufferSubData(NanoPowGl.#gl.UNIFORM_BUFFER, 0, uboView)
+ NanoPowGl.#gl.bindBuffer(NanoPowGl.#gl.UNIFORM_BUFFER, null)
+
+ /** Start drawing to calculate one nonce per pixel */
+ let nonce = null
+ const work = new Uint8Array(8)
+ while (nonce == null) {
+ this.#draw(work)
+ const found = await this.#checkQueryResult()
+ if (found) {
+ nonce = this.#readResult(work)
+ }
+ }
+ return nonce
+ }
}