From efa6102dd4edc957274a7c754788e2ac8182f9ea Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Sun, 12 Jan 2025 11:46:00 -0800 Subject: [PATCH] Move private methods above public. --- src/classes/gl.ts | 74 +++++++++++++++++++++++------------------------ 1 file changed, 37 insertions(+), 37 deletions(-) diff --git a/src/classes/gl.ts b/src/classes/gl.ts index 81ec49b..d7423d2 100644 --- a/src/classes/gl.ts +++ b/src/classes/gl.ts @@ -100,43 +100,6 @@ export class NanoPowGl { 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 { - 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') @@ -196,4 +159,41 @@ export class NanoPowGl { } 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 { + 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 + } } -- 2.34.1