From be1863b74efb60cb914029aa84f6a91d62639ee6 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Wed, 11 Dec 2024 13:55:23 -0800 Subject: [PATCH] Remove progress callback and reorganize Pow parameters. Increase canvas size and remove loop since apparently shaders are not designed for long-running operations. --- perf/block.perf.js | 2 +- src/lib/workers/pow.ts | 26 +++++--------------------- 2 files changed, 6 insertions(+), 22 deletions(-) diff --git a/perf/block.perf.js b/perf/block.perf.js index 74ebaf9..00f6e57 100644 --- a/perf/block.perf.js +++ b/perf/block.perf.js @@ -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) diff --git a/src/lib/workers/pow.ts b/src/lib/workers/pow.ts index 925ec02..78ba3b2 100644 --- a/src/lib/workers/pow.ts +++ b/src/lib/workers/pow.ts @@ -6,7 +6,7 @@ export class Pow { static async find (hash: string, threshold: string = this.SEND_THRESHOLD): Promise { return new Promise(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) => any, progressCallback?: (frames: number) => any, threshold: number | string = '0xFFFFFFF8'): void { + static calculate (hashHex: string, threshold: number | string = '0xFFFFFFF8', callback: (nonce: string | PromiseLike) => 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) -- 2.34.1