import { NanoPowValidate } from '#lib/validate'
import { NanoPowCpu, NanoPowWasm, NanoPowWebgl, NanoPowWebgpu } from '#lib/generate'
import { WorkErrorResponse, WorkGenerateResponse, WorkValidateResponse } from '#types'
-import { bigintFrom, Queue } from '#utils'
+import { bigintFrom, Cache, Queue } from '#utils'
import { NanoPowConfig } from '#lib/config'
const q = new Queue()
return q.add(async (): Promise<WorkGenerateResponse | WorkErrorResponse> => {
try {
const { api, debug, difficulty, effort } = await NanoPowConfig(options)
+ const cached = Cache.search(hash, difficulty)
+ if (cached) return cached
switch (api) {
case 'webgpu': {
- return NanoPowWebgpu(bigintFrom(hash, 'hex'), difficulty, effort, debug)
+ return Cache.store(await NanoPowWebgpu(bigintFrom(hash, 'hex'), difficulty, effort, debug))
}
case 'webgl': {
- return NanoPowWebgl(bigintFrom(hash, 'hex'), difficulty, effort, debug)
+ return Cache.store(await NanoPowWebgl(bigintFrom(hash, 'hex'), difficulty, effort, debug))
}
case 'wasm': {
- return NanoPowWasm(bigintFrom(hash, 'hex'), difficulty, effort, debug)
+ return Cache.store(await NanoPowWasm(bigintFrom(hash, 'hex'), difficulty, effort, debug))
}
default: {
- return NanoPowCpu(bigintFrom(hash, 'hex'), difficulty, debug)
+ return Cache.store(await NanoPowCpu(bigintFrom(hash, 'hex'), difficulty, debug))
}
}
} catch (e: any) {
export async function work_validate (work: unknown, hash: unknown, options: unknown): Promise<WorkValidateResponse | WorkErrorResponse> {
try {
+ const bigintHash = bigintFrom(hash, 'hex')
+ const bigintWork = bigintFrom(work, 'hex')
const { debug, difficulty } = await NanoPowConfig(options)
- const result = await NanoPowValidate(bigintFrom(work, 'hex'), bigintFrom(hash, 'hex'), difficulty, debug)
+ const result = await NanoPowValidate(bigintWork, bigintHash, difficulty, debug)
return result
} catch (e: any) {
return { error: (typeof e === 'string' ? e : (e?.message ?? '')) }
--- /dev/null
+//! SPDX-FileCopyrightText: 2025 Chris Duncan <chris@zoso.dev>
+//! SPDX-License-Identifier: GPL-3.0-or-later
+
+import { WorkGenerateResponse } from "#types"
+import { bigintFrom } from "#utils"
+
+export class Cache {
+ static #storage: { [key: string]: string } = {}
+ static #getItem (key: string): string | null {
+ if (globalThis?.localStorage) return globalThis.localStorage.getItem(key)
+ return this.#storage[key]
+ }
+ static #setItem (key: string, item: string): void {
+ if (globalThis?.localStorage) return globalThis.localStorage.setItem(key, item)
+ this.#storage[key] = item
+ }
+
+ static search (hash: unknown, difficulty: bigint): WorkGenerateResponse | null {
+ const bigintHash = bigintFrom(hash, 'hex')
+ const item = this.#getItem('NanoPowCache')
+ if (item) {
+ const cache = JSON.parse(item) as WorkGenerateResponse[]
+ for (const c of cache) {
+ if (bigintFrom(c.hash, 'hex') === bigintHash && bigintFrom(c.difficulty, 'hex') >= difficulty) {
+ return c
+ }
+ }
+ }
+ return null
+ }
+
+ static store (result: WorkGenerateResponse): WorkGenerateResponse {
+ const item = this.#getItem('NanoPowCache')
+ const cache = JSON.parse(item ?? '[]') as WorkGenerateResponse[]
+ cache.push(result)
+ this.#setItem('NanoPowCache', JSON.stringify(cache))
+ return result
+ }
+}