]> zoso.dev Git - libnemo.git/commitdiff
Use number only for threshold.
authorChris Duncan <chris@zoso.dev>
Sat, 14 Dec 2024 04:56:52 +0000 (20:56 -0800)
committerChris Duncan <chris@zoso.dev>
Sat, 14 Dec 2024 04:56:52 +0000 (20:56 -0800)
src/lib/constants.ts
src/lib/workers/pow.ts

index 16a7f01cf926707c6036c1b01b4f136b138903c6..6e6316c38a9d9834aa75588b6b3831c344f5202f 100644 (file)
@@ -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({
index 710b8d9c3fbd7c7dd095a07da8f1afb79b13ee38..f567943ff9a9aa76fe407cbca06a51ebd2f9ca9e 100644 (file)
@@ -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<string> {
+       static async find (hash: string, threshold: number = this.SEND_THRESHOLD): Promise<string> {
                return new Promise<string>(resolve => {
                        this.calculate(hash, threshold, resolve)
                })
@@ -17,17 +13,11 @@ export class Pow {
        * Nano Currency Proof of Work Value generation using WebGL2
        * Author:  numtel <ben@latenightsketches.com>
        * 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<string>) => 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<string>) => 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)