From: Chris Duncan Date: Wed, 8 Jan 2025 15:30:56 +0000 (-0800) Subject: Simplify imports with additional path mappings. Remove deprecated worker code from... X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=3686ca8db5e3d96580abcf64bfe2cc87be95dcfc;p=libnemo.git Simplify imports with additional path mappings. Remove deprecated worker code from pow classes now that it is handled externally. --- diff --git a/package.json b/package.json index cfa32f8..2620686 100644 --- a/package.json +++ b/package.json @@ -53,7 +53,10 @@ "#~/*": "./src/lib/*", "#dist/*": "./dist/*", "#test/*": "./test/*", - "#nano-pow/*": "./src/lib/nano-pow/*" + "#nano-pow": "./src/lib/nano-pow/index.js", + "#nano-pow/*": "./src/lib/nano-pow/*", + "#workers": "./src/lib/workers/index.js", + "#workers/*": "./src/lib/workers/*" }, "optionalDependencies": { "@ledgerhq/hw-transport-web-ble": "^6.29.4", diff --git a/src/lib/account.ts b/src/lib/account.ts index 001208c..58570ef 100644 --- a/src/lib/account.ts +++ b/src/lib/account.ts @@ -6,7 +6,7 @@ import { ACCOUNT_KEY_LENGTH, ALPHABET, PREFIX, PREFIX_LEGACY } from './constants import { base32, bytes, hex } from './convert.js' import { Rpc } from './rpc.js' import { Safe } from './safe.js' -import { NanoNaCl } from './workers/nano-nacl.js' +import { NanoNaCl } from '#workers/nano-nacl.js' /** * Represents a single Nano address and the associated public key. To include the diff --git a/src/lib/block.ts b/src/lib/block.ts index 7d669d5..d32c5e1 100644 --- a/src/lib/block.ts +++ b/src/lib/block.ts @@ -5,10 +5,10 @@ import { BURN_ADDRESS, PREAMBLE, THRESHOLD_RECEIVE, THRESHOLD_SEND } from './con import { Account } from './account.js' import { Blake2b } from './blake2b.js' import { dec, hex } from './convert.js' -import { NanoNaCl } from './workers/nano-nacl.js' +import { NanoNaCl } from '#workers/nano-nacl.js' import { Pool } from './pool.js' import { Rpc } from './rpc.js' -import { NanoPow } from './workers.js' +import { NanoPow } from '#workers' /** * Represents a block as defined by the Nano cryptocurrency protocol. The Block diff --git a/src/lib/nano-pow/classes/gl.ts b/src/lib/nano-pow/classes/gl.ts index 055dc14..34edf17 100644 --- a/src/lib/nano-pow/classes/gl.ts +++ b/src/lib/nano-pow/classes/gl.ts @@ -2,33 +2,9 @@ // SPDX-License-Identifier: GPL-3.0-or-later // Based on nano-webgl-pow by Ben Green (numtel) // https://github.com/numtel/nano-webgl-pow -// import { WorkerInterface } from '#~/pool.js' -import { NanoPowGlFragmentShader, NanoPowGlVertexShader } from '#nano-pow/shaders/index.js' - -// export class NanoPowGl extends WorkerInterface { -// static { -// NanoPowGl.listen() -// } -// /** -// * Calculates proof-of-work as described by the Nano cryptocurrency protocol. -// * -// * @param {any[]} data - Array of hashes and minimum thresholds -// * @returns Promise for proof-of-work attached to original array objects -// */ -// static async work (data: any[]): Promise { -// return new Promise(async (resolve, reject): Promise => { -// for (const d of data) { -// try { -// d.work = await this.search(d.hash, d.threshold) -// } catch (err) { -// reject(err) -// } -// } -// resolve(data) -// }) -// } -export class NanoPowGl { +import { NanoPowGlFragmentShader, NanoPowGlVertexShader } from '../shaders/index.js' +export class NanoPowGl { /** Used to set canvas size. Must be a multiple of 256. */ static #WORKLOAD: number = 256 * Math.max(1, Math.floor(navigator.hardwareConcurrency)) @@ -50,17 +26,17 @@ export class NanoPowGl { static #workBuffer: WebGLBuffer | null static #query: WebGLQuery | null static #pixels: Uint8Array - // Vertex Positions, 2 triangles + /**Vertex Positions, 2 triangles */ static #positions = new Float32Array([ -1, -1, 0, -1, 1, 0, 1, 1, 0, 1, -1, 0, 1, 1, 0, -1, -1, 0 ]) - // Texture Positions + /** Texture Positions */ static #uvPosArray = new Float32Array([ 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1 ]) - // Compile + /** Compile */ static { this.#gl = new OffscreenCanvas(this.#WORKLOAD, this.#WORKLOAD).getContext('webgl2') if (this.#gl == null) throw new Error('WebGL 2 is required') @@ -89,7 +65,7 @@ export class NanoPowGl { if (!this.#gl.getProgramParameter(this.#program, this.#gl.LINK_STATUS)) throw new Error(this.#gl.getProgramInfoLog(this.#program) ?? `Failed to link program`) - // Construct simple 2D geometry + /** Construct simple 2D geometry */ this.#gl.useProgram(this.#program) const triangleArray = this.#gl.createVertexArray() this.#gl.bindVertexArray(triangleArray) @@ -136,7 +112,7 @@ export class NanoPowGl { 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 + /** 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) @@ -148,7 +124,7 @@ export class NanoPowGl { 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 + /** Start drawing to calculate one nonce per pixel */ let nonce = null const work = new Uint8Array(8) while (nonce == null) { @@ -166,7 +142,7 @@ export class NanoPowGl { if (this.#workBuffer == null) throw new Error('Work buffer is required to draw') this.#gl.clear(this.#gl.COLOR_BUFFER_BIT) - // Upload work buffer + /** Upload work buffer */ crypto.getRandomValues(work) this.#gl.bindBuffer(this.#gl.UNIFORM_BUFFER, this.#workBuffer) this.#gl.bufferSubData(this.#gl.UNIFORM_BUFFER, 0, Uint32Array.from(work)) @@ -182,7 +158,7 @@ export class NanoPowGl { if (this.#gl.getQueryParameter(this.#query, this.#gl.QUERY_RESULT_AVAILABLE)) { return this.#gl.getQueryParameter(this.#query, this.#gl.QUERY_RESULT) } - // Query result not yet available, check again in the next frame + /** Query result not yet available, check again in the next frame */ return new Promise((resolve, reject): void => { try { requestAnimationFrame(async (): Promise => { @@ -208,7 +184,7 @@ export class NanoPowGl { this.#gl.readPixels(0, 0, this.#gl.drawingBufferWidth, this.#gl.drawingBufferHeight, this.#gl.RGBA, this.#gl.UNSIGNED_BYTE, this.#pixels) for (let i = 0; i < this.#pixels.length; i += 4) { if (this.#pixels[i] !== 0) { - // Return the work value with the custom bits + /** Return the work value with the custom bits */ const hex = this.#hexify(work.subarray(4, 8)) + this.#hexify([ this.#pixels[i + 2], this.#pixels[i + 3], @@ -222,13 +198,6 @@ export class NanoPowGl { } } -// export default ` -// const NanoPowGlFragmentShader = ${NanoPowGlFragmentShader} -// const NanoPowGlVertexShader = ${NanoPowGlVertexShader} -// // const WorkerInterface = ${WorkerInterface} -// const PowGl = ${NanoPowGl} -// ` - export default ` const NanoPowGlFragmentShader = \`${NanoPowGlFragmentShader}\` const NanoPowGlVertexShader = \`${NanoPowGlVertexShader}\` diff --git a/src/lib/nano-pow/classes/gpu.ts b/src/lib/nano-pow/classes/gpu.ts index b225007..1478673 100644 --- a/src/lib/nano-pow/classes/gpu.ts +++ b/src/lib/nano-pow/classes/gpu.ts @@ -2,36 +2,11 @@ // SPDX-License-Identifier: GPL-3.0-or-later // BLAKE2b hashing implementation derived from nano-webgl-pow by Ben Green (https://github.com/numtel/nano-webgl-pow) /// -// import { WorkerInterface } from '#~/pool.js' -import { NanoPowGpuComputeShader } from '#nano-pow/shaders/index.js' +import { NanoPowGpuComputeShader } from '../shaders/index.js' /** * Nano proof-of-work using WebGPU. */ -// export class NanoPowGpu extends WorkerInterface { -// static { -// NanoPowGpu.listen() -// } - -// /** -// * Calculates proof-of-work as described by the Nano cryptocurrency protocol. -// * -// * @param {any[]} data - Array of hashes and minimum thresholds -// * @returns Promise for proof-of-work attached to original array objects -// */ -// static async work (data: any[]): Promise { -// return new Promise(async (resolve, reject): Promise => { -// for (const d of data) { -// try { -// d.work = await this.search(d.hash, d.threshold) -// } catch (err) { -// reject(err) -// } -// } -// resolve(data) -// }) -// } -// */ export class NanoPowGpu { // Initialize WebGPU @@ -212,12 +187,6 @@ export class NanoPowGpu { } } -// export default ` -// const NanoPowGpuComputeShader = \`${NanoPowGpuComputeShader}\` -// const WorkerInterface = ${WorkerInterface} -// const NanoPowGpu = ${NanoPowGpu} -// ` - export default ` const NanoPowGpuComputeShader = \`${NanoPowGpuComputeShader}\` const NanoPowGpu = ${NanoPowGpu} diff --git a/src/lib/nano-pow/classes/index.ts b/src/lib/nano-pow/classes/index.ts new file mode 100644 index 0000000..b7298fa --- /dev/null +++ b/src/lib/nano-pow/classes/index.ts @@ -0,0 +1,7 @@ +import { NanoPowGl } from "./gl" +import { NanoPowGpu } from "./gpu" + +export { + NanoPowGl, + NanoPowGpu +} diff --git a/src/lib/nano-pow/index.ts b/src/lib/nano-pow/index.ts index bc02f2d..3e00b77 100644 --- a/src/lib/nano-pow/index.ts +++ b/src/lib/nano-pow/index.ts @@ -1,4 +1,18 @@ -import { NanoPowGl } from "./classes/gl" -import { NanoPowGpu } from "./classes/gpu" +import { + NanoPowGpuComputeShader, + NanoPowGlFragmentShader, + NanoPowGlVertexShader +} from "./shaders" -export { NanoPowGl, NanoPowGpu } +import { + NanoPowGl, + NanoPowGpu +} from "./classes/" + +export { + NanoPowGpuComputeShader, + NanoPowGlFragmentShader, + NanoPowGlVertexShader, + NanoPowGl, + NanoPowGpu +} diff --git a/src/lib/nano-pow/shaders/index.ts b/src/lib/nano-pow/shaders/index.ts index 6f6ed19..9629261 100644 --- a/src/lib/nano-pow/shaders/index.ts +++ b/src/lib/nano-pow/shaders/index.ts @@ -2,4 +2,8 @@ import { NanoPowGpuComputeShader } from "./gpu-compute" import { NanoPowGlFragmentShader } from "./gl-fragment" import { NanoPowGlVertexShader } from "./gl-vertex" -export { NanoPowGpuComputeShader, NanoPowGlFragmentShader, NanoPowGlVertexShader } +export { + NanoPowGpuComputeShader, + NanoPowGlFragmentShader, + NanoPowGlVertexShader +} diff --git a/src/lib/wallet.ts b/src/lib/wallet.ts index 33ca4c3..e1f8910 100644 --- a/src/lib/wallet.ts +++ b/src/lib/wallet.ts @@ -9,7 +9,7 @@ import { Entropy } from './entropy.js' import { Pool } from './pool.js' import { Rpc } from './rpc.js' import { Safe } from './safe.js' -import { Bip44Ckd, NanoNaCl } from './workers.js' +import { Bip44Ckd, NanoNaCl } from '#workers' import type { Ledger } from './ledger.js' type KeyPair = { diff --git a/src/lib/workers.ts b/src/lib/workers/index.ts similarity index 82% rename from src/lib/workers.ts rename to src/lib/workers/index.ts index f91e41e..bdb75d9 100644 --- a/src/lib/workers.ts +++ b/src/lib/workers/index.ts @@ -3,5 +3,9 @@ import { default as Bip44Ckd } from '#~/workers/bip44-ckd.js' import { default as NanoNaCl } from '#~/workers/nano-nacl.js' import { default as NanoPow } from '#~/workers/nano-pow.js' -console.log(NanoPow) -export { Bip44Ckd, NanoNaCl, NanoPow } + +export { + Bip44Ckd, + NanoNaCl, + NanoPow +} diff --git a/src/lib/workers/nano-pow.ts b/src/lib/workers/nano-pow.ts index 49235f4..14e0b29 100644 --- a/src/lib/workers/nano-pow.ts +++ b/src/lib/workers/nano-pow.ts @@ -2,10 +2,10 @@ // SPDX-License-Identifier: GPL-3.0-or-later import { WorkerInterface } from '#~/pool.js' import { NanoPowGpuComputeShader, NanoPowGlFragmentShader, NanoPowGlVertexShader } from '#nano-pow/shaders/index.js' -import { NanoPowGl, NanoPowGpu } from '#nano-pow/index.js' +import { NanoPowGl, NanoPowGpu } from '#nano-pow/classes/index.js' /** -* Nano proof-of-work using WebGPU. +* Nano proof-of-work using WebGPU and Web Workers. */ export class NanoPow extends WorkerInterface { static { diff --git a/src/main.ts b/src/main.ts index 163c934..41c393d 100644 --- a/src/main.ts +++ b/src/main.ts @@ -4,7 +4,7 @@ import { Account } from './lib/account.js' import { Blake2b } from './lib/blake2b.js' import { SendBlock, ReceiveBlock, ChangeBlock } from './lib/block.js' -import { NanoPowGl, NanoPowGpu } from '#nano-pow/index.js' +import { NanoPowGl, NanoPowGpu } from '#nano-pow' import { Rpc } from './lib/rpc.js' import { Rolodex } from './lib/rolodex.js' import { Safe } from './lib/safe.js'