From f5ea3ffe497f2867854f306f1103ade3e875ed56 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Thu, 21 May 2026 10:45:54 -0700 Subject: [PATCH] Delete invalid work from cache if it was somehow stored. --- src/lib/index.ts | 11 ++++++++++- src/utils/cache.ts | 13 +++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/lib/index.ts b/src/lib/index.ts index 3deb723..bbc0dd5 100644 --- a/src/lib/index.ts +++ b/src/lib/index.ts @@ -18,7 +18,16 @@ export async function work_generate (hash: unknown, options?: unknown): Promise< const cached = Cache.search(hash, difficulty) if (cached) { LOG: logger.log('found work in cache') - return cached + const { valid } = NanoPowValidate(bigintFrom(cached.work, 'hex'), bigintFrom(cached.hash, 'hex'), bigintFrom(cached.difficulty, 'hex'), debug) + if (valid === '1') { + return cached + } else { + try { + Cache.delete(hash) + } catch (err) { + LOG: logger.log('error deleting invalid work from cache, continuing with generate', err) + } + } } switch (api) { case 'webgpu': { diff --git a/src/utils/cache.ts b/src/utils/cache.ts index 5d446f8..24d0e0e 100644 --- a/src/utils/cache.ts +++ b/src/utils/cache.ts @@ -10,6 +10,19 @@ export class Cache { this.#removeItem('NanoPowCache') } + static delete (hash: unknown): void { + const bigintHash = bigintFrom(hash, 'hex') + const item = this.#getItem('NanoPowCache') + if (item == null) return + const cache = JSON.parse(item) as WorkGenerateResponse[] + for (let i = 0; i < cache.length; i++) { + if (bigintFrom(cache[i].hash, 'hex') === bigintHash) { + cache.splice(i, 1) + } + } + this.#setItem('NanoPowCache', JSON.stringify(cache)) + } + static search (hash: unknown, difficulty: bigint): WorkGenerateResponse | null { const bigintHash = bigintFrom(hash, 'hex') const item = this.#getItem('NanoPowCache') -- 2.52.0