From d59afee59e3ecd5a28bdacc01aff528992f0e6c8 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Fri, 13 Dec 2024 20:56:52 -0800 Subject: [PATCH] Use number only for threshold. --- src/lib/constants.ts | 4 ++-- src/lib/workers/pow.ts | 46 +++++++++++++++++++----------------------- 2 files changed, 23 insertions(+), 27 deletions(-) diff --git a/src/lib/constants.ts b/src/lib/constants.ts index 16a7f01..6e6316c 100644 --- a/src/lib/constants.ts +++ b/src/lib/constants.ts @@ -16,8 +16,8 @@ export const PREFIX_LEGACY = 'xrb_' export const SEED_LENGTH_BIP44 = 128 export const SEED_LENGTH_BLAKE2B = 64 export const SLIP10_ED25519 = 'ed25519 seed' -export const THRESHOLD_RECEIVE = '0xfffffe' -export const THRESHOLD_SEND = '0xfffffff8' +export const THRESHOLD_RECEIVE = 0xfffffe +export const THRESHOLD_SEND = 0xfffffff8 export const XNO = 'Ó¾' export const LEDGER_STATUS_CODES: { [key: number]: string } = Object.freeze({ diff --git a/src/lib/workers/pow.ts b/src/lib/workers/pow.ts index 710b8d9..f567943 100644 --- a/src/lib/workers/pow.ts +++ b/src/lib/workers/pow.ts @@ -2,11 +2,7 @@ // SPDX-License-Identifier: GPL-3.0-or-later export class Pow { - static SEND_THRESHOLD = '0xfffffff8' - /** Used to set canvas size. Must be a multiple of 256. */ - static WORKLOAD: number = 256 * 4 - - static async find (hash: string, threshold: string = this.SEND_THRESHOLD): Promise { + static async find (hash: string, threshold: number = this.SEND_THRESHOLD): Promise { return new Promise(resolve => { this.calculate(hash, threshold, resolve) }) @@ -17,17 +13,11 @@ export class Pow { * Nano Currency Proof of Work Value generation using WebGL2 * Author: numtel * License: MIT - - * self.NanoWebglPow(hashHex, callback, progressCallback, threshold); - * @param hashHex String Previous Block Hash as Hex String - * @param callback Function Called when work value found - * Receives single string argument, work value as hex - * @param progressCallback Function Optional - * Receives single argument: n, number of frames so far - * Return true to abort - * @param threshold Number|String Optional difficulty threshold (default=0xFFFFFFF8 since v21) */ + static SEND_THRESHOLD = 0xFFFFFFF8 + /** Used to set canvas size. Must be a multiple of 256. */ + static WORKLOAD: number = 256 * 4 static gl = new OffscreenCanvas(this.WORKLOAD, this.WORKLOAD).getContext('webgl2') static work0 = new Uint8Array(4) static work1 = new Uint8Array(4) @@ -63,13 +53,6 @@ export class Pow { } } - /* - for (i = 0; i<12; i++) { - B2B_G(0, 8, 16, 24, SIGMA82[i * 16 + 0], SIGMA82[i * 16 + 1]); - -> - B2B_G(0, 8, 16, 24, SIGMA82[i * 16 + 0], SIGMA82[i * 16 + 1]); - */ - static hexify (arr: number[] | Uint8Array): string { let out = '' for (let i = arr.length - 1; i >= 0; i--) { @@ -77,9 +60,22 @@ export class Pow { } return out } + /* + for (i = 0; i<12; i++) { + B2B_G(0, 8, 16, 24, SIGMA82[i * 16 + 0], SIGMA82[i * 16 + 1]); + -> + B2B_G(0, 8, 16, 24, SIGMA82[i * 16 + 0], SIGMA82[i * 16 + 1]); + */ - static calculate (hashHex: string, threshold: number | string = '0xFFFFFFF8', callback: (nonce: string | PromiseLike) => any): void { - if (typeof threshold === 'number') threshold = '0x' + threshold.toString(16) + /** + * self.NanoWebglPow (hashHex, callback, progressCallback, threshold) + * @param {string} hashHex - Previous Block Hash as Hex String + * @param {string} threshold - Optional difficulty threshold(default=0xFFFFFFF8 since v21) + * @param {function} callback - Called when work value found + * Receives single string argument, work value as hex + */ + static calculate (hashHex: string, threshold: number = this.SEND_THRESHOLD, callback: (nonce: string | PromiseLike) => any): void { + if (typeof threshold !== 'number') throw new TypeError(`invalid_threshold ${threshold}`) if (!/^[A-F-a-f0-9]{64}$/.test(hashHex)) throw new Error(`invalid_hash ${hashHex}`) let reverseHex = '' for (let i = hashHex.length; i > 0; i -= 2) reverseHex += hashHex.slice(i - 2, i) @@ -255,7 +251,7 @@ export class Pow { // Threshold test, first 4 bytes not significant, // only calculate digest of the second 4 bytes - if((BLAKE2B_IV32_1 ^ v[1] ^ v[17]) > ` + threshold + `u) { + if((BLAKE2B_IV32_1 ^ v[1] ^ v[17]) > ${threshold}u) { // Success found, return pixel data so work value can be constructed fragColor = vec4( float(x_index + 1u)/255., // +1 to distinguish from 0 (unsuccessful) pixels @@ -337,7 +333,7 @@ export class Pow { this.gl.uniform4uiv(work0Location, this.work0) this.gl.uniform4uiv(work1Location, this.work1) - this.gl.uniform1uiv(blockHashLocation, new Uint32Array(blockHashComponents)) + this.gl.uniform1uiv(blockHashLocation, blockHashComponents) this.gl.clear(this.gl.COLOR_BUFFER_BIT) this.gl.drawArrays(this.gl.TRIANGLES, 0, 6) -- 2.34.1