nano-rspow-web 0.16.0 → 0.17.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -32,6 +32,13 @@ console.log("Is GPU:", result.is_gpu); // true/false
32
32
  const isValid = validate_work(hash, result.nonce, threshold); // true
33
33
  ```
34
34
 
35
+ The threshold argument is always caller supplied. For current Nano mainnet,
36
+ use `fffffff800000000` for send/change blocks or `fffffe0000000000` for
37
+ receive/open/epoch blocks. Pass the stricter value required by the publishing
38
+ node when it differs. See Nano's
39
+ [Work Generation guide](https://docs.nano.org/integration-guides/work-generation/)
40
+ for current network requirements.
41
+
35
42
  ## Benchmarking Dashboard
36
43
 
37
44
  <table>
@@ -66,20 +73,27 @@ Initializes the asynchronous WASM loader. Await this default export before calli
66
73
  Initializes the module synchronously from bytes or a precompiled `WebAssembly.Module`. Use this only when the module bytes are already available.
67
74
 
68
75
  ### `generate_work(hash_hex: string, threshold_hex: string): Promise<GenerateResult>`
69
- Asynchronously generates Proof of Work for a 32-byte block hash. Tries WebGPU first, falling back to CPU WebAssembly.
76
+ Asynchronously generates Proof of Work for a 32-byte block hash at any hexadecimal threshold. Tries WebGPU first, falling back to CPU WebAssembly.
70
77
 
71
78
  ### `generate_work_gpu(hash_hex: string, threshold_hex: string, cancel_token: WasmCancelToken): Promise<GenerateResult>`
72
- Forces WebGPU PoW generation. Rejects if WebGPU is unavailable or if the cancellation token is triggered.
79
+ Forces WebGPU PoW generation at any hexadecimal threshold. Rejects if WebGPU is unavailable or if the cancellation token is triggered.
73
80
 
74
81
  ### `generate_work_cpu(hash_hex: string, threshold_hex: string): GenerateResult`
75
- Synchronously generates PoW forcing single-threaded CPU WebAssembly.
82
+ Synchronously generates PoW at any hexadecimal threshold, forcing single-threaded CPU WebAssembly.
76
83
 
77
84
  ### `generate_work_cpu_batch(hash_hex: string, threshold_hex: string, max_nonces: number): string | null`
78
85
 
79
86
  Tests at most `max_nonces` nonces synchronously on the CPU. Returns a lowercase 16-character nonce when found, otherwise `null`. The CPU random-number-generator state persists between calls. Pass an unsigned 32-bit integer.
80
87
 
88
+ ### `recommendLocalPow(reprobe = false): Promise<boolean>`
89
+
90
+ Returns whether this browser should prefer local PoW. On a cache miss it tests
91
+ WebGPU capability, or the single-threaded WASM CPU fallback's throughput. The
92
+ result is cached internally. Pass `true` to force a fresh evaluation after a
93
+ hardware or browser capability change.
94
+
81
95
  ### `validate_work(hash_hex: string, nonce_hex: string, threshold_hex: string): boolean`
82
- Synchronously validates whether a nonce meets the difficulty threshold for the given block hash.
96
+ Synchronously validates whether a nonce meets an arbitrary hexadecimal threshold for the given block hash.
83
97
 
84
98
  ### `WasmCancelToken`
85
99
  A class utilized to signal cancellation to the asynchronous GPU solver loop.
package/index.d.ts CHANGED
@@ -1,3 +1,3 @@
1
1
  export { default } from './nano_rspow_web.js';
2
2
  export * from './nano_rspow_web.js';
3
- export { createPowEngine } from './pow-engine.js';
3
+ export { createPowEngine, recommendLocalPow } from './pow-engine.js';
package/index.js CHANGED
@@ -1,3 +1,3 @@
1
1
  export { default } from './nano_rspow_web.js'
2
2
  export * from './nano_rspow_web.js'
3
- export { createPowEngine } from './pow-engine.js'
3
+ export { createPowEngine, recommendLocalPow } from './pow-engine.js'
@@ -21,26 +21,30 @@ export class WasmCancelToken {
21
21
  }
22
22
 
23
23
  /**
24
- * Asynchronously generate Proof of Work for a 32-byte block hash (hex).
24
+ * Asynchronously generate Proof of Work for a 32-byte block hash and an
25
+ * arbitrary hexadecimal threshold.
25
26
  *
26
27
  * Tries WebGPU first, then falls back to single-threaded CPU WASM.
27
28
  */
28
29
  export function generate_work(hash_hex: string, threshold_hex: string): Promise<GenerateResult>;
29
30
 
30
31
  /**
31
- * Synchronously generate Proof of Work forcing single-threaded WASM CPU execution.
32
+ * Synchronously generate Proof of Work at an arbitrary threshold, forcing
33
+ * single-threaded WASM CPU execution.
32
34
  */
33
35
  export function generate_work_cpu(hash_hex: string, threshold_hex: string): GenerateResult;
34
36
 
35
37
  /**
36
- * Try up to max_nonces nonces in a single synchronous batch.
38
+ * Try up to `max_nonces` nonces at an arbitrary hexadecimal threshold in a
39
+ * single synchronous batch.
37
40
  * Returns the nonce as a hex string if found, or `null` if the batch
38
41
  * was exhausted. RNG state persists across calls.
39
42
  */
40
43
  export function generate_work_cpu_batch(hash_hex: string, threshold_hex: string, max_nonces: number): any;
41
44
 
42
45
  /**
43
- * Asynchronously generate Proof of Work forcing WebGPU execution.
46
+ * Asynchronously generate Proof of Work at an arbitrary threshold, forcing
47
+ * WebGPU execution.
44
48
  *
45
49
  * Pass a `WasmCancelToken` created via `new WasmCancelToken()` and call
46
50
  * `.cancel()` on it from JavaScript to abort the GPU batch loop (e.g. on timeout).
@@ -48,7 +52,16 @@ export function generate_work_cpu_batch(hash_hex: string, threshold_hex: string,
48
52
  export function generate_work_gpu(hash_hex: string, threshold_hex: string, cancel_token: WasmCancelToken): Promise<GenerateResult>;
49
53
 
50
54
  /**
51
- * Synchronously validate if a nonce meets the difficulty threshold for a given block hash.
55
+ * Measure whether this browser can reasonably perform local proof-of-work.
56
+ *
57
+ * A usable WebGPU pipeline is always recommended. Without WebGPU, the
58
+ * single-threaded WASM CPU fallback must sustain the same 15 MH/s threshold
59
+ * used by the native recommendation before it is recommended.
60
+ */
61
+ export function probe_local_pow(): Promise<boolean>;
62
+
63
+ /**
64
+ * Synchronously validate a nonce against an arbitrary threshold for a block hash.
52
65
  */
53
66
  export function validate_work(hash_hex: string, nonce_hex: string, threshold_hex: string): boolean;
54
67
 
@@ -64,6 +77,7 @@ export interface InitOutput {
64
77
  readonly generate_work_gpu: (a: number, b: number, c: number, d: number, e: number) => any;
65
78
  readonly generateresult_is_gpu: (a: number) => number;
66
79
  readonly generateresult_nonce: (a: number) => [number, number];
80
+ readonly probe_local_pow: () => any;
67
81
  readonly validate_work: (a: number, b: number, c: number, d: number, e: number, f: number) => [number, number, number];
68
82
  readonly wasmcanceltoken_cancel: (a: number) => void;
69
83
  readonly wasmcanceltoken_new: () => number;
package/nano_rspow_web.js CHANGED
@@ -70,7 +70,8 @@ export class WasmCancelToken {
70
70
  if (Symbol.dispose) WasmCancelToken.prototype[Symbol.dispose] = WasmCancelToken.prototype.free;
71
71
 
72
72
  /**
73
- * Asynchronously generate Proof of Work for a 32-byte block hash (hex).
73
+ * Asynchronously generate Proof of Work for a 32-byte block hash and an
74
+ * arbitrary hexadecimal threshold.
74
75
  *
75
76
  * Tries WebGPU first, then falls back to single-threaded CPU WASM.
76
77
  * @param {string} hash_hex
@@ -87,7 +88,8 @@ export function generate_work(hash_hex, threshold_hex) {
87
88
  }
88
89
 
89
90
  /**
90
- * Synchronously generate Proof of Work forcing single-threaded WASM CPU execution.
91
+ * Synchronously generate Proof of Work at an arbitrary threshold, forcing
92
+ * single-threaded WASM CPU execution.
91
93
  * @param {string} hash_hex
92
94
  * @param {string} threshold_hex
93
95
  * @returns {GenerateResult}
@@ -105,7 +107,8 @@ export function generate_work_cpu(hash_hex, threshold_hex) {
105
107
  }
106
108
 
107
109
  /**
108
- * Try up to max_nonces nonces in a single synchronous batch.
110
+ * Try up to `max_nonces` nonces at an arbitrary hexadecimal threshold in a
111
+ * single synchronous batch.
109
112
  * Returns the nonce as a hex string if found, or `null` if the batch
110
113
  * was exhausted. RNG state persists across calls.
111
114
  * @param {string} hash_hex
@@ -126,7 +129,8 @@ export function generate_work_cpu_batch(hash_hex, threshold_hex, max_nonces) {
126
129
  }
127
130
 
128
131
  /**
129
- * Asynchronously generate Proof of Work forcing WebGPU execution.
132
+ * Asynchronously generate Proof of Work at an arbitrary threshold, forcing
133
+ * WebGPU execution.
130
134
  *
131
135
  * Pass a `WasmCancelToken` created via `new WasmCancelToken()` and call
132
136
  * `.cancel()` on it from JavaScript to abort the GPU batch loop (e.g. on timeout).
@@ -146,7 +150,20 @@ export function generate_work_gpu(hash_hex, threshold_hex, cancel_token) {
146
150
  }
147
151
 
148
152
  /**
149
- * Synchronously validate if a nonce meets the difficulty threshold for a given block hash.
153
+ * Measure whether this browser can reasonably perform local proof-of-work.
154
+ *
155
+ * A usable WebGPU pipeline is always recommended. Without WebGPU, the
156
+ * single-threaded WASM CPU fallback must sustain the same 15 MH/s threshold
157
+ * used by the native recommendation before it is recommended.
158
+ * @returns {Promise<boolean>}
159
+ */
160
+ export function probe_local_pow() {
161
+ const ret = wasm.probe_local_pow();
162
+ return ret;
163
+ }
164
+
165
+ /**
166
+ * Synchronously validate a nonce against an arbitrary threshold for a block hash.
150
167
  * @param {string} hash_hex
151
168
  * @param {string} nonce_hex
152
169
  * @param {string} threshold_hex
@@ -366,6 +383,10 @@ function __wbg_get_imports() {
366
383
  const ret = new Uint8Array(arg0, arg1 >>> 0, arg2 >>> 0);
367
384
  return ret;
368
385
  },
386
+ __wbg_now_81363d44c96dd239: function() {
387
+ const ret = Date.now();
388
+ return ret;
389
+ },
369
390
  __wbg_onSubmittedWorkDone_5f36409816d68e04: function(arg0) {
370
391
  const ret = arg0.onSubmittedWorkDone();
371
392
  return ret;
@@ -607,13 +628,13 @@ function __wbg_get_imports() {
607
628
  arg0.writeBuffer(arg1, arg2, getArrayU8FromWasm0(arg3, arg4), arg5, arg6);
608
629
  }, arguments); },
609
630
  __wbindgen_cast_0000000000000001: function(arg0, arg1) {
610
- // Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [Externref], shim_idx: 65, ret: Unit, inner_ret: Some(Unit) }, mutable: true }) -> Externref`.
611
- const ret = makeMutClosure(arg0, arg1, wasm_bindgen_8b74beb86e9b2264___convert__closures_____invoke___wasm_bindgen_8b74beb86e9b2264___JsValue______true_);
631
+ // Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [Externref], shim_idx: 103, ret: Result(Unit), inner_ret: Some(Result(Unit)) }, mutable: true }) -> Externref`.
632
+ const ret = makeMutClosure(arg0, arg1, wasm_bindgen_8b74beb86e9b2264___convert__closures_____invoke___wasm_bindgen_8b74beb86e9b2264___JsValue__core_f0fd674eaa06beef___result__Result_____wasm_bindgen_8b74beb86e9b2264___JsError___true_);
612
633
  return ret;
