]> zoso.dev Git - nano-pow.git/commitdiff
Replace validate shared code with dispatch call.
authorChris Duncan <chris@zoso.dev>
Sun, 12 Jan 2025 07:28:15 +0000 (23:28 -0800)
committerChris Duncan <chris@zoso.dev>
Sun, 12 Jan 2025 07:28:15 +0000 (23:28 -0800)
src/classes/gpu.ts
test.html

index 42659cc66bee51e4c6a2e9659cabc7cbf97bf216..3e7c325ae01f93b18e4a4b1c0ddf5405ae3eeba0 100644 (file)
@@ -225,68 +225,7 @@ export class NanoPowGpu {
                }
                if (this.#device == null) throw new Error(`WebGPU device failed to load.`)
 
-               // Set up uniform buffer object
-               // Note: u32 size is 4, but total alignment must be multiple of 16
-               const uboView = new DataView(new ArrayBuffer(48))
-               for (let i = 0; i < 64; i += 8) {
-                       const uint32 = hash.slice(i, i + 8)
-                       uboView.setUint32(i / 2, parseInt(uint32, 16))
-               }
-               uboView.setBigUint64(32, BigInt(`0x${work}`), true)
-               uboView.setUint32(40, threshold, true)
-               this.#device.queue.writeBuffer(this.#uboBuffer, 0, uboView)
-
-               // Reset `nonce` and `found` to 0u in WORK before each calculation
-               this.#device.queue.writeBuffer(this.#gpuBuffer, 0, new Uint32Array([0, 0, 0]))
-
-               // Bind UBO read and GPU write buffers
-               const bindGroup = this.#device.createBindGroup({
-                       layout: this.#bindGroupLayout,
-                       entries: [
-                               {
-                                       binding: 0,
-                                       resource: {
-                                               buffer: this.#uboBuffer
-                                       },
-                               },
-                               {
-                                       binding: 1,
-                                       resource: {
-                                               buffer: this.#gpuBuffer
-                                       },
-                               },
-                       ],
-               })
-
-               // Create command encoder to issue commands to GPU and initiate computation
-               const commandEncoder = this.#device.createCommandEncoder()
-               const passEncoder = commandEncoder.beginComputePass()
-
-               // Issue commands and end compute pass structure
-               passEncoder.setPipeline(this.#pipeline)
-               passEncoder.setBindGroup(0, bindGroup)
-               passEncoder.dispatchWorkgroups(1)
-               passEncoder.end()
-
-               // Copy 8-byte nonce and 4-byte found flag from GPU to CPU for reading
-               commandEncoder.copyBufferToBuffer(this.#gpuBuffer, 0, this.#cpuBuffer, 0, 12)
-
-               // End computation by passing array of command buffers to command queue for execution
-               this.#device.queue.submit([commandEncoder.finish()])
-
-               // Read results back to Javascript and then unmap buffer after reading
-               let data = null
-               try {
-                       await this.#cpuBuffer.mapAsync(GPUMapMode.READ)
-                       await this.#device.queue.onSubmittedWorkDone()
-                       data = new DataView(this.#cpuBuffer.getMappedRange().slice(0))
-                       this.#cpuBuffer.unmap()
-               } catch (err) {
-                       console.warn(`Error getting data from GPU. ${err}`)
-                       return this.validate(work, hash, threshold)
-               }
-               if (data == null) throw new Error(`Failed to get data from buffer.`)
-
+               const data = await this.#dispatch(BigInt(`0x${work}`), hash, threshold, 1)
                const nonce = data.getBigUint64(0, true).toString(16).padStart(16, '0')
                const found = !!data.getUint32(8)
                this.#busy = false
index 9cc20dc92f1d762ee66b302485e5c66cd2cc87c0..ffe3bca2764c46955113845ca648b35aec92f6e9 100644 (file)
--- a/test.html
+++ b/test.html
@@ -9,7 +9,7 @@ SPDX-License-Identifier: GPL-3.0-or-later
        <link rel="icon" href="./favicon.ico">
        <script type="module" src="../dist/global.min.js"></script>
        <!-- Comment out nano-pow from CDN if testing locally -->
-       <script type="module" src="https://cdn.jsdelivr.net/npm/nano-pow@0.0.1/dist/global.min.js"></script>
+       <!-- <script type="module" src="https://cdn.jsdelivr.net/npm/nano-pow@0.0.1/dist/global.min.js"></script> -->
        <script type="module" src="https://cdn.jsdelivr.net/npm/nano-webgl-pow@1.1.1/nano-webgl-pow.js"></script>
        <script type="module">
                const { NanoPowGl, NanoPowGpu } = NanoPow
@@ -59,6 +59,12 @@ SPDX-License-Identifier: GPL-3.0-or-later
                        console.log(`Geometric Mean: ${geometric} ms`)
                }
 
+               console.log(`%cNanoWebGpu`, 'color:green', 'Testing validation function')
+               const expectBad = await NanoPowGpu.validate('0000000000000000', '0000000000000000000000000000000000000000000000000000000000000000')
+               console.log(`validate() output for bad nonce is ${expectBad === false ? 'incorrect' : 'correct'}`)
+               const expectGood = await NanoPowGpu.validate('47c83266398728cf', '92BA74A7D6DC7557F3EDA95ADC6341D51AC777A0A6FF0688A5C492AB2B2CB40D')
+               console.log(`validate() output for good nonce is ${expectGood === true ? 'correct' : 'incorrect'}`)
+
                console.log(`%cNanoPowGpu `, 'color:green', `Calculate proof-of-work for ${COUNT} unique send block hashes`)
                times = []
                for (let i = 0; i < COUNT; i++) {