nano-rspow-web 0.17.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 +7 -0
- package/index.d.ts +1 -1
- package/index.js +1 -1
- package/nano_rspow_web.d.ts +10 -0
- package/nano_rspow_web.js +21 -4
- package/nano_rspow_web_bg.wasm +0 -0
- package/package.json +4 -2
- package/pow-engine.d.ts +8 -0
- package/pow-engine.js +11 -1
- package/recommendation.js +72 -0
package/README.md
CHANGED
|
@@ -85,6 +85,13 @@ Synchronously generates PoW at any hexadecimal threshold, forcing single-threade
|
|
|
85
85
|
|
|
86
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.
|
|
87
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
|
+
|
|
88
95
|
### `validate_work(hash_hex: string, nonce_hex: string, threshold_hex: string): boolean`
|
|
89
96
|
Synchronously validates whether a nonce meets an arbitrary hexadecimal threshold for the given block hash.
|
|
90
97
|
|
package/index.d.ts
CHANGED
package/index.js
CHANGED
package/nano_rspow_web.d.ts
CHANGED
|
@@ -51,6 +51,15 @@ export function generate_work_cpu_batch(hash_hex: string, threshold_hex: string,
|
|
|
51
51
|
*/
|
|
52
52
|
export function generate_work_gpu(hash_hex: string, threshold_hex: string, cancel_token: WasmCancelToken): Promise<GenerateResult>;
|
|
53
53
|
|
|
54
|
+
/**
|
|
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
|
+
|
|
54
63
|
/**
|
|
55
64
|
* Synchronously validate a nonce against an arbitrary threshold for a block hash.
|
|
56
65
|
*/
|
|
@@ -68,6 +77,7 @@ export interface InitOutput {
|
|
|
68
77
|
readonly generate_work_gpu: (a: number, b: number, c: number, d: number, e: number) => any;
|
|
69
78
|
readonly generateresult_is_gpu: (a: number) => number;
|
|
70
79
|
readonly generateresult_nonce: (a: number) => [number, number];
|
|
80
|
+
readonly probe_local_pow: () => any;
|
|
71
81
|
readonly validate_work: (a: number, b: number, c: number, d: number, e: number, f: number) => [number, number, number];
|
|
72
82
|
readonly wasmcanceltoken_cancel: (a: number) => void;
|
|
73
83
|
readonly wasmcanceltoken_new: () => number;
|
package/nano_rspow_web.js
CHANGED
|
@@ -149,6 +149,19 @@ export function generate_work_gpu(hash_hex, threshold_hex, cancel_token) {
|
|
|
149
149
|
return ret;
|
|
150
150
|
}
|
|
151
151
|
|
|
152
|
+
/**
|
|
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
|
+
|
|
152
165
|
/**
|
|
153
166
|
* Synchronously validate a nonce against an arbitrary threshold for a block hash.
|
|
154
167
|
* @param {string} hash_hex
|
|
@@ -370,6 +383,10 @@ function __wbg_get_imports() {
|
|
|
370
383
|
const ret = new Uint8Array(arg0, arg1 >>> 0, arg2 >>> 0);
|
|
371
384
|
return ret;
|
|
372
385
|
},
|
|
386
|
+
__wbg_now_81363d44c96dd239: function() {
|
|
387
|
+
const ret = Date.now();
|
|
388
|
+
return ret;
|
|
389
|
+
},
|
|
373
390
|
__wbg_onSubmittedWorkDone_5f36409816d68e04: function(arg0) {
|
|
374
391
|
const ret = arg0.onSubmittedWorkDone();
|
|
375
392
|
return ret;
|
|
@@ -611,13 +628,13 @@ function __wbg_get_imports() {
|
|
|
611
628
|
arg0.writeBuffer(arg1, arg2, getArrayU8FromWasm0(arg3, arg4), arg5, arg6);
|
|
612
629
|
}, arguments); },
|
|
613
630
|
__wbindgen_cast_0000000000000001: function(arg0, arg1) {
|
|
614
|
-
// Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [Externref], shim_idx:
|
|
615
|
-
const ret = makeMutClosure(arg0, arg1,
|
|
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_);
|
|
616
633
|
return ret;
|
|
617
634
|
},
|
|
618
635
|
__wbindgen_cast_0000000000000002: function(arg0, arg1) {
|
|
619
|
-
// Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [Externref], shim_idx:
|
|
620
|
-
const ret = makeMutClosure(arg0, arg1,
|
|
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_);
|
|
621
638
|
return ret;
|
|
622
639
|
},
|
|
623
640
|
__wbindgen_cast_0000000000000003: function(arg0) {
|
package/nano_rspow_web_bg.wasm
CHANGED
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nano-rspow-web",
|
|
3
|
-
"version": "0.17.
|
|
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
|
+
}
|