]> zoso.dev Git - libnemo.git/commitdiff
Remove progress callback and reorganize Pow parameters. Increase canvas size and...
authorChris Duncan <chris@zoso.dev>
Wed, 11 Dec 2024 21:55:23 +0000 (13:55 -0800)
committerChris Duncan <chris@zoso.dev>
Wed, 11 Dec 2024 21:55:23 +0000 (13:55 -0800)
perf/block.perf.js
src/lib/workers/pow.ts

index 74ebaf92cbeaad1df903cb2cf546e691e8fc5cc6..00f6e570c3abed893389191f861b22ece0fe7db8 100644 (file)
@@ -25,7 +25,7 @@ await suite('Block performance', async () => {
                        await block.pow()
                        const end = performance.now()
                        times.push(end - start)
-                       console.log(`${end - start} ms`)
+                       console.log(`${block.work} (${end - start} ms)`)
                }
                console.log(block.work)
                const { total, arithmetic, harmonic, geometric } = average(times)
index 925ec023a6f051cc229eae608aaef3ed78069ead..78ba3b2a94267322a4807db52de7a310aebbcf0c 100644 (file)
@@ -6,7 +6,7 @@ export class Pow {
 
        static async find (hash: string, threshold: string = this.SEND_THRESHOLD): Promise<string> {
                return new Promise<string>(resolve => {
-                       this.calculate(hash, resolve, undefined, threshold)
+                       this.calculate(hash, threshold, resolve)
                })
        }
 
@@ -29,8 +29,8 @@ export class Pow {
        // Both width and height must be multiple of 256, (one byte)
        // but do not need to be the same,
        // matching GPU capabilities is the aim
-       static webglWidth = 256 * 1
-       static webglHeight = 256 * 1
+       static webglWidth = 256 * 8
+       static webglHeight = 256 * 8
 
        static hexify (arr: number[] | Uint8Array): string {
                let out = ''
@@ -48,7 +48,7 @@ export class Pow {
                return out
        }
 
-       static calculate (hashHex: string, callback: (nonce: string | PromiseLike<string>) => any, progressCallback?: (frames: number) => any, threshold: number | string = '0xFFFFFFF8'): void {
+       static calculate (hashHex: string, threshold: number | string = '0xFFFFFFF8', callback: (nonce: string | PromiseLike<string>) => any): void {
                if (typeof threshold === 'number') threshold = '0x' + threshold.toString(16)
 
                const canvas = new OffscreenCanvas(this.webglWidth, this.webglHeight)
@@ -189,8 +189,7 @@ export class Pow {
                        v[b + 1] = (xor0 >> 31) ^ (xor1 << 1);
                }
 
-               bool found = false;
-               void find() {
+               void main() {
                        int i;
                        uint uv_x = uint(uv_pos.x * ${canvas.width - 1}.);
                        uint uv_y = uint(uv_pos.y * ${canvas.height - 1}.);
@@ -232,7 +231,6 @@ export class Pow {
                        //  only calculate digest of the second 4 bytes
                        if((BLAKE2B_IV32_1 ^ v[1] ^ v[17]) > ` + threshold + `u) {
                                // Success found, return pixel data so work value can be constructed
-                               found = true;
                                fragColor = vec4(
                                        float(x_index + 1u)/255., // +1 to distinguish from 0 (unsuccessful) pixels
                                        float(y_index + 1u)/255., // Same as previous
@@ -240,14 +238,6 @@ export class Pow {
                                        float(y_pos)/255.  // Second custom byte
                                );
                        }
-               }
-
-               void main() {
-                       int count = 0;
-                       do {
-                               count++;
-                               find();
-                       } while (!found && count < 10);
                }`
 
                const vertexShader = gl.createShader(gl.VERTEX_SHADER)
@@ -311,20 +301,14 @@ export class Pow {
                // Draw output until success or progressCallback says to stop
                const work0 = new Uint8Array(4)
                const work1 = new Uint8Array(4)
-               let n = 0
 
                const draw = (): void => {
-                       n++
                        crypto.getRandomValues(work0)
                        crypto.getRandomValues(work1)
 
                        gl.uniform4uiv(work0Location, work0)
                        gl.uniform4uiv(work1Location, work1)
 
-                       // Check with progressCallback every 100 frames
-                       if (n % 100 === 0 && typeof progressCallback === 'function' && progressCallback(n))
-                               return
-
                        gl.clear(gl.COLOR_BUFFER_BIT)
                        gl.drawArrays(gl.TRIANGLES, 0, 6)
                        const pixels = new Uint8Array(gl.drawingBufferWidth * gl.drawingBufferHeight * 4)