From: Chris Duncan Date: Thu, 21 May 2026 17:45:54 +0000 (-0700) Subject: Delete invalid work from cache if it was somehow stored. X-Git-Tag: v5.1.15~1^2~8 X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=f5ea3ffe497f2867854f306f1103ade3e875ed56;p=nano-pow.git Delete invalid work from cache if it was somehow stored. --- 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')