From 0b90a7fe4004470a843403bf284ca4f5c9b3e335 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Wed, 18 Jun 2025 13:47:08 -0700 Subject: [PATCH] Create Cache class and implement at top level of work_generate call chain. --- src/lib/index.ts | 16 ++++++++++------ src/utils/cache.ts | 39 +++++++++++++++++++++++++++++++++++++++ src/utils/index.ts | 1 + 3 files changed, 50 insertions(+), 6 deletions(-) create mode 100644 src/utils/cache.ts diff --git a/src/lib/index.ts b/src/lib/index.ts index c783705..12a628f 100644 --- a/src/lib/index.ts +++ b/src/lib/index.ts @@ -4,7 +4,7 @@ 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() @@ -13,18 +13,20 @@ export async function work_generate (hash: unknown, options: unknown): Promise => { 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) { @@ -35,8 +37,10 @@ export async function work_generate (hash: unknown, options: unknown): Promise { 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 ?? '')) } diff --git a/src/utils/cache.ts b/src/utils/cache.ts new file mode 100644 index 0000000..35ee12c --- /dev/null +++ b/src/utils/cache.ts @@ -0,0 +1,39 @@ +//! SPDX-FileCopyrightText: 2025 Chris Duncan +//! 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 + } +} diff --git a/src/utils/index.ts b/src/utils/index.ts index 38f3f86..fa4cc8a 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -3,6 +3,7 @@ export * from './api-support' export * from './bigint' +export * from './cache' export * from './logger' export * from './queue' -- 2.47.3