613
634
  },
614
635
  __wbindgen_cast_0000000000000002: function(arg0, arg1) {
615
- // Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [Externref], shim_idx: 99, ret: Result(Unit), inner_ret: Some(Result(Unit)) }, mutable: true }) -> Externref`.
616
- const ret = makeMutClosure(arg0, arg1, wasm_bindgen_8b74beb86e9b2264___convert__closures_____invoke___wasm_bindgen_8b74beb86e9b2264___JsValue__core_f0fd674eaa06beef___result__Result_____wasm_bindgen_8b74beb86e9b2264___JsError___true_);
636
+ // Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [Externref], shim_idx: 69, ret: Unit, inner_ret: Some(Unit) }, mutable: true }) -> Externref`.
637
+ const ret = makeMutClosure(arg0, arg1, wasm_bindgen_8b74beb86e9b2264___convert__closures_____invoke___wasm_bindgen_8b74beb86e9b2264___JsValue______true_);
617
638
  return ret;
618
639
  },
619
640
  __wbindgen_cast_0000000000000003: function(arg0) {
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nano-rspow-web",
3
- "version": "0.16.0",
3
+ "version": "0.17.1",
4
4
  "type": "module",
5
5
  "description": "WebGPU-accelerated Nano (XNO) Proof of Work generation in the browser using WebAssembly",
6
6
  "keywords": [
@@ -29,10 +29,12 @@
29
29
  "index.d.ts",
30
30
  "pow-engine.js",
31
31
  "pow-engine.d.ts",
32
+ "recommendation.js",
32
33
  "README.md"
33
34
  ],
34
35
  "scripts": {
35
- "build": "cargo build -p nano-rspow-web --target wasm32-unknown-unknown --release && wasm-bindgen --target web --out-dir . ../target/wasm32-unknown-unknown/release/nano_rspow_web.wasm"
36
+ "build": "cargo build -p nano-rspow-web --target wasm32-unknown-unknown --release && wasm-bindgen --target web --out-dir . ../target/wasm32-unknown-unknown/release/nano_rspow_web.wasm",
37
+ "test": "node --test recommendation.test.js"
36
38
  },
37
39
  "dependencies": {
38
40
  "@openrai/nano-pow-contract": "^0.1.0"
package/pow-engine.d.ts CHANGED
@@ -2,3 +2,11 @@ import type { PowEngine } from '@openrai/nano-pow-contract';
2
2
 
3
3
  /** Create the nano-pow-contract adapter for the WebAssembly/WebGPU engine. */
4
4
  export declare function createPowEngine(): PowEngine;
5
+
6
+ /**
7
+ * Return whether this browser should prefer local PoW.
8
+ *
9
+ * The first call performs a capability and CPU-throughput evaluation. Later
10
+ * calls return its cached result unless `reprobe` is `true`.
11
+ */
12
+ export declare function recommendLocalPow(reprobe?: boolean): Promise<boolean>;
package/pow-engine.js CHANGED
@@ -1,4 +1,5 @@
1
- import init, { generate_work, validate_work } from './nano_rspow_web.js'
1
+ import init, { generate_work, probe_local_pow, validate_work } from './nano_rspow_web.js'
2
+ import { createLocalPowRecommendation } from './recommendation.js'
2
3
 
3
4
  let initialization
4
5
 
@@ -7,6 +8,15 @@ function ready () {
7
8
  return initialization
8
9
  }
9
10
 
11
+ /**
12
+ * Return whether this browser should prefer local PoW. The result is cached;
13
+ * pass `true` to force a fresh browser capability and performance probe.
14
+ */
15
+ export const recommendLocalPow = createLocalPowRecommendation(async () => {
16
+ await ready()
17
+ return probe_local_pow()
18
+ })
19
+
10
20
  /** Create the nano-pow-contract adapter for the WebAssembly/WebGPU engine. */
11
21
  export function createPowEngine () {
12
22
  return {
@@ -0,0 +1,72 @@
1
+ const CACHE_KEY = 'nano-rspow-web.local-pow-recommendation.v1'
2
+
3
+ function browserStorage () {
4
+ try {
5
+ return globalThis.localStorage ?? null
6
+ } catch {
7
+ return null
8
+ }
9
+ }
10
+
11
+ /**
12
+ * Cache an asynchronous local-PoW recommendation without exposing browser
13
+ * storage details to consumers.
14
+ *
15
+ * @param {() => Promise<boolean>} probe runs the actual capability check
16
+ * @param {{ storage?: Storage | null }} [options] testable browser storage override
17
+ * @returns {(reprobe?: boolean) => Promise<boolean>} cached recommendation function
18
+ */
19
+ export function createLocalPowRecommendation (probe, { storage = browserStorage() } = {}) {
20
+ let cacheRead = false
21
+ let cachedRecommendation
22
+ let activeProbe
23
+ let generation = 0
24
+
25
+ function readCache () {
26
+ if (cacheRead) return
27
+ cacheRead = true
28
+
29
+ try {
30
+ const value = storage?.getItem(CACHE_KEY)
31
+ if (value === 'true') cachedRecommendation = true
32
+ if (value === 'false') cachedRecommendation = false
33
+ } catch {
34
+ // Storage is optional: a private-browser or quota failure falls back to memory.
35
+ }
36
+ }
37
+
38
+ function writeCache (recommendation) {
39
+ try {
40
+ storage?.setItem(CACHE_KEY, String(recommendation))
41
+ } catch {
42
+ // The in-memory result remains valid for this page session.
43
+ }
44
+ }
45
+
46
+ return function recommendLocalPow (reprobe = false) {
47
+ readCache()
48
+
49
+ if (!reprobe && cachedRecommendation !== undefined) {
50
+ return Promise.resolve(cachedRecommendation)
51
+ }
52
+
53
+ if (!reprobe && activeProbe) return activeProbe.promise
54
+
55
+ const probeGeneration = ++generation
56
+ const promise = Promise.resolve()
57
+ .then(probe)
58
+ .then(recommendation => {
59
+ if (probeGeneration === generation) {
60
+ cachedRecommendation = recommendation
61
+ writeCache(recommendation)
62
+ }
63
+ return recommendation
64
+ })
65
+ .finally(() => {
66
+ if (activeProbe?.generation === probeGeneration) activeProbe = undefined
67
+ })
68
+
69
+ activeProbe = { generation: probeGeneration, promise }
70
+ return promise
71
+ }
72
+ }