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': {
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')