]> git.codecow.com Git - nano-pow.git/commitdiff
Delete invalid work from cache if it was somehow stored.
authorChris Duncan <chris@codecow.com>
Thu, 21 May 2026 17:45:54 +0000 (10:45 -0700)
committerChris Duncan <chris@codecow.com>
Thu, 21 May 2026 17:45:54 +0000 (10:45 -0700)
src/lib/index.ts
src/utils/cache.ts

index 3deb723b55187ae0ef36d21c34a9a1bb54240026..bbc0dd5691188d4087a471364c48be40832ac7a2 100644 (file)
@@ -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': {
index 5d446f82a23ec4a52c6a662ad483e9a202151549..24d0e0eeb737603e14ab742748a5cbf3697f3ddb 100644 (file)
@@ -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')