]> zoso.dev Git - nano-pow.git/commitdiff
Move private methods above public.
authorChris Duncan <chris@zoso.dev>
Sun, 12 Jan 2025 19:46:00 +0000 (11:46 -0800)
committerChris Duncan <chris@zoso.dev>
Sun, 12 Jan 2025 19:46:00 +0000 (11:46 -0800)
src/classes/gl.ts

index 81ec49b2233ef860a9108217d738b28bd55df3a5..d7423d20005313f06b761944b3a72f33b7b91a09 100644 (file)
@@ -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<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')
@@ -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<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
+       }
 }