]> git.codecow.com Git - nano-pow.git/commitdiff
Create Cache class and implement at top level of work_generate call chain.
authorChris Duncan <chris@zoso.dev>
Wed, 18 Jun 2025 20:47:08 +0000 (13:47 -0700)
committerChris Duncan <chris@zoso.dev>
Wed, 18 Jun 2025 20:47:08 +0000 (13:47 -0700)
src/lib/index.ts
src/utils/cache.ts [new file with mode: 0644]
src/utils/index.ts

index c78370544c7fe13018c62b0c437051f2ae694121..12a628f4e0c5520de5c50dedd02bd86982d26535 100644 (file)
@@ -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<W
        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) {
@@ -35,8 +37,10 @@ export async function work_generate (hash: unknown, options: unknown): Promise<W
 
 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 ?? '')) }
diff --git a/src/utils/cache.ts b/src/utils/cache.ts
new file mode 100644 (file)
index 0000000..35ee12c
--- /dev/null
@@ -0,0 +1,39 @@
+//! 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
+       }
+}
index 38f3f86ee8e4bcd4d6ff78b37c64d54db5e55ef6..fa4cc8a236cc4b1e71dfec8b275a6b12d825dcd1 100644 (file)
@@ -3,6 +3,7 @@
 
 export * from './api-support'
 export * from './bigint'
+export * from './cache'
 export * from './logger'
 export * from './queue'